Beispiel #1
0
def test_describe_instance_health():
    ec2_conn = boto.connect_ec2()
    reservation = ec2_conn.run_instances('ami-1234abcd', 2)
    instance_id1 = reservation.instances[0].id
    instance_id2 = reservation.instances[1].id

    conn = boto.connect_elb()
    zones = ['us-east-1a', 'us-east-1b']
    ports = [(80, 8080, 'http'), (443, 8443, 'tcp')]
    lb = conn.create_load_balancer('my-lb', zones, ports)

    instances_health = conn.describe_instance_health('my-lb')
    instances_health.should.be.empty

    lb.register_instances([instance_id1, instance_id2])

    instances_health = conn.describe_instance_health('my-lb')
    instances_health.should.have.length_of(2)
    for instance_health in instances_health:
        instance_health.instance_id.should.be.within([instance_id1, instance_id2])
        instance_health.state.should.equal('InService')

    instances_health = conn.describe_instance_health('my-lb', [instance_id1])
    instances_health.should.have.length_of(1)
    instances_health[0].instance_id.should.equal(instance_id1)
    instances_health[0].state.should.equal('InService')
Beispiel #2
0
def deregister_instances(load_balancer, instances):
    '''
    Deregister EC2 Instances from an Elastic Load Balancer (ELB).

    * See also: :func:`sky.compute.register_instances` and :func:`sky.compute.rotate_instances`.

    :type load_balancer: :class:`boto.ec2.elb.loadbalancer.LoadBalancer`
    :param load_balancer: The :class:`~boto.ec2.elb.loadbalancer.LoadBalancer`
        that the EC2 Instances will be removed from.

        * See also: :func:`sky.compute.create_load_balancer`.

    :type instances: list
    :param instances: A list of EC2 :class:`~boto.ec2.instance.Instance` objects
        that will be deregistered from the Elastic Load Balancer (ELB).

        * See also: :func:`sky.compute.create_instances`.
    '''

    # Connect to the Amazon EC2 Load Balancing (Amazon ELB) service.
    logger.debug('Connecting to the Amazon EC2 Load Balancing (Amazon ELB) service.')
    elb_connection = boto.connect_elb()
    logger.debug('Connected to the Amazon EC2 Load Balancing (Amazon ELB) service.')

    logger.info('Deregistering (%s) from Load Balancer (%s).' % (', '.join([instance.tags['Name'] for instance in instances]) if len(instances) > 1 \
                                                                 else instances[-1].tags['Name'], \
                                                                 load_balancer.name))
    elb_connection.deregister_instances(load_balancer.name, [instance.id for instance in instances])
    logger.info('Deregistered (%s) from Load Balancer (%s).' % (', '.join([instance.tags['Name'] for instance in instances]) if len(instances) > 1 \
                                                                else instances[-1].tags['Name'], \
                                                                load_balancer.name))
 def setUpELB(self):
   conn_elb = boto.connect_elb()
   zones = ['us-east-1a']
   ports = [(80, 8080, 'http')]
   conn_elb.create_load_balancer('servergmsextenderELBstg', zones, ports)
   balancers = conn_elb.get_all_load_balancers(load_balancer_names=['servergmsextenderELBstg'])
   self.assertEqual(balancers[0].name, 'servergmsextenderELBstg')
 def _get_elb_instances(self):
     instance_list = boto.connect_elb().get_all_load_balancers(
             load_balancer_names=['myelb'])[0].instances
     l = []
     for i in instance_list:
         l.append(i.id)
     return l
Beispiel #5
0
def test_create_load_balancer():
    conn = boto.connect_elb()
    ec2 = boto.connect_ec2('the_key', 'the_secret')

    security_group = ec2.create_security_group('sg-abc987', 'description')

    zones = ['us-east-1a', 'us-east-1b']
    ports = [(80, 8080, 'http'), (443, 8443, 'tcp')]
    conn.create_load_balancer('my-lb', zones, ports, scheme='internal', security_groups=[security_group.id])

    balancers = conn.get_all_load_balancers()
    balancer = balancers[0]
    balancer.name.should.equal("my-lb")
    balancer.scheme.should.equal("internal")
    list(balancer.security_groups).should.equal([security_group.id])
    set(balancer.availability_zones).should.equal(
        set(['us-east-1a', 'us-east-1b']))
    listener1 = balancer.listeners[0]
    listener1.load_balancer_port.should.equal(80)
    listener1.instance_port.should.equal(8080)
    listener1.protocol.should.equal("HTTP")
    listener2 = balancer.listeners[1]
    listener2.load_balancer_port.should.equal(443)
    listener2.instance_port.should.equal(8443)
    listener2.protocol.should.equal("TCP")
Beispiel #6
0
def load_balance(ec2, env):
	""" Create the load balancers if they do not exist """
	elb = boto.connect_elb(ec2.access_key, ec2.secret_key)
	for machine in env:
		if 'load_balancer' in machine.keys():
			load_balancer = machine['load_balancer']
			
			# Set the defaults
			availability_zones = machine.get('availability_zones',['us-east-1a', 'us-east-1c', 'us-east-1d'])
			lb_name            = load_balancer.get('name',{})
			lb_listeners       = load_balancer.get('listeners',[(80, 80, 'http')])
			health_check       = load_balancer.get('health_check',{})

			try:
				new_lb = elb.get_all_load_balancers(load_balancer_names=[lb_name])[0]
			except:
				# Create a health check for the load balancer
				hc = boto.ec2.elb.HealthCheck(
						health_check.get('name','instance_health'),
						interval            = health_check.get('interval', 20),
						target              = health_check.get('target', 'HTTP:80/'),
						healthy_threshold   = health_check.get('healthy_threshold',2),
						timeout             = health_check.get('timeout',5),
						unhealthy_threshold = health_check.get('unhealthy_threshold',5),
						)

				# Create the load balancer if it does not exist
				new_lb = elb.create_load_balancer(lb_name, availability_zones, lb_listeners)
				new_lb.configure_health_check(hc)
				print 'Creating load balancer ', new_lb

			# Set the host
			machine['load_balancer']['host'] = new_lb.dns_name
Beispiel #7
0
def test_connection_draining_attribute():
    conn = boto.connect_elb()
    ports = [(80, 8080, 'http'), (443, 8443, 'tcp')]
    lb = conn.create_load_balancer('my-lb', [], ports)

    connection_draining = ConnectionDrainingAttribute()
    connection_draining.enabled = True
    connection_draining.timeout = 60

    conn.modify_lb_attribute(
        "my-lb", "ConnectionDraining", connection_draining)
    attributes = lb.get_attributes(force=True)
    attributes.connection_draining.enabled.should.be.true
    attributes.connection_draining.timeout.should.equal(60)

    connection_draining.timeout = 30
    conn.modify_lb_attribute(
        "my-lb", "ConnectionDraining", connection_draining)
    attributes = lb.get_attributes(force=True)
    attributes.connection_draining.timeout.should.equal(30)

    connection_draining.enabled = False
    conn.modify_lb_attribute(
        "my-lb", "ConnectionDraining", connection_draining)
    attributes = lb.get_attributes(force=True)
    attributes.connection_draining.enabled.should.be.false
Beispiel #8
0
def getELBConnection():
	r=RegionInfo(name='eu-west-1',endpoint='elasticloadbalancing.eu-west-1.amazonaws.com')
	
	print "Connect to region %s" %r
	conn = boto.connect_elb(aws_access_key_id=AWS_CONFIG['AWS_KEY'],aws_secret_access_key=AWS_CONFIG['AWS_SECRET'],region=r)

	return conn 
def elb_status():
	conn = boto.connect_elb(
		aws_access_key_id=app.config['ACCESS_KEY'],
		aws_secret_access_key=app.config['SECRET_KEY']
	)
	load_balancers = conn.get_all_load_balancers(app.config['LOAD_BALANCERS'])
	lbs = []
	for lb in load_balancers:
		instances = []
		for inst in lb.get_instance_health():
			instances.append({
				'id': inst.instance_id,
				'up': inst.state == 'InService',
				'name': get_instance_name(inst.instance_id),
			})
		lb_status = {
			'name': lb.name,
			'instances': instances,
			'redundant': True,
			'serving': True,
		}
		if len(instances) < 2 or not all([i['up'] for i in instances]):
			lb_status['redundant'] = False
		if len([i['up'] for i in instances]) == 0:
			lb_status['serving'] = False
		
		lbs.append(lb_status)
	return render_template('elb_status.html', lbs=lbs)
Beispiel #10
0
def get_url(app_name, env_name):
    protocol = 'http://' # TODO detect if load balancer supports HTTPS
    app = App(env_name, app_name)
    bucket_name = '{}-{}'.format(config.get('system_name', uuid.uuid1().hex), app.repo.name)

    ec2 = boto.connect_ec2()
    elb = boto.connect_elb()
    s3 = boto.connect_s3()

    b = s3.lookup(bucket_name)
    if b is not None:
        return protocol + b.get_website_endpoint()

    lb = None
    try:
        lbresult = elb.get_all_load_balancers(load_balancer_names=['{}-{}'.format(app.env_name, app.repo.name)])
        lb = lbresult[0] if len(lbresult) > 0 else None
    except boto.exception.BotoServerError:
        pass
    if lb is None:
        instances = ec2.get_only_instances(filters={'tag:app': app.name,
                                                    'tag:deployed': 'true',
                                                    'instance-state-name': 'running'})
        if len(instances) != 1:
            return None
        else:
            return protocol + instances[0].public_dns_name
    return protocol + lb.dns_name
Beispiel #11
0
 def instance_elbs(self, instance_id, profile_name=None):
     elb = boto.connect_elb(profile_name=profile_name)
     elbs = elb.get_all_load_balancers()
     for lb in elbs:
         lb_instance_ids = [inst.id for inst in lb.instances]
         if instance_id in lb_instance_ids:
             yield lb
Beispiel #12
0
def find_one_resource(stack, resource_type, only_id=False):
    stackresources = stack.describe_resources()

    resources = [r for r in stackresources if r.resource_type == resource_type]
    if len(resources) == 0:
        raise ValueError("This stack contains no AutoScale")
    if len(resources) > 1:
        raise ValueError("This stack contains more than one AutoScale")

    phy_id = resources[0].physical_resource_id

    if resource_type == RES_TYPE_ASG:
        if only_id:
            return phy_id
        else:
            try:
                return boto.connect_autoscale().get_all_groups([phy_id])[0]
            except IndexError:
                raise ValueError("The AutoScale physical id doesn't exist")

    elif resource_type == RES_TYPE_ELB:
        if only_id:
            return phy_id
        else:
            try:
                return boto.connect_elb().get_all_load_balancers([phy_id])[0]
            except IndexError:
                raise ValueError("The ELB physical id doesn't exist")

    else:
        raise NotImplementedError("Unkown resource type")
Beispiel #13
0
    def ami_for_edp(self, message, env, dep, play):
        ec2 = boto.connect_ec2(profile_name=dep)
        elb = boto.connect_elb(profile_name=dep)
        all_elbs = elb.get_all_load_balancers()

        edp_filter = {
            "tag:environment": env,
            "tag:deployment": dep,
            "tag:play": play,
        }
        reservations = ec2.get_all_instances(filters=edp_filter)
        amis = set()
        for reservation in reservations:
            for instance in reservation.instances:
                elbs = self.instance_elbs(instance.id, dep, all_elbs)
                if instance.state == 'running' and len(list(elbs)) > 0:
                    amis.add(instance.image_id)

        if len(amis) > 1:
            msg = "Multiple AMIs found for {}-{}-{}, there should " \
                "be only one. Please resolve any running deploys " \
                "there before running this command."
            msg = msg.format(env, dep, play)
            self.say(msg, message, color='red')
            return None

        if len(amis) == 0:
            msg = "No AMIs found for {}-{}-{}."
            msg = msg.format(env, dep, play)
            self.say(msg, message, color='red')
            return None

        return amis.pop()
Beispiel #14
0
def get_map(ec2):
	""" Map the data from each available connection """
	# Get extra connections
	elb = boto.connect_elb(ec2.access_key, ec2.secret_key)
	asg = boto.connect_autoscale(ec2.access_key, ec2.secret_key)
	s3b = boto.connect_s3(ec2.access_key,ec2.secret_key)

	# EC2 Keypairs
	keys = {}
	for k in ec2.get_all_key_pairs():
		keys[k.name] = k.fingerprint
	
	# EC2 Security Groups
	security_groups = {}
	for s in ec2.get_all_security_groups():
		rules = {}
		for r in s.rules:
			g = str(r.grants)
			if g not in rules: rules[g] = []
			rules[g].append('%s:[%s%s]' % (r.ip_protocol, r.from_port,
				r.to_port != r.from_port and '-'+r.to_port or ''))
		security_groups[s.name] = rules
	
	# Elastic Load Balancers
	elbs = {}
	for lb in elb.get_all_load_balancers():
		info = {}
		info['instances'] = lb.instances
		info['dns_name']  = lb.dns_name
		elbs[lb.name] = info

	# Need to map out 'asg'
	# * Launch Configurations
	# * AutoScaling Groups
	# * AutoScaling Triggers and Instances

	# S3 Buckets
	buckets = {}
	for b in s3b.get_all_buckets():
		buckets[b.name] = b

	# EC2 Instances
	instances = {}
	for r in ec2.get_all_instances():
		for i in r.instances:
			if i.image_id not in instances:
				instances[i.image_id] = {}
			if i.state not in instances[i.image_id]:
				instances[i.image_id][i.state] = []
			instances[i.image_id][i.state].append(i)
	
	data = {
		'asgs': {},
		'elbs': elbs,
		'instances': instances,
		'keys': keys,
		's3bs': buckets,
		'security_groups': security_groups,
	}
	return data
Beispiel #15
0
def ELBConn(reg,profile = 'default'):
	endpt = 'elasticloadbalancing.' + reg + '.amazonaws.com'
	reg = boto.regioninfo.RegionInfo(name=reg,endpoint=endpt)
	elbconn = ''
	try:
		elbconn = boto.connect_elb(profile_name=profile, region=reg)
        except Exception, e:
                boto.log.error("Cannot validate provided AWS credentials: %s" % e)
Beispiel #16
0
	def elbs(self):
		if len(self.ELBS) > 0:
			#print 'using cached ELB data'
			return self.ELBS
		print 'fetching load balancer data...'
		conn = connect_elb(aws_access_key_id=self.ACCOUNT.key(), aws_secret_access_key=self.ACCOUNT.secret_key())
		self.ELBS = conn.get_all_load_balancers()
		return self.ELBS
def getELBConn(region):
    """ A utility function that gets a connection to ELB. The returned connection
    must be close()d after use."""
    # Pretty sure the endpoint will never change
    endpoint = 'elasticloadbalancing.' + region + '.amazonaws.com';
    # Get the region object
    elb_region = boto.regioninfo.RegionInfo(name=region, endpoint=endpoint);
    return boto.connect_elb(region=elb_region);
Beispiel #18
0
 def connect(self):
     """Connect to API if not already connected; set self.conn."""
     if self.conn is not None:
         return
     elif self.region:
         self.conn = self.connect_via(boto.ec2.elb.connect_to_region)
     else:
         self.conn = boto.connect_elb()
 def setUpELB(self, env='stg'):
   conn_elb = boto.connect_elb()
   zones = ['us-east-1a']
   ports = [(80, 8080, 'http')]
   load_balancer_name = 'servergmsextenderELB{0}'.format(env)
   conn_elb.create_load_balancer(load_balancer_name, zones, ports)
   balancers = conn_elb.get_all_load_balancers(load_balancer_names=[load_balancer_name])
   self.assertEqual(balancers[0].name, load_balancer_name)
Beispiel #20
0
 def make_elb():
     elb_conn = boto.connect_elb()
     name = 'test_elb'
     zones = ['us-east-1a']
     listeners = [(80, 80, 'HTTP', 'HTTP')]
     elb = elb_conn.create_load_balancer(name, zones, complex_listeners=listeners)
     elb.idle_timeout = 60
     return elb_conn, elb
Beispiel #21
0
def test_default_attributes():
    conn = boto.connect_elb()
    ports = [(80, 8080, 'http'), (443, 8443, 'tcp')]
    lb = conn.create_load_balancer('my-lb', [], ports)
    attributes = lb.get_attributes()

    attributes.cross_zone_load_balancing.enabled.should.be.false
    attributes.connection_draining.enabled.should.be.false
    attributes.access_log.enabled.should.be.false
    attributes.connecting_settings.idle_timeout.should.equal(60)
Beispiel #22
0
def test_create_lb_policy():
    conn = boto.connect_elb()
    ports = [(80, 8080, 'http'), (443, 8443, 'tcp')]
    lb = conn.create_load_balancer('my-lb', [], ports)
    policy_name = "ProxyPolicy"

    lb.create_lb_policy(policy_name, 'ProxyProtocolPolicyType', {'ProxyProtocol': True})

    lb = conn.get_all_load_balancers()[0]
    lb.policies.other_policies[0].policy_name.should.equal(policy_name)
Beispiel #23
0
def update_elb_rds_dns(zone):
    """
    Creates elb and rds CNAME records
    in a zone for args.stack_name.
    Uses the tags of the instances attached
    to the ELBs to create the dns name
    """

    elb_con = boto.connect_elb()
    ec2_con = boto.connect_ec2()
    rds_con = boto.connect_rds()
    vpc_id = vpc_for_stack_name(args.stack_name)

    if not zone and args.noop:
        # use a placeholder for zone name
        # if it doesn't exist
        zone_name = "<zone name>"
    else:
        zone_name = zone.Name[:-1]

    stack_rdss = [rds for rds in rds_con.get_all_dbinstances()
                  if hasattr(rds.subnet_group, 'vpc_id') and
                  rds.subnet_group.vpc_id == vpc_id]
    for rds in stack_rdss:
        fqdn = "{}.{}".format('rds', zone_name)
        add_or_update_record(zone, fqdn, 'CNAME', 600,
                             [stack_rdss[0].endpoint[0]])

    stack_elbs = [elb for elb in elb_con.get_all_load_balancers()
                  if elb.vpc_id == vpc_id]
    for elb in stack_elbs:
        for inst in elb.instances:
            instance = ec2_con.get_all_instances(
                instance_ids=[inst.id])[0].instances[0]
            try:
                env_tag = instance.tags['environment']
                if 'play' in instance.tags:
                    play_tag = instance.tags['play']
                else:
                    # deprecated, for backwards compatibility
                    play_tag = instance.tags['role']
                play_tag = instance.tags['role']
                fqdn = "{}-{}.{}".format(env_tag, play_tag, zone_name)
                add_or_update_record(zone, fqdn, 'CNAME', 600, [elb.dns_name])
                if play_tag == 'edxapp':
                    # create courses and studio CNAME records for edxapp
                    for name in ['courses', 'studio']:
                        fqdn = "{}.{}".format(name, zone_name)
                        add_or_update_record(zone, fqdn, 'CNAME',
                                             600, [elb.dns_name])
                break  # only need the first instance for tag info
            except KeyError:
                print("Instance {}, attached to elb {} does not "
                      "have tags for environment and play".format(elb, inst))
                raise
Beispiel #24
0
def create_elb(elb_name):
    """
    Method to create an Elastic Load Balancer.
    """
    boto_elb = boto.connect_elb()
    zones = ['us-east-1a', 'us-east-1b']
    ports = [(80, 8080, 'http'), (443, 8443, 'tcp')]
    load_balancer = boto_elb.create_load_balancer(elb_name, zones, ports)
    instance_ids = ['i-4f8cf126', 'i-0bb7ca62']
    load_balancer.register_instances(instance_ids)
    return load_balancer
Beispiel #25
0
def update_elb_rds_dns(zone):
    """
    Creates elb and rds CNAME records
    in a zone for args.stack_name.
    Uses the tags of the instances attached
    to the ELBs to create the dns name
    """

    dns_records = set()

    elb_con = boto.connect_elb()
    rds_con = boto.connect_rds()

    vpc_id = vpc_for_stack_name(args.stack_name)

    if not zone and args.noop:
        # use a placeholder for zone name
        # if it doesn't exist
        zone_name = "<zone name>"
    else:
        zone_name = zone.Name[:-1]

    stack_elbs = [elb for elb in elb_con.get_all_load_balancers()
                  if elb.vpc_id == vpc_id]

    for elb in stack_elbs:

        if "RabbitMQ" in elb.source_security_group.name or "ElasticSearch" in elb.source_security_group.name:
            env_tag,deployment,play_tag = get_security_group_dns(elb.source_security_group.name)
            fqdn = "{}-{}.{}".format(env_tag, play_tag, zone_name)
            dns_records.add(DNSRecord(zone,fqdn,'CNAME',600,[elb.dns_name]))
        else:
            env_tag,play_tag = get_dns_from_instances(elb)
            fqdn = "{}-{}.{}".format(env_tag, play_tag, zone_name)
            dns_records.add(DNSRecord(zone,fqdn,'CNAME',600,[elb.dns_name]))

        if extra_play_dns.has_key(play_tag):
            for name in extra_play_dns.get(play_tag):
                fqdn = "{}-{}.{}".format(env_tag, name, zone_name)
                dns_records.add(DNSRecord(zone,fqdn,'CNAME',600,[elb.dns_name]))


    stack_rdss = [rds for rds in rds_con.get_all_dbinstances()
                  if hasattr(rds.subnet_group, 'vpc_id') and
                  rds.subnet_group.vpc_id == vpc_id]

    # TODO the current version of the RDS API doesn't support
    # looking up RDS instance tags.  Hence, we are using the 
    # env_tag that was set via the loop over instances above.
    for rds in stack_rdss:
        fqdn = "{}-{}.{}".format(env_tag,'rds', zone_name)
        dns_records.add(DNSRecord(zone,fqdn,'CNAME',600,[stack_rdss[0].endpoint[0]]))

    add_or_update_record(dns_records)
Beispiel #26
0
def _get_elb(name):
    """Get an ELB by name."""
    credentials = _get_credentials()
    if not credentials:
        return None
    conn = boto.connect_elb(credentials["access_key"], credentials["secret_key"])
    for lb in conn.get_all_load_balancers():
        if lb.name == name:
            return lb
    LOG.warning("Failed to find ELB: %s", name)
    return None
Beispiel #27
0
def test_create_lb_cookie_stickiness_policy_no_expiry():
    conn = boto.connect_elb()
    ports = [(80, 8080, 'http'), (443, 8443, 'tcp')]
    lb = conn.create_load_balancer('my-lb', [], ports)
    policy_name = "LBCookieStickinessPolicy"

    lb.create_cookie_stickiness_policy(None, policy_name)

    lb = conn.get_all_load_balancers()[0]
    lb.policies.lb_cookie_stickiness_policies[0].cookie_expiration_period.should.be.none
    lb.policies.lb_cookie_stickiness_policies[0].policy_name.should.equal(policy_name)
Beispiel #28
0
 def __init__(self, *args, **kwargs):
     self.zones = kwargs.pop('zones', list())
     # TODO - Process listeners
     self.listeners = kwargs.pop(
         'listeners',
         [(80, 8000, "HTTP", ), ]
     )
     if len(self.zones) == 0:
         raise LoadBalanceException("Load Balancer must have zones!")
     self.connection = boto.connect_elb()
     super(AWSLoadBalancer, self).__init__(*args, **kwargs)
Beispiel #29
0
 def get_connection_for_region(self, region):
     reg = RegionInfo(
         name=region,
         endpoint='elasticloadbalancing.'+region+'.amazonaws.com'
     )
     conn = boto.connect_elb(
         aws_access_key_id=self.public_api_key,
         aws_secret_access_key=self.private_api_key,
         region=reg
     )
     return conn
Beispiel #30
0
def test_create_app_cookie_stickiness_policy():
    conn = boto.connect_elb()
    ports = [(80, 8080, 'http'), (443, 8443, 'tcp')]
    lb = conn.create_load_balancer('my-lb', [], ports)
    cookie_name = "my-stickiness-policy"
    policy_name = "AppCookieStickinessPolicy"

    lb.create_app_cookie_stickiness_policy(cookie_name, policy_name)

    lb = conn.get_all_load_balancers()[0]
    lb.policies.app_cookie_stickiness_policies[0].cookie_name.should.equal(cookie_name)
    lb.policies.app_cookie_stickiness_policies[0].policy_name.should.equal(policy_name)
Beispiel #31
0
 def test_elb_health_checks_form(self):
     request = self.create_request()
     self.setup_session(request)
     elb = self.make_elb()
     request.matchdict['id'] = elb.name
     elb.health_check = Mock(
         interval=30,
         timeout=30,
         healthy_threshold=3,
         unhealthy_threshold=2,
         target='HTTP:80/index.html',
     )
     elb_conn = boto.connect_elb()
     form = ELBHealthChecksForm(request, elb_conn=elb_conn, elb=elb)
     self.assertEqual(form.ping_protocol.data, 'HTTP')
     self.assertEqual(form.ping_port.data, 80)
     self.assertEqual(form.ping_path.data, '/index.html')
     self.assertEqual(form.time_between_pings.data, '30')
     self.assertEqual(form.response_timeout.data, 30)
     self.assertEqual(form.failures_until_unhealthy.data, '2')
     self.assertEqual(form.passes_until_healthy.data, '3')
Beispiel #32
0
def get_all_load_balancers(names=None):
    """
    Get all the ELBs

    Arguments:
        names (list): A list of ELB names as strings

    Returns:
        a list of :class:`boto.ec2.elb.loadbalancer.LoadBalancer`
    """
    elb_conn = boto.connect_elb()
    fetched_elbs = elb_conn.get_all_load_balancers(names)
    total_elbs = []
    while True:
        total_elbs.extend(list(fetched_elbs))
        if fetched_elbs.next_token:
            fetched_elbs = elb_conn.get_all_load_balancers(
                names, fetched_elbs.next_token)
        else:
            break
    return total_elbs
Beispiel #33
0
def test_create_lb_cookie_stickiness_policy():
    conn = boto.connect_elb()
    ports = [(80, 8080, 'http'), (443, 8443, 'tcp')]
    lb = conn.create_load_balancer('my-lb', [], ports)
    cookie_expiration_period = 60
    policy_name = "LBCookieStickinessPolicy"

    lb.create_cookie_stickiness_policy(cookie_expiration_period, policy_name)

    lb = conn.get_all_load_balancers()[0]
    # There appears to be a quirk about boto, whereby it returns a unicode
    # string for cookie_expiration_period, despite being stated in
    # documentation to be a long numeric.
    #
    # To work around that, this value is converted to an int and checked.
    cookie_expiration_period_response_str = lb.policies.lb_cookie_stickiness_policies[
        0].cookie_expiration_period
    int(cookie_expiration_period_response_str).should.equal(
        cookie_expiration_period)
    lb.policies.lb_cookie_stickiness_policies[
        0].policy_name.should.equal(policy_name)
Beispiel #34
0
def test_create_load_balancer():
    conn = boto.connect_elb()

    zones = ['us-east-1a', 'us-east-1b']
    ports = [(80, 8080, 'http'), (443, 8443, 'tcp')]
    conn.create_load_balancer('my-lb', zones, ports, scheme='internal')

    balancers = conn.get_all_load_balancers()
    balancer = balancers[0]
    balancer.name.should.equal("my-lb")
    balancer.scheme.should.equal("internal")
    set(balancer.availability_zones).should.equal(
        set(['us-east-1a', 'us-east-1b']))
    listener1 = balancer.listeners[0]
    listener1.load_balancer_port.should.equal(80)
    listener1.instance_port.should.equal(8080)
    listener1.protocol.should.equal("HTTP")
    listener2 = balancer.listeners[1]
    listener2.load_balancer_port.should.equal(443)
    listener2.instance_port.should.equal(8443)
    listener2.protocol.should.equal("TCP")
Beispiel #35
0
def test_create_health_check():
    conn = boto.connect_elb()

    hc = HealthCheck(
        interval=20,
        healthy_threshold=3,
        unhealthy_threshold=5,
        target='HTTP:8080/health',
        timeout=23,
    )

    lb = conn.create_load_balancer('my-lb', [], [])
    lb.configure_health_check(hc)

    balancer = conn.get_all_load_balancers()[0]
    health_check = balancer.health_check
    health_check.interval.should.equal(20)
    health_check.healthy_threshold.should.equal(3)
    health_check.unhealthy_threshold.should.equal(5)
    health_check.target.should.equal('HTTP:8080/health')
    health_check.timeout.should.equal(23)
Beispiel #36
0
def test_access_log_attribute():
    conn = boto.connect_elb()
    ports = [(80, 8080, 'http'), (443, 8443, 'tcp')]
    lb = conn.create_load_balancer('my-lb', [], ports)

    access_log = AccessLogAttribute()
    access_log.enabled = True
    access_log.s3_bucket_name = 'bucket'
    access_log.s3_bucket_prefix = 'prefix'
    access_log.emit_interval = 60

    conn.modify_lb_attribute("my-lb", "AccessLog", access_log)
    attributes = lb.get_attributes(force=True)
    attributes.access_log.enabled.should.be.true
    attributes.access_log.s3_bucket_name.should.equal("bucket")
    attributes.access_log.s3_bucket_prefix.should.equal("prefix")
    attributes.access_log.emit_interval.should.equal(60)

    access_log.enabled = False
    conn.modify_lb_attribute("my-lb", "AccessLog", access_log)
    attributes = lb.get_attributes(force=True)
    attributes.access_log.enabled.should.be.false
Beispiel #37
0
def test_create_health_check():
    conn = boto.connect_elb()

    hc = HealthCheck(
        interval=20,
        healthy_threshold=3,
        unhealthy_threshold=5,
        target="HTTP:8080/health",
        timeout=23,
    )

    ports = [(80, 8080, "http"), (443, 8443, "tcp")]
    lb = conn.create_load_balancer("my-lb", [], ports)
    lb.configure_health_check(hc)

    balancer = conn.get_all_load_balancers()[0]
    health_check = balancer.health_check
    health_check.interval.should.equal(20)
    health_check.healthy_threshold.should.equal(3)
    health_check.unhealthy_threshold.should.equal(5)
    health_check.target.should.equal("HTTP:8080/health")
    health_check.timeout.should.equal(23)
Beispiel #38
0
def format_autoscale_instances(stack):
    s = []

    tmpl = "  ASG: {id} {health}/{state} LC:{lc}"

    res_asg = find_one_resource(stack, RES_TYPE_ASG)

    for i in res_asg.instances:
        s.append(
            tmpl.format(id=i.instance_id,
                        state=i.lifecycle_state,
                        health=i.health_status,
                        lc=i.launch_config_name))

    tmpl = "  ELB: {i.instance_id} {i.state} ({i.reason_code})"

    res_elb_id = find_one_resource(stack, RES_TYPE_ELB, only_id=True)
    ihealth = boto.connect_elb().describe_instance_health(res_elb_id)

    for i in ihealth:
        s.append(tmpl.format(i=i))

    return '\n'.join(s)
Beispiel #39
0
def test_create_load_balancer_with_certificate():
    conn = boto.connect_elb()

    zones = ["us-east-1a"]
    ports = [(
        443,
        8443,
        "https",
        "arn:aws:iam:{}:server-certificate/test-cert".format(ACCOUNT_ID),
    )]
    conn.create_load_balancer("my-lb", zones, ports)

    balancers = conn.get_all_load_balancers()
    balancer = balancers[0]
    balancer.name.should.equal("my-lb")
    balancer.scheme.should.equal("internet-facing")
    set(balancer.availability_zones).should.equal(set(["us-east-1a"]))
    listener = balancer.listeners[0]
    listener.load_balancer_port.should.equal(443)
    listener.instance_port.should.equal(8443)
    listener.protocol.should.equal("HTTPS")
    listener.ssl_certificate_id.should.equal(
        "arn:aws:iam:{}:server-certificate/test-cert".format(ACCOUNT_ID))
Beispiel #40
0
def test_connection_draining_attribute():
    conn = boto.connect_elb()
    ports = [(80, 8080, 'http'), (443, 8443, 'tcp')]
    lb = conn.create_load_balancer('my-lb', [], ports)

    connection_draining = ConnectionDrainingAttribute()
    connection_draining.enabled = True
    connection_draining.timeout = 60

    conn.modify_lb_attribute("my-lb", "ConnectionDraining", connection_draining)
    attributes = lb.get_attributes(force=True)
    attributes.connection_draining.enabled.should.be.true
    attributes.connection_draining.timeout.should.equal(60)

    connection_draining.timeout = 30
    conn.modify_lb_attribute("my-lb", "ConnectionDraining", connection_draining)
    attributes = lb.get_attributes(force=True)
    attributes.connection_draining.timeout.should.equal(30)

    connection_draining.enabled = False
    conn.modify_lb_attribute("my-lb", "ConnectionDraining", connection_draining)
    attributes = lb.get_attributes(force=True)
    attributes.connection_draining.enabled.should.be.false
Beispiel #41
0
    def _ami_for_edp(self, message, env, dep, play):
        """
        Given an EDP, return its active AMI.
        """
        ec2 = boto.connect_ec2(profile_name=dep)
        elb = boto.connect_elb(profile_name=dep)
        all_elbs = elb.get_all_load_balancers()

        edp_filter = {
            "tag:environment": env,
            "tag:deployment": dep,
            "tag:play": play,
        }
        reservations = ec2.get_all_instances(filters=edp_filter)
        amis = set()
        for reservation in reservations:
            for instance in reservation.instances:
                elbs = self._instance_elbs(instance.id, dep, all_elbs)
                if instance.state == 'running' and len(list(elbs)) > 0:  # pylint: disable=len-as-condition
                    amis.add(instance.image_id)

        if len(amis) > 1:
            msg = "Multiple AMIs found for {}-{}-{}, there should " \
                "be only one. Please resolve any running deploys " \
                "there before running this command."
            msg = msg.format(env, dep, play)
            self.say(msg, message, color='red')
            return None

        if len(amis) == 0:  # pylint: disable=len-as-condition
            msg = "No AMIs found for {}-{}-{}."
            msg = msg.format(env, dep, play)
            self.say(msg, message, color='red')
            return None

        return amis.pop()
 def _create_external_elb(self):
     return boto.connect_elb().create_load_balancer(
         name='myelb',
         zones='us-east-1a',
         listeners=[[80, 8080, 'http'], [443, 8443, 'tcp']])
Beispiel #43
0
def migrate_cfg(args):
    """Migrate the stack: re-instantiate all instances."""
    config, settings, sinfo = initialize_from_cli(args)
    stack = find_one_stack(args.stack_name, summary=False)
    print(format_stack_summary(stack))

    asg = find_one_resource(stack, RES_TYPE_ASG)
    yield format_autoscale(asg)

    orig_min = asg.min_size
    orig_max = asg.max_size
    orig_desired = asg.desired_capacity
    orig_term_pol = asg.termination_policies

    mig_min = orig_desired * 2
    mig_max = orig_desired * 2
    mig_desired = orig_desired * 2
    mig_term_pol = [u'OldestLaunchConfiguration', u'OldestInstance']

    if orig_desired != len(asg.instances):
        raise CommandError("The ASG is not stable (desired != instances)")

    for instance in asg.instances:
        if instance.health_status != 'Healthy':
            raise CommandError("The ASG is not stable (instance not healthy)")

    warn_for_live(sinfo)
    confirm_action(arg, default=True)

    yield "\n <> Setting termination policy to %s" % mig_term_pol
    asg.termination_policies = mig_term_pol
    asg.update()

    yield "\n <> Growing the desired capacity from %s to %s" % (
        orig_desired, mig_desired)
    asg.min_size = mig_min
    asg.max_size = mig_max
    asg.desired_capacity = mig_desired
    asg.update()

    yield "\n <> Waiting instances to stabilize..."
    while True:
        sleep(30)
        asg = find_one_resource(stack, RES_TYPE_ASG)
        res_elb_id = find_one_resource(stack, RES_TYPE_ELB, only_id=True)
        elbinstances = boto.connect_elb().describe_instance_health(res_elb_id)
        if len(asg.instances) < mig_desired:
            yield "    NOTYET: only %i instances created" % len(asg.instances)
            continue
        elif [i for i in asg.instances if i.health_status != 'Healthy']:
            yield "    NOTYET: still unhealthy instances"
            continue
        elif len(elbinstances) < mig_desired:
            yield "    NOTYET: only %i instances in ELB" % len(elbinstances)
            continue
        elif [i for i in elbinstances if i.state != 'InService']:
            yield "    NOTYET: not all instances are ELB:InService"
            continue
        else:
            yield "    OK: %s healthy instances in ELB" % len(asg.instances)
            break

    yield "\n <> Checking new ASG state..."
    asg = find_one_resource(stack, RES_TYPE_ASG)
    yield format_autoscale(asg)
    yield format_autoscale_instances(stack)

    yield "\n <> Restoring previous ASG control:"
    asg.termination_policies = orig_term_pol
    asg.min_size = orig_min
    asg.max_size = orig_max
    asg.desired_capacity = orig_desired
    yield format_autoscale(asg)

    if confirm('Restoring ASG config?', default=True):
        try:
            asg.update()
        except BotoServerError as error:
            yield "\n <> Restoration failed!"
            yield error
        else:
            yield "\n <> ASG control restored."
    else:
        yield "WARNING: The ASG desired capacity was doubled!"
Beispiel #44
0
def elbs_for_stack_name(stack_name):
    vpc_id = vpc_for_stack_name(stack_name)
    elbs = boto.connect_elb()
    for elb in elbs.get_all_load_balancers():
        if elb.vpc_id == vpc_id:
            yield elb
Beispiel #45
0
def get_active_webserver_ids(load_balancer="LoadBalancer2"):
    elb = boto.connect_elb(*aws_credentials)
    return [
        inst.instance_id
        for inst in elb.describe_instance_health(load_balancer)
    ]
def get_conn(accesskey, secretkey):
    if accesskey and secretkey:
        return boto.connect_elb(accesskey, secretkey)
    else:
        return boto.connect_elb()
Beispiel #47
0
 def get_entities_for_region(self, region):
     elb = boto.connect_elb(self.access_key_id,
                            self.secret_access_key,
                            region=region)
     return elb.get_all_load_balancers()
def startup():
    numinstance = 0
    for res in reservations:
        for inst in res.instances:
            if (inst.state == "stopped" and inst.vpc_id == vpc_id):
                instanceid.append(inst.id)
                instancename.append(inst.tags['Name'])
                if inst.tags['Name'].find("p-bosh") <> -1:
                    microboshinstance = numinstance
                if inst.tags['Name'].find("OpsManager") <> -1:
                    OpsManagerInstanceId = inst.id
                if inst.tags['Name'].find("router") <> -1:
                    print "Found a router - marking for ELB Addition..."
                    routerinstance = numinstance
                    routerinstances.append(inst.id)
                numinstance = numinstance + 1

    for x in range(bootinstances - 1, -1, -1):
        for y in range(0, numinstance):
            if instancename[y].find(bootorder[x]) <> -1:
                if checkinstance(instanceid[y]) == "stopped":
                    print "Starting Instance: " + instanceid[
                        y] + " : " + instancename[y]
                    startinstance(instanceid[y])
                break

    print "Starting Microbosh"
    startinstance(instanceid[microboshinstance])

    ## Since the Router has restarted we need to Remove and Add the router to the existing ELB.
    elbconn = boto.connect_elb()

    ## This section assigns the elb that has a matching vpc_id
    load_balancers = elbconn.get_all_load_balancers()
    for elb in load_balancers:
        if elb.vpc_id == vpc_id:
            load_balancer = elb
    elbrouterinstances = load_balancer.instances
    print "Here's a list of routers: " + str(routerinstances)
    for inst in elbrouterinstances:
        print "Removing instance: " + str(
            inst.id) + " from ELB: " + load_balancer.name
        elbconn.deregister_instances(load_balancer.name, inst.id)
    print "Waiting for router to startup..."
    for inst in routerinstances:
        instanceready = "false"
        while (instanceready == "false"):
            if checkinstance(inst) <> "running":
                time.sleep(5)
            else:
                instanceready = "true"

    ## This appears to be a reasonable amount of time for the services within the VM to startup.
    ## Since this VM is in a private subnet inaccessible from internet there's no way to test for specific service startup
    ## If added to early the ELB views the instance as "unhealthy" and marks it out of service
    ## Another way to potentially check on this would be to check the state of the ELB instance (i.e. InService or OutOfService) and remove/add with delay until InService

    print 'Waiting ',
    example_1(130)

    for inst in routerinstances:
        print "Adding instance: " + inst + " to ELB: " + load_balancer.name
        elbconn.register_instances(load_balancer.name, inst)
    print "Startup Complete!"
    print "Ops Manager Public DNS: http://" + getpublicdns(
        OpsManagerInstanceId)
Beispiel #49
0
if __name__ == '__main__':

    (opts, args) = parse_args()

    int_config = yaml.load(open(opts.int_config).read())
    if not opts.eip_log:
        output_dir = os.path.expanduser(int_config["output_dir"])
        opts.eip_log = output_dir + '/' + opts.match_re.replace(
            '^', '') + '-eip_integration_tests.log'

    # Connect to AWS
    aws = boto.connect_ec2(aws_access_key_id=opts.ec2_access_key,
                           aws_secret_access_key=opts.ec2_secret_key)

    elb = boto.connect_elb(aws_access_key_id=opts.ec2_access_key,
                           aws_secret_access_key=opts.ec2_secret_key)

    asg = boto.connect_autoscale(aws_access_key_id=opts.ec2_access_key,
                                 aws_secret_access_key=opts.ec2_secret_key)

    try:
        # Delete matching keys
        delete_aws_resources(aws.get_all_key_pairs, 'name', opts)

        # Delete matching security groups
        delete_aws_resources(aws.get_all_security_groups, 'name', opts)

        # Delete matching ASGs
        delete_aws_resources(asg.get_all_groups, 'name', opts)

        # Delete matching launch configs
Beispiel #50
0
    def setup_elb_connection(self,
                             endpoint=None,
                             aws_access_key_id=None,
                             aws_secret_access_key=None,
                             is_secure=True,
                             host=None,
                             region=None,
                             path="/",
                             port=443,
                             boto_debug=0):
        """

        :param endpoint:
        :param aws_access_key_id:
        :param aws_secret_access_key:
        :param is_secure:
        :param host:
        :param region:
        :param path:
        :param port:
        :param boto_debug:
        :raise:
        """
        elb_region = RegionInfo()
        if region:
            self.debug("Check region: " + str(region))
            try:
                if not endpoint:
                    elb_region.endpoint = ELBRegionData[region]
                else:
                    elb_region.endpoint = endpoint
            except KeyError:
                raise Exception('Unknown region: %s' % region)
        else:
            elb_region.name = 'eucalyptus'
            if not host:
                if endpoint:
                    elb_region.endpoint = endpoint
                else:
                    elb_region.endpoint = self.get_elb_ip()
        connection_args = {
            'aws_access_key_id': aws_access_key_id,
            'aws_secret_access_key': aws_secret_access_key,
            'is_secure': is_secure,
            'debug': boto_debug,
            'port': port,
            'path': path,
            'region': elb_region
        }

        if re.search('2.6', boto.__version__):
            connection_args['validate_certs'] = False

        try:
            elb_connection_args = copy.copy(connection_args)
            elb_connection_args['path'] = path
            elb_connection_args['region'] = elb_region
            self.debug("Attempting to create load balancer connection to " +
                       elb_region.endpoint + ':' + str(port) + path)
            self.elb = boto.connect_elb(**elb_connection_args)
        except Exception, e:
            self.critical(
                "Was unable to create elb connection because of exception: " +
                str(e))
Beispiel #51
0
def real_main():
    dashes = "-----------------------------------------------"
    ec2_conn = boto.ec2.connect_to_region("eu-west-1",
                    aws_access_key_id=os.environ.get('AWS_ACCESS_KEY', None),
                    aws_secret_access_key=os.environ.get('AWS_SECRET_KEY', None))
    iam_conn = boto.connect_iam(aws_access_key_id=os.environ.get('AWS_ACCESS_KEY', None),
                    aws_secret_access_key=os.environ.get('AWS_SECRET_KEY', None))
    elb_region = boto.regioninfo.RegionInfo( name='eu-west-1', endpoint='elasticloadbalancing.eu-west-1.amazonaws.com')
    elb_conn = boto.connect_elb(region=elb_region, aws_access_key_id=os.environ.get('AWS_ACCESS_KEY', None), aws_secret_access_key=os.environ.get('AWS_SECRET_KEY', None))

    # ACCOUNT INFO
    account_aliases = get_account_aliases(iam_conn)
    print("{0}\nAccount ID:      {1}".format(dashes, get_account_id(iam_conn)))
    print("Account Aliases: {0}\n{1}".format(",".join(account_aliases),dashes))

    # UNNAMED INSTANCES
    unnamed_instances = get_unnamed_instances(ec2_conn)
    if unnamed_instances:
        print("Unnamed instances\n{0}".format(dashes))
        for inst in unnamed_instances:
            print("{0} | {1}".format(inst.id, inst.instance_type))
        print("{0}".format(dashes))

    # EIPS
    unassigned_addresses = get_unassociated_eips(ec2_conn)
    if unassigned_addresses:
        print("Unassigned EIPs\n{0}".format(dashes))
        for addr in unassigned_addresses:
            print(addr)
        print("{0}".format(dashes))

    # OPEN SECURITY GROUPS
    open_groups = get_open_secgroups(ec2_conn)
    elbs = elb_conn.get_all_load_balancers()
    if open_groups:
        print("Open Security Groups\n{0}".format(dashes))
        for og in open_groups:
            og_instances = og.instances()
            has_running_instance = False
            has_elb = False
            for ins in og_instances:
                if ins.state == 'running':
                    has_running_instance = True
                    break
            for elb in elbs:
                if (og.name == elb.source_security_group.name) or ([x for x in elb.security_groups if og.name == x[0]]):
                    has_elb = True

            if has_elb:
                print("{0} associated with ELBs:".format(og.name))
                for elb in elbs:
                    if (og.name == elb.source_security_group.name) or ([x for x in elb.security_groups if og.name == x[0]]):
                        print("  Name: {0}".format(elb.name))
            if has_running_instance:
                print("{0} associated with running instances:".format(og.name))
                for int in og_instances:
                    if ins.state == 'running':
                        print("  id: {0} | Name: {1}".format(ins.id, ins.tags.get('Name', '<Unnamed>')))
            if not any((has_elb, has_running_instance)):
                print("{0}".format(og.name))
        print("{0}".format(dashes))

    # UNATTACHED VOLUMES
    unattached_volumes = get_unattached_volumes(ec2_conn)
    if unattached_volumes:
       print("Unattached Volumes\n{0}".format(dashes))
       for vol in unattached_volumes:
           print("  {} | {}".format(vol.id, vol.tags.get('Name','<Unnamed>')))
       print("{0}".format(dashes))

        
    # ORPHANED SNAPSHOTS
    orphaned_snapshots = get_orphaned_snapshots(ec2_conn)
    if orphaned_snapshots:
       print("Orphaned Snapshots\n{0}".format(dashes))
       for snap in orphaned_snapshots:
           print("{0} | {1} | {2}".format(snap[0], snap[1], snap[2]))
       print("{0}".format(dashes))
Beispiel #52
0
def test_create_load_balancer_duplicate():
    conn = boto.connect_elb()
    ports = [(80, 8080, "http"), (443, 8443, "tcp")]
    conn.create_load_balancer("my-lb", [], ports)
    conn.create_load_balancer.when.called_with(
        "my-lb", [], ports).should.throw(BotoServerError)
 def _get_elbs(self):
     return boto.connect_elb().get_all_load_balancers(
         load_balancer_names=['myelb'])
Beispiel #54
0
def test_getting_missing_elb():
    conn = boto.connect_elb()
    conn.get_all_load_balancers.when.called_with(
        load_balancer_names="aaa").should.throw(BotoServerError)
Beispiel #55
0
 def connect(self):
     if self._connection is None:
         self._connection = connect_elb(**self._credentials)
     return self._connection
def main():
    # Define arguement variables
    app = ""
    nvtype = ""
    env = ""
    ver = ""
    owner = ""
    email = ""
    stackname = ""

    # Define globals
    helpmessage = "fimt-update-cf-stack.py --app <Application> --type <EnvironmentType> --env <ChefEnvironment> --ver <Version> --owner <eid> --email <owneremail> --stackname <aws-cfn-stackname>"
    knifefile = "/opt/chef/developer12/developer/knife.rb"

    # Check if less than 9 parameters were passed
    #if len(sys.argv) < 9:
    #	print helpmessage
    #	sys.exit(2)

    # Grab parameters and populate variables
    try:
        opts, args = getopt.getopt(
            sys.argv[1:], "ha:t:v:o:e:s:",
            ["app=", "type=", "ver=", "owner=", "email=", "stackname="])
    except getopt.GetoptError:
        print helpmessage
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print helpmessage
            sys.exit(2)
        elif opt in ("-a", "--app"):
            app = arg
        elif opt in ("-t", "--type"):
            nvtype = arg
        elif opt in ("-v", "--ver"):
            ver = arg
        elif opt in ("-o", "--owner"):
            owner = arg
        elif opt in ("-e", "--email"):
            email = arg
        elif opt in ("-s", "--stackname"):
            stackname = arg
        else:
            print helpmessage
            sys.exit(2)

    # If any of the variables were blank, echo help and quit
    #if (app == '') or (nvtype == '') or (env == '') or (ver == '') or (owner == '') or (email == '') or (stackname == ''):
    #	print helpmessage
    #	sys.exit(2)

    # Pull Chef Environment default_attributes
    chefpull = "/usr/bin/knife environment show -a default_attributes " + app + "_" + nvtype
    #+ "_" + env + "_" + ver + " -c " + knifefile + " | sed 1,2d"
    print "\nPulling chef environment:\n" + chefpull
    defattrib = commands.getoutput(chefpull)
    print "\nOutput:\n" + defattrib
    diction = dict(item.strip().split(":") for item in defattrib.splitlines())
    region = diction["region"].strip()

    # Build CFN parameters
    cfecho = ""
    cfnparams = diction["cfn_inputs"].strip().split(",")
    for detail in diction["echo2cfdetails"].strip().split(","):
        cfecho = cfecho + "echo " + detail + " >> /tmp/cf_details.txt;"
    paramslist = []
    for param in cfnparams:
        if "TemplateURI" in param:
            paramslist.append((param, 'https://' + diction[param].strip()))
        elif "echo2cfdetails" in param:
            paramslist.append((param, cfecho))
        else:
            paramslist.append((param, diction[param].strip()))
    paramslist.append(('OwnerEID', owner))
    paramslist.append(('OwnerEmail', email))
    paramslist.append(('ApplicationName', app))
    paramslist.append(('Environment', nvtype))
    print "\nParameters Contents:\n"
    print paramslist

    # Set S3 Template URL
    s3template = "https://" + diction["ParentTemplateS3File"].strip()

    # Run CFN command
    cfnconn = boto.cloudformation.connect_to_region(region)
    print "\nUpdating stack...\n"
    cfnoutput = cfnconn.update_stack(stackname,
                                     template_url=s3template,
                                     parameters=paramslist)
    if "arn:aws:cloudformation:" not in cfnoutput:
        print "Stack udpate failed."
        sys.exit(2)
    else:
        print "Output:\n" + cfnoutput

    # Wait for Stack to finish
    stacks = cfnconn.describe_stacks(stackname)
    stack = stacks[0]
    lcv = 0
    print "\nWaiting for stack to update..."
    while "UPDATE_COMPLETE" not in stack.stack_status:
        lcv += 1
        if "ROLLBACK" in stack.stack_status:
            print "\nStack is being rolled back. Check the cloudformation events for your stack to troubleshoot: " + stackname
            sys.exit(2)
        if lcv > 90:
            print "\nStack did not update."
            print "Sending cfn update cancel: \n"
            cfnconn.cancel_update_stack(stackname)
            sys.exit(2)
        time.sleep(10)
        stacks = cfnconn.describe_stacks(stackname)
        stack = stacks[0]
    print "Stack update complete.\n"

    # Get ELB Names
    webelbname = ""
    appelbname = ""
    stacks = cfnconn.describe_stacks(stackname)
    stack = stacks[0]
    for output in stack.outputs:
        if output.key == "WebELBName":
            webelbname = output.value
        elif output.key == "AppELBName":
            appelbname = output.value

    # Get starting IPs of instances
    appinstanceip = ""
    webinstanceip = ""
    elbconn = boto.connect_elb(region)
    ec2conn = boto.ec2.connect_to_region(region)
    load_balancer = elbconn.get_all_load_balancers(appelbname)[0]
    instance_ids = [instance.id for instance in load_balancer.instances]
    reservations = ec2conn.get_all_instances(instance_ids)
    instance_addresses = [
        i.private_ip_address for r in reservations for i in r.instances
    ]
    appinstanceip = "*\t\t"
    for instanceip in instance_addresses:
        appinstanceip = appinstanceip + instanceip + "  "
    load_balancer = elbconn.get_all_load_balancers(webelbname)[0]
    instance_ids = [instance.id for instance in load_balancer.instances]
    reservations = ec2conn.get_all_instances(instance_ids)
    instance_addresses = [
        i.private_ip_address for r in reservations for i in r.instances
    ]
    webinstanceip = "*\t\t"
    for instanceip in instance_addresses:
        webinstanceip = webinstanceip + instanceip + "  "

    # Get ELB Endpoints
    webelbendpoint = ""
    appelbendpoint = ""
    stacks = cfnconn.describe_stacks(stackname)
    stack = stacks[0]
    for output in stack.outputs:
        if output.key == "WebELBURL":
            webelbendpoint = output.value
        elif output.key == "AppELBURL":
            appelbendpoint = output.value

    # Print out summary
    print "******************************************************************************************"
    print "*\tStack Name: " + stackname
    print "*"
    print "*\t" + app + " Web Info:"
    print "*\tWeb ELB: " + webelbendpoint
    print "*\tWeb Chef Roles: " + diction["WebChefRoles"].strip()
    print "*\tStarting Web Instance IPs:"
    print webinstanceip
    print "*"
    print "*\t" + app + " App Info:"
    print "*\tApp ELB: " + appelbendpoint
    print "*\tApp Chef Roles: " + diction["AppChefRoles"].strip()
    print "*\tStarting App Instance IPs:"
    print appinstanceip
    print "******************************************************************************************"
Beispiel #57
0
np = Plugin()

## Add a command line argument
np.add_arg("n", "name", "Amazon ELB name", required=True)
np.add_arg("i", "instance", "Amazon EC2 instance ID", required=True)

## This starts the actual plugin activation
np.activate()

## Use specified ELB name
elb_name = np['name']
instance_id = np['instance']

## Unable to connect
try:
    conn = boto.connect_elb()
except boto.exception.NoAuthHandlerFound:
    np.nagios_exit(
        UNKNOWN,
        "Unable to log into AWS. Please Check your /etc/boto.cfg file or AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variable."
    )

## Unable to get elbs
try:
    instances_health = conn.describe_instance_health(elb_name)
except:
    np.nagios_exit(
        UNKNOWN,
        "Unable to get elb list. Is network up ? Is region configured ? (Region %s)"
        % (conn.DefaultRegionName))
Beispiel #58
0
    parser = argparse.ArgumentParser(description=description)
    parser.add_argument('-s', '--stack-name', required=True,
                        help="The name of the cloudformation stack.")
    parser.add_argument('-n', '--noop',
                        help="Don't make any changes.", action="store_true",
                        default=False)
    parser.add_argument('-z', '--zone-name', default="edx.org",
                        help="The name of the zone under which to "
                             "create the dns entries.")
    parser.add_argument('-f', '--force',
                        help="Force reuse of an existing name in a zone",
                        action="store_true", default=False)
    parser.add_argument('--aws-id', default=None,
                        help="read only aws key for fetching instance information"
                             "the account you wish add entries for")
    parser.add_argument('--aws-secret', default=None,
                        help="read only aws id for fetching instance information for"
                             "the account you wish add entries for")

    args = parser.parse_args()
    # Connect to ec2 using the provided credentials on the commandline
    ec2_con = boto.connect_ec2(args.aws_id, args.aws_secret)
    elb_con = boto.connect_elb(args.aws_id, args.aws_secret)
    rds_con = boto.connect_rds(args.aws_id, args.aws_secret)

    # Connect to route53 using the user's .boto file
    r53 = boto.connect_route53()

    zone = get_or_create_hosted_zone(args.zone_name)
    update_elb_rds_dns(zone)
def test_autoscaling_group_with_elb():

    web_setup_template = {
        "AWSTemplateFormatVersion": "2010-09-09",

        "Resources": {
            "my-as-group": {
                "Type": "AWS::AutoScaling::AutoScalingGroup",
                "Properties": {
                    "AvailabilityZones": ['us-east1'],
                    "LaunchConfigurationName": {"Ref": "my-launch-config"},
                    "MinSize": "2",
                    "MaxSize": "2",
                    "LoadBalancerNames": [{"Ref": "my-elb"}]
                },
            },

            "my-launch-config": {
                "Type": "AWS::AutoScaling::LaunchConfiguration",
                "Properties": {
                    "ImageId": "ami-1234abcd",
                    "UserData": "some user data",
                }
            },

            "my-elb": {
                "Type": "AWS::ElasticLoadBalancing::LoadBalancer",
                "Properties": {
                    "AvailabilityZones": ['us-east1'],
                    "Listeners": [{
                        "LoadBalancerPort": "80",
                        "InstancePort": "80",
                        "Protocol": "HTTP"
                    }],
                    "HealthCheck": {
                        "Target": "80",
                        "HealthyThreshold": "3",
                        "UnhealthyThreshold": "5",
                        "Interval": "30",
                        "Timeout": "5",
                    },
                },
            },
        }
    }

    web_setup_template_json = json.dumps(web_setup_template)

    conn = boto.connect_cloudformation()
    conn.create_stack(
        "web_stack",
        template_body=web_setup_template_json,
    )

    autoscale_conn = boto.connect_autoscale()
    autoscale_group = autoscale_conn.get_all_groups()[0]
    autoscale_group.launch_config_name.should.equal("my-launch-config")
    autoscale_group.load_balancers[0].should.equal('my-elb')

    # Confirm the Launch config was actually created
    autoscale_conn.get_all_launch_configurations().should.have.length_of(1)

    # Confirm the ELB was actually created
    elb_conn = boto.connect_elb()
    elb_conn.get_all_load_balancers().should.have.length_of(1)

    stack = conn.describe_stacks()[0]
    resources = stack.describe_resources()
    as_group_resource = [resource for resource in resources if resource.resource_type == 'AWS::AutoScaling::AutoScalingGroup'][0]
    as_group_resource.physical_resource_id.should.equal("my-as-group")

    launch_config_resource = [resource for resource in resources if resource.resource_type == 'AWS::AutoScaling::LaunchConfiguration'][0]
    launch_config_resource.physical_resource_id.should.equal("my-launch-config")

    elb_resource = [resource for resource in resources if resource.resource_type == 'AWS::ElasticLoadBalancing::LoadBalancer'][0]
    elb_resource.physical_resource_id.should.equal("my-elb")
Beispiel #60
0
    def _show_edp(self, message, env, dep, play):
        """
        Show info about a particular EDP.
        """
        self.say("Reticulating splines...", message)
        ec2 = boto.connect_ec2(profile_name=dep)
        elb = boto.connect_elb(profile_name=dep)
        edp_filter = {
            "tag:environment": env,
            "tag:deployment": dep,
            "tag:play": play,
        }
        instances = ec2.get_all_instances(filters=edp_filter)
        elbs = elb.get_all_load_balancers()
        if not instances:
            self.say('No instances found. The input may be misspelled.', message, color='red')
            return

        output_table = [
            ["Internal DNS", "Versions", "ELBs", "AMI"],
            ["------------", "--------", "----", "---"],
        ]
        instance_len, ref_len, elb_len, ami_len = map(len, output_table[0])

        for reservation in instances:
            for instance in reservation.instances:
                if instance.state != 'running':
                    continue
                msg = "Getting info for: {}"
                logging.info(msg.format(instance.private_dns_name))
                refs = []
                ami_id = instance.image_id
                ami = self._get_ami(ami_id, message=message)
                if not ami:
                    return None
                for name, value in ami.tags.items():
                    if name.startswith('version:'):
                        key = name[8:]
                        if key == "configuration" or \
                           key == "configuration_secure" or \
                           key.endswith("_version") or \
                           key.endswith("_VERSION"):
                            refs.append(
                                "{}={}".format(key, value.split()[1]))
                        else:
                            refs.append(
                                "{}_version={}".format(key, value.split()[1]))

                elb_list = []
                for elb in elbs:
                    lb_instance_ids = [inst.id for inst in elb.instances]
                    if instance.id in lb_instance_ids:
                        elb_list.append(elb.name)

                all_data = izip_longest(
                    [instance.private_dns_name],
                    refs, elb_list, [ami_id],
                    fillvalue="",
                )
                for inst, ref, elb, ami in all_data:
                    output_table.append([inst, ref, elb, ami])
                    if inst:
                        instance_len = max(instance_len, len(inst))

                    if ref:
                        ref_len = max(ref_len, len(ref))

                    if elb:
                        elb_len = max(elb_len, len(elb))

                    if ami:
                        ami_len = max(ami_len, len(ami))

        output = []
        for line in output_table:
            output.append("{} {} {} {}".format(line[0].ljust(instance_len),
                                               line[1].ljust(ref_len),
                                               line[2].ljust(elb_len),
                                               line[3].ljust(ami_len),))

        # Only make chunks of data exceeding the limit
        chunk_size = 65
        if len(output) > chunk_size:
            data = list(self._get_chunks(output, chunk_size))
            for chunk in data:
                self.say("/code {}".format("\n".join(chunk)), message)
        else:
            self.say("/code {}".format("\n".join(output)), message)
        logging.error(output_table)