|
| 1 | +# Preparation Material for Session 03 |
| 2 | + |
| 3 | +Install VirtualBox and Vagrant on your computer. |
| 4 | + |
| 5 | + |
| 6 | +### Vagrant & VirtualBox |
| 7 | + |
| 8 | +In case you are running MacOS or Windows you might find helpful [installation instructions for your host operating system in the linked guide](https://www.itu.dk/people/ropf/blog/vagrant_install.html). Note, Vagrant cannot be run in a VirtualBox VM. This means that you should install Vagrant and related plugins in your host OS (Windows/MacOS) if you are running Linux in a virtual box already. This is because vagrant creates VMs, and creating VMs from a VM can cause problems. |
| 9 | + |
| 10 | +Install Virtualbox: |
| 11 | + |
| 12 | +```bash |
| 13 | +sudo apt install virtualbox virtualbox-ext-pack |
| 14 | +``` |
| 15 | + |
| 16 | +Since the packaged Vagrant in the linked repositories is in a bit buggy version we install it directly from the package provided by the tool vendor: |
| 17 | + |
| 18 | +```bash |
| 19 | +wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg |
| 20 | +echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list |
| 21 | +sudo apt update && sudo apt install vagrant |
| 22 | +``` |
| 23 | + |
| 24 | +After installing Vagrant, we need to install a Vagrant plugin that we will need later: |
| 25 | + |
| 26 | +```bash |
| 27 | +vagrant plugin install vagrant-digitalocean |
| 28 | +vagrant plugin install vagrant-scp |
| 29 | +vagrant plugin install vagrant-vbguest |
| 30 | +vagrant plugin install vagrant-reload |
| 31 | +``` |
| 32 | + |
| 33 | +Save the following into a file called `Vagrantfile` in your current directory: |
| 34 | + |
| 35 | +```ruby |
| 36 | +# -*- mode: ruby -*- |
| 37 | +# vi: set ft=ruby : |
| 38 | + |
| 39 | +Vagrant.configure("2") do |config| |
| 40 | + |
| 41 | + config.vm.box = "bento/ubuntu-22.04" |
| 42 | + |
| 43 | + config.vm.provider "virtualbox" do |vb| |
| 44 | + # Customize the amount of memory on the VM: |
| 45 | + vb.memory = "1024" |
| 46 | + end |
| 47 | +end |
| 48 | +``` |
| 49 | + |
| 50 | +Now, try to run from your current directory (the one in which you saved the `Vagrantfile`): |
| 51 | + |
| 52 | +```bash |
| 53 | +vagrant up |
| 54 | +``` |
| 55 | + |
| 56 | +The command downloads the OS image `bento/ubuntu-22.04` and brings up a virtual machine on your computer (with Virtualbox as backend). |
| 57 | +It will take some time since the corresponding OS image has to be downloaded first. That image will be cached on your disk, i.e., following VM instantiations of the same image will be faster. |
| 58 | +Observe that no error message is displayed. If so, and in case you cannot find a solution for it, we will look at it in class. |
| 59 | + |
| 60 | + |
| 61 | +In case you did not receive any error message, for now, you can run: |
| 62 | + |
| 63 | +```bash |
| 64 | +vagrant destroy |
| 65 | +``` |
| 66 | + |
| 67 | +### Intro to VMs with Vagrant |
| 68 | + |
| 69 | + > Vagrant is a tool for building and managing virtual machine environments in a single workflow. With an easy-to-use workflow and focus on automation, Vagrant lowers development environment setup time, increases production parity, and makes the "works on my machine" excuse a relic of the past. |
| 70 | + > |
| 71 | + > Vagrant provides easy to configure, reproducible, and portable work environments built on top of industry-standard technology and controlled by a single consistent workflow to help maximize the productivity and flexibility of you and your team. |
| 72 | + > |
| 73 | + > https://www.vagrantup.com |
| 74 | +
|
| 75 | +In case you are in doubt about anything in the following quick intro, check the [brief and really good documentation](https://www.vagrantup.com/docs/). |
| 76 | + |
| 77 | +#### Initialize & Start a VM |
| 78 | + |
| 79 | +You can start your setup by creating a `Vagrantfile`. The argument to `vagrant init` is the name of the *box*. That is, |
| 80 | + |
| 81 | +```bash |
| 82 | +$ mkdir vm |
| 83 | +$ cd vm |
| 84 | +$ vagrant init bento/ubuntu-22.04 |
| 85 | +$ vagrant up |
| 86 | +``` |
| 87 | + |
| 88 | + > After running the above commands, you will have a fully functional VM in VirtualBox running Ubuntu 22.04 LTS 64-bit. You can SSH into this machine with `vagrant ssh`, and when you are done playing around, you can terminate the virtual machine with `vagrant destroy`. |
| 89 | +
|
| 90 | + |
| 91 | +#### Accessing a VM |
| 92 | + |
| 93 | +You log onto a VM with SSH as in the following. |
| 94 | + |
| 95 | +```bash |
| 96 | +$ vagrant ssh |
| 97 | +``` |
| 98 | + |
| 99 | +You can leave the guest VM with: |
| 100 | + |
| 101 | +```bash |
| 102 | +$ vagrant@vagrant: exit |
| 103 | +``` |
| 104 | + |
| 105 | +#### Teardown |
| 106 | + |
| 107 | +A running VM can be ... |
| 108 | + |
| 109 | + * put to sleep |
| 110 | + |
| 111 | + ```bash |
| 112 | + $ vagrant suspend |
| 113 | + ``` |
| 114 | + |
| 115 | + * turned off |
| 116 | + |
| 117 | + ```bash |
| 118 | + $ vagrant halt |
| 119 | + ``` |
| 120 | + |
| 121 | + * removed completely |
| 122 | + |
| 123 | + ```bash |
| 124 | + $ vagrant destroy |
| 125 | + ``` |
| 126 | + |
| 127 | +#### State of VMs |
| 128 | + |
| 129 | +The following command will display a list of all VMs and their respective states, i.e., `saved`, `poweroff`, `active`, etc. |
| 130 | + |
| 131 | +```bash |
| 132 | +$ vagrant global-status |
| 133 | +``` |
| 134 | + |
| 135 | +#### VMs with other Operating Systems |
| 136 | + |
| 137 | +That is, you can quickly setup development environments for many operating systems. For example, in the following the initialization and use of a FreeBSD and Windows VM. |
| 138 | + |
| 139 | +```bash |
| 140 | +$ mkdir vm_freebsd |
| 141 | +$ cd vm_freebsd |
| 142 | +$ vagrant init freebsd/FreeBSD-12.3-STABLE |
| 143 | +$ vagrant up |
| 144 | +``` |
| 145 | + |
| 146 | +```bash |
| 147 | +$ mkdir vm_win |
| 148 | +$ cd vm |
| 149 | +$ vagrant init senglin/win-10-enterprise-vs2015community |
| 150 | +$ vagrant up |
| 151 | +``` |
| 152 | + |
| 153 | +You can find a catalog of available boxes at https://app.vagrantup.com/boxes/search |
| 154 | + |
| 155 | +<img src="images/vm_catalogue.png" width="80%"> |
| 156 | + |
| 157 | + |
| 158 | +### Vagrant for **local** development setup |
| 159 | + |
| 160 | +At last, apply `vagrant` for a usual scenario: reproduction of development environments. |
| 161 | + |
| 162 | +The branch [`VMify_local`](https://github.com/itu-devops/flask-minitwit-mongodb/tree/VMify_local) of repository https://github.com/itu-devops/flask-minitwit-mongodb contains an _ITU-MiniTwit_ application similar to the one you took over last week. It was refactored to use a MongoDB database instead of an SQLite3 database. |
| 163 | + |
| 164 | +The application is a two-layered application with a frontend server (called `webserver`) and a database server (called `dbserver`). |
| 165 | +With the VM setup from the [`VMify_local`](https://github.com/itu-devops/flask-minitwit-mongodb/tree/VMify_local) branch, you have a complete running setup of the entire application that you can recreate on you computer, e.g., for local development. |
| 166 | + |
| 167 | +To do so, clone the repository, switch to the right branch, check and try to develop a feeling for the contents of the [`Vagrantfile`](https://github.com/itu-devops/flask-minitwit-mongodb/blob/VMify_local/Vagrantfile), and start-up the VMs. |
| 168 | + |
| 169 | +```bash |
| 170 | +$ git clone https://github.com/itu-devops/flask-minitwit-mongodb.github |
| 171 | +$ cd flask-minitwit-mongodb |
| 172 | +$ git checkout VMify_local |
| 173 | +$ vagrant up |
| 174 | +Bringing machine 'dbserver' up with 'virtualbox' provider... |
| 175 | +Bringing machine 'webserver' up with 'virtualbox' provider... |
| 176 | +... |
| 177 | +``` |
| 178 | + |
| 179 | +If everything starts error-free, you will find the running _ITU-MiniTwit_ at http://192.168.20.3:5000. With `vagrant ssh webserver` you can access the machine with the frontend code. |
| 180 | + |
0 commit comments