Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

thin install - systemD support #184

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 48 additions & 28 deletions lib/thin/controllers/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,66 +6,86 @@ module Controllers
# config files are in a directory.
class Service < Controller
INITD_PATH = File.directory?('/etc/rc.d') ? '/etc/rc.d/thin' : '/etc/init.d/thin'
SYSTEMD_PATH = '/usr/lib/systemd/system/thin.service'
DEFAULT_CONFIG_PATH = '/etc/thin'
TEMPLATE = File.dirname(__FILE__) + '/service.sh.erb'
SYSTEMD = File.exist?('/bin/systemctl')

def initialize(options)
super

raise PlatformNotSupported, 'Running as a service only supported on Linux' unless Thin.linux?
end

def config_path
@options[:all] || DEFAULT_CONFIG_PATH
end

def start
run :start
end

def stop
run :stop
end

def restart
run :restart
end

def install(config_files_path=DEFAULT_CONFIG_PATH)
if File.exist?(INITD_PATH)
log ">> Thin service already installed at #{INITD_PATH}"
else
log ">> Installing thin service at #{INITD_PATH} ..."
sh "mkdir -p #{File.dirname(INITD_PATH)}"
log "writing #{INITD_PATH}"
File.open(INITD_PATH, 'w') do |f|
f << ERB.new(File.read(TEMPLATE)).result(binding)
end
sh "chmod +x #{INITD_PATH}" # Make executable
end

if SYSTEMD
template = File.dirname(__FILE__) + '/thin.service.erb'
if File.exist?(SYSTEMD_PATH)
log ">> Thin SystemD service file already installed. Aborting."
else
log ">> Installing thin service file at #{SYSTEMD_PATH} ..."
File.open(SYSTEMD_PATH, 'w') do |f|
f << ERB.new(File.read(template)).result(binding)
end
end
else # INITD
template = File.dirname(__FILE__) + '/service.sh.erb'
if File.exist?(INITD_PATH)
log ">> Thin service already installed at #{INITD_PATH}. Aborting."
else
log ">> Installing thin service at #{INITD_PATH} ..."
sh "mkdir -p #{File.dirname(INITD_PATH)}"
log "writing #{INITD_PATH}"
File.open(INITD_PATH, 'w') do |f|
f << ERB.new(File.read(template)).result(binding)
end
sh "chmod +x #{INITD_PATH}" # Make executable
end
end

# Create the config file directory
sh "mkdir -p #{config_files_path}"

# Let the user know how to make service persistant
log ''
log "To configure thin to start at system boot:"
log "on RedHat like systems:"
log " sudo /sbin/chkconfig --level 345 #{NAME} on"
log "on Debian-like systems (Ubuntu):"
log " sudo /usr/sbin/update-rc.d -f #{NAME} defaults"
log "on Gentoo:"
log " sudo rc-update add #{NAME} default"
if SYSTEMD
log " sudo systemctl enable thin"
else
log "on RedHat like systems:"
log " sudo /sbin/chkconfig --level 345 #{NAME} on"
log "on Debian-like systems (Ubuntu):"
log " sudo /usr/sbin/update-rc.d -f #{NAME} defaults"
log "on Gentoo:"
log " sudo rc-update add #{NAME} default"
end
log ''
log "Then put your config files in #{config_files_path}"
end

private
def run(command)
Dir[config_path + '/*'].each do |config|
log "[#{command}] #{config} ..."
Command.run(command, :config => config, :daemonize => true)
end
end

def sh(cmd)
log cmd
system(cmd)
Expand Down
12 changes: 12 additions & 0 deletions lib/thin/controllers/thin.service.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[Unit]
Description=A fast and very simple Ruby web server
After=syslog.target network.target

[Service]
Type=simple
ExecStart=<%= Command.script %> start --all <%= config_files_path %>
ExecReload=<%= Command.script %> restart --all <%= config_files_path %>
ExecStop=<%= Command.script %> stop --all <%= config_files_path %>

[Install]
WantedBy=multi-user.target