Documentation ¶
Overview ¶
Package vagrant is a golang wrapper around the vagrant command-line utility. Full documentation can be found in the README on github:
https://github.com/bmatcuk/go-vagrant
Example ¶
client, err := NewVagrantClient("example") if err != nil { fmt.Println("Got error while creating client:", err) os.Exit(-1) } // Let's start bringing up the vm upcmd := client.Up() upcmd.Verbose = true fmt.Println("Bringing up the vm") if err := upcmd.Start(); err != nil { fmt.Println("Error bringing up vm:", err) os.Exit(-1) } // while we're waiting, let's get version info vercmd := client.Version() if err := vercmd.Run(); err != nil { fmt.Println("Error retrieving version info:", err) } // now wait for up to finish if err := upcmd.Wait(); err != nil { fmt.Println("Error waiting for up:", err) os.Exit(-1) } fmt.Println("\n\nInstalled Vagrant version:", vercmd.InstalledVersion) // Get vm status statuscmd := client.Status() if err := statuscmd.Run(); err != nil { fmt.Println("Error getting status:", err) } else { for vm, status := range statuscmd.Status { fmt.Printf("%v: %v\n", vm, status) } } // Destroy vm if err := client.Destroy().Run(); err != nil { fmt.Println("Error destroying vm:", err) os.Exit(-1) }
Output:
Index ¶
- type BaseCommand
- type Box
- type BoxAddCommand
- type BoxListCommand
- type BoxListResponse
- type CheckSumType
- type DestroyCommand
- type ErrorResponse
- type ForwardedPort
- type GlobalStatus
- type GlobalStatusCommand
- type GlobalStatusResponse
- type HaltCommand
- type MachineNameArgument
- type OutputParser
- type PortCommand
- type PortResponse
- type ProvisionCommand
- type ProvisionersArgument
- type ProvisioningArgument
- type ProvisioningOption
- type ReloadCommand
- type ResumeCommand
- type SSHConfig
- type SSHConfigCommand
- type SSHConfigResponse
- type StatusCommand
- type StatusResponse
- type SuspendCommand
- type UpCommand
- type UpResponse
- type VMInfo
- type VagrantClient
- func (client *VagrantClient) BoxAdd(location string) *BoxAddCommand
- func (client *VagrantClient) BoxList() *BoxListCommand
- func (client *VagrantClient) Destroy() *DestroyCommand
- func (client *VagrantClient) GlobalStatus() *GlobalStatusCommand
- func (client *VagrantClient) Halt() *HaltCommand
- func (client *VagrantClient) Port() *PortCommand
- func (client *VagrantClient) Provision() *ProvisionCommand
- func (client *VagrantClient) Reload() *ReloadCommand
- func (client *VagrantClient) Resume() *ResumeCommand
- func (client *VagrantClient) SSHConfig() *SSHConfigCommand
- func (client *VagrantClient) Status() *StatusCommand
- func (client *VagrantClient) Suspend() *SuspendCommand
- func (client *VagrantClient) Up() *UpCommand
- func (client *VagrantClient) Version() *VersionCommand
- type VersionCommand
- type VersionResponse
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BaseCommand ¶
type BaseCommand struct { OutputParser // Context for the running command - nil means none Context context.Context // Additional arguments to pass to the command. go-vagrant attempts to define // each argument as a field on the struct, but future versions of vagrant may // add options that didn't exist at the time of authoring. You can use this // to pass options to vagrant that go-vagrant doesn't know about. AdditionalArgs []string // Env is merged with the current process's environment and passed to the // vagrant command. Each entry is a "key=value" pair with later keys taking // precedence in the case of duplicates (keys here will take precedence over // keys in the current environment, too). Env []string // The underlying process, once it has been started with Run() or Start() Process *os.Process // ProcessState contains information about the process after it has exited. // Available after Run() or Wait(). ProcessState *os.ProcessState // contains filtered or unexported fields }
BaseCommand adds base functionality and fields for all commands constructed from the VagrantClient.
func (*BaseCommand) Start ¶
func (b *BaseCommand) Start() error
Start the command. You must call Wait() to complete execution.
func (*BaseCommand) Wait ¶
func (b *BaseCommand) Wait() error
Wait is used to wait on a command that was started with Start().
type Box ¶ added in v1.5.0
type Box struct { // The box name Name string // The box provider Provider string // The box version Version string }
Box defines a box from the box list command
type BoxAddCommand ¶ added in v1.6.0
type BoxAddCommand struct { BaseCommand ErrorResponse // Clean any temporary download files. This will prevent resuming a // previously started download. Clean bool // Overwrite an existing box if it exists Force bool // Name, url, or path of the box. Location string // Name of the box. Only has to be set if the box is provided as a path or // url without metadata. Name string // Checksum for the box Checksum string // Type of the supplied Checksum. Allowed values are vagrant.MD5, // vagrant.SHA1, vagrant.SHA256 CheckSumType CheckSumType }
A BoxAddCommand specifies the options and output of vagrant box add.
func (*BoxAddCommand) Start ¶ added in v1.6.0
func (cmd *BoxAddCommand) Start() error
Start the command. You must call Wait() to complete execution.
type BoxListCommand ¶ added in v1.5.0
type BoxListCommand struct { BaseCommand ErrorResponse BoxListResponse }
A BoxListCommand specifies the options and output of vagrant box list.
func (*BoxListCommand) Start ¶ added in v1.5.0
func (cmd *BoxListCommand) Start() error
Start the command. You must call Wait() to complete execution.
type BoxListResponse ¶ added in v1.5.0
type BoxListResponse struct { ErrorResponse // Boxes is a list of vagrant boxes. Boxes []*Box // contains filtered or unexported fields }
BoxListResponse is the output from the vagrant box list command
type CheckSumType ¶ added in v1.6.0
type CheckSumType string
const ( MD5 CheckSumType = "md5" SHA1 CheckSumType = "sha1" SHA256 CheckSumType = "sha256" )
type DestroyCommand ¶
type DestroyCommand struct { BaseCommand MachineNameArgument ErrorResponse // Destroy without confirmation (defaults to true because, when false, // vagrant will try to ask for confirmation, but can't because it's running // without a TTY so it errors). Force bool // Enable parallelism if the provider supports it (automatically enables // force, default: false) Parallel bool }
A DestroyCommand specifies the options and output of vagrant destroy.
func (*DestroyCommand) Start ¶
func (cmd *DestroyCommand) Start() error
Start the command. You must call Wait() to complete execution.
type ErrorResponse ¶ added in v1.1.0
type ErrorResponse struct { // If set, there was an error while running the vagrant command. Error error }
ErrorResponse adds the Error field to command output.
type ForwardedPort ¶
type ForwardedPort struct { // Port on the guest OS Guest int // Port on the host which forwards to the guest Host int }
ForwardedPort defines the host port that maps to a guest port.
type GlobalStatus ¶ added in v1.3.0
type GlobalStatus struct { // Id of the vagrant vm Id string // Name of the vagrant vm Name string // Provider of the vagrant vm Provider string // The State of the vagrant vm State string // Directory where the vagrant vm was created Directory string // AdditionalInfo has data which may be provided by vagrant in the future as // a map of string keys and values. AdditionalInfo map[string]string }
GlobalStatus has status information about a single Vagrant VM
type GlobalStatusCommand ¶ added in v1.3.0
type GlobalStatusCommand struct { BaseCommand GlobalStatusResponse // Prune will remove invalid entries. Prune bool }
GlobalStatusCommand specifies options and output from vagrant global-status
func (*GlobalStatusCommand) Run ¶ added in v1.3.0
func (cmd *GlobalStatusCommand) Run() error
Run the command
func (*GlobalStatusCommand) Start ¶ added in v1.3.0
func (cmd *GlobalStatusCommand) Start() error
Start the command. You must call Wait() to complete execution.
type GlobalStatusResponse ¶ added in v1.3.0
type GlobalStatusResponse struct { ErrorResponse // Status per Vagrant VM. Keys are vagrant vm ID's (ex: 1a2b3c4d) and values // are GlobalStatus objects. Status map[string]*GlobalStatus // contains filtered or unexported fields }
GlobalStatusResponse has the output from vagrant global-status
type HaltCommand ¶
type HaltCommand struct { BaseCommand MachineNameArgument ErrorResponse // Force shutdown (equivalent to pulling the power from the machine, default: // false) Force bool }
A HaltCommand specifies the options and output of vagrant halt.
func (*HaltCommand) Start ¶
func (cmd *HaltCommand) Start() error
Start the command. You must call Wait() to complete execution.
type MachineNameArgument ¶ added in v1.4.0
type MachineNameArgument struct { // MachineName is the name or ID of a vagrant VM to act on. If unspecified // (the default), vagrant will act upon all VMs in the current directory. MachineName string }
type OutputParser ¶
type OutputParser struct { // If true, vagrant output will be echoed to stdout. Default: false Verbose bool }
OutputParser is used to parse the output from the vagrant command.
type PortCommand ¶
type PortCommand struct { BaseCommand MachineNameArgument PortResponse }
PortCommand specifies the options and output of vagrant port. Note that the port command is unique in that the MachineName option is required if your Vagrantfile defines more than one VM!
func (*PortCommand) Start ¶
func (cmd *PortCommand) Start() error
Start the command. You must call Wait() to complete execution.
type PortResponse ¶
type PortResponse struct { ErrorResponse // ForwardedPorts is a list of ports forwarded from the host OS to the guest // OS for the requested vagrant machine. ForwardedPorts []ForwardedPort }
PortResponse is the output from the vagrant port command.
type ProvisionCommand ¶
type ProvisionCommand struct { BaseCommand MachineNameArgument ErrorResponse ProvisionersArgument }
ProvisionCommand specifies the options and output of vagrant provision
func (*ProvisionCommand) Start ¶
func (cmd *ProvisionCommand) Start() error
Start the command. You must call Wait() to complete execution.
type ProvisionersArgument ¶ added in v1.1.0
type ProvisionersArgument struct { // Enabled provisioners by type or name (default: blank which means they're // all enable or disabled depending on the Provisioning flag) Provisioners []string }
ProvisionersArgument adds the Provisioners argument to a command.
type ProvisioningArgument ¶ added in v1.1.0
type ProvisioningArgument struct { ProvisionersArgument // Enable or disable provisioning Provisioning ProvisioningOption }
ProvisioningArgument adds Provisioning and Provisioners arguments to a Command.
type ProvisioningOption ¶ added in v1.1.0
type ProvisioningOption uint8
ProvisioningOption is used to set whether provisioning should be forced, disabled, or use the default.
const ( // DefaultProvisioning will cause vagrant to run the provisioners only if // they haven't already been run. DefaultProvisioning ProvisioningOption = iota // ForceProvisioning will force the provisioners to run. ForceProvisioning // DisableProvisioning will disable provisioners. DisableProvisioning )
type ReloadCommand ¶
type ReloadCommand struct { BaseCommand MachineNameArgument ErrorResponse ProvisioningArgument }
ReloadCommand specifies the options and output of vagrant reload
func (*ReloadCommand) Start ¶
func (cmd *ReloadCommand) Start() error
Start the command. You must call Wait() to complete execution.
type ResumeCommand ¶ added in v1.1.0
type ResumeCommand struct { BaseCommand MachineNameArgument ErrorResponse ProvisioningArgument }
A ResumeCommand specifies the options and output of vagrant resume.
func (*ResumeCommand) Start ¶ added in v1.1.0
func (cmd *ResumeCommand) Start() error
Start the command. You must call Wait() to complete execution.
type SSHConfig ¶
type SSHConfig struct { // Any additional fields returned from the ssh-config command. AdditionalFields map[string]string // Whether or not to enable ForwardAgent - "yes" or "no" ForwardAgent string // The Host matches the vagrant machine name (ex: default) Host string // The HostName to connect to (ex: 127.0.0.1) HostName string // Whether or not to enable IdentitiesOnly - "yes" or "no" IdentitiesOnly string // Path to a private key file to use for the connection (ex: ~/.ssh/id_rsa) IdentityFile string // Level of logging to enable (ex: FATAL) LogLevel string // Whether or not to enable password authentication - "yes" or "no" PasswordAuthentication string // Port to connect to (ex: 22) Port int // Whether or not to enable StrictHostKeyChecking - "yes" or "no" StrictHostKeyChecking string // User to connect as (ex: root) User string // Path to a known hosts file (ex: /dev/null) UserKnownHostsFile string }
The SSHConfig struct has all of the settings you'll need to connect to the vagrant machine via ssh. The fields and values match the fields and values that an ssh config file is expecting. For example, you could build a ssh config file like:
Host ... HostName ... Port ... User ... IdentityFile ...
type SSHConfigCommand ¶
type SSHConfigCommand struct { BaseCommand MachineNameArgument SSHConfigResponse // Name of a specific host to get SSH config info for. If not set, info for // all VMs will be pulled. Host string }
SSHConfigCommand specifies options and output from vagrant ssh-config
func (*SSHConfigCommand) Start ¶
func (cmd *SSHConfigCommand) Start() error
Start the command. You must call Wait() to complete execution.
type SSHConfigResponse ¶
type SSHConfigResponse struct { ErrorResponse // SSH Configs per VM. Map keys match vagrant VM names (ex: default) and // the values are configs. Configs map[string]SSHConfig }
SSHConfigResponse has the output from vagrant ssh-config
type StatusCommand ¶
type StatusCommand struct { BaseCommand MachineNameArgument StatusResponse }
StatusCommand specifies options and output from vagrant status
func (*StatusCommand) Start ¶
func (cmd *StatusCommand) Start() error
Start the command. You must call Wait() to complete execution.
type StatusResponse ¶
type StatusResponse struct { ErrorResponse // Status per Vagrant VM. Keys are Vagrant VM names (ex: default) and values // are the status of the VM. Status map[string]string }
StatusResponse has the output from vagrant status
type SuspendCommand ¶
type SuspendCommand struct { BaseCommand MachineNameArgument ErrorResponse }
SuspendCommand specifies options and output from vagrant suspend
func (*SuspendCommand) Start ¶
func (cmd *SuspendCommand) Start() error
Start the command. You must call Wait() to complete execution.
type UpCommand ¶
type UpCommand struct { BaseCommand MachineNameArgument UpResponse ProvisioningArgument // Destroy on error (default: true) DestroyOnError bool // Enable parallel execution if the provider supports it (default: true) Parallel bool // Provider to use (default: blank which means vagrant will use the default // provider) Provider string // Install the provider if it isn't installed, if possible (default: false) InstallProvider bool }
UpCommand specifies options and output from vagrant up.
type UpResponse ¶
type UpResponse struct { ErrorResponse // Info about all of the VMs constructed by the Vagrantfile. The map keys are // vagrant VM names (ex: default) and the values are VMInfo's. VMInfo map[string]*VMInfo }
UpResponse is the output from vagrant up
type VMInfo ¶
type VMInfo struct { // Name of the VM set by vagrant (ex: mydir_default_1534347044260_6006) Name string // The VM provider (ex: virtualbox) Provider string }
VMInfo has information about the created vagrant machines.
type VagrantClient ¶
type VagrantClient struct { // Directory where the Vagrantfile can be found. // // Normally this would be set by NewVagrantClient() and shouldn't // be changed afterward. VagrantfileDir string // contains filtered or unexported fields }
VagrantClient is the main entry point to the library. Users should construct a new VagrantClient using NewVagrantClient().
func NewVagrantClient ¶
func NewVagrantClient(vagrantfileDir string) (*VagrantClient, error)
NewVagrantClient creates a new VagrantClient.
vagrantfileDir should be the path to a directory where the Vagrantfile exists.
func (*VagrantClient) BoxAdd ¶ added in v1.6.0
func (client *VagrantClient) BoxAdd(location string) *BoxAddCommand
BoxAdd adds a vagrant box to your local boxes. Its parameter location is the name, url, or path of the box. After setting options as appropriate, you must call Run() or Start() followed by Wait() to execute. Errors will be recorded in Error.
func (*VagrantClient) BoxList ¶ added in v1.5.0
func (client *VagrantClient) BoxList() *BoxListCommand
BoxList returns the available vagrant boxes. After setting options as appropriate, you must call Run() or Start() followed by Wait() to execute. Output will be in Boxes and any error will be in Error.
func (*VagrantClient) Destroy ¶
func (client *VagrantClient) Destroy() *DestroyCommand
Destroy will destroy the vagrant machines. After setting options as appropriate, you must call Run() or Start() followed by Wait() to execute. Errors will be recorded in Error.
func (*VagrantClient) GlobalStatus ¶ added in v1.3.0
func (client *VagrantClient) GlobalStatus() *GlobalStatusCommand
GlobalStatus will return the status of all vagrant machines regardless of directory. After setting options as appropriate, you must call Run() or Start() followed by Wait() to execute. Output will be in Status and any error will be in Error.
func (*VagrantClient) Halt ¶
func (client *VagrantClient) Halt() *HaltCommand
Halt will shutdown the vagrant machine. After setting options as appropriate, you must call Run() or Start() followed by Wait() to execute. Errors will be recorded in Error.
func (*VagrantClient) Port ¶
func (client *VagrantClient) Port() *PortCommand
Port will return information about ports forwarded from the host to the guest machine. After setting options as appropriate, you must call Run() or Start() followed by Wait() to execute. Output will be in ForwardedPorts with any error in Error.
This appears to be the only vagrant command that cannot handle multi-vm Vagrantfiles for some reason. If your Vagrantfile brings up multiple machines, you MUST specify which machine you are interested in by specifying the PortCommand.MachineName option!
func (*VagrantClient) Provision ¶
func (client *VagrantClient) Provision() *ProvisionCommand
Provision will run the provisioners in a Vagrantfile. After setting options as appropriate, you must call Run() or Start() followed by Wait() to execute. Errors will be recorded in Error.
func (*VagrantClient) Reload ¶
func (client *VagrantClient) Reload() *ReloadCommand
Reload will halt and restart vagrant machines, reloading config from the Vagrantfile. After setting options as appropriate, you must call Run() or Start() followed by Wait() to execute. Errors will be recorded in Error.
func (*VagrantClient) Resume ¶ added in v1.1.0
func (client *VagrantClient) Resume() *ResumeCommand
Resume will restart a vagrant machine that has been suspended or halted. After setting options as appropriate, you must call Run() or Start() followed by Wait() to execute. Errors will be recorded in Error.
func (*VagrantClient) SSHConfig ¶
func (client *VagrantClient) SSHConfig() *SSHConfigCommand
SSHConfig will return connection information for connecting to a vagrant machine via ssh. After setting options as appropriate, you must call Run() or Start() followed by Wait() to execute. Output will be in Configs and any error will be in Error.
func (*VagrantClient) Status ¶
func (client *VagrantClient) Status() *StatusCommand
Status will return the status of vagrant machines. After setting options as appropriate, you must call Run() or Start() followed by Wait() to execute. Output will be in Status and any error will be in Error.
func (*VagrantClient) Suspend ¶
func (client *VagrantClient) Suspend() *SuspendCommand
Suspend will cause the vagrant machine to "suspend", like putting a computer to sleep. After setting options as appropriate, you must call Run() or Start() followed by Wait() to execute. Errors will be recorded in Error.
func (*VagrantClient) Up ¶
func (client *VagrantClient) Up() *UpCommand
Up creates, if necessary, and brings up the vagrant machines. After setting options as appropriate, you must call Run() or Start() followed by Wait() to execute. Output will be in VMInfo or Error.
func (*VagrantClient) Version ¶
func (client *VagrantClient) Version() *VersionCommand
Version returns the current and latest version of vagrant. After setting options as appropriate, you must call Run() or Start() followed by Wait() to execute. Output will be in InstalledVersion and LatestVersion and any error will be in Error.
type VersionCommand ¶
type VersionCommand struct { BaseCommand VersionResponse }
VersionCommand specifies options and output from vagrant version
func (*VersionCommand) Start ¶
func (cmd *VersionCommand) Start() error
Start the command. You must call Wait() to complete execution.
type VersionResponse ¶
type VersionResponse struct { ErrorResponse // The current version InstalledVersion string // The latest version LatestVersion string }
VersionResponse is the output from vagrant version
Source Files ¶
- base_command.go
- command_box_add.go
- command_box_list.go
- command_box_list_response.go
- command_destroy.go
- command_global_status.go
- command_global_status_response.go
- command_halt.go
- command_port.go
- command_port_response.go
- command_provision.go
- command_reload.go
- command_resume.go
- command_sshconfig.go
- command_sshconfig_response.go
- command_status.go
- command_status_response.go
- command_suspend.go
- command_up.go
- command_up_response.go
- command_version.go
- command_version_response.go
- doc.go
- error_response.go
- machine_name_argument.go
- output_handler.go
- output_parser.go
- provisioners_argument.go
- provisioning_argument.go
- vagrant_client.go