Cap deploy doesn't create share/log folder - ruby-on-rails

When I run my cap deploy, it complains that it can't access the log file:
Rails Error: Unable to access log file. Please ensure that
/var/superduperapp/releases/20120329011558/log/production.log exists
and is chmod 0666. The log level has been raised to WARN and the
output directed to STDERR until the problem is fixed.
It seems that I have to manually create a log folder. Is there a way to do this with Capistrano so whoever is deploying it doesn't have to remember to create the folder each time they do a new deploy?

These folders should be created by capistrano when you run cap deploy:setup, have you ran it? To check if everything is fine you can run cap deploy:check before it.

You can create a custom task to create this directory and launch it as the first task:
task :create_log_share do
run "mkdir -p #{shared_path}/log"
end
before 'deploy:update', :create_log_share
This directory does not need to be created each the time when you deploy. Once is enough. The shared directory never changes.

Related

Why Rails 4.2 app create log files with root permissions when deploy it on Ubuntu server using Ansible during running rake db:migrate task

Scope of problem
Rails 4.2.11
Ansible 2.1.1.0
Ubuntu 14
Ubuntu user: deploy
I have Rails app which I deploy to Ubuntu server by Ansible script.
I have problem of understanding why Rails app create log files with root permissions when Ansible script execute rake tasks.
In my example it's running rake db:migrate but also same behaviour with rake assets:precompile
You can see by photo below that application is deployed via user 'deploy' but after run of rake task it create 2 log files with root permission. After restart of web server it crash with permission denied error, so I need manually change ownership to deploy:deploy
Structure of Rails logger is also looks suspicious. You can check #dev=IO:<STDERR> value. I've checked in another project and there I can see something like #dev=#<File:/var/www/.../log/production.log>
I tried to explore source code of ror4 but so far no luck to understand from there what is happening. Only idea is could be that Rails raise exception when create log file and STDERR become output
Please help if you have similar problem or can point out where I can look to.
The rake script runs as root User.
To answer the question you must add the complete ansible script or the bundle script if you manually add a "sudo" commands or something unusual.
There are different possible positions to define the user in ansible.
Read the become section of the ansible documentation https://docs.ansible.com/ansible/latest/user_guide/become.html
Or give a try with
- name: Run db:migrate
shell: ... rake cmd ...
become: yes
become_user: deploy
become_method: su
Change the become_method to your needs e.g become_flags: '-s /bin/sh' or sudo

How to create a log folder on cap deploy

I am new to RoR. I am using capistrano to deploy an RoR app.
I would like to create a log folder in current/public/log on each deploy. I am currently creating this folder manually and grating permissions to it. Is there a way I can do this everytime I do a deploy?
Add this into your deploy task:
execute "mkdir -p current/public/log; chmod u+rwx current/public/log"

Capistrano not deleting old releases

I'm aware of the keep_releases option in capistrano and I have this set in our deploy script. The problem I'm having is I think more related to permission issues. I tried running cap deploy:cleanup but I get a permission denied error when trying to delete directories inside tmp/cache. I'm using fragment caching which is why I have lots of files inside tmp/cache.
Can someone shed any light how to fix this issue? I have to manually delete the folders in the server in order to clean up the releases folder.
It looks like I just have to pass use_sudo
cap production deploy:cleanup -s use_sudo=true
i will try to change the owner of your directory to the user you use for capistrano:
sudo chown -R capistrano_user /path/to/www/app

How to execute a command on the server with Capistrano?

I have a very simple task called update_feeds:
desc "Update feeds"
task :update_feeds do
run "cd #{release_path}"
run "script/console production"
run "FeedEntry.update_all"
end
Whenever I try to run this task, I get the following message:
[out :: mysite.com] sh: script/console: No such file or directory
I figured it's because I am not in the right directory, but trying
run "cd ~/user/mysite.com/current"
instead of
run "cd #{release_path}"
Also fails. Running the exact same commands manually (through ssh) works perfectly.
Why can't capistrano properly cd (change directory) into the site directory to run the command?
Thanks!
Update: Picked an answer, and thank you so much to all who replied.
The best answer may actually be the one on server fault, though the gist of both (the one on server fault and the one on stack overflow) is the same.
You want to use script/runner. It starts an instance of the app to execute the method you want to call. Its slow though as it has to load all of your rails app.
~/user/mysite.com/current/script/runner -e production FeedEntry.update_all 2>&1
You can run that from the capistrano task.
I cannot imagine that you would be able to remotely log into rails console from capistrano. I suggest you call your model method from a rake task.
How do I run a rake task from Capistrano?
As for the latter part of your question, are you logging into the server with the same user account as capistrano?

Apache2, Git, Capistrano & Rails - creating symlinks

I'm sort of stuck with adding symlinks to my app on the server. I currently have the following in .gitignore:
/non-public/system/uploads/*
I basically don't want Git to store the contents of the upload directory. So far so good.
On my server, inside my deploy.rb, I have the following:
namespace :customs do
task :symlink, :roles => :app do
run <<-CMD
ln -nfs #{shared_path}/system/uploads #{release_path}/non-public/system/uploads
CMD
end
end
after "deploy:symlink","customs:symlink"
after "deploy", "deploy:cleanup"
I want to create a symlink after each deployment for the uploads directory, but I keep getting a failed error message because the non-public/system/uploads directory doesn't exist in the git repository in the first place.
I've verified this by taking a look at the repository, and the structure /non-public/system/uploads doesn't exist because I have that set in .gitignore to ignore it.
I've looked at the Git wiki and it doesn't track directories, so I must be missing something. How do other developers symlink the uploads directory with their server?
What I'll usually do on my cap deploys is to create the directories, by doing a basic
set :deploy_to, "/this/dir"
run "mkdir -p #{deploy_to}/then/more/dirs"
after "deploy:symlink"
namespace :deploy....
....
....
then provide the run code to do some symlinks either on an after or whatever. This probably not optimal for all situations, but for the simple stuff it usually gets the job done.

Resources