Exemplo n.º 1
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
Exemplo n.º 2
0
    def upgrade(self):
        inventory_path = get_inventory_path_for_build(self.build_dir)

        # copy resources
        self.copy_resources()

        # upgrade inventory
        inventory_upgrade = AnsibleInventoryUpgrade(self.build_dir,
                                                    self.backup_build_dir)
        inventory_upgrade.upgrade()

        # generate vars
        ansible_vars_generator = AnsibleVarsGenerator(
            inventory_upgrade=inventory_upgrade)
        ansible_vars_generator.generate()

        # pre-flight to prepare machines
        self.pre_flight(inventory_path)

        # run image_registry playbook
        self.ansible_command.run_playbook(
            inventory=inventory_path,
            playbook_path=self.playbook_path('image_registry'))

        # run upgrade playbook
        self.ansible_command.run_playbook(
            inventory=inventory_path,
            playbook_path=self.playbook_path('upgrade'))

        #post-flight after we are done
        self.post_flight(inventory_path)
Exemplo n.º 3
0
    def run_recovery(self):
        try:
            build_directory = Config().output_dir
            backup_role_path = os.path.join(build_directory, 'ansible', 'recovery.yml')
            inventory_path = get_inventory_path_for_build(build_directory)
            self.ansible_command.run_playbook(inventory=inventory_path, playbook_path=backup_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
    def _update_playbook_files_and_run(self, action, component):
        """Update backup/recovery ansible playbooks inside the existing build directory and run the provisioning."""

        self.logger.info(f'Running {action} on {component}...')

        # Copy playbook file
        playbook_build_path = os.path.join(self.build_directory, 'ansible', f'{action}_{component}.yml')
        playbook_source_path = os.path.join(AnsibleRunner.ANSIBLE_PLAYBOOKS_PATH, f'{action}_{component}.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)
Exemplo n.º 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)
Exemplo n.º 6
0
    def upgrade(self):
        inventory_path = get_inventory_path_for_build(self.backup_build_dir)
        build_version = check_build_output_version(self.backup_build_dir)

        self.logger.info(f'Loading backup Ansible inventory: {inventory_path}')
        loaded_inventory = InventoryManager(loader=DataLoader(),
                                            sources=inventory_path)

        # move loaded inventory to templating structure
        new_inventory = []
        for key in loaded_inventory.groups:
            if key != 'all' and key != 'ungrouped':
                group_hosts = loaded_inventory.groups[key].hosts
                new_hosts = []
                for host in group_hosts:
                    new_hosts.append(
                        AnsibleHostModel(host.address,
                                         host.vars['ansible_host']))
                new_inventory.append(AnsibleInventoryItem(key, new_hosts))

        if build_version == BUILD_LEGACY:
            self.logger.info(f'Upgrading Ansible inventory Epiphany < 0.3.0')

            # Epiphany < 0.3.0 did not have manifest file in build folder so lets create bare minimum cluster model from inventory
            self.cluster_model = dict_to_objdict({
                'provider': 'any',
                'specification': {
                    'admin_user': {
                        'name':
                        loaded_inventory.groups['all'].vars['ansible_user'],
                        'key_path':
                        loaded_inventory.groups['all'].
                        vars['ansible_ssh_private_key_file']
                    }
                }
            })

            # Remap roles
            self.rename_role(new_inventory, 'master', 'kubernetes_master')
            self.rename_role(new_inventory, 'worker', 'kubernetes_node')
            self.rename_role(new_inventory, 'deployments', 'applications')
            self.rename_role(new_inventory, 'elasticsearch-curator',
                             'elasticsearch_curator')
            self.rename_role(new_inventory, 'jmx-exporter', 'jmx_exporter')
            self.rename_role(new_inventory, 'kafka-exporter', 'kafka_exporter')
            self.rename_role(new_inventory, 'haproxy_tls_termination',
                             'haproxy')

            # remove linux and reboot roles if present
            self.delete_role(new_inventory, 'linux')
            self.delete_role(new_inventory, 'reboot')
        else:
            self.logger.info(f'Upgrading Ansible inventory Epiphany => 0.3.0')

            # load cluster model from manifest
            self.manifest_docs = load_manifest_docs(self.backup_build_dir)
            self.cluster_model = select_single(
                self.manifest_docs, lambda x: x.kind == 'epiphany-cluster')

        # Merge manifest cluster config with newer defaults
        default_cluster_model = load_yaml_obj(data_types.DEFAULT, 'common',
                                              'epiphany-cluster')
        merge_objdict(default_cluster_model, self.cluster_model)
        self.cluster_model = default_cluster_model

        # Check if repo roles are present and if not add them
        master = self.get_role(new_inventory, 'kubernetes_master')
        if master == None:
            raise Exception('No kubernetes_master to use as repository')
        master_node = master.hosts[0]

        # add image_registry
        image_registry = self.get_role(new_inventory, 'image_registry')
        if image_registry == None:
            hosts = []
            hosts.append(AnsibleHostModel(master_node.name, master_node.ip))
            new_inventory.append(AnsibleInventoryItem('image_registry', hosts))

        # add repository
        repository = self.get_role(new_inventory, 'repository')
        if repository == None:
            hosts = []
            hosts.append(AnsibleHostModel(master_node.name, master_node.ip))
            new_inventory.append(AnsibleInventoryItem('repository', hosts))

        # save new inventory
        save_inventory(new_inventory, self.cluster_model, self.build_dir)

        return 0