Files
Packer-Cn/builder.go
T
Elizaveta Tretyakova b2c6e44c70 Builder (#2)
* the very initial builder: just clones the VM

* removed post-processor

* builder now waits for IP address

* more separated steps

* clean up

* added run and shutdown steps

* added step connecting via ssh

* changed the BuilderId to a proper one

* added shutdown command, only echo works

* added cleanup

* removed wainting for VM to stop, removed error on the non-zero exit status

* removed BuildSuccessFlag

* refactored config structures; fixed interpolation of json template

* the rest of configuration refactoring

* removed duplicated parameters from Config

* removed duplicated parameters drom Config

* changed BuilderId and Artifact

* create a dedicated step for VM hardware configuration

* merged StepSetupCloningEnv into StepCloneVM

* improved cleanup

* added proper handling of 'disconnected' command exit status; added guest os shutdown before halting the machine [in case when no shutdown command is given]

* refactored non-conventional variable and field names

* removed redundant fields from Artifact

* removed the success field at all

* removed ArtifactFiles

* removed unnecessary warnings

* minor change in parameters structure
2017-05-09 17:23:57 +03:00

100 lines
2.3 KiB
Go

package main
import (
"errors"
"log"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/packer"
"github.com/mitchellh/multistep"
"github.com/hashicorp/packer/helper/communicator"
gossh "golang.org/x/crypto/ssh"
"github.com/hashicorp/packer/communicator/ssh"
)
type Builder struct {
config *Config
runner multistep.Runner
}
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
c, warnings, errs := NewConfig(raws...)
if errs != nil {
return warnings, errs
}
b.config = c
return warnings, nil
}
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
// Set up the state.
state := new(multistep.BasicStateBag)
state.Put("hook", hook)
state.Put("ui", ui)
// Build the steps.
steps := []multistep.Step{
&StepConfigureHW{
config: b.config,
},
&StepCloneVM{
config: b.config,
},
&StepRun{},
&communicator.StepConnect{
Config: &b.config.Config,
Host: func(state multistep.StateBag) (string, error) {
return state.Get("ip").(string), nil
},
SSHConfig: func(multistep.StateBag) (*gossh.ClientConfig, error) {
return &gossh.ClientConfig{
User: b.config.Config.SSHUsername,
Auth: []gossh.AuthMethod{
gossh.Password(b.config.Config.SSHPassword),
gossh.KeyboardInteractive(
ssh.PasswordKeyboardInteractive(b.config.Config.SSHPassword)),
},
// TODO: add a proper verification
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
}, nil
},
},
&common.StepProvision{},
&StepShutdown{
Command: b.config.ShutdownCommand,
},
}
// Run!
b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
b.runner.Run(state)
// If there was an error, return that
if rawErr, ok := state.GetOk("error"); ok {
return nil, rawErr.(error)
}
// If we were interrupted or cancelled, then just exit.
if _, ok := state.GetOk(multistep.StateCancelled); ok {
return nil, errors.New("Build was cancelled.")
}
if _, ok := state.GetOk(multistep.StateHalted); ok {
return nil, errors.New("Build was halted.")
}
// No errors, must've worked
artifact := &Artifact{
VMName: b.config.VMName,
}
return artifact, nil
}
func (b *Builder) Cancel() {
if b.runner != nil {
log.Println("Cancelling the step runner...")
b.runner.Cancel()
}
}