rvm cookbook fails installing ruby within vagrant - rvm

I've been running into issues when trying to run the rvm::user recipe from fnichol/chef-rvm. I'm using chef along with a Vagrant box. rvm installs fine, but every time chef tries to install a ruby, it fails with this error:
WARN: Failed to install rvm_ruby[ruby-1.9.3-p448]. Check logs in /log/ruby-1.9.3-p448
Here's my Vagrantfile:
Vagrant.configure("2") do |config|
config.vm.box = 'precise32'
config.vm.box_url = 'http://files.vagrantup.com/precise32.box'
config.vm.provision "chef_solo" do |chef|
chef.add_recipe "rvm::vagrant"
chef.add_recipe "rvm::user"
chef.json = {
:rvm => {
:user_installs => [
{
:user => "vagrant",
:default_ruby => "1.9.3",
:rubies => ["1.9.3"],
:global_gems => [
{ :name => 'bundler' }
],
}
]
}
end
end
Environment Details:
Vagrant version: 1.2.7
Vagrant vm: precise32
rvm version: 1.22.11
chef-rvm ref: 59dc482

It turns out that Vagrant was running chef in a non-interactive/non-tty session. The sudo command doesn't like to run in non-interactive sessions, and causes rvm to fail when it tries install dependencies (via apt-get in ubuntu).
You can allow sudo to run non-interactively by adding this to /etc/sudoers:
vagrant ALL= (ALL:ALL) NOPASSWD: ALL
Once I added this, chef installed the rvm::user recipe successfully.

My setup was much more complicated, however the error was the same. Ultimately i found that adding the apt chef recipe fixed everything.
chef.add_recipe "apt"
chef.add_recipe "rvm::vagrant"
chef.add_recipe "rvm::user"
> Run List is [recipe[apt], recipe[curl], recipe[rvm::vagrant], recipe[rvm::user]]
Adding the line
vagrant ALL=(ALL:ALL) NOPASSWD: ALL
didn't work for my scenario.
I believe the apt chef recipe runs apt update which fixes an issue with old and ill matched versions.
The error messages i received were
Error executing action `install` on resource 'package[libxml2-dev]'
apt-get -q -y install libxml2-dev=2.7.8.dfsg-5.1ubuntu4.1 returned 100, expected 0
....
Error executing action `install` on resource 'rvm_ruby[2.1.1]'

Related

Docker: RVM command not found

I am spinning up a VM with docker installed using Vagrant
which also installs installs RVM and Ruby 2.1:
# -*- mode: ruby -*-
# vi: set ft=ruby :
$rvm= <<SCRIPT
unset GEM_HOME
curl -L https://rvm.io/mpapis.asc | gpg --import -
curl -L https://get.rvm.io | bash -s stable
echo "source /home/vagrant/.rvm/scripts/rvm" >> .bash_history
echo 'gem: --no-ri --no-rdoc' > ~/.gemrc
rvm install 2.1.1
SCRIPT
$setup= <<SCRIPT
docker build -t campaign/development:latest /home/vagrant
#docker run -d -p 3000:3000 -v /home/vagrant:/home/vagrant --name development development:latest
SCRIPT
Vagrant.configure(2) do |config|
config.vm.box = "hashicorp/precise64"
config.vm.provider "virtualbox" do |vb|
vb.memory = 2048
end
# need a private network for NFS shares to work
config.vm.network "private_network", ip: "192.168.50.4"
# Rails Server Port Forwarding
config.vm.network "forwarded_port", guest: 3000, host: 3000
config.vm.synced_folder ".", "/home/vagrant", create: true, type: "nfs"
# Install RVM
config.vm.provision "shell", inline: $rvm
# Install latest docker
config.vm.provision "docker"
config.vm.provision "shell", run: "always", inline: $setup
end
The build command hits the following Dockerfile:
FROM rails
RUN adduser web --home /home/vagrant --shell /bin/bash --disabled-password --gecos ""
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
# Separate Gemfile ADD so that `bundle install` can be cached more effectively
RUN chown -R web:web /home/vagrant &&\
mkdir -p /home/vagrant/bundle &&\
chown -R web:web /home/vagrant/bundle
ADD Gemfile /home/vagrant/
ADD Gemfile.lock /home/vagrant/
RUN rvm use 2.1.1#campaign --create &&\
bundle install --development
...
When I get to STEP 6, the RUN rvm command returns:
Step 6 : RUN rvm use 2.1.1#campaign --create && bundle install --development
---> Running in 86e80ff96036
/bin/sh: rvm: command not found
INFO[0012] The command [/bin/sh -c rvm use 2.1.1#campaign
--create && bundle install --development] returned a non-zero code: 127
I am just confused because I can ssh onto the box and run RVM commands,
and my ruby/gemset are created. Should I even be trying to run RVM commands
in the Dockerfile or should I just have an install script thats ran in the Vagrantfile?
rvm is launched using a script installed. That script has to be added to your session, but it is not being added using directly run in you Dockerfile. You can try using:
> RUN /bin/bash -l -c "rvm use 2.1.1#campaign --create bundle install --development"
If rvm is not found, but needs to be used inside your container, you can either add it to path via console by typing:
bash -l -c PATH=$PATH:/path/to/your/rvm/binary
Or add it to your default container user's .profile or .bashrc (its in /root or under /home/username)

Using vagrant to run virtual machines with desktop environment

My company's development environment is based on virtual machines, running on VirtualBox. We would like to move one step further, and use the capabilities of Vagrant to have the description of the machine in a text file and then be able to "raise" that machine based on that text file. Combined to puppet, this would solve us the problem that everyone have different software versions installed in the VM.
However, Vagrant seems very focused to develop on the host, letting the machine in the background. We would need to have our development environment within the machine, so we would need a complete GUI, so when typing "vagrant up" a machine with a complete desktop environment (XFCE, KDE...) should appear.
So far, I've managed to create a "base" box from a Xubuntu distribution. But when I type "vagrant up", although the desktop appears, and I am able to login properly, Vagrant freezes at the message "Waiting for machine to boot. This may take a few minutes...". After a while Vagrant crashes due timeout. So shared folders are not created, nor the package provisioner -puppet- is executed.
How to create a virtual machine with a complete GUI using vagrant?
I just got this working with basically three steps. The advice from askubuntu.com didn't quite work for me, so try this simplified version:
Get a basic Ubuntu image working. You should be able to boot it and vagrant ssh.
Next, enable the VirtualBox display, which is off by default. Halt the VM and uncomment these lines in Vagrantfile:
config.vm.provider :virtualbox do |vb|
vb.gui = true
end
Boot the VM and observe the new display window. Now you just need to install and start xfce4. Use vagrant ssh and:
sudo apt-get install xfce4
sudo startxfce4&
If this is the first time you're running this Ubuntu environment, you'll need to run the following command before installing xfce4:
sudo apt-get update
That's it, you should be landed in a xfce4 session.
Update: For a better experience, I recommend these improvements:
Don't start the GUI as root. You really want to stay the vagrant user. To do this you need to permit anyone to start the GUI: sudo vim /etc/X11/Xwrapper.config and edit it to allowed_users=anybody.
Next, install the VirtualBox guest tools before starting the GUI. This will give you a healthy screen resolution, integrated mouse, etc.
$ sudo apt-get install -y xfce4 virtualbox-guest-dkms virtualbox-guest-utils virtualbox-guest-x11
$ sudo VBoxClient-all
Only now should you start the GUI as the vagrant user, with $ startxfce4&.
Update 2: Tried this today and the VBoxClient-all script isn't always installed. If it's missing, you can replace with the equivalent:
sudo VBoxClient --clipboard
sudo VBoxClient --draganddrop
sudo VBoxClient --display
sudo VBoxClient --checkhostversion
sudo VBoxClient --seamless
Here's Air's excellent answer in the form of a Vagrantfile
Vagrant.configure(2) do |config|
# Ubuntu 15.10
config.vm.box = "ubuntu/wily64"
config.vm.provider "virtualbox" do |vb|
# Display the VirtualBox GUI when booting the machine
vb.gui = true
end
# Install xfce and virtualbox additions
config.vm.provision "shell", inline: "sudo apt-get update"
config.vm.provision "shell", inline: "sudo apt-get install -y xfce4 virtualbox-guest-dkms virtualbox-guest-utils virtualbox-guest-x11"
# Permit anyone to start the GUI
config.vm.provision "shell", inline: "sudo sed -i 's/allowed_users=.*$/allowed_users=anybody/' /etc/X11/Xwrapper.config"
end
To start the vm
vagrant up
Login with username: vagrant, password: vagrant via the login prompt on the virtualbox GUI.
Start xfce
startx
Here is a slightly adapted Vagrantfile for Ubuntu 18.04 LTS / bionic - thanks to Air's and Nik's answers, and this post explaining how to increase the disk size when using VirtualBox (default = 10 GB).
The VM includes a LightDM login screen.
Update: I've created a GitHub repo from this example, and added many software packages for frontend + backend development.
# Optional - enlarge disk:
#vagrant plugin install vagrant-disksize
vagrant up
vagrant reload
# After reboot, the VM screen should show the LightDM login screen.
# Log in as user "vagrant", password "vagrant".
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/bionic64"
# Optional - enlarge disk (will also convert the format from VMDK to VDI):
#config.disksize.size = "50GB"
config.vm.provider "virtualbox" do |vb|
# Display the VirtualBox GUI when booting the machine
vb.gui = true
end
# https://askubuntu.com/questions/1067929/on-18-04-package-virtualbox-guest-utils-does-not-exist
config.vm.provision "shell", inline: "sudo apt-add-repository multiverse && sudo apt-get update"
# Install xfce and virtualbox additions.
# (Not sure if these packages could be helpful as well: virtualbox-guest-utils-hwe virtualbox-guest-x11-hwe)
config.vm.provision "shell", inline: "sudo apt-get install -y xfce4 virtualbox-guest-dkms virtualbox-guest-utils virtualbox-guest-x11"
# Permit anyone to start the GUI
config.vm.provision "shell", inline: "sudo sed -i 's/allowed_users=.*$/allowed_users=anybody/' /etc/X11/Xwrapper.config"
# Optional: Use LightDM login screen (-> not required to run "startx")
config.vm.provision "shell", inline: "sudo apt-get install -y lightdm lightdm-gtk-greeter"
# Optional: Install a more feature-rich applications menu
config.vm.provision "shell", inline: "sudo apt-get install -y xfce4-whiskermenu-plugin"
end
My 2 cents
Make sure you are running latest vagrant (1.3.3 now) + VirtualBox (4.2.18) to avoid bugs.
You can use shell script or inline command to install a desktop environment or a light weight window manager
For example install LXDE on top of Ubuntu 12.04 Precise base box from vagrantbox.es
Vagrant.configure("2") do |config|
# ... other configuration
config.vm.provision "shell" do |s|
s.inline = "apt-get install lubuntu-desktop -y"
end
end
If you build your own vagrant base boxes, make sure you follow the base box packaging instructions or consider tools like packer (or veewee) to automate the build.
I'm using ubuntu desktop image, it works nicely with two monitors on windows with virtual box provider.
Vagrant.configure(2) do |config|
config.vm.box = "box-cutter/ubuntu1404-desktop"
config.ssh.forward_agent = true
config.vm.network "forwarded_port", guest: 8080, host: 8080
config.vm.network "forwarded_port", guest: 3000, host: 3000
config.vm.synced_folder "../../git", "/home/vagrant/git"
config.vm.provider "virtualbox" do |vb|
vb.gui = true
vb.customize ["modifyvm", :id, "--monitorcount", "2"]
vb.memory = "2048"
end
end
You might also consider using Packer to create VirtualBox images for developers to use.
Rather than sharing the Vagrantfile which developers each use to build and run their VM, you would have a packer template (json) which is used to create a VM image. Developers download or copy the image and run it locally, directly in VB, without having to build it themselves.
Many of the publicly shared Vagrant base boxes are created with Packer.
https://askubuntu.com/questions/300799/does-ubuntu-12-04-lts-32-bit-have-graphic-user-interface/300805#300805
After installing the desktop, you'll also want to install GDM which
will let you boot directly into a graphical environment. You'll also
want to configure it.
So maybe add this?
Vagrant::Config.run do |config|
config.vm.provision :shell, :inline => "sudo apt-get install gdm"
config.vm.provision :shell, :inline => "sudo dpkg-reconfigure gdm"
end
I've patched Nik's answer a bit to avoid HTTP 404:
Vagrant.configure(2) do |config|
# Ubuntu 15.10
config.vm.box = "bento/ubuntu-18.04"
config.vm.provider "virtualbox" do |vb|
# Display the VirtualBox GUI when booting the machine
vb.gui = true
end
# Install xfce and virtualbox additions
config.vm.provision "shell", inline: "sudo apt-get update"
config.vm.provision "shell", inline: "sudo apt-get install -y xfce4 virtualbox-guest-dkms virtualbox-guest-utils virtualbox-guest-x11"
# Permit anyone to start the GUI
config.vm.provision "shell", inline: "sudo sed -i 's/allowed_users=.*$/allowed_users=anybody/' /etc/X11/Xwrapper.config"
end
Adding to billmalarky's comment above, on fedora 20 the following was necessary before starting xfce:
Install VirtualBox-guest.rpm (available from rpmfusion repos)
yum groups mark install 'graphical_environment'
yum groupinstall "Xfce"
yum install xorg-x11-drivers
Here is the code:
config.vm.provision "shell", inline: <<-SHELL
#Install Virtual Box guest additions from rpmfusion repos
cd /vagrant
yum install -y rpmfusion-free-release-20.noarch.rpm
yum install -y rpmfusion-nonfree-release-20.noarch.rpm
yum update -y
yum install -y VirtualBox-guest
#Add XFCE desktop to fedora server
yum groups mark install 'graphical_environment'
yum groupinstall -y "Xfce"
yum install -y xorg-x11-drivers
SHELL
Like the xfce4 solution by #Air. Once I had success, but today I failed with ubuntu16.04. I got this error:
xrdb can't open display 1
But luckily, I found this works:
startx
I see a few people are having problems with "startx: command not found". I had this too and it was because I was trying login and startx before the first-time provisioning had completed. Be patient, go grab a coffee. Check the original console window to see what is happening especially when the provisioning has finished.

How to install a gem into rvm with puppet on a vagrant box?

I've setup a simple Vagrant box with puppet provisioner.
With puppet I've installed rvm:
exec { 'install_rvm':
command => "${as_vagrant} 'curl -L https://get.rvm.io | bash -s stable'",
creates => "${home}/.rvm",
require => Package['curl']
}
and ruby:
exec { 'install_ruby':
command => "${as_vagrant} '${home}/.rvm/bin/rvm install 2.0.0 --latest-binary --autolibs=enabled && rvm --fuzzy alias create default 2.0.0'",
creates => "${home}/.rvm/bin/ruby",
require => Exec['install_rvm']
}
I'm trying to install a gem that would be available on the default ruby version of rvm (in this case 2.0.0)
If I try the puppet documented code it doen's work (I suppose it instals the gem on the system ruby):
package { 'sinatra':
ensure => 'installed',
provider => 'gem',
}
Current workaround: If I execute a command as a vagrant user it works, but it doesn't look nice:
$as_vagrant = 'sudo -u vagrant -H bash -l -c'
exec { "${as_vagrant} 'sudo -u vagrant -H bash -l -c gem install stasis'":
require => Exec['install_ruby']
}
Any ideas?
Thank you.
You could use this rvm module from puppet forge. Check the Vagrant documentation about puppet to know how to use puppet modules with vagrant.

Vagrant fails to mount NFS shared folders because of corrupted /etc/exports. How do I fix that file?

I recently tried to install a VM with vagrant but "vagrant up" always failed with the error:
Mounting NFS shared folders failed. This is most often caused by the NFS
client software not being installed on the guest machine. Please verify
that the NFS client software is properly installed, and consult any resources
specific to the linux distro you're using for more information on how to
do this.
NFS client was properly installed on my machine so I looked for other causes of errors and found a blogpost explaining that my /etc/exports might be corrupted. I restored exportsbak (which contains only commented examples), hoping that vagrant would reconfigure that file properly... but it doesn't, and the error is still there.
How can I force vagrant to regenerate that file or fix it? Thanks.
Just delete the file.
sudo rm -f /etc/exports
The file will be recreated during the vagrant up process.
I was not able to get nfs running on my Ubuntu, because I used the vagrant packages from apt (V 1.2.2)
I installed the latest Vagrant Version (1.5) from here: http://www.vagrantup.com/downloads
and nfs worked.
Check the NSF server is not installed, you can do…
dpkg -l | grep nfs-kernel-server
If it is not installed, install the required packages…
apt-get install nfs-kernel-server
apt-get install nfs-common
service nfs-kernel-server restart
sudo service portmap restart
mkdir -p /var/exports
Then in Vagranfile add line under #shared folders...
config.vm.synced_folder "www", "/var/www", :nfs => { :mount_options => "dmode=755","fmode=755"] }
When vagrant is starting it will ask for root password, to run it without root password you can edit /etc/sudoers and add following lines…
Cmnd_Alias VAGRANT_EXPORTS_ADD = /usr/bin/tee -a /etc/exports
Cmnd_Alias VAGRANT_NFSD_CHECK = /etc/init.d/nfs-kernel-server status
Cmnd_Alias VAGRANT_NFSD_START = /etc/init.d/nfs-kernel-server start
Cmnd_Alias VAGRANT_NFSD_APPLY = /usr/sbin/exportfs -ar
Cmnd_Alias VAGRANT_EXPORTS_REMOVE = /bin/sed -r -e * d -ibak /etc/exports
%sudo ALL=(root) NOPASSWD: VAGRANT_EXPORTS_ADD, VAGRANT_NFSD_CHECK, VAGRANT_NFSD_START, VAGRANT_NFSD_APPLY, VAGRANT_EXPORTS_REMOVE
if your host is Windows, then you need to install a vagrant plugin Vagrant WinNFSd.
$ vagrant plugin install vagrant-winnfsd

Deploying Ruby on rails on Deamhost fails

I'm trying to deploy a simple Ruby on Rails app to dreamhost but I'm getting an error related to the bundle command. Here is the error:
servers: ["bullseye.dreamhost.com"]
[bullseye.dreamhost.com] executing command
command finished
* executing "find /home/USER-NAME/MY-DOMAIN/releases/20110123014150/public/images /home/USER-NAME/MY-DOMAIN/releases/20110123014150/public/stylesheets /home/USER-NAME/MY-DOMAIN/releases/20110123014150/public/javascripts -exec touch -t 201101230141.52 {} ';'; true"
servers: ["bullseye.dreamhost.com"]
[bullseye.dreamhost.com] executing command
command finished
triggering after callbacks for `deploy:update_code'
* executing `bundle:install'
* executing "ls -xt /home/USER-NAME/MY-DOMAIN/releases"
servers: ["bullseye.dreamhost.com"]
[bullseye.dreamhost.com] executing command
command finished
* executing "bundle install --gemfile /home/USER-NAME/MY-DOMAIN/releases/20110123014150/Gemfile --path /home/USER-NAME/MY-DOMAIN/shared/bundle --deployment --quiet --without development test"
servers: ["bullseye.dreamhost.com"]
[bullseye.dreamhost.com] executing command
*** [err :: bullseye.dreamhost.com] sh: bundle: command not found
command finished
*** [deploy:update_code] rolling back
* executing "rm -rf /home/USER-NAME/MY-DOMAIN/releases/20110123014150; true"
servers: ["bullseye.dreamhost.com"]
[bullseye.dreamhost.com] executing command
command finished
failed: "sh -c 'bundle install --gemfile /home/USER-NAME/MY-DOMAIN/releases/20110123014150/Gemfile --path /home/USER-NAME/MY-DOMAIN/shared/bundle --deployment --quiet --without development test'" on bullseye.dreamhost.com
Here is my deploy.rb file.
require 'bundler/capistrano'
set :user, "MY-USERNAME"
set :password, "MY-PASSWORD"
set :domain, 'bullseye.dreamhost.com' # Dreamhost servername where your account is located
set :project, 'blog' # Your application as its called in the repository
set :application, 'MY-DOMAIN' # Your app's location (domain or sub-domain name as setup in panel)
set :applicationdir, "/home/#{user}/#{application}" # The standard Dreamhost setup
# version control config
set :scm_username, 'MY-SVN-USERNAME'
set :scm_password, 'MY-SVN-PWD'
set :repository, "http://MY-SVN-URL/01/blog/"
# roles (servers)
role :web, domain
role :app, domain
role :db, domain, :primary => true
# deploy config
set :deploy_to, applicationdir
set :deploy_via, :export
# additional settings
default_run_options[:pty] = false # Forgo errors when deploying from windows
namespace :deploy do
task :start do ; end
task :stop do ; end
task :restart, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
end
end
I found a similar question at Bundle install failing when deploying a Rails 3 app to Dreamhost with Capistrano but the solution didn't work. Anybody have any ideas as to what might be happening in my case?
I got this figured out eventually. What I did was
ssh into the dreamhost server and execute which bundle command
from ssh session execute echo $PATH command
edit config/deploy.rb and combine both strings with a : between and place inside default_environment PATH value, see below
set :default_environment, {
'PATH' => "'/usr/lib/ruby/gems/1.8/bin//bundle:/home/sayed3/.gems/bin:/usr/lib/ruby/gems/1.8/bin/:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games'"
}
Once I did this it was able to execute the bundle command successfully, but then I started running into some other issues. I eventually decided to use a VPS that I have hosted elsewhere, but I'm sure if I spent a bit more time I could have figured it out.
Besides adding the path to bundle, as specified above, I also had to add the following line to my config/deploy.rb in order to force capistrano to use bash, instead of the default shell, which, on dreamhost, is dash:
set :shell, '/bin/bash'