Exemplo n.º 1
0
def create_db_param_group(stack, name, description, family, parameters={}):
    """Create a DB Parameter Group"""
    return stack.stack.add_resource(
        DBParameterGroup('{0}DBParamGroup'.format(name),
                         Description="{0} Parameter Group".format(description),
                         Family=family,
                         Parameters=parameters))
Exemplo n.º 2
0
def set_parameters_groups_from_macro_parameters(db, template):
    """
    Function to set the DB parameters group if ParametersGroups is set on MacroParameters
    """
    if isinstance(db.cfn_resource, DBCluster):
        props = import_record_properties(
            db.parameters["ParametersGroups"], DBClusterParameterGroup
        )
        params = template.add_resource(
            DBClusterParameterGroup(
                CLUSTER_PARAMETER_GROUP_T,
                **props,
            )
        )
        setattr(db.cfn_resource, "DBClusterParameterGroupName", Ref(params))
    elif isinstance(db.cfn_resource, DBInstance):
        props = import_record_properties(
            db.parameters["ParametersGroups"], DBParameterGroup
        )
        params = template.add_resource(
            DBParameterGroup(
                CLUSTER_PARAMETER_GROUP_T,
                **props,
            )
        )
        setattr(db.cfn_resource, "DBParameterGroupName", Ref(params))
Exemplo n.º 3
0
def add_parameter_group(template, db):
    """
    Function to create a parameter group which uses the same values as default which can later be altered

    :param troposphere.Template template: the RDS template
    :param dict db: the db object as imported from Docker composeX file
    """
    db_family = get_family_from_engine_version(
        db["Properties"][DB_ENGINE_NAME.title],
        db["Properties"][DB_ENGINE_VERSION.title],
    )
    if not db_family:
        raise ValueError("Failed to retrieve the DB Family for "
                         f"{db['Properties']['DB_ENGINE_NAME.title']}"
                         f"{db['Properties']['DB_ENGINE_VERSION.title']}")
    db_settings = get_family_settings(db_family)
    DBParameterGroup(
        PARAMETER_GROUP_T,
        template=template,
        Family=db_family,
        Parameters=db_settings,
        Condition=rds_conditions.NOT_USE_CLUSTER_CON_T,
    )
    DBClusterParameterGroup(
        CLUSTER_PARAMETER_GROUP_T,
        template=template,
        Condition=rds_conditions.USE_CLUSTER_CON_T,
        Family=db_family,
        Parameters=db_settings,
        Description=Sub(f"RDS Settings copy for {db_family}"),
    )
Exemplo n.º 4
0
 def create_parameter_group(self):
     t = self.template
     params = self.local_parameters["DatabaseParameters"]
     t.add_resource(
         DBParameterGroup(
             "ParameterGroup",
             Description=self.name,
             Family=Ref("DBFamily"),
             Parameters=params,
         ))
Exemplo n.º 5
0
 def create_parameter_group(self):
     t = self.template
     variables = self.get_variables()
     params = variables["DatabaseParameters"]
     t.add_resource(
         DBParameterGroup(
             "ParameterGroup",
             Description=self.name,
             Family=variables["DBFamily"],
             Parameters=params,
         ))
IgwSubnet2RouteAssociation = template.add_resource(
    SubnetRouteTableAssociation(
        "IgwSubnet2RouteAssociation",
        SubnetId=Ref(IgwSubnet2),
        RouteTableId=Ref(IgwRouteTable),
        DependsOn="IgwRouteDefault",
    ))

RDSDBParameterGroup = template.add_resource(
    DBParameterGroup(
        "RDSDBParameterGroup",
        Parameters={
            "sql_mode": "STRICT_ALL_TABLES,STRICT_TRANS_TABLES",
            "innodb_strict_mode": 1
        },
        Description="Custom Parameters Make it strict",
        Family="mysql5.7",
        Tags=Tags(Name=Sub("${EnvironmentName} RDS Parameter Group"), ),
        DependsOn=[
            "Private1RouteTableAssociation", "Private2RouteTableAssociation"
        ],
    ))

InternalSecurityGroup = template.add_resource(
    SecurityGroup(
        "InternalSecurityGroup",
        SecurityGroupIngress=[{
            "ToPort": "-1",
            "IpProtocol": "-1",
            "CidrIp": Ref(VpcCIDR),
            "FromPort": "-1"
    def add_rds(self, name, engine, username, password, storage,
                db_instance_type, multiaz, encrypt, cidr, hosted_zone):
        """
        Helper method creates ingress given a source cidr range and a set of
        ports
        @param name [string] Sets name for the RDS instance
        @param engine [string] Tells RDS what database type is needed
        @param username [string] Sets the username for the database
        @param password [string] Sets the password for the database
        @param storage [int] Sets the size of the database
        @param db_instance_type [string] Instance for the application
        @param multiaz [Bool] Status of if MultiAZ or not
        @param cidr [string] Range of addresses for this vpc
        @param hosted_zone [string] Name of the hosted zone the elb will be
        mapped to
        """
        print "Creating RDS: %s" % name
        print 'MultiAZ' + str(multiaz)

        # Add cloudwatch policy
        policies_for_profile = [self.get_cfn_policy()]
        for policy in ('cloudwatch', 's3', 'createtags', 'route53'):
            policies_for_profile.append(self.get_policy(policy, name))

        private_subnets = [{
            "Ref": "privateAZ0"
        }, {
            "Ref": "privateAZ1"
        }, {
            "Ref": "privateAZ2"
        }]

        if engine == 'postgres':
            rds_port = 5432
            family = 'postgres9.5'
            group_parameters = {
                'rds.force_ssl': '1',
                'log_min_duration_statement': '100',
                'log_statement': 'all'
            }
        if engine == 'MySQL':
            rds_port = 3306
            family = 'mysql5.1'
            group_parameters = {}
        if engine == 'mariadb':
            rds_port = 3306
            family = 'mariadb10.1'
            group_parameters = {}

        rds_security_group = self.add_simple_sg_with_cidr(
            name, 'RDSSecurityGroup' + name, 'vpcId', cidr, rds_port, rds_port,
            'tcp')

        rds_parameter_group = self.add_resource(
            DBParameterGroup('RDSParameterGroup' + name,
                             Description='%s DB Parameter Group' % name,
                             Family=family,
                             Parameters=group_parameters))

        # rds_security_group = self.add_rds_security_group(name, self.vpc_id)
        rds_subnet_group = self.add_rds_db_subnet(name, private_subnets)

        client = boto3.client('kms')
        response = client.decrypt(CiphertextBlob=base64.b64decode(password))
        password = response['Plaintext']
        print('Master Password is: %s' % password)
        rds_database = self.add_rds_database(name, engine, username, password,
                                             storage, db_instance_type,
                                             rds_subnet_group,
                                             rds_security_group, multiaz,
                                             rds_parameter_group, encrypt)

        self.add_rds_dns_alias(rds_database, name, hosted_zone)
    "DBPassword",
    NoEcho=True,
    Description="The database admin account password",
    Type="String",
    MinLength="1",
    MaxLength="41",
    AllowedPattern="[a-zA-Z0-9]*",
    ConstraintDescription="must contain only alphanumeric characters."
))


myrdsparamgroup = t.add_resource(DBParameterGroup(
    "MyRDSParamGroup",
    Family="MySQL5.5",
    Description="CloudFormation Sample Database Parameter Group",
    Parameters={
        "autocommit": "1",
        "general_log": "1",
        "old_passwords": "0"
    }
))

mydb = t.add_resource(DBInstance(
    "MyDB",
    AllocatedStorage="5",
    DBInstanceClass="db.m1.small",
    Engine="MySQL",
    EngineVersion="5.5",
    MasterUsername=Ref(dbuser),
    MasterUserPassword=Ref(dbpassword),
    DBParameterGroupName=Ref(myrdsparamgroup),
))
Exemplo n.º 9
0
def add_parameter_group(template, db):
    """
    Function to create a parameter group which uses the same values as default which can later be altered

    :param troposphere.Template template: the RDS template
    :param db: the db object as imported from Docker composeX file
    :type db: ecs_composex.common.compose_resources.Rds
    """

    parameters_properties = [
        "DBClusterParameterGroupName",
        "DBParameterGroupName",
    ]
    if db.properties and not any(
        key in parameters_properties for key in db.properties.keys()
    ):
        LOG.info(f"Parameter group was already set for {db.name}")
        return
    elif (
        not db.properties
        and db.parameters
        and keyisset("ParametersGroups", db.parameters)
    ) or (
        db.properties
        and not any(key in parameters_properties for key in db.properties.keys())
        and db.parameters
        and keyisset("ParametersGroups", db.parameters)
    ):
        set_parameters_groups_from_macro_parameters(db, template)
        return

    if (
        db.properties
        and keyisset(DB_ENGINE_NAME.title, db.properties)
        and keyisset(DB_ENGINE_VERSION.title, db.properties)
    ):
        db_family = get_family_from_engine_version(
            db.properties[DB_ENGINE_NAME.title],
            db.properties[DB_ENGINE_VERSION.title],
        )

    elif (
        not db.properties
        and db.parameters
        and not keyisset("ParametersGroups", db.parameters)
    ):
        db_family = get_family_from_engine_version(
            db.parameters[DB_ENGINE_NAME.title],
            db.parameters[DB_ENGINE_VERSION.title],
        )
    else:
        raise RuntimeError("Failed to determine the DB Parameters family.", db.name)
    db_settings = get_family_settings(db_family)
    if isinstance(db.cfn_resource, DBInstance):
        params = DBParameterGroup(
            PARAMETER_GROUP_T,
            template=template,
            Family=db_family,
            Parameters=db_settings,
        )
        setattr(db.cfn_resource, "DBParameterGroupName", Ref(params))
    elif isinstance(db.cfn_resource, DBCluster):
        params = DBClusterParameterGroup(
            CLUSTER_PARAMETER_GROUP_T,
            template=template,
            Family=db_family,
            Parameters=db_settings,
            Description=Sub(f"RDS Settings copy for {db_family}"),
        )
        setattr(db.cfn_resource, "DBClusterParameterGroupName", Ref(params))