def _sync_capsule_subscription_to_capsule_ak(ak): """Syncs to_version capsule contents, adds to the CV and attaches contents to the AK through which Capsule is registered :param ak: ```nailgun.entities.ActivationKey``` used for capsule subscription """ cv = ak.content_view.read() org = ak.organization capsule_repo = os.environ.get('CAPSULE_URL') to_version = os.environ.get('TO_VERSION') os_ver = os.environ.get('OS')[-1] # If custom capsule repo is not given then # enable capsule repo from Redhat Repositories if capsule_repo: cap_product = entities.Product( name=customcontents['capsule']['prod'], organization=org).create() cap_repo = entities.Repository( name=customcontents['capsule']['repo'], product=cap_product, url=capsule_repo, organization=org, content_type='yum').create() else: cap_product = entities.Product( name=rhelcontents['capsule']['prod'], organization=org ).search(query={'per_page': 100})[0] cap_reposet = entities.RepositorySet( name=rhelcontents['capsule']['repo'].format(cap_ver=to_version, os_ver=os_ver), product=cap_product ).search()[0] try: cap_reposet.enable( data={'basearch': 'x86_64', 'releasever': '7Server', 'organization_id': org.id}) except requests.exceptions.HTTPError as exp: logger.warn(exp) cap_repo = entities.Repository( name=rhelcontents['capsule']['repofull'].format( cap_ver=to_version, os_ver=os_ver, arch='x86_64') ).search(query={'organization_id': org.id, 'per_page': 100})[0] call_entity_method_with_timeout(entities.Repository(id=cap_repo.id).sync, timeout=2500) # Add repos to CV cv.repository += [cap_repo] cv.update(['repository']) ak = ak.read() if capsule_repo: cap_sub = entities.Subscription().search( query={'search': 'name={0}'.format(customcontents['capsule']['prod'])})[0] ak.add_subscriptions(data={ 'quantity': 1, 'subscription_id': cap_sub.id, }) else: ak.content_override( data={ 'content_override': { 'content_label': rhelcontents['capsule']['label'].format( cap_ver=to_version, os_ver=os_ver), 'value': '1'} } )
def _sync_sattools_repos_to_satellite_for_capsule(capsuletools_url, org): """Creates custom / Enables RH Tools repo on satellite and syncs for capsule upgrade :param str capsuletools_url: The capsule tools repo url :param org: ```nailgun.entities.Organization``` entity of capsule :returns: ```nailgun.entities.repository``` entity for capsule """ to_ver = os.environ.get('TO_VERSION') rhelver = '7' arch = 'x86_64' if capsuletools_url: captools_product = entities.Product( name=customcontents['capsule_tools']['prod'], organization=org).create() captools_repo = entities.Repository( name=customcontents['capsule_tools']['repo'], product=captools_product, url=capsuletools_url, organization=org, content_type='yum').create() else: captools_product = entities.Product( name=rhelcontents['tools']['prod'], organization=org).search(query={'per_page': 100})[0] cap_reposet = entities.RepositorySet( name=rhelcontents['tools']['repo'].format(sat_ver=to_ver, os_ver=rhelver), product=captools_product).search()[0] try: cap_reposet.enable(data={ 'basearch': arch, 'organization_id': org.id }) except requests.exceptions.HTTPError as exp: logger.warn(exp) time.sleep(5) captools_repo = entities.Repository( name=rhelcontents['tools']['repofull'].format( sat_ver=to_ver, os_ver=rhelver, arch=arch)).search( query={ 'organization_id': org.id, 'per_page': 100 })[0] call_entity_method_with_timeout( entities.Repository(id=captools_repo.id).sync, timeout=2500) captools_repo.repo_id = rhelcontents['tools']['label'].format( os_ver=rhelver, sat_ver=to_ver) return captools_repo
def _sync_rh_repos_to_satellite(org): """Task to sync Redhat Repositories to latest required during upgrade :param org: ```nailgun.entities.Organization``` entity of capsule :returns tuple: RHSCL and Redhat 7 Server repo name, label name and product name """ rhelver = '7' arch = 'x86_64' # Enable rhscl repository scl_product = entities.Product( name=rhelcontents['rhscl_sat64']['prod'], organization=org ).search(query={'per_page': 100})[0] scl_reposet = entities.RepositorySet( name=rhelcontents['rhscl']['repo'].format(os_ver=rhelver), product=scl_product ).search()[0] try: scl_reposet.enable( data={'basearch': arch, 'releasever': '7Server', 'organization_id': org.id}) except requests.exceptions.HTTPError as exp: logger.warn(exp) time.sleep(20) # Sync enabled Repo from cdn scl_repo = entities.Repository( name=rhelcontents['rhscl']['repofull'].format(os_ver=rhelver, arch=arch) ).search(query={'organization_id': org.id, 'per_page': 100})[0] call_entity_method_with_timeout(entities.Repository(id=scl_repo.id).sync, timeout=2500) # Enable RHEL 7 Server repository server_product = entities.Product( name=rhelcontents['server']['prod'], organization=org).search(query={'per_page': 100})[0] server_reposet = entities.RepositorySet( name=rhelcontents['server']['repo'].format(os_ver=rhelver), product=server_product ).search()[0] try: server_reposet.enable( data={'basearch': arch, 'releasever': '7Server', 'organization_id': org.id}) except requests.exceptions.HTTPError as exp: logger.warn(exp) time.sleep(20) # Sync enabled Repo from cdn server_repo = entities.Repository( name=rhelcontents['server']['repofull'].format(os_ver=rhelver, arch=arch) ).search(query={'organization_id': org.id, 'per_page': 100})[0] call_entity_method_with_timeout(entities.Repository(id=server_repo.id).sync, timeout=3600) scl_repo.repo_id = rhelcontents['rhscl']['label'].format(os_ver=rhelver) server_repo.repo_id = rhelcontents['server']['label'].format(os_ver=rhelver) return scl_repo, server_repo
def sync_capsule_repos_to_upgrade(capsules): """This syncs capsule repo in Satellite server and also attaches the capsule repo subscription to each capsule :param list capsules: The list of capsule hostnames to which new capsule repo subscription will be attached Following environment variable affects this function: CAPSULE_URL The url for capsule repo from latest satellite compose. If not provided, capsule repo from Red Hat repositories will be enabled TOOLS_URL_RHEL{}.format(os_ver) The url for capsuletools repo from latest satellite compose. If not provided, capsule repo from Red Hat repositories will be enabled FROM_VERSION Current Satellite version - to differentiate default organization. e.g. '6.1', '6.0' TO_VERSION Upgradable Satellite version - To enable capsule repo e.g '6.1', '6.2' OS OS version to enable next version capsule repo e.g 'rhel7', 'rhel6' Personal Upgrade Env Vars: CAPSULE_AK The AK name used in capsule subscription RHEV upgrade Env Vars: RHEV_CAPSULE_AK The AK name used in capsule subscription """ to_version = os.environ.get('TO_VERSION') logger.info('Syncing latest capsule repos in Satellite ...') os_ver = os.environ.get('OS')[-1] capsule_repo = os.environ.get('CAPSULE_URL') capsuletools_url = os.environ.get('TOOLS_URL_RHEL{}'.format(os_ver)) ak_name = os.environ.get('CAPSULE_AK', os.environ.get('RHEV_CAPSULE_AK')) if ak_name is None: logger.warning( 'The AK name is not provided for Capsule upgrade! Aborting...') sys.exit(1) org = entities.Organization(id=1).read() ak = entities.ActivationKey(organization=org).search( query={'search': 'name={}'.format(ak_name)})[0] cv = ak.content_view.read() lenv = ak.environment.read() # Fix dead pulp tasks if os_ver == '6': run('for i in pulp_resource_manager pulp_workers pulp_celerybeat; ' 'do service $i restart; done') _sync_capsule_subscription_to_capsule_ak(ak) if float(to_version) >= 6.3: _add_additional_subscription_for_capsule(ak, capsuletools_url) # Publishing and promoting the CV with all newly added capsule, capsuletools, rhscl and # server repos combine call_entity_method_with_timeout(cv.read().publish, timeout=2000) published_ver = entities.ContentViewVersion( id=max([cv_ver.id for cv_ver in cv.read().version])).read() published_ver.promote(data={'environment_id': lenv.id, 'force': False}) # Add capsule and tools custom prod subscription to capsules if capsule_repo: add_custom_product_subscription_to_hosts( customcontents['capsule']['prod'], capsules) if float(to_version) >= 6.3: if capsuletools_url: add_custom_product_subscription_to_hosts( customcontents['capsule_tools']['prod'], capsules)
def sync_tools_repos_to_upgrade(client_os, hosts): """This syncs tools repo in Satellite server and also attaches the new tools repo subscription onto each client :param string client_os: The client OS of which tools repo to be synced e.g: rhel6, rhel7 :param list hosts: The list of capsule hostnames to which new capsule repo subscription will be attached Following environment variable affects this function: TOOLS_URL_{client_os} The url of tools repo from latest satellite compose. FROM_VERSION Current Satellite version - to differentiate default organization. e.g. '6.1', '6.0' Personal Upgrade Env Vars: CLIENT_AK The ak_name attached to subscription of client Rhevm upgrade Env Vars: RHEV_CLIENT_AK The AK name used in client subscription """ client_os = client_os.upper() tools_repo_url = os.environ.get('TOOLS_URL_{}'.format(client_os)) if tools_repo_url is None: logger.warning('The Tools Repo URL for {} is not provided ' 'to perform Client Upgrade !'.format(client_os)) sys.exit(1) ak_name = os.environ.get( 'CLIENT_AK_{}'.format(client_os), os.environ.get('RHEV_CLIENT_AK_{}'.format(client_os))) if ak_name is None: logger.warning('The AK details are not provided for {0} Client ' 'upgrade!'.format(client_os)) sys.exit(1) org = entities.Organization().search( query={'search': 'name="{}"'.format("Default Organization")})[0] ak = entities.ActivationKey(organization=org).search( query={'search': 'name={}'.format(ak_name)})[0] cv = ak.content_view.read() lenv = ak.environment.read() toolsproduct_name = customcontents['tools']['prod'].format( client_os=client_os) toolsrepo_name = customcontents['tools']['repo'].format( client_os=client_os) # adding sleeps in between to avoid race conditions tools_product = entities.Product(name=toolsproduct_name, organization=org).create() tools_repo = entities.Repository(name=toolsrepo_name, product=tools_product, url=tools_repo_url, organization=org, content_type='yum').create() entities.Repository(id=tools_repo.id).sync() cv.repository += [tools_repo] cv.update(['repository']) call_entity_method_with_timeout(cv.read().publish, timeout=2500) published_ver = entities.ContentViewVersion( id=max([cv_ver.id for cv_ver in cv.read().version])).read() published_ver.promote(data={'environment_id': lenv.id, 'force': False}) tools_sub = entities.Subscription().search( query={'search': 'name={0}'.format(toolsproduct_name)})[0] ak.add_subscriptions(data={ 'quantity': 1, 'subscription_id': tools_sub.id, }) # Add this latest tools repo to hosts to upgrade sub = entities.Subscription().search( query={'search': 'name={0}'.format(toolsproduct_name)})[0] for host in hosts: if float(os.environ.get('FROM_VERSION')) <= 6.1: # If not User Hosts then, attach sub to dockered clients if not all([ os.environ.get('CLIENT6_HOSTS'), os.environ.get('CLIENT7_HOSTS') ]): docker_vm = os.environ.get('DOCKER_VM') execute(attach_subscription_to_host_from_content_host, sub.cp_id, True, host, host=docker_vm) # Else, Attach subs to user hosts else: execute(attach_subscription_to_host_from_content_host, sub.cp_id, host=host) else: host = entities.Host().search( query={'search': 'name={}'.format(host)})[0] entities.HostSubscription(host=host).add_subscriptions( data={'subscriptions': [{ 'id': sub.id, 'quantity': 1 }]})