diff --git a/builder/powervs/builder.go b/builder/powervs/builder.go index 2b58121..b1bf936 100644 --- a/builder/powervs/builder.go +++ b/builder/powervs/builder.go @@ -85,6 +85,11 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack return nil, err } + dhcpClient, err := b.config.DHCPClient(ctx, b.config.ServiceInstanceID) + if err != nil { + return nil, err + } + var steps []multistep.Step steps = append(steps, @@ -120,6 +125,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack state.Put("jobClient", jobClient) state.Put("instanceClient", instanceClient) state.Put("networkClient", networkClient) + state.Put("dhcpClient", dhcpClient) // Run! b.runner = commonsteps.NewRunner(steps, b.config.PackerConfig, ui) diff --git a/builder/powervs/common/ssh.go b/builder/powervs/common/ssh.go index afb2974..5842488 100644 --- a/builder/powervs/common/ssh.go +++ b/builder/powervs/common/ssh.go @@ -20,6 +20,8 @@ var ( // for determining the SSH address of the instance. func SSHHost() func(multistep.StateBag) (string, error) { return func(state multistep.StateBag) (string, error) { + ui := state.Get("ui").(packersdk.Ui) + ui.Message("Fetching IP for machine") instanceClient := state.Get("instanceClient").(*instance.IBMPIInstanceClient) host := "" const tries = 25 @@ -38,10 +40,15 @@ func SSHHost() func(multistep.StateBag) (string, error) { return host, nil } - ui := state.Get("ui").(packersdk.Ui) - ui.Say("Get Instance IP from DHCP server") - + dhcpServerID, ok := state.GetOk("dhcpServerID") + if !ok { + // if the dhcpServerID is not set, dont try to fetch IP from DHCP server, instead wait for address to get populated. + ui.Message("Machine IP is not yet found, Trying again") + time.Sleep(sshHostSleepDuration) + continue + } dhcpClient := state.Get("dhcpClient").(*instance.IBMPIDhcpClient) + ui.Message("Getting Instance IP from DHCP server") net := state.Get("network").(*models.Network) networkID := net.NetworkID @@ -55,13 +62,12 @@ func SSHHost() func(multistep.StateBag) (string, error) { } if pvmNetwork == nil { - ui.Error("Failed to get network attached to VM") - return "", err + ui.Message("Failed to get network attached to VM, Trying again") + time.Sleep(sshHostSleepDuration) + continue } - dhcpServerID := state.Get("dhcpServerID").(string) - - dhcpServerDetails, err := dhcpClient.Get(dhcpServerID) + dhcpServerDetails, err := dhcpClient.Get(dhcpServerID.(string)) if err != nil { ui.Error(fmt.Sprintf("Failed to get DHCP server details: %v", err)) return "", err @@ -84,7 +90,7 @@ func SSHHost() func(multistep.StateBag) (string, error) { return internalIP, nil } - ui.Message("Machine IP is not yet found") + ui.Message("Machine IP is not yet found from DHCP server lease, Trying again") time.Sleep(sshHostSleepDuration) } return "", errors.New("couldn't determine address for instance") diff --git a/builder/powervs/step_create_network.go b/builder/powervs/step_create_network.go index bde3ef1..b74a95f 100644 --- a/builder/powervs/step_create_network.go +++ b/builder/powervs/step_create_network.go @@ -45,7 +45,6 @@ func (s *StepCreateNetwork) Run(_ context.Context, state multistep.StateBag) mul DNSServers: []string{"8.8.8.8", "9.9.9.9"}, Type: core.StringPtr("pub-vlan"), } - ui.Message("Creating Network") net, err := networkClient.Create(netBody) if err != nil { ui.Error(fmt.Sprintf("failed to create network: %v", err)) @@ -73,7 +72,7 @@ func (s *StepCreateNetwork) Cleanup(state multistep.StateBag) { dhcpClient := state.Get("dhcpClient").(*instance.IBMPIDhcpClient) if err := dhcpClient.Delete(dhcpServerID); err != nil { - ui.Error(fmt.Sprintf("Error cleaning up DHCP server. Please delete the DHCP server manually: %s", dhcpServerID)) + ui.Error(fmt.Sprintf("Error cleaning up DHCP server. Please delete the DHCP server manually: %s error: %v", dhcpServerID, err.Error())) } return } @@ -90,11 +89,7 @@ func (s *StepCreateNetwork) createDHCPNetwork(state multistep.StateBag) error { ui := state.Get("ui").(packersdk.Ui) dhcpClient := state.Get("dhcpClient").(*instance.IBMPIDhcpClient) - dhcpServerCreateParams := models.DHCPServerCreate{ - Name: core.StringPtr("packerdhcpnetwork"), - } - - dhcpServer, err := dhcpClient.Create(&dhcpServerCreateParams) + dhcpServer, err := dhcpClient.Create(&models.DHCPServerCreate{}) if err != nil { return fmt.Errorf("error failed to create DHCP server: %v", err) } @@ -113,12 +108,13 @@ func (s *StepCreateNetwork) createDHCPNetwork(state multistep.StateBag) error { } if dhcpServerDetails.Network != nil && dhcpServerDetails.Network.ID != nil { networkID = *dhcpServerDetails.Network.ID + ui.Message("DHCP server in active state") break } if time.Since(startTime) > DHCPServerActiveTimeOut { return fmt.Errorf("error DHCP server did not become active even after %f min", DHCPServerActiveTimeOut.Minutes()) } - ui.Say("Wating for DHCP server to become active") + ui.Message("Wating for DHCP server to become active") time.Sleep(DHCPServerInterval) } ui.Say("Fetching network details") diff --git a/go.mod b/go.mod index a627b69..6054b07 100644 --- a/go.mod +++ b/go.mod @@ -124,3 +124,5 @@ require ( gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) + +replace github.com/zclconf/go-cty => github.com/nywilken/go-cty v1.13.3 // added by packer-sdc fix as noted in github.com/hashicorp/packer-plugin-sdk/issues/187 diff --git a/go.sum b/go.sum index 5492a87..47c91d9 100644 --- a/go.sum +++ b/go.sum @@ -307,6 +307,8 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d h1:VhgPp6v9qf9Agr/56bj7Y/xa04UccTW04VP0Qed4vnQ= github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U= github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= +github.com/nywilken/go-cty v1.13.3 h1:03U99oXf3j3g9xgqAE3YGpixCjM8Mg09KZ0Ji9LzX0o= +github.com/nywilken/go-cty v1.13.3/go.mod h1:YKQzy/7pZ7iq2jNFzy5go57xdxdWoLLpaEp4u238AE0= github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= @@ -370,8 +372,6 @@ github.com/ugorji/go/codec v1.2.6 h1:7kbGefxLoDBuYXOms4yD7223OpNMMPNPZxXk5TvFcyQ github.com/ugorji/go/codec v1.2.6/go.mod h1:V6TCNZ4PHqoHGFZuSG1W8nrCzzdgA2DozYxWFFpvxTw= github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8= github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= -github.com/zclconf/go-cty v1.13.3 h1:m+b9q3YDbg6Bec5rr+KGy1MzEVzY/jC2X+YX4yqKtHI= -github.com/zclconf/go-cty v1.13.3/go.mod h1:YKQzy/7pZ7iq2jNFzy5go57xdxdWoLLpaEp4u238AE0= github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b h1:FosyBZYxY34Wul7O/MSKey3txpPYyCqVO5ZyceuQJEI= go.mongodb.org/mongo-driver v1.14.0 h1:P98w8egYRjYe3XDjxhYJagTokP/H6HzlsnojRgZRd80= go.mongodb.org/mongo-driver v1.14.0/go.mod h1:Vzb0Mk/pa7e6cWw85R4F/endUC3u0U9jGcNU603k65c=