How to create a log folder on cap deploy - ruby-on-rails

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"

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

Azure DevOps - Automatically run rails migrations

I created a Azure devops project that deploys my code from Github. My project is a Rails app that runs on Docker. When I create the Azure DevOps project, it creates the CI/CD pipelines for me. The deployment works, but I can't figure out how to automatically migrate the database when we deploy. I know I can do it manually, but I prefer not to as we might forget when we deploy.
I tried to run the following commands in the "Post Deployment Actions"
rake db:migrate
bundle exec rake db:migrate
rbenv exec bundle exec rake db:migrate
docker-compose run web rake db:migrate
I've also generated my own kuduscript (using kuduscript) to add in the line for migration but it did not work. I don't know if it's because it's not reading my deployment script or if that line doesn't work.
Am I missing something? Should I try to figure out how to migrate through Docker instead? I've looked at all these links but they all run the migration manually.
https://learn.microsoft.com/en-us/azure/app-service/containers/quickstart-ruby
https://medium.com/paris-rb/deploying-your-rails-postgresql-app-on-microsoft-azure-180f8a9fab47
https://learn.microsoft.com/en-us/azure/app-service/containers/tutorial-ruby-postgres-app
So not sure if you ever figured this out but I just had to do this for a migration I'm working on. The best way to do what you're after from what I've found is to setup a stage in your release pipeline like so:
stage setup
Where basically:
Your service connection logs into the container registry
Pulls the image you just built and pushed to the registry
Use a docker run command to then run your image with all the necessary environment variables and anything else needed to run it properly.
You can then use another task if needed to run a docker exec bundle exec rake db:migrate
If the migrate task runs successfully it should exit 0 allowing you to then run your app service on the latest tagged image with the correct database changes already done.

aws.push command not running .config commands or container_commands

I have a simple setup with a load-balanced application and want to run some commands for cron setups (for instance, using the whenever gem). However, none of my commands seem to get run on the remote server.
# .elasticbeanstalk/Production.config
container_commands:
20_update_crontab:
command: whenever --update-crontab app
leader_only: true
Even tried:
# .elasticbeanstalk/Production.config
commands:
update_crontab:
command: whenever --update-crontab app
Is there something I am missing? These should run with git aws.push correct?
When I check the logs, I don't really get any information saying it was trying to run:
$ eb logs | grep whenever
Using whenever (0.9.2)
The descriptions on this page are pretty good, just can't figure out why it isn't running.
http://www.infoq.com/news/2012/11/elastic-beanstalk-config-files
Elastic Beanstalk configuration files should be placed in .ebextenstions/. It looks like yours are in .elasticbeanstalk/
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers.html
Create a configuration file with the extension .config (e.g., myapp.config) and place it in an .ebextensions top-level directory of your source bundle.
I had a similar problem where I was trying to install libjpeg for my EC2 instance using the configuration files and it was never installed. I tried everything and was never able to find a "good solution", but I did solve it though.
Solution:
How to solve it? Set up a completely new Elastic Beanstalk Application and deploy the same app again. After I did this it worked from the start with the new EB App.
Check out my other answer here: https://stackoverflow.com/a/23109410/2335675
Check it out, hope it helps for you.

Cap deploy doesn't create share/log folder

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.

What is the importance of creating symlink & how to create it in EngineYard

My rails app is on engineyard server.
I need to create symlink for public folder.
How to create symlink on engineyard server?
I have no experience in deployment so i am very eager to know what is the importance of the symlink & for which folder it should be created?
Also currently i am using my staging environment what should i write in code or create a file so that it should create a symlink automatically when i deploy same code on production.
Thanks!
The proper way to create a symlink on EngineYard is to add deploy hooks. You want to add a /deploy directory to your project and add a before_symlink.rb file.
For example, if I put a configuration file in the shared directory (/data/my_app/shared/config), I can add a deploy hook to symlink this file in.
The contents of your file would look like this:
run "ln -nfs #{shared_path}/config/some_config.yml #{release_path}/config/some_config.yml"
The #{shared_path} variable points to your apps shared directory and #{release_path} is the current release being created as part of the deploy.
More info can be found at: http://docs.engineyard.com/use-deploy-hooks-with-engine-yard-cloud.html
The symlink should get automatically created whenever you deploy. Its purpose is to maintain the same path to your application across multiple deployments. When you deploy your application, you should create a symlink to the latest release, like this (on a Unix machine):
ln -s /application/releases/10102011011029/public /application/current
The first path is the REAL file or directory. The second path is the path and name of the symlink. Now when you point something to /application/current, it will be in the latest release.
If you use Capistrano, all of this is taken care of for you automatically whenever you deploy.

Resources