Beispiel #1
0
def update_all_ssl():
    prepare_app_list()
    access_key_value,secret_key_value=read_properties()
    opsworks = boto.connect_opsworks(access_key_value,secret_key_value)
    for app_id in app_id_list:
        opsworks.update_apps(app_id,SslConfiguration=ssl_dict)
        print "Updated app ssl"+app_id
Beispiel #2
0
def execute_recipes(stack_name,recipes):
    access_key_value,secret_key_value=read_properties()
    opsworks = boto.connect_opsworks(access_key_value,secret_key_value)
    stack_list = []
    stack_id_list = []
    if ',' in stack_name:
        stack_list = stack_name.split(',')
    elif stack_name == 'all':
        for stack_name,stack_id in stack_dict.iteritems():
            print "Stack ID"+stack_name
            execute_recipes = {}
            execute_recipes['Name']='execute_recipes'
            execute_recipes['Args']={}
            recipes_list = []
            recipes_list.append(recipes)
            execute_recipes['Args']['recipes']=recipes_list
            opsworks.create_deployment(stack_id,execute_recipes)
        return 0
    else:
        stack_list.append(stack_name)
    #Connect to OpsWorks API
    recipes_list = []
    recipes_list.append(recipes)
    execute_recipes = {}
    execute_recipes['Name']='execute_recipes'
    execute_recipes['Args']={}
    execute_recipes['Args']['recipes']=recipes_list
    stacks_to_update = dict((k,stack_dict[k]) for k in stack_list if k in stack_dict)
    print stacks_to_update
    for stack_name,stack_id in stacks_to_update.items():
        print "Executing Recipe in Stack :" + stack_name
        deployment_id = opsworks.create_deployment(stack_id,execute_recipes)
        write_deployment_id = open('temp_deplist','w')
        write_deployment_id.write('EXC'+str(deployment_id.values().pop())+'\n')
        write_deployment_id.close()
Beispiel #3
0
def update_stacks():
    access_key_value,secret_key_value=read_properties()
    opsworks = boto.connect_opsworks(access_key_value,secret_key_value)
    print "Connected to AWS OpsWorks \n"
    global our_stacks
    our_stacks = opsworks.describe_stacks()
    stack_len = len(our_stacks['Stacks'])
    print "Number of Stacks to be udpated: "+str(stack_len)
    change_param=int(raw_input("Number of Parameters to Change? \n"))
    list_original=[]
    list_update=[]
    for k in range(0,change_param):
        print str(k)+"of"+str(change_param)
        original_value = str(raw_input('Enter the original value to replace: \n')).strip()
        update_value = str(raw_input('Enter the updated value: \n')).strip()
        list_original.append(original_value)
        list_update.append(update_value)
    for i in range(0,stack_len):
        if 'CustomJson' in our_stacks['Stacks'][i]:
            print 'Updating Stack:' + str(our_stacks['Stacks'][i]['Name'])
            my_customjson = our_stacks['Stacks'][i]['CustomJson']
            update_json = my_customjson
            for k in range(0,change_param):
                update_json = update_json.replace(list_original[k],list_update[k])
            our_stacks['Stacks'][i]['CustomJson'] = update_json
            opsworks.update_stack(our_stacks['Stacks'][i]['StackId'],custom_json=update_json)
        else :
            print 'Skipping Stack:' + str(our_stacks['Stacks'][i]['Name']) +' as there is no CustomJSON defined \n'
    print 'Exiting'
Beispiel #4
0
def add_parameter(stack=None):
    access_key_value,secret_key_value=read_properties()
    opsworks = boto.connect_opsworks(access_key_value,secret_key_value)
    print "Connected to AWS OpsWorks \n"
    our_stacks = opsworks.describe_stacks()
    key = str(raw_input("Enter key to add JSON:")).strip()
    value = str(raw_input("Enter value:")).strip()
    print "Key is "+key
    print "value is"+value
    if type(stack)!=None and len(stack)> 0:
        for i in range(0,len(our_stacks['Stacks'])):
            if (our_stacks['Stacks'][i]['Name'] == stack.strip()):
                print 'Updating Stack:' + str(our_stacks['Stacks'][i]['Name'])
                updated_stack = our_stacks['Stacks'][i]['CustomJson']
                json_dict=simplejson.loads(our_stacks['Stacks'][i]['CustomJson'])
                json_dict[key]=value
                fixed_json_dict = simplejson.dumps(json_dict,sort_keys=False,indent=3*' ')
                opsworks.update_stack(our_stacks['Stacks'][i]['StackId'],custom_json=fixed_json_dict)
    else:
        for i in range(0,len(our_stacks['Stacks'])):
            if 'CustomJson' in our_stacks['Stacks'][i]:
                print "Total stacks to be updated:"+str(len(our_stacks['Stacks']))
                print "Updating Stack: "+our_stacks['Stacks'][i]['Name']
                updated_stack = our_stacks['Stacks'][i]['CustomJson']
                json_dict=simplejson.loads(our_stacks['Stacks'][i]['CustomJson'])
                json_dict[key]=value
                fixed_json_dict = simplejson.dumps(json_dict,sort_keys=False,indent=3*' ').replace('\\"','"').replace('"{','{').replace('}"','}')
                # print "Text:"+str(fixed_json_dict['nodejs'])
                # print fixed_json_dict
                opsworks.update_stack(our_stacks['Stacks'][i]['StackId'],custom_json=fixed_json_dict)
            else:
                print "Couldn't find custom json field"
    return 0
Beispiel #5
0
def update_custom_cookbooks(stack_name=None):
    access_key_value,secret_key_value=read_properties()
    opsworks = boto.connect_opsworks(access_key_value,secret_key_value)
    stack_list = []
    stack_id_list = []
    if ',' in stack_name:
        stack_list = stack_name.split(',')
    elif stack_name == 'all':
        for stack_name,stack_id in stack_dict.items():
            print "Running Update on"+stack_name
            update_cookbooks = {}
            update_cookbooks['Name']='update_custom_cookbooks'
            opsworks.create_deployment(stack_id,update_cookbooks)
        return 0
    else:
        stack_list.append(stack_name)
    #Connect to OpsWorks API
    update_cookbooks = {}
    update_cookbooks['Name']='update_custom_cookbooks'
    stacks_to_update = dict((k,stack_dict[k]) for k in stack_list if k in stack_dict)
    print stacks_to_update
    for stack_name,stack_id in stacks_to_update.items():
        deployment_id = opsworks.create_deployment(stack_id,update_cookbooks)
        print "Triggered Update on: "+stack_name
        write_deployment_id = open('temp_deplist','w')
        write_deployment_id.write('UPG'+str(deployment_id.values().pop())+'\n')
        write_deployment_id.close()
Beispiel #6
0
def prepare_app_list():
    access_key_value,secret_key_value=read_properties()
    opsworks = boto.connect_opsworks(access_key_value,secret_key_value)
    prepare_stack_dict()
    global app_id_list
    for stack_id in stack_dict.itervalues():
        get_dict = opsworks.describe_apps(stack_id)
        length = len(get_dict['Apps'])
        for i in range(0,length):
            get_app_id = get_dict['Apps'][i]['AppId']
            app_id_list.append(get_app_id)
Beispiel #7
0
def prepare_stack_dict():
    access_key_value,secret_key_value=read_properties()
    #Connect to OpsWorks API
    opsworks = boto.connect_opsworks(access_key_value,secret_key_value)
    our_stacks = opsworks.describe_stacks()
    stack_len = len(our_stacks['Stacks'])
    global stack_dict
    for i in range(0,stack_len):
        stack_name = our_stacks['Stacks'][i]['Name']
        stack_id = our_stacks['Stacks'][i]['StackId']
        stack_dict[stack_name]=stack_id
    print "Initiating"
    return 0
Beispiel #8
0
def check_deployment_status():
    access_key_value,secret_key_value=read_properties()
    opsworks = boto.connect_opsworks(access_key_value,secret_key_value)
    check_status = open('temp_deplist','r')
    for line in check_status.read().splitlines():
        print line
        dep_id = []
        dep_id.append(line[3:])
        print str(dep_id)
        if line[0:3] == 'UPG':
            response = opsworks.describe_deployments(deployment_ids=dep_id)
            print "Custom Cookbook Update Status is \n" + response['Deployments'][0]['Status']
            print "Stack :" + response['Deployments'][0]['StackId']
        elif line[0:3] == 'EXC':
            print "Recipe Execution Status is \n"
            print opsworks.describe_deployments(deployment_ids=dep_id)
Beispiel #9
0
def find_in_stack(search_string):
    access_key_value,secret_key_value=read_properties()
    access_key_value=access_key_value.strip()
    secret_key_value=secret_key_value.strip()
    opsworks = boto.connect_opsworks(access_key_value,secret_key_value)
    print "Connected to AWS OpsWorks"
    result_flag = 0
    our_stacks = opsworks.describe_stacks()
    stack_len = len(our_stacks['Stacks'])
    for i in range(0,stack_len):
        if 'CustomJson' in our_stacks['Stacks'][i]:
            my_customjson = our_stacks['Stacks'][i]['CustomJson']
            update_json = my_customjson
            test = update_json.find(search_string)
            if test!=-1:
                result_flag =1
                print "Found in "+str(our_stacks['Stacks'][i]['Name'])
    if result_flag == 0:
        print "No results found."
    print 'Exiting'
Beispiel #10
0
 def __init__(self):
     self.opsworks = boto.connect_opsworks()
     self.changes = []
Beispiel #11
0
from keyring import get_password
import lib.LoadBotoConfig as BotoConfig
import lib.AWSConn as AWSConn
from boto import connect_opsworks
from sys import exit

envs = BotoConfig.envs

for each in envs:
  env = each.split(':')[0]
  region = each.split(':')[1:]
  
  id = BotoConfig.config.get(env, 'aws_access_key_id')
  key = get_password(BotoConfig.config.get(env, 'keyring'), id)
  account = BotoConfig.config.get(env, 'accountid')

  conn = connect_opsworks(aws_access_key_id=id, aws_secret_access_key=key)
  profiles = conn.describe_user_profiles().get('UserProfiles')
  
  for profile in profiles:
    name = profile.get('SshUsername')    
    selfman = profile.get('AllowSelfManagement')
    sshkey = profile.get('SshPublicKey')
    if sshkey:
      sshkey = 'True'
    else:
      sshkey = 'False'
    print("  - {4} {5}: User: {0}, Self-Manage: {1}, SSH Key: {2}".format(name, selfman, sshkey, account, env, region))
  
  print("")
def OpsWorksConn(account, id):
    key = get_password(account, id)
    conn = connect_opsworks(id, key)
    return (conn)
Beispiel #13
0
def update_layer(stack_name,recipe,update_type=None):
    print "DEBUG: Inside Update method"
    access_key_value,secret_key_value=read_properties()
    #Connect to OpsWorks API
    opsworks = boto.connect_opsworks(access_key_value,secret_key_value)
    get_layer_details = opsworks.describe_layers(stack_id=stack_dict[stack_name])
    layer_length = len(get_layer_details['Layers'])
    print "Found "+str(layer_length)+" layers"
    for i in range(0,layer_length):
        layer_id = get_layer_details['Layers'][i]['LayerId']
        custom_recipes = get_layer_details['Layers'][i]['CustomRecipes']

        #Extract Deploy and Setup Recipes seperately and save them to respective lists
        deploy_recipe_list=get_layer_details['Layers'][i]['CustomRecipes']['Deploy']
        setup_recipe_list=get_layer_details['Layers'][i]['CustomRecipes']['Setup']
        undeploy_recipe_list=get_layer_details['Layers'][i]['CustomRecipes']['Undeploy']
        shutdown_recipe_list=get_layer_details['Layers'][i]['CustomRecipes']['Shutdown']
        configure_recipe_list=get_layer_details['Layers'][i]['CustomRecipes']['Configure']

        #update our dictionary for Deploy and Setup lists


        if update_type == None:
            '''
            Dictionary is mapped to the copy after being assigned, so we don't need to update the copy explicitly.
            '''
            deploy_recipe_list.append(recipe)
            setup_recipe_list.append(recipe)
            undeploy_recipe_list.append(recipe)
            shutdown_recipe_list.append(recipe)
            configure_recipe_list.append(recipe)

            get_layer_details['Layers'][i]['CustomRecipes']['Deploy']=deploy_recipe_list
            get_layer_details['Layers'][i]['CustomRecipes']['Setup']=setup_recipe_list
            get_layer_details['Layers'][i]['CustomRecipes']['Configure']=configure_recipe_list
            get_layer_details['Layers'][i]['CustomRecipes']['Undeploy']=undeploy_recipe_list
            get_layer_details['Layers'][i]['CustomRecipes']['Shutdown']=shutdown_recipe_list
            print "Updating All"
        elif update_type == 'Setup':
            setup_recipe_list.append(recipe)
            get_layer_details['Layers'][i]['CustomRecipes']['Setup']=setup_recipe_list
        elif update_type == 'Deploy':
            deploy_recipe_list.append(recipe)
            get_layer_details['Layers'][i]['CustomRecipes']['Deploy']=deploy_recipe_list
        elif update_type == 'Configure':
            configure_recipe_list.append(recipe)
            get_layer_details['Layers'][i]['CustomRecipes']['Configure']=configure_recipe_list
        elif update_type == 'Undeploy':
            undeploy_recipe_list.append(recipe)
            get_layer_details['Layers'][i]['CustomRecipes']['Undeploy']=undeploy_recipe_list
        elif update_type == 'Shutdown':
            shutdown_recipe_list.append(recipe)
            get_layer_details['Layers'][i]['CustomRecipes']['Shutdown']=shutdown_recipe_list
        else:
            print "Invalid Recipe List found, terminating \n"
            sys.exit(0)
        try:
            opsworks.update_layer(layer_id=layer_id,custom_recipes=custom_recipes)
            print "Updated!"
        except Exception,err:
            print "Caught an exception:"
            traceback.print_exc(file=sys.stdout)
Beispiel #14
0
def update_all_layers(recipe,update_type=None):
    access_key_value,secret_key_value=read_properties()

    #Connect to OpsWorks API
    opsworks = boto.connect_opsworks(access_key_value,secret_key_value)
    our_stacks = opsworks.describe_stacks()
    stack_len = len(our_stacks['Stacks'])
    print "WARNING: I AM UPDATING LAYERS IN EACH AND EVERY STACK"
    print "WARNING: BEWARE OF WHAT YOU ARE GOING TO DO"
    if raw_input("Type 'yes' to Confirm :")=='yes':
        for stack_name,stack_id in stack_dict.items():
            get_layer_details = opsworks.describe_layers(stack_id=stack_id)
            layer_length=len(get_layer_details['Layers'])
            print "Found "+str(layer_length)+" layers in the stack "+stack_name
            for i in range(0,layer_length):
                layer_id = get_layer_details['Layers'][i]['LayerId']
                custom_recipes = get_layer_details['Layers'][i]['CustomRecipes']

                #Extract Deploy and Setup Recipes seperately and save them to respective lists
                deploy_recipe_list=get_layer_details['Layers'][i]['CustomRecipes']['Deploy']
                setup_recipe_list=get_layer_details['Layers'][i]['CustomRecipes']['Setup']
                undeploy_recipe_list=get_layer_details['Layers'][i]['CustomRecipes']['Undeploy']
                shutdown_recipe_list=get_layer_details['Layers'][i]['CustomRecipes']['Shutdown']
                configure_recipe_list=get_layer_details['Layers'][i]['CustomRecipes']['Configure']

                #update our dictionary for Deploy and Setup lists
                if update_type == None:
                    '''
                    Dictionary is mapped to the copy after being assigned, so we don't need to update the copy explicitly.
                    '''
                    deploy_recipe_list.append(recipe)
                    setup_recipe_list.append(recipe)
                    undeploy_recipe_list.append(recipe)
                    shutdown_recipe_list.append(recipe)
                    configure_recipe_list.append(recipe)

                    get_layer_details['Layers'][i]['CustomRecipes']['Deploy']=deploy_recipe_list
                    get_layer_details['Layers'][i]['CustomRecipes']['Setup']=setup_recipe_list
                    get_layer_details['Layers'][i]['CustomRecipes']['Configure']=configure_recipe_list
                    get_layer_details['Layers'][i]['CustomRecipes']['Undeploy']=undeploy_recipe_list
                    get_layer_details['Layers'][i]['CustomRecipes']['Shutdown']=shutdown_recipe_list
                    print "Updating All"
                elif update_type == 'Setup':
                    setup_recipe_list.append(recipe)
                    get_layer_details['Layers'][i]['CustomRecipes']['Setup']=setup_recipe_list
                    opsworks.update_layer(layer_id=layer_id,custom_recipes=custom_recipes)
                elif update_type == 'Deploy':
                    deploy_recipe_list.append(recipe)
                    get_layer_details['Layers'][i]['CustomRecipes']['Deploy']=deploy_recipe_list
                elif update_type == 'Configure':
                    configure_recipe_list.append(recipe)
                    get_layer_details['Layers'][i]['CustomRecipes']['Configure']=configure_recipe_list
                elif update_type == 'Undeploy':
                    undeploy_recipe_list.append(recipe)
                    get_layer_details['Layers'][i]['CustomRecipes']['Undeploy']=undeploy_recipe_list
                elif update_type == 'Shutdown':
                    shutdown_recipe_list.append(recipe)
                    get_layer_details['Layers'][i]['CustomRecipes']['Shutdown']=shutdown_recipe_list
                else:
                    print "Invalid Recipe List found, terminating \n"
                    sys.exit(0)
                try:
                    opsworks.update_layer(layer_id=layer_id,custom_recipes=custom_recipes)
                    print "Updated: "+stack_name
                except Exception,err:
                    print "Caught an exception:"
                    traceback.print_exc(file=sys.stdout)
        else:
            print "Aborting"
            sys.exit(0)