Example #1
0
func (b *B2dUtils) UpdateISOCache(isoURL string) error {
	// recreate the cache dir if it has been manually deleted
	if _, err := os.Stat(b.imgCachePath); os.IsNotExist(err) {
		log.Infof("Image cache directory does not exist, creating it at %s...", b.imgCachePath)
		if err := os.Mkdir(b.imgCachePath, 0700); err != nil {
			return err
		}
	}

	exists := b.exists()

	if isoURL != "" {
		if exists {
			// Warn that the b2d iso won't be updated if isoURL is set
			log.Warnf("Boot2Docker URL was explicitly set to %q at create time, so Docker Machine cannot upgrade this machine to the latest version.", isoURL)
		}
		// Non-default B2D are not cached
		return nil
	}

	if !exists {
		log.Info("No default Boot2Docker ISO found locally, downloading the latest release...")
		return b.DownloadLatestBoot2Docker("")
	}

	latest := b.isLatest()
	if !latest {
		log.Info("Default Boot2Docker ISO is out-of-date, downloading the latest release...")
		return b.DownloadLatestBoot2Docker("")
	}

	return nil
}
// Start a host
func (d *Driver) Start() error {
	vmstate, err := d.GetState()
	if err != nil {
		return err
	}

	if vmstate == state.Running {
		log.Info("Machine is already running")
		return nil
	}

	if vmstate == state.Starting {
		log.Info("Machine is already starting")
		return nil
	}

	cs := d.getClient()
	p := cs.VirtualMachine.NewStartVirtualMachineParams(d.Id)

	if _, err = cs.VirtualMachine.StartVirtualMachine(p); err != nil {
		return err
	}

	return nil
}
Example #3
0
func (a AzureClient) CreateVirtualNetworkIfNotExists(resourceGroup, name, location string) error {
	f := logutil.Fields{
		"name":     name,
		"location": location}

	log.Info("Querying if virtual network already exists.", f)

	if exists, err := a.virtualNetworkExists(resourceGroup, name); err != nil {
		return err
	} else if exists {
		log.Info("Virtual network already exists.", f)
		return nil
	}

	log.Debug("Creating virtual network.", f)
	_, err := a.virtualNetworksClient().CreateOrUpdate(resourceGroup, name,
		network.VirtualNetwork{
			Location: to.StringPtr(location),
			Properties: &network.VirtualNetworkPropertiesFormat{
				AddressSpace: &network.AddressSpace{
					AddressPrefixes: to.StringSlicePtr(defaultVnetAddressPrefixes),
				},
			},
		}, nil)
	return err
}
Example #4
0
func (detector StandardDetector) DetectProvisioner(d drivers.Driver) (Provisioner, error) {
	log.Info("Waiting for SSH to be available...")
	if err := drivers.WaitForSSH(d); err != nil {
		return nil, err
	}

	log.Info("Detecting the provisioner...")

	osReleaseOut, err := drivers.RunSSHCommandFromDriver(d, "cat /etc/os-release")
	if err != nil {
		return nil, fmt.Errorf("Error getting SSH command: %s", err)
	}

	osReleaseInfo, err := NewOsRelease([]byte(osReleaseOut))
	if err != nil {
		return nil, fmt.Errorf("Error parsing /etc/os-release file: %s", err)
	}

	for _, p := range provisioners {
		provisioner := p.New(d)
		provisioner.SetOsReleaseInfo(osReleaseInfo)

		if provisioner.CompatibleWithHost() {
			log.Debugf("found compatible host: %s", osReleaseInfo.ID)
			return provisioner, nil
		}
	}

	return nil, ErrDetectionFailed
}
Example #5
0
// Create is the wrapper method which covers all of the boilerplate around
// actually creating, provisioning, and persisting an instance in the store.
func (api *Client) Create(h *host.Host) error {
	if err := cert.BootstrapCertificates(h.HostOptions.AuthOptions); err != nil {
		return fmt.Errorf("Error generating certificates: %s", err)
	}

	log.Info("Running pre-create checks...")

	if err := h.Driver.PreCreateCheck(); err != nil {
		return fmt.Errorf("Error with pre-create check: %s", err)
	}

	if err := api.Save(h); err != nil {
		return fmt.Errorf("Error saving host to store before attempting creation: %s", err)
	}

	log.Info("Creating machine...")

	if err := api.performCreate(h); err != nil {
		sendCrashReport(err, api, h)
		return err
	}

	log.Debug("Reticulating splines...")

	return nil
}
Example #6
0
func (h *Host) Upgrade() error {
	machineState, err := h.Driver.GetState()
	if err != nil {
		return err
	}

	if machineState != state.Running {
		return errMachineMustBeRunningForUpgrade
	}

	provisioner, err := provision.DetectProvisioner(h.Driver)
	if err != nil {
		return crashreport.CrashError{
			Cause:      err,
			Command:    "Upgrade",
			Context:    "provision.DetectProvisioner",
			DriverName: h.Driver.DriverName(),
		}
	}

	log.Info("Upgrading docker...")
	if err := provisioner.Package("docker", pkgaction.Upgrade); err != nil {
		return crashreport.CrashError{
			Cause:      err,
			Command:    "Upgrade",
			Context:    "provisioner.Package",
			DriverName: h.Driver.DriverName(),
		}
	}

	log.Info("Restarting docker...")
	return provisioner.Service("docker", serviceaction.Restart)
}
Example #7
0
func (b *B2dUtils) UpdateISOCache(isoURL string) error {
	// recreate the cache dir if it has been manually deleted
	if _, err := os.Stat(b.imgCachePath); os.IsNotExist(err) {
		log.Infof("Image cache directory does not exist, creating it at %s...", b.imgCachePath)
		if err := os.Mkdir(b.imgCachePath, 0700); err != nil {
			return err
		}
	}

	if isoURL != "" {
		// Non-default B2D are not cached
		return nil
	}

	exists := b.exists()
	if !exists {
		log.Info("No default Boot2Docker ISO found locally, downloading the latest release...")
		return b.DownloadLatestBoot2Docker("")
	}

	latest := b.isLatest()
	if !latest {
		log.Info("Default Boot2Docker ISO is out-of-date, downloading the latest release...")
		return b.DownloadLatestBoot2Docker("")
	}

	return nil
}
Example #8
0
// Create is the wrapper method which covers all of the boilerplate around
// actually creating, provisioning, and persisting an instance in the store.
func (api *Client) Create(h *host.Host) error {
	if err := cert.BootstrapCertificates(h.AuthOptions()); err != nil {
		return fmt.Errorf("Error generating certificates: %s", err)
	}

	log.Info("Running pre-create checks...")

	if err := h.Driver.PreCreateCheck(); err != nil {
		return mcnerror.ErrDuringPreCreate{
			Cause: err,
		}
	}

	if err := api.Save(h); err != nil {
		return fmt.Errorf("Error saving host to store before attempting creation: %s", err)
	}

	log.Info("Creating machine...")

	if err := api.performCreate(h); err != nil {
		return fmt.Errorf("Error creating machine: %s", err)
	}

	log.Debug("Reticulating splines...")

	return nil
}
Example #9
0
func (provisioner *Boot2DockerProvisioner) upgradeIso() error {
	// TODO: Ideally, we should not read from mcndirs directory at all.
	// The driver should be able to communicate how and where to place the
	// relevant files.
	b2dutils := mcnutils.NewB2dUtils(mcndirs.GetBaseDir())

	// Check if the driver has specified a custom b2d url
	jsonDriver, err := json.Marshal(provisioner.GetDriver())
	if err != nil {
		return err
	}
	var d struct {
		Boot2DockerURL string
	}
	json.Unmarshal(jsonDriver, &d)

	log.Info("Downloading latest boot2docker iso...")

	// Usually we call this implicitly, but call it here explicitly to get
	// the latest default boot2docker ISO.
	if d.Boot2DockerURL == "" {
		if err := b2dutils.DownloadLatestBoot2Docker(d.Boot2DockerURL); err != nil {
			return err
		}
	}

	log.Info("Stopping machine to do the upgrade...")

	if err := provisioner.Driver.Stop(); err != nil {
		return err
	}

	if err := mcnutils.WaitFor(drivers.MachineInState(provisioner.Driver, state.Stopped)); err != nil {
		return err
	}

	machineName := provisioner.GetDriver().GetMachineName()

	log.Infof("Upgrading machine %q...", machineName)

	// Either download the latest version of the b2d url that was explicitly
	// specified when creating the VM or copy the (updated) default ISO
	if err := b2dutils.CopyIsoToMachineDir(d.Boot2DockerURL, machineName); err != nil {
		return err
	}

	log.Infof("Starting machine back up...")

	if err := provisioner.Driver.Start(); err != nil {
		return err
	}

	return mcnutils.WaitFor(drivers.MachineInState(provisioner.Driver, state.Running))
}
Example #10
0
// Create is the wrapper method which covers all of the boilerplate around
// actually creating, provisioning, and persisting an instance in the store.
func (api *Client) Create(h *host.Host) error {
	if err := cert.BootstrapCertificates(h.HostOptions.AuthOptions); err != nil {
		return fmt.Errorf("Error generating certificates: %s", err)
	}

	log.Info("Running pre-create checks...")

	if err := h.Driver.PreCreateCheck(); err != nil {
		return fmt.Errorf("Error with pre-create check: %s", err)
	}

	if err := api.Save(h); err != nil {
		return fmt.Errorf("Error saving host to store before attempting creation: %s", err)
	}

	log.Info("Creating machine...")

	if err := h.Driver.Create(); err != nil {
		return fmt.Errorf("Error in driver during machine creation: %s", err)
	}

	if err := api.Save(h); err != nil {
		return fmt.Errorf("Error saving host to store after attempting creation: %s", err)
	}

	// TODO: Not really a fan of just checking "none" here.
	if h.Driver.DriverName() != "none" {
		log.Info("Waiting for machine to be running, this may take a few minutes...")
		if err := mcnutils.WaitFor(drivers.MachineInState(h.Driver, state.Running)); err != nil {
			return fmt.Errorf("Error waiting for machine to be running: %s", err)
		}

		log.Info("Machine is running, waiting for SSH to be available...")
		if err := drivers.WaitForSSH(h.Driver); err != nil {
			return fmt.Errorf("Error waiting for SSH: %s", err)
		}

		log.Info("Detecting operating system of created instance...")
		provisioner, err := provision.DetectProvisioner(h.Driver)
		if err != nil {
			return fmt.Errorf("Error detecting OS: %s", err)
		}

		log.Infof("Provisioning with %s...", provisioner.String())
		if err := provisioner.Provision(*h.HostOptions.SwarmOptions, *h.HostOptions.AuthOptions, *h.HostOptions.EngineOptions); err != nil {
			return fmt.Errorf("Error running provisioning: %s", err)
		}
	}

	log.Debug("Reticulating splines...")

	return nil
}
// TestPreApplyDeploymentJobs - setup some information from icsp
//TODO: This test requires a server profile to have been created
func TestPreApplyDeploymentJobs(t *testing.T) {
	var (
		d                     *ICSPTest
		c                     *icsp.ICSPClient
		serialNumber, macAddr string
	)
	if os.Getenv("ICSP_TEST_ACCEPTANCE") == "true" {
		log.Debug("implements acceptance test for ApplyDeploymentJobs")
		d, c = getTestDriverA()
		if c == nil {
			t.Fatalf("Failed to execute getTestDriver() ")
		}
		if os.Getenv("ONEVIEW_TEST_PROVISION") != "true" {
			log.Info("env ONEVIEW_TEST_PROVISION != true")
			log.Info("Skipping FreeBlade testing")
			serialNumber = d.Tc.GetTestData(d.Env, "SerialNumber").(string)
			macAddr = d.Tc.GetTestData(d.Env, "MacAddr").(string)
		} else {
			// serialNumber := d.Tc.GetTestData(d.Env, "FreeBladeSerialNumber").(string)
			serialNumber = d.Tc.GetTestData(d.Env, "FreeICSPSerialNumber").(string)
			macAddr = d.Tc.GetTestData(d.Env, "FreeMacAddr").(string)
		}
		s, err := c.GetServerBySerialNumber(serialNumber)
		assert.NoError(t, err, "GetServerBySerialNumber threw error -> %s, %+v\n", err, s)

		pubinet, err := s.GetInterface(1)
		assert.NoError(t, err, "GetInterface(1) threw error -> %s, %+v\n", err, s)
		assert.Equal(t, macAddr, pubinet.MACAddr, fmt.Sprintf("should get a valid interface -> %+v", pubinet))

		s, err = c.PreApplyDeploymentJobs(s, pubinet) // responsible for configuring the Pulbic IP CustomAttributes
		assert.NoError(t, err, "ApplyDeploymentJobs threw error -> %+v, %+v", err, s)
		s, err = s.ReloadFull(c)
		assert.NoError(t, err, "ReloadFull threw error -> %+v, %+v", err, s)

		// verify that the server attribute was saved by getting the server again and checking the value
		_, testValue2 := s.GetValueItem("public_interface", "server")
		// unmarshal the custom attribute
		var inet *icsp.Interface
		log.Debugf("public_interface value -> %+v", testValue2.Value)
		assert.NotEqual(t, "", testValue2.Value,
			fmt.Sprintf("public_interface for %s Should have a value", serialNumber))

		if testValue2.Value != "" {
			err = json.Unmarshal([]byte(testValue2.Value), &inet)
			assert.NoError(t, err, "Unmarshal Interface threw error -> %s, %+v\n", err, testValue2.Value)

			log.Infof("We got public ip addr -> %s", inet.MACAddr)
			assert.Equal(t, macAddr, inet.MACAddr, "Should return the saved custom attribute for mac address")
		}

	}
}
Example #12
0
// Stop issues a power off for the virtual machine instance.
func (d *Driver) Stop() error {
	if err := d.checkLegacyDriver(true); err != nil {
		return err
	}

	c, err := d.newAzureClient()
	if err != nil {
		return err
	}
	log.Info("NOTICE: Stopping an Azure Virtual Machine is just going to power it off, not deallocate.")
	log.Info("NOTICE: You should remove the machine if you would like to avoid unexpected costs.")
	return c.StopVirtualMachine(d.ResourceGroup, d.naming().VM())
}
Example #13
0
// deleteResourceIfExists is an utility method to determine if a resource exists
// from the error returned from its Get response. If so, deletes it. name is
// used only for logging purposes.
func deleteResourceIfExists(resourceType, name string, getFunc func() error, deleteFunc func() (autorest.Response, error)) error {
	f := logutil.Fields{"name": name}
	log.Debug(fmt.Sprintf("Querying if %s exists.", resourceType), f)
	if exists, err := checkResourceExistsFromError(getFunc()); err != nil {
		return err
	} else if !exists {
		log.Info(fmt.Sprintf("%s does not exist. Skipping.", resourceType), f)
		return nil
	}
	log.Info(fmt.Sprintf("Removing %s resource.", resourceType), f)
	_, err := deleteFunc()
	return err
}
Example #14
0
func (a AzureClient) CreateAvailabilitySetIfNotExists(ctx *DeploymentContext, resourceGroup, name, location string) error {
	f := logutil.Fields{"name": name}
	if ctx.AvailabilitySetID != "" {
		log.Info("Availability Set already exists.", f)
		return nil
	}
	log.Debug("Could not find existing availability set.", f)
	log.Info("Creating availability set...", f)
	as, err := a.availabilitySetsClient().CreateOrUpdate(resourceGroup, name,
		compute.AvailabilitySet{
			Location: to.StringPtr(location),
		})
	ctx.AvailabilitySetID = to.String(as.ID)
	return err
}
func (d *Driver) checkImage() error {
	client, err := d.getClient()
	if err != nil {
		return err
	}
	var image *brightbox.Image
	if d.Image == "" {
		log.Info("No image specified. Looking for default image")
		log.Debugf("Brightbox API Call: List of Images")
		images, err := client.Images()
		if err != nil {
			return err
		}
		image, err = GetDefaultImage(images)
		if err != nil {
			return err
		}
		d.Image = image.Id
	} else {
		log.Debugf("Brightbox API Call: Image Details for %s", d.Image)
		image, err = client.Image(d.Image)
		if err != nil {
			return err
		}
	}
	if image.Arch != "x86_64" {
		return fmt.Errorf("Docker requires a 64 bit image. Image %s not suitable", d.Image)
	}
	if d.SSHUser == "" {
		log.Debug("Setting SSH Username from image details")
		d.SSHUser = image.Username
	}
	log.Debugf("Image %s selected. SSH user is %s", d.Image, d.SSHUser)
	return nil
}
Example #16
0
func (a AzureClient) findStorageAccount(resourceGroup, location, prefix string, storageType storage.SkuName) (*storage.AccountProperties, error) {
	f := logutil.Fields{
		"sku":      storageType,
		"prefix":   prefix,
		"location": location}
	log.Debug("Querying existing storage accounts.", f)
	l, err := a.storageAccountsClient().ListByResourceGroup(resourceGroup)
	if err != nil {
		return nil, err
	}

	if l.Value != nil {
		for _, v := range *l.Value {
			log.Debug("Iterating...", logutil.Fields{
				"name":     to.String(v.Name),
				"sku":      storageType,
				"location": to.String(v.Location),
			})
			if to.String(v.Location) == location && v.Sku.Name == storageType && strings.HasPrefix(to.String(v.Name), prefix) {
				log.Debug("Found eligible storage account.", logutil.Fields{"name": to.String(v.Name)})
				log.Info("Using existing storage account.", logutil.Fields{
					"name": to.String(v.Name),
					"sku":  storageType,
				})
				return v.Properties, nil
			}
		}
	}
	log.Debug("No account matching the pattern is found.", f)
	return nil, err
}
// SubmitDeploymentJobs api call to deployment jobs
func (c *ICSPClient) SubmitDeploymentJobs(dj DeploymentJobs) (jt *JobTask, err error) {
	log.Info("Applying OS Build plan for ICSP")
	var (
		uri  = "/rest/os-deployment-jobs"
		juri ODSUri
	)
	// refresh login
	c.RefreshLogin()
	c.SetAuthHeaderOptions(c.GetAuthHeaderMap())

	jt = jt.NewJobTask(c)
	jt.Reset()
	data, err := c.RestAPICall(rest.POST, uri, dj)
	if err != nil {
		jt.IsDone = true
		log.Errorf("Error submitting new build request: %s", err)
		return jt, err
	}

	log.Debugf("Response submit new os build plan job %s", data)
	if err := json.Unmarshal([]byte(data), &juri); err != nil {
		jt.IsDone = true
		jt.JobURI = juri
		log.Errorf("Error with task un-marshal: %s", err)
		return jt, err
	}
	jt.JobURI = juri

	return jt, err
}
Example #18
0
func (a AzureClient) createStorageAccount(resourceGroup, location string, storageType storage.SkuName) (*storage.AccountProperties, error) {
	name := randomAzureStorageAccountName() // if it's not random enough, then you're unlucky

	f := logutil.Fields{
		"name":     name,
		"location": location,
		"sku":      storageType,
	}

	log.Info("Creating storage account.", f)
	_, err := a.storageAccountsClient().Create(resourceGroup, name,
		storage.AccountCreateParameters{
			Location: to.StringPtr(location),
			Sku:      &storage.Sku{Name: storageType},
		}, nil)
	if err != nil {
		return nil, err
	}

	s, err := a.storageAccountsClient().GetProperties(resourceGroup, name)
	if err != nil {
		return nil, err
	}
	return s.Properties, nil
}
Example #19
0
func (c *GenericClient) CreateInstance(d *Driver) (string, error) {
	serverOpts := servers.CreateOpts{
		Name:             d.MachineName,
		FlavorRef:        d.FlavorId,
		ImageRef:         d.ImageId,
		SecurityGroups:   d.SecurityGroups,
		AvailabilityZone: d.AvailabilityZone,
	}
	if d.NetworkId != "" {
		serverOpts.Networks = []servers.Network{
			{
				UUID: d.NetworkId,
			},
		}
	}

	log.Info("Creating machine...")

	server, err := servers.Create(c.Compute, keypairs.CreateOptsExt{
		serverOpts,
		d.KeyPairName,
	}).Extract()
	if err != nil {
		return "", err
	}
	return server.ID, nil
}
Example #20
0
func (d *Driver) Create() error {
	if err := d.setUserSubscription(); err != nil {
		return err
	}

	log.Info("Creating Azure machine...")
	vmConfig, err := vmClient.CreateAzureVMConfiguration(d.MachineName, d.Size, d.Image, d.Location)
	if err != nil {
		return err
	}

	log.Debug("Generating certificate for Azure...")
	if err := d.generateCertForAzure(); err != nil {
		return err
	}

	log.Debug("Adding Linux provisioning...")
	vmConfig, err = vmClient.AddAzureLinuxProvisioningConfig(vmConfig, d.GetSSHUsername(), d.UserPassword, d.azureCertPath(), d.SSHPort)
	if err != nil {
		return err
	}

	log.Debug("Authorizing ports...")
	if err := d.addDockerEndpoints(vmConfig); err != nil {
		return err
	}

	log.Debug("Creating VM...")
	if err := vmClient.CreateAzureVM(vmConfig, d.MachineName, d.Location); err != nil {
		return err
	}

	return nil
}
Example #21
0
func (c *GenericClient) CreateInstance(d *Driver) (string, error) {
	serverOpts := servers.CreateOpts{
		Name:             d.MachineName,
		FlavorRef:        d.FlavorId,
		ImageRef:         d.ImageId,
		SecurityGroups:   d.SecurityGroups,
		AvailabilityZone: d.AvailabilityZone,
	}
	if len(d.NetworkIds) > 0 {
		networks := make([]servers.Network, len(d.NetworkIds))
		for i, networkId := range d.NetworkIds {
			networks[i] = servers.Network{UUID: networkId}
		}
		serverOpts.Networks = networks
	}

	log.Info("Creating machine...")

	server, err := servers.Create(c.Compute, keypairs.CreateOptsExt{
		serverOpts,
		d.KeyPairName,
	}).Extract()
	if err != nil {
		return "", err
	}
	return server.ID, nil
}
// Remove a host
func (d *Driver) Remove() error {
	cs := d.getClient()
	p := cs.VirtualMachine.NewDestroyVirtualMachineParams(d.Id)
	p.SetExpunge(d.Expunge)

	if err := d.deleteFirewallRules(); err != nil {
		return err
	}

	if err := d.disassociatePublicIP(); err != nil {
		return err
	}

	log.Info("Removing CloudStack instance...")
	if _, err := cs.VirtualMachine.DestroyVirtualMachine(p); err != nil {
		return err
	}
	if d.NetworkType == "Basic" {
		if err := d.deleteSecurityGroup(); err != nil {
			return err
		}
	}

	if err := d.deleteKeyPair(); err != nil {
		return err
	}

	return nil
}
func (d *Driver) Remove() error {
	log.Debug("deleting instance...")
	log.Info("Deleting aliyun instance...")
	c := ecs.NewClient(
		d.AccessKeyID,
		d.AccessKeySecret,
	)
	if cstate, _ := d.GetState(); cstate == state.Running {

		if err := c.StopInstance(d.InstanceID, "true"); err != nil {
			return fmt.Errorf(errorOnStopMachine, err.Error())
		}
		//等待虚拟机停机
		for {
			if cstate, _ = d.GetState(); cstate == state.Stopped {
				break
			}
			time.Sleep(3 * time.Second)
		}
	}
	if err := c.DeleteInstance(d.InstanceID); err != nil {
		return fmt.Errorf(errorOnRemoveMachine, err.Error())
	}

	if d.IsTempSecurityGroup {
		for {
			if cstate, _ := d.GetState(); cstate == state.None {
				d.deleteSecurityGroup()
				break
			}
			time.Sleep(1 * time.Second)
		}
	}
	return nil
}
Example #24
0
// StopVirtualMachine power offs the virtual machine and waits until it reaches
// the goal state (stopped) or times out.
func (a AzureClient) StopVirtualMachine(resourceGroup, name string) error {
	log.Info("Stopping virtual machine.", logutil.Fields{"vm": name})
	if _, err := a.virtualMachinesClient().PowerOff(resourceGroup, name, nil); err != nil {
		return err
	}
	return a.waitVMPowerState(resourceGroup, name, Stopped, waitPowerOffTimeout)
}
Example #25
0
// RestartVirtualMachine restarts the virtual machine and waits until it reaches
// the goal state (stopped) or times out.
func (a AzureClient) RestartVirtualMachine(resourceGroup, name string) error {
	log.Info("Restarting virtual machine.", logutil.Fields{"vm": name})
	if _, err := a.virtualMachinesClient().Restart(resourceGroup, name, nil); err != nil {
		return err
	}
	return a.waitVMPowerState(resourceGroup, name, Running, waitStartTimeout)
}
Example #26
0
// RegisterResourceProviders registers current subscription to the specified
// resource provider namespaces if they are not already registered. Namespaces
// are case-insensitive.
func (a AzureClient) RegisterResourceProviders(namespaces ...string) error {
	l, err := a.providersClient().List(nil)
	if err != nil {
		return err
	}
	if l.Value == nil {
		return errors.New("Resource Providers list is returned as nil.")
	}

	m := make(map[string]bool)
	for _, p := range *l.Value {
		m[strings.ToLower(to.String(p.Namespace))] = to.String(p.RegistrationState) == "Registered"
	}

	for _, ns := range namespaces {
		registered, ok := m[strings.ToLower(ns)]
		if !ok {
			return fmt.Errorf("Unknown resource provider %q", ns)
		}
		if registered {
			log.Debugf("Already registered for %q", ns)
		} else {
			log.Info("Registering subscription to resource provider.", logutil.Fields{
				"ns":   ns,
				"subs": a.subscriptionID,
			})
			if _, err := a.providersClient().Register(ns); err != nil {
				return err
			}
		}
	}
	return nil
}
Example #27
0
func (lbp *LocalBinaryPlugin) execServer() error {
	outScanner, errScanner, err := lbp.Executor.Start()
	if err != nil {
		return err
	}

	// Scan just one line to get the address, then send it to the relevant
	// channel.
	outScanner.Scan()
	addr := outScanner.Text()
	if err := outScanner.Err(); err != nil {
		return fmt.Errorf("Reading plugin address failed: %s", err)
	}

	lbp.addrCh <- strings.TrimSpace(addr)

	stdOutCh, stopStdoutCh := lbp.AttachStream(outScanner)
	stdErrCh, stopStderrCh := lbp.AttachStream(errScanner)

	for {
		select {
		case out := <-stdOutCh:
			log.Info(fmt.Sprintf(pluginOutPrefix, lbp.MachineName), out)
		case err := <-stdErrCh:
			log.Debug(fmt.Sprintf(pluginErrPrefix, lbp.MachineName), err)
		case _ = <-lbp.stopCh:
			stopStdoutCh <- true
			stopStderrCh <- true
			if err := lbp.Executor.Close(); err != nil {
				return fmt.Errorf("Error closing local plugin binary: %s", err)
			}
			return nil
		}
	}
}
Example #28
0
func (a AzureClient) CreatePublicIPAddress(ctx *DeploymentContext, resourceGroup, name, location string, isStatic bool) error {
	log.Info("Creating public IP address.", logutil.Fields{
		"name":   name,
		"static": isStatic})

	var ipType network.IPAllocationMethod
	if isStatic {
		ipType = network.Static
	} else {
		ipType = network.Dynamic
	}

	_, err := a.publicIPAddressClient().CreateOrUpdate(resourceGroup, name,
		network.PublicIPAddress{
			Location: to.StringPtr(location),
			Properties: &network.PublicIPAddressPropertiesFormat{
				PublicIPAllocationMethod: ipType,
			},
		}, nil)
	if err != nil {
		return err
	}
	ip, err := a.publicIPAddressClient().Get(resourceGroup, name, "")
	ctx.PublicIPAddressID = to.String(ip.ID)
	return err
}
Example #29
0
// Submit desired power state and wait
// Most of our concurrency will happen in PowerExecutor
func (pt *PowerTask) PowerExecutor(s PowerState) error {
	currenttime := 0
	pt.State = P_UKNOWN
	pt.ResetTask()
	go pt.SubmitPowerState(s)
	for !pt.TaskIsDone && (currenttime < pt.Timeout) {
		if err := pt.GetCurrentTaskStatus(); err != nil {
			return err
		}
		if pt.URI != "" && T_COMPLETED.Equal(pt.TaskState) {
			pt.TaskIsDone = true
		}
		if pt.URI != "" {
			log.Debugf("Waiting to set power state %s for blade %s, %s", s, pt.Blade.Name)
			log.Infof("Working on power state,%d%%, %s.", pt.ComputedPercentComplete, pt.TaskStatus)
		} else {
			log.Info("Working on power state.")
		}

		// wait time before next check
		time.Sleep(time.Millisecond * (1000 * pt.WaitTime)) // wait 10sec before checking the status again
		currenttime++
	}
	if !(currenttime < pt.Timeout) {
		log.Warnf("Power %s state timed out for %s.", s, pt.Blade.Name)
	}
	log.Infof("Power Task Execution Completed")
	return nil
}
Example #30
0
func (d *Driver) PreCreateCheck() error {
	if d.UserDataFile != "" {
		if d.OSID == 159 {
			return fmt.Errorf("User Data is currently not supported with 'Custom OS' (159)")
		}
		if _, err := os.Stat(d.UserDataFile); os.IsNotExist(err) {
			return fmt.Errorf("Unable to find User Data file at %s", d.UserDataFile)
		}
	}

	log.Info("Validating Vultr VPS parameters...")

	if d.ScriptID != 0 && d.OSID != 159 {
		return fmt.Errorf("Using PXE boot script requires 'Custom OS' (159)")
	}

	if err := d.validateRegion(); err != nil {
		return err
	}

	if err := d.validatePlan(); err != nil {
		return err
	}

	if err := d.validateApiCredentials(); err != nil {
		return err
	}

	return nil
}