func (p linux) CreateUser(username, password, basePath string) error { err := p.fs.MkdirAll(basePath, userBaseDirPermissions) if err != nil { return bosherr.WrapError(err, "Making user base path") } args := []string{"-m", "-b", basePath, "-s", "/bin/bash"} if password != "" { args = append(args, "-p", password) } args = append(args, username) _, _, _, err = p.cmdRunner.RunCommand("useradd", args...) if err != nil { return bosherr.WrapError(err, "Shelling out to useradd") } userHomeDir, err := p.fs.HomeDir(username) if err != nil { return bosherr.WrapErrorf(err, "Unable to retrieve home directory for user %s", username) } _, _, _, err = p.cmdRunner.RunCommand("chmod", "700", userHomeDir) if err != nil { return bosherr.WrapError(err, "Shelling out to chmod") } return nil }
func (vm SoftLayerVM) writeOpenIscsiConfBasedOnShellScript(virtualGuest datatypes.SoftLayer_Virtual_Guest, volume datatypes.SoftLayer_Network_Storage, credential AllowedHostCredential) (bool, error) { buffer := bytes.NewBuffer([]byte{}) t := template.Must(template.New("open_iscsid_conf").Parse(etcIscsidConfTemplate)) if len(credential.Password) == 0 { err := t.Execute(buffer, volume) if err != nil { return false, bosherr.WrapError(err, "Generating config from template") } } else { err := t.Execute(buffer, credential) if err != nil { return false, bosherr.WrapError(err, "Generating config from template") } } file, err := ioutil.TempFile(os.TempDir(), "iscsid_conf_") if err != nil { return false, bosherr.WrapError(err, "Generating config from template") } defer os.Remove(file.Name()) _, err = file.WriteString(buffer.String()) if err != nil { return false, bosherr.WrapError(err, "Generating config from template") } if err = vm.uploadFile(virtualGuest, file.Name(), "/etc/iscsi/iscsid.conf"); err != nil { return false, bosherr.WrapError(err, "Writing to /etc/iscsi/iscsid.conf") } return true, nil }
func (y DeploymentManifestParser) GetDeploymentManifest(deploymentManifestPath string, releaseSetManifest birelsetmanifest.Manifest, stage biui.Stage) (bideplmanifest.Manifest, error) { var deploymentManifest bideplmanifest.Manifest err := stage.Perform("Validating deployment manifest", func() error { var err error deploymentManifest, err = y.DeploymentParser.Parse(deploymentManifestPath) if err != nil { return bosherr.WrapErrorf(err, "Parsing deployment manifest '%s'", deploymentManifestPath) } err = y.DeploymentValidator.Validate(deploymentManifest, releaseSetManifest) if err != nil { return bosherr.WrapError(err, "Validating deployment manifest") } err = y.DeploymentValidator.ValidateReleaseJobs(deploymentManifest, y.ReleaseManager) if err != nil { return bosherr.WrapError(err, "Validating deployment jobs refer to jobs in release") } return nil }) if err != nil { return bideplmanifest.Manifest{}, err } return deploymentManifest, nil }
func (net UbuntuNetManager) writeNetworkInterfaces(dhcpConfigs DHCPInterfaceConfigurations, staticConfigs StaticInterfaceConfigurations, dnsServers []string) (bool, error) { sort.Stable(dhcpConfigs) sort.Stable(staticConfigs) networkInterfaceValues := networkInterfaceConfig{ DHCPConfigs: dhcpConfigs, StaticConfigs: staticConfigs, HasDNSNameServers: true, DNSServers: dnsServers, } buffer := bytes.NewBuffer([]byte{}) t := template.Must(template.New("network-interfaces").Parse(networkInterfacesTemplate)) err := t.Execute(buffer, networkInterfaceValues) if err != nil { return false, bosherr.WrapError(err, "Generating config from template") } changed, err := net.fs.ConvergeFileContents("/etc/network/interfaces", buffer.Bytes()) if err != nil { return changed, bosherr.WrapError(err, "Writing to /etc/network/interfaces") } return changed, nil }
func (vm SoftLayerVM) ReloadOS(stemcell bslcstem.Stemcell) error { reload_OS_Config := sldatatypes.Image_Template_Config{ ImageTemplateId: strconv.Itoa(stemcell.ID()), } virtualGuestService, err := vm.softLayerClient.GetSoftLayer_Virtual_Guest_Service() if err != nil { return bosherr.WrapError(err, "Creating VirtualGuestService from SoftLayer client") } err = bslcommon.WaitForVirtualGuestToHaveNoRunningTransactions(vm.softLayerClient, vm.ID()) if err != nil { return bosherr.WrapError(err, fmt.Sprintf("Waiting for VirtualGuest %d to have no pending transactions before os reload", vm.ID())) } vm.logger.Info(SOFTLAYER_VM_OS_RELOAD_TAG, fmt.Sprintf("No transaction is running on this VM %d", vm.ID())) err = virtualGuestService.ReloadOperatingSystem(vm.ID(), reload_OS_Config) if err != nil { return bosherr.WrapError(err, "Failed to reload OS on the specified VirtualGuest from SoftLayer client") } err = vm.postCheckActiveTransactionsForOSReload(vm.softLayerClient) if err != nil { return err } return nil }
func (c httpClient) PutCustomized(endpoint string, payload []byte, f func(*http.Request)) (*http.Response, error) { putPayload := strings.NewReader(string(payload)) redactedEndpoint := endpoint if !c.opts.NoRedactUrlQuery { redactedEndpoint = scrubEndpointQuery(endpoint) } c.logger.Debug(c.logTag, "Sending PUT request to endpoint '%s'", redactedEndpoint) request, err := http.NewRequest("PUT", endpoint, putPayload) if err != nil { return nil, bosherr.WrapError(err, "Creating PUT request") } if f != nil { f(request) } response, err := c.client.Do(request) if err != nil { return nil, bosherr.WrapError(scrubErrorOutput(err), "Performing PUT request") } return response, nil }
func (c Config) validate() error { if c.AssetsDir == "" { return bosherr.Error("Must provide non-empty assets_dir") } if c.ReposDir == "" { return bosherr.Error("Must provide non-empty repos_dir") } err := c.EventLog.Validate() if err != nil { return bosherr.WrapError(err, "Validating event_log configuration") } if c.Blobstore.Type != bpprov.BlobstoreConfigTypeLocal { return bosherr.Error("Blobstore type must be local") } err = c.Blobstore.Validate() if err != nil { return bosherr.WrapError(err, "Validating blobstore configuration") } return nil }
func (p linux) SetupHostname(hostname string) error { if !p.state.Linux.HostsConfigured { _, _, _, err := p.cmdRunner.RunCommand("hostname", hostname) if err != nil { return bosherr.WrapError(err, "Setting hostname") } err = p.fs.WriteFileString("/etc/hostname", hostname) if err != nil { return bosherr.WrapError(err, "Writing to /etc/hostname") } buffer := bytes.NewBuffer([]byte{}) t := template.Must(template.New("etc-hosts").Parse(etcHostsTemplate)) err = t.Execute(buffer, hostname) if err != nil { return bosherr.WrapError(err, "Generating config from template") } err = p.fs.WriteFile("/etc/hosts", buffer.Bytes()) if err != nil { return bosherr.WrapError(err, "Writing to /etc/hosts") } p.state.Linux.HostsConfigured = true err = p.state.SaveState() if err != nil { return bosherr.WrapError(err, "Setting up hostname") } } return nil }
func (c SoftLayerCreator) Create(size int, cloudProps DiskCloudProperties, virtualGuestId int) (Disk, error) { c.logger.Debug(SOFTLAYER_DISK_CREATOR_LOG_TAG, "Creating disk of size '%d'", size) vmService, err := c.softLayerClient.GetSoftLayer_Virtual_Guest_Service() if err != nil { return SoftLayerDisk{}, bosherr.WrapError(err, "Create SoftLayer Virtual Guest Service error.") } vm, err := vmService.GetObject(virtualGuestId) if err != nil || vm.Id == 0 { return SoftLayerDisk{}, bosherr.WrapError(err, fmt.Sprintf("Cannot retrieve vitual guest with id: %d.", virtualGuestId)) } storageService, err := c.softLayerClient.GetSoftLayer_Network_Storage_Service() if err != nil { return SoftLayerDisk{}, bosherr.WrapError(err, "Create SoftLayer Network Storage Service error.") } disk, err := storageService.CreateIscsiVolume(c.getSoftLayerDiskSize(size), strconv.Itoa(vm.Datacenter.Id)) if err != nil { return SoftLayerDisk{}, bosherr.WrapError(err, "Create SoftLayer iSCSI disk error.") } return NewSoftLayerDisk(disk.Id, c.softLayerClient, c.logger), nil }
func (ms httpMetadataService) getUserData() (UserDataContentsType, error) { var userData UserDataContentsType err := ms.ensureMinimalNetworkSetup() if err != nil { return userData, err } userDataURL := fmt.Sprintf("%s/latest/user-data", ms.metadataHost) userDataResp, err := http.Get(userDataURL) if err != nil { return userData, bosherr.WrapError(err, "Getting user data from url") } defer func() { if err := userDataResp.Body.Close(); err != nil { ms.logger.Warn(ms.logTag, "Failed to close response body when getting user data: %s", err.Error()) } }() userDataBytes, err := ioutil.ReadAll(userDataResp.Body) if err != nil { return userData, bosherr.WrapError(err, "Reading user data response body") } err = json.Unmarshal(userDataBytes, &userData) if err != nil { return userData, bosherr.WrapError(err, "Unmarshalling user data") } return userData, nil }
func (ms httpMetadataService) GetInstanceID() (string, error) { err := ms.ensureMinimalNetworkSetup() if err != nil { return "", err } url := fmt.Sprintf("%s/latest/meta-data/instance-id", ms.metadataHost) resp, err := http.Get(url) if err != nil { return "", bosherr.WrapError(err, "Getting instance id from url") } defer func() { if err := resp.Body.Close(); err != nil { ms.logger.Warn(ms.logTag, "Failed to close response body when getting instance id: %s", err.Error()) } }() bytes, err := ioutil.ReadAll(resp.Body) if err != nil { return "", bosherr.WrapError(err, "Reading instance id response body") } return string(bytes), nil }
func (w *windowsJobSupervisor) Start() error { // Set the starttype of the service running the Agent to 'manual'. // This will prevent the agent from automatically starting if the // machine is rebooted. // // Do this here, as we know the agent has successfully connected // with the director and is healthy. w.cmdRunner.RunCommand("-Command", disableAgentAutoStart) _, _, _, err := w.cmdRunner.RunCommand("-Command", manualStartJobScript) if err != nil { return bosherr.WrapError(err, "Starting windows job process") } _, _, _, err = w.cmdRunner.RunCommand("-Command", startJobScript) if err != nil { return bosherr.WrapError(err, "Starting windows job process") } err = w.fs.RemoveAll(w.stoppedFilePath()) if err != nil { return bosherr.WrapError(err, "Removing stopped file") } w.stateSet(stateEnabled) return nil }
func (p linux) SetupHostname(hostname string) (err error) { _, _, _, err = p.cmdRunner.RunCommand("hostname", hostname) if err != nil { err = bosherr.WrapError(err, "Shelling out to hostname") return } err = p.fs.WriteFileString("/etc/hostname", hostname) if err != nil { err = bosherr.WrapError(err, "Writing /etc/hostname") return } buffer := bytes.NewBuffer([]byte{}) t := template.Must(template.New("etc-hosts").Parse(etcHostsTemplate)) err = t.Execute(buffer, hostname) if err != nil { err = bosherr.WrapError(err, "Generating config from template") return } err = p.fs.WriteFile("/etc/hosts", buffer.Bytes()) if err != nil { err = bosherr.WrapError(err, "Writing to /etc/hosts") } return }
func (p linux) SetupTmpDir() error { systemTmpDir := "/tmp" boshTmpDir := p.dirProvider.TmpDir() boshRootTmpPath := path.Join(p.dirProvider.DataDir(), "root_tmp") err := p.fs.MkdirAll(boshTmpDir, tmpDirPermissions) if err != nil { return bosherr.WrapError(err, "Creating temp dir") } err = os.Setenv("TMPDIR", boshTmpDir) if err != nil { return bosherr.WrapError(err, "Setting TMPDIR") } err = p.changeTmpDirPermissions(systemTmpDir) if err != nil { return err } // /var/tmp is used for preserving temporary files between system reboots varTmpDir := "/var/tmp" err = p.changeTmpDirPermissions(varTmpDir) if err != nil { return err } if p.options.UseDefaultTmpDir { return nil } err = p.fs.RemoveAll(boshRootTmpPath) if err != nil { return bosherr.WrapError(err, fmt.Sprintf("cleaning up %s", boshRootTmpPath)) } _, _, _, err = p.cmdRunner.RunCommand("mkdir", "-p", boshRootTmpPath) if err != nil { return bosherr.WrapError(err, "Creating root tmp dir") } // change permissions err = p.changeTmpDirPermissions(boshRootTmpPath) if err != nil { return bosherr.WrapError(err, "Chmoding root tmp dir") } err = p.bindMountDir(boshRootTmpPath, systemTmpDir) if err != nil { return err } err = p.bindMountDir(boshRootTmpPath, varTmpDir) if err != nil { return err } return nil }
func (p VCAPUserProvisioner) Provision() error { stage := p.eventLog.BeginStage("Setting up vcap user", 3) task := stage.BeginTask("Adding vcap user") err := task.End(p.setUpVcapUser()) if err != nil { return bosherr.WrapError(err, "Setting up vcap user") } task = stage.BeginTask("Configuring locales") err = task.End(p.configureLocales()) if err != nil { return bosherr.WrapError(err, "Configuring locales") } task = stage.BeginTask("Harden permissions") err = task.End(p.hardenPermissinons()) if err != nil { return bosherr.WrapError(err, "Harden permissions") } return nil }
func (net centosNetManager) writeNetworkInterfaces(dhcpInterfaceConfigurations []DHCPInterfaceConfiguration, staticInterfaceConfigurations []StaticInterfaceConfiguration, dnsServers []string) (bool, error) { anyInterfaceChanged := false staticConfig := centosStaticIfcfg{} staticConfig.DNSServers = newDNSConfigs(dnsServers) staticTemplate := template.Must(template.New("ifcfg").Parse(centosStaticIfcfgTemplate)) for i := range staticInterfaceConfigurations { staticConfig.StaticInterfaceConfiguration = &staticInterfaceConfigurations[i] changed, err := net.writeIfcfgFile(staticConfig.StaticInterfaceConfiguration.Name, staticTemplate, staticConfig) if err != nil { return false, bosherr.WrapError(err, "Writing static config") } anyInterfaceChanged = anyInterfaceChanged || changed } dhcpTemplate := template.Must(template.New("ifcfg").Parse(centosDHCPIfcfgTemplate)) for i := range dhcpInterfaceConfigurations { config := &dhcpInterfaceConfigurations[i] changed, err := net.writeIfcfgFile(config.Name, dhcpTemplate, config) if err != nil { return false, bosherr.WrapError(err, "Writing dhcp config") } anyInterfaceChanged = anyInterfaceChanged || changed } return anyInterfaceChanged, nil }
func (a ApplyAction) Run(desiredSpec boshas.V1ApplySpec) (string, error) { settings := a.settingsService.GetSettings() resolvedDesiredSpec, err := a.specService.PopulateDHCPNetworks(desiredSpec, settings) if err != nil { return "", bosherr.WrapError(err, "Resolving dynamic networks") } if desiredSpec.ConfigurationHash != "" { currentSpec, err := a.specService.Get() if err != nil { return "", bosherr.WrapError(err, "Getting current spec") } err = a.applier.Apply(currentSpec, resolvedDesiredSpec) if err != nil { return "", bosherr.WrapError(err, "Applying") } } err = a.specService.Set(resolvedDesiredSpec) if err != nil { return "", bosherr.WrapError(err, "Persisting apply spec") } err = a.writeInstanceData(resolvedDesiredSpec) if err != nil { return "", err } return "applied", nil }
func (d *disk) Delete() error { deleteErr := d.cloud.DeleteDisk(d.cid) if deleteErr != nil { // allow DiskNotFoundError for idempotency cloudErr, ok := deleteErr.(bicloud.Error) if !ok || cloudErr.Type() != bicloud.DiskNotFoundError { return bosherr.WrapError(deleteErr, "Deleting disk in the cloud") } } diskRecord, found, err := d.repo.Find(d.cid) if err != nil { return bosherr.WrapErrorf(err, "Finding disk record (cid=%s)", d.cid) } if !found { return nil } err = d.repo.Delete(diskRecord) if err != nil { return bosherr.WrapError(err, "Deleting disk record") } // returns bicloud.Error only if it is a DiskNotFoundError return deleteErr }
func NewConfigFromPath(path string, fs boshsys.FileSystem) (Config, error) { var config Config bytes, err := fs.ReadFile(path) if err != nil { return config, bosherr.WrapErrorf(err, "Reading config %s", path) } config = DefaultConfig err = json.Unmarshal(bytes, &config) if err != nil { return config, bosherr.WrapError(err, "Unmarshalling config") } if config.VMProvisioner.AgentProvisioner.Configuration == nil { config.VMProvisioner.AgentProvisioner.Configuration = DefaultAgentConfiguration } err = config.validate() if err != nil { return config, bosherr.WrapError(err, "Validating config") } return config, nil }
func (p linux) findRootDevicePathAndNumber() (string, int, error) { mounts, err := p.diskManager.GetMountsSearcher().SearchMounts() if err != nil { return "", 0, bosherr.WrapError(err, "Searching mounts") } for _, mount := range mounts { if mount.MountPoint == "/" && strings.HasPrefix(mount.PartitionPath, "/dev/") { p.logger.Debug(logTag, "Found root partition: `%s'", mount.PartitionPath) stdout, _, _, err := p.cmdRunner.RunCommand("readlink", "-f", mount.PartitionPath) if err != nil { return "", 0, bosherr.WrapError(err, "Shelling out to readlink") } rootPartition := strings.Trim(stdout, "\n") p.logger.Debug(logTag, "Symlink is: `%s'", rootPartition) validRootPartition := regexp.MustCompile(`^/dev/[a-z]+\d$`) if !validRootPartition.MatchString(rootPartition) { return "", 0, bosherr.Error("Root partition has an invalid name" + rootPartition) } devNum, err := strconv.Atoi(rootPartition[len(rootPartition)-1:]) if err != nil { return "", 0, bosherr.WrapError(err, "Parsing device number failed") } devPath := rootPartition[:len(rootPartition)-1] return devPath, devNum, nil } } return "", 0, bosherr.Error("Getting root partition device") }
func (fs *osFileSystem) Chown(path, username string) error { fs.logger.Debug(fs.logTag, "Chown %s to user %s", path, username) uid, err := fs.runCommand(fmt.Sprintf("id -u %s", username)) if err != nil { return bosherr.WrapErrorf(err, "Getting user id for '%s'", username) } uidAsInt, err := strconv.Atoi(uid) if err != nil { return bosherr.WrapError(err, "Converting UID to integer") } gid, err := fs.runCommand(fmt.Sprintf("id -g %s", username)) if err != nil { return bosherr.WrapErrorf(err, "Getting group id for '%s'", username) } gidAsInt, err := strconv.Atoi(gid) if err != nil { return bosherr.WrapError(err, "Converting GID to integer") } err = os.Chown(path, uidAsInt, gidAsInt) if err != nil { return bosherr.WrapError(err, "Doing Chown") } return nil }
func (p linux) partitionEphemeralDisk(realPath string) (string, string, error) { p.logger.Info(logTag, "Creating swap & ephemeral partitions on ephemeral disk...") p.logger.Debug(logTag, "Getting device size of `%s'", realPath) diskSizeInBytes, err := p.diskManager.GetPartitioner().GetDeviceSizeInBytes(realPath) if err != nil { return "", "", bosherr.WrapError(err, "Getting device size") } p.logger.Debug(logTag, "Calculating ephemeral disk partition sizes of `%s' with total disk size %dB", realPath, diskSizeInBytes) swapSizeInBytes, linuxSizeInBytes, err := p.calculateEphemeralDiskPartitionSizes(diskSizeInBytes) if err != nil { return "", "", bosherr.WrapError(err, "Calculating partition sizes") } partitions := []boshdisk.Partition{ {SizeInBytes: swapSizeInBytes, Type: boshdisk.PartitionTypeSwap}, {SizeInBytes: linuxSizeInBytes, Type: boshdisk.PartitionTypeLinux}, } p.logger.Info(logTag, "Partitioning ephemeral disk `%s' with %s", realPath, partitions) err = p.diskManager.GetPartitioner().Partition(realPath, partitions) if err != nil { return "", "", bosherr.WrapErrorf(err, "Partitioning ephemeral disk `%s'", realPath) } swapPartitionPath := realPath + "1" dataPartitionPath := realPath + "2" return swapPartitionPath, dataPartitionPath, nil }
func (net UbuntuNetManager) detectMacAddresses() (map[string]string, error) { addresses := map[string]string{} filePaths, err := net.fs.Glob("/sys/class/net/*") if err != nil { return addresses, bosherr.WrapError(err, "Getting file list from /sys/class/net") } var macAddress string for _, filePath := range filePaths { isPhysicalDevice := net.fs.FileExists(path.Join(filePath, "device")) if isPhysicalDevice { macAddress, err = net.fs.ReadFileString(path.Join(filePath, "address")) if err != nil { return addresses, bosherr.WrapError(err, "Reading mac address from file") } macAddress = strings.Trim(macAddress, "\n") interfaceName := path.Base(filePath) addresses[macAddress] = interfaceName } } return addresses, nil }
func (p linux) SaveDNSRecords(dnsRecords boshsettings.DNSRecords, hostname string) error { dnsRecordsContents, err := p.generateDefaultEtcHosts(hostname) if err != nil { return bosherr.WrapError(err, "Generating default /etc/hosts") } for _, dnsRecord := range dnsRecords.Records { dnsRecordsContents.WriteString(fmt.Sprintf("%s %s\n", dnsRecord[0], dnsRecord[1])) } uuid, err := p.uuidGenerator.Generate() if err != nil { return bosherr.WrapError(err, "Generating UUID") } etcHostsUUIDFileName := fmt.Sprintf("/etc/hosts-%s", uuid) err = p.fs.WriteFile(etcHostsUUIDFileName, dnsRecordsContents.Bytes()) if err != nil { return bosherr.WrapError(err, fmt.Sprintf("Writing to %s", etcHostsUUIDFileName)) } err = p.fs.Rename(etcHostsUUIDFileName, "/etc/hosts") if err != nil { return bosherr.WrapError(err, fmt.Sprintf("Renaming %s to /etc/hosts", etcHostsUUIDFileName)) } return nil }
func (vm SoftLayerVM) getAllowedHostCredential(virtualGuest datatypes.SoftLayer_Virtual_Guest) (AllowedHostCredential, error) { virtualGuestService, err := vm.softLayerClient.GetSoftLayer_Virtual_Guest_Service() if err != nil { return AllowedHostCredential{}, bosherr.WrapError(err, "Cannot get softlayer virtual guest service.") } allowedHost, err := virtualGuestService.GetAllowedHost(virtualGuest.Id) if err != nil { return AllowedHostCredential{}, bosherr.WrapErrorf(err, "Cannot get allowed host with instance id: %d", virtualGuest.Id) } if allowedHost.Id == 0 { return AllowedHostCredential{}, bosherr.Errorf("Cannot get allowed host with instance id: %d", virtualGuest.Id) } allowedHostService, err := vm.softLayerClient.GetSoftLayer_Network_Storage_Allowed_Host_Service() if err != nil { return AllowedHostCredential{}, bosherr.WrapError(err, "Cannot get network storage allowed host service.") } credential, err := allowedHostService.GetCredential(allowedHost.Id) if err != nil { return AllowedHostCredential{}, bosherr.WrapErrorf(err, "Cannot get credential with allowed host id: %d", allowedHost.Id) } return AllowedHostCredential{ Iqn: allowedHost.Name, Username: credential.Username, Password: credential.Password, }, nil }
func (p linux) SetupHostname(hostname string) error { if !p.state.Linux.HostsConfigured { _, _, _, err := p.cmdRunner.RunCommand("hostname", hostname) if err != nil { return bosherr.WrapError(err, "Setting hostname") } err = p.fs.WriteFileString("/etc/hostname", hostname) if err != nil { return bosherr.WrapError(err, "Writing to /etc/hostname") } buffer, err := p.generateDefaultEtcHosts(hostname) if err != nil { return err } err = p.fs.WriteFile("/etc/hosts", buffer.Bytes()) if err != nil { return bosherr.WrapError(err, "Writing to /etc/hosts") } p.state.Linux.HostsConfigured = true err = p.state.SaveState() if err != nil { return bosherr.WrapError(err, "Setting up hostname") } } return nil }
func (vm SoftLayerVM) postCheckActiveTransactionsForOSReload(softLayerClient sl.Client) error { virtualGuestService, err := softLayerClient.GetSoftLayer_Virtual_Guest_Service() if err != nil { return bosherr.WrapError(err, "Creating VirtualGuestService from SoftLayer client") } totalTime := time.Duration(0) for totalTime < bslcommon.TIMEOUT { activeTransactions, err := virtualGuestService.GetActiveTransactions(vm.ID()) if err != nil { return bosherr.WrapError(err, "Getting active transactions from SoftLayer client") } if len(activeTransactions) > 0 { vm.logger.Info(SOFTLAYER_VM_OS_RELOAD_TAG, "OS Reload transaction started") break } totalTime += bslcommon.POLLING_INTERVAL time.Sleep(bslcommon.POLLING_INTERVAL) } if totalTime >= bslcommon.TIMEOUT { return errors.New(fmt.Sprintf("Waiting for OS Reload transaction to start TIME OUT!")) } err = bslcommon.WaitForVirtualGuest(vm.softLayerClient, vm.ID(), "RUNNING") if err != nil { return bosherr.WrapError(err, fmt.Sprintf("PowerOn failed with VirtualGuest id %d", vm.ID())) } vm.logger.Info(SOFTLAYER_VM_OS_RELOAD_TAG, fmt.Sprintf("The virtual guest %d is powered on", vm.ID())) return nil }
func (p linux) MigratePersistentDisk(fromMountPoint, toMountPoint string) (err error) { p.logger.Debug(logTag, "Migrating persistent disk %v to %v", fromMountPoint, toMountPoint) err = p.diskManager.GetMounter().RemountAsReadonly(fromMountPoint) if err != nil { err = bosherr.WrapError(err, "Remounting persistent disk as readonly") return } // Golang does not implement a file copy that would allow us to preserve dates... // So we have to shell out to tar to perform the copy instead of delegating to the FileSystem tarCopy := fmt.Sprintf("(tar -C %s -cf - .) | (tar -C %s -xpf -)", fromMountPoint, toMountPoint) _, _, _, err = p.cmdRunner.RunCommand("sh", "-c", tarCopy) if err != nil { err = bosherr.WrapError(err, "Copying files from old disk to new disk") return } _, err = p.diskManager.GetMounter().Unmount(fromMountPoint) if err != nil { err = bosherr.WrapError(err, "Unmounting old persistent disk") return } err = p.diskManager.GetMounter().Remount(toMountPoint, fromMountPoint) if err != nil { err = bosherr.WrapError(err, "Remounting new disk on original mountpoint") } return }
func (p partedPartitioner) createMapperPartition(devicePath string) error { _, _, _, err := p.cmdRunner.RunCommand("/etc/init.d/open-iscsi", "restart") if err != nil { return bosherr.WrapError(err, "Shelling out to restart open-iscsi") } detectPartitionRetryable := boshretry.NewRetryable(func() (bool, error) { output, _, _, err := p.cmdRunner.RunCommand("dmsetup", "ls") if err != nil { return true, bosherr.WrapError(err, "Shelling out to dmsetup ls") } if strings.Contains(output, "No devices found") { return true, bosherr.Errorf("No devices found") } device := strings.TrimPrefix(devicePath, "/dev/mapper/") lines := strings.Split(strings.Trim(output, "\n"), "\n") for i := 0; i < len(lines); i++ { if match, _ := regexp.MatchString("-part1", lines[i]); match { if strings.Contains(lines[i], device) { p.logger.Info(p.logTag, "Succeeded in detecting partition %s", devicePath+"-part1") return false, nil } } } return true, bosherr.Errorf("Partition %s does not show up", devicePath+"-part1") }) detectPartitionRetryStrategy := NewPartitionStrategy(detectPartitionRetryable, p.timeService, p.logger) return detectPartitionRetryStrategy.Try() }
func marshalResponse(response Response, maxResponseLength int, logger boshlog.Logger) ([]byte, error) { respJSON, err := json.Marshal(response) if err != nil { logger.Error(mbusHandlerLogTag, "Failed to marshal response: %s", err.Error()) return respJSON, bosherr.WrapError(err, "Marshalling JSON response") } if maxResponseLength == UnlimitedResponseLength { return respJSON, nil } if len(respJSON) > maxResponseLength { respJSON, err = json.Marshal(response.Shorten()) if err != nil { logger.Error(mbusHandlerLogTag, "Failed to marshal response: %s", err.Error()) return respJSON, bosherr.WrapError(err, "Marshalling JSON response") } } if len(respJSON) > maxResponseLength { respJSON, err = BuildErrorWithJSON(responseMaxLengthErrMsg, logger) if err != nil { logger.Error(mbusHandlerLogTag, "Failed to build 'max length exceeded' response: %s", err.Error()) return respJSON, bosherr.WrapError(err, "Building error") } } return respJSON, nil }