Rails Capistrano Deploy Best Practices / Log Files - ruby-on-rails

I have a rails app that I inherited. In deploy.rb, it performs the following commands:
run "mv #{shared_path}/log/#{rails_env}.log #{shared_path}/log/#{rails_env}_old"
run "touch #{shared_path}/log/#{rails_env}.log && chmod -R 777 #{shared_path}/log"
So you can see it's moving the existing log file to one called _old and then creating a new one.
This causes a problem when in some situations, the first deploy fails. When I deploy again, it overwrites the _old file a second time and now the previously existing logs are gone.
The thing is, that I don't understand why the deploy script is doing this. I don't understand why it was written like this in the first place. I believe everybody would be fine if we just left the log files alone during the deploy.
Does anybody have any clues for me?

Remove it, and use log rotate.

What the deploy script is doing is good because log files get big really soon and writing to big file is costly. You should use log rotate or some other utility. But if you want to keep it simple, give unique file names by appending timestamps
run "mv #{shared_path}/log/#{rails_env}.log #{shared_path}/log/#{rails_env}_old_#{Time.now.Time.now.to_i}"
run "touch #{shared_path}/log/#{rails_env}.log && chmod -R 777 #{shared_path}/log"

Related

Problems cleaning up after cypress run

I'm executing cypress tests in mounted directory and I want to clean up after myself by deleting the node_modules:
npm uninstall
rm -rf node_modules
However, from time to time I'm getting errors:
rm: cannot remove 'node_modules/.rxjs.DELETE/src/internal/scheduled':
Directory not empty
All files in the volume have the owner nobody:nogroup, so my current user should be able to delete everything. From time to time it's not the case.
What I'm mising? How to correctly clean up after myself?
I have encountered a similar problem before.
Apparently the message you have shown is also caused by files still being used in that directory (I know, misleading... It seems like -rf is just not working).
You mentioning that it works from time to time reinforces my suspicion, things like this are often timing related.
So what you could do is add a sleep 5 or something to test that. The long-term solution should obviously look different... Just make sure, that everything is finished before attempting to clean stuff up.

How to edit permissions on Heroku application?

My problem in simple terms is that I have an executable that can't be run on Heroku, because it doesn't have the right permissions.
In more details, I have a RoR application on Heroku and I want to use server pdftk. But after installing it I need to chmod the file to be able to use it. And if I run a console on Heroku dashboard, put the chmod command in and try running pdftk it works, but it works just for that temporary dyno and it doesn't work on production server.
I tried creating .profile and putting the command in and that didn't work.
I tried creating Procfile and put release: chmod u+x /app/vendor/pdftk/bin/pdftk and it didn't work.
I tried all different versions of release, web, worker...
I tried creating a .sh file and putting the command in there and then running the file and it doesn't work either.
command for setting permission: chmod u+x /app/vendor/pdftk/bin/pdftk
If you need more info, please tell me.
Any help would be appreciated.
Okay, I figured out what the problem was.
I have a pipeline from gitlab and the permissions just needed to be set through git, so that they were correct when they came to the production enviroment.
I needed to run this code:git update-index --add --chmod=+x pdftk

How to change back to the first_app directory?

I'm new to Ruby and am trying to make my way through the Hartl tutorial. I ran into a couple issues this morning and am afraid that I might have made one of them worse.
I was doing fine in the tutorial until I got to the Heroku deployment section of chapter 1 and realized that I had yet to setup the Sublime Text 2 "subl" command so that my terminal could interact with Sublime Text.
I then went on a chase to figure out how to get the subl command to work. While trying to get that figured out, I came across this thread (Installing Sublime Text's command line tool 'subl' in terminal, permission denied?) and went ahead and changed directories to the "mkdir bin" so that I could run "sudo ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" /usr/bin/subl" and get my subl command line to work.
Well, when I entered the "sudo ln -s...." line it asked for a password and basically said I should be careful what I was doing. In light of that, I just want to return to my "first_app" directory and try to figure stuff out a different way. Only problem is, when I hit "cd first_app" it tells me there is no such file or directory... I'm starting to freak out a little bit now...
How can I get back to the first_app directory? Surely it's not gone?!' <-- most important!!
If I can get back to the first_app directory, how in the world can I get the subl command line so that I can continue on with deployment, etc...?
Any and all help is much appreciated as I try and work through this really frustrating phase.
UPDATE: I just changed it back to the first_app directory -- what is the best way to get this subl command line working?
Depending on what directory you're in, if you try to cd first_app, it may not be a sub-directory of the dir you're currently in.
Some basic linux commands to help you out:
cd .. <-- Moves up a level
pwd <-- Shows where you currently are in the directory structure
ls <-- Shows files/folders that exist in the directory you are in.

Reloading nginx config from within a rails app

I have a multi-app system running on a centOS box, that consists of our main app and a deployer app. when a client wants a new instance of our app, they use our deployer, fill in some info and the new install is created on our server. the issue i am having is that i can't get nginx to reload it's config file automatically. so after the deploy when visiting the new app we receive a 404 until i reload manually.
I've tried a few different ways including chmod /opt/nginx/sbin/nginx to 777, chmod the install script and deployer app to 777,
the script goes like this:
#create install directory -- works correctly
#copy files over -- works correctly
#run install script
##-- and then at this point i've tried multiple lines, including:
system("nginx -s reload") ## this works manually
system("/etc/init.d/nginx reload") ## this works manually
i've followed directions here: Restart nginx without sudo? to create a script to run without a sudo password and then tried this:
system("sudo /var/www/vhosts/deployer/lib/nginx_reload")
nothing seems to work, i'm assuming this is a permissions error, but maybe i'm wrong, if anyone could point me in any direction, that would be very helpful since i've been trying to figure this out for a few days too long and i'm fresh out of new ideas
sudo /etc/init.d/nginx reload

Do I need to re-make and re-install couchdb everytime I want to test a change to the source?

I am trying to contribute more with couchdb code, but I have really no idea how it is done the right way.
I have cloned the source from apache git repository and built it with
./configure
make && sudo make install
Then I wanted to change a file from the source called couch_httpd_show.erl
Do I need to run make && sudo make install again for every change I make to the source code and want to see how it behaves?
I am sure there's a more practical way to do it, because this approach is a bit time and patience consuming right?
Yes, there is a shortcut.
./configure
make dev
./utils/run
This builds and runs CouchDB entirely in the current directory. Instead of running as a background daemon, CouchDB will run in the foreground and output log messages to the terminal. It uses some local directories to store stuff: ./tmp/log for logs, ./tmp/lib for databases, and (if I remember correctly) ./etc/couch/local_dev.ini for configuration.
If you run this instead:
./utils/run -i
then you will also have an interactive Erlang prompt, which you can use to help debug.
When I work on CouchDB, I run this in the shell:
make dev && ./utils/run -i
After I change some code, I press ^C, up-arrow, return.
When I joined Couchio, I was responsible for production CouchDB deployments. I asked Chris Anderson for advice about something and he said, "Sorry, ask Jan. I've been just using utils/run for years!"
You can rebuild that one file and drop the output beam in place and restart.
erlc <file.erl>
& then copy the .beam file into place. To restart couchdb use either init:restart(). in the erlang shell or POST /_restart to CouchDB.
Although you might want to consider using the commandline erlang & javascript test suite also to ensure you didn't break anything.

Resources