Ejemplo n.º 1
0
    def activate(self):
        conn = util.as_conn()
        conn = boto.ec2.autoscale.connect_to_region('us-west-2')

        name = self.name()

        # check if this LC already exists
        if self.exists():
            if not util.confirm("LaunchConfig {} already exists, overwrite?".format(name)):
                return True
            # delete existing
            conn.delete_launch_configuration(name)

        # get configuration for this LC
        cfg = self.role_config
        lc = LaunchConfiguration(
            name = name,
            image_id = cfg.get('ami'),
            instance_profile_name = cfg.get('iam_profile'),
            instance_type = cfg.get('instance_type'),
            security_groups = cfg.get('security_groups'),
            key_name = cfg.get('keypair_name'),
            user_data = self.cloud_init_script(),
            associate_public_ip_address = True,  # this is required for your shit to actually work
        )
        if not conn.create_launch_configuration(lc):
            print "Error creating LaunchConfig {}".format(name)
            return False

        util.message_integrations("Activated LaunchConfig {}".format(name))

        return lc
Ejemplo n.º 2
0
def create_autoscaling_group(autoscale, cluster_name, master_node, opts,
                             slave_group):
    lclist = autoscale.get_all_launch_configurations(
        names=[cluster_name + "-lc"])
    if lclist:
        lc = lclist[0]
    else:
        lc = LaunchConfiguration(name=cluster_name + "-lc",
                                 image_id=opts.ami,
                                 key_name=opts.key_pair,
                                 security_groups=[slave_group.id],
                                 instance_type=opts.instance_type,
                                 user_data="SPARK_MASTER=" +
                                 master_node.private_dns_name + "\n",
                                 instance_monitoring=True,
                                 spot_price=opts.max_spot_price)
        autoscale.create_launch_configuration(lc)
    aglist = autoscale.get_all_groups(names=[cluster_name + "-ag"])
    if aglist:
        ag = aglist[0]
    else:
        ag = AutoScalingGroup(group_name=cluster_name + "-ag",
                              launch_config=lc,
                              min_size=opts.min_instances,
                              max_size=opts.max_instances,
                              connection=autoscale,
                              vpc_zone_identifier=opts.subnet_id,
                              availability_zones=[opts.zone])
        autoscale.create_auto_scaling_group(ag)
    as_tag = boto.ec2.autoscale.Tag(key='Name',
                                    value=cluster_name + '-worker',
                                    propagate_at_launch=True,
                                    resource_id=cluster_name + "-ag")
    autoscale.create_or_update_tags([as_tag])
Ejemplo n.º 3
0
def aws_update_autoscaler():
    """
    Update auto-scaling configuration for the configured (see env.aws) scaler
    """
    ami_id = aws_create_ami_from()
    cur_date = time.strftime('%Y%m%d', time.gmtime())
    lcName = 'ns11-%s' % cur_date
    lc = LaunchConfiguration(name=lcName,
                             image_id=ami_id,
                             instance_type=env.aws.get('instance_type'),
                             key_name=env.aws.get('key_pair'),
                             security_groups=env.aws.get('security_groups'))
    env.asConn.create_launch_configuration(lc)
    print "Created launchConfiguration %s" % lcName

    ag = AutoScalingGroup(connection=env.asConn,
                          launch_config=lc,
                          group_name=env.aws.get('as_group'),
                          load_balancers=env.aws.get('balancers'),
                          availability_zones=env.aws.get('availability_zones'))
    # min_size=env.aws.get('min_size'), max_size=env.aws.get('max_size'))
    ag.update()
    # env.asConn.create_auto_scaling_group(ag)
    print "Added launchConfiguration %s to group %s (updated AutoScaleGroup)" % (
        lcName, env.aws.get('as_group'))
Ejemplo n.º 4
0
def create_launch_config(connection, module):
    name = module.params.get('name')
    image_id = module.params.get('image_id')
    key_name = module.params.get('key_name')
    security_groups = module.params['security_groups']
    user_data = module.params.get('user_data')
    volumes = module.params['volumes']
    instance_type = module.params.get('instance_type')
    spot_price = module.params.get('spot_price')
    instance_monitoring = module.params.get('instance_monitoring')
    assign_public_ip = module.params.get('assign_public_ip')
    kernel_id = module.params.get('kernel_id')
    ramdisk_id = module.params.get('ramdisk_id')
    instance_profile_name = module.params.get('instance_profile_name')
    ebs_optimized = module.params.get('ebs_optimized')
    classic_link_vpc_id = module.params.get('classic_link_vpc_id')
    classic_link_vpc_security_groups = module.params.get(
        'classic_link_vpc_security_groups')
    bdm = BlockDeviceMapping()

    if volumes:
        for volume in volumes:
            if 'device_name' not in volume:
                module.fail_json(msg='Device name must be set for volume')
            # Minimum volume size is 1GB. We'll use volume size explicitly set to 0
            # to be a signal not to create this volume
            if 'volume_size' not in volume or int(volume['volume_size']) > 0:
                bdm[volume['device_name']] = create_block_device(
                    module, volume)

    lc = LaunchConfiguration(
        name=name,
        image_id=image_id,
        key_name=key_name,
        security_groups=security_groups,
        user_data=user_data,
        block_device_mappings=[bdm],
        instance_type=instance_type,
        kernel_id=kernel_id,
        spot_price=spot_price,
        instance_monitoring=instance_monitoring,
        associate_public_ip_address=assign_public_ip,
        ramdisk_id=ramdisk_id,
        instance_profile_name=instance_profile_name,
        ebs_optimized=ebs_optimized,
        classic_link_vpc_security_groups=classic_link_vpc_security_groups,
        classic_link_vpc_id=classic_link_vpc_id,
    )

    launch_configs = connection.get_all_launch_configurations(names=[name])
    changed = False
    if not launch_configs:
        try:
            connection.create_launch_configuration(lc)
            launch_configs = connection.get_all_launch_configurations(
                names=[name])
            changed = True
        except BotoServerError, e:
            module.fail_json(msg=str(e))
Ejemplo n.º 5
0
def create_launch_config(_asg):
    lc = LaunchConfiguration(name=C['lc_name'], image_id=C['ami'],
                             key_name=C['key_name'],
                             security_groups=[C['sec_group_id']])
    lcs = _asg.get_all_launch_configurations(names=[C['lc_name']])
    if len(lcs) == 0:
        _asg.create_launch_configuration(lc)
    return lc
Ejemplo n.º 6
0
 def SetupNew(self):
     lc = LaunchConfiguration(name=self.name,
                              image_id=self.input_config.spot.ami,
                              user_data=self.input_config.bootstrap.script,
                              spot_price=self.input_config.spot.bid_price,
                              instance_type=self.input_config.spot.instance_type)
     print 'Creating launch config: %s' % self.name
     self.launch_config = self.autoscale_conn.create_launch_configuration(lc)
Ejemplo n.º 7
0
def create_lc(autoscale, name, image_id, instance_type, key_name,
        security_groups, instance_monitoring):
    l = LaunchConfiguration(name=name, image_id=image_id,
            instance_type=instance_type,key_name=key_name,
            security_groups=security_groups,
            instance_monitoring=instance_monitoring)
    l = autoscale.create_launch_configuration(l)
    return list_lc(autoscale)
Ejemplo n.º 8
0
def launch_config(lcname, image_id):
    '''Create a launch configuration for auto scalling group'''
    lc = LaunchConfiguration(name=lcname,
                             image_id=image_id,
                             instance_type='c3.large',
                             key_name='allservers',
                             security_groups=[('sg-0518e960')])
    as_conn.create_launch_configuration(lc)
Ejemplo n.º 9
0
def _create_launch_configuration(c, opts):
    lc = LaunchConfiguration(name=opts.name + s.LAUNCH_CONFIG_SUFFIX,
            image_id=opts.image,
            key_name=opts.key,
            security_groups=[opts.group],
            instance_type=opts.type)
    c.create_launch_configuration(lc)
    return lc
Ejemplo n.º 10
0
 def launch_config(self, conn, launch_name, key_name):
     """ Creates a new launch config """
     lc = LaunchConfiguration(name=launch_name, image_id='ami-e1398992',
                              instance_type='t2.micro', key_name=key_name)
     cf = conn.create_launch_configuration(lc)
     if cf:
         print "Launch Config Created"
     else:
         "Error creating launch config"
Ejemplo n.º 11
0
    def create_launch_config(lconfig_name, image_id, inst_type, key_name, security_groups,
                             spot_price=0,
                             user_data_file=None,
                             user_data=None,
                             block_dev_mappings=None,
                             ebs_optimized=False,
                             overwrite=False):

        existing_config = Cluster.get_launch_config(lconfig_name)
        if existing_config is not None:
            if overwrite:
                existing_config.delete()
                Cluster.log_info("Deleted launch config %s to overwrite new config", lconfig_name)
            else:
                Cluster.log_error("Launch config %s already exists.", lconfig_name)
                raise Exception("Launch configuration already exists")

        auto_scale_conn = Cluster._autoscale()

        if user_data is None:
            if user_data_file is not None:
                with open(user_data_file, 'r') as udf:
                    user_data = udf.read()

        lconfig = LaunchConfiguration()
        lconfig.instance_type = inst_type
        lconfig.name = lconfig_name
        lconfig.image_id = image_id
        lconfig.key_name = key_name
        lconfig.security_groups = security_groups
        lconfig.user_data = user_data

        if spot_price > 0:
            lconfig.spot_price = spot_price

        if block_dev_mappings is not None:
            lconfig.block_device_mappings = block_dev_mappings

        if ebs_optimized:
            lconfig.ebs_optimized = True

        auto_scale_conn.create_launch_configuration(lconfig)
        Cluster.log_info("Created launch configuration %s", lconfig.name)
Ejemplo n.º 12
0
 def createLaunchConfiguration(self, lc_name, ami_id, key_name):
     """
     Creates launch configuration for the auto scaling cluster
     """
     self.launchConfiguration = LaunchConfiguration(name=lc_name,
                                                    image_id=ami_id,
                                                    key_name=key_name)
     self.autoscale_connection.create_launch_configuration(
         self.launchConfiguration)
     print ">>> Created launch configuration: " + lc_name
Ejemplo n.º 13
0
def replace_launch_config(name,
                          image_id=None,
                          key_name=None,
                          security_groups=None,
                          instance_type=None,
                          instance_monitoring=None):
    """Replace launch configuration associated with auto scaling group."""

    conn = boto.connect_autoscale()
    as_group = conn.get_all_groups(['{0}-as-group'.format(name)],
                                   max_records=1)[0]
    orig_launch_config_name = as_group.launch_config_name

    orig_launch_config = conn.get_all_launch_configurations(
        names=[orig_launch_config_name], max_records=1)[0]

    if not image_id:
        image_id = orig_launch_config.image_id
    if not key_name:
        key_name = orig_launch_config.key_name
    if not security_groups:
        security_groups = orig_launch_config.security_groups
    if not instance_type:
        instance_type = orig_launch_config.instance_type
    if not instance_monitoring:
        instance_monitoring = orig_launch_config.instance_monitoring

    config_name = "{0}_{1}".format(name, datetime.now().strftime("%Y%m%d%H%M"))

    puts("Replace launch config {0} with {1}".format(orig_launch_config_name,
                                                     config_name))

    new_launch_config = LaunchConfiguration(
        name=config_name,
        image_id=image_id,
        key_name=key_name,
        security_groups=security_groups,
        instance_type=instance_type,
        instance_monitoring=instance_monitoring)
    conn.create_launch_configuration(new_launch_config)

    min_size = int(as_group.min_size)
    max_size = int(as_group.max_size)

    as_group.launch_config_name = config_name
    # We need to setup min & max size again
    as_group.min_size = min_size
    as_group.max_size = max_size
    as_group.update()

    orig_launch_config.delete()
    return orig_launch_config_name, config_name
Ejemplo n.º 14
0
def main(REGION, NEW_AMI_ID, ASG_NAME, SERVICE_NAME, SECURITY_GROUPS):
    print('Creating new launch config in the "{0}" region.'.format(REGION))
    asConnection = boto.ec2.autoscale.connect_to_region(REGION)
    try:
        group = asConnection.get_all_groups([ASG_NAME])[0]
    except IndexError as i:
        print('Apparently there is no group named "{0}" in the "{1}" region.'.
              format(ASG_NAME, REGION))
        return False  # This region does not have the ASG we are looking for, moving on to next region.

    if group.launch_config_name == SERVICE_NAME:
        exit('Launch Config name must be unique: {0}'.format(SERVICE_NAME))

    oldLC = asConnection.get_all_launch_configurations(
        names=[group.launch_config_name])[0]
    if not oldLC:
        exit('Old launch configuration does not exist')

    if oldLC.instance_monitoring.enabled == 'true':
        monitoring = True
    else:
        monitoring = False

    launchconfig = LaunchConfiguration(
        name=SERVICE_NAME,
        image_id=NEW_AMI_ID,
        instance_profile_name=oldLC.instance_profile_name,
        key_name=oldLC.key_name,
        ebs_optimized=oldLC.ebs_optimized,
        user_data=oldLC.user_data,
        instance_type=oldLC.instance_type,
        instance_monitoring=monitoring,
        security_groups=SECURITY_GROUPS,
    )
    # create returns a request id
    if asConnection.create_launch_configuration(launchconfig):
        if group:
            group.launch_config_name = SERVICE_NAME
            group.update()

        # The old launch configuration is no longer attached to a group, we can delete it
        #delete_result = asConnection.delete_launch_configuration(oldLC.name) # delete returns request id

        print('Created the launch config "{0}" in the "{1}" region.'.format(
            SERVICE_NAME, REGION))
        return True
    else:
        print('Failed to create a new launch configuration in "{0}" region.'.
              format(REGION))
        return False
Ejemplo n.º 15
0
    def create_launch_config(self, name=None, image_id=None, key_name=None, security_groups=None):
        """
        Creates a new launch configuration with specified attributes.

        :param name: Name of the launch configuration to create. (Required)
        :param image_id: Unique ID of the Amazon Machine Image (AMI) assigned during registration. (Required)
        :param key_name: The name of the EC2 key pair.
        :param security_groups: Names of the security groups with which to associate the EC2 instances.
        """
        lc = LaunchConfiguration(name=name,
                                 image_id=image_id,
                                 key_name=key_name,
                                 security_groups=security_groups)
        self.debug("Creating launch config: " + name)
        self.autoscale.create_launch_configuration(lc)
Ejemplo n.º 16
0
def create_autoscaler(region_name, vpc, elb, subnet, sg, name, aws_config,
                      as_config):
    print "Creating auto scaler %s" % name
    conn = boto.ec2.autoscale.connect_to_region(region_name)
    asg_list = conn.get_all_groups(names=[name])
    if not len(asg_list):
        lc_name = name + "-LC"
        lc_list = conn.get_all_launch_configurations(names=[lc_name])
        if not len(lc_list):
            print "Creating Launch Configuration (%s)" % lc_name
            lc = LaunchConfiguration(
                name=lc_name,
                image_id=as_config["ami_id"],
                key_name=as_config["key_pair"],
                security_groups=[sg.id],
                user_data=as_config["user_data"],
                instance_type=as_config["instance_type"],
                instance_monitoring=as_config["instance_monitoring"],
                associate_public_ip_address=True)
            conn.create_launch_configuration(lc)
        else:
            lc = lc_list[0]
            print "Launch Configuration (%s) already exists" % lc_name
        tag = boto.ec2.autoscale.tag.Tag(key="Name",
                                         value=name + "Instance",
                                         propagate_at_launch=True,
                                         resource_id=name)
        asg = AutoScalingGroup(group_name=name,
                               load_balancers=[elb.name],
                               availability_zones=aws_config["zones"],
                               launch_config=lc,
                               min_size=as_config["min_size"],
                               max_size=as_config["max_size"],
                               vpc_zone_identifier=[subnet.id],
                               health_check_type="ELB",
                               health_check_period="300",
                               tags=[tag],
                               connection=conn)
        conn.create_auto_scaling_group(asg)
        print "Created Auto Scaler Group (%s) for VPC(%s)" % (asg.name, vpc.id)
    else:
        asg = asg_list[0]
        print "Auto Scaler Group (%s) found for VPC(%s)" % (asg.name,
                                                            elb.vpc_id)
    for act in conn.get_all_activities(asg):
        print "Activiity %s" % act
    return asg
Ejemplo n.º 17
0
def create_launch_config():
    # Load the User Data file
    with open(USER_DATA_SCRIPT_FILE) as userDataScriptFile:
        userDataScript = userDataScriptFile.read()

    print('\nEnter a name for the Launch Configuration')
    lcName = raw_input(': ')
    # Make the Launch Configuration name unique with a timestamp
    lcName = lcName + '-' + datetime.datetime.utcnow().strftime(
        '%Y.%m.%d-%H%M%S')

    # Use Monitoring?
    # Use User Data?
    # Use load balancers?
    # Use ami from config or specify?
    # Use IAM role?
    global INSTANCE_PROFILE_NAME
    if INSTANCE_PROFILE_NAME:
        print('Use default or specify?')
    else:
        print(
            'Warning: You have not configured your INSTANCE_PROFILE_NAME. Disabling use of IAM role.'
        )
        INSTANCE_PROFILE_NAME = None

    exit()

    launchconfig = LaunchConfiguration(
        name=lcName,
        image_id=REGIONS[CURRENT_REGION]['image'],
        instance_type=select_instance_type(),
        key_name=select_key_pair(),
        security_groups=select_security_groups(),
        instance_profile_name=INSTANCE_PROFILE_NAME,
        user_data=userDataScript)
    # create returns a request id
    if asConnection.create_launch_configuration(launchconfig):
        print('Successfully created a new launch configuration.')
        return lcName
    else:
        print('Failed to create a new launch configuration.')
        return False
Ejemplo n.º 18
0
    def create_as_group(conn_as, launch_config_name, as_group_name,
                        min_instances, max_instances):
        """ This method is tasked with the creation of both a Launch Configuration and an Auto Scaling Group
        based on the user requested names and features"""

        try:
            # Here we create the Launch configuration (a "how to" for the auto scaling group to start its instances)
            # and select from the constructor the values we need or want to use, in this case, we will hard-code
            # the ami and the instance type with free tier instances, we will retrieve from boto the AWS key_name
            # so anyone can use the interface without having to hard-code it.
            lc = LaunchConfiguration(name=launch_config_name,
                                     image_id='ami-c6972fb5',
                                     instance_type='t2.micro',
                                     key_name=config.get(
                                         'Credentials', 'key_name'),
                                     security_groups=[])

            # Once created the launch configuration it's time to commit the configuration to AWS
            conn_as.create_launch_configuration(lc)
            print "Launch configuration created"

            # Then we move on to the AutoScaling group creation, group that includes the previously created launch
            # config as this launch configuration will contain the instructions to create new instances for the group.
            # Other than that, we include the availability zones for the group to launch on. Load Balancer along with
            # a number of extra options can be used in the Auto Scaling Group.
            ag = AutoScalingGroup(group_name=as_group_name,
                                  availability_zones=['eu-west-1c'],
                                  launch_config=lc,
                                  min_size=min_instances,
                                  max_size=max_instances,
                                  connection=conn_as)
            print "Auto Scaling Group created"

            # Once again, we will commit the created group and as a result, the minimum number of instances requested
            # will start to deploy.
            conn_as.create_auto_scaling_group(ag)
            print "Instances are being deployed"
        except exception.BotoServerError:
            print "The launch configurator name or the group name already exists"

        return True
    def create_launch_configuration(self):
        self.logger.log("Creating launch configuration ...")

        try:
            lc = LaunchConfiguration(self.conn,
                                 "cloudscale-lc",
                                 self.as_ami_id,
                                 self.key_name,
                                 ['http'],
                                 None,
                                 self.instance_type,
                                 instance_monitoring=True
            )

            self.conn.create_launch_configuration(lc)
            return lc
        except boto.exception.BotoServerError as e:
            if e.error_code == 'AlreadyExists':
                return self.conn.get_all_launch_configurations(names=['cloudscale-lc'])
            else:
                raise
Ejemplo n.º 20
0
def launch_auto_scaling(stage='development'):
    config = get_provider_dict()
    from boto.ec2.autoscale import AutoScaleConnection, AutoScalingGroup, LaunchConfiguration, Trigger
    conn = AutoScaleConnection(fabric.api.env.conf['AWS_ACCESS_KEY_ID'],
                               fabric.api.env.conf['AWS_SECRET_ACCESS_KEY'],
                               host='%s.autoscaling.amazonaws.com' %
                               config['location'][:-1])

    for name, values in config.get(stage, {}).get('autoscale', {}):
        if any(group.name == name for group in conn.get_all_groups()):
            fabric.api.warn(
                fabric.colors.orange('Autoscale group %s already exists' %
                                     name))
            continue
        lc = LaunchConfiguration(name='%s-launch-config' % name,
                                 image_id=values['image'],
                                 key_name=config['key'])
        conn.create_launch_configuration(lc)
        ag = AutoScalingGroup(group_name=name,
                              load_balancers=values.get('load-balancers'),
                              availability_zones=[config['location']],
                              launch_config=lc,
                              min_size=values['min-size'],
                              max_size=values['max-size'])
        conn.create_auto_scaling_group(ag)
        if 'min-cpu' in values and 'max-cpu' in values:
            tr = Trigger(name='%s-trigger' % name,
                         autoscale_group=ag,
                         measure_name='CPUUtilization',
                         statistic='Average',
                         unit='Percent',
                         dimensions=[('AutoScalingGroupName', ag.name)],
                         period=60,
                         lower_threshold=values['min-cpu'],
                         lower_breach_scale_increment='-1',
                         upper_threshold=values['max-cpu'],
                         upper_breach_scale_increment='2',
                         breach_duration=60)
            conn.create_trigger(tr)
Ejemplo n.º 21
0
    def create_launch_config(self,
                             name,
                             image_id,
                             key_name=None,
                             security_groups=None,
                             user_data=None,
                             instance_type=None,
                             kernel_id=None,
                             ramdisk_id=None,
                             block_device_mappings=None,
                             instance_monitoring=False,
                             instance_profile_name=None):
        """
        Creates a new launch configuration with specified attributes.

        :param name: Name of the launch configuration to create. (Required)
        :param image_id: Unique ID of the Amazon Machine Image (AMI) assigned during registration. (Required)
        :param key_name: The name of the EC2 key pair.
        :param security_groups: Names of the security groups with which to associate the EC2 instances.
        """
        lc = LaunchConfiguration(name=name,
                                 image_id=image_id,
                                 key_name=key_name,
                                 security_groups=security_groups,
                                 user_data=user_data,
                                 instance_type=instance_type,
                                 kernel_id=kernel_id,
                                 ramdisk_id=ramdisk_id,
                                 block_device_mappings=block_device_mappings,
                                 instance_monitoring=instance_monitoring,
                                 instance_profile_name=instance_profile_name)
        self.debug("Creating launch config: " + name)
        self.autoscale.create_launch_configuration(lc)
        if len(self.describe_launch_config([name])) != 1:
            raise Exception('Launch Config not created')
        launch_config = self.describe_launch_config([name])[0]
        self.test_resources["launch-configurations"].append(launch_config)
        self.debug('SUCCESS: Created Launch Config: ' +
                   self.describe_launch_config([name])[0].name)
Ejemplo n.º 22
0
    def create(self):
        """
        Creates a launch configuration using configuration file. It will update
        the autocale configuration `name` property by appending the current
        date and a version
        """
        version = 1
        found = False
        while not found:
            name = "%s-%s" % (self.generated_name, version)
            launch_configurations = self.autoscale.get_all_launch_configurations(
                names=[name])
            if launch_configurations:
                version += 1
            else:
                found = True

        self.name = name
        launch_configuration = LaunchConfiguration(name=self.name,
                                                   **self.configuration)
        self.resource = self.autoscale.create_launch_configuration(
            launch_configuration)
Ejemplo n.º 23
0
    def create_launch_configuration(self):
        self.log.info("Getting launch_configuration: {l}".format(
            l=self.launch_configuration))

        lc = self.conn.get_all_launch_configurations(
            names=[self.launch_configuration])
        if not lc:
            self.log.info("Creating new launch_configuration: {l}".format(
                l=self.launch_configuration))
            lc = LaunchConfiguration(
                name=self.launch_configuration,
                image_id=self.node_obj.ami,
                key_name=self.node_obj.keypair,
                security_groups=self.node_obj.get_security_group_ids(
                    self.node_obj.security_groups),
                instance_monitoring=False,
                block_device_mappings=[self.volumes],
                user_data=self.node_obj.user_data,
                instance_type=self.node_obj.instance_type,
                instance_profile_name=self.node_obj.role)
            self.conn.create_launch_configuration(lc)
            self.launch_configuration = lc
Ejemplo n.º 24
0
def create_launch_config():
    oldLC = asConnection.get_all_launch_configurations(
        names=[OLD_LAUNCH_CONFIGURATION_NAME])[0]
    if not oldLC:
        exit('Old launch configuration does not exist')

    if oldLC.instance_monitoring.enabled == 'true':
        monitoring = True
    else:
        monitoring = False

    launchconfig = LaunchConfiguration(
        name=NEW_LAUNCH_CONFIGURATION_NAME,
        image_id=NEW_AMI_ID,
        instance_profile_name=oldLC.instance_profile_name,
        key_name=oldLC.key_name,
        ebs_optimized=oldLC.ebs_optimized,
        user_data=oldLC.user_data,
        instance_type=oldLC.instance_type,
        instance_monitoring=monitoring,
        security_groups=oldLC.security_groups,
    )
    # create returns a request id
    if asConnection.create_launch_configuration(launchconfig):
        group = get_asg_attachment(oldLC.name)

        if group:
            # The old launch configuration is attached to a group
            group.launch_config_name = NEW_LAUNCH_CONFIGURATION_NAME
            group.update()

        # The old launch configuration is no longer attached to a group, we can delete it
        #delete_result = asConnection.delete_launch_configuration(oldLC.name) # delete returns request id
    else:
        print('Failed to create a new launch configuration.')
        return False
Ejemplo n.º 25
0
def create_group():
    global conn, lcname

    lc = LaunchConfiguration(name=lcname,
                             image_id=options.ami,
                             key_name='relwellnlp',
                             instance_type='m2.4xlarge',
                             security_groups=['sshable'])

    conn.create_launch_configuration(lc)

    min = options.min if options.min is not None else DEFAULT_MIN
    max = options.max if options.max is not None else DEFAULT_MAX
    group = AutoScalingGroup(group_name=options.group,
                             availability_zones=options.zones.split(','),
                             launch_config=lc,
                             min_size=min,
                             max_size=max,
                             connection=conn)

    conn.create_auto_scaling_group(group)

    scale_up_policy = ScalingPolicy(name='scale_up',
                                    adjustment_type='ChangeInCapacity',
                                    as_name=options.group,
                                    scaling_adjustment=1,
                                    cooldown=180)

    scale_down_policy = ScalingPolicy(name='scale_down',
                                      adjustment_type='ChangeInCapacity',
                                      as_name=options.group,
                                      scaling_adjustment=-1,
                                      cooldown=180)

    conn.create_scaling_policy(scale_up_policy)
    conn.create_scaling_policy(scale_down_policy)
Ejemplo n.º 26
0
def create_launch_config(connection, module):
    name = module.params.get('name')
    image_id = module.params.get('image_id')
    key_name = module.params.get('key_name')
    security_groups = module.params['security_groups']
    user_data = module.params.get('user_data')
    user_data_path = module.params.get('user_data_path')
    volumes = module.params['volumes']
    instance_type = module.params.get('instance_type')
    spot_price = module.params.get('spot_price')
    instance_monitoring = module.params.get('instance_monitoring')
    assign_public_ip = module.params.get('assign_public_ip')
    kernel_id = module.params.get('kernel_id')
    ramdisk_id = module.params.get('ramdisk_id')
    instance_profile_name = module.params.get('instance_profile_name')
    ebs_optimized = module.params.get('ebs_optimized')
    classic_link_vpc_id = module.params.get('classic_link_vpc_id')
    classic_link_vpc_security_groups = module.params.get(
        'classic_link_vpc_security_groups')
    bdm = BlockDeviceMapping()

    if user_data_path:
        try:
            with open(user_data_path, 'r') as user_data_file:
                user_data = user_data_file.read()
        except IOError as e:
            module.fail_json(msg=str(e), exception=traceback.format_exc())

    if volumes:
        for volume in volumes:
            if 'device_name' not in volume:
                module.fail_json(msg='Device name must be set for volume')
            # Minimum volume size is 1GB. We'll use volume size explicitly set to 0
            # to be a signal not to create this volume
            if 'volume_size' not in volume or int(volume['volume_size']) > 0:
                bdm[volume['device_name']] = create_block_device(
                    module, volume)

    lc = LaunchConfiguration(
        name=name,
        image_id=image_id,
        key_name=key_name,
        security_groups=security_groups,
        user_data=user_data,
        block_device_mappings=[bdm],
        instance_type=instance_type,
        kernel_id=kernel_id,
        spot_price=spot_price,
        instance_monitoring=instance_monitoring,
        associate_public_ip_address=assign_public_ip,
        ramdisk_id=ramdisk_id,
        instance_profile_name=instance_profile_name,
        ebs_optimized=ebs_optimized,
        classic_link_vpc_security_groups=classic_link_vpc_security_groups,
        classic_link_vpc_id=classic_link_vpc_id,
    )

    launch_configs = connection.get_all_launch_configurations(names=[name])
    changed = False
    if not launch_configs:
        try:
            connection.create_launch_configuration(lc)
            launch_configs = connection.get_all_launch_configurations(
                names=[name])
            changed = True
        except BotoServerError as e:
            module.fail_json(msg=str(e))

    result = dict(
        ((a[0], a[1]) for a in vars(launch_configs[0]).items()
         if a[0] not in ('connection', 'created_time', 'instance_monitoring',
                         'block_device_mappings')))
    result['created_time'] = str(launch_configs[0].created_time)
    # Looking at boto's launchconfig.py, it looks like this could be a boolean
    # value or an object with an enabled attribute.  The enabled attribute
    # could be a boolean or a string representation of a boolean.  Since
    # I can't test all permutations myself to see if my reading of the code is
    # correct, have to code this *very* defensively
    if launch_configs[0].instance_monitoring is True:
        result['instance_monitoring'] = True
    else:
        try:
            result['instance_monitoring'] = module.boolean(
                launch_configs[0].instance_monitoring.enabled)
        except AttributeError:
            result['instance_monitoring'] = False
    if launch_configs[0].block_device_mappings is not None:
        result['block_device_mappings'] = []
        for bdm in launch_configs[0].block_device_mappings:
            result['block_device_mappings'].append(
                dict(device_name=bdm.device_name,
                     virtual_name=bdm.virtual_name))
            if bdm.ebs is not None:
                result['block_device_mappings'][-1]['ebs'] = dict(
                    snapshot_id=bdm.ebs.snapshot_id,
                    volume_size=bdm.ebs.volume_size)

    if user_data_path:
        result[
            'user_data'] = "hidden"  # Otherwise, we dump binary to the user's terminal

    module.exit_json(changed=changed,
                     name=result['name'],
                     created_time=result['created_time'],
                     image_id=result['image_id'],
                     arn=result['launch_configuration_arn'],
                     security_groups=result['security_groups'],
                     instance_type=result['instance_type'],
                     result=result)
Ejemplo n.º 27
0
                    str(grp.id) for grp in grp_details
                    if str(grp.name) in group_name
                ]
                security_groups.extend(group_id)
        except boto.exception.NoAuthHandlerFound, e:
            module.fail_json(msg=str(e))

    lc = LaunchConfiguration(
        name=name,
        image_id=image_id,
        key_name=key_name,
        security_groups=security_groups,
        user_data=user_data,
        block_device_mappings=[bdm],
        instance_type=instance_type,
        kernel_id=kernel_id,
        spot_price=spot_price,
        instance_monitoring=instance_monitoring,
        associate_public_ip_address=assign_public_ip,
        ramdisk_id=ramdisk_id,
        instance_profile_name=instance_profile_name,
        ebs_optimized=ebs_optimized,
        classic_link_vpc_security_groups=classic_link_vpc_security_groups,
        classic_link_vpc_id=classic_link_vpc_id,
    )

    launch_configs = connection.get_all_launch_configurations(names=[name])
    changed = False
    if not launch_configs:
        try:
            connection.create_launch_configuration(lc)
            launch_configs = connection.get_all_launch_configurations(
Ejemplo n.º 28
0
def create_AutoScaling():
    print "Creating AutoScaling..."
    # establish connection
    as_conn = AutoScaleConnection(AWSAccessKeyId, AWSSecretKey)
    # create launch configuration
    global lc
    lc = LaunchConfiguration(name='lc',
                             image_id=DATA_CEN_AMI,
                             key_name=ACCESS_KEY,
                             instance_monitoring=True,
                             security_groups=[SECURITY_GRP],
                             instance_type=MACHINE_TYPE)
    as_conn.create_launch_configuration(lc)

    # create tag for autoscaling group
    as_tag = Tag(key="Project",
                 value="2.2",
                 propagate_at_launch=True,
                 resource_id='my_group')

    # create aotoscaling group
    global ag
    ag = AutoScalingGroup(group_name='my_group',
                          load_balancers=['myELB'],
                          availability_zones=['us-east-1a'],
                          launch_config=lc,
                          min_size=MIN_SIZE,
                          max_size=MAX_SIZE,
                          connection=as_conn,
                          tags=[as_tag])
    # associate the autoscaling group with launch configuration
    as_conn.create_auto_scaling_group(ag)

    # build the scale policy
    scale_up_policy = ScalingPolicy(name='scale_up',
                                    adjustment_type='ChangeInCapacity',
                                    as_name='my_group',
                                    scaling_adjustment=1,
                                    cooldown=60)
    scale_down_policy = ScalingPolicy(name='scale_down',
                                      adjustment_type='ChangeInCapacity',
                                      as_name='my_group',
                                      scaling_adjustment=-1,
                                      cooldown=60)

    # register the scale policy
    as_conn.create_scaling_policy(scale_up_policy)
    as_conn.create_scaling_policy(scale_down_policy)

    # refresh the scale policy for extra information
    scale_up_policy = as_conn.get_all_policies(as_group='my_group',
                                               policy_names=['scale_up'])[0]
    scale_down_policy = as_conn.get_all_policies(as_group='my_group',
                                                 policy_names=['scale_down'
                                                               ])[0]

    # create cloudwatch alarm
    cloudwatch = CloudWatchConnection(aws_access_key_id=AWSAccessKeyId,
                                      aws_secret_access_key=AWSSecretKey,
                                      is_secure=True)
    # region='us-east-1a')

    # assocate cloudwatch with alarm
    alarm_dimensions = {"AutoScalingGroupName": 'my_group'}

    # create scale up alarm
    scale_up_alarm = MetricAlarm(name='scale_up_on_cpu',
                                 namespace='AWS/EC2',
                                 metric='CPUUtilization',
                                 statistic='Average',
                                 comparison='>',
                                 threshold='50',
                                 period='60',
                                 evaluation_periods=2,
                                 alarm_actions=[scale_up_policy.policy_arn],
                                 dimensions=alarm_dimensions)
    cloudwatch.create_alarm(scale_up_alarm)

    # create scale down alarm
    scale_down_alarm = MetricAlarm(
        name='scale_down_on_cpu',
        namespace='AWS/EC2',
        metric='CPUUtilization',
        statistic='Average',
        comparison='<',
        threshold='20',
        period='60',
        evaluation_periods=1,
        alarm_actions=[scale_down_policy.policy_arn],
        dimensions=alarm_dimensions)
    cloudwatch.create_alarm(scale_down_alarm)

    print "AutoScaling created successfully"
Ejemplo n.º 29
0
            interval=30,
            healthy_threshold=3,
            unhealthy_threshold=5,
            target='HTTP:80/heartbeat?username=baiyangw')
lb.configure_health_check(hc)
elb_name = lb.dns_name
print '1.The elb_address is: ' + lb.dns_name
import boto.ec2.autoscale
from boto.ec2.autoscale import LaunchConfiguration
from boto.ec2.autoscale import AutoScalingGroup
from boto.ec2.autoscale import ScalingPolicy
#2.Create LC
asg_conn = boto.ec2.autoscale.connect_to_region('us-east-1')
lc = LaunchConfiguration(name='baiyangwlc',
                         image_id='ami-ec14ba84',
                         key_name='Baiyangw',
                         instance_type='m3.medium',
                         instance_monitoring = True,
                         security_groups=['Project'])
asg_conn.create_launch_configuration(lc)
print '2.lc creation success!'
#3.Create ASG
asg = AutoScalingGroup(group_name='baiyangwgroup',
			  load_balancers=['baiyangwlb'],
			  health_check_type = 'ELB',
			  health_check_period = '300',
			  desired_capacity = 1,
			  availability_zones=['us-east-1c'],
			  launch_config = lc,
			  min_size = 1,
			  max_size = 2,
			  tags = [boto.ec2.autoscale.tag.Tag(key='Project',value='2.2', 
Ejemplo n.º 30
0
def setup(CONF):
  global out

  lookup_tbl = {
    'name': CONF['NAME'],
  }

  conn = AutoScaleConnection()

  out['conn'] = conn

  # Launch Configurations
  LC = CONF['LC']
  LC['name'] = LC['name'] % lookup_tbl

  lc = LaunchConfiguration(**LC)
  conn.create_launch_configuration(lc)
  out['lc'] = lc

  # Auto Scaling Group
  ASG = CONF['ASG']
  ASG['group_name'] = ASG['group_name'] % lookup_tbl
  ASG['launch_config'] = lc

  groups = conn.get_all_groups(names=[ASG['group_name']])
  if (len(groups) > 0):
    # update
    asg = groups[0]
    for k in ASG :
      # asg not iterable, try-except to make sure asg[k] exists
      try: asg.__getattribute__(k)
      except: continue
      asg.__setattr__(k, ASG[k])
    asg.launch_config_name = LC['name']
    asg.update()
    out['asg'] = asg
  else:
    #create
    asg = AutoScalingGroup(**ASG)
    conn.create_auto_scaling_group(asg)

  # ASG Tags
  ASG_TAGS = CONF['ASG_TAGS']
  for i in ASG_TAGS:
    if 'propagate_at_launch' not in i:
      i['propagate_at_launch'] = True
    i['key'] = i['key'] % lookup_tbl
    i['value'] = i['value'] % lookup_tbl

  tags = [
      Tag(**dict(x.items() + [('resource_id', ASG['group_name'])])) for x in ASG_TAGS
  ]
  conn.create_or_update_tags(tags)

  # Triggers (Scaling Policy / Cloudwatch Alarm)
  conn_cw = connect_to_region(CONF['REGION'])

  TRIGGERS = CONF['TRIGGERS']
  for T in TRIGGERS:
    T['policy']['name'] = T['policy']['name'] % lookup_tbl
    T['policy']['as_name'] = ASG['group_name']
    T['alarm']['dimensions'] = {'AutoScalingGroupName': ASG['group_name']}
    T['alarm']['alarm_actions'] = None

    if 'name' in T['alarm']:
      T['alarm']['name'] = T['alarm']['name'] % lookup_tbl
    else:
      T['alarm']['name'] = T['policy']['name']

    # Policies are safely overwritten, so not checked for existence
    conn.create_scaling_policy(ScalingPolicy(**T['policy']))
    policy = conn.get_all_policies(as_group=ASG['group_name'], policy_names=[T['policy']['name']])[0]

    T['alarm']['alarm_actions'] = [policy.policy_arn]
    hits = conn_cw.describe_alarms(alarm_names=[T['alarm']['name']])

    conn_cw.create_alarm(MetricAlarm(**T['alarm']))
Ejemplo n.º 31
0
def setup_autoscale(name,
                    ami_id,
                    key_name,
                    security_groups,
                    load_balancers,
                    instance_type='m1.micro',
                    availability_zones=['us-east-1b'],
                    min_instances=1,
                    max_instances=12,
                    sp_up_adjustment=1,
                    sp_up_cooldown=180,
                    sp_down_adjustment=-1,
                    sp_down_cooldown=180,
                    instance_monitoring=True):
    """
    Configure AutoScaling for Amazon EC2 instances

        `name`: name used to identify the autoscale group
        `ami_id`: AMI ID from instances will be generated
        `key_name`: name of the SSH key which will have access to the instance
        `security_groups`: list of security groups to associate with each
                           created instance
        `instance_type`: type of the instance that will be launched
                         (see http://aws.amazon.com/ec2/instance-types/)
        `availability_zones`: in which zones instances can be launched. This
                              must match with zones configured in ELB.
        `min_instances`: minimal number of instances that must be running
        `max_instances`: maximum number of instance that must be running
        `sp_up_adjustment`: sets the number of instances to launch on the up
                            scaling policy trigger
        `sp_down_adjustment`: sets the number of instances to kill on the down
                            scaling policy trigger
    """
    launch_config = "{0}_{1}".format(name,
                                     datetime.now().strftime("%Y%m%d-%H%M"))
    group_name = '{0}-as-group'.format(name)

    sp_up_name = '{0}-scaling-up'.format(name)
    sp_down_name = '{0}-scaling-down'.format(name)

    conn_as = boto.connect_autoscale()
    lc = LaunchConfiguration(name=launch_config,
                             image_id=ami_id,
                             key_name=key_name,
                             security_groups=security_groups,
                             instance_type=instance_type,
                             instance_monitoring=instance_monitoring)
    conn_as.create_launch_configuration(lc)

    ag = AutoScalingGroup(group_name=group_name,
                          load_balancers=load_balancers,
                          availability_zones=availability_zones,
                          launch_config=launch_config,
                          min_size=min_instances,
                          max_size=max_instances)
    conn_as.create_auto_scaling_group(ag)

    create_scaling_policy(conn_as, sp_up_name, group_name, sp_up_adjustment,
                          sp_up_cooldown)
    create_scaling_policy(conn_as, sp_down_name, group_name,
                          sp_down_adjustment, sp_down_cooldown)
Ejemplo n.º 32
0
#For a complete list of options see http://boto.cloudhackers.com/ref/ec2.html#boto.ec2.elb.ELBConnection.create_load_balancer
lb = conn_elb.create_load_balancer(
    elastic_load_balancer['name'], zoneStrings,
    elastic_load_balancer['connection_forwarding'])

lb.configure_health_check(hc)

#DNS name for your new load balancer
print "Map the CNAME of your website to: %s" % (lb.dns_name)

#=================Create a Auto Scaling Group and a Launch Configuration=============================================
#For a complete list of options see http://boto.cloudhackers.com/ref/ec2.html#boto.ec2.autoscale.launchconfig.LaunchConfiguration
lc = LaunchConfiguration(name=lc_name,
                         image_id=as_ami['id'],
                         key_name=as_ami['access_key'],
                         security_groups=as_ami['security_groups'],
                         instance_type=as_ami['instance_type'],
                         instance_monitoring=as_ami['instance_monitoring'])
conn_as.create_launch_configuration(lc)

#For a complete list of options see http://boto.cloudhackers.com/ref/ec2.html#boto.ec2.autoscale.group.AutoScalingGroup
ag = AutoScalingGroup(group_name=autoscaling_group['name'],
                      load_balancers=[elastic_load_balancer['name']],
                      availability_zones=zoneStrings,
                      launch_config=lc,
                      min_size=autoscaling_group['min_size'],
                      max_size=autoscaling_group['max_size'])
conn_as.create_auto_scaling_group(ag)

#=================Create Scaling Policies=============================================
#Policy for scaling the number of servers up and down