def csv_reader(component, subcommand): """ Reads all component entities data using hammer csv output and returns the dict representation of all the entities. Representation: {component_name: [{comp1_name:comp1, comp1_id:1}, {comp2_name:comp2, comp2_ip:192.168.0.1}] } e.g: {'host':[{name:host1.ab.com, id:10}, {name:host2.xz.com, ip:192.168.0.1}]} :param string component: Satellite component name. e.g host, capsule :param string subcommand: subcommand for above component. e.g list, info :returns dict: The dict repr of hammer csv output of given command """ comp_dict = {} entity_list = [] sat_host = get_setup_data()['sat_host'] set_hammer_config() data = execute(hammer, '{0} {1}'.format(component, subcommand), 'csv', host=sat_host)[sat_host] csv_read = csv.DictReader(data.lower().split('\n')) for row in csv_read: entity_list.append(row) comp_dict[component] = entity_list return comp_dict
def template_reader(template_type, template_id, sat_host=None): """Hammer read and returns the template dump of template_id :param str template_type: The satellite template type :param str template_id: The template id :return str: The template content as string """ set_hammer_config() sat_host = sat_host or get_setup_data()['sat_host'] template_dump = execute( hammer, f'{template_type} dump --id {template_id}', 'base', host=sat_host )[sat_host] return template_dump
def template_reader(template_type, template_id): """Hammer read and returns the template dump of template_id :param str template_type: The satellite template type :param str template_id: The template id :return str: The template content as string """ set_hammer_config() sat_host = get_setup_data()['sat_host'] template_dump = execute( hammer, '{0} dump --id {1}'.format(template_type, template_id), 'base', host=sat_host )[sat_host] return template_dump
def set_api_server_config(sat_host=None, user=None, passwd=None, verify=None): """Sets ServerConfig configuration required by nailgun to read entities :param str user: The web username of satellite user 'admin' by default if not provided :param str passwd: The web password of satellite user 'changeme' by default if not provided :param bool verify: The ssl verification to connect to satellite host False by default if not provided """ sat_host = sat_host or get_setup_data()['sat_host'] auth = (user or 'admin', passwd or 'changeme') url = f'https://{sat_host}' verify = verify or False ServerConfig(auth=auth, url=url, verify=verify).save()
def set_api_server_config(user=None, passwd=None, verify=None): """Sets ServerConfig configuration required by nailgun to read entities :param str user: The web username of satellite user 'admin' by default if not provided :param str passwd: The web password of satellite user 'changeme' by default if not provided :param bool verify: The ssl verification to connect to satellite host False by default if not provided """ sat_host = get_setup_data()['sat_host'] auth = ('admin' if not user else user, 'changeme' if not passwd else passwd) url = 'https://{}'.format(sat_host) verify = False if not verify else verify ServerConfig(auth=auth, url=url, verify=verify).save()
def product_upgrade(product, upgrade_type, satellite=None): """ Used to drive the satellite, Capsule and Content-host upgrade based on their product type and upgrade type :param product: Product can be satellite, capsule, longrun and n-1 1- If product is satellite then upgrade only satellite 2- If product is capsule then upgrade satellite and capsule 3- If product is client then upgrade satellite and client 4- If product is longrun then upgrade satellite, capsule and client 5- If product is n-1 then upgrades only satellite by keeping capsule at last z-stream released version :param upgrade_type: Upgrade_type can be satellite, capsule and client """ def product_upgrade_satellite(sat_host): try: with LogAnalyzer(sat_host): current = execute(get_sat_cap_version, 'sat', host=sat_host)[sat_host] if settings.upgrade.from_version != settings.upgrade.to_version: execute(satellite_upgrade, host=sat_host) else: execute(satellite_upgrade, True, host=sat_host) upgraded = execute(get_sat_cap_version, 'sat', host=sat_host)[sat_host] check_upgrade_compatibility(upgrade_type, current, upgraded) execute(foreman_debug, f'satellite_{sat_host}', host=sat_host) except Exception: execute(foreman_debug, f'satellite_{sat_host}', host=sat_host) raise def product_upgrade_capsule(cap_host): try: with LogAnalyzer(cap_host): current = execute(get_sat_cap_version, 'cap', host=cap_host)[cap_host] if settings.upgrade.from_version != settings.upgrade.to_version: execute(satellite_capsule_upgrade, cap_host, sat_host, host=cap_host) elif settings.upgrade.from_version == settings.upgrade.to_version: execute(satellite_capsule_zstream_upgrade, cap_host, host=cap_host) upgraded = execute(get_sat_cap_version, 'cap', host=cap_host)[cap_host] check_upgrade_compatibility(upgrade_type, current, upgraded) # Generate foreman debug on capsule postupgrade execute(foreman_debug, f'capsule_{cap_host}', host=cap_host) # Execute tasks as post upgrade tier1 tests # are dependent if product == 'longrun': post_upgrade_test_tasks(sat_host, cap_host) except Exception: execute(foreman_debug, f'capsule_{cap_host}', host=cap_host) raise def product_upgrade_client(): clients6 = setup_dict['clients6'] clients7 = setup_dict['clients7'] puppet_clients7 = setup_dict['puppet_clients7'] puppet_clients6 = setup_dict['puppet_clients6'] satellite6_client_upgrade('rhel6', clients6) satellite6_client_upgrade('rhel7', clients7) satellite6_client_upgrade('rhel7', puppet_clients7, puppet=True) satellite6_client_upgrade('rhel6', puppet_clients6, puppet=True) env.disable_known_hosts = True check_necessary_env_variables_for_upgrade(product) logger.info(f'performing upgrade from {settings.upgrade.from_version} TO ' f'{settings.upgrade.to_version}') # Get the setup dict returned by setup_products_for_upgrade setup_dict = get_setup_data(sat_hostname=satellite) sat_host = setup_dict['sat_host'] cap_hosts = setup_dict['capsule_hosts'] pre_upgrade_system_checks(cap_hosts) env['satellite_host'] = sat_host if upgrade_type == "satellite": product_upgrade_satellite(sat_host) elif (product == 'capsule' or product == 'longrun')\ and upgrade_type == 'capsule': for cap_host in cap_hosts: settings.upgrade.capsule_hostname = cap_host product_upgrade_capsule(cap_host) elif (product == 'client' or product == 'longrun') and upgrade_type == 'client': product_upgrade_client() execute(unsubscribe, host=sat_host)
def product_upgrade(product): """Task which upgrades the product. Product is satellite or capsule or client or longrun. If product is satellite then upgrade only satellite If product is capsule then upgrade satellite and capsule If product is client then upgrade satellite and client If product is longrun then upgrade satellite, capsule and client If product is n-1 then upgrades only satellite by keeping capsule at last z-stream released version :param string product: product name wanted to upgrade. Environment Variables necessary to proceed Upgrade: ----------------------------------------------------- FROM_VERSION The satellite/capsule current version to upgrade to latest. e.g '6.1','6.0' TO_VERSION To which Satellite/Capsule version to upgrade. e.g '6.1','6.2' OS The OS Version on which the satellite is installed. e.g 'rhel7','rhel6' Environment variables populated from jenkins: ------------------------------------------------------ RHN_USERNAME Red Hat Network username to register the system. RHN_PASSWORD Red Hat Network password to register the system. RHN_POOLID Optional. Red Hat Network pool ID. Determines what software will be available from RHN BASE_URL URL for the compose repository. CAPSULE_URL The url for capsule repo from latest satellite compose. If CDN, defaults to latest available capsule version TOOLS_URL_RHEL6 The url for rhel6 tools repo from latest satellite compose If CDN, defaults to latest available tools version TOOLS_URL_RHEL7 The url for rhel7 tools repo from latest satellite compose If CDN, defaults to latest available tools version Environment variables required to run upgrade on user's own setup: -------------------------------------------------------------------- SATELLITE_HOSTNAME The Satellite hostname to run upgrade on CAPSULE_HOSTNAME The Satellite hostname to run upgrade on CLIENT6_HOSTS The RHEL6 clients hostnames to run upgrade on CLIENT7_HOSTS The RHEL7 clients hostnames to run upgrade on. CAPSULE_AK Activation Key name attached to the subscription of capsule CLIENT_AK Activation Key name attached to the subscription of client Environment variables required to run upgrade on RHEVM Setup and will be fetched from Jenkins: ---------------------------------------------------------------------- RHEV_SAT_IMAGE The satellite Image from which satellite instance will be created RHEV_SAT_HOST The rhevm satellite hostname to run upgrade on RHEV_CAP_IMAGE The capsule Image from which capsule instance will be created RHEV_CAP_HOST The rhevm capsule hostname to run upgrade on DOCKER_VM The Docker VM IP/Hostname on rhevm to create and upgrade clients CLIENTS_COUNT The number of clients(docker containers) to generate to run upgrade RHEV_CAPSULE_AK The AK name used in capsule subscription RHEV_CLIENT_AK The AK name used in client subscription """ if check_necessary_env_variables_for_upgrade(product): from_version = os.environ.get('FROM_VERSION') to_version = os.environ.get('TO_VERSION') logger.info('Performing UPGRADE FROM {0} TO {1}'.format( from_version, to_version)) # Get the setup dict returned by setup_products_for_upgrade setup_dict = get_setup_data() sat_host = setup_dict['sat_host'] env['satellite_host'] = sat_host try: with LogAnalyzer(sat_host): current = execute( get_sat_cap_version, 'sat', host=sat_host)[sat_host] if from_version != to_version: execute(satellite6_upgrade, host=sat_host) else: execute(satellite6_zstream_upgrade, host=sat_host) upgraded = execute( get_sat_cap_version, 'sat', host=sat_host)[sat_host] if LooseVersion(upgraded) > LooseVersion(current): logger.highlight( 'The Satellite is upgraded from {0} to {1}'.format( current, upgraded)) else: logger.highlight( 'The Satellite is NOT upgraded to next version. Now ' 'its {}'.format(upgraded)) # Generate foreman debug on satellite after upgrade execute(foreman_debug, 'satellite_{}'.format(sat_host), host=sat_host) # Execute tasks as post upgrade tier1 tests are dependent post_upgrade_test_tasks(sat_host) if product == 'capsule' or product == 'longrun': cap_hosts = setup_dict['capsule_hosts'] for cap_host in cap_hosts: try: with LogAnalyzer(cap_host): current = execute( get_sat_cap_version, 'cap', host=cap_host )[cap_host] if from_version != to_version: execute(satellite6_capsule_upgrade, cap_host, sat_host, host=cap_host) elif from_version == to_version: execute(satellite6_capsule_zstream_upgrade, host=cap_host) upgraded = execute( get_sat_cap_version, 'cap', host=cap_host )[cap_host] if current: if LooseVersion(upgraded) > LooseVersion( current): logger.highlight( 'The Capsule is upgraded from {0} ' 'to {1}.'.format(current, upgraded) ) else: logger.highlight( 'The Capsule is NOT upgraded to ' 'next version. Now its {}'.format( upgraded)) else: logger.highlight( 'Unable to fetch previous version but ' 'after upgrade capsule is {}.'.format( upgraded)) # Generate foreman debug on capsule postupgrade execute( foreman_debug, 'capsule_{}'.format(cap_host), host=cap_host) except Exception: # Generate foreman debug on failed capsule upgrade execute( foreman_debug, 'capsule_{}'.format(cap_host), host=cap_host) raise if product == 'client' or product == 'longrun': clients6 = setup_dict['clients6'] clients7 = setup_dict['clients7'] satellite6_client_upgrade('rhel6', clients6) satellite6_client_upgrade('rhel7', clients7) except Exception: # Generate foreman debug on failed satellite upgrade execute(foreman_debug, 'satellite_{}'.format(sat_host), host=sat_host) raise