Beispiel #1
0
func (d *Driver) Create() error {
	d.KeyPairName = fmt.Sprintf("%s-%s", d.MachineName, mcnutils.GenerateRandomID())

	if err := d.resolveIds(); err != nil {
		return err
	}
	if err := d.createSSHKey(); err != nil {
		return err
	}
	if err := d.createMachine(); err != nil {
		return err
	}
	if err := d.waitForInstanceActive(); err != nil {
		return err
	}
	if d.FloatingIpPool != "" {
		if err := d.assignFloatingIp(); err != nil {
			return err
		}
	}
	if err := d.lookForIpAddress(); err != nil {
		return err
	}
	return nil
}
// PreCreateCheck does the network side validation
func (d *Driver) PreCreateCheck() error {
	client, err := d.getClient()
	if err != nil {
		return err
	}

	// Validate billing period
	log.Debug("Validating billing period")
	if d.BillingPeriod != "monthly" && d.BillingPeriod != "hourly" {
		return fmt.Errorf("Invalid billing period '%s'. Please select one of 'hourly', 'monthly'", d.BillingPeriod)
	}
	log.Debug("Selecting billing period", d.BillingPeriod)

	// Validate project id
	log.Debug("Validating project")
	if d.ProjectName != "" {
		project, err := client.GetProjectByName(d.ProjectName)
		if err != nil {
			return err
		}
		d.ProjectID = project.ID
	} else {
		projects, err := client.GetProjects()
		if err != nil {
			return err
		}

		// If there is only one project, take it
		if len(projects) == 1 {
			d.ProjectID = projects[0]
		} else if len(projects) == 0 {
			return fmt.Errorf("No Cloud project could be found. To create a new one, please visit %s", CustomerInterface)
		} else {
			// Build a list of project names to help choose one
			var projectNames []string
			for _, projectID := range projects {
				project, err := client.GetProject(projectID)
				if err != nil {
					projectNames = append(projectNames, projectID)
				} else {
					projectNames = append(projectNames, project.Name)
				}
			}

			return fmt.Errorf("Multiple Cloud project found (%s), to select one, use '--ovh-project' option", strings.Join(projectNames[:], ", "))
		}
	}
	log.Debug("Found project id ", d.ProjectID)

	// Validate region
	log.Debug("Validating region")
	regions, err := client.GetRegions(d.ProjectID)
	if err != nil {
		return err
	}
	var ok bool
	for _, region := range regions {
		if region == d.RegionName {
			ok = true
			break
		}
	}
	if ok != true {
		return fmt.Errorf("Invalid region %s. For a list of valid ovh regions, please visis %s", d.RegionName, CustomerInterface)
	}

	// Validate flavor
	log.Debug("Validating flavor")
	flavor, err := client.GetFlavorByName(d.ProjectID, d.RegionName, d.FlavorName)
	if err != nil {
		return err
	}
	d.FlavorID = flavor.ID
	log.Debug("Found flavor id ", d.FlavorID)

	// Validate image
	log.Debug("Validating image")
	image, err := client.GetImageByName(d.ProjectID, d.RegionName, d.ImageID)
	if err != nil {
		return err
	}
	d.ImageID = image.ID
	log.Debug("Found image id ", d.ImageID)

	// Use a common key or create a machine specific one
	keyPath := filepath.Join(d.StorePath, "sshkeys", d.KeyPairName)
	if len(d.KeyPairName) != 0 {
		if _, err := os.Stat(keyPath); err == nil {
			d.SSHKeyPath = keyPath
		} else {
			log.Debug("SSH key", keyPath, "does not exist. Assuming the key (", d.KeyPairName, ") is in '~/.ssh/' or in a SSH agent.")
		}
	} else {
		d.KeyPairName = fmt.Sprintf("%s-%s", d.MachineName, mcnutils.GenerateRandomID())
		d.SSHKeyPath = d.ResolveStorePath(d.KeyPairName)
	}

	return nil
}
Beispiel #3
0
func generateVMName() string {
	randomID := mcnutils.TruncateID(mcnutils.GenerateRandomID())
	return fmt.Sprintf("docker-host-%s", randomID)
}
// PreCreateCheck does the network side validation
func (d *Driver) PreCreateCheck() error {
	client := d.getClient()

	// Validate project id
	log.Debug("Validating project")
	if d.ProjectName != "" {
		project, err := client.GetProjectByName(d.ProjectName)
		if err != nil {
			return err
		}
		d.ProjectID = project.Id
	} else {
		projects, err := client.GetProjects()
		if err != nil {
			return err
		}

		// If there is only one project, take it
		if len(projects) == 1 {
			d.ProjectID = projects[0]
		} else if len(projects) == 0 {
			return fmt.Errorf("No Cloud project could be found. To create a new one, please visit %s", CustomerInterface)
		} else {
			return fmt.Errorf("Multiple Cloud project found, to select one, use '--ovh-project' option")
		}
	}
	log.Debug("Found project id ", d.ProjectID)

	// Validate region
	log.Debug("Validating region")
	regions, err := client.GetRegions(d.ProjectID)
	if err != nil {
		return err
	}
	var ok bool
	for _, region := range regions {
		if region == d.RegionName {
			ok = true
			break
		}
	}
	if ok != true {
		return fmt.Errorf("Invalid region %s. For a list of valid ovh regions, please visis %s", d.RegionName, CustomerInterface)
	}

	// Validate flavor
	log.Debug("Validating flavor")
	flavor, err := client.GetFlavorByName(d.ProjectID, d.RegionName, d.FlavorName)
	if err != nil {
		return err
	}
	d.FlavorID = flavor.Id
	log.Debug("Found flavor id ", d.FlavorID)

	// Validate image
	log.Debug("Validating image")
	image, err := client.GetImageByName(d.ProjectID, d.RegionName, ImageName)
	if err != nil {
		return err
	}
	d.ImageID = image.Id
	log.Debug("Found image id ", d.ImageID)

	// Use a common key or create a machine specific one
	if len(d.KeyPairName) != 0 {
		d.SSHKeyPath = filepath.Join(d.StorePath, "sshkeys", d.KeyPairName)
	} else {
		d.KeyPairName = fmt.Sprintf("%s-%s", d.MachineName, mcnutils.GenerateRandomID())
	}

	return nil
}