Example #1
0
    def prepare(self):
        prepare_src = os.path.join(self.PREPARE_PATH, self.os)
        charts_src = self.CHARTS_PATH
        skopeo_src = os.path.join(dirname(dirname(inspect.getfile(os))),
                                  'skopeo_linux')

        prepare_dst = os.path.join(Config().output_dir, 'prepare_scripts')
        charts_dst = os.path.join(prepare_dst, 'charts', 'system')

        if not os.path.exists(prepare_src):
            supported_os = os.listdir(self.PREPARE_PATH)
            raise Exception(
                f'Unsupported OS: {self.os}. Currently supported: {supported_os}'
            )

        if not os.path.exists(skopeo_src):
            raise Exception('Skopeo dependency not found')

        # copy files to output dir
        copy_files_recursively(prepare_src, prepare_dst)
        copy_files_recursively(charts_src, charts_dst)
        shutil.copy(skopeo_src, prepare_dst)

        # make sure the scripts and skopeo are executable
        self.make_file_executable(os.path.join(prepare_dst, 'skopeo_linux'))
        self.make_file_executable(
            os.path.join(prepare_dst, 'download-requirements.sh'))

        self.logger.info(
            f'Prepared files for downloading the offline requirements in: {prepare_dst}'
        )
        return 0
    def _update_role_files_and_vars(self, action, document):
        """Render mandatory vars files for backup/recovery ansible roles inside the existing build directory."""

        self.logger.info(f'Updating {action} role files...')

        # Copy role files
        roles_build_path = os.path.join(self.build_directory, 'ansible/roles', action)
        roles_source_path = os.path.join(AnsibleRunner.ANSIBLE_PLAYBOOKS_PATH, 'roles', action)
        copy_files_recursively(roles_source_path, roles_build_path)

        # Render role vars
        vars_dir = os.path.join(roles_build_path, 'vars')
        os.makedirs(vars_dir, exist_ok=True)
        vars_file_path = os.path.join(vars_dir, 'main.yml')
        with open(vars_file_path, 'w') as stream:
            dump(document, stream)
Example #3
0
    def copy_resources(self):
        self.logger.info('Copying Ansible resources')
        if self.cluster_model != None:
            ansible_dir = get_ansible_path(
                self.cluster_model.specification.name)
        else:
            ansible_dir = get_ansible_path_for_build(self.build_dir)

        shutil.rmtree(ansible_dir, ignore_errors=True)
        copy_files_recursively(AnsibleRunner.ANSIBLE_PLAYBOOKS_PATH,
                               ansible_dir)

        # copy skopeo so Ansible can move it to the repositry machine
        if not Config().offline_requirements:
            shutil.copy(
                os.path.join(dirname(dirname(inspect.getfile(os))),
                             'skopeo_linux'), '/tmp')
Example #4
0
    def run(self):
        inventory_path = get_inventory_path(
            self.cluster_model.specification.name)

        # create inventory on every run
        self.inventory_creator.create()
        time.sleep(10)

        copy_files_recursively(
            AnsibleRunner.ANSIBLE_PLAYBOOKS_PATH,
            get_ansible_path(self.cluster_model.specification.name))

        # todo: install packages to run ansible on Red Hat hosts
        self.ansible_command.run_task_with_retries(
            hosts="all",
            inventory=inventory_path,
            module="raw",
            args="cat /etc/lsb-release | grep -i DISTRIB_ID | grep -i ubuntu && "
            "sudo apt-get install -y python-simplejson "
            "|| echo 'Cannot find information about Ubuntu distribution'",
            retries=5)

        self.ansible_vars_generator.run()

        common_play_result = self.ansible_command.run_playbook_with_retries(
            inventory=inventory_path,
            playbook_path=os.path.join(
                get_ansible_path(self.cluster_model.specification.name),
                "common.yml"),
            retries=5)
        if common_play_result != 0:
            return

        enabled_roles = self.inventory_creator.get_enabled_roles()

        for role in enabled_roles:
            play_result = self.ansible_command.run_playbook_with_retries(
                inventory=inventory_path,
                playbook_path=os.path.join(
                    get_ansible_path(self.cluster_model.specification.name),
                    to_role_name(role) + ".yml"),
                retries=1)
            if play_result != 0:
                break
Example #5
0
    def upgrade_patch_files_and_run(self, action):
        self.logger.info(f'Running {action}...')

        #copy role files
        roles_build_path = os.path.join(self.build_directory, 'ansible/roles',
                                        action)
        roles_source_path = os.path.join(AnsibleRunner.ANSIBLE_PLAYBOOKS_PATH,
                                         'roles', action)
        copy_files_recursively(roles_source_path, roles_build_path)

        #copy playbook file
        playbook_build_path = os.path.join(self.build_directory,
                                           'ansible/') + action + '.yml'
        playbook_source_path = os.path.join(
            AnsibleRunner.ANSIBLE_PLAYBOOKS_PATH) + action + '.yml'
        copy_file(playbook_source_path, playbook_build_path)

        #run the playbook
        inventory_path = get_inventory_path_for_build(self.build_directory)
        self.ansible_command.run_playbook(inventory=inventory_path,
                                          playbook_path=playbook_build_path)
Example #6
0
    def run_upgrade(self):
        try:
            build_directory = Config().output_dir
            build_roles_directory = os.path.join(build_directory, 'ansible/roles')

            upgrade_playbook_path = os.path.join(build_roles_directory, 'upgrade')
            backup_playbook_path = os.path.join(build_roles_directory, 'backup')
            recovery_playbook_path = os.path.join(build_roles_directory, 'recovery')

            upgrade_role_path = os.path.join(build_directory, 'ansible', 'upgrade.yml')

            epiphany_playbooks_path = os.path.dirname(__file__) + AnsibleRunner.ANSIBLE_PLAYBOOKS_PATH
            epiphany_roles_path = os.path.join(epiphany_playbooks_path, 'roles')

            upgrade_role_source_path = os.path.join(epiphany_roles_path, 'upgrade')
            backup_role_source_path = os.path.join(epiphany_roles_path, 'backup')
            restore_role_source_path = os.path.join(epiphany_roles_path, 'recovery')
            playbook_source_path = os.path.join(epiphany_playbooks_path, 'upgrade.yml')

            copy_files_recursively(upgrade_role_source_path, upgrade_playbook_path)
            copy_files_recursively(backup_role_source_path, backup_playbook_path)
            copy_files_recursively(restore_role_source_path, recovery_playbook_path)
            copy_file(playbook_source_path, upgrade_role_path)

            inventory_path = get_inventory_path_for_build(build_directory)
            self.ansible_command.run_playbook(inventory=inventory_path, playbook_path=upgrade_role_path)
            return 0
        except Exception as e:
            self.logger.error(e, exc_info=True)  # TODO extensive debug output might not always be wanted. Make this configurable with input flag?
            return 1