Protractor tests failing on Codeship due to 405 API response - ruby-on-rails

The protractor configuration and scripts work as expected both on our local development environment and in a continuous integration environment (similar to Codeship).
The project structure is the following (I'm describing the Codeship env. status below):
The AngularJS app requests data from the REST API build with the Ruby on rails app, using AJAX requests.
In our Codeship configuration we set up the rails app, migrate and seed the database so that all necessary data is available.
The AngularJS app is also configured correctly since the login page is rendered as expected (I'm doing a print screen, and the main page is loaded correctly).
The usename and password are filled in by protractor using valid credentials (available in the mysql DB).
But when clicking the 'Log In' button the returned response from the AJAX call is a 405 Method Not Allowed.
As we never encountered this response in our other environments we believe it has something to do with the specific codeship setup.
Any thoughts why the API would return 405 only on Codeship?
The setup is below:
rvm use 2.2.0 --install
cp config/database.codeship.yml config/database.yml
bundle install
export RAILS_ENV="test"
bundle exec rake db:drop RAILS_ENV=test
bundle exec rake db:create RAILS_ENV=test
bundle exec rake db:test:prepare
bundle exec rake db:migrate RAILS_ENV=test
bundle exec rake seed:migrate RAILS_ENV=test
cd frontend && npm install && cd ..
npm install -g grunt-cli
npm install -g http-server
cd frontend && npm install bower && cd ..
cd frontend && bower install && cd ..
cd frontend && grunt build && cd ..
cd frontend && webdriver-manager update --standalone && cd ..
export RAILS_ENV="development"
rake db:structure:load
rake seed:migrate
http-server public -a 127.0.0.1 -p 9000 > /dev/null &
http-server app > /dev/null &
bundle exec rake
cd frontend && grunt test && cd ..
Here is the part of the screenshot that shows the API response:

In the end, the solution for us was to use protractor-rails
- basically using the base url of the rails server instead of trying to run the app on a different URL through grunt. Our setup now looks like this:
rvm use 2.2.0 --install
cp config/database.codeship.yml config/database.yml
bundle install
# protractor tests
export RAILS_ENV="development"
rake db:structure:load
rake seed:migrate
npm install
webdriver-manager update
bundle exec rake protractor:init RAILS_ENV=development
bundle exec rake protractor:spec RAILS_ENV=development
# rails tests
bundle exec rake db:test:prepare
bundle exec rake db:migrate RAILS_ENV=test
bundle exec rake seed:migrate RAILS_ENV=test
bundle exec rake

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

Running RAILS_ENV=production with the system command

I have got a really weird problem with a script that I am running on my server, the details are below. But essentially I am using a script to set up a rails application and I am calling that script from an existing rails application.
In my existing application (the application that is calling the script on the server) I have:-
Spawnling.new do
system "cd ~/ && ~/*/create_site.sh param1 param2 >> /tmp/logger"
end
The create_site.sh script creates a fresh installation of rails using the below:
rails new $DIR --database postgresql
It then does a number of things to set up the application. The issue is that the script seems to run absolutely fine until it gets to the following command:
cd $DIR && RAILS_ENV=production rails g roroacms:install -v && RAILS_ENV=production rake assets:clean && RAILS_ENV=production rake assets:precompile
It is really odd because when I run the top command manually as the root user it seems to run absolutely fine without any issues at all. When I view the logger file at the end of the top command it looks like the below:
Your bundle is updated!
Bundle is installed and up to date
RoroaCMS installation
Installation complete with assets
Server started
When I run this manually it outputs a number of messages between each line where it is running the command. Any ideas on this? I am thinking that it could be something to do with RAILS_ENV as the rails new command runs fine earlier in the script.
Hi I do a fair bit of scripting rails on servers and I always use the format bundle exec rails g rspec:install production so your command would be cd $DIR && bundle exec rails g roroacms:install -v production && bundle exec rake assets:clean production && bundle exec rake assets:precompile production. I dont have a whole lot of reasoning behind it but that format seems to be most reliable across different server environments.

Backup a Ruby in Rails app (Gitlab) with Cron job

I tried ti backup my gitlab project (it's based on ROR) with a bash script triggered with a CRON job.
The bash script is ok except the rake migrations wiche returns an error:
gitlabBackUp.sh: 12:
/home/backup/scripts/gitlabBackUp.sh: bundle: not found
Here is the way i did it in my gitlabBackUp.sh
# Export the data
bundle exec rake gitlab:backup:create --trace RAILS_ENV=production
I tried without the bundle exec but it returns
/home/backup/scripts/gitlabBackUp.sh: 14:
/home/backup/scripts/gitlabBackUp.sh: rake: not found
Any tips?
EDIT:
I finally get the cron task get working with:
/usr/local/bin/bundle exec /usr/local/bin/rake gitlab:backup:create --trace RAILS_ENV=production
PATH is probably not well specified in cron. You can:
1) Call (exec) bash with -l e.g. exec bash -l /path/to/real-script.sh
Or perhaps directly in cron like 0 16 * * * /bin/bash -l '/home/backup/scripts/gitlabBackUp.sh'
2) Explicitly specify path for bundle and rake e.g. /usr/local/bin/bundle exec /usr/binrake gitlab:backup:create --trace RAILS_ENV=production
You can always know the location of bundle and rake through which bundle and which rake; or type -P bundle and type -P rake.

Asset Precompile on Amazon Linux

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.

Travis CI In Sub Directory

I have a gem with a subdirectory (test) containing a Rails 3.1.1 application used to test the project. I'm trying to setup Travis-CI for continuous integration, however I cannot figure out how to setup my .travis.yml configuration. I have:
gemfile: test/Gemfile
rvm:
- 1.8.7
- 1.9.2
- 1.9.3
- jruby
- ree
script: sh -e 'cd test' && bundle exec rake db:drop db:create db:migrate test
Which causes:
sh: Can't open cd test
Any ideas?
Found it was an issue with some of the documentation. The script call should have been:
script: sh -c 'cd test && bundle exec rake db:drop db:create db:migrate test'

Resources