func WaitForVirtualGuestToTargetState(softLayerClient sl.Client, virtualGuestId int, targetState string, logger boshlog.Logger) error {
	virtualGuestService, err := softLayerClient.GetSoftLayer_Virtual_Guest_Service()
	if err != nil {
		return bosherr.WrapError(err, "Creating VirtualGuestService from SoftLayer client")
	}

	getTargetStateRetryable := boshretry.NewRetryable(
		func() (bool, error) {
			vgPowerState, err := virtualGuestService.GetPowerState(virtualGuestId)
			if err != nil {
				return false, bosherr.WrapErrorf(err, "Getting PowerState from vitrual guest %d", virtualGuestId)
			} else {
				if strings.Contains(vgPowerState.KeyName, targetState) {
					return false, nil
				}
				return true, nil
			}
		})

	timeService := clock.NewClock()
	timeoutRetryStrategy := boshretry.NewTimeoutRetryStrategy(TIMEOUT, POLLING_INTERVAL, getTargetStateRetryable, timeService, logger)
	err = timeoutRetryStrategy.Try()
	if err != nil {
		return bosherr.Errorf("Waiting for virtual guest with ID '%d' to have be in state '%s'", virtualGuestId, targetState)
	}

	return nil
}
func WaitForVirtualGuestIsNotPingable(softLayerClient sl.Client, virtualGuestId int, logger boshlog.Logger) error {
	virtualGuestService, err := softLayerClient.GetSoftLayer_Virtual_Guest_Service()
	if err != nil {
		return bosherr.WrapError(err, "Creating VirtualGuestService from SoftLayer client")
	}

	checkPingableRetryable := boshretry.NewRetryable(
		func() (bool, error) {
			state, err := virtualGuestService.IsPingable(virtualGuestId)
			if err != nil {
				return false, bosherr.WrapErrorf(err, "Checking pingable against vitrual guest %d", virtualGuestId)
			} else {
				return state, nil
			}
		})

	timeService := clock.NewClock()
	timeoutRetryStrategy := boshretry.NewTimeoutRetryStrategy(TIMEOUT, POLLING_INTERVAL, checkPingableRetryable, timeService, logger)
	err = timeoutRetryStrategy.Try()
	if err != nil {
		return bosherr.Errorf("Waiting for virtual guest with ID '%d' is not pingable", virtualGuestId)
	}

	return nil
}
func WaitForVirtualGuestToHaveNoRunningTransaction(softLayerClient sl.Client, virtualGuestId int, logger boshlog.Logger) error {

	virtualGuestService, err := softLayerClient.GetSoftLayer_Virtual_Guest_Service()
	if err != nil {
		return bosherr.WrapError(err, "Creating VirtualGuestService from SoftLayer client")
	}

	runningTransactionsRetryable := boshretry.NewRetryable(
		func() (bool, error) {
			activeTransactions, err := virtualGuestService.GetActiveTransactions(virtualGuestId)
			if err != nil {
				return false, bosherr.WrapErrorf(err, "Getting active transaction against vitrual guest %d", virtualGuestId)
			} else {
				if len(activeTransactions) == 0 {
					return false, nil
				}
				return true, nil
			}
		})

	timeService := clock.NewClock()
	timeoutRetryStrategy := boshretry.NewTimeoutRetryStrategy(TIMEOUT, POLLING_INTERVAL, runningTransactionsRetryable, timeService, logger)
	err = timeoutRetryStrategy.Try()
	if err != nil {
		return bosherr.Errorf("Waiting for virtual guest with ID '%d' to have no active transactions", virtualGuestId)
	}
	return nil
}
func (vmInfoDB *VMInfoDB) QueryVMInfobyAgentID(retryTimeout time.Duration, retryInterval time.Duration) error {

	execStmtRetryable := boshretry.NewRetryable(
		func() (bool, error) {
			tx, err := vmInfoDB.db.Begin()
			if err != nil {
				sqliteErr := err.(sqlite3.Error)
				if sqliteErr.Code == sqlite3.ErrBusy || sqliteErr.Code == sqlite3.ErrLocked {
					return true, bosherr.WrapError(sqliteErr, "retrying...")
				} else {
					return false, bosherr.WrapError(sqliteErr, "Failed to begin DB transcation")
				}
			}

			var prepareStmt string
			if vmInfoDB.VmProperties.InUse == "t" {
				prepareStmt = "SELECT id, image_id, agent_id FROM vms WHERE in_use='t' AND agent_id=?"
			} else if vmInfoDB.VmProperties.InUse == "f" {
				prepareStmt = "SELECT id, image_id, agent_id FROM vms WHERE in_use='f' AND agent_id=?"
			} else {
				prepareStmt = "SELECT id, image_id, agent_id FROM vms WHERE agent_id==?"
			}

			sqlStmt, err := tx.Prepare(prepareStmt)
			defer sqlStmt.Close()
			if err != nil {
				sqliteErr := err.(sqlite3.Error)
				if sqliteErr.Code == sqlite3.ErrBusy || sqliteErr.Code == sqlite3.ErrLocked {
					return true, bosherr.WrapError(sqliteErr, "retrying...")
				} else {
					return false, bosherr.WrapError(sqliteErr, "Failed to prepare sql statement")
				}
			}

			err = sqlStmt.QueryRow(vmInfoDB.VmProperties.AgentId).Scan(&vmInfoDB.VmProperties.Id, &vmInfoDB.VmProperties.ImageId, &vmInfoDB.VmProperties.AgentId)
			if err != nil && !strings.Contains(err.Error(), "no rows") {
				sqliteErr := err.(sqlite3.Error)
				if sqliteErr.Code == sqlite3.ErrBusy || sqliteErr.Code == sqlite3.ErrLocked {
					return true, bosherr.WrapError(sqliteErr, "retrying...")
				} else {
					return false, bosherr.WrapError(sqliteErr, "Failed to query VM info from vms table")
				}

			}
			tx.Commit()
			return false, nil
		})

	timeService := clock.NewClock()
	timeoutRetryStrategy := boshretry.NewTimeoutRetryStrategy(retryTimeout, retryInterval, execStmtRetryable, timeService, vmInfoDB.logger)
	err := timeoutRetryStrategy.Try()
	if err != nil {
		return bosherr.WrapError(err, fmt.Sprintf("Failed to run QueryVMInfobyAgentID"))
	} else {
		return nil
	}

}
func (cmd *ImportImageCmd) Run() error {
	vgbdtgService, err := cmd.client.GetSoftLayer_Virtual_Guest_Block_Device_Template_Group_Service()
	if err != nil {
		return errors.New(fmt.Sprintf("Could not get virtual guest block device template group service from softlayer-go service: `%s`", err.Error()))
	}

	configuration := sldatatypes.SoftLayer_Container_Virtual_Guest_Block_Device_Template_Configuration{
		Name: cmd.Name,
		Note: cmd.Note,
		OperatingSystemReferenceCode: cmd.OsRefCode,
		Uri: cmd.Uri,
	}

	vgbdtgObject, err := vgbdtgService.CreateFromExternalSource(configuration)
	if err != nil {
		return errors.New(fmt.Sprintf("Problem creating image template from external source: `%s`", err.Error()))
	}

	cmd.Id = vgbdtgObject.Id
	cmd.Uuid = vgbdtgObject.GlobalIdentifier

	if cmd.Public {
		execStmtRetryable := boshretry.NewRetryable(
			func() (bool, error) {
				id, err := vgbdtgService.CreatePublicArchiveTransaction(cmd.Id, cmd.PublicName, cmd.PublicNote, cmd.PublicNote, locations)
				if err != nil {
					return true, errors.New(fmt.Sprintf("There would be an active transaction in progress."))
				}

				cmd.Id = id
				cmd.Uuid = ""

				return false, nil
			})
		timeService := clock.NewClock()
		timeoutRetryStrategy := boshretry.NewTimeoutRetryStrategy(common.TIMEOUT, common.POLLING_INTERVAL, execStmtRetryable, timeService, boshlog.NewLogger(boshlog.LevelInfo))
		err = timeoutRetryStrategy.Try()
		if err != nil {
			return errors.New(fmt.Sprintf("Problem occurred when making image template public: `%s`", err.Error()))
		} else {
			return nil
		}
	}

	return nil
}
func exec(db DB, sqlStmt string, retryTimeout time.Duration, retryInterval time.Duration, logger boshlog.Logger) error {

	execStmtRetryable := boshretry.NewRetryable(
		func() (bool, error) {
			tx, err := db.Begin()
			if err != nil {
				sqliteErr := err.(sqlite3.Error)
				if sqliteErr.Code == sqlite3.ErrBusy || sqliteErr.Code == sqlite3.ErrLocked {
					fmt.Println("err is " + err.Error())
					return true, bosherr.WrapError(sqliteErr, "Retrying...")
				} else {
					return false, bosherr.WrapError(sqliteErr, "Failed to begin DB transcation")
				}
			}

			_, err = tx.Exec(sqlStmt)
			if err != nil {
				sqliteErr := err.(sqlite3.Error)
				if sqliteErr.Code == sqlite3.ErrBusy || sqliteErr.Code == sqlite3.ErrLocked {
					return true, bosherr.WrapError(sqliteErr, "Retrying...")
				} else {
					return false, bosherr.WrapError(sqliteErr, "Failed to execute sql statement: "+sqlStmt)
				}
			}

			tx.Commit()
			return false, nil
		})

	timeService := clock.NewClock()
	timeoutRetryStrategy := boshretry.NewTimeoutRetryStrategy(retryTimeout, retryInterval, execStmtRetryable, timeService, logger)
	err := timeoutRetryStrategy.Try()
	if err != nil {
		return bosherr.WrapError(err, fmt.Sprintf("Failed to execute the sql statment %s", sqlStmt))
	} else {
		return nil
	}

}
func (slns *softLayer_Network_Storage_Service) CreateNetworkStorage(size int, capacity int, location string, useHourlyPricing bool) (datatypes.SoftLayer_Network_Storage, error) {
	if size < 0 {
		return datatypes.SoftLayer_Network_Storage{}, errors.New("Cannot create negative sized volumes")
	}

	sizeItemPriceId, err := slns.getIscsiVolumeItemIdBasedOnSize(size)
	if err != nil {
		return datatypes.SoftLayer_Network_Storage{}, err
	}

	var iopsItemPriceId int

	if capacity == 0 {
		iopsItemPriceId, err = slns.selectMediumIopsItemPriceIdOnSize(size)
		if err != nil {
			return datatypes.SoftLayer_Network_Storage{}, err
		}

	} else {
		iopsItemPriceId, err = slns.getItemPriceIdBySizeAndIops(size, capacity)
		if err != nil {
			return datatypes.SoftLayer_Network_Storage{}, err
		}
	}

	blockStorageItemPriceId, err := slns.getBlockStorageItemPriceId()

	order := datatypes.SoftLayer_Container_Product_Order_Network_PerformanceStorage_Iscsi{
		Location:    location,
		ComplexType: "SoftLayer_Container_Product_Order_Network_PerformanceStorage_Iscsi",
		OsFormatType: datatypes.SoftLayer_Network_Storage_Iscsi_OS_Type{
			Id:      12,
			KeyName: "LINUX",
		},
		Prices: []datatypes.SoftLayer_Product_Item_Price{
			datatypes.SoftLayer_Product_Item_Price{
				Id: sizeItemPriceId,
			},
			datatypes.SoftLayer_Product_Item_Price{
				Id: iopsItemPriceId,
			},
			datatypes.SoftLayer_Product_Item_Price{
				Id: blockStorageItemPriceId,
			},
		},
		PackageId:        NETWORK_PERFORMANCE_STORAGE_PACKAGE_ID,
		Quantity:         1,
		UseHourlyPricing: useHourlyPricing,
	}

	productOrderService, err := slns.client.GetSoftLayer_Product_Order_Service()
	if err != nil {
		return datatypes.SoftLayer_Network_Storage{}, err
	}

	receipt, err := productOrderService.PlaceContainerOrderNetworkPerformanceStorageIscsi(order)
	if err != nil {
		return datatypes.SoftLayer_Network_Storage{}, err
	}

	var iscsiStorage datatypes.SoftLayer_Network_Storage
	SL_CREATE_ISCSI_VOLUME_TIMEOUT, err := strconv.Atoi(os.Getenv("SL_CREATE_ISCSI_VOLUME_TIMEOUT"))
	if err != nil || SL_CREATE_ISCSI_VOLUME_TIMEOUT == 0 {
		SL_CREATE_ISCSI_VOLUME_TIMEOUT = 600
	}
	SL_CREATE_ISCSI_VOLUME_POLLING_INTERVAL, err := strconv.Atoi(os.Getenv("SL_CREATE_ISCSI_VOLUME_POLLING_INTERVAL"))
	if err != nil || SL_CREATE_ISCSI_VOLUME_POLLING_INTERVAL == 0 {
		SL_CREATE_ISCSI_VOLUME_POLLING_INTERVAL = 10
	}

	execStmtRetryable := boshretry.NewRetryable(
		func() (bool, error) {
			iscsiStorage, err = slns.findIscsiVolumeId(receipt.OrderId)
			if err != nil {
				return true, errors.New(fmt.Sprintf("Failed to find iSCSI volume with id `%d` due to `%s`, retrying...", receipt.OrderId, err.Error()))
			}

			return false, nil
		})
	timeService := clock.NewClock()
	timeoutRetryStrategy := boshretry.NewTimeoutRetryStrategy(time.Duration(SL_CREATE_ISCSI_VOLUME_TIMEOUT)*time.Second, time.Duration(SL_CREATE_ISCSI_VOLUME_POLLING_INTERVAL)*time.Second, execStmtRetryable, timeService, boshlog.NewLogger(boshlog.LevelInfo))
	err = timeoutRetryStrategy.Try()
	if err != nil {
		return datatypes.SoftLayer_Network_Storage{}, errors.New(fmt.Sprintf("Failed to find iSCSI volume with id `%d` after retry within `%d` seconds", receipt.OrderId, SL_CREATE_ISCSI_VOLUME_TIMEOUT))
	}

	return iscsiStorage, nil
}
Beispiel #8
0
func (vm *vm) WaitUntilReady(timeout time.Duration, delay time.Duration) error {
	agentPingRetryable := biagentclient.NewPingRetryable(vm.agentClient)
	timeService := clock.NewClock() //TODO: inject timeService
	agentPingRetryStrategy := boshretry.NewTimeoutRetryStrategy(timeout, delay, agentPingRetryable, timeService, vm.logger)
	return agentPingRetryStrategy.Try()
}
func (vmInfoDB *VMInfoDB) UpdateVMInfoByID(retryTimeout time.Duration, retryInterval time.Duration) error {

	execStmtRetryable := boshretry.NewRetryable(
		func() (bool, error) {
			tx, err := vmInfoDB.db.Begin()
			if err != nil {
				sqliteErr := err.(sqlite3.Error)
				if sqliteErr.Code == sqlite3.ErrBusy || sqliteErr.Code == sqlite3.ErrLocked {
					return true, bosherr.WrapError(sqliteErr, "retrying...")
				} else {
					return false, bosherr.WrapError(sqliteErr, "Failed to begin DB transcation")
				}
			}

			if vmInfoDB.VmProperties.InUse == "f" || vmInfoDB.VmProperties.InUse == "t" {
				sqlStmt := fmt.Sprintf("UPDATE vms SET in_use='%s', timestamp=CURRENT_TIMESTAMP WHERE id = %d", vmInfoDB.VmProperties.InUse, vmInfoDB.VmProperties.Id)
				_, err = tx.Exec(sqlStmt)
				if err != nil {
					sqliteErr := err.(sqlite3.Error)
					if sqliteErr.Code == sqlite3.ErrBusy || sqliteErr.Code == sqlite3.ErrLocked {
						return true, bosherr.WrapError(sqliteErr, "retrying...")
					} else {
						return false, bosherr.WrapError(sqliteErr, "Failed to update in_use column in vms")
					}
				}
			}

			if vmInfoDB.VmProperties.ImageId != "" {
				sqlStmt := fmt.Sprintf("UPDATE vms SET image_id='%s' WHERE id = %d", vmInfoDB.VmProperties.ImageId, vmInfoDB.VmProperties.Id)
				_, err = tx.Exec(sqlStmt)
				if err != nil {
					sqliteErr := err.(sqlite3.Error)
					if sqliteErr.Code == sqlite3.ErrBusy || sqliteErr.Code == sqlite3.ErrLocked {
						return true, bosherr.WrapError(sqliteErr, "retrying...")
					} else {
						return false, bosherr.WrapError(sqliteErr, "Failed to update in_use column in vms")
					}
				}
			}

			if vmInfoDB.VmProperties.AgentId != "" {
				sqlStmt := fmt.Sprintf("UPDATE vms SET agent_id='%s' WHERE id = %d", vmInfoDB.VmProperties.AgentId, vmInfoDB.VmProperties.Id)
				_, err = tx.Exec(sqlStmt)
				if err != nil {
					sqliteErr := err.(sqlite3.Error)
					if sqliteErr.Code == sqlite3.ErrBusy || sqliteErr.Code == sqlite3.ErrLocked {
						return true, bosherr.WrapError(sqliteErr, "retrying...")
					} else {
						return false, bosherr.WrapError(sqliteErr, "Failed to update in_use column in vms")
					}
				}
			}
			tx.Commit()
			return false, nil
		})

	timeService := clock.NewClock()
	timeoutRetryStrategy := boshretry.NewTimeoutRetryStrategy(retryTimeout, retryInterval, execStmtRetryable, timeService, vmInfoDB.logger)
	err := timeoutRetryStrategy.Try()
	if err != nil {
		return bosherr.WrapError(err, fmt.Sprintf("Failed to run UpdateVMInfoByID"))
	} else {
		return nil
	}
}