Beispiel #1
0
def precheck_cmd(constants: dict) -> list:
    """Get the arg list to run prechecks

    Args:
        constants ([dict]): config dict
    """
    playbook_dir = constants["playbooks"]
    return AnsiblePlay(playbook=f"{playbook_dir}/main.yml",
                       tags=['upgrade_precheck'],
                       extra_vars=constants)
Beispiel #2
0
    def destroy_gateways(self, cluster_config):
        """ destroy the AWS gateways instantiated through test cluster
        Args:
            cluster_config (dict): Test cluster configuration
        """
        if not cluster_config.gateways:
            print("Gateway information not found in cluster configs")
            return

        project_dir = self.constants["project_dir"]
        playbook_dir = self.constants["cloudstrapper_playbooks"]

        # cloudstrapper expects secrets.yaml to be present
        Path(f"{project_dir}/secrets.yaml").touch()

        aws_configs = get_aws_configs()
        cstrap_dict = {
            "testClusterStacks":
            [gw.gateway_id for gw in cluster_config.gateways],
            "dirLocalInventory": self.constants["project_dir"],
            "idSite": "TestCluster",
            "awsAccessKey": aws_configs["aws_access_key_id"],
            "awsSecretKey": aws_configs["aws_secret_access_key"],
            "dirSecretsLocal": self.constants["secret_dir"],
        }

        cluster_cleanup = AnsiblePlay(
            playbook=f"{playbook_dir}/cluster-provision.yaml",
            tags=['clusterCleanup'],
            extra_vars=cstrap_dict,
        )
        network_cleanup = AnsiblePlay(
            playbook=f"{playbook_dir}/agw-provision.yaml",
            tags=['cleanupBridge', 'cleanupNet'],
            skip_tags=['attachIface'],
            extra_vars=cstrap_dict,
        )

        for playbook in [cluster_cleanup, network_cleanup]:
            print(f"Running playbook {playbook}")
            rc = run_playbook(playbook)
            if rc != 0:
                raise ClusterDestroyError(f"Failed destroying cluster")
Beispiel #3
0
def verify_cmd(constants: dict, namespace: str) -> list:
    """Get the arg list to run prechecks

    Args:
        constants ([dict]): config dict
        namespace ([str]): orc8r namespace
    """
    playbook_dir = constants["playbooks"]
    extra_vars = {
        "orc8r_namespace": namespace,
    }
    return AnsiblePlay(playbook=f"{playbook_dir}/main.yml",
                       tags=['verify_sanity'],
                       extra_vars=extra_vars)
Beispiel #4
0
def cleanup_cmd(constants: dict, dryrun: bool = False) -> list:
    """Get the arg list to run cleanup resources

    Args:
        constants (dict): config dict
        dryrun (bool): flag to indicate dryrun. Defaults to False.

    Returns:
        list: command list
    """
    playbook_dir = constants["playbooks"]
    return AnsiblePlay(playbook=f"{playbook_dir}/cleanup.yml",
                       tags=['cleanup_dryrun'] if dryrun else ['cleanup'],
                       extra_vars=constants)
Beispiel #5
0
def certs_cmd(constants: dict, self_signed: bool = False) -> list:
    """Provide the arg list to add certs

    Args:
        constants (dict): config constants
        self_signed (bool): add self_signed certs. Defaults to False.

    Returns:
        list: command list
    """
    playbook_dir = constants['playbooks']
    skip_tags = []
    if not self_signed:
        skip_tags = ['self_signed']

    return AnsiblePlay(
        playbook=f"{playbook_dir}/certs.yml",
        tags=['addcerts'],
        skip_tags=skip_tags,
        extra_vars=constants)
Beispiel #6
0
    def create_orc8r(
        self,
        constants: dict,
        cluster_uuid: str,
        template: ClusterTemplate = None,
        skip_certs=False,
        skip_precheck=False,
    ) -> Dict[str, Any]:
        """ Create an orc8r instance in the test cluster

        Args:
            constants (dict): Constants dictionary
            template (ClusterTemplate, optional): Cluster template definition. Defaults to None.
            skip_certs (bool, optional): Skip certs creation. Defaults to False.
            skip_precheck (bool, optional): Skip prechecks. Defaults to False.

        Raises:
            ClusterCreateError: Exception raised when cluster creation fails
        """
        # configure deployment
        template.orc8r.infra['magma_uuid'] = cluster_uuid
        template.orc8r.infra.update(get_aws_configs())

        # set elastic deploy role based on current state
        k = 'deploy_elasticsearch_service_linked_role'
        template.orc8r.platform[k] = check_elastic_role_not_exists()

        mgr = ConfigManager(constants)
        template_dict = template.orc8r.to_dict()
        for component, configs in template_dict.items():
            for k, v in configs.items():
                mgr.set(component, k, v)
            mgr.commit(component)

        # run playbooks in order
        if not skip_certs:
            logging.debug("Adding self signed and application certs")
            rc = run_playbook(certs_cmd(constants, self_signed=True))
            if rc != 0:
                raise ClusterCreateError(f"Failed running adding certs")

        if not skip_precheck:
            logging.debug("Running installation prechecks")
            rc = run_playbook(precheck_cmd(constants))
            if rc != 0:
                raise ClusterCreateError(f"Failed running prechecks")

        # create the orc8r cluster
        rc = tf_install(constants, warn=False)
        if rc != 0:
            raise ClusterCreateError(f"Failed installing cluster")

        # update dns record for parent domain
        dns_dict = {
            "domain_name": template.orc8r.infra["orc8r_domain_name"],
        }
        dns_dict.update(constants)
        rc = run_playbook(
            AnsiblePlay(
                playbook=f"{constants['playbooks']}/main.yml",
                tags=['update_dns_records'],
                extra_vars=dns_dict,
            ), )
        if rc != 0:
            raise ClusterCreateError(
                f"Failed updating dns records for parent domain", )

        cluster_config_dict = {
            "uuid": cluster_uuid,
            "cluster_type": ClusterType.AWS,
            "template": template,
        }
        return cluster_config_dict
Beispiel #7
0
    def create_gateways(
        self,
        constants: dict,
        cluster_uuid: str,
        template: ClusterTemplate = None,
    ) -> Dict[str, Any]:
        """ Create AGW gateways in the test cluster

        Args:
            constants (dict): Constants dictionary
            template (ClusterTemplate, optional): Cluster template definition. Defaults to None.
            skip_certs (bool, optional): Skip certs creation. Defaults to False.
            skip_precheck (bool, optional): Skip prechecks. Defaults to False.

        Raises:
            ClusterCreateError: Exception raised when cluster creation fails

        Returns:
            ClusterConfig: Returns the cluster configuration
        """
        # create edge network
        project_dir = constants["project_dir"]
        playbook_dir = constants["cloudstrapper_playbooks"]

        # cloudstrapper expects secrets.yaml to be present
        Path(f"{project_dir}/secrets.yaml").touch()

        aws_configs = get_aws_configs()
        cluster_stack = AWSClusterFactory.generate_cluster_stack(
            template.gateway.prefix,
            template.gateway.count,
        )

        cstrap_dict = {
            "clusterUuid": cluster_uuid,
            "dirLocalInventory": constants["project_dir"],
            "idSite": "TestCluster",
            "testClusterStacks": cluster_stack,
            "awsAccessKey": aws_configs["aws_access_key_id"],
            "awsSecretKey": aws_configs["aws_secret_access_key"],
            "idGw": "dummy_gateway",
            "dirSecretsLocal": constants["secret_dir"],
            "awsAgwAmi": template.gateway.ami,
            "awsCloudstrapperAmi": template.gateway.cloudstrapper_ami,
            "awsAgwRegion": template.gateway.region,
            "awsAgwAz": template.gateway.az,
            "orc8rDomainName": template.orc8r.infra['orc8r_domain_name'],
        }

        key_create = AnsiblePlay(
            playbook=f"{playbook_dir}/aws-prerequisites.yaml",
            tags=['keyCreate'],
            extra_vars=cstrap_dict,
        )
        bridge_gw_create = AnsiblePlay(
            playbook=f"{playbook_dir}/agw-provision.yaml",
            tags=['createNet', 'createBridge', 'inventory'],
            skip_tags=['attachIface'],
            extra_vars=cstrap_dict,
        )

        # create test instances
        test_inst_create = AnsiblePlay(
            playbook=f"{playbook_dir}/cluster-provision.yaml",
            tags=['clusterStart'],
            extra_vars=cstrap_dict,
        )

        jump_config_dict = {"agws": f"tag_Name_TestClusterBridge"}
        jump_config_dict.update(cstrap_dict)
        test_ssh_configure = AnsiblePlay(
            playbook=f"{playbook_dir}/cluster-provision.yaml",
            tags=['clusterJump'],
            extra_vars=jump_config_dict,
        )

        # configure test instances
        agws_config_dict = {
            "agws": f"tag_Name_{template.gateway.prefix}*",
        }
        agws_config_dict.update(template.gateway.service_config)
        agws_config_dict.update(cstrap_dict)
        test_inst_configure = AnsiblePlay(
            inventory=f"{project_dir}/common_instance_aws_ec2.yaml",
            playbook=f"{playbook_dir}/cluster-configure.yaml",
            tags=['exporter', 'clusterConfigure'],
            extra_vars=agws_config_dict,
        )

        max_retries = 3
        for i in range(max_retries):
            fail = False
            for playbook in [
                    key_create,
                    bridge_gw_create,
                    test_inst_create,
                    test_ssh_configure,
                    test_inst_configure,
            ]:
                print(f"Running playbook {playbook}")
                rc = run_playbook(playbook)
                if rc != 0:
                    fail = True
                    print("Failed creating gateway cluster...trying again")
                    break
                # sleep 10 seconds
                time.sleep(10)

            if not fail:
                break

        # get the newly instantiated gateways
        gateways = []
        for gw_info in get_gateways(template.gateway.prefix):
            (gateway_id, hostname) = gw_info
            gateways.append(
                GatewayConfig(
                    gateway_id=gateway_id,
                    hostname=hostname,
                    hardware_id="",
                ), )
        internal_config = ClusterInternalConfig(bastion_ip=get_bastion_ip(), )
        cluster_config_dict = {
            "uuid": cluster_uuid,
            "internal_config": internal_config,
            "cluster_type": ClusterType.AWS,
            "template": template,
            "gateways": gateways,
        }
        return cluster_config_dict