def execute_script_in_nodes(config, vapp, password, script, nodes, check_tools=True): vs = VSphere( config['vcs']['host'], config['vcs']['username'], config['vcs']['password'], port=int(config['vcs']['port'])) vs.connect() all_results = [] for node in nodes: LOGGER.debug('running script on %s' % node.get('name')) print('running script on %s' % node.get('name')) 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.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() LOGGER.debug(result[0]) LOGGER.debug(result_stderr) LOGGER.debug(result_stdout) print(result[0]) print(result_stderr) print(result_stdout) all_results.append(result) return all_results
def get_file_from_nodes(config, vapp, password, file_name, nodes, check_tools=True): vs = VSphere( config['vcs']['host'], config['vcs']['username'], config['vcs']['password'], port=int(config['vcs']['port'])) vs.connect() all_results = [] for node in nodes: LOGGER.debug('getting file from node %s' % node.get('name')) 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
url = os.environ['VGR_URL'] verify_ssl_certs = False moid = os.environ['VGR_MOID'] guest_user = os.environ['VGR_GUEST_USER'] guest_password = os.environ['VGR_GUEST_PASSWORD'] if len(sys.argv) < 2: print('usage: run-cmd.py \'program to run on guest with arguments\'') sys.exit() vgr_command = sys.argv[1] tokens = url.split(':') vc_user = tokens[0] tokens = tokens[1].split('@') vc_host = tokens[-1] vc_password = '' for token in tokens[:-1]: if len(vc_password) > 0: vc_password += '@' vc_password += token vs = VSphere(vc_host, vc_user, vc_password, verify=verify_ssl_certs) vs.connect() vm = vs.get_vm_by_moid(moid) result = vs.execute_program_in_guest(vm, guest_user, guest_password, vgr_command, get_output=False, wait_for_completion=False) print(result)
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() moid = vapp.get_vm_moid(template['temp_vapp']) vs = VSphere( config['vcs']['host'], config['vcs']['username'], config['vcs']['password'], port=int(config['vcs']['port'])) vs.connect() 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 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)