Inlined ctx

This commit is contained in:
Rickard von Essen 2017-04-09 20:33:05 +02:00
parent 3ad05eb85c
commit 5ee212c85f
No known key found for this signature in database
GPG Key ID: E0C0327388876CBA
8 changed files with 15 additions and 27 deletions

View File

@ -45,8 +45,7 @@ func (a *Artifact) State(name string) interface{} {
}
func (a *Artifact) Destroy() error {
ctx := context.TODO()
log.Printf("Destroying image: %d (%s)", a.snapshotId, a.snapshotName)
_, err := a.client.Images.Delete(ctx, a.snapshotId)
_, err := a.client.Images.Delete(context.TODO(), a.snapshotId)
return err
}

View File

@ -20,7 +20,6 @@ func (s *stepCreateDroplet) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(Config)
sshKeyId := state.Get("ssh_key_id").(int)
ctx := context.TODO()
// Create the droplet based on configuration
ui.Say("Creating droplet...")
@ -36,7 +35,7 @@ func (s *stepCreateDroplet) Run(state multistep.StateBag) multistep.StepAction {
userData = string(contents)
}
droplet, _, err := client.Droplets.Create(ctx, &godo.DropletCreateRequest{
droplet, _, err := client.Droplets.Create(context.TODO(), &godo.DropletCreateRequest{
Name: c.DropletName,
Region: c.Region,
Size: c.Size,
@ -74,11 +73,10 @@ func (s *stepCreateDroplet) Cleanup(state multistep.StateBag) {
client := state.Get("client").(*godo.Client)
ui := state.Get("ui").(packer.Ui)
ctx := context.TODO()
// Destroy the droplet we just created
ui.Say("Destroying droplet...")
_, err := client.Droplets.Delete(ctx, s.dropletId)
_, err := client.Droplets.Delete(context.TODO(), s.dropletId)
if err != nil {
ui.Error(fmt.Sprintf(
"Error destroying droplet. Please destroy it manually: %s", err))

View File

@ -28,7 +28,6 @@ type stepCreateSSHKey struct {
func (s *stepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*godo.Client)
ui := state.Get("ui").(packer.Ui)
ctx := context.TODO()
ui.Say("Creating temporary ssh key for droplet...")
@ -54,7 +53,7 @@ func (s *stepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction {
name := fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID())
// Create the key!
key, _, err := client.Keys.Create(ctx, &godo.KeyCreateRequest{
key, _, err := client.Keys.Create(context.TODO(), &godo.KeyCreateRequest{
Name: name,
PublicKey: pub_sshformat,
})
@ -109,10 +108,9 @@ func (s *stepCreateSSHKey) Cleanup(state multistep.StateBag) {
client := state.Get("client").(*godo.Client)
ui := state.Get("ui").(packer.Ui)
ctx := context.TODO()
ui.Say("Deleting temporary ssh key...")
_, err := client.Keys.DeleteByID(ctx, s.keyId)
_, err := client.Keys.DeleteByID(context.TODO(), s.keyId)
if err != nil {
log.Printf("Error cleaning up ssh key: %s", err)
ui.Error(fmt.Sprintf(

View File

@ -16,7 +16,6 @@ func (s *stepDropletInfo) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(Config)
dropletID := state.Get("droplet_id").(int)
ctx := context.TODO()
ui.Say("Waiting for droplet to become active...")
@ -29,7 +28,7 @@ func (s *stepDropletInfo) Run(state multistep.StateBag) multistep.StepAction {
}
// Set the IP on the state for later
droplet, _, err := client.Droplets.Get(ctx, dropletID)
droplet, _, err := client.Droplets.Get(context.TODO(), dropletID)
if err != nil {
err := fmt.Errorf("Error retrieving droplet: %s", err)
state.Put("error", err)

View File

@ -17,9 +17,8 @@ func (s *stepPowerOff) Run(state multistep.StateBag) multistep.StepAction {
c := state.Get("config").(Config)
ui := state.Get("ui").(packer.Ui)
dropletId := state.Get("droplet_id").(int)
ctx := context.TODO()
droplet, _, err := client.Droplets.Get(ctx, dropletId)
droplet, _, err := client.Droplets.Get(context.TODO(), dropletId)
if err != nil {
err := fmt.Errorf("Error checking droplet state: %s", err)
state.Put("error", err)
@ -34,7 +33,7 @@ func (s *stepPowerOff) Run(state multistep.StateBag) multistep.StepAction {
// Pull the plug on the Droplet
ui.Say("Forcefully shutting down Droplet...")
_, _, err = client.DropletActions.PowerOff(ctx, dropletId)
_, _, err = client.DropletActions.PowerOff(context.TODO(), dropletId)
if err != nil {
err := fmt.Errorf("Error powering off droplet: %s", err)
state.Put("error", err)

View File

@ -18,14 +18,13 @@ func (s *stepShutdown) Run(state multistep.StateBag) multistep.StepAction {
c := state.Get("config").(Config)
ui := state.Get("ui").(packer.Ui)
dropletId := state.Get("droplet_id").(int)
ctx := context.TODO()
// Gracefully power off the droplet. We have to retry this a number
// of times because sometimes it says it completed when it actually
// did absolutely nothing (*ALAKAZAM!* magic!). We give up after
// a pretty arbitrary amount of time.
ui.Say("Gracefully shutting down droplet...")
_, _, err := client.DropletActions.Shutdown(ctx, dropletId)
_, _, err := client.DropletActions.Shutdown(context.TODO(), dropletId)
if err != nil {
// If we get an error the first time, actually report it
err := fmt.Errorf("Error shutting down droplet: %s", err)
@ -52,7 +51,7 @@ func (s *stepShutdown) Run(state multistep.StateBag) multistep.StepAction {
for attempts := 2; attempts > 0; attempts++ {
log.Printf("ShutdownDroplet attempt #%d...", attempts)
_, _, err := client.DropletActions.Shutdown(ctx, dropletId)
_, _, err := client.DropletActions.Shutdown(contect.TODO(), dropletId)
if err != nil {
log.Printf("Shutdown retry error: %s", err)
}

View File

@ -19,10 +19,9 @@ func (s *stepSnapshot) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(Config)
dropletId := state.Get("droplet_id").(int)
ctx := context.TODO()
ui.Say(fmt.Sprintf("Creating snapshot: %v", c.SnapshotName))
action, _, err := client.DropletActions.Snapshot(ctx, dropletId, c.SnapshotName)
action, _, err := client.DropletActions.Snapshot(context.TODO(), dropletId, c.SnapshotName)
if err != nil {
err := fmt.Errorf("Error creating snapshot: %s", err)
state.Put("error", err)
@ -53,7 +52,7 @@ func (s *stepSnapshot) Run(state multistep.StateBag) multistep.StepAction {
}
log.Printf("Looking up snapshot ID for snapshot: %s", c.SnapshotName)
images, _, err := client.Droplets.Snapshots(ctx, dropletId, nil)
images, _, err := client.Droplets.Snapshots(context.TODO(), dropletId, nil)
if err != nil {
err := fmt.Errorf("Error looking up snapshot ID: %s", err)
state.Put("error", err)

View File

@ -13,7 +13,6 @@ import (
// avoid "pending" errors when making state changes.
func waitForDropletUnlocked(
client *godo.Client, dropletId int, timeout time.Duration) error {
ctx := context.TODO()
done := make(chan struct{})
defer close(done)
@ -24,7 +23,7 @@ func waitForDropletUnlocked(
attempts += 1
log.Printf("[DEBUG] Checking droplet lock state... (attempt: %d)", attempts)
droplet, _, err := client.Droplets.Get(ctx, dropletId)
droplet, _, err := client.Droplets.Get(context.TODO(), dropletId)
if err != nil {
result <- err
return
@ -64,7 +63,6 @@ func waitForDropletUnlocked(
func waitForDropletState(
desiredState string, dropletId int,
client *godo.Client, timeout time.Duration) error {
ctx := context.TODO()
done := make(chan struct{})
defer close(done)
@ -75,7 +73,7 @@ func waitForDropletState(
attempts += 1
log.Printf("Checking droplet status... (attempt: %d)", attempts)
droplet, _, err := client.Droplets.Get(ctx, dropletId)
droplet, _, err := client.Droplets.Get(context.TODO(), dropletId)
if err != nil {
result <- err
return
@ -115,7 +113,6 @@ func waitForDropletState(
func waitForActionState(
desiredState string, dropletId, actionId int,
client *godo.Client, timeout time.Duration) error {
ctx := context.TODO()
done := make(chan struct{})
defer close(done)
@ -126,7 +123,7 @@ func waitForActionState(
attempts += 1
log.Printf("Checking action status... (attempt: %d)", attempts)
action, _, err := client.DropletActions.Get(ctx, dropletId, actionId)
action, _, err := client.DropletActions.Get(context.TODO(), dropletId, actionId)
if err != nil {
result <- err
return