def instanceupdate(region=None): if request.method == 'POST': # Set variables passed in from the session: instance ID, and extension (hours) region = request.form['region'] form_id = request.form['instance_id'] extension_hr = int(request.form['extension']) # Convert millisecond_hr = int(3600000 * extension_hr) creds = config.get_ec2_conf() conn = connect_to_region(region, aws_access_key_id=creds['AWS_ACCESS_KEY_ID'], aws_secret_access_key=creds['AWS_SECRET_ACCESS_KEY']) # Get info about the instance; only the instance we want instance = conn.get_only_instances(filters={'instance-id': form_id}) for i in instance: "Extending runtime of instance " + i.id + " by " + str(extension_hr) + " hours." # Set new shutoff time #new_shutoff_time = datetime.now() + timedelta(hours=hours) current_milli_time = lambda: int(round(time.time() * 1000)) new_shutoff_time = current_milli_time() + millisecond_hr print "New instance shutoff time for " + i.tags['Name'] + " is " + str(datetime.fromtimestamp(new_shutoff_time/1000.0)) # If dev instance was shutdown or previously off, start it up until the specified shutdown if i.state == 'stopped' : i.start() # lt_datetime = datetime.datetime.strptime(i.launch_time, '%Y-%m-%dT%H:%M:%S.000Z') # lt_delta = datetime.datetime.utcnow() - lt_datetime # uptime = str(lt_delta) i.add_tag('Shutoff Time', new_shutoff_time) return redirect(url_for('index'))
def delete_ebs_vol(region=None,vol_id=None): creds = config.get_ec2_conf() conn = connect_to_region(region, aws_access_key_id=creds['AWS_ACCESS_KEY_ID'], aws_secret_access_key=creds['AWS_SECRET_ACCESS_KEY']) vol_id = vol_id.encode('ascii') vol_ids = conn.get_all_volumes(volume_ids=vol_id) for vol in vol_ids: vol.delete() return redirect(url_for('ebs_volumes', region=region))
def elastic_lbs(region=None): creds = config.get_ec2_conf() connelb = boto.ec2.elb.connect_to_region(region, aws_access_key_id=creds['AWS_ACCESS_KEY_ID'], aws_secret_access_key=creds['AWS_SECRET_ACCESS_KEY']) load_balancers = connelb.get_all_load_balancers() elbs = [] for lb in load_balancers: lb_info = { 'lb_name' : lb.name, 'lb_dns' : lb.dns_name, 'lb_instances' : lb.instances, 'lb_vpc' : lb.vpc_id, 'lb_sg' : lb.source_security_group } elbs.append(lb_info) return render_template('active_lb.html',elbs=elbs)
def delete_elastic_ip(region=None,ip=None): creds = config.get_ec2_conf() conn = connect_to_region(region, aws_access_key_id=creds['AWS_ACCESS_KEY_ID'], aws_secret_access_key=creds['AWS_SECRET_ACCESS_KEY']) ip = ip.encode('ascii') elis = conn.get_all_addresses(addresses=ip) for eli in elis: eli.release() return redirect(url_for('elastic_ips', region=region))
def index(): list = [] creds = config.get_ec2_conf() for region in config.region_list(): conn = connect_to_region( region, aws_access_key_id=creds['AWS_ACCESS_KEY_ID'], aws_secret_access_key=creds['AWS_SECRET_ACCESS_KEY']) zones = conn.get_all_zones() instances = conn.get_all_instances() instance_count = len(instances) ebs = conn.get_all_volumes() ebscount = len(ebs) unattached_ebs = 0 unattached_eli = 0 active_count = 0 for instance in instances: active = instances if active: active_count = active_count + 1 for vol in ebs: state = vol.attachment_state() if state == None: unattached_ebs = unattached_ebs + 1 elis = conn.get_all_addresses() eli_count = len(elis) for eli in elis: instance_id = eli.instance_id if not instance_id: unattached_eli = unattached_eli + 1 connelb = boto.ec2.elb.connect_to_region( region, aws_access_key_id=creds['AWS_ACCESS_KEY_ID'], aws_secret_access_key=creds['AWS_SECRET_ACCESS_KEY']) elb = connelb.get_all_load_balancers() elb_count = len(elb) list.append({ 'region': region, 'zones': zones, 'instance_count': instance_count, 'ebscount': ebscount, 'unattached_ebs': unattached_ebs, 'eli_count': eli_count, 'unattached_eli': unattached_eli, 'elb_count': elb_count, 'active_count': active_count }) return render_template('index.html', list=list)
def ebs_volumes(region=None): creds = config.get_ec2_conf() conn = connect_to_region(region, aws_access_key_id=creds['AWS_ACCESS_KEY_ID'], aws_secret_access_key=creds['AWS_SECRET_ACCESS_KEY']) ebs = conn.get_all_volumes() ebs_vol = [] for vol in ebs: state = vol.attachment_state() ebs_info = { 'id' : vol.id, 'size' : vol.size, 'iops' : vol.iops, 'status' : vol.status } ebs_vol.append(ebs_info) return render_template('ebs_volume.html',ebs_vol=ebs_vol,region=region)
def lowipsubnet(region=None): creds = config.get_ec2_conf() vpcconn = boto.vpc.connect_to_region(region, aws_access_key_id=creds['AWS_ACCESS_KEY_ID'], aws_secret_access_key=creds['AWS_SECRET_ACCESS_KEY']) subnets = vpcconn.get_all_subnets() lowsubnet = [] for subnet in subnets: if subnet.available_ip_address_count < 15: subnet_info = {'subnet_id': subnet.id, 'subnet_cidr': subnet.cidr_block, 'subnet_az': subnet.availability_zone, 'subnet_avail_ip': subnet.available_ip_address_count, 'subnet_tags': subnet.tags} lowsubnet.append(subnet_info) return render_template('subnet.html', lowsubnet=lowsubnet)
def elbimproper(region=None): creds = config.get_ec2_conf() connelb = boto.ec2.elb.connect_to_region(region, aws_access_key_id=creds['AWS_ACCESS_KEY_ID'], aws_secret_access_key=creds['AWS_SECRET_ACCESS_KEY']) elbs = connelb.get_all_load_balancers() badelb = [] for elb in elbs: if len(elb.instances) < 1: elb_info = {'elb_name': elb.dns_name, 'elb_attached_instances': elb.instances, 'elb_healthcheck': elb.health_check} badelb.append(elb_info) return render_template('elb.html', badelb=badelb)
def delete_ebs_vol(region=None, vol_id=None): creds = config.get_ec2_conf() conn = connect_to_region( region, aws_access_key_id=creds['AWS_ACCESS_KEY_ID'], aws_secret_access_key=creds['AWS_SECRET_ACCESS_KEY']) vol_id = vol_id.encode('ascii') vol_ids = conn.get_all_volumes(volume_ids=vol_id) for vol in vol_ids: vol.delete() return redirect(url_for('ebs_volumes', region=region))
def elastic_ips(region=None): creds = config.get_ec2_conf() conn = connect_to_region(region, aws_access_key_id=creds['AWS_ACCESS_KEY_ID'], aws_secret_access_key=creds['AWS_SECRET_ACCESS_KEY']) elis = conn.get_all_addresses() un_eli = [] for eli in elis: instance_id = eli.instance_id if not instance_id: eli_info = { 'public_ip' : eli.public_ip, 'domain' : eli.domain} un_eli.append(eli_info) return render_template('elastic_ip.html',un_eli=un_eli,region=region)
def instance_active(region=None): creds = config.get_ec2_conf() conn = connect_to_region(region, aws_access_key_id=creds['AWS_ACCESS_KEY_ID'], aws_secret_access_key=creds['AWS_SECRET_ACCESS_KEY']) rsvs = conn.get_all_instances() instances = [] for rsv in rsvs: insts = rsv.instances for inst in insts: active_info = { 'instance_id' : inst.id, 'Name' : inst.tags['Name'], 'Public_IP' : inst.ip_address, 'Private_IP' : inst.private_ip_address, 'Type':inst.get_attribute('instanceType')['instanceType'], 'Zone' : inst.placement, 'Subnet' : inst.subnet_id, 'VPC' : inst.vpc_id } instances.append(active_info) return render_template('active_instances.html',instances=instances)
def instance_events(region=None): creds = config.get_ec2_conf() conn = connect_to_region(region, aws_access_key_id=creds['AWS_ACCESS_KEY_ID'], aws_secret_access_key=creds['AWS_SECRET_ACCESS_KEY']) instances = conn.get_all_instance_status() instance_event_list = [] for instance in instances: event = instance.events if event: event_info = { 'instance_id' : instance.id, 'event' : instance.events[0].code, 'description' : instance.events[0].description, 'event_before' : instance.events[0].not_before, 'event_after': instance.events[0].not_after } instance_event_list.append(event_info) return render_template('instance_events.html', instance_event_list=instance_event_list)
def delete_elastic_ip(region=None, ip=None): creds = config.get_ec2_conf() conn = connect_to_region( region, aws_access_key_id=creds['AWS_ACCESS_KEY_ID'], aws_secret_access_key=creds['AWS_SECRET_ACCESS_KEY']) ip = ip.encode('ascii') elis = conn.get_all_addresses(addresses=ip) for eli in elis: eli.release() return redirect(url_for('elastic_ips', region=region))
def elastic_ips(region=None): creds = config.get_ec2_conf() conn = connect_to_region( region, aws_access_key_id=creds['AWS_ACCESS_KEY_ID'], aws_secret_access_key=creds['AWS_SECRET_ACCESS_KEY']) elis = conn.get_all_addresses() un_eli = [] for eli in elis: instance_id = eli.instance_id if not instance_id: eli_info = {'public_ip': eli.public_ip, 'domain': eli.domain} un_eli.append(eli_info) return render_template('elastic_ip.html', un_eli=un_eli, region=region)
def __init__(self): # TODO load ec2 credentials from config file creds = config.get_ec2_conf() self.conn = None self.access_key = creds['AWS_ACCESS_KEY_ID'] self.secret_key = creds['AWS_SECRET_ACCESS_KEY'] self.region = 'us-east-1' self.account_id = creds['ACCOUNT_ID'] # create a connection to aws to pass around self.conn = connect_to_region(self.region, aws_access_key_id=self.access_key, aws_secret_access_key=self.secret_key) self.SecurityGroupRule = config.SecurityGroupRule
def elastic_lbs(region=None): creds = config.get_ec2_conf() connelb = boto.ec2.elb.connect_to_region( region, aws_access_key_id=creds['AWS_ACCESS_KEY_ID'], aws_secret_access_key=creds['AWS_SECRET_ACCESS_KEY']) load_balancers = connelb.get_all_load_balancers() elbs = [] for lb in load_balancers: lb_info = { 'lb_name': lb.name, 'lb_dns': lb.dns_name, 'lb_instances': lb.instances, 'lb_vpc': lb.vpc_id, 'lb_sg': lb.source_security_group } elbs.append(lb_info) return render_template('active_lb.html', elbs=elbs)
def ebs_volumes(region=None): creds = config.get_ec2_conf() conn = connect_to_region( region, aws_access_key_id=creds['AWS_ACCESS_KEY_ID'], aws_secret_access_key=creds['AWS_SECRET_ACCESS_KEY']) ebs = conn.get_all_volumes() ebs_vol = [] for vol in ebs: state = vol.attachment_state() if state == None: ebs_info = { 'id': vol.id, 'size': vol.size, 'iops': vol.iops, 'status': vol.status } ebs_vol.append(ebs_info) return render_template('ebs_volume.html', ebs_vol=ebs_vol, region=region)
def index(): list = [] creds = config.get_ec2_conf() for region in config.region_list(): conn = connect_to_region(region, aws_access_key_id=creds['AWS_ACCESS_KEY_ID'], aws_secret_access_key=creds['AWS_SECRET_ACCESS_KEY']) zones = conn.get_all_zones() instances = conn.get_all_instance_status(max_results=2000) instance_count = len(instances) ebs = conn.get_all_volumes() ebscount = len(ebs) unattached_ebs = 0 unattached_eli = 0 event_count = 0 for instance in instances: events = instance.events if events: event_count = event_count + 1 for vol in ebs: state = vol.attachment_state() if state == None: unattached_ebs = unattached_ebs + 1 elis = conn.get_all_addresses() eli_count = len(elis) for eli in elis: instance_id = eli.instance_id if not instance_id: unattached_eli = unattached_eli + 1 connelb = boto.ec2.elb.connect_to_region(region, aws_access_key_id=creds['AWS_ACCESS_KEY_ID'], aws_secret_access_key=creds['AWS_SECRET_ACCESS_KEY']) elb = connelb.get_all_load_balancers() elb_count = len(elb) list.append({ 'region' : region, 'zones': zones, 'instance_count' : instance_count, 'ebscount' : ebscount, 'unattached_ebs' : unattached_ebs, 'eli_count' : eli_count, 'unattached_eli' : unattached_eli, 'elb_count' : elb_count, 'event_count' : event_count}) return render_template('index.html',list=list)
def instance_events(region=None): creds = config.get_ec2_conf() conn = connect_to_region( region, aws_access_key_id=creds['AWS_ACCESS_KEY_ID'], aws_secret_access_key=creds['AWS_SECRET_ACCESS_KEY']) instances = conn.get_all_instance_status() instance_event_list = [] for instance in instances: event = instance.events if event: event_info = { 'instance_id': instance.id, 'event': instance.events[0].code, 'description': instance.events[0].description, 'event_before': instance.events[0].not_before, 'event_after': instance.events[0].not_after } instance_event_list.append(event_info) return render_template('instance_events.html', instance_event_list=instance_event_list)
def instance_active(region=None): creds = config.get_ec2_conf() conn = connect_to_region( region, aws_access_key_id=creds['AWS_ACCESS_KEY_ID'], aws_secret_access_key=creds['AWS_SECRET_ACCESS_KEY']) rsvs = conn.get_all_instances() instances = [] for rsv in rsvs: insts = rsv.instances for inst in insts: active_info = { 'instance_id': inst.id, 'Name': inst.tags['Name'], 'Public_IP': inst.ip_address, 'Private_IP': inst.private_ip_address, 'Type': inst.get_attribute('instanceType')['instanceType'], 'Zone': inst.placement, 'Subnet': inst.subnet_id, 'VPC': inst.vpc_id } instances.append(active_info) return render_template('active_instances.html', instances=instances)
def index(): if (len(session.keys()) > 0): list = [] creds = config.get_ec2_conf() for region in config.region_list(): ec2 = boto3.client('ec2', region) resp = ec2.describe_availability_zones() # zones=[d['ZoneName'] for d in resp['AvailabilityZones'] if d['ZoneName']] zones = [d for d in resp['AvailabilityZones'] if d['ZoneName']] ec2 = boto3.resource('ec2', region_name=region) instances = ec2.instances.filter() instances = [i.id for i in instances] ebs = [ volume for instance in ec2.instances.all() for volume in instance.volumes.all() ] ebscount = len(ebs) instance_count = len(instances) ## instance_count = len(instances) ## ebs = conn.get_all_volumes() ## ebscount = len(ebs) unattached_ebs = 0 unattached_eli = 0 ## event_count = 0 ## ## for instance in instances: ## events = instance.events ## if events: ## event_count = event_count + 1 ## ## for vol in ebs: ## state = vol.attachment_state() ## if state == None: ## unattached_ebs = unattached_ebs + 1 ## ## elis = conn.get_all_addresses() ## eli_count = len(elis) ## ## ## for eli in elis: ## instance_id = eli.instance_id ## if not instance_id: ## unattached_eli = unattached_eli + 1 ## ## connelb = boto.ec2.elb.connect_to_region(region, aws_access_key_id=creds['AWS_ACCESS_KEY_ID'], aws_secret_access_key=creds['AWS_SECRET_ACCESS_KEY']) ## elb = connelb.get_all_load_balancers() ## elb_count = len(elb) ## list.append({ 'region' : region, 'zones': zones, 'instance_count' : instance_count, 'ebscount' : ebscount, 'unattached_ebs' : unattached_ebs, 'eli_count' : eli_count, 'unattached_eli' : unattached_eli, 'elb_count' : elb_count, 'event_count' : event_count}) list.append({ 'region': region, 'zones': zones, 'instance_count': instance_count, 'unattached_ebs': unattached_ebs, 'unattached_eli': unattached_eli }) ## return render_template('index.html', list=list) else: return redirect(url_for('login'))
def index(): list = [] creds = config.get_ec2_conf() key = creds['AWS_ACCESS_KEY_ID'] secret = creds['AWS_SECRET_ACCESS_KEY'] for region in config.region_list(): conn = connect_to_region(region, aws_access_key_id=key, aws_secret_access_key=secret) vpcconn = boto.vpc.connect_to_region(region, aws_access_key_id=key, aws_secret_access_key=secret) zones = conn.get_all_zones() instances = conn.get_all_instance_status(max_results=2000) instance_count = len(instances) ebs = conn.get_all_volumes() ebscount = len(ebs) subnets = vpcconn.get_all_subnets() unattached_ebs = 0 unattached_eli = 0 event_count = 0 improperelb = 0 subnet_counter = 0 ip_low_subnet = 0 subnet_counter = len(subnets) for subnet in subnets: if subnet.available_ip_address_count < 15: ip_low_subnet = ip_low_subnet + 1 for instance in instances: events = instance.events if events and "Completed" not in instance.events[0].description: event_count = event_count + 1 for vol in ebs: state = vol.attachment_state() if state == None: try: vol.tags['Status'] if vol.tags['Status'] == "InUse": continue except KeyError: unattached_ebs = unattached_ebs + 1 elis = conn.get_all_addresses() eli_count = len(elis) for eli in elis: instance_id = eli.instance_id if not instance_id: unattached_eli = unattached_eli + 1 connelb = boto.ec2.elb.connect_to_region(region, aws_access_key_id=creds['AWS_ACCESS_KEY_ID'], aws_secret_access_key=creds['AWS_SECRET_ACCESS_KEY']) elbs = connelb.get_all_load_balancers() elb_count = len(elbs) for elb in elbs: if len(elb.instances) < 1: improperelb = improperelb + 1 list.append({ 'region' : region, 'zones': zones, 'instance_count' : instance_count, 'ebscount' : ebscount, 'unattached_ebs' : unattached_ebs, 'eli_count' : eli_count, 'unattached_eli' : unattached_eli, 'elb_count' : elb_count, 'event_count' : event_count, 'improper_elb': improperelb, 'subnet_counter': subnet_counter, 'ip_low_subnet': ip_low_subnet}) return render_template('index.html', list=list)
def instance_events(region=None): creds = config.get_ec2_conf() shutdown_type = config.filter_instance_shutdown() conn = connect_to_region(region, aws_access_key_id=creds['AWS_ACCESS_KEY_ID'], aws_secret_access_key=creds['AWS_SECRET_ACCESS_KEY']) instances = conn.get_only_instances() instance_list = [] # AWS holds a ton of relevant info that we might want to use # For the purposes of this project, we want: # instance_id (instance id), instance_type (instance type, i.e. m3.medium), instance_state (i.e. running/stopped) # instance_launch(time(UTC) when instance was started, instance_name ('Name' tag), instance_region (AWS region) # instance_poc ('POC' Point of Contact tag), instance_team ('Team' tag for group using the instance) # instance_use ('Usefor' tag in order to filter shutdown elements on template; only going to be used for 'Dev') # instance_shutdown ('Shutoff Time' tag (in ms since epoch) for when to shutdown the instance) # instance_start_readable (Parse the start time and make it a bit more reasonable) for instance in instances: instance_info = { 'instance_id' : instance.id, 'instance_type' : instance.instance_type, 'state' : instance.state, 'instance_launch' : instance.launch_time, 'instance_name' : instance.tags['Name'], 'instance_region' : region, 'instance_ip' : instance.ip_address} # If the instance has a Point of Contact tag, add it now if 'POC' in instance.tags : instance_info.update({ 'instance_poc' : instance.tags['POC'] }) # If the instance has a team tag, add it now if 'Team' in instance.tags : instance_info.update({ 'instance_team' : instance.tags['Team'] }) # If the instance has a Usefor tag (i.e. dev, production, etc.) add it now if 'Use' in instance.tags : instance_info.update({ 'instance_use' : instance.tags['Use']}) else : instance_info.update({ 'instance_use' : 'None' }) # Set readable time for when the instance was last started instance_info.update({ 'instance_start_readable' : datetime.strptime(instance.launch_time, '%Y-%m-%dT%H:%M:%S.000Z')}) # If there is no public IP, try finding a private IP instead if instance.ip_address == 'None' : print "No public IP for instance ",instance.id # If the instance has a shutdown time flag (time after which to shut the instance down), add it now if 'Shutoff Time' in instance.tags : try: aws_shutdown = int(instance.tags['Shutoff Time']) shutoff_readable = datetime.fromtimestamp(aws_shutdown/1000.0) except ValueError: print "Shutdown Time is incorrect in Amazon. Setting shutdown time of instance", instance.id, " to 0." shutoff_readable = "0" instance.remove_tag('Shutoff Time') instance.add_tag('Shutoff Time', "0") except: print "Unexpected error:", sys.exec_info()[0] raise instance_info.update({ 'instance_shutdown' : shutoff_readable}) if 'Use' in instance.tags and instance.tags['Use'] == shutdown_type['SHUTDOWN_TAG_TYPE'] : # Shut down Dev instance if after its shutdown time current_milli_time = lambda: int(round(time.time() * 1000)) now = current_milli_time() # Get time from now to shutoff time # If positive, shutoff is still in the future; if negative, shutoff is in the past shutoff_time_from_now = int(instance.tags['Shutoff Time']) - int(now) if shutoff_time_from_now < 0 and instance.state == 'running': print "Evaluating " + instance.id + " for overstepping its time constraint" print "Shutoff time is " + str(instance.tags['Shutoff Time']) + " and current time is " + str(now) print "Going to shut " + instance.tags['Name'] + " down now." print "Scheduled shutdown was " + instance.tags['Shutoff Time'] + " and it is now " + str(now) #conn.stop_instances(instance_ids={'instance-id' : instance.id }) instance.stop() print "Instance has been stopped" # Remove shutdown time tag instance.remove_tag('Shutoff Time') instance_list.append(instance_info) return render_template('instance_events.html', instance_list=instance_list, shutdown_type_filter=shutdown_type['SHUTDOWN_TAG_TYPE'])