Skip to content

Instantly share code, notes, and snippets.

@jpfuentes2
Last active December 26, 2015 15:59
Show Gist options
  • Save jpfuentes2/7177020 to your computer and use it in GitHub Desktop.
Save jpfuentes2/7177020 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
ip = `ip addr | grep eth0 | grep inet | awk '{print $2}' | cut -f1 -d"/"`
pid_file = "/var/run/serf.pid"
if File.exists? pid_file
pid = File.read pid_file
Process.kill "INT", pid rescue nil
end
pid = fork do
File.open(pid_file, "w+") { |f| f.write Process.pid.to_s }
exec "serf agent -role=app -bind=#{ip} -event-handler='user:deploy=/srv/app/deploy.rb'"
end
sleep 2
retries = 5
while retries > 0
serf_join_err = `serf join #{ENV["SERF_HOST"]}`
break if $?.exitstatus == 0
retries -= 1
end
if retries == 0
abort "Could not 'serf join' because #{serf_join_err}"
exit $?.exitstatus
else
Process.wait pid
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment