Exemplo n.º 1
0
    def test_get_token(self):
        with open(mock_file_name, 'w') as f:
            f.write(file_ok)

        config = Config()
        config.conf = mock_file_name
        self.assertEqual(config.get_token(), 'some-token-1234')
Exemplo n.º 2
0
    def test_get_hostname(self):
        with open(mock_file_name, 'w') as f:
            f.write(file_ok)

        config = Config()
        config.conf = mock_file_name
        self.assertEqual(config.get_hostname(), 'egi.tenant.com')
Exemplo n.º 3
0
    def test_read(self):
        with open(mock_file_name, 'w') as f:
            f.write(file_ok)

        config = Config()
        config.conf = mock_file_name

        self.assertTrue(config.read())
Exemplo n.º 4
0
    def test_get_hostname_no_section(self):
        with open(mock_file_name, 'w') as f:
            f.write(file_missing_general)

        config = Config()
        config.conf = mock_file_name
        self.assertRaises(
            ConfigParser.NoSectionError,
            config.get_hostname
        )
Exemplo n.º 5
0
    def test_get_profiles_no_option(self):
        with open(mock_file_name, 'w') as f:
            f.write(file_missing_mp)

        config = Config()
        config.conf = mock_file_name
        self.assertRaises(
            ConfigParser.NoOptionError,
            config.get_profiles
        )
Exemplo n.º 6
0
    def test_get_profiles(self):
        with open(mock_file_name, 'w') as f:
            f.write(file_ok)

        config = Config()
        config.conf = mock_file_name

        self.assertEqual(
            config.get_profiles(), ['TEST_PROFILE1', 'TEST_PROFILE2']
        )
Exemplo n.º 7
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--noop',
                        action='store_true',
                        dest='noop',
                        help='run script without installing')
    parser.add_argument(
        '--backup-repos',
        action='store_true',
        dest='backup',
        help='backup/restore yum repos instead overriding them')
    args = parser.parse_args()
    noop = args.noop
    backup_repos = args.backup

    logger = logging.getLogger("argo-poem-packages")
    logger.setLevel(logging.INFO)

    stdout = logging.StreamHandler()
    if not noop:
        stdout.setLevel(logging.WARNING)

    stdout.setFormatter(logging.Formatter("%(levelname)s - %(message)s"))
    logger.addHandler(stdout)

    # setting up logging to file
    logfile = logging.handlers.RotatingFileHandler(LOGFILE,
                                                   maxBytes=512 * 1024,
                                                   backupCount=5)
    logfile.setLevel(logging.INFO)
    logfile.setFormatter(
        logging.Formatter(
            "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
            "%Y-%m-%d %H:%M:%S"))

    # add the handler to the root logger
    logger.addHandler(logfile)

    try:
        subprocess.call(['yum', 'clean', 'all'])

        config = Config()
        token = config.get_token()
        hostname = config.get_hostname()
        profiles = config.get_profiles()

        logger.info('Sending request for profile(s): ' + ', '.join(profiles))
        if backup_repos:
            repos = YUMRepos(hostname=hostname,
                             token=token,
                             profiles=profiles,
                             override=False)

        else:
            repos = YUMRepos(hostname=hostname, token=token, profiles=profiles)

        data = repos.get_data()

        if not data:
            logger.warning('No data for given metric profile(s): ' +
                           ', '.join(profiles))
            sys.exit(2)

        else:
            logger.info('Creating YUM repo files...')

            files = repos.create_file()

            logger.info('Created files: ' + '; '.join(files))

            pkg = Packages(data)

            if noop:
                info_msg, warn_msg = pkg.no_op()

            else:
                info_msg, warn_msg = pkg.install()

            # if there were repo files backed up, now they are restored
            repos.clean()

            if info_msg:
                for msg in info_msg:
                    logger.info(msg)

            if warn_msg:
                for msg in warn_msg:
                    logger.warning(msg)

                sys.exit(1)

            else:
                missing_packages_msg = ''
                if repos.missing_packages:
                    missing_packages_msg = \
                        'Missing packages for given distro: ' + \
                        ', '.join(repos.missing_packages)
                    logger.warning(missing_packages_msg)

                if not noop:
                    if missing_packages_msg:
                        print('WARNING: ' + missing_packages_msg)

                logger.info("The run finished successfully.")
                sys.exit(0)

    except requests.exceptions.ConnectionError as err:
        logger.error(err)
        sys.exit(2)

    except requests.exceptions.RequestException as err:
        logger.error(err)
        sys.exit(2)

    except configparser.ParsingError as err:
        logger.error(err)
        sys.exit(2)

    except configparser.NoSectionError as err:
        logger.error(err)
        sys.exit(2)

    except configparser.NoOptionError as err:
        logger.error(err)
        sys.exit(2)

    except PackageException as err:
        logger.error(err)
        sys.exit(2)
Exemplo n.º 8
0
def main():
    logger = logging.getLogger('argo-poem-packages')
    logger.setLevel(logging.INFO)

    # setting up logging to file
    logfile = logging.FileHandler('/var/log/messages')
    logfile.setLevel(logging.INFO)
    logfile.setFormatter(
        logging.Formatter(
            '%(asctime)s - %(name)s - %(levelname)s - %(message)s'))

    # add the handler to the root logger
    logger.addHandler(logfile)

    try:
        subprocess.call(['yum', 'clean', 'all'])

        config = Config()
        token = config.get_token()
        hostname = config.get_hostname()
        profiles = config.get_profiles()

        logger.info('Sending request for profile(s): ' + ', '.join(profiles))
        data = get_repo_data(build_api_url(hostname), token, profiles)

        if not data:
            logger.warning('No data for given metric profile(s): ' +
                           ', '.join(profiles))
            sys.exit(2)

        else:
            logger.info('Creating YUM repo files...')
            create_yum_repo_file(data)

            pkg = Packages(data)

            installed, not_installed, downgraded, not_downgraded = \
                pkg.install_packages()

            warn_msg = []
            info_msg = []
            if pkg.get_packages_not_found():
                warn_msg.append('Packages not found: ' +
                                ', '.join(pkg.get_packages_not_found()))

            if installed:
                new_installed = \
                    pkg.get_packages_installed_with_versions_as_requested(
                        installed
                    )

                if new_installed:
                    info_msg.append('Packages installed: ' +
                                    ', '.join(new_installed))

                installed_diff = \
                    pkg.get_packages_installed_with_different_version()

                if installed_diff:
                    info_msg.append(
                        'Packages installed with different version: ' +
                        ', '.join(installed_diff))

            if not_installed:
                warn_msg.append('Packages not installed: ' +
                                ', '.join(not_installed))

            if downgraded:
                info_msg.append('Packages downgraded: ' +
                                ', '.join(downgraded))

            if not_downgraded:
                warn_msg.append('Packages not downgraded: ' +
                                ', '.join(not_downgraded))

            if info_msg:
                logger.info('; '.join(info_msg))

            if warn_msg:
                logger.warning('; '.join(warn_msg))
                sys.exit(1)

            else:
                logger.info('ok!')
                sys.exit(0)

    except requests.exceptions.ConnectionError as err:
        logger.error(err)
        sys.exit(2)

    except requests.exceptions.RequestException as err:
        logger.error(err)
        sys.exit(2)

    except ConfigParser.ParsingError as err:
        logger.error(err)
        sys.exit(2)

    except ConfigParser.NoSectionError as err:
        logger.error(err)
        sys.exit(2)

    except ConfigParser.NoOptionError as err:
        logger.error(err)
        sys.exit(2)