2016-06-28 22:35:41 -04:00
|
|
|
package profitbricks
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2016-11-01 17:08:04 -04:00
|
|
|
"encoding/json"
|
2017-03-28 20:45:01 -04:00
|
|
|
"time"
|
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2016-06-28 22:35:41 -04:00
|
|
|
"github.com/profitbricks/profitbricks-sdk-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepTakeSnapshot struct{}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepTakeSnapshot) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2016-06-28 22:35:41 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
c := state.Get("config").(*Config)
|
|
|
|
|
|
|
|
ui.Say("Creating ProfitBricks snapshot...")
|
|
|
|
|
|
|
|
profitbricks.SetAuth(c.PBUsername, c.PBPassword)
|
|
|
|
|
|
|
|
dcId := state.Get("datacenter_id").(string)
|
|
|
|
volumeId := state.Get("volume_id").(string)
|
|
|
|
|
2017-08-10 09:15:53 -04:00
|
|
|
snapshot := profitbricks.CreateSnapshot(dcId, volumeId, c.SnapshotName, "")
|
2016-06-28 22:35:41 -04:00
|
|
|
|
2016-07-07 04:28:46 -04:00
|
|
|
state.Put("snapshotname", c.SnapshotName)
|
2016-06-28 22:35:41 -04:00
|
|
|
|
2016-08-01 07:09:07 -04:00
|
|
|
if snapshot.StatusCode > 299 {
|
2016-10-12 18:41:04 -04:00
|
|
|
var restError RestError
|
2017-03-29 16:38:31 -04:00
|
|
|
if err := json.Unmarshal([]byte(snapshot.Response), &restError); err != nil {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2016-11-01 17:08:04 -04:00
|
|
|
if len(restError.Messages) > 0 {
|
2016-10-12 18:41:04 -04:00
|
|
|
ui.Error(restError.Messages[0].Message)
|
|
|
|
} else {
|
|
|
|
ui.Error(snapshot.Response)
|
|
|
|
}
|
|
|
|
|
2016-06-28 22:35:41 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2016-08-01 07:09:07 -04:00
|
|
|
s.waitTillProvisioned(snapshot.Headers.Get("Location"), *c)
|
2016-06-28 22:35:41 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepTakeSnapshot) Cleanup(state multistep.StateBag) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *stepTakeSnapshot) waitTillProvisioned(path string, config Config) {
|
|
|
|
d.setPB(config.PBUsername, config.PBPassword, config.PBUrl)
|
2016-08-01 07:09:07 -04:00
|
|
|
waitCount := 50
|
2016-11-15 18:17:30 -05:00
|
|
|
if config.Retries > 0 {
|
|
|
|
waitCount = config.Retries
|
2016-08-01 07:09:07 -04:00
|
|
|
}
|
2016-06-28 22:35:41 -04:00
|
|
|
for i := 0; i < waitCount; i++ {
|
|
|
|
request := profitbricks.GetRequestStatus(path)
|
2016-08-01 07:09:07 -04:00
|
|
|
if request.Metadata.Status == "DONE" {
|
2016-06-28 22:35:41 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *stepTakeSnapshot) setPB(username string, password string, url string) {
|
|
|
|
profitbricks.SetAuth(username, password)
|
|
|
|
profitbricks.SetEndpoint(url)
|
|
|
|
}
|