Пример #1
0
def capture_vapp(client, context, capture_info):
    logging.debug("__INIT__capture_vapp [%s]", capture_info)
    vapp_name = capture_info.vapp_name
    vdc_name = capture_info.vdc_name
    catalog_name = capture_info.catalog_name
    item_name = capture_info.item_name
    desc = capture_info.description
    customize_on_instantiate = capture_info.customize_on_instantiate

    result = catalog_item_pb2.CaptureVAppResult()
    try:
        logged_in_org = client.get_org()
        org = Org(client, resource=logged_in_org)
        v = org.get_vdc(vdc_name)
        if v is None:
            raise errors.VDCNotFoundError(vappInfo.vdc)
        vdc = VDC(client, href=v.get('href'))
        vapp = vdc.get_vapp(vapp_name)
        logging.info(vapp.get('href'))
        catalog = org.get_catalog(catalog_name)
        org.capture_vapp(catalog_resource=catalog,
                         vapp_href=vapp.get('href'),
                         catalog_item_name=item_name,
                         description=desc,
                         customize_on_instantiate=customize_on_instantiate)
        result.captured = True
        logging.debug("__DONE__capture_vapp [%s] , [%s] , [%s]  ", vapp_name,
                      vdc_name, catalog_name)
        return result
    except Exception as e:
        error_message = 'ERROR.. capture vapp failed  {0} {1} {2}'.format(
            vapp_name, vdc_name, catalog_name)
        logging.warn(error_message, e)
        context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
        context.set_details(error_message)
def create(client, context, vappInfo):
    logging.debug('__INIT__vapp_create [ {0} ]'.format(vappInfo))
    cresult = vapp_pb2.CreateVAppResult()
    cresult.created = False
    #cresult.in_vapp_info=vapp_pb2.CreateVAppInfo()
    cresult.in_vapp_info.CopyFrom(vappInfo)
    try:
        logged_in_org = client.get_org()
        org = Org(client, resource=logged_in_org)
        v = org.get_vdc(vappInfo.vdc)
        if v is None:
            raise errors.VDCNotFoundError(vappInfo.vdc)
        vdc = VDC(client, href=v.get('href'))

        logging.info("__LOG__vapp_create_calling instantiate_vapp............")

        network = None
        memory = None
        storage_profile = None
        accept_all_eulas = False
        power_on = False
        if vappInfo.network:
            network = vappInfo.network
        if vappInfo.memory:
            memory = vappInfo.memory
        if vappInfo.storage_profile:
            storage_profile = vappInfo.storage_profile
        if vappInfo.accept_all_eulas:
            accept_all_eulas = vappInfo.accept_all_eulas
        if vappInfo.power_on:
            power_on = vappInfo.power_on

        logging.info(
            "__LOG__ CREATE VAPP Params - MEMORY = [%s] NETWORK = [%s] storage_profile =[%s] ",
            memory, network, storage_profile)

        result = vdc.instantiate_vapp(name=vappInfo.name,
                                      catalog=vappInfo.catalog_name,
                                      template=vappInfo.template_name,
                                      network=network,
                                      memory=memory,
                                      cpu=vappInfo.cpu,
                                      power_on=power_on,
                                      storage_profile=storage_profile,
                                      accept_all_eulas=accept_all_eulas)

        task = client.get_task_monitor().wait_for_status(
            task=result.Tasks.Task[0],
            timeout=60,
            poll_frequency=2,
            fail_on_statuses=None,
            expected_target_statuses=[
                TaskStatus.SUCCESS, TaskStatus.ABORTED, TaskStatus.ERROR,
                TaskStatus.CANCELED
            ],
            callback=task_callback)

        st = task.get('status')
        if st == TaskStatus.SUCCESS.value:
            cresult.created = True
        else:
            raise errors.VCDVappCreationError(
                etree.tostring(task, pretty_print=True))

        vapp = vdc.get_vapp(vappInfo.name)
        if vappInfo.power_on is True:
            logging.info("Powering on [Vapp %v]".format(vappInfo.name))
            vapp.power_on()
        else:
            logging.info("Powering off [Vapp %v]".format(vappInfo.name))
            vapp.undeploy()

    except Exception as e:

        error_message = 'ERROR.. Not Created VApp {0}  {1}'.format(
            vappInfo.name, str(e))
        logging.warn(error_message, e)
        context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
        context.set_details(error_message)
    return cresult