From 957bdae441ba959d5b40abf5652b67343aed2c28 Mon Sep 17 00:00:00 2001 From: Marin Salinas Date: Mon, 4 Feb 2019 12:17:32 -0600 Subject: [PATCH] feature: bsusurrogate, add clean connect step --- builder/osc/bsusurrogate/builder.go | 8 +++ builder/osc/common/ssh.go | 84 +++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 builder/osc/common/ssh.go diff --git a/builder/osc/bsusurrogate/builder.go b/builder/osc/bsusurrogate/builder.go index 810b73dfa..85a60af3d 100644 --- a/builder/osc/bsusurrogate/builder.go +++ b/builder/osc/bsusurrogate/builder.go @@ -11,6 +11,7 @@ import ( osccommon "github.com/hashicorp/packer/builder/osc/common" "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" @@ -192,6 +193,13 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe Timeout: b.config.WindowsPasswordTimeout, BuildName: b.config.PackerBuildName, }, + &communicator.StepConnect{ + Config: &b.config.RunConfig.Comm, + Host: osccommon.SSHHost( + oapiconn, + b.config.Comm.SSHInterface), + SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(), + }, } b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) diff --git a/builder/osc/common/ssh.go b/builder/osc/common/ssh.go new file mode 100644 index 000000000..751c3c925 --- /dev/null +++ b/builder/osc/common/ssh.go @@ -0,0 +1,84 @@ +package common + +import ( + "errors" + "fmt" + "time" + + "github.com/hashicorp/packer/helper/multistep" + "github.com/outscale/osc-go/oapi" +) + +type oapiDescriber interface { + POST_ReadVms(oapi.ReadVmsRequest) (*oapi.POST_ReadVmsResponses, error) +} + +var ( + // modified in tests + sshHostSleepDuration = time.Second +) + +// SSHHost returns a function that can be given to the SSH communicator +// for determining the SSH address based on the vm DNS name. +func SSHHost(e oapiDescriber, sshInterface string) func(multistep.StateBag) (string, error) { + return func(state multistep.StateBag) (string, error) { + const tries = 2 + // <= with current structure to check result of describing `tries` times + for j := 0; j <= tries; j++ { + var host string + i := state.Get("vm").(oapi.Vm) + if sshInterface != "" { + switch sshInterface { + case "public_ip": + if i.PublicIp != "" { + host = i.PublicIp + } + case "private_ip": + if i.PrivateIp != "" { + host = i.PrivateIp + } + case "public_dns": + if i.PublicDnsName != "" { + host = i.PublicDnsName + } + case "private_dns": + if i.PrivateDnsName != "" { + host = i.PrivateDnsName + } + default: + panic(fmt.Sprintf("Unknown interface type: %s", sshInterface)) + } + } else if i.NetId != "" { + if i.PublicIp != "" { + host = i.PublicIp + } else if i.PrivateIp != "" { + host = i.PrivateIp + } + } else if i.PublicDnsName != "" { + host = i.PublicDnsName + } + + if host != "" { + return host, nil + } + + r, err := e.POST_ReadVms(oapi.ReadVmsRequest{ + Filters: oapi.FiltersVm{ + VmIds: []string{i.VmId}, + }, + }) + if err != nil { + return "", err + } + + if len(r.OK.Vms) == 0 { + return "", fmt.Errorf("vm not found: %s", i.VmId) + } + + state.Put("vm", r.OK.Vms[0]) + time.Sleep(sshHostSleepDuration) + } + + return "", errors.New("couldn't determine address for vm") + } +}