def check_config(config_file_name, template='*'): click.secho('Validating CSE on vCD from file: %s' % config_file_name) config = get_config(config_file_name) validate_broker_config_elements(config['broker']) amqp = config['amqp'] credentials = pika.PlainCredentials(amqp['username'], amqp['password']) parameters = pika.ConnectionParameters( amqp['host'], amqp['port'], '/', credentials, ssl=amqp['ssl']) connection = pika.BlockingConnection(parameters) click.echo('Connected to AMQP server (%s:%s): %s' % (amqp['host'], amqp['port'], bool_to_msg(connection.is_open))) connection.close() v = VSphere( config['vcs']['host'], config['vcs']['username'], config['vcs']['password'], port=int(config['vcs']['port'])) v.connect() click.echo('Connected to vCenter Server as %s ' '(%s:%s): %s' % (config['vcs']['username'], config['vcs']['host'], config['vcs']['port'], bool_to_msg(True))) if not config['vcd']['verify']: click.secho( 'InsecureRequestWarning: ' 'Unverified HTTPS request is being made. ' 'Adding certificate verification is strongly ' 'advised.', fg='yellow', err=True) requests.packages.urllib3.disable_warnings() client = Client( config['vcd']['host'], api_version=config['vcd']['api_version'], verify_ssl_certs=config['vcd']['verify'], log_file='cse.log', log_headers=True, log_bodies=True) client.set_credentials( BasicLoginCredentials(config['vcd']['username'], 'System', config['vcd']['password'])) click.echo('Connected to vCloud Director as system ' 'administrator (%s:%s): %s' % (config['vcd']['host'], config['vcd']['port'], bool_to_msg(True))) click.secho('Validating \'%s\' service broker' % config['broker']['type']) if config['broker']['type'] == 'default': validate_broker_config_content(config, client, template) return config
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 _validate_vcd_and_vcs_config( vcd_dict, vcs, msg_update_callback=NullPrinter(), log_file=None, log_wire=False ): """Ensure that 'vcd' and vcs' section of config are correct. Checks that * 'vcd' and 'vcs' section of config have correct keys and value types. * vCD and all registered VCs in vCD are accessible. * api version specified for vcd is supported by CSE. :param dict vcd_dict: 'vcd' section of config file as a dict. :param list vcs: 'vcs' section of config file as a list of dicts. :param utils.ConsoleMessagePrinter msg_update_callback: Callback object. :param str log_file: log_file for pyvcloud wire log. :param bool log_wire: If pyvcloud requests should be logged. :raises KeyError: if @vcd_dict or a vc in @vcs has missing or extra properties. :raises TypeError: if the value type for a @vcd_dict or vc property is incorrect. :raises ValueError: if vCD has a VC that is not listed in the config file. """ check_keys_and_value_types(vcd_dict, SAMPLE_VCD_CONFIG['vcd'], location="config file 'vcd' section", msg_update_callback=msg_update_callback) if not vcd_dict['verify']: msg_update_callback.general( 'InsecureRequestWarning: Unverified HTTPS request is ' 'being made. Adding certificate verification is ' 'strongly advised.' ) requests.packages.urllib3.disable_warnings() client = None try: _validate_vcd_url_scheme(vcd_dict['host']) client = Client( vcd_dict['host'], verify_ssl_certs=vcd_dict['verify'], log_file=log_file, log_requests=log_wire, log_headers=log_wire, log_bodies=log_wire ) client.set_credentials( BasicLoginCredentials( vcd_dict['username'], SYSTEM_ORG_NAME, vcd_dict['password'] ) ) msg_update_callback.general( "Connected to vCloud Director " f"({vcd_dict['host']}:{vcd_dict['port']})" ) for index, vc in enumerate(vcs, 1): check_keys_and_value_types( vc, SAMPLE_VCS_CONFIG['vcs'][0], location=f"config file 'vcs' section, vc #{index}", msg_update_callback=msg_update_callback ) # Check that all registered VCs in vCD are listed in config file platform = Platform(client) config_vc_names = [vc['name'] for vc in vcs] for platform_vc in platform.list_vcenters(): platform_vc_name = platform_vc.get('name') if platform_vc_name not in config_vc_names: raise ValueError( f"vCenter '{platform_vc_name}' registered in " "vCD but not found in config file" ) # Check that all VCs listed in config file are registered in vCD for vc in vcs: vcenter = platform.get_vcenter(vc['name']) if not (hasattr(vcenter, 'IsConnected') and vcenter.IsConnected): msg = f"vCenter Server '{vc['name']}' not available" msg_update_callback.info(msg) continue vsphere_url = urlparse(vcenter.Url.text) vsphere_url_port = vsphere_url.port if vsphere_url_port: v = VSphere( vsphere_url.hostname, vc['username'], vc['password'], vsphere_url.port ) else: v = VSphere( vsphere_url.hostname, vc['username'], vc['password'] ) v.connect() msg = f"Connected to vCenter Server '{vc['name']}' as " \ f"'{vc['username']}' ({vsphere_url.hostname}" if vsphere_url_port: msg += f":{vsphere_url.port}" msg += ")" msg_update_callback.general(msg) finally: if client is not None: client.logout()
def check_config(config_file_name, template=None): click.secho('Validating CSE on vCD from file: %s' % config_file_name) if sys.version_info.major >= 3 and sys.version_info.minor >= 6: python_valid = True else: python_valid = False click.echo('Python version >= 3.6 (installed: %s.%s.%s): %s' % (sys.version_info.major, sys.version_info.minor, sys.version_info.micro, bool_to_msg(python_valid))) if not python_valid: raise Exception('Python version not supported') config = get_config(config_file_name) validate_broker_config_elements(config['broker']) amqp = config['amqp'] credentials = pika.PlainCredentials(amqp['username'], amqp['password']) parameters = pika.ConnectionParameters(amqp['host'], amqp['port'], '/', credentials, ssl=amqp['ssl']) connection = pika.BlockingConnection(parameters) click.echo('Connected to AMQP server (%s:%s): %s' % (amqp['host'], amqp['port'], bool_to_msg(connection.is_open))) connection.close() if not config['vcd']['verify']: click.secho( 'InsecureRequestWarning: ' 'Unverified HTTPS request is being made. ' 'Adding certificate verification is strongly ' 'advised.', fg='yellow', err=True) requests.packages.urllib3.disable_warnings() client = Client(config['vcd']['host'], api_version=config['vcd']['api_version'], verify_ssl_certs=config['vcd']['verify'], log_file='cse-check.log', log_headers=True, log_bodies=True) client.set_credentials( BasicLoginCredentials(config['vcd']['username'], 'System', config['vcd']['password'])) click.echo( 'Connected to vCloud Director as system ' 'administrator (%s:%s): %s' % (config['vcd']['host'], config['vcd']['port'], bool_to_msg(True))) platform = Platform(client) for vc in platform.list_vcenters(): found = False for config_vc in config['vcs']: if vc.get('name') == config_vc.get('name'): found = True break if not found: raise Exception('vCenter \'%s\' defined in vCloud Director ' 'but not in CSE config file' % vc.get('name')) return None for vc in config['vcs']: vcenter = platform.get_vcenter(vc['name']) vsphere_url = urlparse(vcenter.Url.text) v = VSphere(vsphere_url.hostname, vcenter.Username.text, vc['password'], vsphere_url.port) v.connect() click.echo('Connected to vCenter Server %s as %s ' '(%s:%s): %s' % (vc['name'], vcenter.Username.text, vsphere_url.hostname, vsphere_url.port, bool_to_msg(True))) if template is None: pass else: click.secho('Validating \'%s\' service broker' % config['broker']['type']) if config['broker']['type'] == 'default': validate_broker_config_content(config, client, template) return config
def validate_vcd_and_vcs_config(vcd_dict, vcs): """Ensures that 'vcd' and vcs' section of config are correct. Checks that 'vcd' and 'vcs' section of config have correct keys and value types. Also checks that vCD and all registered VCs in vCD are accessible. :param dict vcd_dict: 'vcd' section of config file as a dict. :param list vcs: 'vcs' section of config file as a list of dicts. :raises KeyError: if @vcd_dict or a vc in @vcs has missing or extra properties. :raises: ValueError: if the value type for a @vcd_dict or vc property is incorrect, or if vCD has a VC that is not listed in the config file. """ check_keys_and_value_types(vcd_dict, SAMPLE_VCD_CONFIG['vcd'], location="config file 'vcd' section") if not vcd_dict['verify']: click.secho( 'InsecureRequestWarning: Unverified HTTPS request is ' 'being made. Adding certificate verification is ' 'strongly advised.', fg='yellow', err=True) requests.packages.urllib3.disable_warnings() client = None try: client = Client(vcd_dict['host'], api_version=vcd_dict['api_version'], verify_ssl_certs=vcd_dict['verify']) client.set_credentials( BasicLoginCredentials(vcd_dict['username'], SYSTEM_ORG_NAME, vcd_dict['password'])) click.secho( f"Connected to vCloud Director " f"({vcd_dict['host']}:{vcd_dict['port']})", fg='green') for index, vc in enumerate(vcs, 1): check_keys_and_value_types(vc, SAMPLE_VCS_CONFIG['vcs'][0], location=f"config file 'vcs' section, " f"vc #{index}") # Check that all registered VCs in vCD are listed in config file platform = Platform(client) config_vc_names = [vc['name'] for vc in vcs] for platform_vc in platform.list_vcenters(): platform_vc_name = platform_vc.get('name') if platform_vc_name not in config_vc_names: raise ValueError(f"vCenter '{platform_vc_name}' registered in " f"vCD but not found in config file") # Check that all VCs listed in config file are registered in vCD for vc in vcs: vcenter = platform.get_vcenter(vc['name']) vsphere_url = urlparse(vcenter.Url.text) v = VSphere(vsphere_url.hostname, vc['username'], vc['password'], vsphere_url.port) v.connect() click.secho( f"Connected to vCenter Server '{vc['name']}' as " f"'{vc['username']}' ({vsphere_url.hostname}:" f"{vsphere_url.port})", fg='green') finally: if client is not None: client.logout()
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)
def _validate_vcd_and_vcs_config(vcd_dict, vcs, msg_update_callback=None): """Ensure that 'vcd' and vcs' section of config are correct. Checks that 'vcd' and 'vcs' section of config have correct keys and value types. Also checks that vCD and all registered VCs in vCD are accessible. :param dict vcd_dict: 'vcd' section of config file as a dict. :param list vcs: 'vcs' section of config file as a list of dicts. :param utils.ConsoleMessagePrinter msg_update_callback: Callback object that writes messages onto console. :raises KeyError: if @vcd_dict or a vc in @vcs has missing or extra properties. :raises TypeError: if the value type for a @vcd_dict or vc property is incorrect. :raises ValueError: if vCD has a VC that is not listed in the config file. """ check_keys_and_value_types(vcd_dict, SAMPLE_VCD_CONFIG['vcd'], location="config file 'vcd' section", msg_update_callback=msg_update_callback) if not vcd_dict['verify']: if msg_update_callback: msg_update_callback.general( 'InsecureRequestWarning: Unverified HTTPS request is ' 'being made. Adding certificate verification is ' 'strongly advised.') requests.packages.urllib3.disable_warnings() client = None try: # TODO() we get an error during client initialization if the specified # logfile points to the directory which doesn't exist. This issue # should be fixed in pyvcloud, where the logging setup creates # directories used in the log filepath if they do not exist yet. setup_log_file_directory() client = Client(vcd_dict['host'], api_version=vcd_dict['api_version'], verify_ssl_certs=vcd_dict['verify'], log_file=SERVER_DEBUG_WIRELOG_FILEPATH, log_requests=True, log_headers=True, log_bodies=True) client.set_credentials( BasicLoginCredentials(vcd_dict['username'], SYSTEM_ORG_NAME, vcd_dict['password'])) if msg_update_callback: msg_update_callback.general( "Connected to vCloud Director " f"({vcd_dict['host']}:{vcd_dict['port']})") for index, vc in enumerate(vcs, 1): check_keys_and_value_types( vc, SAMPLE_VCS_CONFIG['vcs'][0], location=f"config file 'vcs' section, vc #{index}", msg_update_callback=msg_update_callback) # Check that all registered VCs in vCD are listed in config file platform = Platform(client) config_vc_names = [vc['name'] for vc in vcs] for platform_vc in platform.list_vcenters(): platform_vc_name = platform_vc.get('name') if platform_vc_name not in config_vc_names: raise ValueError(f"vCenter '{platform_vc_name}' registered in " f"vCD but not found in config file") # Check that all VCs listed in config file are registered in vCD for vc in vcs: vcenter = platform.get_vcenter(vc['name']) vsphere_url = urlparse(vcenter.Url.text) v = VSphere(vsphere_url.hostname, vc['username'], vc['password'], vsphere_url.port) v.connect() if msg_update_callback: msg_update_callback.general( f"Connected to vCenter Server '{vc['name']}' as " f"'{vc['username']}' ({vsphere_url.hostname}:" f"{vsphere_url.port})") finally: if client is not None: client.logout()