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 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 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