README.md 8.99 KB
Newer Older
Noe Nieto's avatar
Noe Nieto committed
1
# The box command
2

Noe Nieto's avatar
Noe Nieto committed
3
The `box` command is a small wrapper around Vagrant that allows you to manage virtual machines easily. Currently the supported providers are libvirt and Digital Ocean. For most commands you only need to provide the hostname.
4

5 6
## How it works

Noe Nieto's avatar
Noe Nieto committed
7
The `box` command uses `~/.config/ldh_developer/box/` as configuration directory.
8

Noe Nieto's avatar
Noe Nieto committed
9
Every folder inside the `box` folder represents a box, so for a box with hostname `floyd` the `$VAGRANT_HOME` directory is `~/.config/lds_developer/box/floyd`:
10 11

```bash
Noe Nieto's avatar
Noe Nieto committed
12
ls -l ~/.config/ldh_developer/box/
13 14 15 16
total 8
drwxr-xr-x 3 nnieto nnieto 4.0K Nov 27 21:46 floyd/
```

Noe Nieto's avatar
Noe Nieto committed
17 18 19
The `floyd` folder contains two files (`Vagrantfile` and `shim.yml`) and the vagrant hidden folder(`.vagrant`).

Once `box` creates a box, you can manage it with the libvirt, digital ocean and vagrant tools of your choice. Recommended tools are:
20 21 22 23 24 25

- [GNOME Vagrant Indicator](https://github.com/fffilo/gnome-vagrant-indicator)
- virt-manager (apt install virt-manager)

## Commands

Noe Nieto's avatar
Noe Nieto committed
26
`box` supports these commands:
27

Noe Nieto's avatar
Noe Nieto committed
28
- `status`: report the status of the virtual machines created/managed by `box`.
29 30 31 32 33 34 35 36 37 38 39
- `create`: create a new VM. You only need to provide the hostname.
- `up`: starts a VM.
- `halt`:  stops a VM.
- `destroy`: destroys the VM
- `ssh`: opens a ssh session directly to the VM as `vagrant` user.
- `playbook`: provisions a VM with an ansible playbook.
- `which`: prints the configuration directory path of a VM.
- `open`: opens the configuration directory of a VM with the current file manager (nautilus, dolphin, etc.)

### The `status` command

Noe Nieto's avatar
Noe Nieto committed
40
Prints a summary of the state of all VM's managed by `box`. Example:
41 42

```
Noe Nieto's avatar
Noe Nieto committed
43
$ box status
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
hostname 	 State  	 IP Address
nina 	     running	 192.168.121.185
pinta   	 shutoff	 <N/A>
stmaria 	 running	 192.168.121.178
Total 3 machine(s)
$
```

### The `create` command

This command creates a new VM. You only need to provide the hostname. But you can specify the ram size and the number of cpus.

The following example creates a VM with the hostname `usoland`. The default values for ram size is 512 MB, and 1 CPU.

```
Noe Nieto's avatar
Noe Nieto committed
59
$ box create usoland
60 61 62 63 64 65 66 67 68
Bringing machine 'usoland' up with 'libvirt' provider...
==> usoland: Checking if box 'debian/stretch64' is up to date...
[...]
==> usoland:  -- Memory:            512M
[...]
==> usoland:  -- Cpus:              1
[...]

```
Noe Nieto's avatar
Noe Nieto committed
69
The following example creates a VM with the hostname `usoland` with 4 cpus and 1GB of ram:
70 71

```
Noe Nieto's avatar
Noe Nieto committed
72
$ box create usoland --cpus 4 --ram 1024
73 74 75 76 77 78 79 80 81 82 83 84 85
Bringing machine 'usoland' up with 'libvirt' provider...
[...]
==> usoland:  -- Cpus:              4
[...]
==> usoland:  -- Memory:            1024M
[...]
==> usoland: Pruning invalid NFS exports. Administrator privileges will be required...
==> usoland: Creating shared folders metadata...
    usoland: Running ansible-playbook...
[...]

$
```
Noe Nieto's avatar
Noe Nieto committed
86 87 88 89 90 91
**Note**: Vagrant/libvirt needs root access to create and modify networks on the machine. Vagrant will try to use sudo and will request your passowrd.

Finally the following example creates a box using Digital Ocean as provider.

```
$ shipwright vm create starblade --provider digital_ocean --token 2f17c...808c50ae9
92

Noe Nieto's avatar
Noe Nieto committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
Creating ssh key required by Vagrant's Digital Ocean plugin ...
Bringing machine 'starblade' up with 'digital_ocean' provider...
==> starblade: Using existing SSH key: Vagrant
==> starblade: Creating a new droplet...
==> starblade: Assigned IP address: 104.248.176.67
[...]
Bringing machine 'starblade' up with 'digital_ocean' provider...
==> starblade: Creating new SSH key: Vagrant...
==> starblade: Creating a new droplet...
==> starblade: Assigned IP address: 178.128.79.235
[...]
```

**Known issue**: The create command freezes on this message: `==> starblade: Assigned IP address: ...` for some minutes before continuing to destroy the droplet. If you hit Ctrl-C shortly after the `.. Assigned IP adress [..]` messages, the droplet stays alive.

**default provider**: The default provider is `libvirt`, you need to specify `--provider digital_ocean` and  `--token` on the command line to spin the machine in Digital Ocean. If you don't have a token yet, you ca generate one on _Manage_ / _API_ section in your DO's control panel.
109

Noe Nieto's avatar
Noe Nieto committed
110
### The `up`, `halt` and `restart` commands
111

Noe Nieto's avatar
Noe Nieto committed
112
Use these commands to start, shutdown, or restart a box. As an example, the previous section we created one. If we check the status we find that it's running.
113 114

```
Noe Nieto's avatar
Noe Nieto committed
115
$ box status
116 117 118 119 120 121 122 123
hostname 	 State	 IP Address
usoland 	 running	 192.168.121.57
Total 1 machine(s)
$
```

Now, let's use the `halt` command.

Noe Nieto's avatar
Noe Nieto committed
124 125
```
box halt usoland
126 127 128 129 130 131
==> usoland: Halting domain...
$
```

We can see that the VM status is `shutoff`:
```
Noe Nieto's avatar
Noe Nieto committed
132
$ box status
133 134 135 136 137 138 139 140 141 142
hostname 	 State	 IP Address
usoland 	 shutoff	 <N/A>
Total 1 machine(s)

$
```

We use the `up` command to start the VM again.

```
Noe Nieto's avatar
Noe Nieto committed
143
$ box up usoland
144 145 146 147 148 149 150 151 152 153 154 155
Bringing machine 'usoland' up with 'libvirt' provider...
==> usoland: Checking if box 'debian/stretch64' is up to date...
==> usoland: Starting domain.
==> usoland: Waiting for domain to get an IP address...
==> usoland: Waiting for SSH to become available...
==> usoland: Creating shared folders metadata...
==> usoland: Exporting NFS shared folders...
==> usoland: Preparing to edit /etc/exports. Administrator privileges will be required...
==> usoland: Mounting NFS shared folders...
==> usoland: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> usoland: flag to force provisioning. Provisioners marked to run always will still run.
$
156 157
```

158
Now `status` shows that the vm is running.
159

160
```
Noe Nieto's avatar
Noe Nieto committed
161
$ box status
162
hostname 	 State	 IP Address
Noe Nieto's avatar
Noe Nieto committed
163
usoland 	 running	 192.168.121.57
164 165 166 167 168
Total 1 machine(s)

$
```

Noe Nieto's avatar
Noe Nieto committed
169 170 171
Now, `restart` is very straightforward.

```
Noe Nieto's avatar
Noe Nieto committed
172
$ box restart starblade
Noe Nieto's avatar
Noe Nieto committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
==> starblade: Halting domain...
Bringing machine 'starblade' up with 'libvirt' provider...
==> starblade: Checking if box 'debian/stretch64' is up to date...
==> starblade: Starting domain.
==> starblade: Waiting for domain to get an IP address...
==> starblade: Waiting for SSH to become available...
==> starblade: Creating shared folders metadata...
==> starblade: Exporting NFS shared folders...
==> starblade: Preparing to edit /etc/exports. Administrator privileges will be required...
==> starblade: Mounting NFS shared folders...
==> starblade: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> starblade: flag to force provisioning. Provisioners marked to run always will still run.

$
```

189 190 191 192 193
### The `ssh` command

The ssh command opens an ssh to the VM.

```bash
Noe Nieto's avatar
Noe Nieto committed
194
$ box ssh usoland
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
Linux usoland 4.9.0-8-amd64 #1 SMP Debian 4.9.130-2 (2018-10-27) x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Fri Nov 30 07:26:45 2018 from 192.168.121.1
vagrant@usoland:~$ logout
Connection to 192.168.121.57 closed.

$
```

### The `which` command

The `which` command prints the full path to the `$VAGRANT_HOME` directory of the VM.

```
Noe Nieto's avatar
Noe Nieto committed
215 216
$ box which usoland
/home/nnieto/.config/ldh_developer/box/usoland
217 218 219 220 221 222 223 224 225

$
```

### The `open` command

This command opens a new window of your file manager pointing to the `$VAGRANT_HOME` of the VM.

```bash
Noe Nieto's avatar
Noe Nieto committed
226
./box open --hostname=starone0
227 228 229 230 231 232
[... nautilis shows up ...]
```


### The `ansible` command

Noe Nieto's avatar
Noe Nieto committed
233
This command allows you to run an ansible playbook against one of the VMs. For example: I have two VM's managed by `box`:
234 235

```
Noe Nieto's avatar
Noe Nieto committed
236
./box status
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
hostname 	 State	 IP Address
usoland 	 shutoff	 <N/A>
going-merry 	 running	 192.168.121.89
Total 2 machine(s)
```

Let's install rust on`going-merry`. The playbook looks like this:

```yaml
---
- name: Rust Playbook
  become: yes
  hosts: all
  tasks:
    - name: Install Rust
      apt: name=rustc update_cache=yes
```

Let's save it as `rust.yml` and run the playbook against `going-merry`:

```
Noe Nieto's avatar
Noe Nieto committed
258
./box playbook going-merry rust.yaml
259 260 261 262 263 264 265 266 267 268

PLAY [Rust Playbook] *************************************************************************************

TASK [Gathering Facts] ***********************************************************************************
ok: [going-merry]

TASK [Install Rust] **************************************************************************************
changed: [going-merry]

PLAY RECAP ***********************************************************************************************
Noe Nieto's avatar
Noe Nieto committed
269
going-merry                      : ok=2    changed=1    unreachable=0    failed=0
270 271 272 273 274 275

```

Now I can use `rustc` on the VM:

```bash
Noe Nieto's avatar
Noe Nieto committed
276
./box ssh going-merry
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
Linux going-merry 4.9.0-8-amd64 #1 SMP Debian 4.9.130-2 (2018-10-27) x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Wed Nov 28 23:17:19 2018 from 192.168.121.1

vagrant@going-merry:~$ whereis rustc
rustc: /usr/bin/rustc /usr/share/rustc /usr/share/man/man1/rustc.1.gz

vagrant@going-merry:~$ logout
Connection to 192.168.121.89 closed.
```
293