2017-05-09 10:23:57 -04:00
|
|
|
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"
|
2017-06-27 23:04:25 -04:00
|
|
|
"github.com/vmware/govmomi"
|
|
|
|
"context"
|
|
|
|
"net/url"
|
|
|
|
"github.com/vmware/govmomi/find"
|
2017-06-29 18:08:49 -04:00
|
|
|
"github.com/vmware/govmomi/object"
|
2017-07-01 10:18:54 -04:00
|
|
|
"fmt"
|
2017-05-09 10:23:57 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
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) {
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
2017-06-27 23:04:25 -04:00
|
|
|
ctx := context.TODO()
|
|
|
|
state.Put("ctx", ctx)
|
|
|
|
|
2017-07-01 11:54:10 -04:00
|
|
|
vcenter_url, err := url.Parse(fmt.Sprintf("https://%v/sdk", b.config.VCenterServer))
|
2017-06-27 23:04:25 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
vcenter_url.User = url.UserPassword(b.config.Username, b.config.Password)
|
2017-07-01 11:59:36 -04:00
|
|
|
client, err := govmomi.NewClient(ctx, vcenter_url, b.config.InsecureConnection)
|
2017-06-27 23:04:25 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
state.Put("client", client)
|
|
|
|
|
|
|
|
finder := find.NewFinder(client.Client, false)
|
2017-07-01 10:45:13 -04:00
|
|
|
datacenter, err := finder.DatacenterOrDefault(ctx, b.config.Datacenter)
|
2017-06-27 23:04:25 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-01 10:45:13 -04:00
|
|
|
finder.SetDatacenter(datacenter)
|
2017-06-27 23:04:25 -04:00
|
|
|
state.Put("finder", finder)
|
2017-07-01 10:45:13 -04:00
|
|
|
state.Put("datacenter", datacenter)
|
2017-06-27 23:04:25 -04:00
|
|
|
|
|
|
|
vmSrc, err := finder.VirtualMachine(ctx, b.config.Template)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
state.Put("vmSrc", vmSrc)
|
2017-05-09 10:23:57 -04:00
|
|
|
|
|
|
|
steps := []multistep.Step{
|
2017-05-19 00:44:27 -04:00
|
|
|
&StepCloneVM{
|
2017-05-09 10:23:57 -04:00
|
|
|
config: b.config,
|
|
|
|
},
|
2017-07-01 18:34:50 -04:00
|
|
|
&StepConfigureHardware{
|
|
|
|
config: &b.config.HardwareConfig,
|
2017-05-09 10:23:57 -04:00
|
|
|
},
|
|
|
|
&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{
|
2017-07-01 20:06:02 -04:00
|
|
|
config: &b.config.ShutdownConfig,
|
2017-06-13 07:11:41 -04:00
|
|
|
},
|
2017-06-27 03:32:59 -04:00
|
|
|
&StepCreateSnapshot{
|
|
|
|
createSnapshot: b.config.CreateSnapshot,
|
|
|
|
},
|
|
|
|
&StepConvertToTemplate{
|
|
|
|
ConvertToTemplate: b.config.ConvertToTemplate,
|
2017-05-09 10:23:57 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.")
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact := &Artifact{
|
|
|
|
VMName: b.config.VMName,
|
2017-06-29 18:08:49 -04:00
|
|
|
Conn: state.Get("vm").(*object.VirtualMachine),
|
2017-05-09 10:23:57 -04:00
|
|
|
}
|
|
|
|
return artifact, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Cancel() {
|
|
|
|
if b.runner != nil {
|
|
|
|
log.Println("Cancelling the step runner...")
|
|
|
|
b.runner.Cancel()
|
|
|
|
}
|
|
|
|
}
|