Vagrant: Difference between revisions

From Wildsong
Jump to navigationJump to search
Brian Wilson (talk | contribs)
mNo edit summary
Brian Wilson (talk | contribs)
 
(4 intermediate revisions by the same user not shown)
Line 24: Line 24:
== Using a generic Debian box ==
== Using a generic Debian box ==


mkdir generic
mkdir generic
cd generic
cd generic
vagrant init debian/jessie64; vagrant up --provider virtualbox
vagrant init debian/stretch64
vagrant up
vagrant up


== Keeping guest additions up to date ==
== Keeping guest additions up to date ==
Line 40: Line 40:


Refinement: Make the virtual hard drive smaller, there is no earthly reason for it to be 40GB.
Refinement: Make the virtual hard drive smaller, there is no earthly reason for it to be 40GB.
The default 8GB is big enough, in fact 4GB would be fine.
The default 8GB is big enough.


Quick version
Quick version


'''Build a virtualbox.''' Use a 4GB VMDK drive. Turn off audio and USB, put the network interface in bridged mode so it gets a real IP.
'''Build a virtualbox.''' Use 8GB VMDK drive. Turn off audio and USB, put the network interface in bridged mode so it gets a real IP.


'''Install Debian into it.''' Name the machine 'vastra', create 2 accounts, root and vagrant. Use this as a proxy: http://192.168.1.2:8000/ Install only the ssh server and the system utilities here.
'''Install Debian into it.''' Name the machine 'vastra', create 2 accounts, root and vagrant. Use this as a proxy: http://192.168.1.2:8000/ Install only the ssh server and the system utilities here.


'''Boot the machine and set it up.'''
'''Boot the machine and set it up.'''
* I am only running the machine from vagrant (ssh) I don't need the guest additions. Bypass those instructions.
* Ignore the instructions about setting root password, this is Debian and you did that already.


You do need to set up sudo. This works- log in as root--
* When I need guest additions (on a development machine) I install them later. That way they are always up to date.
apt-get install sudo
 
echo "vagrant ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/vagrant
In [[Debian preseed]] I set up the user accounts and packages so that's done on first boot of the virtual machine. I includef openssh and sudo in the preseed file, and I set up a proxy so repeated slow Internet downloads are eliminated.
 
That leaves only a few things to do on first boot.


* Since we just installed from the 'net there is no reason to update/upgrade now. You can if you want.
'''Set up keys.''' Log out ssh as user vagrant so you can paste this:
mkdir .ssh
chmod 0700 .ssh
cd .ssh
wget http://bellman/vagrant.pub -O authorized_keys
chmod 0600 authorized_keys
chown -R vagrant ~/.ssh


'''Set up keys.''' Log out from the virtualbox console and log in via ssh as user vagrant so you can paste this:
  su
  mkdir -p /home/vagrant/.ssh
chmod 0700 /home/vagrant/.ssh
wget --no-check-certificate https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub -O /home/vagrant/.ssh/authorized_keys
chmod 0600 /home/vagrant/.ssh/authorized_keys
chown -R vagrant /home/vagrant/.ssh


You already installed the SSH server but still need to edit config. I do it like this
# Allow "vagrant" to use "sudo"
  sudo -s
echo "vagrant ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/vagrant
  # Edit ssh config to allow keys.
  echo "AuthorizedKeysFile %h/.ssh/authorized_keys" >> /etc/ssh/sshd_config
  echo "AuthorizedKeysFile %h/.ssh/authorized_keys" >> /etc/ssh/sshd_config
 
# Zero out the box
'''Zero out the box and shut it down'''
  dd if=/dev/zero of=/EMPTY bs=1M
  dd if=/dev/zero of=/EMPTY bs=1M
  rm -f /EMPTY
  rm -f /EMPTY
  poweroff
  poweroff


'''Package it!'''
'''Package it.''' This turns the VM into a Vagrant Box.
  cd ~/Projects/vastra-box
  cd ~/Projects/vastra-box
  vagrant package --base vastra-4gb
  vagrant package --base vastra-8gb
  mv package.box vastra-4gb.box
  mv package.box vastra-8gb.box


'''Add it to your inventory'''
'''Add it to your inventory'''
  vagrant box add vastra-4gb vastra-4gb.box
  vagrant box add vastra-8gb vastra-8gb.box


'''Test it!'''
'''Test it!'''


  vagrant init vastra-4gb
  vagrant init vastra-8gb
  cat > VagrantFile <<EOF
  cp ../vastra-ansible/Vagrantfile .
# -*- mode: ruby -*-
# Adjust settings as needed, will this be a pbx or dev machine?
# vi: set ft=ruby :
emacs Vagrantfile
 
# Deployment machines don't need this
Vagrant.configure(2) do |config|
# but development machines do-- allows shared folders to work
 
vagrant plugin install vagrant-vbguest
  config.vm.box = "vastra-4gb"
  config.vm.network "public_network"
 
  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "../vastra-ansible/vastra-setup.yml"
  end
 
end
EOF
  vagrant up
  vagrant up


Line 114: Line 107:
  mkdir vastra-debian
  mkdir vastra-debian
  cd vastra-debian
  cd vastra-debian
  vagrant init deb/jessie-amd64
  vagrant init vastra-4gb
  vagrant up --provider virtualbox
cp ../vastra-ansible/Vagrantfile .
# Select dev options
emacs Vagrantfile
# Allow shared src folder
vagrant plugin install vagrant-vbguest
  vagrant up
  vagrant ssh
  vagrant ssh
You can log into the Squid cache machine and tail its log to see that it is indeed caching package requests from the vm.
ssh bellman
sudo tail -f /var/log/squid-deb-proxy/access.log


I also need to be able to simulate a target machine, one stripped down to the minimal requirements to run Vastra.
I also need to be able to simulate a target machine, one stripped down to the minimal requirements to run Vastra.


  cd ~/Projects
  cd ~/Projects
  mkdir vastra-target
  mkdir pbx
  cd vastra-target
  cd pbx
  vagrant init deb/jessie-amd64
vagrant init vastra-4gb
  vagrant up --provider virtualbox
  cp ../vastra-ansible/Vagrantfile .
  vagrant up
  vagrant ssh
  vagrant ssh

Latest revision as of 05:34, 1 December 2018

"Vagrant" is a system for managing virtual machines.

http://vagrantup.com/

You set up a config file that sets up the vm, including installing packages and configuration settings.

Normally I run Parallels to use virtual machines on my Mac, but as of version 11, I have to pony up another $20 to get the pro version for Vagrant, and there is no current Debian provider. So I am going back to using VirtualBox for now.

Running a squid proxy

I put a proxy on Bellman so that I don't have to pull every DEB package over the Internet every time I create a new Vagrant machine.

apt-get install squid-deb-proxy squid-deb-proxy-client

On each client you can install the avahi client software and it will automatically find the server.

apt-get install squid-deb-proxy-client

The debian proxy runs on port 8000, the standard squid proxy runs on 3128 If you want to use squid as well as the dpkg cache then you need to edit /etc/squid3/squid.conf to add an acl -- acl localnet 192.168.1.0/24 for example

Using a generic Debian box

mkdir generic
cd generic
vagrant init debian/stretch64
vagrant up

Keeping guest additions up to date

Install this plugin: https://github.com/dotless-de/vagrant-vbguest

Building a custom box

All the details

https://blog.engineyard.com/2014/building-a-vagrant-box

Refinement: Make the virtual hard drive smaller, there is no earthly reason for it to be 40GB. The default 8GB is big enough.

Quick version

Build a virtualbox. Use 8GB VMDK drive. Turn off audio and USB, put the network interface in bridged mode so it gets a real IP.

Install Debian into it. Name the machine 'vastra', create 2 accounts, root and vagrant. Use this as a proxy: http://192.168.1.2:8000/ Install only the ssh server and the system utilities here.

Boot the machine and set it up.

  • When I need guest additions (on a development machine) I install them later. That way they are always up to date.

In Debian preseed I set up the user accounts and packages so that's done on first boot of the virtual machine. I includef openssh and sudo in the preseed file, and I set up a proxy so repeated slow Internet downloads are eliminated.

That leaves only a few things to do on first boot.

Set up keys. Log out ssh as user vagrant so you can paste this:

mkdir .ssh
chmod 0700 .ssh
cd .ssh
wget http://bellman/vagrant.pub -O authorized_keys
chmod 0600 authorized_keys
chown -R vagrant ~/.ssh
su 
# Allow "vagrant" to use "sudo"
echo "vagrant ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/vagrant
# Edit ssh config to allow keys.
echo "AuthorizedKeysFile %h/.ssh/authorized_keys" >> /etc/ssh/sshd_config
# Zero out the box
dd if=/dev/zero of=/EMPTY bs=1M
rm -f /EMPTY
poweroff

Package it. This turns the VM into a Vagrant Box.

cd ~/Projects/vastra-box
vagrant package --base vastra-8gb
mv package.box vastra-8gb.box

Add it to your inventory

vagrant box add vastra-8gb vastra-8gb.box

Test it!

vagrant init vastra-8gb
cp ../vastra-ansible/Vagrantfile .
# Adjust settings as needed, will this be a pbx or dev machine?
emacs Vagrantfile
# Deployment machines don't need this
# but development machines do-- allows shared folders to work
vagrant plugin install vagrant-vbguest
vagrant up

This should bring the machine up and provision it to be a Vastra box all in one go.

Make a note to look at Packer to further automate this...

Vastra development machine

I need to be able to run a test machine on the Mac so that I can go offline in Gold Beach. And maybe so that I don't mess up our phones every time I get crazy testing.

cd ~/Projects
mkdir vastra-debian
cd vastra-debian
vagrant init vastra-4gb
cp ../vastra-ansible/Vagrantfile .
# Select dev options
emacs Vagrantfile
# Allow shared src folder
vagrant plugin install vagrant-vbguest
vagrant up
vagrant ssh

You can log into the Squid cache machine and tail its log to see that it is indeed caching package requests from the vm.

ssh bellman
sudo tail -f /var/log/squid-deb-proxy/access.log

I also need to be able to simulate a target machine, one stripped down to the minimal requirements to run Vastra.

cd ~/Projects
mkdir pbx
cd pbx
vagrant init vastra-4gb
cp ../vastra-ansible/Vagrantfile .
vagrant up
vagrant ssh