func (d *Driver) GetState() (state.State, error) { client := egoscale.NewClient(d.URL, d.ApiKey, d.ApiSecretKey) vm, err := client.GetVirtualMachine(d.Id) if err != nil { return state.Error, err } switch vm.State { case "Starting": return state.Starting, nil case "Running": return state.Running, nil case "Stopping": return state.Running, nil case "Stopped": return state.Stopped, nil case "Destroyed": return state.Stopped, nil case "Expunging": return state.Stopped, nil case "Migrating": return state.Paused, nil case "Error": return state.Error, nil case "Unknown": return state.Error, nil case "Shutdowned": return state.Stopped, nil } return state.None, nil }
func (d *Driver) Restart() error { client := egoscale.NewClient(d.URL, d.APIKey, d.APISecretKey) svmresp, err := client.RebootVirtualMachine(d.ID) if err != nil { return err } return d.waitForJob(client, svmresp) }
// Uses the supplied parameters to return a DNSProvider instance // configured for Exoscale. func NewDNSProviderClient(key, secret, endpoint string) (*DNSProvider, error) { if key == "" || secret == "" { return nil, fmt.Errorf("Exoscale credentials missing") } if endpoint == "" { endpoint = "https://api.exoscale.ch/dns" } return &DNSProvider{ client: egoscale.NewClient(endpoint, key, secret), }, nil }
func (d *Driver) Remove() error { client := egoscale.NewClient(d.URL, d.ApiKey, d.ApiSecretKey) // Destroy the SSH key if _, err := client.DeleteKeypair(d.KeyPair); err != nil { return err } // Destroy the virtual machine dvmresp, err := client.DestroyVirtualMachine(d.Id) if err != nil { return err } if err = d.waitForJob(client, dvmresp); err != nil { return err } return nil }
func (d *Driver) Restart() error { vmstate, err := d.GetState() if err != nil { return err } if vmstate == state.Stopped { return fmt.Errorf("Host is stopped, use start command to start it") } client := egoscale.NewClient(d.URL, d.ApiKey, d.ApiSecretKey) svmresp, err := client.RebootVirtualMachine(d.Id) if err != nil { return err } if err = d.waitForJob(client, svmresp); err != nil { return err } return nil }
func (d *Driver) Stop() error { vmstate, err := d.GetState() if err != nil { return err } if vmstate == state.Stopped { log.Infof("Host is already stopped") return nil } client := egoscale.NewClient(d.URL, d.ApiKey, d.ApiSecretKey) svmresp, err := client.StopVirtualMachine(d.Id) if err != nil { return err } if err = d.waitForJob(client, svmresp); err != nil { return err } return nil }
func (d *Driver) Start() error { vmstate, err := d.GetState() if err != nil { return err } if vmstate == state.Running || vmstate == state.Starting { log.Infof("Host is already running or starting") return nil } client := egoscale.NewClient(d.URL, d.APIKey, d.APISecretKey) svmresp, err := client.StartVirtualMachine(d.ID) if err != nil { return err } if err = d.waitForJob(client, svmresp); err != nil { return err } return nil }
func (d *Driver) Create() error { log.Infof("Querying exoscale for the requested parameters...") client := egoscale.NewClient(d.URL, d.ApiKey, d.ApiSecretKey) topology, err := client.GetTopology() if err != nil { return err } // Availability zone UUID zone, ok := topology.Zones[d.AvailabilityZone] if !ok { return fmt.Errorf("Availability zone %v doesn't exist", d.AvailabilityZone) } log.Debugf("Availability zone %v = %s", d.AvailabilityZone, zone) // Image UUID var tpl string images, ok := topology.Images[strings.ToLower(d.Image)] if ok { tpl, ok = images[d.DiskSize] } if !ok { return fmt.Errorf("Unable to find image %v with size %d", d.Image, d.DiskSize) } log.Debugf("Image %v(%d) = %s", d.Image, d.DiskSize, tpl) // Profile UUID profile, ok := topology.Profiles[strings.ToLower(d.InstanceProfile)] if !ok { return fmt.Errorf("Unable to find the %s profile", d.InstanceProfile) } log.Debugf("Profile %v = %s", d.InstanceProfile, profile) // Security groups securityGroups := strings.Split(d.SecurityGroup, ",") sgs := make([]string, len(securityGroups)) for idx, group := range securityGroups { sg, ok := topology.SecurityGroups[group] if !ok { log.Infof("Security group %v does not exist, create it", group) sg, err = d.createDefaultSecurityGroup(client, group) if err != nil { return err } } log.Debugf("Security group %v = %s", group, sg) sgs[idx] = sg } log.Infof("Generate an SSH keypair...") keypairName := fmt.Sprintf("docker-machine-%s", d.MachineName) kpresp, err := client.CreateKeypair(keypairName) if err != nil { return err } err = ioutil.WriteFile(d.GetSSHKeyPath(), []byte(kpresp.Privatekey), 0600) if err != nil { return err } d.KeyPair = keypairName log.Infof("Spawn exoscale host...") userdata, err := d.getCloudInit() if err != nil { return err } log.Debugf("Using the following cloud-init file:") log.Debugf("%s", userdata) machineProfile := egoscale.MachineProfile{ Template: tpl, ServiceOffering: profile, SecurityGroups: sgs, Userdata: userdata, Zone: zone, Keypair: d.KeyPair, Name: d.MachineName, } cvmresp, err := client.CreateVirtualMachine(machineProfile) if err != nil { return err } vm, err := d.waitForVM(client, cvmresp) if err != nil { return err } d.IPAddress = vm.Nic[0].Ipaddress d.Id = vm.Id return nil }