Пример #1
0
class RedshiftCluster(redshift.RedshiftClusterResource):
    cluster_identifier = "data"
    database_name = "pacbot_data"
    master_username = "******"
    master_password = "******"
    node_type = "dc2.large"
    cluster_type = "single-node"
    number_of_nodes = 1
    skip_final_snapshot = True
    publicly_accessible = False
    vpc_security_group_ids = [InfraSecurityGroupResource.get_output_attr('id')]
    cluster_parameter_group_name = RedshiftParameterGroup.get_output_attr('name')
    cluster_subnet_group_name = RedshiftSubnetGroup.get_output_attr('name')

    @classmethod
    def get_redshift_info(cls):
        info = "%s:%s" % (cls.get_input_attr('master_username'), cls.get_input_attr('master_password'))

        return base64.b64encode(info.encode()).decode()  # Since base64 takes up only bytes we need to encode then decode

    @classmethod
    def get_redshift_url(cls):
        endpoint = cls.get_output_attr('endpoint')
        dbname = cls.get_input_attr('database_name')

        return "jdbc:redshift://%s/%s" % (endpoint, dbname)

    def render_output(self, outputs):
        if self.resource_in_tf_output(outputs):
            return {
                'Redshift Host': outputs[self.get_resource_id()]['endpoint'],
                'Redshift DB': self.get_input_attr('database_name')
            }
Пример #2
0
class ApplicationLoadBalancer(LoadBalancerResource):
    name = ""
    internal = True
    load_balancer_type = "application"
    security_groups = [InfraSecurityGroupResource.get_output_attr('id')]
    subnets = Settings.get('VPC')['SUBNETS']

    OUTPUT_LIST = ['dns_name']

    @classmethod
    def get_http_url(cls):
        return "http://%s" % cls.get_output_attr('dns_name')

    @classmethod
    def get_api_base_url(cls):
        return "http://%s/api" % cls.get_output_attr('dns_name')

    @classmethod
    def get_api_version_url(cls, service):
        version_url = cls.get_api_server_url(service)
        return version_url if service == "auth" else version_url + "/v1"

    @classmethod
    def get_api_server_url(cls, service):
        return "%s/%s" % (cls.get_api_base_url(), service)

    def render_output(self, outputs):
        if self.resource_in_tf_output(outputs):
            return {
                'Pacbot Domain': outputs[self.get_resource_id()]['dns_name'],
                'Admin': Settings.PACBOT_LOGIN_CREDENTIALS['Admin'],
                'User': Settings.PACBOT_LOGIN_CREDENTIALS['User']
            }
Пример #3
0
class RuleEngineBatchJobEnv(BatchComputeEnvironmentResource):
    compute_environment_name = ""
    instance_role = ECSRoleInstanceProfile.get_output_attr('arn')
    instance_type = ["m4.xlarge"]
    max_vcpus = 256
    min_vcpus = 0
    desired_vcpus = 0
    ec2_key_pair = ""
    resource_type = "EC2"
    security_group_ids = [InfraSecurityGroupResource.get_output_attr('id')]
    subnets = Settings.get('VPC')['SUBNETS']
    env_type = "MANAGED"
    service_role = BatchRole.get_output_attr('arn')
    compute_resources_tags = get_all_resource_tags()

    DEPENDS_ON = [
        BatchIAMRolePolicyAttach
    ]  # This is required otherwise policy would be dettached from Batchrole

    def pre_terraform_apply(self):
        ec2_client = get_ec2_client(self.input.AWS_AUTH_CRED)
        ec2_key_pair = self.get_input_attr('ec2_key_pair')
        try:
            key_obj = ec2_client.create_key_pair(KeyName=ec2_key_pair)
            with open(os.path.join(Settings.OUTPUT_DIR, ec2_key_pair + ".pem"),
                      "w") as keyfile:
                keyfile.write(key_obj['KeyMaterial'])
        except Exception as e:
            pass

    def check_batch_jobs_running(self):
        envs = get_compute_environments(
            [self.get_input_attr('compute_environment_name')],
            self.input.AWS_AUTH_CRED)

        if not len(envs):
            return

        if envs[0]['computeResources']['desiredvCpus'] > int(
                self.get_input_attr('desired_vcpus')):
            return True

    def pre_generate_terraform(self):
        warn_msg = "Batch Jobs are running, please try after it gets completed and desired CPUs comes to 0."
        if self.check_batch_jobs_running():
            message = "\n\t ** %s **\n" % warn_msg
            print(MsgMixin.BERROR_ANSI + message + MsgMixin.RESET_ANSI)
            sys.exit()

    def post_terraform_destroy(self):
        ec2_client = get_ec2_client(self.input.AWS_AUTH_CRED)
        ec2_key_pair = self.get_input_attr('ec2_key_pair')
        try:
            key_obj = ec2_client.delete_key_pair(KeyName=ec2_key_pair)
        except Exception as e:
            print(ec2_key_pair +
                  " Not able to delete Key Pair. Error: %s" % str(e))
Пример #4
0
class BaseEcsService:
    desired_count = 1
    launch_type = "FARGATE"
    cluster = ApplicationECSCluster.get_output_attr('id')
    network_configuration_security_groups = [InfraSecurityGroupResource.get_output_attr('id')]
    network_configuration_subnets = Settings.get('VPC')['SUBNETS']
    network_configuration_assign_public_ip = True
    load_balancer_container_port = 80
    tags = None
Пример #5
0
class MySQLDatabase(RDSResource):
    name = "pacmandata"
    instance_class = Settings.get('RDS_INSTANCE_TYPE', "db.t2.medium")
    identifier = "data"
    storage_type = "gp2"
    engine = "mysql"
    engine_version = "5.6.40"
    allocated_storage = 10
    username = "******"
    password = "******"
    parameter_group_name = DBParameterGroup.get_input_attr('name')
    option_group_name = DBOptionGroup.get_input_attr('name')
    db_subnet_group_name = DBSubnetGroup.get_input_attr('name')
    vpc_security_group_ids = [InfraSecurityGroupResource.get_output_attr('id')]
    skip_final_snapshot = True
    apply_immediately = True

    DEPENDS_ON = [DBOptionGroup, DBParameterGroup, DBSubnetGroup]

    @classmethod
    def get_rds_info(cls):
        info = "%s:%s" % (cls.get_input_attr('username'),
                          cls.get_input_attr('password'))

        return base64.b64encode(info.encode()).decode()

    @classmethod
    def get_rds_db_url(cls):
        rds_endpoint = cls.get_output_attr('endpoint')
        db_name = cls.get_input_attr('name')

        return "jdbc:mysql://%s/%s" % (rds_endpoint, db_name)

    def render_output(self, outputs):
        if self.resource_in_tf_output(outputs):
            return {
                'MySQL Host': outputs[self.get_resource_id()]['endpoint'],
                'MySQL DB': self.get_input_attr('name')
            }

    def pre_terraform_apply(self):
        status, msg = create_iam_service_linked_role(
            Settings.AWS_ACCESS_KEY, Settings.AWS_SECRET_KEY,
            "rds.amazonaws.com", Settings.RESOURCE_DESCRIPTION)

        SysLog().write_debug_log(
            "RDS IAM Service Linked role creation: Status:%s, Message: %s" %
            (str(status), msg))
Пример #6
0
class ESDomain(ElasticsearchDomainResource):
    domain_name = "data"
    elasticsearch_version = "5.5"
    instance_type = Settings.get('ES_INSTANCE_TYPE', "m4.large.elasticsearch")
    instance_count = 1
    dedicated_master_enabled = False
    zone_awareness_enabled = False
    ebs_enabled = True
    volume_type = "gp2"
    volume_size = 20
    automated_snapshot_start_hour = 23
    security_group_ids = [InfraSecurityGroupResource.get_output_attr('id')]
    subnet_ids = [Settings.get('VPC')['SUBNETS'][0]]
    cloudwatch_log_group_arn = ESCloudWatchLogGroup.get_output_attr('arn')
    log_type = "ES_APPLICATION_LOGS"

    @classmethod
    def get_http_url_with_port(cls):
        return "%s:%s" % (cls.get_http_url(), "80")

    @classmethod
    def get_http_url(cls):
        return "http://%s" % cls.get_output_attr('endpoint')

    @classmethod
    def get_es_port(cls):
        return 80

    def pre_terraform_apply(self):
        status, msg = create_iam_service_linked_role(
            Settings.AWS_ACCESS_KEY,
            Settings.AWS_SECRET_KEY,
            "es.amazonaws.com",
            Settings.RESOURCE_DESCRIPTION)

        SysLog().write_debug_log("ElasticSearch IAM Service Linked role creation: Status:%s, Message: %s" % (str(status), msg))

    def render_output(self, outputs):
        if self.resource_in_tf_output(outputs):
            resource_id = self.get_resource_id()
            return {
              'ES Host': outputs[resource_id]['endpoint'],
              'Kibana Host': outputs[resource_id]['kibana_endpoint']
            }
Пример #7
0
class ApplicationLoadBalancer(LoadBalancerResource):
    name = ""
    internal = Settings.get('MAKE_ALB_INTERNAL', False)
    load_balancer_type = "application"
    security_groups = [InfraSecurityGroupResource.get_output_attr('id')]
    subnets = Settings.get('VPC')['SUBNETS']

    OUTPUT_LIST = ['dns_name']

    @classmethod
    def get_http_url(cls):
        pacbot_domain = cls.get_output_attr('dns_name')
        return "%s://%s" % ("http", pacbot_domain)

        # TODO: Replace with this once dev team fix https issue
        # pacbot_domain = Settings.get('PACBOT_DOMAIN', None)
        # pacbot_domain = pacbot_domain if pacbot_domain else cls.get_output_attr('dns_name')
        # return "%s://%s" % (Settings.get('ALB_PROTOCOL', "HTTP").lower(), pacbot_domain)

    @classmethod
    def get_pacbot_domain_url(cls):
        pacbot_domain = Settings.get('PACBOT_DOMAIN', None)
        pacbot_domain = pacbot_domain if pacbot_domain else cls.get_output_attr(
            'dns_name')

        return "%s://%s" % (Settings.get('ALB_PROTOCOL',
                                         "HTTP").lower(), pacbot_domain)

    @classmethod
    def get_api_base_url(cls):
        pacbot_domain = cls.get_output_attr('dns_name')
        return "%s://%s/api" % ("http", pacbot_domain)

        # TODO: Replace with this once dev team fix https issue
        # pacbot_domain = Settings.get('PACBOT_DOMAIN', None)
        # pacbot_domain = pacbot_domain if pacbot_domain else cls.get_output_attr('dns_name')
        # return "%s://%s/api" % (Settings.get('ALB_PROTOCOL', "HTTP").lower(), pacbot_domain)

    @classmethod
    def get_api_version_url(cls, service):
        version_url = cls.get_api_server_url(service)
        return version_url if service == "auth" else version_url + "/v1"

    @classmethod
    def get_api_server_url(cls, service):
        return "%s/%s" % (cls.get_api_base_url(), service)

    def _get_printable_abs_url(self, dns_name):
        """
        This function returns the absolute URL of the domain ie. with http/https

        Args:
            dns_name (str): Loadbalancer dns name

        Returns:
            url (str): abs url of pacbot
        """
        pacbot_domain = Settings.get('PACBOT_DOMAIN', None)
        pacbot_domain = pacbot_domain if pacbot_domain else dns_name

        return "%s://%s" % (Settings.get('ALB_PROTOCOL',
                                         "HTTP").lower(), pacbot_domain)

    def render_output(self, outputs):
        if self.resource_in_tf_output(outputs):
            abs_url = self._get_printable_abs_url(
                outputs[self.get_resource_id()]['dns_name'])
            return {
                'Pacbot URL': abs_url,
                'Admin': Settings.PACBOT_LOGIN_CREDENTIALS['Admin'],
                'User': Settings.PACBOT_LOGIN_CREDENTIALS['User']
            }