vagrantutil

package module
v0.0.0-...-c8f4598 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2018 License: BSD-3-Clause Imports: 12 Imported by: 1

README

Vagrantutil GoDoc

Vagrantutil is a toolset for managing Vagrant boxes via an idiomatic Go (Golang) API. The package is work in progress, so please vendor it. Checkout the examples below for the usage.

Install

go get github.com/koding/vagrantutil

Usage and Examples

package main

import (
	"log"

	"github.com/koding/vagrantutil"
)

func main() {
	vagrant, _ := vagrantutil.NewVagrant("myfolder")

	vagrant.Create(`# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu/trusty64"
  config.vm.hostname = "vagrant"

  config.vm.provider "virtualbox" do |vb|
    # Use VBoxManage to customize the VM. For example to change memory:
    vb.customize ["modifyvm", :id, "--memory", "2048", "--cpus", "2"]
  end
end
`)

	status, _ := vagrant.Status() // prints "NotCreated"

	// starts the box
	output, _ := vagrant.Up()

	// print the output
	for line := range output {
		log.Println(line)
	}

	// stop/halt the box
	vagrant.Halt()

	// destroy the box
	vagrant.Destroy()
}

Documentation

Overview

Package vagrantutil is a high level wrapper around Vagrant which provides an idiomatic go API.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBoxAlreadyExists  = errors.New("box already exists")
	ErrBoxInvalidVersion = errors.New("invalid box version")
	ErrBoxNotAvailable   = errors.New("box is not available")
	ErrBoxDownload       = errors.New("unable to download the box")
	ErrVirtualBox        = errors.New("VirtualBox is missing or not operational")
)

The following errors are returned by the Wait function:

Functions

func Wait

func Wait(out <-chan *CommandOutput, err error) error

Wait is a convenience function that consumes Vagrant output looking for well-known errors.

Types

type Box

type Box struct {
	Name     string
	Provider string
	Version  string
}

Box represents a single line of `vagrant box list` output.

type CommandOutput

type CommandOutput struct {
	Line  string
	Error error
}

CommandOutput is the streaming output of a command

type Status

type Status int
const (
	// Some possible states:
	// https://github.com/mitchellh/vagrant/blob/master/templates/locales/en.yml#L1504
	Unknown Status = iota
	NotCreated
	Running
	Saved
	PowerOff
	Aborted
	Preparing
)

func (Status) String

func (i Status) String() string

type Vagrant

type Vagrant struct {
	// VagrantfilePath is the directory with specifies the directory where
	// Vagrantfile is being stored.
	VagrantfilePath string

	// ProviderName overwrites the default provider used for the Vagrantfile.
	ProviderName string

	// ID is the unique ID of the given box.
	ID string

	// State is populated/updated if the Status() or List() method is called.
	State string

	// Log is used for logging output of vagrant commands in debug mode.
	Log logging.Logger
}

func NewVagrant

func NewVagrant(path string) (*Vagrant, error)

NewVagrant returns a new Vagrant instance for the given path. The path should be unique. If the path already exists in the system it'll be used, if not a new setup will be createad.

func (*Vagrant) BoxAdd

func (v *Vagrant) BoxAdd(box *Box) (<-chan *CommandOutput, error)

BoxAdd executes "vagrant box add" for the given box. The returned channel contains the output stream. At the end of the output, the error is put into the Error field if there is any.

TODO(rjeczalik): BoxAdd does not support currently adding boxes directly from files.

func (*Vagrant) BoxList

func (v *Vagrant) BoxList() ([]*Box, error)

BoxList executes "vagrant box list", parses the output and returns all available base boxes.

func (*Vagrant) BoxRemove

func (v *Vagrant) BoxRemove(box *Box) (<-chan *CommandOutput, error)

BoxRemove executes "vagrant box remove" for the given box. The returned channel contains the output stream. At the end of the output, the error is put into the Error field if there is any.

func (*Vagrant) Create

func (v *Vagrant) Create(vagrantFile string) error

Create creates the vagrantFile in the pre initialized vagrant path.

func (*Vagrant) Destroy

func (v *Vagrant) Destroy() (<-chan *CommandOutput, error)

Destroy executes "vagrant destroy". The returned reader contains the output stream. The client is responsible of calling the Close method of the returned reader.

func (*Vagrant) Halt

func (v *Vagrant) Halt() (<-chan *CommandOutput, error)

Halt executes "vagrant halt". The returned reader contains the output stream. The client is responsible of calling the Close method of the returned reader.

func (*Vagrant) List

func (v *Vagrant) List() ([]*Vagrant, error)

List returns all available boxes on the system. Under the hood it calls "global-status" and parses the output.

func (*Vagrant) PowerOff

func (v *Vagrant) PowerOff() (<-chan *CommandOutput, error)

PowerOff executes "vagrant halt -f". The returned reader contains the output stream. The client is responsible of calling the Close method of the returned reader.

func (*Vagrant) Provider

func (v *Vagrant) Provider() (string, error)

func (*Vagrant) SSH

func (v *Vagrant) SSH(command string) (<-chan *CommandOutput, error)

SSH executes "vagrant ssh" for the given vagrantfile. The returned channel contains the output stream. At the end of the output, the error is put into the Error field if there is any.

func (*Vagrant) SSHConfig

func (v *Vagrant) SSHConfig() (string, error)

Runs "ssh-config" and returns the output.

func (*Vagrant) Status

func (v *Vagrant) Status() (s Status, err error)

Status returns the state of the box, such as "Running", "NotCreated", etc...

func (*Vagrant) Up

func (v *Vagrant) Up() (<-chan *CommandOutput, error)

Up executes "vagrant up" for the given vagrantfile. The returned channel contains the output stream. At the end of the output, the error is put into the Error field if there is any.

func (*Vagrant) Version

func (v *Vagrant) Version() (string, error)

Version returns the current installed vagrant version

type Waiter

type Waiter struct {
	// OutputFunc, when non-nil, is called each time a Wait method receives
	// a line of output from command channel.
	OutputFunc func(string)
}

Waiter is used to consume output channel from a Vagrant command, parsing each line looking for an error when command execution fails.

func (*Waiter) Wait

func (w *Waiter) Wait(out <-chan *CommandOutput, err error) error

Wait is a convenience method that consumes Vagrant output looking for well-known errors.

It returns an error variable for any known error condition, which makes it easier for the caller to recover from failure.

If OutputFunc is non-nil, it's called on each line of output received from the out channel.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL