2017-07-10 20:52:48 -04:00
|
|
|
package vsphere_template
|
2017-07-09 14:12:37 -04:00
|
|
|
|
|
|
|
import (
|
2017-07-09 15:58:42 -04:00
|
|
|
"context"
|
2017-11-08 13:57:34 -05:00
|
|
|
"errors"
|
2017-07-09 14:12:37 -04:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2017-07-09 20:33:03 -04:00
|
|
|
"strings"
|
2017-07-09 22:13:31 -04:00
|
|
|
"time"
|
2017-07-09 14:12:37 -04:00
|
|
|
|
2018-10-25 17:17:35 -04:00
|
|
|
vmwcommon "github.com/hashicorp/packer/builder/vmware/common"
|
2017-07-09 14:12:37 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/config"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-07-09 14:12:37 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-09-20 21:50:37 -04:00
|
|
|
"github.com/hashicorp/packer/post-processor/vsphere"
|
2017-07-09 14:12:37 -04:00
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2017-07-09 15:58:42 -04:00
|
|
|
"github.com/vmware/govmomi"
|
2017-07-09 14:12:37 -04:00
|
|
|
)
|
2017-07-09 15:58:42 -04:00
|
|
|
|
2017-07-09 20:33:03 -04:00
|
|
|
var builtins = map[string]string{
|
2018-10-25 17:17:35 -04:00
|
|
|
vsphere.BuilderId: "vmware",
|
|
|
|
vmwcommon.BuilderIdESX: "vmware",
|
2017-07-09 20:33:03 -04:00
|
|
|
}
|
|
|
|
|
2017-07-09 14:12:37 -04:00
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
Host string `mapstructure:"host"`
|
2017-07-09 15:58:42 -04:00
|
|
|
Insecure bool `mapstructure:"insecure"`
|
2017-07-09 14:12:37 -04:00
|
|
|
Username string `mapstructure:"username"`
|
|
|
|
Password string `mapstructure:"password"`
|
2017-07-19 00:44:18 -04:00
|
|
|
Datacenter string `mapstructure:"datacenter"`
|
2017-07-09 14:12:37 -04:00
|
|
|
Folder string `mapstructure:"folder"`
|
2018-11-07 23:59:12 -05:00
|
|
|
SnapshotEnable bool `mapstructure:"snapshot_enable"`
|
|
|
|
SnapshotName string `mapstructure:"snapshot_name"`
|
|
|
|
SnapshotDescription string `mapstructure:"snapshot_description"`
|
2017-07-09 14:12:37 -04:00
|
|
|
|
|
|
|
ctx interpolate.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
type PostProcessor struct {
|
|
|
|
config Config
|
|
|
|
url *url.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PostProcessor) Configure(raws ...interface{}) error {
|
|
|
|
err := config.Decode(&p.config, &config.DecodeOpts{
|
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &p.config.ctx,
|
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{},
|
|
|
|
},
|
|
|
|
}, raws...)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
errs := new(packer.MultiError)
|
|
|
|
vc := map[string]*string{
|
|
|
|
"host": &p.config.Host,
|
|
|
|
"username": &p.config.Username,
|
|
|
|
"password": &p.config.Password,
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, ptr := range vc {
|
|
|
|
if *ptr == "" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("%s must be set", key))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-18 23:10:05 -04:00
|
|
|
if p.config.Folder != "" && !strings.HasPrefix(p.config.Folder, "/") {
|
2017-07-09 14:56:39 -04:00
|
|
|
errs = packer.MultiErrorAppend(
|
2017-07-18 23:10:05 -04:00
|
|
|
errs, fmt.Errorf("Folder must be bound to the root"))
|
2017-07-09 14:56:39 -04:00
|
|
|
}
|
|
|
|
|
2017-07-18 23:10:05 -04:00
|
|
|
sdk, err := url.Parse(fmt.Sprintf("https://%v/sdk", p.config.Host))
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Error invalid vSphere sdk endpoint: %s", err))
|
2018-07-23 19:12:21 -04:00
|
|
|
return errs
|
2017-07-18 23:10:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sdk.User = url.UserPassword(p.config.Username, p.config.Password)
|
|
|
|
p.url = sdk
|
|
|
|
|
2017-07-09 14:12:37 -04:00
|
|
|
if len(errs.Errors) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
|
2017-07-09 20:33:03 -04:00
|
|
|
if _, ok := builtins[artifact.BuilderId()]; !ok {
|
2018-03-20 14:32:01 -04:00
|
|
|
return nil, false, fmt.Errorf("The Packer vSphere Template post-processor "+
|
|
|
|
"can only take an artifact from the VMware-iso builder, built on "+
|
|
|
|
"ESXi (i.e. remote) or an artifact from the vSphere post-processor. "+
|
|
|
|
"Artifact type %s does not fit this requirement", artifact.BuilderId())
|
2017-07-09 20:33:03 -04:00
|
|
|
}
|
2017-07-09 14:56:39 -04:00
|
|
|
|
2018-10-25 17:17:35 -04:00
|
|
|
f := artifact.State(vmwcommon.ArtifactConfFormat)
|
|
|
|
k := artifact.State(vmwcommon.ArtifactConfKeepRegistered)
|
|
|
|
s := artifact.State(vmwcommon.ArtifactConfSkipExport)
|
2017-09-20 21:50:37 -04:00
|
|
|
|
2017-11-08 13:57:34 -05:00
|
|
|
if f != "" && k != "true" && s == "false" {
|
|
|
|
return nil, false, errors.New("To use this post-processor with exporting behavior you need set keep_registered as true")
|
2017-09-20 21:50:37 -04:00
|
|
|
}
|
|
|
|
|
2017-08-14 22:00:19 -04:00
|
|
|
// In some occasions the VM state is powered on and if we immediately try to mark as template
|
|
|
|
// (after the ESXi creates it) it will fail. If vSphere is given a few seconds this behavior doesn't reappear.
|
2017-08-24 21:46:35 -04:00
|
|
|
ui.Message("Waiting 10s for VMware vSphere to start")
|
2017-07-09 14:56:39 -04:00
|
|
|
time.Sleep(10 * time.Second)
|
2017-07-18 23:10:05 -04:00
|
|
|
c, err := govmomi.NewClient(context.Background(), p.url, p.config.Insecure)
|
2017-07-09 15:58:42 -04:00
|
|
|
if err != nil {
|
2017-07-25 23:14:59 -04:00
|
|
|
return nil, false, fmt.Errorf("Error connecting to vSphere: %s", err)
|
2017-07-09 15:58:42 -04:00
|
|
|
}
|
|
|
|
|
2017-07-31 10:30:13 -04:00
|
|
|
defer c.Logout(context.Background())
|
|
|
|
|
2017-07-09 15:58:42 -04:00
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
state.Put("client", c)
|
|
|
|
|
2017-07-09 14:56:39 -04:00
|
|
|
steps := []multistep.Step{
|
2017-07-18 23:10:05 -04:00
|
|
|
&stepChooseDatacenter{
|
2017-07-09 14:56:39 -04:00
|
|
|
Datacenter: p.config.Datacenter,
|
|
|
|
},
|
2017-07-25 23:14:59 -04:00
|
|
|
&stepCreateFolder{
|
|
|
|
Folder: p.config.Folder,
|
|
|
|
},
|
2018-11-07 23:59:12 -05:00
|
|
|
NewStepCreateSnapshot(artifact, p),
|
2017-11-10 12:03:15 -05:00
|
|
|
NewStepMarkAsTemplate(artifact),
|
2017-07-09 14:56:39 -04:00
|
|
|
}
|
2017-08-31 23:33:40 -04:00
|
|
|
runner := common.NewRunnerWithPauseFn(steps, p.config.PackerConfig, ui, state)
|
2017-07-09 14:56:39 -04:00
|
|
|
runner.Run(state)
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
2017-07-25 23:14:59 -04:00
|
|
|
return nil, false, rawErr.(error)
|
2017-07-09 14:56:39 -04:00
|
|
|
}
|
2017-07-09 14:12:37 -04:00
|
|
|
return artifact, true, nil
|
|
|
|
}
|