2019-04-11 12:52:21 -04:00
|
|
|
package proxmox
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
2019-09-04 17:03:25 -04:00
|
|
|
"errors"
|
2019-04-11 12:52:21 -04:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/Telmate/proxmox-api-go/proxmox"
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/communicator"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep/commonsteps"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2019-04-11 12:52:21 -04:00
|
|
|
)
|
|
|
|
|
2020-09-11 19:45:16 -04:00
|
|
|
func NewSharedBuilder(id string, config Config, preSteps []multistep.Step, postSteps []multistep.Step, vmCreator ProxmoxVMCreator) *Builder {
|
2020-09-04 17:53:09 -04:00
|
|
|
return &Builder{
|
|
|
|
id: id,
|
|
|
|
config: config,
|
|
|
|
preSteps: preSteps,
|
|
|
|
postSteps: postSteps,
|
2020-09-14 14:58:50 -04:00
|
|
|
vmCreator: vmCreator,
|
2020-09-04 17:53:09 -04:00
|
|
|
}
|
|
|
|
}
|
2019-04-11 12:52:21 -04:00
|
|
|
|
|
|
|
type Builder struct {
|
2020-09-04 17:53:09 -04:00
|
|
|
id string
|
2019-04-11 12:52:21 -04:00
|
|
|
config Config
|
2020-09-04 17:53:09 -04:00
|
|
|
preSteps []multistep.Step
|
|
|
|
postSteps []multistep.Step
|
2019-04-11 12:52:21 -04:00
|
|
|
runner multistep.Runner
|
|
|
|
proxmoxClient *proxmox.Client
|
2020-09-14 14:58:50 -04:00
|
|
|
vmCreator ProxmoxVMCreator
|
2019-04-11 12:52:21 -04:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook, state multistep.StateBag) (packersdk.Artifact, error) {
|
2019-04-11 12:52:21 -04:00
|
|
|
var err error
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
InsecureSkipVerify: b.config.SkipCertValidation,
|
|
|
|
}
|
2019-10-14 10:10:35 -04:00
|
|
|
b.proxmoxClient, err = proxmox.NewClient(b.config.proxmoxURL.String(), nil, tlsConfig)
|
2019-04-11 12:52:21 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-07 15:07:09 -04:00
|
|
|
err = b.proxmoxClient.Login(b.config.Username, b.config.Password, "")
|
2019-04-11 12:52:21 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the state
|
|
|
|
state.Put("config", &b.config)
|
|
|
|
state.Put("proxmoxClient", b.proxmoxClient)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
|
2020-09-14 14:58:50 -04:00
|
|
|
comm := &b.config.Comm
|
2020-09-10 00:46:57 -04:00
|
|
|
|
2019-04-11 12:52:21 -04:00
|
|
|
// Build the steps
|
2020-09-04 17:53:09 -04:00
|
|
|
coreSteps := []multistep.Step{
|
2020-09-11 19:45:16 -04:00
|
|
|
&stepStartVM{
|
2020-09-14 14:58:50 -04:00
|
|
|
vmCreator: b.vmCreator,
|
|
|
|
},
|
2020-11-11 18:04:28 -05:00
|
|
|
&commonsteps.StepHTTPServer{
|
2019-04-11 12:52:21 -04:00
|
|
|
HTTPDir: b.config.HTTPDir,
|
|
|
|
HTTPPortMin: b.config.HTTPPortMin,
|
|
|
|
HTTPPortMax: b.config.HTTPPortMax,
|
2020-05-27 16:34:24 -04:00
|
|
|
HTTPAddress: b.config.HTTPAddress,
|
2019-04-11 12:52:21 -04:00
|
|
|
},
|
|
|
|
&stepTypeBootCommand{
|
|
|
|
BootConfig: b.config.BootConfig,
|
2020-09-04 17:53:09 -04:00
|
|
|
Ctx: b.config.Ctx,
|
2019-04-11 12:52:21 -04:00
|
|
|
},
|
|
|
|
&communicator.StepConnect{
|
2020-09-10 00:46:57 -04:00
|
|
|
Config: comm,
|
|
|
|
Host: commHost((*comm).Host()),
|
|
|
|
SSHConfig: (*comm).SSHConfigFunc(),
|
2019-04-11 12:52:21 -04:00
|
|
|
},
|
2020-11-11 18:04:28 -05:00
|
|
|
&commonsteps.StepProvision{},
|
|
|
|
&commonsteps.StepCleanupTempKeys{
|
2019-04-11 12:52:21 -04:00
|
|
|
Comm: &b.config.Comm,
|
|
|
|
},
|
|
|
|
&stepConvertToTemplate{},
|
|
|
|
&stepFinalizeTemplateConfig{},
|
|
|
|
&stepSuccess{},
|
2020-09-04 17:53:09 -04:00
|
|
|
}
|
|
|
|
steps := append(b.preSteps, coreSteps...)
|
|
|
|
steps = append(steps, b.postSteps...)
|
2019-04-11 12:52:21 -04:00
|
|
|
// Run the steps
|
2020-11-11 18:04:28 -05:00
|
|
|
b.runner = commonsteps.NewRunner(steps, b.config.PackerConfig, ui)
|
2019-04-11 12:52:21 -04:00
|
|
|
b.runner.Run(ctx, state)
|
|
|
|
// If there was an error, return that
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
2019-09-04 17:03:25 -04:00
|
|
|
// If we were interrupted or cancelled, then just exit.
|
|
|
|
if _, ok := state.GetOk(multistep.StateCancelled); ok {
|
|
|
|
return nil, errors.New("build was cancelled")
|
|
|
|
}
|
2019-04-11 12:52:21 -04:00
|
|
|
|
2019-09-04 17:03:25 -04:00
|
|
|
// Verify that the template_id was set properly, otherwise we didn't progress through the last step
|
2019-09-04 08:21:52 -04:00
|
|
|
tplID, ok := state.Get("template_id").(int)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("template ID could not be determined")
|
|
|
|
}
|
|
|
|
|
2019-04-11 12:52:21 -04:00
|
|
|
artifact := &Artifact{
|
2020-09-04 17:53:09 -04:00
|
|
|
builderID: b.id,
|
2019-09-04 08:21:52 -04:00
|
|
|
templateID: tplID,
|
2019-04-11 12:52:21 -04:00
|
|
|
proxmoxClient: b.proxmoxClient,
|
2020-01-30 05:27:58 -05:00
|
|
|
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
|
2019-04-11 12:52:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return artifact, nil
|
|
|
|
}
|
|
|
|
|
2019-04-30 16:26:40 -04:00
|
|
|
// Returns ssh_host or winrm_host (see communicator.Config.Host) config
|
|
|
|
// parameter when set, otherwise gets the host IP from running VM
|
|
|
|
func commHost(host string) func(state multistep.StateBag) (string, error) {
|
|
|
|
if host != "" {
|
2019-04-25 05:43:12 -04:00
|
|
|
return func(state multistep.StateBag) (string, error) {
|
2019-04-30 16:26:40 -04:00
|
|
|
return host, nil
|
2019-04-25 05:43:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return getVMIP
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reads the first non-loopback interface's IP address from the VM.
|
|
|
|
// qemu-guest-agent package must be installed on the VM
|
2019-04-11 12:52:21 -04:00
|
|
|
func getVMIP(state multistep.StateBag) (string, error) {
|
2020-09-14 06:24:01 -04:00
|
|
|
client := state.Get("proxmoxClient").(*proxmox.Client)
|
|
|
|
config := state.Get("config").(*Config)
|
2019-04-11 12:52:21 -04:00
|
|
|
vmRef := state.Get("vmRef").(*proxmox.VmRef)
|
|
|
|
|
2020-09-14 06:24:01 -04:00
|
|
|
ifs, err := client.GetVmAgentNetworkInterfaces(vmRef)
|
2019-04-11 12:52:21 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-09-14 06:24:01 -04:00
|
|
|
if config.VMInterface != "" {
|
|
|
|
for _, iface := range ifs {
|
|
|
|
if config.VMInterface != iface.Name {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range iface.IPAddresses {
|
|
|
|
if addr.IsLoopback() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return addr.String(), nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("Interface %s only has loopback addresses", config.VMInterface)
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("Interface %s not found in VM", config.VMInterface)
|
|
|
|
}
|
|
|
|
|
2019-04-11 12:52:21 -04:00
|
|
|
for _, iface := range ifs {
|
|
|
|
for _, addr := range iface.IPAddresses {
|
|
|
|
if addr.IsLoopback() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return addr.String(), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("Found no IP addresses on VM")
|
|
|
|
}
|