bmuschko / gradle-vagrant-plugin

Gradle plugin for managing Vagrant boxes.
Apache License 2.0
48 stars 11 forks source link
gradle-plugin vagrant

= Gradle Vagrant plugin image:https://github.com/bmuschko/gradle-vagrant-plugin/workflows/Build%20and%20Release%20%5BLinux%5D/badge.svg["Build Status", link="https://github.com/bmuschko/gradle-vagrant-plugin/actions?query=workflow%3A%22Build+and+Release+%5BLinux%5D%22"]

image:https://hyzxph.media.zestyio.com/blog-vagrant.svg[Vagrant Logo, scaledwidth="5%"]

Gradle plugin for managing link:http://www.vagrantup.com/[Vagrant] boxes.

== Usage

To use the plugin, include in your build script:

[source,groovy]

buildscript { repositories { mavenCentral() }

dependencies {
    classpath 'com.bmuschko:gradle-vagrant-plugin:3.0.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.

[source,groovy]

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

=== Custom task types

The base plugin provides the following custom task types:

[options="header"] |======= |Type |Description |link:http://bmuschko.github.io/gradle-vagrant-plugin/docs/groovydoc/com/bmuschko/gradle/vagrant/tasks/VagrantDestroy.html[VagrantDestroy] |Stops the running machine Vagrant is managing and destroys all resources. |link:http://bmuschko.github.io/gradle-vagrant-plugin/docs/groovydoc/com/bmuschko/gradle/vagrant/tasks/VagrantHalt.html[VagrantHalt] |Shuts down the running machine Vagrant is managing. |link:http://bmuschko.github.io/gradle-vagrant-plugin/docs/groovydoc/com/bmuschko/gradle/vagrant/tasks/VagrantReload.html[VagrantReload] |The equivalent of running a halt followed by an up. |link:http://bmuschko.github.io/gradle-vagrant-plugin/docs/groovydoc/com/bmuschko/gradle/vagrant/tasks/VagrantSsh.html[VagrantSsh] |Executes a SSH command on the Vagrant machine. |link:http://bmuschko.github.io/gradle-vagrant-plugin/docs/groovydoc/com/bmuschko/gradle/vagrant/tasks/VagrantSshConfig.html[VagrantSshConfig] |Outputs the valid configuration for an SSH config file to SSH. |link:http://bmuschko.github.io/gradle-vagrant-plugin/docs/groovydoc/com/bmuschko/gradle/vagrant/tasks/VagrantStatus.html[VagrantStatus] |Outputs the state of the machines Vagrant is managing. |link:http://bmuschko.github.io/gradle-vagrant-plugin/docs/groovydoc/com/bmuschko/gradle/vagrant/tasks/VagrantSuspend.html[VagrantSuspend] |Suspends the guest machine Vagrant is managing. |link:http://bmuschko.github.io/gradle-vagrant-plugin/docs/groovydoc/com/bmuschko/gradle/vagrant/tasks/VagrantUp.html[VagrantUp] |Creates and configures guest machines according to your Vagrantfile. |=======

=== Extension properties

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

[options="header"] |======= |Property name |Type |Default value |Description |boxDir |File |project.file("vagrant") |The directory the targeted Vagrant box resides in. |provider |String |virtualbox |The link:http://docs.vagrantup.com/v2/providers/index.html[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.

[options="header"] |======= |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.

[options="header"] |======= |Property name |Type |Default value |Description |validate |Boolean |true |Installation validation |=======

=== Example

[source,groovy]

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.

[source,groovy]

apply plugin: 'com.bmuschko.vagrant'

=== Default tasks

The plugin defines the following tasks:

[options="header"] |======= |Task name |Depends on |Type |vagrantDestroy |- |link:http://bmuschko.github.io/gradle-vagrant-plugin/docs/groovydoc/com/bmuschko/gradle/vagrant/tasks/VagrantDestroy.html[VagrantDestroy] |vagrantHalt |- |link:http://bmuschko.github.io/gradle-vagrant-plugin/docs/groovydoc/com/bmuschko/gradle/vagrant/tasks/VagrantHalt.html[VagrantHalt] |vagrantReload |- |link:http://bmuschko.github.io/gradle-vagrant-plugin/docs/groovydoc/com/bmuschko/gradle/vagrant/tasks/VagrantReload.html[VagrantReload] |vagrantResume |- |link:http://bmuschko.github.io/gradle-vagrant-plugin/docs/groovydoc/com/bmuschko/gradle/vagrant/tasks/VagrantResume.html[VagrantResume] |vagrantSshConfig |- |link:http://bmuschko.github.io/gradle-vagrant-plugin/docs/groovydoc/com/bmuschko/gradle/vagrant/tasks/VagrantSshConfig.html[VagrantSshConfig] |vagrantStatus |- |link:http://bmuschko.github.io/gradle-vagrant-plugin/docs/groovydoc/com/bmuschko/gradle/vagrant/tasks/VagrantStatus.html[VagrantStatus] |vagrantSuspend |- |link:http://bmuschko.github.io/gradle-vagrant-plugin/docs/groovydoc/com/bmuschko/gradle/vagrant/tasks/VagrantSuspend.html[VagrantSuspend] |vagrantUp |- |link:http://bmuschko.github.io/gradle-vagrant-plugin/docs/groovydoc/com/bmuschko/gradle/vagrant/tasks/VagrantUp.html[VagrantUp] |=======

=== Example

[source,groovy]

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

}