def execute_script_in_nodes(config, vapp, password, script, nodes, check_tools=True, wait=True): all_results = [] for node in nodes: if 'chpasswd' in script: p = re.compile(':.*\"') debug_script = p.sub(':***\"', script) else: debug_script = script LOGGER.debug('will try to execute script on %s:\n%s' % (node.get('name'), debug_script)) vs = get_vsphere(config, vapp, node.get('name')) vs.connect() moid = vapp.get_vm_moid(node.get('name')) vm = vs.get_vm_by_moid(moid) if check_tools: LOGGER.debug('waiting for tools on %s' % node.get('name')) vs.wait_until_tools_ready( vm, sleep=5, callback=wait_for_tools_ready_callback) wait_until_ready_to_exec(vs, vm, password) LOGGER.debug('about to execute script on %s, wait=%s' % (node.get('name'), wait)) if wait: result = vs.execute_script_in_guest( vm, 'root', password, script, target_file=None, wait_for_completion=True, wait_time=10, get_output=True, delete_script=True, callback=wait_for_guest_execution_callback) result_stdout = result[1].content.decode() result_stderr = result[2].content.decode() else: result = vs.execute_script_in_guest( vm, 'root', password, script, target_file=None, wait_for_completion=False, get_output=False, delete_script=False, callback=wait_for_guest_execution_callback) result_stdout = '' result_stderr = '' LOGGER.debug(result[0]) LOGGER.debug(result_stderr) LOGGER.debug(result_stdout) all_results.append(result) return all_results
def get_file_from_nodes(config, vapp, password, file_name, nodes, check_tools=True): all_results = [] for node in nodes: LOGGER.debug('getting file from node %s' % node.get('name')) vs = get_vsphere(config, vapp, node.get('name')) vs.connect() moid = vapp.get_vm_moid(node.get('name')) vm = vs.get_vm_by_moid(moid) if check_tools: vs.wait_until_tools_ready( vm, sleep=5, callback=wait_for_tools_ready_callback) wait_until_ready_to_exec(vs, vm, password) result = vs.download_file_from_guest(vm, 'root', password, file_name) all_results.append(result) return all_results
def create_template(ctx, config, client, org, vdc_resource, catalog, no_capture, template): ctx.obj = {} ctx.obj['client'] = client try: source_ova_item = org.get_catalog_item(config['broker']['catalog'], template['source_ova_name']) except Exception: source_ova_item = upload_source_ova(config, client, org, template) click.secho('Find source ova \'%s\': %s' % (template['source_ova_name'], bool_to_msg(source_ova_item is not None))) if source_ova_item is None: return None item_id = source_ova_item.get('id') flag = False while True: q = client.get_typed_query( 'adminCatalogItem', query_result_format=QueryResultFormat.ID_RECORDS, qfilter='id==%s' % item_id) records = list(q.execute()) if records[0].get('status') == 'RESOLVED': if flag: click.secho('done', fg='blue') break else: if flag: click.secho('.', nl=False, fg='green') else: click.secho('Waiting for upload to complete...', nl=False, fg='green') flag = True time.sleep(5) vdc = VDC(client, resource=vdc_resource) try: vapp_resource = vdc.get_vapp(template['temp_vapp']) except Exception: vapp_resource = None if vapp_resource is None: click.secho('Creating vApp template \'%s\'' % template['temp_vapp'], fg='green') init_script = get_data_file('init-%s.sh' % template['name']) vapp_resource = vdc.instantiate_vapp( template['temp_vapp'], catalog.get('name'), template['source_ova_name'], network=config['broker']['network'], fence_mode='bridged', ip_allocation_mode=config['broker']['ip_allocation_mode'], deploy=True, power_on=True, memory=template['mem'], cpu=template['cpu'], password=None, cust_script=init_script, accept_all_eulas=True, vm_name=template['temp_vapp'], hostname=template['temp_vapp'], storage_profile=config['broker']['storage_profile']) stdout(vapp_resource.Tasks.Task[0], ctx) vapp = VApp(client, resource=vapp_resource) vapp.reload() vs = get_vsphere(config, vapp, template['temp_vapp']) vs.connect() moid = vapp.get_vm_moid(template['temp_vapp']) vm = vs.get_vm_by_moid(moid) vs.wait_until_tools_ready(vm, sleep=5, callback=wait_for_tools_ready_callback) click.secho('Customizing vApp template \'%s\'' % template['temp_vapp'], fg='green') vapp.reload() password_auto = vapp.get_admin_password(template['temp_vapp']) cust_script = get_data_file('cust-%s.sh' % template['name']) result = vs.execute_script_in_guest( vm, 'root', password_auto, cust_script, target_file=None, wait_for_completion=True, wait_time=10, get_output=True, delete_script=True, callback=wait_for_guest_execution_callback) click.secho('Result: %s' % result, fg='green') result_stdout = result[1].content.decode() result_stderr = result[2].content.decode() click.secho('stderr:') if len(result_stderr) > 0: click.secho(result_stderr, err=True) click.secho('stdout:') if len(result_stdout) > 0: click.secho(result_stdout, err=False) if result[0] != 0: raise Exception('Failed customizing VM') if not no_capture: capture_as_template(ctx, config, vapp_resource, org, catalog, template) if template['cleanup']: click.secho('Deleting vApp template \'%s\' ' % template['temp_vapp'], fg='green') vdc.reload() task = vdc.delete_vapp(template['temp_vapp'], force=True) stdout(task, ctx)
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)
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) 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) 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: # TODO replace raw exception with specific exception # unsure what exception execute_script_in_guest can throw LOGGER.error(traceback.format_exc()) click.secho(traceback.format_exc(), fg='red') raise if len(result) > 0: click.echo(f'Result: {result}') result_stdout = result[1].content.decode() result_stderr = result[2].content.decode() click.secho('stderr:') if len(result_stderr) > 0: click.secho(result_stderr, err=True) click.secho('stdout:') if len(result_stdout) > 0: click.secho(result_stdout, err=False) if len(result) == 0 or result[0] != 0: msg = 'Failed to customize VM' click.secho(msg, fg='red') # TODO replace raw exception with specific exception raise Exception(msg)