Inlined ctx
This commit is contained in:
parent
3ad05eb85c
commit
5ee212c85f
|
@ -45,8 +45,7 @@ func (a *Artifact) State(name string) interface{} {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Artifact) Destroy() error {
|
func (a *Artifact) Destroy() error {
|
||||||
ctx := context.TODO()
|
|
||||||
log.Printf("Destroying image: %d (%s)", a.snapshotId, a.snapshotName)
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,6 @@ func (s *stepCreateDroplet) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
c := state.Get("config").(Config)
|
c := state.Get("config").(Config)
|
||||||
sshKeyId := state.Get("ssh_key_id").(int)
|
sshKeyId := state.Get("ssh_key_id").(int)
|
||||||
ctx := context.TODO()
|
|
||||||
|
|
||||||
// Create the droplet based on configuration
|
// Create the droplet based on configuration
|
||||||
ui.Say("Creating droplet...")
|
ui.Say("Creating droplet...")
|
||||||
|
@ -36,7 +35,7 @@ func (s *stepCreateDroplet) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
userData = string(contents)
|
userData = string(contents)
|
||||||
}
|
}
|
||||||
|
|
||||||
droplet, _, err := client.Droplets.Create(ctx, &godo.DropletCreateRequest{
|
droplet, _, err := client.Droplets.Create(context.TODO(), &godo.DropletCreateRequest{
|
||||||
Name: c.DropletName,
|
Name: c.DropletName,
|
||||||
Region: c.Region,
|
Region: c.Region,
|
||||||
Size: c.Size,
|
Size: c.Size,
|
||||||
|
@ -74,11 +73,10 @@ func (s *stepCreateDroplet) Cleanup(state multistep.StateBag) {
|
||||||
|
|
||||||
client := state.Get("client").(*godo.Client)
|
client := state.Get("client").(*godo.Client)
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
ctx := context.TODO()
|
|
||||||
|
|
||||||
// Destroy the droplet we just created
|
// Destroy the droplet we just created
|
||||||
ui.Say("Destroying droplet...")
|
ui.Say("Destroying droplet...")
|
||||||
_, err := client.Droplets.Delete(ctx, s.dropletId)
|
_, err := client.Droplets.Delete(context.TODO(), s.dropletId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ui.Error(fmt.Sprintf(
|
ui.Error(fmt.Sprintf(
|
||||||
"Error destroying droplet. Please destroy it manually: %s", err))
|
"Error destroying droplet. Please destroy it manually: %s", err))
|
||||||
|
|
|
@ -28,7 +28,6 @@ type stepCreateSSHKey struct {
|
||||||
func (s *stepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction {
|
func (s *stepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
client := state.Get("client").(*godo.Client)
|
client := state.Get("client").(*godo.Client)
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
ctx := context.TODO()
|
|
||||||
|
|
||||||
ui.Say("Creating temporary ssh key for droplet...")
|
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())
|
name := fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID())
|
||||||
|
|
||||||
// Create the key!
|
// Create the key!
|
||||||
key, _, err := client.Keys.Create(ctx, &godo.KeyCreateRequest{
|
key, _, err := client.Keys.Create(context.TODO(), &godo.KeyCreateRequest{
|
||||||
Name: name,
|
Name: name,
|
||||||
PublicKey: pub_sshformat,
|
PublicKey: pub_sshformat,
|
||||||
})
|
})
|
||||||
|
@ -109,10 +108,9 @@ func (s *stepCreateSSHKey) Cleanup(state multistep.StateBag) {
|
||||||
|
|
||||||
client := state.Get("client").(*godo.Client)
|
client := state.Get("client").(*godo.Client)
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
ctx := context.TODO()
|
|
||||||
|
|
||||||
ui.Say("Deleting temporary ssh key...")
|
ui.Say("Deleting temporary ssh key...")
|
||||||
_, err := client.Keys.DeleteByID(ctx, s.keyId)
|
_, err := client.Keys.DeleteByID(context.TODO(), s.keyId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error cleaning up ssh key: %s", err)
|
log.Printf("Error cleaning up ssh key: %s", err)
|
||||||
ui.Error(fmt.Sprintf(
|
ui.Error(fmt.Sprintf(
|
||||||
|
|
|
@ -16,7 +16,6 @@ func (s *stepDropletInfo) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
c := state.Get("config").(Config)
|
c := state.Get("config").(Config)
|
||||||
dropletID := state.Get("droplet_id").(int)
|
dropletID := state.Get("droplet_id").(int)
|
||||||
ctx := context.TODO()
|
|
||||||
|
|
||||||
ui.Say("Waiting for droplet to become active...")
|
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
|
// 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 {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error retrieving droplet: %s", err)
|
err := fmt.Errorf("Error retrieving droplet: %s", err)
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
|
|
|
@ -17,9 +17,8 @@ func (s *stepPowerOff) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
c := state.Get("config").(Config)
|
c := state.Get("config").(Config)
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
dropletId := state.Get("droplet_id").(int)
|
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 {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error checking droplet state: %s", err)
|
err := fmt.Errorf("Error checking droplet state: %s", err)
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
|
@ -34,7 +33,7 @@ func (s *stepPowerOff) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
|
|
||||||
// Pull the plug on the Droplet
|
// Pull the plug on the Droplet
|
||||||
ui.Say("Forcefully shutting down Droplet...")
|
ui.Say("Forcefully shutting down Droplet...")
|
||||||
_, _, err = client.DropletActions.PowerOff(ctx, dropletId)
|
_, _, err = client.DropletActions.PowerOff(context.TODO(), dropletId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error powering off droplet: %s", err)
|
err := fmt.Errorf("Error powering off droplet: %s", err)
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
|
|
|
@ -18,14 +18,13 @@ func (s *stepShutdown) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
c := state.Get("config").(Config)
|
c := state.Get("config").(Config)
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
dropletId := state.Get("droplet_id").(int)
|
dropletId := state.Get("droplet_id").(int)
|
||||||
ctx := context.TODO()
|
|
||||||
|
|
||||||
// Gracefully power off the droplet. We have to retry this a number
|
// Gracefully power off the droplet. We have to retry this a number
|
||||||
// of times because sometimes it says it completed when it actually
|
// of times because sometimes it says it completed when it actually
|
||||||
// did absolutely nothing (*ALAKAZAM!* magic!). We give up after
|
// did absolutely nothing (*ALAKAZAM!* magic!). We give up after
|
||||||
// a pretty arbitrary amount of time.
|
// a pretty arbitrary amount of time.
|
||||||
ui.Say("Gracefully shutting down droplet...")
|
ui.Say("Gracefully shutting down droplet...")
|
||||||
_, _, err := client.DropletActions.Shutdown(ctx, dropletId)
|
_, _, err := client.DropletActions.Shutdown(context.TODO(), dropletId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If we get an error the first time, actually report it
|
// If we get an error the first time, actually report it
|
||||||
err := fmt.Errorf("Error shutting down droplet: %s", err)
|
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++ {
|
for attempts := 2; attempts > 0; attempts++ {
|
||||||
log.Printf("ShutdownDroplet attempt #%d...", attempts)
|
log.Printf("ShutdownDroplet attempt #%d...", attempts)
|
||||||
_, _, err := client.DropletActions.Shutdown(ctx, dropletId)
|
_, _, err := client.DropletActions.Shutdown(contect.TODO(), dropletId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Shutdown retry error: %s", err)
|
log.Printf("Shutdown retry error: %s", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,10 +19,9 @@ func (s *stepSnapshot) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
c := state.Get("config").(Config)
|
c := state.Get("config").(Config)
|
||||||
dropletId := state.Get("droplet_id").(int)
|
dropletId := state.Get("droplet_id").(int)
|
||||||
ctx := context.TODO()
|
|
||||||
|
|
||||||
ui.Say(fmt.Sprintf("Creating snapshot: %v", c.SnapshotName))
|
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 {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error creating snapshot: %s", err)
|
err := fmt.Errorf("Error creating snapshot: %s", err)
|
||||||
state.Put("error", 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)
|
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 {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error looking up snapshot ID: %s", err)
|
err := fmt.Errorf("Error looking up snapshot ID: %s", err)
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
|
|
|
@ -13,7 +13,6 @@ import (
|
||||||
// avoid "pending" errors when making state changes.
|
// avoid "pending" errors when making state changes.
|
||||||
func waitForDropletUnlocked(
|
func waitForDropletUnlocked(
|
||||||
client *godo.Client, dropletId int, timeout time.Duration) error {
|
client *godo.Client, dropletId int, timeout time.Duration) error {
|
||||||
ctx := context.TODO()
|
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
defer close(done)
|
defer close(done)
|
||||||
|
|
||||||
|
@ -24,7 +23,7 @@ func waitForDropletUnlocked(
|
||||||
attempts += 1
|
attempts += 1
|
||||||
|
|
||||||
log.Printf("[DEBUG] Checking droplet lock state... (attempt: %d)", attempts)
|
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 {
|
if err != nil {
|
||||||
result <- err
|
result <- err
|
||||||
return
|
return
|
||||||
|
@ -64,7 +63,6 @@ func waitForDropletUnlocked(
|
||||||
func waitForDropletState(
|
func waitForDropletState(
|
||||||
desiredState string, dropletId int,
|
desiredState string, dropletId int,
|
||||||
client *godo.Client, timeout time.Duration) error {
|
client *godo.Client, timeout time.Duration) error {
|
||||||
ctx := context.TODO()
|
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
defer close(done)
|
defer close(done)
|
||||||
|
|
||||||
|
@ -75,7 +73,7 @@ func waitForDropletState(
|
||||||
attempts += 1
|
attempts += 1
|
||||||
|
|
||||||
log.Printf("Checking droplet status... (attempt: %d)", attempts)
|
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 {
|
if err != nil {
|
||||||
result <- err
|
result <- err
|
||||||
return
|
return
|
||||||
|
@ -115,7 +113,6 @@ func waitForDropletState(
|
||||||
func waitForActionState(
|
func waitForActionState(
|
||||||
desiredState string, dropletId, actionId int,
|
desiredState string, dropletId, actionId int,
|
||||||
client *godo.Client, timeout time.Duration) error {
|
client *godo.Client, timeout time.Duration) error {
|
||||||
ctx := context.TODO()
|
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
defer close(done)
|
defer close(done)
|
||||||
|
|
||||||
|
@ -126,7 +123,7 @@ func waitForActionState(
|
||||||
attempts += 1
|
attempts += 1
|
||||||
|
|
||||||
log.Printf("Checking action status... (attempt: %d)", attempts)
|
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 {
|
if err != nil {
|
||||||
result <- err
|
result <- err
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue