def _create(softlayer_client): # validate instance_properties = ctx.node.properties price_ids = _validate_inputs_and_generate_price_ids( softlayer_client, **instance_properties) # create the order vs_manager = ExtendedVSManager(softlayer_client) order = _create_order_options(instance_properties=instance_properties, price_ids=price_ids, vs_manager=vs_manager) # verify before placing the order ctx.logger.info('verifying order: {0}'.format(order)) vs_manager.verify_place_order(**order) # place an order hostname = order[constants.HOSTNAME] ctx.logger.info('Placing an order for VM [{0}]'.format(hostname)) order_result = vs_manager.place_order(**order) # store information in runtime properties instance_id = order_result['orderDetails']['virtualGuests'][0]['id'] ctx.instance.runtime_properties[constants.INSTANCE_ID] = instance_id ctx.instance.runtime_properties[constants.HOSTNAME] = hostname ctx.logger.info( 'An order for creating host [id: {0}, hostname: {1}] ' 'has been placed. waiting fot transactions to begin..'.format( instance_id, hostname)) ctx.instance.runtime_properties[constants.STATUS] = constants.CREATE_SENT
def _create(softlayer_client): # validate instance_properties = ctx.node.properties price_ids = _validate_inputs_and_generate_price_ids( softlayer_client, **instance_properties) # create the order vs_manager = ExtendedVSManager(softlayer_client) order = _create_order_options(instance_properties=instance_properties, price_ids=price_ids, vs_manager=vs_manager) # verify before placing the order ctx.logger.info('verifying order: {0}'.format(order)) vs_manager.verify_place_order(**order) # place an order hostname = order[constants.HOSTNAME] ctx.logger.info('Placing an order for VM [{0}]'.format(hostname)) order_result = vs_manager.place_order(**order) # store information in runtime properties instance_id = order_result['orderDetails']['virtualGuests'][0]['id'] ctx.instance.runtime_properties[constants.INSTANCE_ID] = instance_id ctx.instance.runtime_properties[constants.HOSTNAME] = hostname ctx.logger.info('An order for creating host [id: {0}, hostname: {1}] ' 'has been placed. waiting fot transactions to begin..' .format(instance_id, hostname)) ctx.instance.runtime_properties[constants.STATUS] = constants.CREATE_SENT
def _delete(softlayer_client): vm_details = _get_vm_details() ctx.logger.info('deleting server [{0}]'.format( _get_vm_details_for_print(vm_details))) vs = ExtendedVSManager(softlayer_client) vs.cancel_instance(vm_details[constants.INSTANCE_ID]) ctx.instance.runtime_properties[constants.STATUS] = constants.DELETE_SENT
def _delete(softlayer_client): vm_details = _get_vm_details() ctx.logger.info('deleting server [{0}]' .format(_get_vm_details_for_print(vm_details))) vs = ExtendedVSManager(softlayer_client) vs.cancel_instance(vm_details[constants.INSTANCE_ID]) ctx.instance.runtime_properties[constants.STATUS] = constants.DELETE_SENT
def start(softlayer_client, retry_interval_secs, **_): vm_details = _get_vm_details() instance_id = vm_details[constants.INSTANCE_ID] vm_details_to_print = _get_vm_details_for_print(vm_details) # retry if there is an active transaction virtual_guest_service = softlayer_client[constants.VIRTUAL_GUEST] transaction_name = _get_transaction_full_name( get_active_transaction(virtual_guest_service, instance_id)) if transaction_name: return ctx.operation.retry( message='host [{0}] has active transaction [{1}], ' 'waiting for transactions to end.. ' 'operation will be retried'.format(vm_details_to_print, transaction_name), retry_after=retry_interval_secs) # starting the server if the state is not RUNNING state = virtual_guest_service.getPowerState( id=instance_id)[constants.POWER_STATE_KEY_NAME] if state != constants.RUNNING_STATE: ctx.logger.info('starting server [{0}]'.format(vm_details_to_print)) ok = virtual_guest_service.powerOn(id=instance_id) state = virtual_guest_service.getPowerState( id=instance_id)[constants.POWER_STATE_KEY_NAME] if ok and state == constants.RUNNING_STATE: ctx.logger.info('server [{0}] was started successfully'.format( vm_details_to_print)) ctx.instance.runtime_properties[ constants.STATUS] = constants.STARTED else: raise NonRecoverableError( 'failed to start server [{0}], power state: {1}'.format( vm_details_to_print, state)) else: # state is RUNNING ctx.logger.info( 'server [{0}] ' 'is already running, ' ' start will not be performed.'.format(vm_details_to_print)) vm = ExtendedVSManager(softlayer_client).get_instance(instance_id) if not _retrieve_vm_credentials(vm): raise NonRecoverableError( 'Login information is not available for VM [{0}]'.format( vm_details_to_print))
def os_reload(softlayer_client, post_uri, ssh_keys, **_): vs_manager = ExtendedVSManager(softlayer_client) instance_id = ctx.instance.runtime_properties[constants.INSTANCE_ID] ctx.logger.info('os reload for server with' ' instance Id {0}'.format(instance_id)) vs_manager.reload_instance(instance_id, post_uri=post_uri, ssh_keys=ssh_keys) ctx.logger.info('waiting for os reload on ' 'server with Id {0} to become' ' ready...'.format(instance_id)) vs_manager.wait_for_ready(instance_id, pending=True) vm = vs_manager.get_instance(instance_id=instance_id) is_active = \ vm is not None \ and vm[constants.STATUS][constants.STATUS_KEY_NAME] == constants.ACTIVE if is_active: vs_ready = _retrieve_vm_credentials(vm) ctx.logger.info('VM [{0}] is {1} ready'.format( vm['fullyQualifiedDomainName'], ('' if vs_ready else 'not')))
def os_reload(softlayer_client, post_uri, ssh_keys, **_): vs_manager = ExtendedVSManager(softlayer_client) instance_id = ctx.instance.runtime_properties[constants.INSTANCE_ID] ctx.logger.info('os reload for server with' ' instance Id {0}'.format(instance_id)) vs_manager.reload_instance(instance_id, post_uri=post_uri, ssh_keys=ssh_keys) ctx.logger.info('waiting for os reload on ' 'server with Id {0} to become' ' ready...'.format(instance_id)) vs_manager.wait_for_ready(instance_id, pending=True) vm = vs_manager.get_instance(instance_id=instance_id) is_active = \ vm is not None \ and vm[constants.STATUS][constants.STATUS_KEY_NAME] == constants.ACTIVE if is_active: vs_ready = _retrieve_vm_credentials(vm) ctx.logger.info('VM [{0}] is {1} ready'. format(vm['fullyQualifiedDomainName'], ('' if vs_ready else 'not')))