Пример #1
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))
Пример #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])
Пример #3
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
Пример #4
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'))
Пример #5
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)
Пример #6
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"
Пример #7
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
Пример #8
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
Пример #9
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)
Пример #10
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
Пример #11
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
Пример #12
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")
Пример #13
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
Пример #14
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
Пример #15
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
Пример #16
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()
Пример #17
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
Пример #18
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)
Пример #19
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)
    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]
Пример #21
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()
Пример #22
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)
Пример #23
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) ])
Пример #24
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)
Пример #25
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]
Пример #26
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"
Пример #27
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',
Пример #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']))
Пример #29
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)
Пример #30
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,