From ca1388f7bcf904499281667b0f8d9ab1dcd1f786 Mon Sep 17 00:00:00 2001 From: Mark Peek Date: Tue, 27 Aug 2013 22:08:18 -0700 Subject: [PATCH] builder/openstack: simplify by passing csp instead of accessor and api --- builder/openstack/builder.go | 16 +++++----- builder/openstack/server.go | 5 +-- builder/openstack/ssh.go | 3 +- builder/openstack/step_create_image.go | 9 ++---- builder/openstack/step_key_pair.go | 10 ++---- builder/openstack/step_run_source_server.go | 34 ++++----------------- 6 files changed, 23 insertions(+), 54 deletions(-) diff --git a/builder/openstack/builder.go b/builder/openstack/builder.go index cb24f9761..8bb81a7da 100644 --- a/builder/openstack/builder.go +++ b/builder/openstack/builder.go @@ -60,19 +60,21 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe if err != nil { return nil, err } - - // Setup the state bag and initial state for the steps - state := make(map[string]interface{}) - state["config"] = b.config - state["accessor"] = auth api := &gophercloud.ApiCriteria{ Name: "cloudServersOpenStack", Region: "DFW", VersionId: "2", UrlChoice: gophercloud.PublicURL, } - state["api"] = api + csp, err := gophercloud.ServersApi(auth, *api) + if err != nil { + return nil, err + } + // Setup the state bag and initial state for the steps + state := make(map[string]interface{}) + state["config"] = b.config + state["csp"] = csp state["hook"] = hook state["ui"] = ui @@ -85,7 +87,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe SourceImage: b.config.SourceImage, }, &common.StepConnectSSH{ - SSHAddress: SSHAddress(&auth, api, b.config.SSHPort), + SSHAddress: SSHAddress(csp, b.config.SSHPort), SSHConfig: SSHConfig(b.config.SSHUsername), SSHWaitTimeout: b.config.SSHTimeout(), }, diff --git a/builder/openstack/server.go b/builder/openstack/server.go index 66703bb44..810c854b7 100644 --- a/builder/openstack/server.go +++ b/builder/openstack/server.go @@ -23,8 +23,6 @@ type StateRefreshFunc func() (result interface{}, state string, progress int, er // StateChangeConf is the configuration struct used for `WaitForState`. type StateChangeConf struct { - Accessor *gophercloud.Access - Api *gophercloud.ApiCriteria Pending []string Refresh StateRefreshFunc StepState map[string]interface{} @@ -33,9 +31,8 @@ type StateChangeConf struct { // ServerStateRefreshFunc returns a StateRefreshFunc that is used to watch // an openstacn server. -func ServerStateRefreshFunc(accessor *gophercloud.Access, api *gophercloud.ApiCriteria, s *gophercloud.Server) StateRefreshFunc { +func ServerStateRefreshFunc(csp gophercloud.CloudServersProvider, s *gophercloud.Server) StateRefreshFunc { return func() (interface{}, string, int, error) { - csp, err := gophercloud.ServersApi(accessor, *api) resp, err := csp.ServerById(s.Id) if err != nil { log.Printf("Error on ServerStateRefresh: %s", err) diff --git a/builder/openstack/ssh.go b/builder/openstack/ssh.go index 0c415d074..5d4a13d3c 100644 --- a/builder/openstack/ssh.go +++ b/builder/openstack/ssh.go @@ -11,14 +11,13 @@ import ( // SSHAddress returns a function that can be given to the SSH communicator // for determining the SSH address based on the server AccessIPv4 setting.. -func SSHAddress(accessor *gophercloud.AccessProvider, api *gophercloud.ApiCriteria, port int) func(map[string]interface{}) (string, error) { +func SSHAddress(csp gophercloud.CloudServersProvider, port int) func(map[string]interface{}) (string, error) { return func(state map[string]interface{}) (string, error) { for j := 0; j < 2; j++ { s := state["server"].(*gophercloud.Server) if s.AccessIPv4 != "" { return fmt.Sprintf("%s:%d", s.AccessIPv4, port), nil } - csp, err := gophercloud.ServersApi(*accessor, *api) serverState, err := csp.ServerById(s.Id) if err != nil { diff --git a/builder/openstack/step_create_image.go b/builder/openstack/step_create_image.go index 761fa4954..bd98425fc 100644 --- a/builder/openstack/step_create_image.go +++ b/builder/openstack/step_create_image.go @@ -12,8 +12,7 @@ import ( type stepCreateImage struct{} func (s *stepCreateImage) Run(state map[string]interface{}) multistep.StepAction { - accessor := state["accessor"].(*gophercloud.Access) - api := state["api"].(*gophercloud.ApiCriteria) + csp := state["csp"].(gophercloud.CloudServersProvider) config := state["config"].(config) server := state["server"].(*gophercloud.Server) ui := state["ui"].(packer.Ui) @@ -23,7 +22,6 @@ func (s *stepCreateImage) Run(state map[string]interface{}) multistep.StepAction createOpts := gophercloud.CreateImage{ Name: config.ImageName, } - csp, err := gophercloud.ServersApi(accessor, *api) imageId, err := csp.CreateImage(server.Id, createOpts) if err != nil { err := fmt.Errorf("Error creating image: %s", err) @@ -38,7 +36,7 @@ func (s *stepCreateImage) Run(state map[string]interface{}) multistep.StepAction // Wait for the image to become ready ui.Say("Waiting for image to become ready...") - if err := WaitForImage(accessor, api, imageId); err != nil { + if err := WaitForImage(csp, imageId); err != nil { err := fmt.Errorf("Error waiting for image: %s", err) state["error"] = err ui.Error(err.Error()) @@ -53,9 +51,8 @@ func (s *stepCreateImage) Cleanup(map[string]interface{}) { } // WaitForImage waits for the given Image ID to become ready. -func WaitForImage(accessor *gophercloud.Access, api *gophercloud.ApiCriteria, imageId string) error { +func WaitForImage(csp gophercloud.CloudServersProvider, imageId string) error { for { - csp, err := gophercloud.ServersApi(accessor, *api) image, err := csp.ImageById(imageId) if err != nil { return err diff --git a/builder/openstack/step_key_pair.go b/builder/openstack/step_key_pair.go index ea630527c..0a1ec7c66 100644 --- a/builder/openstack/step_key_pair.go +++ b/builder/openstack/step_key_pair.go @@ -15,14 +15,12 @@ type StepKeyPair struct { } func (s *StepKeyPair) Run(state map[string]interface{}) multistep.StepAction { - accessor := state["accessor"].(*gophercloud.Access) - api := state["api"].(*gophercloud.ApiCriteria) + csp := state["csp"].(gophercloud.CloudServersProvider) ui := state["ui"].(packer.Ui) ui.Say("Creating temporary keypair for this instance...") keyName := fmt.Sprintf("packer %s", hex.EncodeToString(identifier.NewUUID().Raw())) log.Printf("temporary keypair name: %s", keyName) - csp, err := gophercloud.ServersApi(accessor, *api) keyResp, err := csp.CreateKeyPair(gophercloud.NewKeyPair{Name: keyName}) if err != nil { state["error"] = fmt.Errorf("Error creating temporary keypair: %s", err) @@ -45,13 +43,11 @@ func (s *StepKeyPair) Cleanup(state map[string]interface{}) { return } - accessor := state["accessor"].(*gophercloud.Access) - api := state["api"].(*gophercloud.ApiCriteria) + csp := state["csp"].(gophercloud.CloudServersProvider) ui := state["ui"].(packer.Ui) ui.Say("Deleting temporary keypair...") - csp, err := gophercloud.ServersApi(accessor, *api) - err = csp.DeleteKeyPair(s.keyName) + err := csp.DeleteKeyPair(s.keyName) if err != nil { ui.Error(fmt.Sprintf( "Error cleaning up keypair. Please delete the key manually: %s", s.keyName)) diff --git a/builder/openstack/step_run_source_server.go b/builder/openstack/step_run_source_server.go index 11f47afbd..7a2b7bc64 100644 --- a/builder/openstack/step_run_source_server.go +++ b/builder/openstack/step_run_source_server.go @@ -17,19 +17,10 @@ type StepRunSourceServer struct { } func (s *StepRunSourceServer) Run(state map[string]interface{}) multistep.StepAction { - accessor := state["accessor"].(*gophercloud.Access) - api := state["api"].(*gophercloud.ApiCriteria) + csp := state["csp"].(gophercloud.CloudServersProvider) keyName := state["keyPair"].(string) ui := state["ui"].(packer.Ui) - csp, err := gophercloud.ServersApi(accessor, *api) - if err != nil { - err := fmt.Errorf("Error connecting to api: %s", err) - state["error"] = err - ui.Error(err.Error()) - return multistep.ActionHalt - } - // XXX - validate image and flavor is available server := gophercloud.NewServer{ @@ -52,11 +43,9 @@ func (s *StepRunSourceServer) Run(state map[string]interface{}) multistep.StepAc ui.Say(fmt.Sprintf("Waiting for server (%s) to become ready...", s.server.Id)) stateChange := StateChangeConf{ - Accessor: accessor, - Api: api, Pending: []string{"BUILD"}, Target: "ACTIVE", - Refresh: ServerStateRefreshFunc(accessor, api, s.server), + Refresh: ServerStateRefreshFunc(csp, s.server), StepState: state, } latestServer, err := WaitForState(&stateChange) @@ -78,18 +67,9 @@ func (s *StepRunSourceServer) Cleanup(state map[string]interface{}) { return } - accessor := state["accessor"].(*gophercloud.Access) - api := state["api"].(*gophercloud.ApiCriteria) + csp := state["csp"].(gophercloud.CloudServersProvider) ui := state["ui"].(packer.Ui) - csp, err := gophercloud.ServersApi(accessor, *api) - if err != nil { - err := fmt.Errorf("Error connecting to api: %s", err) - state["error"] = err - ui.Error(err.Error()) - return - } - ui.Say("Terminating the source server...") if err := csp.DeleteServerById(s.server.Id); err != nil { ui.Error(fmt.Sprintf("Error terminating server, may still be around: %s", err)) @@ -97,11 +77,9 @@ func (s *StepRunSourceServer) Cleanup(state map[string]interface{}) { } stateChange := StateChangeConf{ - Accessor: accessor, - Api: api, - Pending: []string{"ACTIVE", "BUILD", "REBUILD", "SUSPENDED"}, - Refresh: ServerStateRefreshFunc(accessor, api, s.server), - Target: "DELETED", + Pending: []string{"ACTIVE", "BUILD", "REBUILD", "SUSPENDED"}, + Refresh: ServerStateRefreshFunc(csp, s.server), + Target: "DELETED", } WaitForState(&stateChange)