Example #1
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'))
Example #2
0
    def op_create(self, args):
        conn = self._conn
        lc = LaunchConfiguration(name='test',
                image_id='ami-d58ff7bc', # Centos6-64 - EBS
                instance_type='t1.micro',
                spot_price=0.02,
                key_name='webops',
                security_groups=['sg-063ec469', 'sg-0657f169'],
                )
        conn.create_launch_configuration(lc)
        as_tag = Tag(key='cfhost', value = 'centos6-test-{ZONE}+',
                propagate_at_launch=True,
                resource_id='test')

        ag = AutoScalingGroup(name='test',
                availability_zones=['us-east-1a'],
                vpc_zone_identifier='subnet-b1c96fdd',
                tags=[as_tag],
                launch_config=lc,
                min_size=0,
                max_size=0,
                connection=conn,
                )
        topic = 'arn:aws:sns:us-east-1:311514151974:test'
        conn.create_auto_scaling_group(ag)

        ag.put_notification_configuration(topic, ['autoscaling:EC2_INSTANCE_LAUNCH'])
Example #3
0
def create_autoscale_group(_asg):
    lc = create_launch_config(_asg)
    ag = AutoScalingGroup(group_name=C['asg_name'], load_balancers=[C['elb_name']],
                          availability_zones=[C['zonea'],C['zoneb']],
                          launch_config=lc, min_size=C['asg_min'], max_size=C['asg_max'],
                          connection=_asg)
    ags = _asg.get_all_groups(names=[C['asg_name']])
    if len(ags) == 0:
        _asg.create_auto_scaling_group(ag)
    while [ True ]:
        ag.get_activities()
        time.sleep(5)
        print("mark")
Example #4
0
 def scalinggroup_create(self):
     """Handles the POST from the Create Scaling Group wizard"""
     if self.create_form.validate():
         with boto_error_handler(self.request, self.request.route_path('scalinggroups')):
             scaling_group_name = self.request.params.get('name')
             self.log_request(_(u"Creating scaling group {0}").format(scaling_group_name))
             scaling_group = AutoScalingGroup(
                 name=scaling_group_name,
                 launch_config=self.request.params.get('launch_config'),
                 availability_zones=self.request.params.getall('availability_zones'),
                 # load_balancers=self.request.params.getall('load_balancers'),
                 health_check_type=self.request.params.get('health_check_type'),
                 health_check_period=self.request.params.get('health_check_period'),
                 desired_capacity=self.request.params.get('desired_capacity'),
                 min_size=self.request.params.get('min_size'),
                 max_size=self.request.params.get('max_size'),
                 tags=self.parse_tags_param(scaling_group_name=scaling_group_name),
             )
             self.autoscale_conn.create_auto_scaling_group(scaling_group)
             msg = _(u'Successfully created scaling group')
             msg += ' {0}'.format(scaling_group.name)
             self.request.session.flash(msg, queue=Notification.SUCCESS)
             location = self.request.route_path('scalinggroup_view', id=scaling_group.name)
             return HTTPFound(location=location)
     else:
         self.request.error_messages = self.create_form.get_errors_list()
     return self.render_dict
Example #5
0
    def create_autoscaling_group(self):
        existing_asg = self.conn.get_all_groups(names=[self.autoscaling_group])

        if not existing_asg:
            self.log.info("Creating new autoscaling group: {g}".format(
                g=self.autoscaling_group))

            # Convert our tags list into something that AWS can understand:
            aws_tags = list()
            for tag in self.tags:
                self.log.info("Adding tag [" + str(tag['name']) +
                              "] with value [" + str(tag['value']) + "]")
                aws_tags.append(
                    Tag(resource_id=self.autoscaling_group,
                        key=tag['name'],
                        value=tag['value'],
                        propagate_at_launch=True))

            ag = AutoScalingGroup(
                name=self.autoscaling_group,
                tags=aws_tags,
                availability_zones=self.autoscale_availability_zones,
                desired_capacity=self.desired_capacity,
                health_check_period=self.health_check_grace_period,
                launch_config=self.launch_configuration,
                min_size=self.min_size,
                max_size=self.max_size,
                default_cooldown=self.default_cooldown,
                vpc_zone_identifier=self.autoscale_subnets,
                connection=self.conn)
            self.conn.create_auto_scaling_group(ag)

        else:
            self.log.info('Autoscaling group {g} already exists.'.format(
                g=self.autoscaling_group))
Example #6
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])
Example #7
0
    def create_autoscale_group(gname,
                               lconfig_name,
                               placement_group,
                               size,
                               zones=None):
        existing_group = CompEC2._get_autoscale_group(gname)
        if existing_group is not None:
            Cluster.log_error("Autoscale group %s already exists!", gname)
            return None

        tags = [
            Tag(key='Name',
                value=gname,
                propagate_at_launch=True,
                resource_id=gname)
        ]

        if zones is None:
            zones = [x.name for x in Cluster._ec2().get_all_zones()]

        Cluster.log_info("zones: %r", zones)
        ag = AutoScalingGroup(group_name=gname,
                              availability_zones=zones,
                              launch_config=lconfig_name,
                              placement_group=placement_group,
                              tags=tags,
                              desired_capacity=0,
                              min_size=0,
                              max_size=size)
        conn = Cluster._autoscale()
        return conn.create_auto_scaling_group(ag)
def main():
    
    create_elb()
    
    # ======create auto scale group with configuration ======
    conn = boto.ec2.autoscale.connect_to_region('us-east-1')
    
    
    ag = AutoScalingGroup(group_name='Auto_scale_group', load_balancers=['ELBdemo'],
                              availability_zones=['us-east-1a'],
                              launch_config='my-launch_config', min_size=2, max_size=5,
                              connection=conn)
                              
    conn.create_auto_scaling_group(ag)
    
    ag.get_activities()
    
    create_autoscalegroup_watch(conn)
Example #9
0
 def create_autoscale_group(self, conn, launch_c, group_name):
     """ Creates new auto scaling group """
     ag = AutoScalingGroup(group_name=group_name, availability_zones=['eu-west-1a', 'eu-west-1b', 'eu-west-1c'],
                           launch_config=launch_c, min_size=1, max_size=3, connection=conn)
     group = conn.create_auto_scaling_group(ag)
     if group:
         print "new group has been created,", group_name
     else:
         print "Error creating group"
Example #10
0
def _create_autoscaling_group(c, lc, opts):
    g = AutoScalingGroup(name=opts.name + s.GROUP_SUFFIX,
            launch_config=lc,
            availability_zones=opts.zones,
            load_balancers=opts.lbs,
            min_size=opts.min,
            max_size=opts.max)
    c.create_auto_scaling_group(g)
    return g
Example #11
0
def create(autoscale, name, zones, lc_name, load_balancers, hc_type, hc_period,
        min_size, max_size, cooldown, capacity):
    """Create an ASG named <name>"""
    g = AutoScalingGroup(name=name, launch_config=lc_name,
            availability_zones=zones, load_balancers=load_balancers,
            default_cooldown=cooldown, health_check_type=hc_type,
            health_check_period=hc_period, desired_capacity=capacity,
            min_size=min_size, max_size=max_size)
    g = autoscale.create_auto_scaling_group(g)
    return list(autoscale)
Example #12
0
 def autoScalingGroup(self):
     ag = AutoScalingGroup(
         group_name=self.groupName,
         load_balancers=[self.elb_name],
         availability_zones=self.a_zone,
         launch_config=AutoScaling.Launch_configuration(self),
         min_size=self.min_size,
         max_size=self.max_size,
         connection=AutoScaling.conn)
     return ag
Example #13
0
def create_group():
    # Get the list of LC's
    print('\nLaunch Configuration List')
    launchconfigs = read_launch_configs(True)
    if len(launchconfigs) < 1:
        print('You have not yet created a Launch Configuration.')
        return

    print('\n')
    print('Enter the Launch Configuration number to use:')
    lc_number = get_choice(range(1, len(launchconfigs) + 1))
    launchConfigName = launchconfigs[lc_number].name

    autoscalingGroupName = None
    while not autoscalingGroupName:
        print('Enter a name for this new AutoScaling Group:')
        autoscalingGroupName = raw_input('#: ')

    print('Enter the Minimum Size')
    GROUP_MIN_SIZE = get_int()
    print('Enter the Maximum Size')
    GROUP_MAX_SIZE = get_int()
    print('Enter the default cooldown in seconds (default is 300 (5 minutes))')
    DEFAULT_COOLDOWN = get_int()
    print(
        'Enter the desired capacity (number of instances to start right now)')
    DESIRED_CAPACITY = get_int()
    print('Enter a Name tag for intances of this group:')
    NAME_TAG = raw_input('#: ')
    if NAME_TAG != '':
        NAME_TAG = [
            Tag(key='Name',
                value=NAME_TAG,
                propagate_at_launch=True,
                resource_id=autoscalingGroupName)
        ]
    else:
        NAME_TAG = None

    asgroup = AutoScalingGroup(
        group_name=autoscalingGroupName,
        availability_zones=REGIONS[CURRENT_REGION]['zones'],
        launch_config=launchConfigName,
        min_size=GROUP_MIN_SIZE,
        max_size=GROUP_MAX_SIZE,
        default_cooldown=DEFAULT_COOLDOWN,
        desired_capacity=DESIRED_CAPACITY,
        tags=NAME_TAG,
        #TODO: load_balancers = [elb, list] # health_check_type?? [ELB, EC2]
        #connection=asConnection
    )
    asConnection.create_auto_scaling_group(asgroup)  # returns request id
Example #14
0
    def activate(self):
        conn = util.as_conn()
        cfg = self.role_config
        name = self.name()

        # ensure this LC already exists
        if not self.activate_lc():
            return False

        # look up subnet id
        subnets = [
            self.find_vpc_subnet_by_cidr(cidr)
            for cidr in cfg.get('subnets_cidr')
        ]
        if not subnets or not len(subnets) or None in subnets:
            print "Valid subnets_cidr are required for {}/{}".format(
                self.cluster_name, self.role_name)
            return False
        print "Using subnets {}".format(", ".join([s.id for s in subnets]))
        print "AZs: {}".format(cfg.get('availability_zones'))

        # does the ASgroup already exist?
        ag = AutoScalingGroup(
            group_name=self.name(),
            load_balancers=cfg.get('elbs'),
            availability_zones=cfg.get('availability_zones'),
            vpc_zone_identifier=[s.id for s in subnets],
            launch_config=self.lc().name(),
            desired_capacity=cfg.get('scale_policy', 'desired'),
            min_size=cfg.get('scale_policy', 'min_size'),
            max_size=cfg.get('scale_policy', 'max_size'),
        )

        if not conn.create_auto_scaling_group(ag):
            print "Failed to create autoscale group"
            return False

        # prepare instance tags
        tags = cfg.get('tags')
        if not tags:
            tags = {}
        tags['cluster'] = self.cluster_name
        tags['role'] = self.role_name
        tags['Name'] = self.name()

        # apply tags
        tag_set = [self.ag_tag(ag, k, v) for (k, v) in tags.iteritems()]
        conn.create_or_update_tags(tag_set)

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

        return ag
Example #15
0
 def createAutoScaleGroup(self, asg_name):
     """
     Create a Auto scaling group for the auto scaling cluster
     """
     autoScalingGroup = AutoScalingGroup(
         group_name=asg_name,
         load_balancers=[self.args.lb_name],
         launch_config=self.launchConfiguration,
         min_size=self.args.min_size,
         max_size=self.args.max_size,
         availability_zones=['us-east-1a'])
     self.autoscale_connection.create_auto_scaling_group(autoScalingGroup)
     print ">>> Created auto scaling group: " + asg_name
Example #16
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'))
def create_auto_scaling(instance_id):
	autoscaling_conn = boto.ec2.autoscale.connect_to_region('eu-west-1')
	# Create launch configuration using my ami from the tutorial
	launch_config = LaunchConfiguration(name = 'DK-LC-Boto', image_id = 'ami-86c87ef1', instance_type = 't2.micro',
		key_name = 'David_kav', security_groups = ['witsshrdp'])
	autoscaling_conn.create_launch_configuration(launch_config)
    
    # create an autoscaling group
	autoscaling_group = AutoScalingGroup(name = 'DK-AutoScale-Boto',
		launch_config = launch_config,
		min_size = 1, max_size = 3,
		availability_zones = ['eu-west-1a', 'eu-west-1b'],
		# vpc_zone_identifier = ['subnet-747ea503','subnet-bfcb34e6'],
		# security_group_ids = ['sg-0edd5d6b'], 
		connection = autoscaling_conn)
    
    # tag the instance
	tag = Tag(propagate_at_launch = True, resource_id = 'DK-AutoScale-Boto')
	tag.key = 'Name'
	tag.value = "P_DavidK_boto"
	tags_list = [tag]
	autoscaling_group.tags = tags_list
	# submit the group to aws
	autoscaling_conn.create_auto_scaling_group(autoscaling_group)

    # create a scaling policy
	scale_up_policy = ScalingPolicy(name = 'scale_up',adjustment_type = 'ChangeInCapacity',
		as_name = 'DK-AutoScale-Boto',scaling_adjustment = 1, cooldown = 180)
    # submit the policy to aws through the autoscaling connection object
	autoscaling_conn.create_scaling_policy(scale_up_policy)
    # get the policy now that aws has added the extra properties
	scale_up_policy = autoscaling_conn.get_all_policies(as_group='DK-AutoScale-Boto', policy_names=['scale_up'])[0]

	as_policy_arn = scale_up_policy.policy_arn

	return as_policy_arn
Example #18
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
Example #19
0
    def create_as_group(self,
                        group_name,
                        launch_config,
                        availability_zones,
                        min_size,
                        max_size,
                        load_balancers=None,
                        desired_capacity=None,
                        termination_policies=None,
                        default_cooldown=None,
                        health_check_type=None,
                        health_check_period=None,
                        tags=None):
        """
        Create auto scaling group.

        :param group_name: Name of autoscaling group (required).
        :param load_balancers: List of load balancers.
        :param availability_zones: List of availability zones (required).
        :param launch_config: Name of launch configuration (required).
        :param min_size:  Minimum size of group (required).
        :param max_size: Maximum size of group (required).
        """
        self.log.debug("Creating Auto Scaling group: " + group_name)
        as_group = AutoScalingGroup(connection=self.connection,
                                    group_name=group_name,
                                    load_balancers=load_balancers,
                                    availability_zones=availability_zones,
                                    launch_config=launch_config,
                                    min_size=min_size,
                                    max_size=max_size,
                                    desired_capacity=desired_capacity,
                                    default_cooldown=default_cooldown,
                                    health_check_type=health_check_type,
                                    health_check_period=health_check_period,
                                    tags=tags,
                                    termination_policies=termination_policies)
        self.connection.create_auto_scaling_group(as_group)

        as_group = self.describe_as_group(group_name)

        self.log.debug("SUCCESS: Created Auto Scaling Group: " + as_group.name)
        return as_group
Example #20
0
 def update_or_create(self):
     """
     Creates autoscaling group and sets a `propagate_at_launch` tag for
     future instances the autoscale group boots
     """
     self.group = self._get_autoscaling_group()
     if self.group is None:
         autoscaling_group = AutoScalingGroup(group_name=self.name,
                                              **self.configuration)
         self.resource = self.autoscale.create_auto_scaling_group(
             autoscaling_group)
         self.group = self._get_autoscaling_group()
         name_tag = Tag(key='Name',
                        value=self.application,
                        propagate_at_launch=True,
                        resource_id=self.name)
         application_tag = Tag(key='forseti:application',
                               value=self.application,
                               propagate_at_launch=True,
                               resource_id=self.name)
         date_tag = Tag(key='forseti:date',
                        value=self.today,
                        propagate_at_launch=True,
                        resource_id=self.name)
         self.autoscale.create_or_update_tags(
             [name_tag, application_tag, date_tag])
     else:
         self.group.launch_config_name = self.configuration['launch_config']
         self.group.availability_zones = self.configuration[
             'availability_zones']
         if 'desired_capacity' in self.configuration:
             self.group.desired_capacity = self.configuration[
                 'desired_capacity']
         self.group.max_size = self.configuration['max_size']
         self.group.min_size = self.configuration['min_size']
         self.group.load_balancers = self.configuration['load_balancers']
         self.group.default_cooldown = self.configuration.get(
             'default_cooldown', None)
         self.group.termination_policies = self.configuration[
             'termination_policies']
         self.group.update()
         self.group = self._get_autoscaling_group()
Example #21
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_autoscalability_group(self, lb_name, lc):
        self.logger.log("Creating autoscalability group ...")

        try:
            tag = Tag(
                key='Name',
                value = self.instance_identifier,
                propagate_at_launch=True,
                resource_id='cloudscale-as'
            )
            ag = AutoScalingGroup(group_name='cloudscale-as',
                              load_balancers=[lb_name],
                              availability_zones=[self.availability_zone],
                              launch_config=lc,
                              min_size=1,
                              max_size=10,
                              connection=self.conn,
                              tags=[tag])
            self.conn.create_auto_scaling_group(ag)
        except boto.exception.BotoServerError as e:
            if e.error_code != 'AlreadyExists':
                raise # self.conn.get_all_groups(names=['cloudscale-as'])[0]
Example #23
0
    def create_as_group(self, group_name=None, load_balancers=None, availability_zones=None, launch_config=None,
                        min_size=None, max_size=None, connection=None):
        """
        Create auto scaling group.

        :param group_name: Name of autoscaling group (required).
        :param load_balancers: List of load balancers.
        :param availability_zones: List of availability zones (required).
        :param launch_config: Name of launch configuration (required).
        :param min_size:  Minimum size of group (required).
        :param max_size: Maximum size of group (required).
        :param connection: connection to auto scaling service
        """
        as_group = AutoScalingGroup(group_name=group_name,
                                    load_balancers=load_balancers,
                                    availability_zones=availability_zones,
                                    launch_config=launch_config,
                                    min_size=min_size,
                                    max_size=max_size,
                                    connection=connection)
        self.debug("Creating Auto Scaling group: " + group_name)
        self.autoscale.create_auto_scaling_group(as_group)
Example #24
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)
Example #25
0
    def update_as_group(self,
                        group_name,
                        launch_config,
                        min_size,
                        max_size,
                        availability_zones=None,
                        desired_capacity=None,
                        termination_policies=None,
                        default_cooldown=None,
                        health_check_type=None,
                        health_check_period=None):
        """

        :param group_name: REQUIRED
        :param launch_config: REQUIRED
        :param min_size: REQUIRED
        :param max_size: REQUIRED
        :param availability_zones:
        :param desired_capacity:
        :param termination_policies:
        :param default_cooldown:
        :param health_check_type:
        :param health_check_period:
        """
        self.log.debug("Updating ASG: " + group_name)
        return AutoScalingGroup(
            connection=self.connection,
            name=group_name,
            launch_config=launch_config,
            min_size=min_size,
            max_size=max_size,
            availability_zones=availability_zones,
            desired_capacity=desired_capacity,
            default_cooldown=default_cooldown,
            health_check_type=health_check_type,
            health_check_period=health_check_period,
            termination_policies=termination_policies).update()
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)
Example #27
0
    def SetupNew(self):
        # Launch config first
        ag = AutoScalingGroup(group_name=self.name,
                              availability_zones=self.zones,
                              launch_config=self.launch_config.launch_config,
                              desired_capacity = 0,
                              min_size = self.input_config.spot.min_nodes,
                              max_size = self.input_config.spot.max_nodes,
                              termination_policies = [ 'NewestInstance',
                                                       'ClosestToNextInstanceHour',
                                                       'Default' ],
                              connection=self.autoscale_conn)
        print 'Creating autoscale group: %s' % self.name
        self.autoscale_conn.create_auto_scaling_group(ag)
        self.autoscale = ag

        # Scaling
        self.SetupNewScaling()

        # Tagging
        print 'Adding tag to autoscaling group'
        self.autoscale_conn.create_or_update_tags([
            boto.ec2.autoscale.Tag(key="billing", value=self.instance,
                                   resource_id=ag.name) ])
Example #28
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']))
Example #29
0
File: api.py Project: gimlids/riker
def deploy_latest_app_ami(app_name, env_name):

    global aws
    aws = AWS.from_config(config)

    aws.connect()

    lb_group_ids=[aws.get_security_group_id('riker-load-balancer')]
    inst_group_ids=[aws.get_security_group_id('riker-instance')]

    app = App(env_name, app_name)

    health_check_target = app.config.get('health_check', 'TCP:80')

    name = re.sub('[^A-Za-z0-9\-]', '-', app.name)

    app_image = LatestAppImage(app).get()

    print '-----> Connecting to ELB'
    elb_conn = boto.connect_elb()

    log('info', 'Load balancer', show_header=True)
    load_balancer_name = name
    try:
        elb_result = elb_conn.get_all_load_balancers(load_balancer_names=[load_balancer_name])
        lb = elb_result[0]
        log('info', 'Found {}'.format(load_balancer_name))
    except boto.exception.BotoServerError:
        log('info', 'Not found, creating load balancer')
        listeners = [(80, 80, 'HTTP', 'HTTP')]
        lb = elb_conn.create_load_balancer(name=load_balancer_name,
                                           zones=None,
                                           complex_listeners=listeners,
                                           security_groups=lb_group_ids,
                                           subnets=[aws.subnet_id])
    hc = HealthCheck(target=health_check_target)
    lb.configure_health_check(hc)
    cda = ConnectionDrainingAttribute()
    cda.enabled = True
    cda.timeout = 300
    elb_conn.modify_lb_attribute(load_balancer_name=load_balancer_name,
                                  attribute='connectionDraining',
                                  value=cda)

    print '-----> Connecting to AutoScale'
    as_conn = boto.connect_autoscale()

    log('info', 'Launch configuration', show_header=True)
    launch_config_name = "{}-{}".format(name, app_image.tags['deploy-id'])
    lc_result = as_conn.get_all_launch_configurations(names=[launch_config_name])
    if len(lc_result) == 0:
        log('info', 'Not found, creating LaunchConfiguration')
        lc = LaunchConfiguration(name=launch_config_name,
                                 image_id=app_image.id,
                                 key_name=aws.key_pair_name,
                                 security_groups=inst_group_ids,
                                 instance_type=aws.instance_type)
        as_conn.create_launch_configuration(lc)
    else:
        log('info', 'Found {}'.format(launch_config_name))
        lc = lc_result[0]

    existing_group = None
    deploy_id = int(app_image.tags['deploy-id'] or 0)
    log('info', 'Getting previous auto-scaling group', show_header=True)
    for did in xrange(deploy_id-1, 0, -1):
        existing_group_name = "{}-{}".format(name, did)
        log('info', '{} ?'.format(existing_group_name))
        ag_result = as_conn.get_all_groups(names=[existing_group_name])
        if len(ag_result) > 0:
            existing_group = ag_result[0]
            log('info', 'Found {}'.format(existing_group.name))
            break
        else:
            log('info', 'No')

    if existing_group is not None:
        existing_healthy_instances = [inst for inst in existing_group.instances if inst.lifecycle_state == 'InService' and inst.health_status == 'Healthy']
        existing_healthy_instance_count = len(existing_healthy_instances)
        desired_capacity = existing_group.desired_capacity
        min_size = existing_group.min_size
        max_size = existing_group.max_size
        if existing_healthy_instance_count == 0 and desired_capacity == 0:
            print '-----> WARNING: existing auto-scaling group {} has no healthy instances and a desired capacity of 0. New auto-scaling group will launch 1 instance.'.format(existing_group)
            desired_capacity = 1
            min_size = 1
            max_size = max_size if max_size > 0 else 1
    else:
        existing_healthy_instance_count = 0
        desired_capacity = 1
        min_size = 1
        max_size = 1

    log('info', '{} existing instance(s) found'.format(existing_healthy_instance_count), show_header=True)

    log('info', 'Existing auto-scale properties: desired_capacity={}, min_size={}, max_size={}'.format(desired_capacity, min_size, max_size))

    log('info', 'Auto-scaling group', show_header=True)
    group_name = "{}-{}".format(name, app_image.tags['deploy-id'])
    ag_result = as_conn.get_all_groups(names=[group_name])
    if len(ag_result) == 0:
        log('info', 'Not found, creating autoscale group')
        ag = AutoScalingGroup(name=group_name,
                              load_balancers=[load_balancer_name], launch_config=lc,
                              desired_capacity=desired_capacity, min_size=min_size, max_size=max_size,
                              health_check_type='ELB', health_check_period='300',
                              vpc_zone_identifier=aws.subnet_id)
        as_conn.create_auto_scaling_group(ag)
    else:
        log('info', 'Found {}'.format(group_name))
        ag = ag_result[0]
        ag.desired_capacity = desired_capacity
        ag.max_size = max_size
        ag.min_size = min_size
        ag.launch_config_name = launch_config_name
        ag.update()

    log('info', 'Waiting for new instances to become healthy', show_header=True)
    all_healthy = False
    for i in xrange(60):
        if i > 0:
            print '       ---'
            time.sleep(10)
        elb_result = elb_conn.get_all_load_balancers(load_balancer_names=[load_balancer_name])
        lb = elb_result[0]
        lb_insts = lb.get_instance_health()
        print '       Load-balancer instances: {}'.format(lb_insts)
        # NOTE: re-get auto-scaling group to get updated instance info.
        ag = as_conn.get_all_groups(names=[group_name])[0]
        ag_insts = [inst for inst in ag.instances]
        log('info', 'Auto-scaling group Instances: {}'.format(ag_insts))
        if len(ag_insts) < desired_capacity:
            not_yet_launched_count = desired_capacity - len(ag_insts)
            log('info', '{} new instance(s) not yet launched'.format(not_yet_launched_count))
            continue
        ag_inst_ids = set(inst.instance_id for inst in ag_insts)
        lb_inst_ids = set(inst.instance_id for inst in lb_insts)
        asg_insts_not_in_lb = ag_inst_ids.difference(lb_inst_ids)
        if len(asg_insts_not_in_lb) > 0:
            log('info', '{} new instance(s) not yet in load balancer'.format(len(asg_insts_not_in_lb)))
            continue
        new_lb_insts = [inst for inst in lb_insts if inst.instance_id in ag_inst_ids]
        healthy_new_lb_insts = [inst for inst in new_lb_insts if inst.state == 'InService']
        all_healthy = len(healthy_new_lb_insts) == len(ag_insts)
        log('info', '{} new instance(s) are healthy'.format(len(healthy_new_lb_insts)))
        diff = existing_healthy_instance_count - len(healthy_new_lb_insts)
        if existing_group is not None and diff >= 0:
            change = False
            if existing_group.desired_capacity != diff:
                existing_group.desired_capacity = diff
                change = True
            if existing_group.max_size != diff:
                existing_group.max_size = diff
                change = True
            if diff < existing_group.min_size:
                existing_group.min_size = diff
                change = True
            if change:
                existing_group.update()
                log('info', 'Change previous auto-scale group {} properties: desired_capacity={}, min_size={}, max_size={}'.format(existing_group, existing_group.desired_capacity, existing_group.min_size, existing_group.max_size))
        if all_healthy:
            log('info', 'All new instances healthy!', show_header=True)
            healthy_lb_inst_ids = [inst.instance_id for inst in lb_insts if inst.state == 'InService']
            previous_healthy_inst_ids = [inst.instance_id for inst in existing_healthy_instances] if existing_group else []
            not_yet_out_of_service = set(previous_healthy_inst_ids).intersection(healthy_lb_inst_ids)
            if len(not_yet_out_of_service) > 0:
                log('info', 'Waiting to remove previous instances ({}) from load balancer'.format(not_yet_out_of_service))
            else:
                log('info', 'All previous instances ({}) have been removed from load balancer'.format(previous_healthy_inst_ids), show_header=True)
        if all_healthy and len(not_yet_out_of_service) == 0:
            break
    else:
        raise Exception("Timeout")

    elb_result = elb_conn.get_all_load_balancers(load_balancer_names=[load_balancer_name])
    lb = elb_result[0]
    lb_insts = [inst for inst in lb.get_instance_health() if inst.state == 'InService']
    print '-----> Deployed {} instance(s) of {} to {}'.format(lb_insts, app.name, lb.dns_name)

    print '-----> DONE!'
Example #30
0
                            resource_id=group_name))

    if not as_groups:
        if not vpc_zone_identifier and not availability_zones:
            availability_zones = module.params['availability_zones'] = [
                zone.name for zone in ec2_connection.get_all_zones()
            ]
        enforce_required_arguments(module)
        launch_configs = connection.get_all_launch_configurations(
            names=[launch_config_name])
        ag = AutoScalingGroup(group_name=group_name,
                              load_balancers=load_balancers,
                              availability_zones=availability_zones,
                              launch_config=launch_configs[0],
                              min_size=min_size,
                              max_size=max_size,
                              desired_capacity=desired_capacity,
                              vpc_zone_identifier=vpc_zone_identifier,
                              connection=connection,
                              tags=asg_tags,
                              health_check_period=health_check_period,
                              health_check_type=health_check_type)

        try:
            connection.create_auto_scaling_group(ag)
            asg_properties = get_properties(ag)
            changed = True
            return (changed, asg_properties)
        except BotoServerError, e:
            module.fail_json(msg=str(e))
    else:
        as_group = as_groups[0]
Example #31
0
load_balancer.configure_health_check(health_check)

time.sleep(20)
print "load balacner dns: " + load_balancer.dns_name

# create auto scaling group
conn = boto.ec2.autoscale.connect_to_region("us-east-1", aws_access_key_id=ACCESS_KEY_ID, aws_secret_access_key=SECRET_ACCESS_KEY)

lc = LaunchConfiguration(name='m3.medium.non_spot', image_id=Data_Center,
                             key_name='worker', instance_type=Data_Center_Type,
                             security_groups=SECURITY_GROUP)

# How about time to cool down
ag = AutoScalingGroup(group_name='SmartGroup', load_balancers=['SmartELB'],
                          availability_zones=['us-east-1a'],
                          launch_config=lc, min_size=MIN_INSTANCE_SIZE, max_size=MAX_INSTANCE_SIZE, health_check_type='ELB',
                          health_check_period='120', connection=conn)

conn.create_auto_scaling_group(ag)


scale_up_policy = ScalingPolicy(
            name='scale_up', adjustment_type='ChangeInCapacity',
            as_name=AUTO_SCALE_GROUP, scaling_adjustment=1, cooldown=SCALE_UP_COOLDOWN)
scale_down_policy = ScalingPolicy(
            name='scale_down', adjustment_type='ChangeInCapacity',
            as_name=AUTO_SCALE_GROUP, scaling_adjustment=-1, cooldown=SCALE_DOWN_COOLDOWN)

conn.create_scaling_policy(scale_up_policy)
conn.create_scaling_policy(scale_down_policy)
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"
Example #33
0
print 'start creating launch configuration'
asg_conn = boto.ec2.autoscale.connect_to_region(conn_region, aws_access_key_id = AWS_Access_Key_Id,
                                                aws_secret_access_key = AWS_Secret_Key)
launch_config = LaunchConfiguration(name = 'tianqiw_2.2_config', image_id = dataCenterAMI,
                            key_name = key_name, security_groups = [security_group_id],
                            instance_type = data_center_instance_type, instance_monitoring = True)
                            
asg_conn.create_launch_configuration(launch_config)
print 'launch configuration created'

# create AutoScaling Group
print 'start creating autoscaling group'
asg = AutoScalingGroup(group_name = asg_name, load_balancers = [elb_name],
                        availability_zones = ['us-east-1a'],
                        health_check_type = 'ELB',
                        health_check_period = 300,
                        launch_config = launch_config,
                        min_size = MIN_SIZE, max_size = MAX_SIZE, 
                        desired_capacity = DESIRED_SIZE, connection = asg_conn)
                 
asg_conn.create_auto_scaling_group(asg)

# Create a Tag for the auto scaling group
asg_tag = Tag(key = tag_key,
            value = tag_value,
            propagate_at_launch = True,
            resource_id = asg_name)

# Add the tag to the auto scaling group
asg_conn.create_or_update_tags([asg_tag])
Example #34
0
            'Key': 'Project',
            'Value': '2.2'
        },
    ]
)
#Create Launch Configuration

print 'Creating LC'
lc = LaunchConfiguration(name='Project22', image_id=DC_IMAGE, key_name=KEY_NAME, instance_type=DC_TYPE, instance_monitoring = True, security_groups=['All_Traffic'])
asg_conn.create_launch_configuration(lc)
print 'LC created'

#Create Auto Scaling Group

print 'Creating ASG'
asg = AutoScalingGroup(group_name='Project22group', load_balancers=['Project22hongf'], health_check_type = 'ELB', health_check_period = '119', desired_capacity = 5, availability_zones=['us-east-1c'], launch_config = lc, min_size = 5, max_size = 5, tags = [boto.ec2.autoscale.tag.Tag(key='Project',value='2.2', resource_id = 'Project22group', propagate_at_launch=True)])
asg_conn.create_auto_scaling_group(asg)
print 'ASG created'

#Create Scaling Policy

print 'Creating Scaling Policy'
scale_out_policy = ScalingPolicy(name = 'scale_out', adjustment_type = 'ChangeInCapacity', as_name = 'Project22group', scaling_adjustment = 1, cooldown = 60)
asg_conn.create_scaling_policy(scale_out_policy)
scale_in_policy = ScalingPolicy(name = 'scale_in', adjustment_type = 'ChangeInCapacity', as_name = 'Project22group', scaling_adjustment = -1, cooldown = 60)
asg_conn.create_scaling_policy(scale_in_policy)

#Check Policys and get them for CloudWatch
ScaleOut = asg_conn.get_all_policies(as_group = 'Project22group', policy_names = ['scale_out'])[0]
ScaleIn = asg_conn.get_all_policies(as_group = 'Project22group', policy_names = ['scale_in'])[0]
print 'Scaling Policy created'
Example #35
0
    def run(self):
        all_lcs = self.as_conn.get_all_launch_configurations()
        lc_by_group = defaultdict(list)
        lc_max_num_by_group = defaultdict(int)

        for lc in all_lcs:
            name, num = lc.name.split('-')
            num = int(num)

            lc_by_group[name].append(lc)

            if num > lc_max_num_by_group[name]:
                lc_max_num_by_group[name] = num

        all_ags = self.as_conn.get_all_groups()
        ag_by_name = {}
        for ag in all_ags:
            ag_by_name[ag.name] = ag

        for group_name, config in self.as_config["groups"].iteritems():
            print "Configuring %s" % group_name
            use_lc = None
            lc_to_delete = []
            for lc in lc_by_group[group_name]:
                if use_lc is None and \
                   lc.image_id == config['ami'] and \
                   lc.key_name == config['ssh_key'] and \
                   lc.instance_type == config['instance_type'] and \
                   lc.security_groups == [config['security_group']] and \
                   lc.user_data == self.user_data:
                    print " Found LaunchConfig %s that matches profile" % \
                            lc.name
                    use_lc = lc
                else:
                    lc_to_delete.append(lc)

            print " Found %d LaunchConfigurations to delete" % len(lc_to_delete)

            if not use_lc:
                print " Making LaunchConfiguration for %s" % group_name
                lc_num = lc_max_num_by_group[group_name] + 1
                use_lc = LaunchConfiguration(
                                 name="%s-%d" % (group_name, lc_num),
                                 image_id=config['ami'],
                                 key_name=config['ssh_key'],
                                 instance_type=config['instance_type'],
                                 security_groups=[config['security_group']],
                                 user_data=self.user_data)
                self.as_conn.create_launch_configuration(use_lc)

            if group_name in ag_by_name:
                print " Found existing AutoScalingGroup, updating"
                ag = ag_by_name[group_name]
                ag_exists = True
            else:
                print " Making new AutoScalingGroup"
                ag = AutoScalingGroup()
                ag_exists = False

            # config ASG as we want it
            ag.name = group_name
            ag.launch_config_name = use_lc.name
            ag.availability_zones = config['zones']
            ag.desired_capacity = config['capacity']
            ag.min_size = config['min_size']
            ag.max_size = config['max_size']

            # create or update as appropriate
            if ag_exists:
                ag.update()
            else:
                self.as_conn.create_auto_scaling_group(ag)

            # make it send e-mail whenever it does something
            if 'notification_topic' in self.as_config:
                # NOTE [adam Sept/18/12]: this is a hack designed to work
                #      around that boto support for this isn't in a release yet.
                #      when the next release is out, we should uncomment the
                #      code below.
                params = {'AutoScalingGroupName': ag.name,
                          'TopicARN': self.as_config['notification_topic'] }
                self.as_conn.build_list_params(params, self.AS_NOTIFICATIONS,
                                               'NotificationTypes')
                self.as_conn.get_status('PutNotificationConfiguration', params)
                #as_conn.put_notification_configuration(
                #       ag.name,
                #       self.as_config['notification_topic'],
                #       self.AS_NOTIFICATIONS)

            tags = []
            for tag_name, tag_value in config.get('tags', {}).iteritems():
                print " Adding tag %s = %s" % (tag_name, tag_value)
                tags.append(Tag(key=tag_name, value=tag_value,
                                propagate_at_launch=True,
                                resource_id=ag.name))

            self.as_conn.create_or_update_tags(tags)

            for lc in lc_to_delete:
                print " Deleting old LaunchConfiguration %s" % lc.name
                lc.delete()

            for alarm_name, alarm_cfg in config.get('alarms', {}).iteritems():
                alarm_policy_arn = self.make_policy(group_name,
                                          alarm_cfg['policy'])

                alarm_name = '%s|%s|%s' % (group_name,
                                           alarm_cfg['policy'],
                                           alarm_cfg['metric'])
                alarm = MetricAlarm(
                    name=alarm_name,
                    namespace=alarm_cfg['namespace'],
                    metric=alarm_cfg['metric'],
                    statistic='Average',
                    dimensions={'AutoScalingGroupName': group_name},
                    comparison=alarm_cfg['comparison'],
                    threshold=alarm_cfg['threshold'],
                    period=alarm_cfg['period'],
                    evaluation_periods=alarm_cfg.get('evaluation_periods', 1),
                    alarm_actions=[alarm_policy_arn])

                self.cw_conn.put_metric_alarm(alarm)
import boto.ec2.autoscale
from boto.ec2.autoscale import AutoScalingGroup

asgName = 'Wealthment US East'
lbName = ['WealthmentLB']
subnetIDs=['subnet-ff7ebf88','subnet-5de51004','subnet-450e106d']
conn = boto.ec2.autoscale.connect_to_region('us-east-1')

ag = AutoScalingGroup(group_name=asgName, vpc_zone_identifier=subnetIDs, load_balancers=lbName, launch_config=lc, min_size=3, max_size=9, connection=conn)
conn.create_auto_scaling_group(ag)

ag.get_activities()

group = conn.get_all_groups(names=[asgName])[0]
print group
Example #37
0
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
#For a complete list of options see http://boto.cloudhackers.com/ref/ec2.html#boto.ec2.autoscale.policy.ScalingPolicy
scalingUpPolicy = ScalingPolicy(name='webserverScaleUpPolicy',
                                adjustment_type='ChangeInCapacity',
                                as_name=ag.name,
                                scaling_adjustment=2,
                                cooldown=180)

scalingDownPolicy = ScalingPolicy(name='webserverScaleDownPolicy',
                                  adjustment_type='ChangeInCapacity',
Example #38
0
sudo cp third.html /var/www/html/test-web/third.html
sudo systemctl restart apache2"""

#reservation=conn.run_instances('ami-cd0f5cb6',key_name='ec2_instance1',instance_type='t2.micro',security_groups=['apache'],user_data=data_script)
lc = LaunchConfiguration(name='my_launch_config',
                         image_id='ami-cd0f5cb6',
                         key_name='ec2_instance1',
                         instance_type='t2.micro',
                         security_groups=['apache'],
                         user_data=data_script)

conn.create_launch_configuration(lc)

ag = AutoScalingGroup(group_name='my_autoscale_group',
                      availability_zones=['us-east-1a', 'us-east-1b'],
                      launch_config=lc,
                      min_size=1,
                      max_size=2,
                      connection=conn)

conn.create_auto_scaling_group(ag)

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

scale_down_policy = ScalingPolicy(name='scale_down',
                                  adjustment_type='ChangeInCapacity',
                                  as_name='my_autoscale_group',
                                  scaling_adjustment=-1,
Example #39
0
lb=elb.create_load_balancer('jianLb',zones,ports)
lb.configure_health_check(hc)
lbdns=lb.dns_name
print 'load banlancer dns name:%s'%(lbdns)

#Create a Launch Configuration for the instances that will become part of the auto scaling group, with the following parameters:
#AMI ID: ami-ec14ba84
#Instance Type: m3.medium
#Detailed Monitoring: enabled
autoscale=boto.ec2.autoscale.connect_to_region('us-east-1') #the client
#lc
lc=LaunchConfiguration(name='jianLaunchConfig',image_id='ami-3c8f3a54',key_name='jj',security_groups=['http'],instance_type='m3.medium',instance_monitoring=True)
autoscale.create_launch_configuration(lc)
print 'launch cofig created'
#ag
ag=AutoScalingGroup(group_name='jianGroup',load_balancers=['jianLb'],availability_zones=['us-east-1a'],launch_config=lc,min_size=2,max_size=4,connection=autoscale)
autoscale.create_auto_scaling_group(ag)
ag.put_notification_configuration(topic="arn:aws:sns:us-east-1:683895670525:launch",notification_types=['autoscaling:EC2_INSTANCE_LAUNCH', 'autoscaling:EC2_INSTANCE_LAUNCH_ERROR'])
ag.put_notification_configuration(topic="arn:aws:sns:us-east-1:683895670525:terminate",notification_types=['autoscaling:EC2_INSTANCE_TERMINATE','autoscaling:EC2_INSTANCE_TERMINATE_ERROR'])
print 'aotuscaling group createc'
#scaling policy
scale_up_policy=ScalingPolicy(name='jianScaleUp',adjustment_type='ChangeInCapacity',as_name='jianGroup',scaling_adjustment=2,cooldown=60)
scale_down_policy=ScalingPolicy(name='jianScaleDown',adjustment_type='ChangeInCapacity',as_name='jianGroup',scaling_adjustment=-2,cooldown=120)
#submit to aws
autoscale.create_scaling_policy(scale_up_policy)
autoscale.create_scaling_policy(scale_down_policy)
#request back again
scale_up_policy=autoscale.get_all_policies(as_group='jianGroup',policy_names=['jianScaleUp'])[0]
scale_down_policy=autoscale.get_all_policies(as_group='jianGroup',policy_names=['jianScaleDown'])[0]
print 'scaling policy created'
Example #40
0
def create_autoscaling_group(connection, module):
    group_name = module.params.get('name')
    load_balancers = module.params['load_balancers']
    availability_zones = module.params['availability_zones']
    launch_config_name = module.params.get('launch_config_name')
    min_size = module.params['min_size']
    max_size = module.params['max_size']
    desired_capacity = module.params.get('desired_capacity')
    vpc_zone_identifier = module.params.get('vpc_zone_identifier')
    set_tags = module.params.get('tags')
    health_check_period = module.params.get('health_check_period')
    health_check_type = module.params.get('health_check_type')
    default_cooldown = module.params.get('default_cooldown')
    wait_for_instances = module.params.get('wait_for_instances')
    as_groups = connection.get_all_groups(names=[group_name])
    wait_timeout = module.params.get('wait_timeout')
    termination_policies = module.params.get('termination_policies')
    notification_topic = module.params.get('notification_topic')
    notification_types = module.params.get('notification_types')

    if not vpc_zone_identifier and not availability_zones:
        region, ec2_url, aws_connect_params = get_aws_connection_info(module)
        try:
            ec2_connection = connect_to_aws(boto.ec2, region, **aws_connect_params)
        except (boto.exception.NoAuthHandlerFound, AnsibleAWSError) as e:
            module.fail_json(msg=str(e))
    elif vpc_zone_identifier:
        vpc_zone_identifier = ','.join(vpc_zone_identifier)

    asg_tags = []
    for tag in set_tags:
        for k,v in tag.iteritems():
            if k !='propagate_at_launch':
                asg_tags.append(Tag(key=k,
                     value=v,
                     propagate_at_launch=bool(tag.get('propagate_at_launch', True)),
                     resource_id=group_name))

    if not as_groups:
        if not vpc_zone_identifier and not availability_zones:
            availability_zones = module.params['availability_zones'] = [zone.name for zone in ec2_connection.get_all_zones()]
        enforce_required_arguments(module)
        launch_configs = connection.get_all_launch_configurations(names=[launch_config_name])
        ag = AutoScalingGroup(
                 group_name=group_name,
                 load_balancers=load_balancers,
                 availability_zones=availability_zones,
                 launch_config=launch_configs[0],
                 min_size=min_size,
                 max_size=max_size,
                 desired_capacity=desired_capacity,
                 vpc_zone_identifier=vpc_zone_identifier,
                 connection=connection,
                 tags=asg_tags,
                 health_check_period=health_check_period,
                 health_check_type=health_check_type,
                 default_cooldown=default_cooldown,
                 termination_policies=termination_policies)

        try:
            connection.create_auto_scaling_group(ag)
            if wait_for_instances:
                wait_for_new_inst(module, connection, group_name, wait_timeout, desired_capacity, 'viable_instances')
                wait_for_elb(connection, module, group_name)

            if notification_topic:
                ag.put_notification_configuration(notification_topic, notification_types)

            as_group = connection.get_all_groups(names=[group_name])[0]
            asg_properties = get_properties(as_group)
            changed = True
            return(changed, asg_properties)
        except BotoServerError as e:
            module.fail_json(msg="Failed to create Autoscaling Group: %s" % str(e), exception=traceback.format_exc(e))
    else:
        as_group = as_groups[0]
        changed = False
        for attr in ASG_ATTRIBUTES:
            if module.params.get(attr, None) is not None:
                module_attr = module.params.get(attr)
                if attr == 'vpc_zone_identifier':
                    module_attr = ','.join(module_attr)
                group_attr = getattr(as_group, attr)
                # we do this because AWS and the module may return the same list
                # sorted differently
                if attr != 'termination_policies':
                    try:
                        module_attr.sort()
                    except:
                        pass
                    try:
                        group_attr.sort()
                    except:
                        pass
                if group_attr != module_attr:
                    changed = True
                    setattr(as_group, attr, module_attr)

        if len(set_tags) > 0:
            have_tags = {}
            want_tags = {}

            for tag in asg_tags:
                want_tags[tag.key] = [tag.value, tag.propagate_at_launch]

            dead_tags = []
            for tag in as_group.tags:
                have_tags[tag.key] = [tag.value, tag.propagate_at_launch]
                if tag.key not in want_tags:
                    changed = True
                    dead_tags.append(tag)

            if dead_tags != []:
                connection.delete_tags(dead_tags)

            if have_tags != want_tags:
                changed = True
                connection.create_or_update_tags(asg_tags)

        # handle loadbalancers separately because None != []
        load_balancers = module.params.get('load_balancers') or []
        if load_balancers and as_group.load_balancers != load_balancers:
            changed = True
            as_group.load_balancers = module.params.get('load_balancers')

        if changed:
            try:
                as_group.update()
            except BotoServerError as e:
                module.fail_json(msg="Failed to update Autoscaling Group: %s" % str(e), exception=traceback.format_exc(e))

        if notification_topic:
            try:
                as_group.put_notification_configuration(notification_topic, notification_types)
            except BotoServerError as e:
                module.fail_json(msg="Failed to update Autoscaling Group notifications: %s" % str(e), exception=traceback.format_exc(e))

        if wait_for_instances:
            wait_for_new_inst(module, connection, group_name, wait_timeout, desired_capacity, 'viable_instances')
            wait_for_elb(connection, module, group_name)
        try:
            as_group = connection.get_all_groups(names=[group_name])[0]
            asg_properties = get_properties(as_group)
        except BotoServerError as e:
            module.fail_json(msg="Failed to read existing Autoscaling Groups: %s" % str(e), exception=traceback.format_exc(e))
        return(changed, asg_properties)
# Create a launch configuration; the set of information needed
# by the autoscale group to launch new instances
lc = LaunchConfiguration(
    name = LAUNCH_CONFIGURATION,
    image_id = AMI,
    key_name = KEY,
    security_groups = [SECURITY_GROUP])

print lc

asConnection.create_launch_configuration(lc)

# Create an autoscale group associated with the launch configuration
ag = AutoScalingGroup(
    group_name = AUTOSCALING_GROUP,
    availability_zones = [AVAILABILITY_ZONE],
    launch_config = lc,
    min_size = 1, max_size = 5,
    connection = asConnection)

print ag

asConnection.create_auto_scaling_group(ag)


#====================#
#= Scaling policies =#
#====================#

# The scaling policies tell us *how* to scale

scale_up_policy = ScalingPolicy(
Example #42
0
def create_autoscaling_group(connection, module):
    group_name = module.params.get('name')
    load_balancers = module.params['load_balancers']
    availability_zones = module.params['availability_zones']
    launch_config_name = module.params.get('launch_config_name')
    min_size = module.params['min_size']
    max_size = module.params['max_size']
    desired_capacity = module.params.get('desired_capacity')
    vpc_zone_identifier = module.params.get('vpc_zone_identifier')
    set_tags = module.params.get('tags')
    health_check_period = module.params.get('health_check_period')
    health_check_type = module.params.get('health_check_type')
    default_cooldown = module.params.get('default_cooldown')
    wait_for_instances = module.params.get('wait_for_instances')
    as_groups = connection.get_all_groups(names=[group_name])
    wait_timeout = module.params.get('wait_timeout')
    termination_policies = module.params.get('termination_policies')

    if not vpc_zone_identifier and not availability_zones:
        region, ec2_url, aws_connect_params = get_aws_connection_info(module)
        try:
            ec2_connection = connect_to_aws(boto.ec2, region,
                                            **aws_connect_params)
        except (boto.exception.NoAuthHandlerFound, AnsibleAWSError) as e:
            module.fail_json(msg=str(e))
    elif vpc_zone_identifier:
        vpc_zone_identifier = ','.join(vpc_zone_identifier)

    asg_tags = []
    for tag in set_tags:
        for k, v in tag.iteritems():
            if k != 'propagate_at_launch':
                asg_tags.append(
                    Tag(key=k,
                        value=v,
                        propagate_at_launch=bool(
                            tag.get('propagate_at_launch', True)),
                        resource_id=group_name))

    if not as_groups:
        if not vpc_zone_identifier and not availability_zones:
            availability_zones = module.params['availability_zones'] = [
                zone.name for zone in ec2_connection.get_all_zones()
            ]
        enforce_required_arguments(module)
        launch_configs = connection.get_all_launch_configurations(
            names=[launch_config_name])
        ag = AutoScalingGroup(group_name=group_name,
                              load_balancers=load_balancers,
                              availability_zones=availability_zones,
                              launch_config=launch_configs[0],
                              min_size=min_size,
                              max_size=max_size,
                              desired_capacity=desired_capacity,
                              vpc_zone_identifier=vpc_zone_identifier,
                              connection=connection,
                              tags=asg_tags,
                              health_check_period=health_check_period,
                              health_check_type=health_check_type,
                              default_cooldown=default_cooldown,
                              termination_policies=termination_policies)

        try:
            connection.create_auto_scaling_group(ag)
            if wait_for_instances:
                wait_for_new_inst(module, connection, group_name, wait_timeout,
                                  desired_capacity, 'viable_instances')
                wait_for_elb(connection, module, group_name)
            as_group = connection.get_all_groups(names=[group_name])[0]
            asg_properties = get_properties(as_group)
            changed = True
            return (changed, asg_properties)
        except BotoServerError as e:
            module.fail_json(msg=str(e))
    else:
        as_group = as_groups[0]
        changed = False
        for attr in ASG_ATTRIBUTES:
            if module.params.get(attr, None) is not None:
                module_attr = module.params.get(attr)
                if attr == 'vpc_zone_identifier':
                    module_attr = ','.join(module_attr)
                group_attr = getattr(as_group, attr)
                # we do this because AWS and the module may return the same list
                # sorted differently
                try:
                    module_attr.sort()
                except:
                    pass
                try:
                    group_attr.sort()
                except:
                    pass
                if group_attr != module_attr:
                    changed = True
                    setattr(as_group, attr, module_attr)

        if len(set_tags) > 0:
            have_tags = {}
            want_tags = {}

            for tag in asg_tags:
                want_tags[tag.key] = [tag.value, tag.propagate_at_launch]

            dead_tags = []
            for tag in as_group.tags:
                have_tags[tag.key] = [tag.value, tag.propagate_at_launch]
                if tag.key not in want_tags:
                    changed = True
                    dead_tags.append(tag)

            if dead_tags != []:
                connection.delete_tags(dead_tags)

            if have_tags != want_tags:
                changed = True
                connection.create_or_update_tags(asg_tags)

        # handle loadbalancers separately because None != []
        load_balancers = module.params.get('load_balancers') or []
        if load_balancers and as_group.load_balancers != load_balancers:
            changed = True
            as_group.load_balancers = module.params.get('load_balancers')

        if changed:
            try:
                as_group.update()
            except BotoServerError as e:
                module.fail_json(msg=str(e))

        if wait_for_instances:
            wait_for_new_inst(module, connection, group_name, wait_timeout,
                              desired_capacity, 'viable_instances')
            wait_for_elb(connection, module, group_name)
        try:
            as_group = connection.get_all_groups(names=[group_name])[0]
            asg_properties = get_properties(as_group)
        except BotoServerError as e:
            module.fail_json(msg=str(e))
        return (changed, asg_properties)
Example #43
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)