def get(self, region, stack_name): mystack = Stack(stack_name, region) if not mystack.store_current_action( 'runsql', stack_locking_enabled(), True, session['saml']['subject'] if 'saml' in session else False): return False try: outcome = mystack.run_sql() except Exception as e: log.exception('Error occurred running SQL') mystack.log_msg(ERROR, f'Error occurred running SQL: {e}', write_to_changelog=True) mystack.clear_current_action() return False mystack.clear_current_action() return outcome
def post(self): content = request.get_json()[0] for param in content: if param['ParameterKey'] == 'TemplateName': template_name = param['ParameterValue'] elif param['ParameterKey'] == 'StackName': stack_name = param['ParameterValue'] elif param['ParameterKey'] == 'ClonedFromStackName': cloned_from = param['ParameterValue'] elif param['ParameterKey'] == 'Region': region = param['ParameterValue'] # find product type for source stack source_stack = Stack( next(param for param in content if param['ParameterKey'] == 'ClonedFromStackName')['ParameterValue'], next(param for param in content if param['ParameterKey'] == 'ClonedFromRegion')['ParameterValue'], ) app_type = source_stack.get_tag('product') # remove stackName, region and templateName from params to send content.remove( next(param for param in content if param['ParameterKey'] == 'StackName')) content.remove( next(param for param in content if param['ParameterKey'] == 'Region')) content.remove( next(param for param in content if param['ParameterKey'] == 'TemplateName')) # remove any params that are not in the Clone template template_file = get_template_file(template_name) yaml.SafeLoader.add_multi_constructor(u'!', general_constructor) template_params = yaml.safe_load(open(template_file, 'r'))['Parameters'] params_to_send = [] for param in content: if next((template_param for template_param in template_params if template_param == param['ParameterKey']), None): params_to_send.append(param) clone_stack = Stack(stack_name, region) if not clone_stack.store_current_action( 'clone', stack_locking_enabled(), True, session['saml']['subject'] if 'saml' in session else False): return False clustered = 'true' if 'Server' not in template_name else 'false' creator = session['saml']['subject'] if 'saml' in session else 'unknown' outcome = clone_stack.clone(params_to_send, template_file, app_type.lower(), clustered, creator, region, cloned_from) clone_stack.clear_current_action() return outcome
def post(self, region, stack_name): tags = request.get_json() mystack = Stack(stack_name, region) if not mystack.store_current_action( 'tag', stack_locking_enabled(), True, session['saml']['subject'] if 'saml' in session else False): return False try: outcome = mystack.tag(tags) except Exception as e: log.exception('Error occurred tagging stack') mystack.log_msg(ERROR, f'Error occurred tagging stack: {e}', write_to_changelog=True) mystack.clear_current_action() return False mystack.clear_current_action() return outcome
def post(self): content = request.get_json()[0] for param in content: if param['ParameterKey'] == 'Product': app_type = param['ParameterValue'] continue if param['ParameterKey'] == 'StackName': stack_name = param['ParameterValue'] continue elif param['ParameterKey'] == 'TemplateName': template_name = param['ParameterValue'] continue mystack = Stack(stack_name, session['region'] if 'region' in session else '') if not mystack.store_current_action( 'create', stack_locking_enabled(), True, session['saml']['subject'] if 'saml' in session else False): return False params_for_create = [ param for param in content if param['ParameterKey'] != 'StackName' and param['ParameterKey'] != 'TemplateName' and param['ParameterKey'] != 'Product' ] clustered = 'true' if 'Server' not in template_name else 'false' creator = session['saml']['subject'] if 'saml' in session else 'unknown' outcome = mystack.create(params_for_create, get_template_file(template_name), app_type, clustered, creator, session['region']) session['stacks'] = sorted(get_cfn_stacks_for_region()) mystack.clear_current_action() return outcome
def post(self, stack_name, change_set_name): mystack = Stack(stack_name, session['region'] if 'region' in session else '') if not mystack.store_current_action( 'update', stack_locking_enabled(), True, session['saml']['subject'] if 'saml' in session else False): return False outcome = mystack.execute_change_set(change_set_name) mystack.clear_current_action() return outcome
def get(self, region, stack_name): mystack = Stack(stack_name, region) tags = mystack.get_tags() return tags
def get(self, region, stack_name, node): mystack = Stack(stack_name, region) return mystack.get_node_cpu(node)
def get(self, region, stack_name): mystack = Stack(stack_name, region) mystack.clear_current_action() return True
def get(self, region, stack_name): mystack = Stack(stack_name, region) action = mystack.get_stack_action_in_progress() return action if action else 'None'
def get(self, region, stack_name): mystack = Stack(stack_name, region) stack_info = mystack.get_stack_info() return stack_info
def get(self, region, stack_name): mystack = Stack(stack_name, region) return mystack.get_sql()
def get(self, region, stack_name): mystack = Stack(stack_name, region) if not mystack.store_current_action( 'rollingrebuild', stack_locking_enabled(), True, session['saml']['subject'] if 'saml' in session else False): return False try: mystack.rolling_rebuild() except Exception as e: log.exception('Error occurred doing rolling rebuild') mystack.log_msg(ERROR, f'Error occurred doing rolling rebuild: {e}', write_to_changelog=True) mystack.clear_current_action() mystack.clear_current_action() return
def post(self, region, stack_name, new_version, zdu): mystack = Stack(stack_name, region) if not mystack.store_current_action( 'upgrade', stack_locking_enabled(), True, session['saml']['subject'] if 'saml' in session else False): return False try: if zdu == 'true': auth = request.get_json()[0] mystack.upgrade_zdu(new_version, auth['username'], auth['password']) else: mystack.upgrade(new_version) except Exception as e: log.exception('Error occurred upgrading stack') mystack.log_msg(ERROR, f'Error occurred upgrading stack: {e}', write_to_changelog=True) mystack.clear_current_action() mystack.clear_current_action() return
def get(self, region, stack_name, change_set_name): mystack = Stack(stack_name, region) return mystack.get_change_set_details(change_set_name)
def get(self, region, stack_name): mystack = Stack(stack_name, region) if not mystack.store_current_action( 'diagnostics', stack_locking_enabled(), False, False): return False try: mystack.heap_dump() except Exception as e: log.exception('Error occurred taking heap dumps') mystack.log_msg(ERROR, f'Error occurred taking heap dumps: {e}', write_to_changelog=True) mystack.clear_current_action() mystack.clear_current_action() return
def get(self, region, stack_name): mystack = Stack(stack_name, region) return mystack.get_thread_dump_links()
def get(self, region, stack_name, delete_changelogs, delete_threaddumps): mystack = Stack(stack_name, region) if not mystack.store_current_action( 'destroy', stack_locking_enabled(), not delete_changelogs, session['saml']['subject'] if 'saml' in session else False): return False try: mystack.destroy(delete_changelogs == 'true', delete_threaddumps == 'true') except Exception as e: log.exception('Error occurred destroying stack') mystack.log_msg(ERROR, f'Error occurred destroying stack: {e}', write_to_changelog=True) mystack.clear_current_action() session['stacks'] = sorted(get_cfn_stacks_for_region()) mystack.clear_current_action() return
def get(self, region, stack_name): mystack = Stack(stack_name, region) return mystack.has_termination_protection()
def get(self, region, stack_name, drain, threads, heaps): mystack = Stack(stack_name, region) if not mystack.store_current_action( 'rollingrestart', stack_locking_enabled(), True, session['saml']['subject'] if 'saml' in session else False): return False try: if threads == 'true': mystack.thread_dump(alsoHeaps=heaps) if heaps == 'true': mystack.heap_dump() mystack.rolling_restart(drain == 'true') except Exception as e: log.exception('Error occurred doing rolling restart') mystack.log_msg(ERROR, f'Error occurred doing rolling restart: {e}', write_to_changelog=True) mystack.clear_current_action() mystack.clear_current_action() return
def get(self, region, stack_name): mystack = Stack(stack_name, region) return mystack.get_zdu_compatibility()
def get(self, region, stack_name, node, threads, heaps): mystack = Stack(stack_name, region) if not mystack.store_current_action( 'togglenode', stack_locking_enabled(), True, session['saml']['subject'] if 'saml' in session else False): return False try: if threads == 'true': mystack.thread_dump(node=node, alsoHeaps=heaps) if heaps == 'true': mystack.heap_dump(node=node) mystack.toggle_node_registration(node) except Exception as e: log.exception('Error occurred toggling node registration') mystack.log_msg(ERROR, f'Error occurred toggling node registration: {e}', write_to_changelog=True) mystack.clear_current_action() mystack.clear_current_action() return
from pprint import pprint from forge.aws_cfn_stack.stack import Stack stack_region = 'us-east-1' app_type = 'confluence' stack_name = 'extlab' mystack = Stack(stack_name, stack_region, app_type) # status = mystack.print_action_log() outcome = mystack.get_current_state() outcome = mystack.debug_stackstate() # outcome = mystack.rolling_restart() # outcome = mystack.full_restart() # outcome = mystack.destroy() # outcome = mystack.upgrade('7.8.0') # try: # outcome = mystack.destroy() # except: # pass # outcome = mystack.clone('snap-0f1b5498862ee87dc','dr-lab-master-snap-201803090026', 'ChangeMe', 'cHANGEmE', 'confluence') # outcome = mystack.clone('snap-0f1b5498862ee87dc','dr-lab-master-snap-201803090026', 'ChangeMe', 'cHANGEmE', 'confluence', 'eac-stg','us-east-1') pprint(outcome)
def post(self, stack_name): content = request.get_json() new_params = content[0] orig_params = content[1] mystack = Stack(stack_name, session['region'] if 'region' in session else '') if not mystack.store_current_action( 'update', stack_locking_enabled(), True, session['saml']['subject'] if 'saml' in session else False): return False mystack.log_change('Original parameters') for param in orig_params: mystack.log_change( f"{param['ParameterKey']}: {param['ParameterValue']}") cfn_client = boto3.client('cloudformation', region_name=session['region']) cfn_resource = boto3.resource('cloudformation', region_name=session['region']) try: stack_details = cfn_client.describe_stacks(StackName=stack_name) existing_template_params = cfn_resource.Stack( stack_name).parameters except Exception as e: if e.response and "does not exist" in e.response['Error'][ 'Message']: log.error(f'Stack {stack_name} does not exist') return f'Stack {stack_name} does not exist' log.exception('Error occurred getting stack parameters for update') return False template_name = next( param for param in new_params if param['ParameterKey'] == 'TemplateName')['ParameterValue'] for param in new_params: # if param was not in previous template, always pass it in the change set if not next( (existing_param for existing_param in existing_template_params if existing_param['ParameterKey'] == param['ParameterKey']), None): continue # if param has not changed from previous, delete the value and set UsePreviousValue to true if ('ParameterValue' in param and param['ParameterValue'] == next( orig_param for orig_param in orig_params if orig_param['ParameterKey'] == param['ParameterKey'])['ParameterValue']): del param['ParameterValue'] param['UsePreviousValue'] = True # if param is subnets and the value has not changed from previous (even if the order has), do not pass in changeset, or pass in correct order if there are additional elif param['ParameterKey'] in ('InternalSubnets', 'ExternalSubnets'): orig_subnets = next( (subnet_param for subnet_param in orig_params if param['ParameterKey'] == subnet_param['ParameterKey']), None) if orig_subnets: orig_subnets_list = orig_subnets['ParameterValue'].split( ',') new_subnets_list = param['ParameterValue'].split(',') subnets_to_send = [] for subnet in orig_subnets_list: subnets_to_send.append(subnet) # append newly added subnets for new_subnet in new_subnets_list: if new_subnet not in orig_subnets_list: subnets_to_send.append(new_subnet) # remove any deleted subnets for orig_subnet in orig_subnets_list: if orig_subnet not in new_subnets_list: subnets_to_send.remove(orig_subnet) if subnets_to_send == orig_subnets_list: del param['ParameterValue'] param['UsePreviousValue'] = True else: param['ParameterValue'] = ','.join(subnets_to_send) params_for_update = [ param for param in new_params if (param['ParameterKey'] != 'StackName' and param['ParameterKey'] != 'TemplateName') ] env_param = next((tag for tag in stack_details['Stacks'][0]['Tags'] if tag['Key'] == 'environment'), None) if not env_param: log.warning('Stack is not tagged with environment, assuming prod') else: env = env_param['Value'] if env == 'stg' or env == 'dr': if not next( (param for param in params_for_update if param['ParameterKey'] == 'EBSSnapshotId'), None): params_for_update.append({ 'ParameterKey': 'EBSSnapshotId', 'UsePreviousValue': True }) if not next( (param for param in params_for_update if param['ParameterKey'] == 'DBSnapshotName'), None): params_for_update.append({ 'ParameterKey': 'DBSnapshotName', 'UsePreviousValue': True }) outcome = mystack.create_change_set(params_for_update, get_template_file(template_name)) mystack.clear_current_action() return outcome