// Remove deletes a machine and it's SSH keys from OVH Cloud
func (d *Driver) Remove() error {
	log.WithField("MachineId", d.InstanceID).Debug("deleting instance...")
	log.Info("Deleting OVH instance...")

	client := d.getClient()

	// Deletes instance
	err := client.DeleteInstance(d.ProjectID, d.InstanceID)
	if err != nil {
		return err
	}

	// If key name  does not starts with the machine ID, this is a pre-existing key, keep it
	if !strings.HasPrefix(d.KeyPairName, d.MachineName) {
		log.WithField("KeyPairID", d.KeyPairID).Debug("keeping key pair...")
		return nil
	}

	// Deletes ssh key
	log.WithField("KeyPairID", d.KeyPairID).Debug("deleting key pair...")
	err = client.DeleteSshkey(d.ProjectID, d.KeyPairID)
	if err != nil {
		return err
	}

	return nil
}
示例#2
0
func (d *Driver) assignFloatingIP() error {
	var err error

	if d.ComputeNetwork {
		err = d.initCompute()
	} else {
		err = d.initNetwork()
	}

	if err != nil {
		return err
	}

	ips, err := d.client.GetFloatingIPs(d)
	if err != nil {
		return err
	}

	var floatingIP *FloatingIP

	log.WithFields(log.Fields{
		"MachineId": d.MachineId,
		"Pool":      d.FloatingIpPool,
	}).Debugf("Looking for an available floating IP")

	for _, ip := range ips {
		if ip.PortId == "" {
			log.WithFields(log.Fields{
				"MachineId": d.MachineId,
				"IP":        ip.Ip,
			}).Debugf("Available floating IP found")
			floatingIP = &ip
			break
		}
	}

	if floatingIP == nil {
		floatingIP = &FloatingIP{}
		log.WithField("MachineId", d.MachineId).Debugf("No available floating IP found. Allocating a new one...")
	} else {
		log.WithField("MachineId", d.MachineId).Debugf("Assigning floating IP to the instance")
	}

	if err := d.client.AssignFloatingIP(d, floatingIP); err != nil {
		return err
	}
	d.IPAddress = floatingIP.Ip
	return nil
}
示例#3
0
func (d *Driver) Remove() error {
	log.WithField("MachineId", d.MachineId).Debug("deleting instance...")
	log.Info("Deleting OpenStack instance...")
	if err := d.initCompute(); err != nil {
		return err
	}
	if err := d.client.DeleteInstance(d); err != nil {
		return err
	}
	log.WithField("Name", d.KeyPairName).Debug("deleting key pair...")
	if err := d.client.DeleteKeyPair(d, d.KeyPairName); err != nil {
		return err
	}
	return nil
}
示例#4
0
func (d *Driver) waitForInstanceActive() error {
	log.WithField("MachineId", d.MachineId).Debug("Waiting for the OpenStack instance to be ACTIVE...")
	if err := d.client.WaitForInstanceStatus(d, "ACTIVE"); err != nil {
		return err
	}
	return nil
}
示例#5
0
func (d *Driver) GetState() (state.State, error) {
	log.WithField("MachineId", d.MachineId).Debug("Get status for OpenStack instance...")
	if err := d.initCompute(); err != nil {
		return state.None, err
	}

	s, err := d.client.GetInstanceState(d)
	if err != nil {
		return state.None, err
	}

	log.WithFields(log.Fields{
		"MachineId": d.MachineId,
		"State":     s,
	}).Debug("State for OpenStack instance")

	switch s {
	case "ACTIVE":
		return state.Running, nil
	case "PAUSED":
		return state.Paused, nil
	case "SUSPENDED":
		return state.Saved, nil
	case "SHUTOFF":
		return state.Stopped, nil
	case "BUILDING":
		return state.Starting, nil
	case "ERROR":
		return state.Error, nil
	}
	return state.None, nil
}
示例#6
0
func (d *Driver) GetIP() (string, error) {
	if d.IPAddress != "" {
		return d.IPAddress, nil
	}

	log.WithField("MachineId", d.MachineId).Debug("Looking for the IP address...")

	if err := d.initCompute(); err != nil {
		return "", err
	}

	addressType := Fixed
	if d.FloatingIpPool != "" {
		addressType = Floating
	}

	// Looking for the IP address in a retry loop to deal with OpenStack latency
	for retryCount := 0; retryCount < 200; retryCount++ {
		addresses, err := d.client.GetInstanceIpAddresses(d)
		if err != nil {
			return "", err
		}
		for _, a := range addresses {
			if a.AddressType == addressType && a.Version == d.IpVersion {
				return a.Address, nil
			}
		}
		time.Sleep(2 * time.Second)
	}
	return "", fmt.Errorf("No IP found for the machine")
}
// GetState return instance status
func (d *Driver) GetState() (state.State, error) {
	log.WithField("MachineId", d.InstanceID).Debug("Get status for OVH instance...")

	client := d.getClient()

	instance, err := client.GetInstance(d.ProjectID, d.InstanceID)
	if err != nil {
		return state.None, err
	}

	log.WithFields(log.Fields{
		"MachineId": d.InstanceID,
		"State":     instance.Status,
	}).Debug("State for OVH instance")

	switch instance.Status {
	case "ACTIVE":
		return state.Running, nil
	case "PAUSED":
		return state.Paused, nil
	case "SUSPENDED":
		return state.Saved, nil
	case "SHUTOFF":
		return state.Stopped, nil
	case "BUILDING":
		return state.Starting, nil
	case "ERROR":
		return state.Error, nil
	}

	return state.None, nil
}
示例#8
0
func (d *Driver) Restart() error {
	log.WithField("MachineId", d.MachineId).Info("Restarting OpenStack instance...")
	if err := d.initCompute(); err != nil {
		return err
	}
	if err := d.client.RestartInstance(d); err != nil {
		return err
	}
	return nil
}
// Restart this docker-machine
func (d *Driver) Restart() error {
	log.WithField("MachineId", d.InstanceID).Info("Restarting OVH instance...")

	client := d.getClient()

	err := client.RebootInstance(d.ProjectID, d.InstanceID, false)
	if err != nil {
		return err
	}
	return nil
}
示例#10
0
// Create a new docker machine instance on OVH Cloud
func (d *Driver) Create() error {
	client := d.getClient()

	// Ensure ssh key
	err := d.ensureSSHKey()
	if err != nil {
		return err
	}

	// Create instance
	log.Debug("Creating OVH instance...")
	instance, err := client.CreateInstance(
		d.ProjectID,
		d.MachineName,
		d.KeyPairID,
		d.FlavorID,
		d.ImageID,
		d.RegionName,
		false,
	)
	if err != nil {
		return err
	}
	d.InstanceID = instance.Id

	// Wait until instance is ACTIVE
	log.WithField("MachineId", d.InstanceID).Debug("Waiting for OVH instance...")
	instance, err = d.waitForInstanceStatus("ACTIVE")
	if err != nil {
		return err
	}

	// Save Ip address
	d.IPAddress = ""
	for _, ip := range instance.IpAddresses {
		if ip.Type == "public" {
			d.IPAddress = ip.Ip
			break
		}
	}

	if d.IPAddress == "" {
		return fmt.Errorf("No IP found for instance %s", instance.Id)
	}

	log.WithFields(log.Fields{
		"IP":        d.IPAddress,
		"MachineId": d.InstanceID,
	}).Debug("IP address found")

	// All done !
	return nil
}
示例#11
0
// ensureSSHKey makes sure an SSH key for the machine exists with requested name
func (d *Driver) ensureSSHKey() error {
	client := d.getClient()

	// Attempt to get an existing key
	log.WithField("Name", d.KeyPairName).Debug("Checking Key Pair...")
	sshKey, _ := client.GetSshkeyByName(d.ProjectID, d.RegionName, d.KeyPairName)
	if sshKey != nil {
		d.KeyPairID = sshKey.Id
		log.Debug("Found key id ", d.KeyPairID)
		return nil
	}

	// Generate key and parent dir if needed
	log.WithField("Name", d.KeyPairName).Debug("Creating Key Pair...")
	keyfile := d.GetSSHKeyPath()
	keypath := filepath.Dir(keyfile)
	err := os.MkdirAll(keypath, 0700)
	if err != nil {
		return err
	}

	err = ssh.GenerateSSHKey(d.GetSSHKeyPath())
	if err != nil {
		return err
	}
	publicKey, err := ioutil.ReadFile(d.publicSSHKeyPath())
	if err != nil {
		return err
	}

	// Upload key
	sshKey, err = client.CreateSshkey(d.ProjectID, d.KeyPairName, string(publicKey))
	if err != nil {
		return err
	}
	d.KeyPairID = sshKey.Id

	log.Debug("Created key id ", d.KeyPairID)
	return nil
}
示例#12
0
func (d *Driver) createSSHKey() error {
	log.WithField("Name", d.KeyPairName).Debug("Creating Key Pair...")
	if err := ssh.GenerateSSHKey(d.GetSSHKeyPath()); err != nil {
		return err
	}
	publicKey, err := ioutil.ReadFile(d.publicSSHKeyPath())
	if err != nil {
		return err
	}

	if err := d.initCompute(); err != nil {
		return err
	}
	if err := d.client.CreateKeyPair(d, d.KeyPairName, string(publicKey)); err != nil {
		return err
	}
	return nil
}
示例#13
0
// waitForInstanceStatus waits until instance reaches status. Copied from openstack Driver
func (d *Driver) waitForInstanceStatus(status string) (instance *Instance, err error) {
	return instance, mcnutils.WaitForSpecificOrError(func() (bool, error) {
		instance, err = d.client.GetInstance(d.ProjectID, d.InstanceID)
		if err != nil {
			return true, err
		}
		log.WithField("MachineId", d.InstanceID).Debugf("Machine state: %s", instance.Status)

		if instance.Status == "ERROR" {
			return true, fmt.Errorf("Instance creation failed. Instance is in ERROR state")
		}

		if instance.Status == status {
			return true, nil
		}

		return false, nil
	}, (statusTimeout / 4), 4*time.Second)
}