Rails / Puma在作为Systemd服务启动时不读取环境变量

时间:2018-04-24 12:53:24

标签: ruby-on-rails puma

我刚刚在Ubuntu 16.04上以生产模式部署了Rails服务器。当我从命令行启动服务器时,服务器启动并读取所有环境变量。

bundle exec puma -e production -C /home/deploy/shared/config/puma.rb

但是,当我切换到Systemd.service并从那里启动服务器时,没有读取任何环境变量。我也尝试在before_configuration钩子中读取和设置环境变量,这似乎也没有帮助。

设置环境变量

#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服务文件 -

#/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文件 -

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"

启动puma.service - 日志文件

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] 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)  

3 个答案:

答案 0 :(得分:1)

我相信arnvald是对的。 "systemd has an Environment directive which sets environment variables for executed processes. It takes a space-separated list of variable assignments"在该链接中,您可以看到如何将变量设置为systemd单位。

我在这个answer中了解到目前的最佳做法是将它们设置在一个文件中。

希望它有所帮助。

答案 1 :(得分:0)

有点晚了,但在您的服务中,您可以手动或通过文件添加变量。

EnvironmentFile=/path/to/file
Environment=MY_VAR=something

注意:EnvironementFile 必须加载具有以下模式的文件:

MY_VAR2=something
MY_VAR3=something_new

Documentation

答案 2 :(得分:0)

我遇到了同样的问题,puma 根本没有加载 .env 文件值......我所要做的就是使用 {{1} 定义 puma.service 文件中的值}} 例如:

Environment

sudo vi /etc/systemd/system/puma.service

另一种选择是在 [Unit] Description=Puma HTTP Server After=network.target [Service] Type=simple User=ubuntu WorkingDirectory=/home/ubuntu/rails_current_server Environment="RAILS_ENV=production" Environment="SERVER_TYPE=111" ExecStart=/bin/bash -lc 'bundle exec puma -C /home/ubuntu/rails_current_server/config/puma.rb' Restart=always KillMode=process [Install] WantedBy=multi-user.target 中硬编码环境变量的值,如下所示:ExecStart