def _register_def_schema(client: Client, msg_update_callback=utils.NullPrinter(), log_wire=False): """Register defined entity interface and defined entity type. If vCD api version is >= 35, register the vCD api version based defined entity interface and defined entity type. Read the schema present in the location dictated by api version to register the defined entity type. :param pyvcloud.vcd.client.Client client: :param utils.ConsoleMessagePrinter msg_update_callback: Callback object. :param bool log_wire: wire logging enabled """ msg = "Registering defined entity schema" msg_update_callback.info(msg) INSTALL_LOGGER.debug(msg) logger_wire = SERVER_CLOUDAPI_WIRE_LOGGER if log_wire else NULL_LOGGER cloudapi_client = vcd_utils.get_cloudapi_client_from_vcd_client( client=client, # noqa: E501 logger_debug=INSTALL_LOGGER, # noqa: E501 logger_wire=logger_wire) # noqa: E501 schema_file = None try: def_utils.raise_error_if_def_not_supported(cloudapi_client) schema_svc = def_schema_svc.DefSchemaService(cloudapi_client) keys_map = def_utils.MAP_API_VERSION_TO_KEYS[float( client.get_api_version())] # noqa: E501 defKey = def_utils.DefKey native_interface = def_models.\ DefInterface(name=keys_map[defKey.INTERFACE_NAME], vendor=keys_map[defKey.VENDOR], nss=keys_map[defKey.INTERFACE_NSS], version=keys_map[defKey.INTERFACE_VERSION], # noqa: E501 readonly=False) msg = "" try: schema_svc.get_interface(native_interface.get_id()) msg = "defined entity interface already exists." \ " Skipping defined entity interface creation" except HTTPError: # TODO handle this part only if the interface was not found native_interface = schema_svc.create_interface(native_interface) msg = "Successfully created defined entity interface" msg_update_callback.general(msg) INSTALL_LOGGER.debug(msg) # TODO stop-gap fix - find efficient way to import schema import importlib import importlib.resources as pkg_resources schema_module = importlib.import_module( f'{def_utils.DEF_SCHEMA_DIRECTORY}.{keys_map[defKey.ENTITY_TYPE_SCHEMA_VERSION]}' ) # noqa: E501 schema_file = pkg_resources.open_text( schema_module, def_utils.DEF_ENTITY_TYPE_SCHEMA_FILE) # noqa: E501 native_entity_type = def_models.\ DefEntityType(name=keys_map[defKey.ENTITY_TYPE_NAME], description='', vendor=keys_map[defKey.VENDOR], nss=keys_map[defKey.ENTITY_TYPE_NSS], version=keys_map[defKey.ENTITY_TYPE_VERSION], schema=json.load(schema_file), interfaces=[native_interface.get_id()], readonly=False) msg = "" try: schema_svc.get_entity_type(native_entity_type.get_id()) msg = "defined entity type already exists." \ " Skipping defined entity type creation" except HTTPError: # TODO handle this part only if the entity type was not found native_entity_type = schema_svc.create_entity_type( native_entity_type) # noqa: E501 msg = "Successfully registered defined entity type" msg_update_callback.general(msg) INSTALL_LOGGER.debug(msg) except cse_exception.DefNotSupportedException: msg = "Skipping defined entity type and defined entity interface" \ " registration" msg_update_callback.general(msg) INSTALL_LOGGER.debug(msg) except Exception as e: msg = f"Error occured while registering defined entity schema: {str(e)}" # noqa: E501 msg_update_callback.error(msg) INSTALL_LOGGER.error(msg) raise (e) finally: try: schema_file.close() except Exception: pass
def _setup_placement_policies(client, policy_list, msg_update_callback=utils.NullPrinter(), log_wire=False): """ Create placement policies for each cluster type. Create the global pvdc compute policy if not present and create placement policy for each policy in the policy list. This should be done only for vcd api version >= 35 (zeus) :parma client vcdClient.Client :param policy_list str[] """ msg = "Setting up placement policies for cluster types" msg_update_callback.info(msg) INSTALL_LOGGER.debug(msg) computePolicyManager = cpm.ComputePolicyManager(client, log_wire=log_wire) pvdc_compute_policy = None try: try: pvdc_compute_policy = computePolicyManager.get_pvdc_compute_policy( server_constants.CSE_GLOBAL_PVDC_COMPUTE_POLICY_NAME) msg = "Skipping global PVDC compute policy creation. Policy already exists" # noqa: E501 msg_update_callback.general(msg) INSTALL_LOGGER.debug(msg) except EntityNotFoundException: msg = "Creating global PVDC compute policy" msg_update_callback.general(msg) INSTALL_LOGGER.debug(msg) pvdc_compute_policy = computePolicyManager.add_pvdc_compute_policy( server_constants.CSE_GLOBAL_PVDC_COMPUTE_POLICY_NAME, server_constants.CSE_GLOBAL_PVDC_COMPUTE_POLICY_DESCRIPTION) for policy in policy_list: try: computePolicyManager.get_vdc_compute_policy( policy, is_placement_policy=True) # noqa: E501 msg = f"Skipping creating VDC placement policy '{policy}'. Policy already exists" # noqa: E501 msg_update_callback.general(msg) INSTALL_LOGGER.debug(msg) except EntityNotFoundException: msg = f"Creating placement policy '{policy}'" msg_update_callback.general(msg) INSTALL_LOGGER.debug(msg) computePolicyManager.add_vdc_compute_policy( policy, pvdc_compute_policy_id=pvdc_compute_policy['id'] ) # noqa: E501 except cse_exception.GlobalPvdcComputePolicyNotSupported: msg = "Global PVDC compute policies are not supported." \ "Skipping placement policy creation." msg_update_callback.general(msg) INSTALL_LOGGER.debug(msg)
def _customize_vm(ctx, config, vapp, vm_name, cust_script, is_photon=False): """Customizes a VM in a VApp using the customization script @cust_script. :param click.core.Context ctx: click context object. Needed to pass to stdout. :param dict config: CSE config. :param pyvcloud.vcd.vapp.VApp vapp: :param str vm_name: :param str cust_script: the customization script to run on :param bool is_photon: True if the vapp was instantiated from a 'photon' ova file, False otherwise (False is safe even if the vapp is photon-based). :raises Exception: if unable to execute the customization script in VSphere. """ callback = vgr_callback(prepend_msg='Waiting for guest tools, status: "') if not is_photon: vs = get_vsphere(config, vapp, vm_name, logger=LOGGER) wait_until_tools_ready(vapp, vs, callback=callback) vapp.reload() task = vapp.shutdown() stdout(task, ctx=ctx) vapp.reload() task = vapp.power_on() stdout(task, ctx=ctx) vapp.reload() vs = get_vsphere(config, vapp, vm_name, logger=LOGGER) wait_until_tools_ready(vapp, vs, callback=callback) password_auto = vapp.get_admin_password(vm_name) try: result = vs.execute_script_in_guest(vs.get_vm_by_moid( vapp.get_vm_moid(vm_name)), 'root', password_auto, cust_script, target_file=None, wait_for_completion=True, wait_time=10, get_output=True, delete_script=True, callback=vgr_callback()) except Exception as err: # TODO replace raw exception with specific exception # unsure all errors execute_script_in_guest can result in # Docker TLS handshake timeout can occur when internet is slow click.secho("Failed VM customization. Check CSE install log", fg='red') LOGGER.error(f"Failed VM customization with error: f{err}", exc_info=True) raise if len(result) > 0: msg = f'Result: {result}' click.echo(msg) LOGGER.debug(msg) result_stdout = result[1].content.decode() result_stderr = result[2].content.decode() msg = 'stderr:' click.echo(msg) LOGGER.debug(msg) if len(result_stderr) > 0: click.echo(result_stderr) LOGGER.debug(result_stderr) msg = 'stdout:' click.echo(msg) LOGGER.debug(msg) if len(result_stdout) > 0: click.echo(result_stdout) LOGGER.debug(result_stdout) if len(result) == 0 or result[0] != 0: msg = "Failed VM customization" click.secho(f"{msg}. Check CSE install log", fg='red') LOGGER.error(msg, exc_info=True) # TODO replace raw exception with specific exception raise Exception(msg)