Esempio n. 1
0
    def test(self):
        # get manifest documents
        docs = load_manifest_docs(self.build_directory)
        cluster_model = select_single(docs,
                                      lambda x: x.kind == 'epiphany-cluster')

        # get inventory
        path_to_inventory = os.path.join(self.build_directory,
                                         INVENTORY_FILE_NAME)
        if not os.path.isfile(path_to_inventory):
            raise Exception(
                f'No "{INVENTORY_FILE_NAME}" inside the build directory: "{self.build_directory}"'
            )

        # get admin user
        admin_user = cluster_model.specification.admin_user
        if not os.path.isfile(admin_user.key_path):
            raise Exception(
                f'No SSH key file in directory: "{admin_user.key_path}"')

        # get and create the spec output dir if it does not exist
        spec_output = os.path.join(self.build_directory, SPEC_OUTPUT_DIR)
        if not os.path.exists(spec_output):
            os.makedirs(spec_output)

        # run the spec tests
        spec_command = SpecCommand()
        spec_command.run(spec_output, path_to_inventory, admin_user.name,
                         admin_user.key_path, self.group)

        return 0
Esempio n. 2
0
    def delete(self):
        docs = load_manifest_docs(self.build_directory)
        cluster_model = select_single(docs, lambda x: x.kind == 'epiphany-cluster')
        
        if cluster_model.provider == 'any':
            raise Exception('Delete works only for cloud providers')

        with TerraformRunner(cluster_model, docs) as tf_runner:
            tf_runner.delete()     
            
        shutil.rmtree(self.build_directory, ignore_errors=True)     

        return 0
Esempio n. 3
0
    def _process_input_docs(self):
        """Load, validate and merge (with defaults) input yaml documents."""

        # Get existing manifest config documents
        self.manifest_docs = load_manifest_docs(self.build_directory)
        self.cluster_model = select_single(self.manifest_docs, lambda x: x.kind == 'epiphany-cluster')

        # Load backup / recovery configuration documents
        self.input_docs = load_yamls_file(self.file)

        # Validate input documents
        with SchemaValidator(self.cluster_model, self.input_docs) as schema_validator:
            schema_validator.run_for_individual_documents()

        # Merge the input docs with defaults
        with DefaultMerger(self.input_docs) as doc_merger:
            self.input_docs = doc_merger.run()
Esempio n. 4
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