Merge pull request #2423 from rn/p5

Allowing re-using existing machines on packet.net
This commit is contained in:
Justin Cormack 2017-08-17 11:34:59 +01:00 committed by GitHub
commit fb9805eac5
19 changed files with 100 additions and 18 deletions

View File

@ -22,6 +22,17 @@ The `linuxkit run packet` command can mostly either be configured via
command line options or with environment variables. see `linuxkit run
packet --help` for the options and environment variables.
By default, `linuxkit run` will provision a new machine and remove it
once you are done. With the `-keep` option the provisioned machine
will not be removed. You can then use the `-device` option with the
device ID on subsequent `linuxkit run` invocations to re-use an
existing machine. These subsequent runs will update the iPXE data so
you can boot alternative kernels on an existing machine.
**Note**: The update of the iPXE configuration sometimes may take some
time and the first boot may fail. Hitting return on the console to
retry the boot typically fixes this.
## Boot
LinuxKit on Packet boots the `kernel+initrd` output from moby
@ -67,6 +78,9 @@ Packet
requires `ssh` access, i.e., you must have uploaded your SSH keys to
Packet beforehand.
You can exit the console vi `~.` on a new line once you are
disconnected from the serial, e.g. after poweroff.
**Note**: We also require that the Packet SOS host is in your
`known_hosts` file, otherwise the connection to the console will
fail. There is a Packet SOS host per zone.

View File

@ -16,7 +16,7 @@ import (
"path/filepath"
"time"
"github.com/packethost/packngo"
"github.com/rn/packngo"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
@ -60,6 +60,7 @@ func runPacket(args []string) {
machineFlag := flags.String("machine", packetDefaultMachine, "Packet Machine Type (or "+packetMachineVar+")")
apiKeyFlag := flags.String("api-key", "", "Packet API key (or "+packetAPIKeyVar+")")
projectFlag := flags.String("project-id", "", "Packet Project ID (or "+packetProjectIDVar+")")
deviceFlag := flags.String("device", "", "The ID of an existing device")
hostNameFlag := flags.String("hostname", packetDefaultHostname, "Hostname of new instance (or "+packetHostnameVar+")")
nameFlag := flags.String("img-name", "", "Overrides the prefix used to identify the files. Defaults to [name] (or "+packetNameVar+")")
alwaysPXE := flags.Bool("always-pxe", true, "Reboot from PXE every time.")
@ -150,20 +151,52 @@ func runPacket(args []string) {
client := packngo.NewClient("", apiKey, nil)
tags := []string{}
req := packngo.DeviceCreateRequest{
HostName: hostname,
Plan: plan,
Facility: facility,
OS: osType,
BillingCycle: billing,
ProjectID: projectID,
UserData: userData,
Tags: tags,
AlwaysPXE: *alwaysPXE,
}
dev, _, err := client.Devices.Create(&req)
if err != nil {
log.Fatal(err)
var dev *packngo.Device
var err error
if *deviceFlag != "" {
dev, _, err = client.Devices.Get(*deviceFlag)
if err != nil {
log.Fatalf("Getting info for device %s failed: %v", *deviceFlag, err)
}
b, err := json.MarshalIndent(dev, "", " ")
if err != nil {
log.Fatal(err)
}
log.Debugf("%s\n", string(b))
req := packngo.DeviceUpdateRequest{
HostName: hostname,
BillingCycle: dev.BillingCycle,
UserData: userData,
Locked: dev.Locked,
Tags: dev.Tags,
AlwaysPXE: *alwaysPXE,
}
dev, _, err = client.Devices.Update(*deviceFlag, &req)
if err != nil {
log.Fatalf("Update device %s failed: %v", *deviceFlag, err)
}
if _, err := client.Devices.Reboot(*deviceFlag); err != nil {
log.Fatalf("Rebooting device %s failed: %v", *deviceFlag, err)
}
} else {
// Create a new device
req := packngo.DeviceCreateRequest{
HostName: hostname,
Plan: plan,
Facility: facility,
OS: osType,
BillingCycle: billing,
ProjectID: projectID,
UserData: userData,
Tags: tags,
AlwaysPXE: *alwaysPXE,
}
dev, _, err = client.Devices.Create(&req)
if err != nil {
log.Fatalf("Creating device failed: %v", err)
}
}
b, err := json.MarshalIndent(dev, "", " ")
if err != nil {
@ -200,12 +233,15 @@ func runPacket(args []string) {
httpServer.Shutdown(ctx)
}
if !*keepFlag {
if *keepFlag {
log.Printf("The machine is kept...")
log.Printf("Device ID: %s", dev.ID)
log.Printf("Serial: ssh %s@%s", dev.ID, sshHost)
} else {
if _, err := client.Devices.Delete(dev.ID); err != nil {
log.Fatalf("Unable to delete device: %v", err)
}
}
}
// validateHTTPURL does a sanity check that a URL returns a 200 or 300 response

View File

@ -13,10 +13,10 @@ github.com/gophercloud/gophercloud 2804b72cf099b41d2e25c8afcca786f9f962ddee
github.com/jmespath/go-jmespath bd40a432e4c76585ef6b72d3fd96fb9b6dc7b68d
github.com/mitchellh/go-ps 4fdf99ab29366514c69ccccddab5dc58b8d84062
github.com/moby/hyperkit a82b409a87f12fa3306813410c37f4eed270efac
github.com/packethost/packngo 9d9409c8c09de7695281e900a776cca03676026a
github.com/radu-matei/azure-sdk-for-go 3b12823551999669c9a325a32472508e0af7978e
github.com/radu-matei/azure-vhd-utils e52754d5569d2a643a7775f72ff2a6cf524f4c25
github.com/rn/iso9660wrap 4606f848a055435cdef85305960b0e1bb788d506
github.com/rn/packngo eacc1098296fb3d75607a13e2607474ce40f55b9
github.com/sirupsen/logrus 1.0.2
github.com/vmware/govmomi 6f8ebd89d521d9f9af7a6c2219c4deee511020dd
golang.org/x/crypto 573951cbe80bb6352881271bb276f48749eab6f4

View File

@ -9,6 +9,7 @@ type DeviceService interface {
List(ProjectID string) ([]Device, *Response, error)
Get(string) (*Device, *Response, error)
Create(*DeviceCreateRequest) (*Device, *Response, error)
Update(string, *DeviceUpdateRequest) (*Device, *Response, error)
Delete(string) (*Response, error)
Reboot(string) (*Response, error)
PowerOff(string) (*Response, error)
@ -38,6 +39,7 @@ type Device struct {
Facility *Facility `json:"facility,omitempty"`
Project *Project `json:"project,omitempty"`
ProvisionPer float32 `json:"provisioning_percentage,omitempty"`
UserData string `json:"userdata",omitempty`
IPXEScriptUrl string `json:"ipxe_script_url,omitempty"`
AlwaysPXE bool `json:"always_pxe,omitempty"`
}
@ -61,6 +63,18 @@ type DeviceCreateRequest struct {
AlwaysPXE bool `json:"always_pxe,omitempty"`
}
// DeviceUpdateRequest type used to update a Packet device
type DeviceUpdateRequest struct {
HostName string `json:"hostname"`
Description string `json:"description"`
BillingCycle string `json:"billing_cycle"`
UserData string `json:"userdata"`
Locked bool `json:"locked"`
Tags []string `json:"tags"`
AlwaysPXE bool `json:"always_pxe,omitempty"`
IPXEScriptUrl string `json:"ipxe_script_url,omitempty"`
}
func (d DeviceCreateRequest) String() string {
return Stringify(d)
}
@ -133,6 +147,24 @@ func (s *DeviceServiceOp) Create(createRequest *DeviceCreateRequest) (*Device, *
return device, resp, err
}
// Update updates an existing device
func (s *DeviceServiceOp) Update(deviceID string, updateRequest *DeviceUpdateRequest) (*Device, *Response, error) {
path := fmt.Sprintf("%s/%s?include=facility", deviceBasePath, deviceID)
req, err := s.client.NewRequest("PUT", path, updateRequest)
if err != nil {
return nil, nil, err
}
device := new(Device)
resp, err := s.client.Do(req, device)
if err != nil {
return nil, resp, err
}
return device, resp, err
}
// Delete deletes a device
func (s *DeviceServiceOp) Delete(deviceID string) (*Response, error) {
path := fmt.Sprintf("%s/%s", deviceBasePath, deviceID)