# Run Vagrant machines

Vagrant can make its machines as clones of an OSMates template, and manage them with `vagrant up`, `vagrant ssh`, and `vagrant destroy`.

You need Vagrant 2.4 or later, the [command line](https://osmates.com/docs/install#add-the-command-line), and a copy of the OSMates repository.

## Install the plugin

From the repository's folder:

```sh
cd integrations/vagrant
gem build vagrant-osmates.gemspec
vagrant plugin install ./vagrant-osmates-0.1.0.gem
```

## Make a machine from a template

1. [Make a template](https://osmates.com/docs/templates#make-a-template) from a machine set up the way new machines should start.
2. Put this in your project's `Vagrantfile`, with your template's name:

```ruby
Vagrant.configure("2") do |config|
  config.vm.provider :osmates do |osmates|
    osmates.template = "debian-dev"
  end
end
```

3. Start the machine and open a shell in it:

```sh
vagrant up --provider=osmates
vagrant ssh
```

The machine appears in the library, named after the project folder and the Vagrant machine, such as `web-default-3f9a`. To skip `--provider`, set `VAGRANT_DEFAULT_PROVIDER=osmates`. `vagrant halt` shuts the guest down, and `vagrant destroy` deletes the machine.

To change CPUs and memory, add them to the provider block. They apply at each `vagrant up` and `vagrant reload`. Without them, the machine keeps the template's.

```ruby
config.vm.provider :osmates do |osmates|
  osmates.template = "debian-dev"
  osmates.cpus = 4
  osmates.memory = "8G"
end
```

The plugin's README lists every setting. To wait when your edition runs no more machines at once, set `osmates.queue = true`.

## Reach macOS and Windows guests

Vagrant reaches a guest with ssh, so a macOS or Windows template needs an ssh server:

- macOS needs [Remote Login](https://osmates.com/docs/ssh#turn-on-remote-login-in-macos), without a password for provisioners that use `sudo`.
- Windows needs [OpenSSH Server](https://osmates.com/docs/ssh#add-openssh-server-to-windows), and `config.vm.communicator = "winssh"` in the `Vagrantfile`.

## Share folders and forward ports

The project folder appears at `/vagrant` in the guest. Each synced folder becomes a [shared folder](https://osmates.com/docs/files#share-a-folder-of-your-mac), on Linux and macOS guests only:

```ruby
config.vm.synced_folder "data", "/srv/data", osmates__read_only: true
config.vm.network "forwarded_port", guest: 80, host: 8080
```

A writable folder that holds a Git repository gets a warning. Programs in the guest can change its hooks, which run on your Mac.

Forwarded ports follow the limits in [Connect with ssh and forward ports](https://osmates.com/docs/ssh).

## Package a machine as a box

`vagrant package` writes a box that holds the machine's disk:

```sh
vagrant package --output web.box
vagrant box add --name web web.box
```

The first `vagrant up` from that box imports the disk as a template, and later machines clone it. `vagrant box remove` leaves that template in the library, so delete it with `osmates vm delete`.

A disk box suits Linux. For Windows and macOS, use a box that names a template, as the plugin's README shows. That template must exist on each Mac that uses the box.

## Limits

- A Vagrant snapshot holds the disk, not the memory. Restoring one restarts the guest.
