Foreman export upstart resetting $PATH - ruby-on-rails

Here is what I am running:
rbenv sudo foreman export upstart /etc/init -a myapp -p 8080 -u myuser
What gets generated in ...web-1.conf
start on starting myapp-web
stop on stopping myapp-web
respawn
exec su - myuser -c 'cd /home/myuser/apps/myapp; export PORT=8080; bundle exec unicorn -p $PORT -c ./config/unicorn.rb >> /var/log/myapp/web-1.log 2>&1'
When I run tail -f /var/log/myapp/web-1.log, I see the following:
-su: bundle: command not found
It appears $PATH is being reset. If I manually cd into that directory, while running under myuser, I can execute the command just fine. Thoughts?
I am using foreman, rbenv, rbenv-sudo, unicorn, rails 4.0.0, and ruby 2.0.0-p247.
Thanks!

Okay, so I had my rbenv being configured in ~/.bashrc.
su - myuser -c is a login shell, but not an interactive shell.
I moved rbenv config to ~/.profile and everything seems to be working now.
Thanks!

Related

reboot script to start rails server

My rails server is running under a deployer user and I normally start it like this
#restart.sh
#!/bin/bash
sudo -u deployer -H bash -l
cd /var/www/html/cms/
./server -e staging start
So when I login over SSH i run ./restart.sh and that works great.
I'm trying to do this automatically after a reboot so I've added a reboot_site.service in
/etc/systemd/system/reboot_site.service
# /etc/systemd/system/reboot_site.service
[Unit]
Description=reboot_site
After=syslog.target network.target
[Service]
Type=simple
WorkingDirectory=/var/www/html/cms
# If you use rbenv:
# ExecStart=/bin/bash -lc '/home/deploy/.rbenv/shims/bundle exec sidekiq -e production'
# If you use the system's ruby:
ExecStart=/home/ec2-user/restart.sh
[Install]
WantedBy=multi-user.target
The service is enabled using sudo systemctl enable reboot_site.service
However when I now reboot the server the script stops execution after the following line
sudo -u deployer -H bash -l
Server is an AWS EC2 AIM instance
What's wrong with my configuration to make this work and how can I fix this?
It stops on sudo -u deployer -H bash -l because your script just changes user and will not continue until you logout as this user. So the remaining commands in your script are not executed.
I would suggest the following version which uses runuser:
#!/bin/bash
runuser -l deployer -c 'cd /var/www/html/cms/ && ./server -e staging start'
if you just need to run a script on startup you can configure a crontab for this
#crontab -e
#reboot /home/ec2-user/restart.sh

How can I run puma server on boot instead of crontab

I wonder what's the better practice to run rails server with Puma and nginx on reboot.
because my crontab doesn't work.
I want to learn how to run the server automatically in other ways
PATH=/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/home/userA/bin:/home/userA/.rbenv/shims:/home/userA/.rbenv/bin:/home/userA/.rbenv$
SHELL=/usr/bin/zsh
#reboot zsh -l -c 'cd /project && bundle exec puma config/puma.rb'

God using sudo fails to find the environment parameter SECRET_KEY_BASE in the .bash_profile file

I added those rows to /home/web/.bash_profile. The web is the user of Rails app.
SECRET_KEY_BASE=xxxxxxxxxxxxxxxxxxxxx
export SECRET_KEY_BASE
I added this row into /etc/sudoers.
Defaults env_keep += "SECRET_KEY_BASE"
This command returns expected result.
sudo ruby -e 'puts ENV["SECRET_KEY_BASE"]'
If I run Unicorn without God, it can find SECRET_KEY_BASE from environment, and performs normally.
bundle exec unicorn_rails -c config/unicorn.rb -E production -D
Running Unicorn using sudo without God performs normally, too.
sudo bundle exec unicorn_rails -c config/unicorn.rb -E production -D
But when I run Unicorn with God using sudo, it failed to find SECRET_KEY_BASE and becomes error.
sudo god start my_app
The start command in the my_app.god is this.
God.watch do |w|
w.start = "cd #{rails_root} && bundle exec unicorn_rails -c config/unicorn.rb -E #{rails_env} -D"
w.uid = 'web'
w.gid = 'web'
...
end
It seems that the problem exists around God rather than .bash_profile and Unicorn. Besides what should I try?
I added this line into the my_app.god
ENV["SECRET_KEY_BASE"] = 'xxxxxxxxxxxxxxxxxxxxxxxx'
I know that this isn't a cool solution, but it works. And SECRET_KEY_BASE isn't included in a Git repository as source code.

Supervisord with rails - can't find command 'RAILS_ENV=production'

I try to run sidekiq service with supervisord
Here's my config:
[program:my-app-sidekiq-staging]
directory=/srv/www/DOMAIN/current
command=RAILS_ENV=production /usr/local/rvm/bin/rvm ruby-2.2.2#my-app-staging do bundle exec sidekiq -e production -d -C config/sidekiq.yml -L log/sidekiq.log
autostart=true
autorestart=true
redirect_stderr=true
After startup I’m have fatal error:
can't find command 'RAILS_ENV=production'
I’m confused, because my config for rails runs without errors
directory=/srv/www/DOMAIN/current
command=RAILS_ENV=production /usr/local/rvm/bin/rvm ruby-2.2.2#my-app-staging do bundle exec passenger start -S tmp/unicorn/ilp-app-unicorn.sock --environment production --friendly-error-pages
Your environment should not be set in the command but in a separate environment value.
environment=RAILS_ENV=production
See this question.
Supervisor and Environment Variables

rvmsudo unicorn_rails -c config/unicorn/production.rb -D --env production is not working

I have installed new ec2-instance for production server
when i tried the following command in the current folder
[ec2-user#ip-xx-xxx-xxx-xxx current]$ rvmsudo unicorn_rails -c config/unicorn/production.rb
-D --env production
sudo: unicorn_rails: command not found
Please let me know how to start the production server which is running at port 80.
Thanks in advance
For port 80: First run
export rvmsudo_secure_path=1
then
rvmsudo unicorn_rails -c config/unicorn/production.rb -D --env production
it will work for sure
You dont have to use rvmsudo and all to start your unicorn app server. You can do it
bundle exec unicorn -D -c /path/to/app/unicorn.rb -E production

Resources