/gradle-vagrant-plugin

Gradle plugin for managing Vagrant boxes.

Primary LanguageGroovyApache License 2.0Apache-2.0

Gradle Vagrant plugin

Vagrant Logo

Gradle plugin for managing Vagrant boxes.

Usage

To use the plugin, include in your build script:

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.bmuschko:gradle-vagrant-plugin:2.0'
    }
}

Base plugin

If you want to create your own task for managing Vagrant, you should go with the base plugin. This option is usually helpful if you are dealing with multiple VMs in parallel. The base plugin provides all custom task types and preconfigures them with sensible defaults. To use the base plugin, add the following apply notation to your build script.

apply plugin: 'com.bmuschko.vagrant-base'

Custom task types

The base plugin provides the following custom task types:

Type Description

VagrantDestroy

Stops the running machine Vagrant is managing and destroys all resources.

VagrantHalt

Shuts down the running machine Vagrant is managing.

VagrantReload

The equivalent of running a halt followed by an up.

VagrantSsh

Executes a SSH command on the Vagrant machine.

VagrantSshConfig

Outputs the valid configuration for an SSH config file to SSH.

VagrantStatus

Outputs the state of the machines Vagrant is managing.

VagrantSuspend

Suspends the guest machine Vagrant is managing.

VagrantUp

Creates and configures guest machines according to your Vagrantfile.

Extension properties

The base plugin defines the following extension properties in the vagrant closure:

Property name Type Default value Description

boxDir

File

project.projectDir

The directory the targeted Vagrant box resides in.

provider

String

virtualbox

The backend provider to be used.

The recommended way for providing values to the Vagrantfile from the outside is to use environment variables. This is made possible through the nested configuration element environmentVariables. This element exposes the method variable that takes two parameters: the key and value for a environment variable. For each invocation of this method a new key/value pair is added to internal property named variables.

Property name Type Default value Description

variables

Map<String, String>

[:]

Provided environment variables as key/value pairs.

By default the plugin validates the installation of the Vagrant runtime and the selected provider. This validation logic can be disabled from your buildscript. This is made possible through the nested configuration element installation. This element exposes the method validate that take a single parameter.

Property name Type Default value Description

validate

Boolean

true

Installation validation

Example

import com.bmuschko.gradle.vagrant.tasks.VagrantUp
import com.bmuschko.gradle.vagrant.tasks.VagrantDestroy

ext {
    virtualBoxDir = file('virtualbox-box')
    fusionBoxDir = file('fusion-box')
    fusionProvider = 'vmware_fusion'
}

task startVirtualBoxVm(type: VagrantUp) {
    description = 'Starts VM machine running on VirtualBox.'
    group = 'VirtualBox VM'
    boxDir = virtualBoxDir
}

task stopVirtualBoxVm(type: VagrantDestroy) {
    description = 'Stops VM machine running on VirtualBox.'
    group = 'VirtualBox VM'
    boxDir = virtualBoxDir
}

task startFusionVm(type: VagrantUp) {
    description = 'Starts VM machine running on VMware Fusion.'
    group = 'Fusion VM'
    boxDir = fusionBoxDir
    provider = fusionProvider
}

task stopFusionVm(type: VagrantDestroy) {
    description = 'Stops VM machine running on VMware Fusion.'
    group = 'Fusion VM'
    boxDir = fusionBoxDir
    provider = fusionProvider
}

Convention plugin

If you want the plugin to preconfigure commonly-used tasks for you, you should go with the full-fledged convention plugin. This plugin is a viable option if you only have to deal with a single VM. To use the convention plugin, add the following apply notation to your build script.

apply plugin: 'com.bmuschko.vagrant'

Default tasks

The plugin defines the following tasks:

Task name Depends on Type

vagrantDestroy

-

VagrantDestroy

vagrantHalt

-

VagrantHalt

vagrantReload

-

VagrantReload

vagrantResume

-

VagrantResume

vagrantSshConfig

-

VagrantSshConfig

vagrantStatus

-

VagrantStatus

vagrantSuspend

-

VagrantSuspend

vagrantUp

-

VagrantUp

Example

vagrant {
    boxDir = file('~/dev/my-vagrant-box')

    environmentVariables {
        variable 'IP', '192.168.1.33'
        variable 'OPERATINGSYSTEM', 'redhat'
    }

    installation {
        validate = false
    }
}

import com.bmuschko.gradle.vagrant.tasks.Vagrant
import com.bmuschko.gradle.vagrant.tasks.VagrantSsh

task vagrantListsBoxes(type: Vagrant) {
    description = 'Outputs a list of available Vagrant boxes.'
    commands = ['box', 'list']
}

task vagrantEcho(type: VagrantSsh) {
    description = 'Runs remote SSH command in Vagrant box.'
    sshCommand = "echo 'hello'"

    dependsOn vagrantUp
    finalizedBy vagrantDestroy
}