referencing a ruby gem from git breaks aws beanstalk deployment - ruby

I have set up my aws environment succesfully. However i am having one probelem. In my Gemfile i have the following line:
gem 'activeadmin', :github => 'gregbell/active_admin'
When i do a git aws.push i can see the following error in the aws logs:
/usr/local/share/ruby/gems/2.0/gems/bundler-1.3.5/lib/bundler/source/git.rb:177:in `rescue in load_spec_files': git://github.com/gregbell/active_admin.git (at master) is not checked out. Please run `bundle install` (Bundler::GitError)
I was able to fix that problem by creating this file: .ebextensions/ruby.config
option_settings:
- option_name: BUNDLE_DISABLE_SHARED_GEMS
value: "1"
- option_name: BUNDLE_PATH
value: "vendor/bundle"
packages:
yum:
git: []
This packages everything into vendor/bundle and my app starts correctyl.
However I have two problems with this approach:
It takes very long to deploy because it needs to install all gems every time
I am not longer able to execute commands when I ssh into the EC2 instance. For example I have to start an rpush deamon. Locally this works with bundle exec rpush development but on EC2 this results in:
bundle exec rpush
/usr/local/share/ruby/gems/2.0/gems/bundler-1.3.5/lib/bundler.rb:284: warning: Insecure world writable dir /var/app/current/vendor/bundle/bin in PATH, mode 040777
git://github.com/gregbell/active_admin.git (at master) is not checked out. Please run bundle install
Is there an other way for installing the gems correctly and at the same time using the system gems? Or if that is not possible, how can I start rpush when the gems are bundled?

Update your Gemfile.lock (bundle install&& bundle update) in your local env. and push a commit with Gemfile and Gemfile.lock. Deploy to AWS again.

Can you check if using gem 'activeadmin', 'git://github.com:gregbell/active_admin' in your Gemfile works for you?
You can also get faster deployments utilizing vendor/cache by following the instructions given here:
http://blogs.aws.amazon.com/application-management/post/Tx2XVRWSS4E971S/Locally-Packaging-Gem-Dependencies-for-Ruby-Applications-in-Elastic-Beanstalk

What solved my problem: I modified the bundle install script and added the --deployment flag

Related

The git source is not yet checked out when running Docker

I'm currently facing an error when trying to use a gem from GitHub. I have the following in my Gemfile:
# Gemfile
source 'https://rubygems.org'
ruby '2.3.1'
gem 'sinatra'
gem 'rack'
gem 'puma'
group :development do
gem 'byebug'
gem 'rack-test'
gem 'rerun', github: 'alexch/rerun', branch: 'master'
end
When I run bundle install from Dockerfile it works with a message like:
...
Installing sinatra 1.4.6
Installing listen 3.1.5 (was 3.0.6)
Using rerun 0.11.0 from git://github.com/alexch/rerun.git (at master#3e4c486)
Bundle complete! 6 Gemfile dependencies, 14 gems now installed.
...
However, when I go to start the container I get:
The git source git://github.com/alexch/rerun.git is not yet checked out. Please run `bundle install` before trying to start your application
I've seen similar issues but not related to Docker.
Turns out I was able to find the issue. The problem was related to my Gemfile being used to build the Docker image.
I had not locally run a bundle install command which led to the Gemfile.lock not being updated. Once I ran the command the following was added to my Gemfile.lock:
## -1,14 +1,20 ##
+GIT
+ remote: git://github.com/alexch/rerun.git
+ revision: 3e4c486304be406cb86180ef70ec24e9ae055ce4
+ branch: master
+ specs:
+ rerun (0.11.0)
+ listen (~> 3.0)
Turns out that was all I needed. Once I had the updated file, rebuilt the image and ran the container everything worked as expected with bundle exec rerun. So the catch was that I was using the Gemfile.lock to help with caching / version locking but failed to keep it updated with my Gemfile.
In case anyone is interested there's an alternative.
During image creation, if your dockerfile runs bundle install it will generate a Gemfile.lock in the container. You can use docker run to get the contents, e.g.
docker run web cat Gemfile.lock
You can copy the contents and save a Gemfile.lock to your project directory.
Worked to me:
docker-compose run web bundle install

CloudFoundry microbosh unable to deploy on AWS

I've followed the "minimal AWS deployment" guide here https://github.com/cloudfoundry/cf-release/tree/master/example_manifests to try CloudFoundry.
I understand that I have to install the bosh_cli_plugin_micro, but when I run this on an AWS Ubuntu 14.04 VM:
gem install bosh_cli_plugin_micro
I get:
ERROR: While executing gem ... (Gem::DependencyError)
Unable to resolve dependencies: blobstore_client requires aws-sdk-resources (= 2.2.0); aws-sdk requires aws-sdk-v1 (= 1.60.2)
Installing the AWS ruby sdk doesn't solve the problem. I suspect it's a problem of the aws sdk being a newer version than the one the micro bosh cli expects. Anyone have any suggestions?
One way to ensure you get a consistent set of gems would be to create a Gemfile like this:
source 'https://rubygems.org'
gem 'bosh_cli'
gem 'bosh_cli_plugin_micro'
and then run the bundle command from the same working directory as where your Gemfile is placed. To make sure you're always using the right version of the CLI and plugin, prepend commands with bundle exec, e.g.
$ bundle exec bosh micro deploy /path/to/stemcell
You may even wish to alias bosh to bundle exec bosh.

Heroku: where did my gems go?

I have been deploying a Rails 3.2 application to heroku for several weeks now. I have also been executing rake tasks on the Cedar stack where my application is located.
One day after a deploy I noticed that rake was no longer working. I get, for example, the following:
$:~/dev/my_project$ heroku run rake -T
Running `rake -T` attached to terminal... up, run.7566
bundler: command not found: rake
Install missing gem executables with `bundle install`
Trying to run commands with bundle exec yields the same results.
What I've tried:
heroku run bundle install. This works and informs me that gems were installed into ./vendor/bundle. However, heroku run ls ./vendor/bundle yields only the following:
$:~/dev/my_project$ heroku run ls ./vendor/bundle/
Runningls ./vendor/bundle/attached to terminal... up, run.3458
bin ruby
bundle package. Although the deployment works it does not help my problem.
fiddling around with the rubygems-bundler gem (although I think this is now part of core bundler). This does not seem to have any effect.
On Heroku, gems are installed within the vendor/bundle/ruby/<version>/gems directory. I just checked my Heroku instance and confirmed this.
You are going to want to use bundle exec rake task because the gems are not in the users PATH.

Is it possible to bundle / install gems from a local cache?

I have bunch of gems on my computer that I want to use in a chef recipe.
I know it is possible to put them in a directory like /tmp/gems and just:
cd /tmp/gems
gem install *.gem
Is it possible to put all gems in one directory where I can install them with bundler without downloading them again?
cd /somedir/my_rails_project
bundle
I want to save bandwidth.
bundle install --local should be what you want. From the bundle-install manpage:
--local
Do not attempt to connect to rubygems.org, instead using just the
gems located in vendor/cache. Note that if a more appropriate
platform-specific gem exists on rubygems.org, this will bypass
the normal lookup.
You can add local directories to your Gemfile (example from the docs):
gem "nokogiri", :path => "~/sw/gems/nokogiri"
Alternatively, you can set up a local Git repository with the gems in it and write a Gemfile like this:
gem "gem1", :git => "file:///tmp/gems",
:branch => "gem1"
Use
bundle package
Locks and then caches the gems into ./vendor/cache.
The package command will copy the .gem files for your gems in the
bundle into ./vendor/cache. Afterward, when you run bundle install,
Bundler will use the gems in the cache in preference to the ones on
rubygems.org.
http://bundler.io/v1.6/bundle_package.html
You can use the BUNDLE_CACHE_PATH configuration key:
cache_path (BUNDLE_CACHE_PATH): The directory that bundler will place cached gems in when running bundle package, and that bundler will look in when installing gems. Defaults to vendor/bundle.
Source: https://bundler.io/v1.16/bundle_config.html#LIST-OF-AVAILABLE-KEYS
In GitLab CI, I defined this value in the environment of runners: "BUNDLE_CACHE_PATH=/cache-ci/bundle", this directory is mounted automatically in CI runners.
Then bundle install will install gems from the cache directory (once cache will be populated).
If you want to use a local cache for the purpose of speeding up bundle install on CI, for example when a docker container is used to run the tests, you could use --path. This will use gems in the given path unless they are not present, otherwise it will download them to that location.
This assumes the CI build can mount a persistent volume inside the docker container. So for example if the CI machine has a directory /var/cache/drone which can be mounted in the docker container as ./cache then you can do:
bundle install --without=development --quiet --path=cache

Unable to update gems on production server

Can not update gems on production server.
I've tried bundle install --deployment and bundle install --without development test
But keep getting:
You are trying to install in deployment mode after changing
your Gemfile. Run `bundle install` elsewhere and add the
updated Gemfile.lock to version control.
If this is a development machine, remove the Gemfile freeze
by running `bundle install --no-deployment
EDIT
I don't know if this is correct, but needed a quick fix. I ran bundle install --no-deployment then bundle update then ran bundle install --deployment again
The instructions are probably a bit confusing. It's saying that you've modified your Gemfile on your development machine and just pushed those changes rather than running bundle install BEFORE committing the changes.
By running bundle install you will update your Gemfile.lock file. This should be pushed to your server as it's more important than Gemfile. Consider the Gemfile the plans for the Gemfile.lock file.
Always remember to:
Run bundle install if you change your Gemfile, even just to make sure. If it's too slow, pass --local through which forces it to only use local gems to resolve its dependencies.
Commit both the Gemfile and Gemfile.lock file to your repository
Deploy both the Gemfile and Gemfile.lock to your production servers to ensure that they're running the exact same dependencies as your development environment.
Running bundle update by itself can be construed as dangerous that will update all the dependencies of your application. It's mainly dangerous if you don't have solid version numbers specified in the Gemfile. I wrote about it here.
FWIW I had this problem and fixed it by removing some conditional statements from my Gemfile (conditionals on OS) and rerunning bundle.
FYI: You can also get this error if you use source blocks like this:
source 'https://rails-assets.org' do
gem 'rails-assets-jquery'
end
You'll see an exclamation point in the Gemfile.lock for this gem:
rails-assets-jquery!
Just define the additional source normally, i.e.
source 'https://rails-assets.org'
gem 'rails-assets-jquery'
(BTW cf. here about using multiple gem sources.)
This can be caused by an old version of the bundler gem on the server you're deploying to (in this case production). Logging into the server and running a gem update bundler resolved the issue for me. The server I was deploying to was running version 1.7.4 and the current version was 1.9.
I had an issue with my production server still using an old version of a gem, even though the Gemfile.lock showed the correct, updated version. My production server was running on Unicorn - and shutting down / starting it back up again fixed the issue - instead of sending the HUP signal, which did jack all to fix the issue.
bundle install failed on my "development" machine because of the mysql-gem on osx...
I also needed a quick fix. So I cloned the repo to a new folder on the production machine, ran "bundle install" there and committed the Gemfile.lock to the repo.
I have had this problem (Ubuntu 12.10 & 12.04, one of which behind a proxy server).
My problem was that I had some git:// protocols in the Gemfile. Changing this to http:// helped me get it all working.

Resources