Archive

Archive for the ‘vagrant’ Category

Multiple Vagrant VMs in One Vagrantfile

May 13, 2017 Leave a comment

One of the things I love about Vagrant is how it allows you to quickly create a VM that is very close to your production environment (or better yet EXACTLY like it). We’re starting to work on scaling STAGES from a single server to multiple servers so I’m using Vagrant as the test bed for this process. Because of this we need to work with multiple VMs in the same Vagrant file which isn’t covered by most tutorials.


http://www.thisprogrammingthing.com/2015/multiple-vagrant-vms-in-one-vagrantfile/

Categories: Interesting, vagrant

Enable ssh X11 forwarding on Vagrant boxes

April 22, 2017 Leave a comment

Problem:
I got the following error when trying to run tkinter on a vagrant box. (Ubuntu/trusty32).

TclError: no display name and no $DISPLAY environment variable

Solution:
Forward X11 Display to the host machine.

Following the tutorial shown in the source section below, the minimum I needed to do was to add

config.ssh.forward_x11 = true

to the Vagrantfile.

$ less Vagrantfile
  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "ubuntu/trusty32"

  # Forward X11
   config.ssh.forward_x11 = true

Then, vagrant up, vagrant ssh, then tried to run the python code again. Code works as expected.

Source
http://computingforgeeks.com/how-to-enable-and-use-ssh-x11-forwarding-on-vagrant-instances/
https://www.vagrantup.com/docs/vagrantfile/ssh_settings.html

Categories: vagrant

Vagrant was unable to communicate with the guest machine

October 23, 2016 2 comments

Problem:
I was setting up vagrant on an old laptop running debian 8. I also had virtualbox installed. I ran into the following error when trying to set up an ubuntu vm.

$ vagrant init ubuntu/trusty32
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/trusty32'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'ubuntu/trusty32' is up to date...
==> default: Setting the name of the VM: flaskbox_default_1477256195436_10223
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.

If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.

If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.

If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.

The main reason for this was because the networking on ubuntu was not working.

Solution:
The fix was to restart the network interface on the ubuntu box.

To do this I had to log into the box via the gui interface using virtualbox or uncommenting “vb.gui=true” in the Vagrantfile.

After connecting I ran the following commands

$ sudo ifdown eth0
$ sudo ifup eth0

I then made sure that I could ping google.com and the host box. Now I needed to enable this when the machine booted. That meant adding the same command to /etc/rc.local

$ less /etc/rc.local
ifdown eth0
ifup eth0

exit 0

I now did a test run on ubuntu/precise32. This worked well and vagrant was now working as expected. Trying the same on ubuntu/trusty32 resulted in a timeout once again. This meant that I had to add the following to the Vagrantfile

  # For ubuntu/trusty32 the box takes a while to boot up.
  # Default timeout is 300 seconds.
  config.vm.boot_timeout = 600

Now both boxes are able to come up even after reboot.

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'ubuntu/trusty32' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default: 
    default: Guest Additions Version: 4.3.36
    default: VirtualBox Version: 5.1
==> default: Mounting shared folders...
    default: /vagrant => /home/rodnee/boxes/testbox
$ vagrant ssh
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-100-generic i686)

 * Documentation:  https://help.ubuntu.com/

  System information as of Sun Oct 23 21:21:41 UTC 2016

  System load:  0.25              Processes:           81
  Usage of /:   3.3% of 39.34GB   Users logged in:     0
  Memory usage: 15%               IP address for eth0: 10.0.2.15
  Swap usage:   0%

  Graph this data and manage this system at:
    https://landscape.canonical.com/

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.


Last login: Sun Oct 23 21:12:08 2016 from 10.0.2.2
vagrant@vagrant-ubuntu-trusty-32:~$ exit
logout
Connection to 127.0.0.1 closed.
$ vagrant halt
==> default: Attempting graceful shutdown of VM...

Note:
You would have to log into a new box the very first time to bring up the network interface. The new changes will persist after a reboot.

Sources:
This took me so long to fix and I went down so many different rabbit holes that I can’t quite say that there was a definitive source that helped me out.

Lessons learnt
1. Installing the latest version of virtualbox did not work on debian 8 (stretch). I needed to stick with what came with the distribution.
2. I did not need to install virtualbox-guest-additions as it already comes with virtualbox.
3. There is no need to change the Network settings on virtualbox. The default settings for NAT work just fine and were not part of the problem.

# Packages that I needed to install
$ sudo apt-get install virtualbox virtualbox-guest-dkms dkms
$ sudo apt-get install vagrant

sshpass

May 28, 2016 Leave a comment

Problem:
Once again, portforwarding port 3141 on one vagrant box, to point to a local devpi-server running on another vagrant box has gotten tiring. I need to be able have this functionality on any vagrant box I provision.

Solution:
Moved the command to a script. This may not be a secure way of doing this, but it suffices for now as this is only for local development work. This is not intended for work on remote production or staging servers.

1. Install sshpass

$ sudo apt-get install sshpass

2. Put the command in a script file (port3141.sh).

#!/bin/bash

cd /home/vagrant

export SSHPASS="vagrant"

sshpass -e  ssh -L 3141:192.168.1.2:3141 -o LogLevel=Error -o StrictHostKeyChecking=no vagrant@192.168.1.2

3. Make it executable

$ chmod +x port3141.sh

4. Now everytime I need to port forward I simply open up a separate tmux window and run the script.

Note:
Only changing StrictHostKeyChecking because of my frequency of using a new box. Change with caution.

StrictHostKeyChecking
If this flag is set to “yes”, ssh(1) will never automatically add host keys to the ~/.ssh/known_hosts file, and refuses to connect to hosts whose host key has changed. This pro‐
vides maximum protection against trojan horse attacks, though it can be annoying when the /etc/ssh/ssh_known_hosts file is poorly maintained or when connections to new hosts are
frequently made. This option forces the user to manually add all new hosts. If this flag is set to “no”, ssh will automatically add new host keys to the user known hosts files.
If this flag is set to “ask”, new host keys will be added to the user known host files only after the user has confirmed that is what they really want to do, and ssh will refuse to
connect to hosts whose host key has changed. The host keys of known hosts will be verified automatically in all cases. The argument must be “yes”, “no”, or “ask”. The default is
“ask”.

Source:
http://stackoverflow.com/questions/4780893/use-expect-in-bash-script-to-provide-password-to-ssh-command

Categories: bash, vagrant Tags: ,

Installing tmux 2.2

May 28, 2016 Leave a comment

Problem:
How to install tmux 2.2 from source. (I am installing this on a vagrant box running Ubuntu 14.0).

Solution:
The read me works. Installing from the git version did not work for me.

1. Check the current version of tmux if installed. Remove it.

$ tmux -V
1.8
$ sudo apt-get remove tmux

2. Install the dependencies.

$ sudo apt-get install libevent-dev
$ sudo apt-get install ncurses-dev

3. Download and install tmux

$ cd ~
$ wget https://github.com/tmux/tmux/releases/download/2.2/tmux-2.2.tar.gz
$ tar -zxf tmux-2.2.tar.gz
$ cd tmux-2.2
$ ./configure && make

4. Create a symlink to new version of tmux

$ sudo ln -s /home/vagrant/tmux-2.2/tmux /usr/local/bin/tmux

5. Confirm that you now have the new version of tmux.

$ tmux -V
2.2

Alternatively:
You could use this script. (I have not tried it for 2.2 but I am guessing that it would work)
https://github.com/kaosf/ubuntu-setup/blob/master/tmux-setup.sh

Update – 2017-06-02
To get the latest version of tmux from github

apt-get install the following

libevent-dev
ncurses-dev
automake
autoconf
pkg-config
$ git clone https://github.com/tmux/tmux.git
$ cd tmux
$ sh autogen.sh
$ ./configure && make

As root

root> make install
$ tmux -V
2.5

Source:
https://github.com/tmux/tmux/issues/257

Categories: bash, vagrant Tags: , ,

Specify a default provider for vagrant

May 1, 2016 Leave a comment

Problem:
I got tired of specifying the provider. The default provider was vmware_fusion.

$ vagrant up --provider virtualbox

Solution:
As per the documentation.

export VAGRANT_DEFAULT_PROVIDER="virtualbox";

So now all I do is

$ vagrant up

Source:
https://www.vagrantup.com/docs/providers/default.html

Categories: bash, vagrant Tags: ,

xterm colors

April 30, 2016 Leave a comment

Problem:
Editing a file using vim looks like this:
Screen Shot 2016-04-30 at 12.29.40

instead of this (ie note the colors):
Screen Shot 2016-04-30 at 12.30.06

This happens to me on freshly set up vagrant boxes. I am yet to find a permanent solution.

Solution:
Run the following command and try again.

$ export TERM=xterm-256color