2020-09-04 17:53:09 -04:00
|
|
|
package proxmoxclone
|
|
|
|
|
|
|
|
import (
|
2020-09-10 00:46:57 -04:00
|
|
|
proxmoxapi "github.com/Telmate/proxmox-api-go/proxmox"
|
2020-09-04 17:53:09 -04:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2020-09-14 15:49:38 -04:00
|
|
|
proxmox "github.com/hashicorp/packer/builder/proxmox/common"
|
2020-09-10 00:46:57 -04:00
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
2020-09-14 14:58:50 -04:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2020-09-04 17:53:09 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-09-14 15:49:38 -04:00
|
|
|
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-09-04 17:53:09 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// The unique id for the builder
|
|
|
|
const BuilderID = "proxmox.clone"
|
|
|
|
|
|
|
|
type Builder struct {
|
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// Builder implements packer.Builder
|
|
|
|
var _ packer.Builder = &Builder{}
|
|
|
|
|
|
|
|
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
|
|
|
|
return b.config.Prepare(raws...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("clone-config", &b.config)
|
2020-09-14 14:58:50 -04:00
|
|
|
state.Put("comm", &b.config.Comm)
|
2020-09-04 17:53:09 -04:00
|
|
|
|
2020-09-10 00:46:57 -04:00
|
|
|
preSteps := []multistep.Step{
|
2020-09-14 14:58:50 -04:00
|
|
|
&StepSshKeyPair{
|
|
|
|
Debug: b.config.PackerDebug,
|
2020-09-10 00:46:57 -04:00
|
|
|
DebugKeyPath: fmt.Sprintf("%s.pem", b.config.PackerBuildName),
|
|
|
|
Comm: &b.config.Comm,
|
2020-09-14 14:58:50 -04:00
|
|
|
},
|
|
|
|
}
|
2020-09-04 17:53:09 -04:00
|
|
|
postSteps := []multistep.Step{}
|
|
|
|
|
2020-09-11 19:45:16 -04:00
|
|
|
sb := proxmox.NewSharedBuilder(BuilderID, b.config.Config, preSteps, postSteps, &cloneVMCreator{})
|
2020-09-04 17:53:09 -04:00
|
|
|
return sb.Run(ctx, ui, hook, state)
|
|
|
|
}
|
2020-09-10 00:46:57 -04:00
|
|
|
|
|
|
|
type cloneVMCreator struct{}
|
|
|
|
|
|
|
|
func (*cloneVMCreator) Create(vmRef *proxmoxapi.VmRef, config proxmoxapi.ConfigQemu, state multistep.StateBag) error {
|
|
|
|
client := state.Get("proxmoxClient").(*proxmoxapi.Client)
|
2020-09-14 14:58:50 -04:00
|
|
|
c := state.Get("clone-config").(*Config)
|
|
|
|
comm := state.Get("comm").(*communicator.Config)
|
2020-09-10 00:46:57 -04:00
|
|
|
|
2020-09-14 14:58:50 -04:00
|
|
|
fullClone := 1
|
2020-10-06 20:24:45 -04:00
|
|
|
if c.FullClone.False() {
|
2020-09-14 14:58:50 -04:00
|
|
|
fullClone = 0
|
|
|
|
}
|
2020-09-10 00:46:57 -04:00
|
|
|
|
2020-09-14 14:58:50 -04:00
|
|
|
config.FullClone = &fullClone
|
|
|
|
config.CIuser = comm.SSHUsername
|
|
|
|
config.Sshkeys = string(comm.SSHPublicKey)
|
|
|
|
sourceVmr, err := client.GetVmRefByName(c.CloneVM)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = config.CloneVm(sourceVmr, vmRef, client)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = config.UpdateConfig(vmRef, client)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-10 00:46:57 -04:00
|
|
|
return nil
|
|
|
|
}
|