Module: Systemd::Rails

Defined in:
lib/shared_infrastructure/systemd/rails.rb

Class Method Summary collapse

Class Method Details

.puma_uri(domain_name) ⇒ Object



6
7
8
# File 'lib/shared_infrastructure/systemd/rails.rb', line 6

def puma_uri(domain_name)
  "unix:///tmp/#{domain_name}.sock"
end

.redis_location(domain_name) ⇒ Object



10
11
12
# File 'lib/shared_infrastructure/systemd/rails.rb', line 10

def redis_location(domain_name)
  "redis." + domain_name
end

.write_unit_file(domain_name, domain, rails_env = "production", user) ⇒ Object



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
72
73
74
# File 'lib/shared_infrastructure/systemd/rails.rb', line 14

def write_unit_file(domain_name, domain, rails_env = "production", user)
  # if ENV["SECRET_KEY_BASE"].nil? ||
  #    ENV["DATABASE_USERNAME"].nil? ||
  #    ENV["DATABASE_PASSWORD"].nil? ||
  #    ENV["EMAIL_PASSWORD"].nil?
  #   raise "Missing environment variable"
  # end
  #
  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=#{user}
      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=#{rails_env}
      Environment=RAILS_ENV=#{rails_env}
      # FIXME: The following is the wrong place
      Environment=SECRET_KEY_BASE=#{ENV['SECRET_KEY_BASE']}
      Environment=DATABASE_USERNAME=#{ENV['DATABASE_USERNAME']}
      Environment=DATABASE_PASSWORD=#{ENV['DATABASE_PASSWORD']}
      Environment=EMAIL_PASSWORD=#{ENV['EMAIL_PASSWORD']}
      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-#{rails_env}.stdout.log \
        --redirect-stderr=#{Nginx.root_directory(domain_name)}/log/puma-#{rails_env}.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