2016-11-13 17:34:36 -05:00
|
|
|
package oneandone
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2016-11-13 17:34:36 -05:00
|
|
|
"fmt"
|
2018-01-22 20:21:10 -05:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2016-11-13 17:34:36 -05:00
|
|
|
"github.com/1and1/oneandone-cloudserver-sdk-go"
|
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-11-13 17:34:36 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepCreateServer struct{}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *stepCreateServer) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2016-11-13 17:34:36 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
c := state.Get("config").(*Config)
|
|
|
|
|
2016-11-15 18:17:30 -05:00
|
|
|
if sshkey, ok := state.GetOk("publicKey"); ok {
|
|
|
|
c.SSHKey = sshkey.(string)
|
|
|
|
}
|
2016-11-13 17:34:36 -05:00
|
|
|
|
|
|
|
token := oneandone.SetToken(c.Token)
|
2016-11-15 18:17:30 -05:00
|
|
|
|
2016-11-13 17:34:36 -05:00
|
|
|
//Create an API client
|
|
|
|
api := oneandone.New(token, c.Url)
|
|
|
|
|
|
|
|
// List server appliances
|
|
|
|
saps, _ := api.ListServerAppliances()
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 10)
|
|
|
|
|
|
|
|
var sa oneandone.ServerAppliance
|
|
|
|
for _, a := range saps {
|
|
|
|
|
|
|
|
if a.Type == "IMAGE" && strings.Contains(strings.ToLower(a.Name), strings.ToLower(c.Image)) {
|
|
|
|
sa = a
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.DiskSize < sa.MinHddSize {
|
|
|
|
ui.Error(fmt.Sprintf("Minimum required disk size %d", sa.MinHddSize))
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Creating Server...")
|
|
|
|
|
|
|
|
// Create a server
|
|
|
|
req := oneandone.ServerRequest{
|
|
|
|
Name: c.SnapshotName,
|
|
|
|
Description: "Example server description.",
|
|
|
|
ApplianceId: sa.Id,
|
|
|
|
PowerOn: true,
|
|
|
|
Hardware: oneandone.Hardware{
|
|
|
|
Vcores: 1,
|
|
|
|
CoresPerProcessor: 1,
|
|
|
|
Ram: 2,
|
|
|
|
Hdds: []oneandone.Hdd{
|
|
|
|
{
|
|
|
|
Size: c.DiskSize,
|
|
|
|
IsMain: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-11-15 18:17:30 -05:00
|
|
|
if c.DataCenterId != "" {
|
|
|
|
req.DatacenterId = c.DataCenterId
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Comm.SSHPassword != "" {
|
|
|
|
req.Password = c.Comm.SSHPassword
|
2016-11-13 17:34:36 -05:00
|
|
|
}
|
2016-11-15 18:17:30 -05:00
|
|
|
if c.SSHKey != "" {
|
|
|
|
req.SSHKey = c.SSHKey
|
|
|
|
}
|
|
|
|
|
2016-11-13 17:34:36 -05:00
|
|
|
server_id, server, err := api.CreateServer(&req)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
// Wait until server is created and powered on for at most 60 x 10 seconds
|
2016-11-15 18:17:30 -05:00
|
|
|
err = api.WaitForState(server, "POWERED_ON", 10, c.Retries)
|
2016-11-13 17:34:36 -05:00
|
|
|
} else {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a server
|
|
|
|
server, err = api.GetServer(server_id)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Put("server_id", server_id)
|
|
|
|
|
|
|
|
state.Put("server_ip", server.Ips[0].Ip)
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepCreateServer) Cleanup(state multistep.StateBag) {
|
|
|
|
c := state.Get("config").(*Config)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
ui.Say("Removing Server...")
|
|
|
|
|
|
|
|
token := oneandone.SetToken(c.Token)
|
|
|
|
//Create an API client
|
|
|
|
api := oneandone.New(token, oneandone.BaseUrl)
|
|
|
|
|
2016-11-15 18:17:30 -05:00
|
|
|
var serverId string
|
|
|
|
if temp, ok := state.GetOk("server_id"); ok {
|
|
|
|
serverId = temp.(string)
|
2016-11-13 17:34:36 -05:00
|
|
|
}
|
|
|
|
|
2016-11-15 18:17:30 -05:00
|
|
|
if serverId != "" {
|
|
|
|
server, err := api.ShutdownServer(serverId, false)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error shutting down 1and1 server. Please destroy it manually: %s", serverId))
|
|
|
|
ui.Error(err.Error())
|
|
|
|
}
|
|
|
|
err = api.WaitForState(server, "POWERED_OFF", 10, c.Retries)
|
2016-11-13 17:34:36 -05:00
|
|
|
|
2016-11-15 18:17:30 -05:00
|
|
|
server, err = api.DeleteServer(server.Id, false)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error deleting 1and1 server. Please destroy it manually: %s", serverId))
|
|
|
|
ui.Error(err.Error())
|
|
|
|
}
|
2016-11-13 17:34:36 -05:00
|
|
|
}
|
|
|
|
}
|