Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
brian smith committed Jun 12, 2015
1 parent 5c22a8e commit ca763c4
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
50 changes: 50 additions & 0 deletions aws_ec2_add_HQ_ip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/python
# https://github.com/akabdog 2015-06-12
#
# This script will check the WAN ip from wherever you run it, then add it to all "HQ and Remote" security groups for prod and dev, if it doesn't already exist.

import boto.ec2
import urllib2
import re

DRY_RUN = False
URL = 'http://ip.the408.com'
DEV_SG_STRING = "Some_string_in_SG_name"
PROD_SG_STRING = "Some_other_string_in_SG_name"

def dry_run_notify (DRY_RUN):
if DRY_RUN == True: print "DRY RUN is true, nothing will be modified"
if DRY_RUN == False: print "DRY RUN is false, things are happening for real"
print "#########################################################"

def get_wan_ip ():
wan_ip = urllib2.urlopen(URL)
wan_ip = wan_ip.read()
wan_ip = wan_ip.rstrip()
wan_ip = wan_ip + '/32'
print "I think your IP is " + wan_ip + " this is what we will be adding."
return wan_ip


def add_ip_dev (profile, wan_ip, sg_string):
conn = boto.ec2.connect_to_region("us-west-1", profile_name = profile)
groups = conn.get_all_security_groups()
for group in groups:
if re.search(sg_string, group.name):
print '########## Using ' + profile + ' to add ' + wan_ip + ' to ' + group.name + ' ##########'
try:
if DRY_RUN == False: group.authorize('tcp', 0, 65535, wan_ip)
print "ADDED\n"
except boto.exception.BotoServerError as e:
if e.error_code == "InvalidPermission.Duplicate":
print "Entry already exists\n"
else:
print e.error_code

#
# MAIN
#
dry_run_notify ( DRY_RUN )
wan_ip = get_wan_ip()
add_ip_dev('dev', wan_ip, DEV_SG_STRING)
add_ip_dev('prod', wan_ip, PROD_SG_STRING)
76 changes: 76 additions & 0 deletions aws_ec2_restore_from_CPM_snapshot_staging_all.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/python
# https://github.com/akabdog 2015-05-13

import boto.ec2
import boto.opsworks
import time
import re

DRY_RUN = True
PROFILE = "prod"

print "#########################################################"
print "REMINDER, disable auto-healing on the Opsworks layer to allow instance shutdowns."
print "#########################################################"

def dry_run_notify (DRY_RUN):
if DRY_RUN == True: print "DRY RUN is true, nothing will be modified"
if DRY_RUN == False: print "DRY RUN is false, things are happening for real"
print "#########################################################"

def get_instances ():
CONN = boto.ec2.connect_to_region("us-west-1", profile_name = PROFILE)
ALL_SNAPS = CONN.get_all_snapshots()
INSTANCES = []
for SNAP in ALL_SNAPS:
SNAP_NAME = SNAP.description
if re.search("CPM_job_policy_name_here", SNAP_NAME):
INSTANCE_ID = SNAP_NAME.split(" ")[6]
if INSTANCE_ID not in INSTANCES:
INSTANCES.append(INSTANCE_ID)
print "adding this instance to the list : " + INSTANCE_ID
print "WARNING, this script will shutdown ALL instance you are reverting : "
print INSTANCES
return INSTANCES


#this function take a list of instances and reverts each one to it's most recent snapshot
def revert_all (INSTANCES):
CONN = boto.ec2.connect_to_region("us-west-1", profile_name = PROFILE)
for ID in INSTANCES:
print "#########################################################"
INSTANCE = CONN.get_only_instances(instance_ids=ID)
print ID
if 'Name' in INSTANCE[0].tags: print "instance name : " + INSTANCE[0].tags['Name']
PLACEMENT = INSTANCE[0].placement
print "Shutting down instance and taking a nap..."
if DRY_RUN == False: INSTANCE[0].stop(force=True)
if DRY_RUN == False: time.sleep(50)
VOLUMES = CONN.get_all_volumes(filters={'attachment.instance-id': ID })
for V in VOLUMES:
SNAPSHOTS = V.snapshots()
A = V.attach_data
MOUNTPOINT = A.device
if DRY_RUN == False: V.detach()
SNAPSHOT = SNAPSHOTS[0]
if DRY_RUN == False: VOLUME = SNAPSHOT.create_volume(PLACEMENT)
if DRY_RUN == False: CONN.attach_volume (VOLUME.id, INSTANCE_ID, MOUNTPOINT)
print "original volume : "+ V.id
print "original mountpoint : "+ MOUNTPOINT
print "snapshot : "+ SNAPSHOT.id
if DRY_RUN == False: print "new volume : "+ VOLUME.id
print "instance id : "+ ID
print "zone : "+ PLACEMENT
print "Starting instance..."
if DRY_RUN == False: INSTANCE[0].start()
print "#########################################################"
print "if this script completed without errors, your instance is ready to be started in Opsworks, do that manually"
print "#########################################################"

#
# MAIN
#

dry_run_notify ( DRY_RUN )
revert_all ( get_instances() )

0 comments on commit ca763c4

Please sign in to comment.