Scriptable virtual environments with Vagrant

February 27th 2020

https://bit.ly/techtalk-vagrant

Got a question?

Please put your hand up at any point and I’ll take questions throughout

Whoami

Alex Coleman

Agenda

  • What is Vagrant?
  • Basic Vagrant commands
  • Vagrant case studies
    • Vagrant Ubuntu Desktop
    • Using host machine Github ssh keys
    • Building containers as root
    • Web app port forwarding
    • Accessing host files
  • How do I get it?
  • Further coding

What is Vagrant?

Is this a familiar conversation?

  • "I've got some really cool code, want a go?"

  • "Yes sure, send it to me!"

  • "Err, it's giving me an error?"

  • "Well, it worked on my machine..."

What is Vagrant?


  • Open source tool for building and managing virtual environments in a single workflow
  • Using a single file you can specify the exact environment required for your workflow
  • Works with VirtualBox, VMware and other providers
  • Runs on Windows, macOS and Linux

Basic Vagrant commands

  • Creating a virtual machine running Ubuntu 18.04 LTS 64-bit
          
            $ vagrant init hashicorp/bionic64
            $ vagrant up
            $ vagrant ssh
          
        

Vagrantfile

A single script to initialize a VM

          
            # Basic Vagrantfile
            Vagrant.configure("2") do |config|
              config.vm.box = "hashicorp/bionic64"
            end
          
        

Vagrant boxes

  • Configuring and setting up virtual machines can be complicated and slow. Vagrant allows you bypass this by using existing base images called 'boxes'.
          
            $ vagrant box add continuumio/anaconda3
          
        

Provisioning

  • Set up what you want on your virtual box
          
  #!/usr/bin/env bash
  # bootstrap.sh

  apt-get update
  apt-get install -y apache2
  if ! [ -L /var/www ]; then
    rm -rf /var/www
    ln -fs /vagrant /var/www
  fi
          
        
          
  # line in your Vagrantfile
  Vagrant.configure("2") do |config|
    config.vm.box = "hashicorp/bionic64"
    config.vm.provision :shell, path: "bootstrap.sh"
  end
          
        

Cleaning up Vagrant

Switching off your Vagrant VM

          
            $ vagrant suspend

            $ vagrant halt

            $ vagrant destroy
          
        

Vagrant case studies

Test all this code out yourself using the vagrant_examples directory in the GitHub Repo for this presentation

https://github.com/ARCLeeds/Techtalks/tree/master/techtalk-vagrant/vagrant_examples

Vagrant case studies

  • Get yourself a Ubuntu Desktop
          
            $ git clone https://github.com/heidemn/vagrant-bionic-desktop.git
            $ cd vagrant-bionic-desktop
            $ vagrant up 2>&1 | tee log.txt
            $ vagrant reload
          
        

Vagrant case studies

  • Using your host ssh keys on a Vagrant box
          
      Vagrant.configure("2") do |config|
        config.vm.box = "hashicorp/bionic64"
        # line to forward active ssh agent from host to guest
        config.ssh.forward_agent = true
        config.vm.provision :shell, path: "bootstrap.sh"
      end
          
        

Vagrant case studies

  • Accessing host machine files on Vagrant
          
                      $ vagrant up
                      ...
                      $ vagrant ssh
                      ...
                      vagrant@bionic64:~$ ls /vagrant
                      Vagrantfile
          
        

Vagrant case studies

  • So you've got a nice shiny/flask app you want to develop and test before putting it on a server?
          
    Vagrant.configure(2) do |config|
      config.vm.box = "hashicorp/bionic64"
      # run bootstrap.sh when vagrant sets up the VM
      config.vm.provision :shell, path: "bootstrap.sh"
      # networking through Vagrant
      config.vm.network :forwarded_port, guest: 4567, host: 4567
    end
          
        

Vagrant case studies

  • Building Docker containers
          
        Vagrant.configure("2") do |config|
          config.vm.box = "pogosoftware/ubuntu-18.04-docker"
          config.vm.network "forwarded_port", guest: 8000, host: 8000
          config.vm.provision "shell", inline: <<-SHELL
          git clone https://github.com/datawire/hello-world.git
          SHELL
        end
          
        

Getting Vagrant

  • IT Services can install Vagrant on your workstation via submitting an install request.

Submit an install request now!

Further coding/reading

Coming up

  • The next TechTalk (date TBC) will be on containerisation!

  • Join us for an informal chat with the Research Computing Team

  • Subscribe to the Research Computing mailing list by sending an email to research-computing-join@lists.leeds.ac.uk with Subscribe as the subject

Please give us some feedback!