14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/shared_infrastructure/systemd/rails.rb', line 14
def write_unit_file(domain_name, domain)
puts "writing unit file (domain_name): #{Systemd.unit_file(domain_name)} (#{domain_name})" if Runner.debug
result = File.open(Systemd.unit_file(domain_name), "w") do |f|
f << <<~UNIT_FILE
[Unit]
Description=Puma HTTP Server for #{domain_name}
After=network.target
# Uncomment for socket activation (see below)
# Requires=#{domain_name}.socket
[Service]
# Foreground process (do not use --daemon in ExecStart or config.rb)
Type=simple
User=nobody
Group=www-data
# Specify the path to the Rails application root
WorkingDirectory=#{Nginx.root_directory(domain_name)}
# Helpful for debugging socket activation, etc.
# Environment=PUMA_DEBUG=1
Environment=RACK_ENV=production
Environment=RAILS_ENV=production
# FIXME: The following is the wrong place
EnvironmentFile=#{domain.secrets}
Environment=REDIS_URL=unix:///tmp/#{redis_location(domain_name)}.sock
# The command to start Puma
# NOTE: TLS would be handled by Nginx
ExecStart=#{Nginx.root_directory(domain_name)}/bin/puma -b #{puma_uri(domain_name)} \
--redirect-stdout=#{Nginx.root_directory(domain_name)}/log/puma-production.stdout.log \
--redirect-stderr=#{Nginx.root_directory(domain_name)}/log/puma-production.stderr.log
# ExecStart=/usr/local/bin/puma -b tcp://#{puma_uri(domain_name)}
Restart=always
[Install]
WantedBy=multi-user.target
UNIT_FILE
end
puts "changing mode of unit file" if Runner.debug
FileUtils.chmod(0o600, Systemd.unit_file(domain_name))
puts "enabling service" if Runner.debug && Process.uid.zero?
`systemctl enable #{domain_name}.service` if Process.uid.zero?
result
end
|