I just deployed a Rails server in Production mode on Ubuntu 16.04. When I start the server from the command line as below, the server starts up and reads all the Environment varaiables.
bundle exec puma -e production -C /home/deploy/shared/config/puma.rb
But, when I switch to Systemd.service and have the server started from there, none of the Environment variables are read. I also tried reading and setting the environment variables in a before_configuration hook and that does not seem to help either.
Setting Environment variables
#config/initializers/set_environment_variables.rb
module SetEnvironmentVariables
class Application < Rails::Application
config.before_configuration do
env_file = Rails.root.join("config", 'environment.yml').to_s
if File.exists?(env_file)
YAML.load_file(env_file)[Rails.env].each do |key, value|
ENV[key.to_s] = value
end # end YAML.load_file
end # end if File.exists?
end # end config.before_configuration
end # end class
end # end module
Puma systemd service file -
#/etc/systemd/puma.service
[Unit]
Description=Puma Rails Server
After=network.target
[Service]
Type=simple
User=deploy
WorkingDirectory=/home/deploy/current
ExecStart=/bin/bash -lc 'bundle exec puma -C /home/deploy/shared/config/puma.rb'
ExecStop=/bin/bash -lc 'bundle exec pumactl -S /home/deploy/shared/tmp/pids/puma.state stop'
TimeoutSec=15
Restart=always
[Install]
WantedBy=multi-user.target
Rollbar initiliazer file -
Rollbar.configure do |config|
config.access_token = ENV['ROLLBAR_ACCESS_TOKEN']
unless Rails.env.production?
config.enabled = false
end
config.environment = ENV['ROLLBAR_ENV'] || Rails.env
end
.bashrc
#~/.bashrc
export ROLLBAR_ACCESS_TOKEN="11111111111"
Start puma.service - Log file
deploy:~/current/log$ systemctl status puma.service
● puma.service - Puma Rails Server
Loaded: loaded (/etc/systemd/system/puma.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2018-04-24 01:34:49 UTC; 3min 23s ago
Process: 20593 ExecStop=/bin/bash -lc bundle exec pumactl -S /home/deploy/shared/tmp/pids/puma.state stop (code=exited, status=0/SUCCESS)
Main PID: 20948 (ruby)
Tasks: 11
Memory: 133.7M
CPU: 5.849s
CGroup: /system.slice/puma.service
└─20948 puma 3.11.3 (unix:///home/deploy/shared/tmp/sockets/puma.sock) [20180423205334]
lines 1-10/10 (END)
Rollbar Error log
[Rollbar] Scheduling item
[Rollbar] Sending item
[Rollbar] Got unexpected status code from Rollbar api: 400
[Rollbar] Response: {
"err": 1,
"message": "access token required"
}
[Rollbar] Details: https://rollbar.com/instance/uuid?uuid=xxx-xxx-xxx-xxx-xxexxx (only available if report was successful)
I believe arnvald is right. "systemd has an Environment directive which sets environment variables for executed processes. It takes a space-separated list of variable assignments" In that link you can see how to set variables to systemd units.
I readed in this answer that the current best practice is to set them in a file.
Hope it helps.
A bit late but on your service, you can add variable manually or via a file.
EnvironmentFile=/path/to/file
Environment=MY_VAR=something
Note: EnvironementFile must load a file with the following pattern:
MY_VAR2=something
MY_VAR3=something_new
Documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With