From 6ec8ab2a537eafc3f2abe6c102b4969ddcff0afe Mon Sep 17 00:00:00 2001 From: Devendra Singh Date: Thu, 29 Aug 2019 19:12:20 +0530 Subject: [PATCH] Capsule code refactoring and cleanup --- upgrade/capsule.py | 103 +++++++-------------------------------- upgrade/helpers/tasks.py | 77 ++++++++++++++++++++++++++++- upgrade/satellite.py | 41 ++-------------- 3 files changed, 97 insertions(+), 124 deletions(-) diff --git a/upgrade/capsule.py b/upgrade/capsule.py index 155a9006..e6456f45 100644 --- a/upgrade/capsule.py +++ b/upgrade/capsule.py @@ -2,10 +2,7 @@ import sys from automation_tools import setup_capsule_firewall -from automation_tools.repository import enable_repos -from automation_tools.satellite6.capsule import generate_capsule_certs -from automation_tools.utils import distro_info, update_packages -from datetime import datetime +from automation_tools.utils import distro_info from fabric.api import env, execute, run from upgrade.helpers.logger import logger from upgrade.helpers.rhevm4 import ( @@ -15,7 +12,10 @@ from upgrade.helpers.tasks import ( sync_capsule_repos_to_upgrade, add_baseOS_repo, - setup_foreman_maintain_repo + nonfm_upgrade, + upgrade_validation, + setup_foreman_maintain_repo, + ) from upgrade.helpers.tools import ( copy_ssh_key, @@ -121,70 +121,26 @@ def satellite6_capsule_upgrade(cap_host, sat_host): """ logger.highlight('\n========== CAPSULE UPGRADE =================\n') from_version = os.environ.get('FROM_VERSION') - to_version = os.environ.get('TO_VERSION') setup_capsule_firewall() major_ver = distro_info()[1] - # Re-register Capsule for 6.2 and 6.3 - # AS per host unification feature: if there is a host registered where the - # Host and Content Host are in different organizations (e.g. host not in - # org, and content host in one), the content host will be unregistered as - # part of the upgrade process. - if float(to_version) >= 6.2: - ak_name = os.environ.get('CAPSULE_AK') if os.environ.get( - 'CAPSULE_AK') else os.environ.get('RHEV_CAPSULE_AK') - run('subscription-manager register --org="Default_Organization" ' - '--activationkey={0} --force'.format(ak_name)) + ak_name = os.environ.get('CAPSULE_AK') if os.environ.get( + 'CAPSULE_AK') else os.environ.get('RHEV_CAPSULE_AK') + run('subscription-manager register --org="Default_Organization" ' + '--activationkey={0} --force'.format(ak_name)) disable_old_repos('rhel-{0}-server-satellite-capsule-{1}-rpms'.format( major_ver, from_version)) - if from_version == '6.1' and major_ver == '6': - enable_repos('rhel-server-rhscl-{0}-rpms'.format(major_ver)) # setup foreman-maintain - if to_version not in ['6.1', '6.2', '6.3']: - setup_foreman_maintain_repo() + setup_foreman_maintain_repo() # Check what repos are set - run('yum repolist') - if from_version == '6.0': - # Stop katello services, except mongod - run('for i in qpidd pulp_workers pulp_celerybeat ' - 'pulp_resource_manager httpd; do service $i stop; done') - else: - # Stop katello services - run('katello-service stop') - run('yum clean all', warn_only=True) - logger.info('Updating system and capsule packages ... ') - preyum_time = datetime.now().replace(microsecond=0) - update_packages(quiet=False) - postyum_time = datetime.now().replace(microsecond=0) - logger.highlight('Time taken for capsule packages update - {}'.format( - str(postyum_time-preyum_time))) - if from_version == '6.0': - run('yum install -y capsule-installer', warn_only=True) - # Copy answer file from katello to capule installer - run('cp /etc/katello-installer/answers.capsule-installer.yaml.rpmsave ' - '/etc/capsule-installer/answers.capsule-installer.yaml', - warn_only=True) - execute( - generate_capsule_certs, - cap_host, - True, - host=sat_host - ) - # Copying the capsule cert to capsule - execute(lambda: run("scp -o 'StrictHostKeyChecking no' {0}-certs.tar " - "root@{0}:/home/".format(cap_host)), host=sat_host) - setup_capsule_firewall() - preup_time = datetime.now().replace(microsecond=0) - run('satellite-installer --scenario capsule --upgrade ' - '--certs-tar-file /home/{0}-certs.tar ' - '--certs-update-all'.format(cap_host)) - postup_time = datetime.now().replace(microsecond=0) - logger.highlight('Time taken for Capsule Upgrade - {}'.format( - str(postup_time-preup_time))) + + nonfm_upgrade(satellite_upgrade=False, + cap_host=cap_host, + sat_host=sat_host) # Rebooting the capsule for kernel update if any reboot(160) host_ssh_availability_check(cap_host) # Check if Capsule upgrade is success - run('katello-service status', warn_only=True) + upgrade_validation() def satellite6_capsule_zstream_upgrade(cap_host): @@ -212,34 +168,9 @@ def satellite6_capsule_zstream_upgrade(cap_host): disable_old_repos('rhel-{0}-server-satellite-capsule-{1}-rpms'.format( major_ver, from_version)) # Check what repos are set - run('yum repolist') - if from_version == '6.0': - # Stop katello services, except mongod - run('for i in qpidd pulp_workers pulp_celerybeat ' - 'pulp_resource_manager httpd; do service $i stop; done') - else: - # Stop katello services - run('katello-service stop') - run('yum clean all', warn_only=True) - logger.info('Updating system and capsule packages ... ') - preyum_time = datetime.now().replace(microsecond=0) - update_packages(quiet=False) - postyum_time = datetime.now().replace(microsecond=0) - logger.highlight('Time taken for capsule packages update - {}'.format( - str(postyum_time-preyum_time))) - setup_capsule_firewall() - preup_time = datetime.now().replace(microsecond=0) - if to_version == '6.0': - run('katello-installer --upgrade') - elif to_version == '6.1': - run('capsule-installer --upgrade') - else: - run('satellite-installer --scenario capsule --upgrade ') - postup_time = datetime.now().replace(microsecond=0) - logger.highlight('Time taken for Capsule Upgrade - {}'.format( - str(postup_time-preup_time))) + nonfm_upgrade(satellite_upgrade=False) # Rebooting the capsule for kernel update if any reboot(160) host_ssh_availability_check(cap_host) # Check if Capsule upgrade is success - run('katello-service status', warn_only=True) + upgrade_validation() diff --git a/upgrade/helpers/tasks.py b/upgrade/helpers/tasks.py index dbdf5877..5b9d31a0 100644 --- a/upgrade/helpers/tasks.py +++ b/upgrade/helpers/tasks.py @@ -14,9 +14,11 @@ setup_alternate_capsule_ports, setup_fake_manifest_certificate, ) +from automation_tools import setup_capsule_firewall from automation_tools import setup_foreman_discovery, setup_avahi_discovery from automation_tools.repository import enable_repos, disable_repos -from automation_tools.utils import get_discovery_image +from automation_tools.utils import get_discovery_image, update_packages +from automation_tools.satellite6.capsule import generate_capsule_certs from nailgun import entities from robozilla.decorators import bz_bug_is_open from upgrade.helpers.constants import customcontents, rhelcontents @@ -923,3 +925,76 @@ def repository_cleanup(repo_name): for fname in os.listdir('/etc/yum.repos.d/'): if repo_name in fname.lower(): os.remove('/etc/yum.repos.d/{}'.format(fname)) + + +def nonfm_upgrade(satellite_upgrade=True, + cap_host=None, sat_host=None): + """ + The purpose of this module to perform the upgrade task without foreman-maintain. + In this function we setup the repository, stop the katello services, + cleanup, and execute satellite upgrade task" + :param bool satellite_upgrade: If satellite_upgrade is True then upgrade + type satellite otherwise capsule + :param bool zstream: Capsule zStream upgrade + :param str cap_host: hostname of capsule it used to generate certificate for + capsules major version upgrade. + :param str sat_host: hostname of satellite used to generate certificate for + capsules major version upgrade. + : + """ + # Check what repos are set + upgrade_type = "satellite" if satellite_upgrade else "capsule" + run('yum repolist') + # Stop katello services, except mongod + run('katello-service stop') + run('yum clean all', warn_only=True) + # Updating the packages again after setting sat6 repo + logger.info('Updating system and {} packages... '.format(upgrade_type)) + preyum_time = datetime.now().replace(microsecond=0) + update_packages(quiet=False) + postyum_time = datetime.now().replace(microsecond=0) + logger.highlight('Time taken for system and {} packages update' + ' - {}'.format(upgrade_type, str(postyum_time - preyum_time))) + # non zStream capsule upgrade + if sat_host and cap_host: + execute( + generate_capsule_certs, + cap_host, + True, + host=sat_host + ) + execute(lambda: run("scp -o 'StrictHostKeyChecking no' {0}-certs.tar " + "root@{0}:/home/".format(cap_host)), host=sat_host) + setup_capsule_firewall() + preup_time = datetime.now().replace(microsecond=0) + upgrade_task(upgrade_type, cap_host) + else: + preup_time = datetime.now().replace(microsecond=0) + upgrade_task(upgrade_type) + postup_time = datetime.now().replace(microsecond=0) + logger.highlight('Time taken for Satellite Upgrade - {}'.format( + str(postup_time - preup_time))) + + +def upgrade_task(upgrade_type, cap_host=None): + """ + :param str upgrade_type: upgrade type would be an string either it is + satellite or capsule + :param str cap_host: hostname for capsule's major version upgrade + """ + if cap_host: + run('satellite-installer --scenario {0} --upgrade ' + '--certs-tar-file /home/{1}-certs.tar ' + '--certs-update-all'.format(upgrade_type, cap_host)) + else: + run('satellite-installer --scenario {} --upgrade'.format(upgrade_type)) + + +def upgrade_validation(upgrade_type=False): + """ + In this function we check the system states after upgrade. + :param bool upgrade_type: if upgrade_type is True then we check both the services. + """ + if upgrade_type: + run('hammer ping', warn_only=True) + run('katello-service status', warn_only=True) diff --git a/upgrade/satellite.py b/upgrade/satellite.py index c346c3cd..5e4cf8fb 100644 --- a/upgrade/satellite.py +++ b/upgrade/satellite.py @@ -12,7 +12,6 @@ install_prerequisites ) from automation_tools.utils import distro_info, update_packages -from datetime import datetime from fabric.api import env, execute, run from upgrade.helpers.logger import logger from upgrade.helpers.rhevm4 import ( @@ -24,7 +23,9 @@ foreman_maintain_upgrade, repository_setup, repository_cleanup, - setup_foreman_maintain + setup_foreman_maintain, + nonfm_upgrade, + upgrade_validation ) logger = logger() @@ -144,38 +145,4 @@ def satellite6_upgrade(zstream=False): reboot(180) host_ssh_availability_check(env.get('satellite_host')) # Test the Upgrade is successful - upgrade_validation() - - -def nonfm_upgrade(): - """ - The purpose of this module to perform the upgrade task without foreman-maintain. - In this function we setup the repository, stop the katello services, - cleanup, and execute satellite upgrade task" - """ - # Check what repos are set - run('yum repolist') - # Stop katello services, except mongod - run('katello-service stop') - run('yum clean all', warn_only=True) - # Updating the packages again after setting sat6 repo - logger.info('Updating system and satellite packages... ') - preyum_time = datetime.now().replace(microsecond=0) - update_packages(quiet=False) - postyum_time = datetime.now().replace(microsecond=0) - logger.highlight('Time taken for system and satellite packages update' - ' - {}'.format(str(postyum_time - preyum_time))) - # Running Upgrade - preup_time = datetime.now().replace(microsecond=0) - run('satellite-installer --scenario satellite --upgrade') - postup_time = datetime.now().replace(microsecond=0) - logger.highlight('Time taken for Satellite Upgrade - {}'.format( - str(postup_time - preup_time))) - - -def upgrade_validation(): - """ - In this function we check the system states after upgrade. - """ - run('hammer ping', warn_only=True) - run('katello-service status', warn_only=True) + upgrade_validation(True)