Skip to content

Commit

Permalink
Merge pull request #123 from digitalocean/asb/issues/122
Browse files Browse the repository at this point in the history
builder: fix distributing images to additional regions
  • Loading branch information
andrewsomething authored Feb 8, 2024
2 parents b1b67c4 + a065707 commit 0df3361
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
54 changes: 53 additions & 1 deletion builder/digitalocean/builder_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"os/exec"
"testing"

"github.com/digitalocean/godo"
Expand All @@ -20,6 +21,14 @@ func TestBuilderAcc_basic(t *testing.T) {
acctest.TestPlugin(t, &acctest.PluginTestCase{
Name: "test-digitalocean-builder-basic",
Template: fmt.Sprintf(testBuilderAccBasic, "ubuntu-20-04-x64"),
Check: func(buildCommand *exec.Cmd, logfile string) error {
if buildCommand.ProcessState != nil {
if buildCommand.ProcessState.ExitCode() != 0 {
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
}
}
return nil
},
})
}

Expand All @@ -30,6 +39,32 @@ func TestBuilderAcc_imageId(t *testing.T) {
acctest.TestPlugin(t, &acctest.PluginTestCase{
Name: "test-digitalocean-builder-imageID",
Template: makeTemplateWithImageId(t),
Check: func(buildCommand *exec.Cmd, logfile string) error {
if buildCommand.ProcessState != nil {
if buildCommand.ProcessState.ExitCode() != 0 {
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
}
}
return nil
},
})
}

func TestBuilderAcc_multiRegion(t *testing.T) {
if skip := testAccPreCheck(t); skip == true {
return
}
acctest.TestPlugin(t, &acctest.PluginTestCase{
Name: "test-digitalocean-builder-multi-region",
Template: fmt.Sprintf(testBuilderAccMultiRegion, "ubuntu-20-04-x64"),
Check: func(buildCommand *exec.Cmd, logfile string) error {
if buildCommand.ProcessState != nil {
if buildCommand.ProcessState.ExitCode() != 0 {
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
}
}
return nil
},
})
}

Expand Down Expand Up @@ -84,7 +119,8 @@ func makeTemplateWithImageId(t *testing.T) string {
return ""
}

const testBuilderAccBasic = `
const (
testBuilderAccBasic = `
{
"builders": [{
"type": "digitalocean",
Expand All @@ -97,3 +133,19 @@ const testBuilderAccBasic = `
}]
}
`

testBuilderAccMultiRegion = `
{
"builders": [{
"type": "digitalocean",
"region": "nyc2",
"size": "s-1vcpu-1gb",
"image": "%v",
"ssh_username": "root",
"user_data": "",
"user_data_file": "",
"snapshot_regions": ["nyc2", "nyc3"]
}]
}
`
)
6 changes: 4 additions & 2 deletions builder/digitalocean/step_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,17 @@ func (s *stepSnapshot) Run(ctx context.Context, state multistep.StateBag) multis
"type": "transfer",
"region": snapshotRegions[transfer],
}

ui.Say(fmt.Sprintf("Transferring snapshot (ID: %d) to %s", images[0].ID, snapshotRegions[transfer]))
imageTransfer, _, err := client.ImageActions.Transfer(context.TODO(), images[0].ID, transferRequest)
if err != nil {
err := fmt.Errorf("Error transferring snapshot: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
ui.Say(fmt.Sprintf("transferring Snapshot ID: %d", imageTransfer.ID))
if err := WaitForImageState(godo.ActionCompleted, imageTransfer.ID, action.ID,

if err := WaitForImageState(godo.ActionCompleted, images[0].ID, imageTransfer.ID,
client, 20*time.Minute); err != nil {
// If we get an error the first time, actually report it
err := fmt.Errorf("Error waiting for snapshot transfer: %s", err)
Expand Down

0 comments on commit 0df3361

Please sign in to comment.