Esempio n. 1
0
func (driver *Driver) RemoveVolume(volumeID string) error {

	fields := eff(map[string]interface{}{
		"volumeId": volumeID,
	})

	if volumeID == "" {
		return errors.WithFields(fields, "volumeId is required")
	}

	volumes, err := driver.getVolume(volumeID, "")
	if err != nil {
		return errors.WithFieldsE(fields, "error getting volume", err)
	}

	targetVolume := goscaleio.NewVolume(driver.Client)
	targetVolume.Volume = volumes[0]

	err = targetVolume.RemoveVolume("ONLY_ME")
	if err != nil {
		return errors.WithFieldsE(fields, "error removing volume", err)
	}

	log.WithFields(fields).Debug("removed volume")
	return nil
}
Esempio n. 2
0
func (driver *Driver) DetachVolume(runAsync bool, volumeID, instanceID string) error {
	fields := eff(map[string]interface{}{
		"runAsync":   runAsync,
		"volumeId":   volumeID,
		"instanceId": instanceID,
	})

	if volumeID == "" {
		return errors.WithFields(fields, "volumeId is required")
	}
	volume, err := driver.GetVolume(volumeID, "")
	if err != nil {
		return errors.WithFieldsE(fields, "error getting volume", err)
	}

	fields["instanceId"] = volume[0].Attachments[0].InstanceID
	resp := volumeattach.Delete(
		driver.Client, volume[0].Attachments[0].InstanceID, volumeID)
	if resp.Err != nil {
		return errors.WithFieldsE(fields, "error deleting volume", err)
	}

	if !runAsync {
		log.WithFields(fields).Debug("waiting for volume to detach")
		err = driver.waitVolumeDetach(volumeID)
		if err != nil {
			return errors.WithFieldsE(
				fields, "error waiting for volume to detach", err)
		}
	}

	log.WithFields(fields).Debug("volume detached")
	return nil
}
Esempio n. 3
0
func (driver *Driver) AttachVolume(runAsync bool, volumeID, instanceID string) ([]*storage.VolumeAttachment, error) {

	fields := eff(map[string]interface{}{
		"runAsync":   runAsync,
		"volumeId":   volumeID,
		"instanceId": instanceID,
	})

	if volumeID == "" {
		return nil, errors.WithFields(fields, "volumeId is required")
	}

	mapVolumeSdcParam := &types.MapVolumeSdcParam{
		SdcID: driver.Sdc.Sdc.ID,
		AllowMultipleMappings: "false",
		AllSdcs:               "",
	}

	volumes, err := driver.getVolume(volumeID, "")
	if err != nil {
		return nil, errors.WithFieldsE(fields, "error getting volume", err)
	}

	if len(volumes) == 0 {
		return nil, errors.WithFields(fields, "no volumes returned")
	}

	targetVolume := goscaleio.NewVolume(driver.Client)
	targetVolume.Volume = volumes[0]

	err = targetVolume.MapVolumeSdc(mapVolumeSdcParam)
	if err != nil {
		return nil, errors.WithFieldsE(fields, "error mapping volume sdc", err)
	}

	_, err = waitMount(volumes[0].ID)
	if err != nil {
		fields["volumeId"] = volumes[0].ID
		return nil, errors.WithFieldsE(
			fields, "error waiting on volume to mount", err)
	}

	volumeAttachment, err := driver.GetVolumeAttach(volumeID, instanceID)
	if err != nil {
		return nil, errors.WithFieldsE(
			fields, "error getting volume attachments", err)
	}

	log.WithFields(log.Fields{
		"provider":   ProviderName,
		"volumeId":   volumeID,
		"instanceId": instanceID,
	}).Debug("attached volume to instance")
	return volumeAttachment, nil
}
Esempio n. 4
0
func (driver *Driver) getVolume(volumeID, volumeName string) (volumesRet []volumes.Volume, err error) {
	if volumeID != "" {
		volume, err := volumes.Get(driver.ClientBlockStorage, volumeID).Extract()
		if err != nil {
			return []volumes.Volume{},
				errors.WithFieldsE(eff(errors.Fields{
					"volumeId":   volumeID,
					"volumeName": volumeName}),
					"error getting volumes", err)
		}
		volumesRet = append(volumesRet, *volume)
	} else {
		listOpts := &volumes.ListOpts{
		//Name:       volumeName,
		}

		allPages, err := volumes.List(driver.ClientBlockStorage, listOpts).AllPages()
		if err != nil {
			return []volumes.Volume{},
				errors.WithFieldsE(eff(errors.Fields{
					"volumeId":   volumeID,
					"volumeName": volumeName}),
					"error listing volumes", err)
		}
		volumesRet, err = volumes.ExtractVolumes(allPages)
		if err != nil {
			return []volumes.Volume{},
				errors.WithFieldsE(eff(errors.Fields{
					"volumeId":   volumeID,
					"volumeName": volumeName}),
					"error extracting volumes", err)
		}

		var volumesRetFiltered []volumes.Volume
		if volumeName != "" {
			var found bool
			for _, volume := range volumesRet {
				if volume.Name == volumeName {
					volumesRetFiltered = append(volumesRetFiltered, volume)
					found = true
					break
				}
			}
			if !found {
				return []volumes.Volume{}, nil
			}
		}
		volumesRet = volumesRetFiltered
	}

	return volumesRet, nil
}
Esempio n. 5
0
func (driver *Driver) GetSnapshot(volumeID, snapshotID, snapshotName string) ([]*storage.Snapshot, error) {
	snapshots, err := driver.getSnapshot(volumeID, snapshotID, snapshotName)
	if err != nil {
		return nil,
			errors.WithFieldsE(eff(errors.Fields{
				"volumeId":     volumeID,
				"snapshotId":   snapshotID,
				"snapshotName": snapshotName}),
				"error getting snapshot", err)
	}

	var snapshotsInt []*storage.Snapshot
	for _, snapshot := range snapshots {
		snapshotSD := &storage.Snapshot{
			Name:        snapshot.Name,
			VolumeID:    snapshot.VolumeID,
			SnapshotID:  snapshot.ID,
			VolumeSize:  strconv.Itoa(snapshot.Size),
			StartTime:   snapshot.CreatedAt,
			Description: snapshot.Description,
			Status:      snapshot.Status,
		}
		snapshotsInt = append(snapshotsInt, snapshotSD)
	}

	return snapshotsInt, nil
}
Esempio n. 6
0
func (driver *Driver) GetVolumeAttach(volumeID, instanceID string) ([]*storage.VolumeAttachment, error) {

	fields := eff(map[string]interface{}{
		"volumeId":   volumeID,
		"instanceId": instanceID,
	})

	if volumeID == "" {
		return []*storage.VolumeAttachment{},
			errors.WithFields(fields, "volumeId is required")
	}
	volume, err := driver.GetVolume(volumeID, "")
	if err != nil {
		return []*storage.VolumeAttachment{},
			errors.WithFieldsE(fields, "error getting volume attach", err)
	}

	if instanceID != "" {
		var attached bool
		for _, volumeAttachment := range volume[0].Attachments {
			if volumeAttachment.InstanceID == instanceID {
				return volume[0].Attachments, nil
			}
		}
		if !attached {
			return []*storage.VolumeAttachment{}, nil
		}
	}
	return volume[0].Attachments, nil
}
Esempio n. 7
0
func (driver *Driver) GetVolumeMapping() ([]*storage.BlockDevice, error) {
	blockDevices, err := driver.getBlockDevices(driver.InstanceID)
	if err != nil {
		return nil,
			errors.WithFieldsE(eff(errors.Fields{
				"instanceId": driver.InstanceID,
			}), "error getting block devices", err)
	}

	var BlockDevices []*storage.BlockDevice
	for _, blockDevice := range blockDevices {
		sdBlockDevice := &storage.BlockDevice{
			ProviderName: ProviderName,
			InstanceID:   driver.InstanceID,
			VolumeID:     blockDevice.VolumeID,
			DeviceName:   blockDevice.Device,
			Region:       driver.Region,
			Status:       "",
		}
		BlockDevices = append(BlockDevices, sdBlockDevice)
	}

	return BlockDevices, nil

}
Esempio n. 8
0
func (driver *Driver) GetVolumeMapping() ([]*storage.BlockDevice, error) {
	blockDevices, err := driver.getBlockDevices()
	if err != nil {
		return nil,
			errors.WithFieldsE(ef(), "error getting block devices", err)
	}

	var BlockDevices []*storage.BlockDevice
	for _, blockDevice := range blockDevices {
		sdBlockDevice := &storage.BlockDevice{
			ProviderName: ProviderName,
			InstanceID:   driver.Sdc.Sdc.ID,
			Region:       blockDevice.MdmID,
			DeviceName:   blockDevice.SdcDevice,
			VolumeID:     blockDevice.VolumeID,
			Status:       "",
		}
		BlockDevices = append(BlockDevices, sdBlockDevice)
	}

	log.WithFields(log.Fields{
		"provider":     ProviderName,
		"blockDevices": BlockDevices,
	}).Debug("got block device mappings")
	return BlockDevices, nil
}
Esempio n. 9
0
func (driver *Driver) CreateSnapshot(runAsync bool, snapshotName, volumeID, description string) ([]*storage.Snapshot, error) {

	fields := eff(map[string]interface{}{
		"runAsync":     runAsync,
		"snapshotName": snapshotName,
		"volumeId":     volumeID,
		"description":  description,
	})

	opts := snapshots.CreateOpts{
		Name:        snapshotName,
		VolumeID:    volumeID,
		Description: description,
		Force:       true,
	}

	resp, err := snapshots.Create(driver.ClientBlockStorage, opts).Extract()
	if err != nil {
		return nil,
			errors.WithFieldsE(fields, "error creating snapshot", err)
	}

	if !runAsync {
		log.Debug("waiting for snapshot creation to complete")
		err = snapshots.WaitForStatus(driver.ClientBlockStorage, resp.ID, "available", 120)
		if err != nil {
			return nil,
				errors.WithFieldsE(fields,
					"error waiting for snapshot creation to complete", err)
		}
	}

	snapshot, err := driver.GetSnapshot("", resp.ID, "")
	if err != nil {
		return nil, err
	}

	log.WithFields(log.Fields{
		"runAsync":     runAsync,
		"snapshotName": snapshotName,
		"volumeId":     volumeID,
		"description":  description}).Debug("created snapshot")

	return snapshot, nil

}
Esempio n. 10
0
func (driver *Driver) getBlockDevices() ([]*goscaleio.SdcMappedVolume, error) {
	volumeMaps, err := goscaleio.GetLocalVolumeMap()
	if err != nil {
		return []*goscaleio.SdcMappedVolume{},
			errors.WithFieldsE(ef(), "error getting local volume map", err)
	}
	return volumeMaps, nil
}
Esempio n. 11
0
func (driver *Driver) getInstance() (*servers.Server, error) {
	server, err := servers.Get(driver.Client, driver.InstanceID).Extract()
	if err != nil {
		return nil,
			errors.WithFieldsE(ef(), "error getting server instance", err)
	}

	return server, nil
}
Esempio n. 12
0
func (driver *Driver) AttachVolume(runAsync bool, volumeID, instanceID string) ([]*storage.VolumeAttachment, error) {
	fields := eff(map[string]interface{}{
		"runAsync":   runAsync,
		"volumeId":   volumeID,
		"instanceId": instanceID,
	})

	nextDeviceName, err := driver.GetDeviceNextAvailable()
	if err != nil {
		return nil, errors.WithFieldsE(
			fields, "error getting next available device", err)
	}

	options := &volumeattach.CreateOpts{
		Device:   nextDeviceName,
		VolumeID: volumeID,
	}

	_, err = volumeattach.Create(driver.Client, instanceID, options).Extract()
	if err != nil {
		return nil, errors.WithFieldsE(
			fields, "error attaching volume", err)
	}

	if !runAsync {
		log.WithFields(fields).Debug("waiting for volume to attach")
		err = driver.waitVolumeAttach(volumeID)
		if err != nil {
			return nil, errors.WithFieldsE(
				fields, "error waiting for volume to detach", err)
		}
	}

	volumeAttachment, err := driver.GetVolumeAttach(volumeID, instanceID)
	if err != nil {
		return nil, err
	}

	log.WithFields(fields).Debug("volume attached")
	return volumeAttachment, nil
}
Esempio n. 13
0
func (driver *Driver) DetachVolume(runAsync bool, volumeID string, blank string) error {

	fields := eff(map[string]interface{}{
		"runAsync": runAsync,
		"volumeId": volumeID,
		"blank":    blank,
	})

	if volumeID == "" {
		return errors.WithFields(fields, "volumeId is required")
	}

	volumes, err := driver.getVolume(volumeID, "")
	if err != nil {
		return errors.WithFieldsE(fields, "error getting volume", err)
	}

	if len(volumes) == 0 {
		return errors.WithFields(fields, "no volumes returned")
	}

	targetVolume := goscaleio.NewVolume(driver.Client)
	targetVolume.Volume = volumes[0]

	unmapVolumeSdcParam := &types.UnmapVolumeSdcParam{
		SdcID:                driver.Sdc.Sdc.ID,
		IgnoreScsiInitiators: "true",
		AllSdcs:              "",
	}

	// need to detect if unmounted first
	err = targetVolume.UnmapVolumeSdc(unmapVolumeSdcParam)
	if err != nil {
		return errors.WithFieldsE(fields, "error unmapping volume sdc", err)
	}

	log.WithFields(log.Fields{
		"provider": ProviderName,
		"volumeId": volumeID}).Debug("detached volume")
	return nil
}
Esempio n. 14
0
func (driver *Driver) getSnapshot(volumeID, snapshotID, snapshotName string) (allSnapshots []snapshots.Snapshot, err error) {

	fields := eff(map[string]interface{}{
		"volumeId":     volumeID,
		"snapshotId":   snapshotID,
		"snapshotName": snapshotName,
	})

	if snapshotID != "" {
		snapshot, err := snapshots.Get(driver.ClientBlockStorage, snapshotID).Extract()
		if err != nil {
			return []snapshots.Snapshot{},
				errors.WithFieldsE(fields, "error getting snapshot", err)
		}

		allSnapshots = append(allSnapshots, *snapshot)
	} else {
		opts := snapshots.ListOpts{
			VolumeID: volumeID,
			Name:     snapshotName,
		}

		allPages, err := snapshots.List(driver.ClientBlockStorage, opts).AllPages()
		if err != nil {
			return []snapshots.Snapshot{},
				errors.WithFieldsE(fields, "error listing snapshot", err)
		}

		allSnapshots, err = snapshots.ExtractSnapshots(allPages)
		if err != nil {
			return []snapshots.Snapshot{},
				errors.WithFieldsE(fields, "error extracting snapshot", err)
		}
	}

	return allSnapshots, nil
}
Esempio n. 15
0
func (driver *Driver) RemoveVolume(volumeID string) error {
	fields := eff(map[string]interface{}{
		"volumeId": volumeID,
	})
	if volumeID == "" {
		return errors.WithFields(fields, "volumeId is required")
	}
	res := volumes.Delete(driver.ClientBlockStorage, volumeID)
	if res.Err != nil {
		return errors.WithFieldsE(fields, "error removing volume", res.Err)
	}

	log.WithFields(fields).Debug("removed volume")
	return nil
}
Esempio n. 16
0
func (driver *Driver) GetInstance() (*storage.Instance, error) {
	server, err := driver.getInstance()
	if err != nil {
		return nil,
			errors.WithFieldsE(ef(), "error getting driver instance", err)
	}

	instance := &storage.Instance{
		ProviderName: ProviderName,
		InstanceID:   driver.InstanceID,
		Region:       driver.Region,
		Name:         server.Name,
	}

	return instance, nil
}
Esempio n. 17
0
func (driver *Driver) getBlockDevices(instanceID string) ([]volumeattach.VolumeAttachment, error) {
	// volumes := volumeattach.Get(driver.Client, driver.InstanceID, "")
	allPages, err := volumeattach.List(driver.Client, driver.InstanceID).AllPages()

	// volumeAttachments, err := volumes.VolumeAttachmentResult.ExtractAll()
	volumeAttachments, err := volumeattach.ExtractVolumeAttachments(allPages)
	if err != nil {
		return []volumeattach.VolumeAttachment{},
			errors.WithFieldsE(eff(errors.Fields{
				"instanceId": instanceID}),
				"error extracting volume attachments", err)
	}

	return volumeAttachments, nil

}
Esempio n. 18
0
func getLocalDevices() (deviceNames []string, err error) {
	file := "/proc/partitions"
	contentBytes, err := ioutil.ReadFile(file)
	if err != nil {
		return []string{},
			errors.WithFieldsE(
				eff(errors.Fields{"file": file}), "error reading file", err)
	}

	content := string(contentBytes)

	lines := strings.Split(content, "\n")
	for _, line := range lines[2:] {
		fields := strings.Fields(line)
		if len(fields) == 4 {
			deviceNames = append(deviceNames, fields[3])
		}
	}

	return deviceNames, nil
}
Esempio n. 19
0
func (driver *Driver) GetVolume(volumeID, volumeName string) ([]*storage.Volume, error) {

	volumesRet, err := driver.getVolume(volumeID, volumeName)
	if err != nil {
		return []*storage.Volume{},
			errors.WithFieldsE(eff(errors.Fields{
				"volumeId":   volumeID,
				"volumeName": volumeName}),
				"error getting volume", err)
	}

	var volumesSD []*storage.Volume
	for _, volume := range volumesRet {
		var attachmentsSD []*storage.VolumeAttachment
		for _, attachment := range volume.Attachments {
			attachmentSD := &storage.VolumeAttachment{
				VolumeID:   attachment["volume_id"].(string),
				InstanceID: attachment["server_id"].(string),
				DeviceName: attachment["device"].(string),
				Status:     "",
			}
			attachmentsSD = append(attachmentsSD, attachmentSD)
		}

		volumeSD := &storage.Volume{
			Name:             volume.Name,
			VolumeID:         volume.ID,
			AvailabilityZone: volume.AvailabilityZone,
			Status:           volume.Status,
			VolumeType:       volume.VolumeType,
			IOPS:             0,
			Size:             strconv.Itoa(volume.Size),
			Attachments:      attachmentsSD,
		}
		volumesSD = append(volumesSD, volumeSD)
	}

	return volumesSD, nil
}
Esempio n. 20
0
func (driver *Driver) waitVolumeAttach(volumeID string) error {

	fields := eff(map[string]interface{}{
		"volumeId": volumeID,
	})

	if volumeID == "" {
		return errors.WithFields(fields, "volumeId is required")
	}
	for {
		volume, err := driver.GetVolume(volumeID, "")
		if err != nil {
			return errors.WithFieldsE(fields, "error getting volume", err)
		}
		if volume[0].Status == "in-use" {
			break
		}
		time.Sleep(1 * time.Second)
	}

	return nil
}
Esempio n. 21
0
func Init(cfg *config.Config) (storage.Driver, error) {

	fields := eff(map[string]interface{}{
		"endpoint":         cfg.XtremIoEndpoint,
		"userName":         cfg.XtremIoUserName,
		"deviceMapper":     cfg.XtremIoDeviceMapper,
		"multipath":        cfg.XtremIoMultipath,
		"remoteManagement": cfg.XtremIoRemoteManagement,
		"insecure":         cfg.XtremIoInsecure,
	})

	if cfg.XtremIoPassword == "" {
		fields["password"] = ""
	} else {
		fields["password"] = "******"
	}

	if !isXtremIOAttached() && !cfg.XtremIoRemoteManagement {
		return nil, errors.WithFields(fields, "device not detected")
	}

	client, cErr := xtio.NewClientWithArgs(
		cfg.XtremIoEndpoint,
		cfg.XtremIoInsecure,
		cfg.XtremIoUserName,
		cfg.XtremIoPassword)

	if cErr != nil {
		return nil,
			errors.WithFieldsE(fields, "error creating xtremio client", cErr)
	}

	var iqn string
	var ini xtio.Initiator
	if !cfg.XtremIoRemoteManagement {
		var iqnErr error
		iqn, iqnErr = getIQN()
		if iqnErr != nil {
			return nil, iqnErr
		}

		var iniErr error
		ini, iniErr = client.GetInitiator("", iqn)
		if iniErr != nil {
			return nil, iniErr
		}
	}

	useDeviceMapper, _ := strconv.ParseBool(os.Getenv("REXRAY_XTREMIO_DM"))
	useMultipath, _ := strconv.ParseBool(os.Getenv("REXRAY_XTREMIO_MULTIPATH"))

	driver := &Driver{
		Client:          client,
		Initiator:       ini,
		UseDeviceMapper: useDeviceMapper,
		UseMultipath:    useMultipath,
		Config:          cfg,
		VolumesByNAA:    map[string]xtio.Volume{},
	}

	log.WithField("provider", ProviderName).Debug(
		"storage driver initialized")

	return driver, nil
}
Esempio n. 22
0
func (driver *Driver) CreateVolume(
	runAsync bool,
	volumeName string,
	volumeID string,
	snapshotID string,
	volumeType string,
	IOPS int64,
	size int64,
	availabilityZone string) (*storage.Volume, error) {

	fields := map[string]interface{}{
		"provider":         ProviderName,
		"runAsync":         runAsync,
		"volumeName":       volumeName,
		"volumeId":         volumeID,
		"snapshotId":       snapshotID,
		"volumeType":       volumeType,
		"iops":             IOPS,
		"size":             size,
		"availabilityZone": availabilityZone,
	}

	if volumeID != "" && runAsync {
		return nil, errors.WithFields(fields,
			"cannot create volume from volume & run async")
	}

	if snapshotID != "" {
		snapshot, err := driver.GetSnapshot("", snapshotID, "")
		if err != nil {
			return nil,
				errors.WithFieldsE(fields, "error getting snapshot", err)
		}

		if len(snapshot) == 0 {
			return nil,
				errors.WithFields(fields, "snapshot array is empty")
		}

		volSize := snapshot[0].VolumeSize
		sizeInt, err := strconv.Atoi(volSize)
		if err != nil {
			f := errors.Fields{
				"volumeSize": volSize,
			}
			for k, v := range fields {
				f[k] = v
			}
			return nil,
				errors.WithFieldsE(f, "error casting volume size", err)
		}
		size = int64(sizeInt)
	}

	var volume []*storage.Volume
	var err error
	if volumeID != "" {
		volume, err = driver.GetVolume(volumeID, "")
		if err != nil {
			return nil, errors.WithFields(fields, "error getting volume")
		}

		if len(volume) == 0 {
			return nil,
				errors.WithFields(fields, "volume array is empty")
		}

		volSize := volume[0].Size
		sizeInt, err := strconv.Atoi(volSize)
		if err != nil {
			f := errors.Fields{
				"volumeSize": volSize,
			}
			for k, v := range fields {
				f[k] = v
			}
			return nil,
				errors.WithFieldsE(f, "error casting volume size", err)
		}
		size = int64(sizeInt)

		volumeID := volume[0].VolumeID
		snapshot, err := driver.CreateSnapshot(
			false, fmt.Sprintf("temp-%s", volumeID), volumeID, "")
		if err != nil {
			return nil,
				errors.WithFields(fields, "error creating snapshot")
		}

		snapshotID = snapshot[0].SnapshotID

		if availabilityZone == "" {
			availabilityZone = volume[0].AvailabilityZone
		}

	}

	if size != 0 && size < minSize {
		size = minSize
	}

	options := &volumes.CreateOpts{
		Name:         volumeName,
		Size:         int(size),
		SnapshotID:   snapshotID,
		VolumeType:   volumeType,
		Availability: availabilityZone,
	}
	resp, err := volumes.Create(driver.ClientBlockStorage, options).Extract()
	if err != nil {
		return nil,
			errors.WithFields(fields, "error creating volume")
	}

	if !runAsync {
		log.Debug("waiting for volume creation to complete")
		err = volumes.WaitForStatus(driver.ClientBlockStorage, resp.ID, "available", 120)
		if err != nil {
			return nil,
				errors.WithFields(fields,
					"error waiting for volume creation to complete")
		}

		if volumeID != "" {
			err := driver.RemoveSnapshot(snapshotID)
			if err != nil {
				return nil,
					errors.WithFields(fields,
						"error removing snapshot")
			}
		}
	}

	fields["volumeId"] = resp.ID
	fields["volumeName"] = ""

	volume, err = driver.GetVolume(resp.ID, "")
	if err != nil {
		return nil, errors.WithFields(fields,
			"error removing snapshot")
	}

	log.WithFields(fields).Debug("created volume")
	return volume[0], nil
}
Esempio n. 23
0
func Init(cfg *config.Config) (storage.Driver, error) {

	fields := eff(map[string]interface{}{
		"endpoint": cfg.ScaleIoEndpoint,
		"insecure": cfg.ScaleIoInsecure,
		"useCerts": cfg.ScaleIoUseCerts,
	})

	client, err := goscaleio.NewClientWithArgs(
		cfg.ScaleIoEndpoint,
		cfg.ScaleIoInsecure,
		cfg.ScaleIoUseCerts)

	if err != nil {
		return nil, errors.WithFieldsE(fields,
			"error constructing new client", err)
	}

	_, err = client.Authenticate(&goscaleio.ConfigConnect{
		cfg.ScaleIoEndpoint, cfg.ScaleIoUserName, cfg.ScaleIoPassword})
	if err != nil {
		fields["userName"] = cfg.ScaleIoUserName
		if cfg.ScaleIoPassword != "" {
			fields["password"] = "******"
		}
		return nil, errors.WithFieldsE(fields,
			"error authenticating", err)
	}

	system, err := client.FindSystem(
		cfg.ScaleIoSystemId, cfg.ScaleIoSystemName, "")
	if err != nil {
		fields["systemId"] = cfg.ScaleIoSystemId
		fields["systemName"] = cfg.ScaleIoSystemName
		return nil, errors.WithFieldsE(fields,
			"error finding system", err)
	}

	pd, err := system.FindProtectionDomain(
		cfg.ScaleIoProtectionDomainId, cfg.ScaleIoProtectionDomainName, "")
	if err != nil {
		fields["domainId"] = cfg.ScaleIoProtectionDomainId
		fields["domainName"] = cfg.ScaleIoProtectionDomainName
		return nil, errors.WithFieldsE(fields,
			"error finding protection domain", err)
	}

	protectionDomain := goscaleio.NewProtectionDomain(client)
	protectionDomain.ProtectionDomain = pd

	sp, err := protectionDomain.FindStoragePool(
		cfg.ScaleIoStoragePoolId, cfg.ScaleIoStoragePoolName, "")
	if err != nil {
		fields["storagePoolId"] = cfg.ScaleIoStoragePoolId
		fields["storagePoolName"] = cfg.ScaleIoStoragePoolName
		return nil, errors.WithFieldsE(fields,
			"error finding storage pool", err)
	}

	storagePool := goscaleio.NewStoragePool(client)
	storagePool.StoragePool = sp

	sdcguid, err := goscaleio.GetSdcLocalGUID()
	if err != nil {
		return nil, errors.WithFieldsE(fields,
			"error getting sdc local guid", err)
	}

	sdc, err := system.FindSdc("SdcGuid", strings.ToUpper(sdcguid))
	if err != nil {
		fields["sdcGuid"] = sdcguid
		return nil, errors.WithFieldsE(fields,
			"error finding sdc", err)
	}

	driver := &Driver{
		Client:           client,
		System:           system,
		ProtectionDomain: protectionDomain,
		StoragePool:      storagePool,
		Sdc:              sdc,
	}

	log.WithField("providerName", ProviderName).Debug(
		"storage driver initialized")

	return driver, nil
}
Esempio n. 24
0
func Init(cfg *config.Config) (storage.Driver, error) {

	fields := ef()
	instanceID, err := getInstanceID(cfg)
	if err != nil {
		return nil, err
	}
	fields["instanceId"] = instanceID

	region, err := getInstanceRegion(cfg)
	if err != nil {
		return nil, err
	}
	fields["region"] = region

	authOpts := getAuthOptions(cfg)

	fields["identityEndpoint"] = cfg.RackspaceAuthUrl
	fields["userId"] = cfg.RackspaceUserId
	fields["userName"] = cfg.RackspaceUserName
	if cfg.RackspacePassword == "" {
		fields["password"] = ""
	} else {
		fields["password"] = "******"
	}
	fields["tenantId"] = cfg.RackspaceTenantId
	fields["tenantName"] = cfg.RackspaceTenantName
	fields["domainId"] = cfg.RackspaceDomainId
	fields["domainName"] = cfg.RackspaceDomainName

	provider, err := openstack.AuthenticatedClient(authOpts)
	if err != nil {
		return nil,
			errors.WithFieldsE(fields, "error getting authenticated client", err)
	}

	region = strings.ToUpper(region)
	client, err := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{
		Region: region,
	})
	if err != nil {
		return nil,
			errors.WithFieldsE(fields, "error getting newComputeV2", err)
	}

	clientBlockStorage, err := openstack.NewBlockStorageV1(provider, gophercloud.EndpointOpts{
		Region: region,
	})
	if err != nil {
		return nil, errors.WithFieldsE(
			fields, "error getting newBlockStorageV1", err)
	}

	driver := &Driver{
		Provider:           provider,
		Client:             client,
		ClientBlockStorage: clientBlockStorage,
		Region:             region,
		InstanceID:         instanceID,
		Config:             cfg,
	}

	return driver, nil
}
Esempio n. 25
0
func StartModule(modInstId int32) error {

	modInstancesRwl.RLock()
	defer modInstancesRwl.RUnlock()

	lf := map[string]interface{}{"id": modInstId}

	mod, modExists := modInstances[modInstId]

	if !modExists {
		return errors.WithFields(lf, "unknown module instance")
	}

	lf["id"] = mod.Id
	lf["typeId"] = mod.Type.Id
	lf["typeName"] = mod.Type.Name
	lf["address"] = mod.Config.Address

	started := make(chan bool)
	timeout := make(chan bool)
	startError := make(chan error)

	go func() {

		defer func() {
			r := recover()
			m := "error starting module"

			errMsg := fmt.Sprintf(
				"Error starting module type %d, %d-%s at %s",
				mod.TypeId, mod.Id, mod.Name, mod.Config.Address)

			if r == nil {
				startError <- errors.New(errMsg)
				return
			}

			switch x := r.(type) {
			case string:
				lf["inner"] = x
				startError <- errors.WithFields(lf, m)
			case error:
				startError <- errors.WithFieldsE(lf, m, x)
			default:
				startError <- errors.WithFields(lf, m)
			}
		}()

		sErr := mod.Inst.Start()
		if sErr != nil {
			startError <- sErr
		} else {
			started <- true
		}
	}()

	go func() {
		time.Sleep(3 * time.Second)
		timeout <- true
	}()

	select {
	case <-started:
		mod.IsStarted = true
		log.WithFields(lf).Info("started module")
	case <-timeout:
		log.WithFields(lf).Debug("timed out while monitoring module start")
	case sErr := <-startError:
		return sErr
	}

	return nil
}