generated from hashicorp/packer-plugin-scaffolding
/
step_vm_info.go
68 lines (53 loc) · 1.76 KB
/
step_vm_info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package chroot
import (
"context"
"fmt"
"log"
"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
oscgo "github.com/outscale/osc-sdk-go/v2"
osccommon "github.com/outscale/packer-plugin-outscale/builder/common"
)
// StepVmInfo verifies that this builder is running on an Outscale vm.
type StepVmInfo struct{}
func (s *StepVmInfo) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
oscconn := state.Get("osc").(*osccommon.OscClient)
//session := state.Get("clientConfig").(*session.Session)
ui := state.Get("ui").(packersdk.Ui)
// Get our own vm ID
ui.Say("Gathering information about this Outscale vm...")
cmd := ShellCommand("curl http://169.254.169.254/latest/meta-data/instance-id")
vmID, err := cmd.Output()
if err != nil {
err := fmt.Errorf(
"Error retrieving the ID of the vm Packer is running on.\n" +
"Please verify Packer is running on a proper Outscale vm.")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
log.Printf("[Debug] VmID got: %s", string(vmID))
// Query the entire vm metadata
resp, _, err := oscconn.Api.VmApi.ReadVms(oscconn.Auth).ReadVmsRequest(oscgo.ReadVmsRequest{
Filters: &oscgo.FiltersVm{
VmIds: &[]string{string(vmID)},
},
}).Execute()
if err != nil {
err := fmt.Errorf("Error getting vm data: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
vmsResp := resp.GetVms()
if len(vmsResp) == 0 {
err := fmt.Errorf("Error getting vm data: no vm found")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
vm := vmsResp[0]
state.Put("vm", vm)
return multistep.ActionContinue
}
func (s *StepVmInfo) Cleanup(multistep.StateBag) {}