Asset Precompile on Amazon Linux - ruby-on-rails

I installed locomotivecms app on a small amazon linux instance. I ran following to precompile the assets:
bundle exec rake assets:precompile
It's been like half an hour and the assets are still precompiling. Following is what I have on the terminal so far:
[ec2-user#domU-12-31-39-09-15-88 locomotivecms]$ bundle exec rake assets:precompile
/home/ec2-user/.rvm/rubies/ruby-1.9.3-p448/bin/ruby /home/ec2-user/.rvm/gems/ruby-1.9.3-p448/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets
which: no convert in (/home/ec2-user/.rvm/gems/ruby-1.9.3-p448/bin:/home/ec2-user/.rvm/gems/ruby-1.9.3-p448#global/bin:/home/ec2-user/.rvm/rubies/ruby-1.9.3-p448/bin:/home/ec2-user/.rvm/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin)
which: no identify in (/home/ec2-user/.rvm/gems/ruby-1.9.3-p448/bin:/home/ec2-user/.rvm/gems/ruby-1.9.3-p448#global/bin:/home/ec2-user/.rvm/rubies/ruby-1.9.3-p448/bin:/home/ec2-user/.rvm/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin)
mkdir -p /home/ec2-user/locomotivecms/public/assets
cp -rp /home/ec2-user/.rvm/gems/ruby-1.9.3-p448/gems/locomotive-tinymce-rails-3.5.8.2/vendor/assets/javascripts/tinymce /home/ec2-user/locomotivecms/public/assets
mkdir -p /home/ec2-user/locomotivecms/public/assets
cp -rp /home/ec2-user/.rvm/gems/ruby-1.9.3-p448/gems/locomotive-aloha-rails-0.23.2.2/vendor/assets/javascripts/aloha /home/ec2-user/locomotivecms/public/assets
which: no convert in (/home/ec2-user/.rvm/gems/ruby-1.9.3-p448/bin:/home/ec2-user/.rvm/gems/ruby-1.9.3-p448#global/bin:/home/ec2-user/.rvm/rubies/ruby-1.9.3-p448/bin:/home/ec2-user/.rvm/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin)
which: no identify in (/home/ec2-user/.rvm/gems/ruby-1.9.3-p448/bin:/home/ec2-user/.rvm/gems/ruby-1.9.3-p448#global/bin:/home/ec2-user/.rvm/rubies/ruby-1.9.3-p448/bin:/home/ec2-user/.rvm/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin)
Do the which commands mean that time is being spent finding the rake executable for each file that is getting precompiled?

Seems like you do not have imagemagick installed.
Depending on your distro, it should be either sudo yum install ImageMagick ImageMagick-devel or sudo apt-get install imagemagick.
You can test it whether it installed successfully by issuing the commands convert or identify.
However, the documentation recommends that your precompile on your own local machine, not on the remote server. Meaning, you issue bundle exec rake assets:precompile on your own machine first. That would avoid having to precompile on the remote server.

Related

How to run a bash script to update a Rails app from outside its directory?

I have this script that should be running from the /~ directory:
#!/bin/bash
APP=/root/apps/monitoring
cd $APP
git pull
rake assets:precompile RAILS_ENV=production
touch $APP/tmp/restart.txt
As you can see, it pulls new commits and updates the assets and restarts Apache. The problem is when it runs the line rake assets:precompile RAILS_ENV=production, It says:
Could not find proper version of rake (12.1.0) in any of the sources
Run `bundle install` to install missing gems.
Which is strange because I am supposed to be inside the app's folder (/root/apps/monitoring) when this command is executed. What I am doing wrong?
You may need to load rvm in the script (https://rvm.io/workflow/scripting) and may be select proper ruby/gemset.
Also you can consider using wrapper for bundle created with rvm wrapper
Please try
#!/bin/bash
APP=/root/apps/monitoring
cd $APP
git pull
bundle exec rake assets:precompile RAILS_ENV=production
touch $APP/tmp/restart.txt
With bundle exec it should work.
I run rake tasks using bash script wrappers. The trick is to use the source command to load in the rvm environment after the task is started
example.sh
#!/bin/bash
# change the directory
cd /home/ubuntu/GSeries
# load rvm ruby
source /home/ubuntu/.rvm/environments/ruby-2.4.2#mygemset
bundle exec rake db:prune_logs RAILS_ENV="production" &>> /home/ubuntu/GSeries/log/prune_logs.log

How can I precompile assets using Travis CI?

In the rails guides they have an example on how to precompile assets with Capistrano. It is as simple as adding load 'deploy/assets' to the Capfile. I simply want to achieve the same effect, precompiling assets, while using Travis CI instead. This is what I've done so far:
script/travis.sh:
run "bundle exec rake assets:precompile"
.travis.yml:
before_install:
- chmod +x script/travis.sh
script: script/travis.sh
language: ruby
rvm:
- 2.2
deploy:
provider: heroku
When this is built on Travis it fails and I get this from the log:
$ script/travis.sh
script/travis.sh: line 1: run: command not found
The command "script/travis.sh" exited with 127.
I also want to add that my shell script knowledge is very limited.
I'm not entirely sure, but I'm leaning towards the run command not being available in the travis shell, as against Capistrano. I'd say you should probably just leave your travis.sh as:
bundle exec rake assets:precompile
And try again. Let me know the results of that

Running bash script on server through ssh, unable to call Rails commands through script

I have a script that commits my code to GitHub and I modified it to also run a script on the web server that is supposed to pull the new code, which it does successfully, but then is unable to run the necessary Rails commands like Rake or Bundle. I'm confused because I change to the project directory at the top of the script and git pull runs fine. I even tried putting the Rails command calls inside a subshell with cd /home/rails/ at the top but that still didn't work and neither did specifying the full path to each Rails script. Am I going about this the wrong way or is there a better way to automate these two processes?
Commit script:
git add -A
git commit -m "$1"
git push
ssh root#example.com sh /home/rails/update_script.sh
Update script on server:
service unicorn stop
cd /home/rails/
git pull
rake db:migrate RAILS_ENV=production
rake assets:precompile RAILS_ENV=production
bundle install
service unicorn start
exit
Edit: Oops, forgot the output. Here is the output from the server:
* Stopping Unicorn web server unicorn
...done.
From https://github.com/my_name/example
7e0fee4..17fd564 master -> origin/master
Updating 7e0fee4..17fd564
Fast-forward
fresh.sh | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
/usr/bin/env: ruby: No such file or directory
/usr/bin/env: ruby * Starting Unicorn web server unicorn
: No such file or directory
/usr/bin/env: ruby: No such file or directory
...done.
Maybe you have to add /usr/local/rvm/rubies/ruby-2.1.5/bin to $PATH.
And I think you should run bundle install before running rake tasks.
Try this:
service unicorn stop
cd /home/rails/
git pull
export PATH=$PATH:/usr/local/rvm/rubies/ruby-2.1.5/bin
bundle install
RAILS_ENV=production bundle exec rake db:migrate
RAILS_ENV=production bundle exec rake assets:precompile
service unicorn start
exit

Clearing All Assets In GitLab

I want to use custom logo-white, logo-black, and favicon in my company installation of GitLab.
I've Googled my butt off and tried eveything I can find to clear these damn images and nothing seems to work.
Here's the only process that seems to run successfully, yet it does not remove the images:
bundle exec rake cache:clear RAILS_ENV=production
service gitlab stop
redis-cli FLUSHALL
bundle exec rake assets:precompile RAILS_ENV=production
service gitlab start
Then I clear my browser cache and go to the domain and there are the same damn images again!
I even deleted all the logo and favicon files I could find completely from the application, and yet somehow they are still there.
HELP!
There's a rake task assets:clean since version 6.0. So run bundle exec rake assets:clean RAILS_ENV=production from the command line and it will remove the assets.
After you nuke them, you'll probably want to run bundle exec rake assets:precompile RAILS_ENV=production to rebuild them.
I was able to combine some of the info here, and successfully replaced the logo using the following steps (note that some commands are slightly different using the latest version of gitlab, 7.11.4, on CentOS 7, so I am adding this as an answer in the hopes that it will be helpful to users with the new version):
Replace the appropriate images in /opt/gitlab/embedded/service/gitlab-rails/app/assets/images
Stop gitlab using sudo gitlab-ctl stop
Refresh the rails cache sudo gitlab-rake assets:clean RAILS_ENV=production followed by sudo gitlab-rake assets:precompile RAILS_ENV=production
Start gitlab using sudo gitlab-ctl start
For some reason I was getting permission errors in the precompile step. This was solved by changing the write permissions to /opt/gitlab/embedded/service/gitlab-rails/public/assets/ to be globally writable (a+w). It seems that gitlab-rake runs as the git user, even though I used sudo. So, after the steps above, I also changed the owner to root (sudo chown -R root:root /opt/gitlab/embedded/service/gitlab-rails/public/assets), and the permissions back using a-w.
I assume that upgrading gitlab will revert the logo back to the original one, but I haven't tried that yet.
I found the answer for anyone looking.
You also have to replace the images in
app/assets/images/

"heroku run rake assets:clean" doesn't clean anything

I execute
$ heroku run rake assets:clean
Running `rake assets:clean` attached to terminal... up, run.2
/usr/local/bin/ruby /app/vendor/bundle/ruby/1.9.1/bin/rake assets:clean:all RAILS_ENV=production RAILS_GROUPS=assets
rm -rf /app/public/assets
But it doesn't look like it is working due I can access to the assets by http request and also if I open a heroku console I can see the files:
$ heroku run console
irb(main):013:0> Dir.glob "./public/assets/*"
=> ["./public/assets/img", "./public/assets/application.js.gz", "./public/assets/application.js", "./public/assets/rails.png", "./public/assets/manifest.yml", "./public/assets/application.css", "./public/assets/assets", "./public/assets/application.css.gz"]
I need to remove the assets because they are producing conflicts with my new configuration.
The rake assets:clean functionality has been replaced with
rake assets:clobber
in the latest version of Rails.
https://github.com/rails/sprockets-rails/blob/master/README.md
I had to do:
heroku repo:purge_cache
and wait a bit. rake assets:clobber did not work for me, even though it printed:
INFO -- : Removed /app/public/assets
rm -rf /app/tmp/cache/assets]
if you run the heroku command and get:
! `repo:purge_cache` is not a heroku command.
! See `heroku help` for a list of available commands.
install the command in your heroku toolbelt with:
heroku plugins:install https://github.com/heroku/heroku-repo.git
and rerun.

Resources