Hi Everyone,
I’m using mongrel as my rails server and my goal is to have tracks start automatically with OS X, having got this working I though I would share the details here in case some of you wish to do the same, this should also work on OS X Server.
I should point out that if you’re using apache there’s a rails module (mod_rails) available so you could use that, I haven’t looked at apache on OS X yet so don’t have it running, however that is another option.
I should also point out that I’m still learning about OS X and this is my first foray into the launchd technology so this may not be the most robust way of setting this up but it seems to work well.
If you don’t have Git installed you can get it here.
Ok, first we need tracks:
git clone—depth 1 git://github.com/bsag/tracks.git ~/tracks
If you don’t want the bleeding edge checkout a stable version:
cd ~/tracks
git checkout v1.6
Next setup the rails environment:
cd ~/tracks
cp ./config/environment.rb.tmpl ./config/environment.rb
sed -i -e ‘/^SALT/s/change-me/<your salt secret>/’ ./config/environment.rb
cp -r ./log.tmpl ./log
touch ./log/mongrel.log
echo -e "\
> production:\n\
> adapter: sqlite3\n\
> database: db/production.sqlite3\n\
> timeout: 5000" > ./config/database.yml
rake db:migrate RAILS_ENV=production
cd ~
Check it works, use Ctrl-C if it started Ok:
mongrel_rails start -e production -c ~/tracks
Create a group for mongrel based services using the next available gid:
(not sure why I created a mongrel group, seemed like a good idea at the time)
dscl . list groups PrimaryGroupID | awk ‘id<$2 {id=$2} END {print id+1}’
if the gid returned above was greater than 500, replace 500 below with it’s value.
sudo dseditgroup -o create -i 500 _mongrel
sudo dscl . create Groups/_mongrel RecordName _mongrel mongrel
Create a user for Tracks using the next available uid:
dscl . list users UniqueID | awk ‘id<$2 {id=$2} END {print id+1}’
If the uid returned above was greater than 500, replace 500 below with it’s value.
The PrimaryGroupID should be the gid you assigned to the _mongrel group above.
sudo dscl . create Users/_tracks
sudo dscl . create Users/_tracks RecordName _tracks tracks
sudo dscl . create Users/_tracks UniqueID 500
sudo dscl . create Users/_tracks PrimaryGroupID 502
sudo dscl . create Users/_tracks NFSHomeDirectory /var/empty
sudo dscl . create Users/_tracks UserShell /usr/bin/false
Move tracks into /opt and set owner:
sudo mv ~/tracks /opt/tracks
sudo chown -R tracks:mongrel /opt/tracks
Create the file ‘~/uk.org.rousette.tracks.plist’ with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>uk.org.rousette.tracks</string>
<key>KeepAlive</key>
<true/>
<key>RunAtLoad</key>
<true/>
<key>UserName</key>
<string>_tracks</string>
<key>WorkingDirectory</key>
<string>/opt/tracks</string>
<key>Program</key>
<string>/usr/bin/mongrel_rails</string>
<key>ProgramArguments</key>
<array>
<string>mongrel_rails</string>
<string>start</string>
<string>-eproduction</string>
[b]<string>-a127.0.0.1</string>[/b]
[b]<string>-p3000</string>[/b]
<string>-llog/mongrel.log</string>
</array>
</dict>
</plist>
You should probably set the port argument above to something other than 3000.
If you want to access Tracks across a network remove the address argument or set it to the machines network address.
Personally if I wanted to access tracks across the network I would probably use apache, either as a proxy to the mongrel daemon or using mod_rails.
Install the plist as a launch daemon:
sudo mv ~/uk.org.rousette.tracks.plist /Library/LaunchDaemons
sudo chown root:wheel /Library/LaunchDaemons/uk.org.rousette.tracks.plist
sudo chmod 644 /Library/LaunchDaemons/uk.org.rousette.tracks.plist
All that’s left is to launch:
To start the daemon:
sudo launchctl load /Library/LaunchDaemons/uk.org.rousette.tracks.plist
To stop the daemon:
sudo launchctl remove uk.org.rousette.tracks
To restart the daemon:
sudo launchctl stop uk.org.rousette.tracks
stop does stop the daemon but because we have KeepAlive set to true launchd simply restarts it, removing KeepAlive will allow you to use stop/start instead of load/remove.
You should now be able to access Tracks from your browser at the port you’ve specified and this should remain the case after reboot, if that’s not the case take a look at the Console application and the mongrel log for errors.
When you want to update to the latest code cd to the /opt/tracks folder and do one of the following:
sudo git pull
or
sudo git pull—tags
If you’re not running the bleeding edge code don’t forget to checkout the changeset or tag you want.
Happy tracking ![]()


