def modify(aws_account_number: str, region: str, managed_name: str, iam_role_name: tuple, interactive: bool): click.echo('{} Starting aurora-echo for {}'.format(log_prefix(), managed_name)) util = EchoUtil(region, aws_account_number) # click doesn't allow mismatches between option and parameter names, so just for clarity, this is a tuple iam_role_names = iam_role_name found_instance = util.find_instance_in_stage(managed_name, ECHO_NEW_STAGE) if found_instance: cluster_identifier = found_instance['DBClusterIdentifier'] if is_cluster_available(cluster_identifier): click.echo('{} Instance has modifiable cluster: {}'.format( log_prefix(), cluster_identifier)) modify_iam(cluster_identifier, iam_role_names, interactive, util) click.echo('{} Updating tag for modified instance: {}'.format( log_prefix(), found_instance['DBInstanceIdentifier'])) util.add_stage_tag(managed_name, found_instance, ECHO_MODIFY_STAGE) click.echo('{} Done!'.format(log_prefix())) else: click.echo( '{} Cluster {} does not have status \'available\'. Not proceeding.' .format(log_prefix(), cluster_identifier)) else: click.echo('{} No instance found in stage {}. Not proceeding.'.format( log_prefix(), ECHO_NEW_STAGE))
def promote(aws_account_number: str, region: str, managed_name: str, hosted_zone_id: tuple, record_set: str, ttl: str, interactive: bool): click.echo('{} Starting aurora-echo for {}'.format(log_prefix(), managed_name)) util = EchoUtil(region, aws_account_number) found_instance = util.find_instance_in_stage(managed_name, ECHO_NEW_STAGE) if found_instance and found_instance['DBInstanceStatus'] == 'available': click.echo('{} Found promotable instance: {}'.format(log_prefix(), found_instance['DBInstanceIdentifier'])) cluster_endpoint = found_instance['Endpoint']['Address'] for zone_id in hosted_zone_id: record_set_dict = find_record_set(zone_id, record_set) if record_set_dict: update_dns(zone_id, record_set_dict, cluster_endpoint, ttl, interactive) old_promoted_instance = util.find_instance_in_stage(managed_name, ECHO_PROMOTE_STAGE) if old_promoted_instance: click.echo('{} Retiring old instance: {}'.format(log_prefix(), old_promoted_instance['DBInstanceIdentifier'])) util.add_stage_tag(managed_name, old_promoted_instance, ECHO_RETIRE_STAGE) click.echo('{} Updating tag for promoted instance: {}'.format(log_prefix(), found_instance['DBInstanceIdentifier'])) util.add_stage_tag(managed_name, found_instance, ECHO_PROMOTE_STAGE) click.echo('{} Done!'.format(log_prefix())) else: click.echo('{} No record set found at hosted zone {} with name {}. Unable to promote instance.'.format(log_prefix(), zone_id, record_set)) else: click.echo('{} No instance found in stage {} with status \'available\'. Not proceeding.'.format(log_prefix(), ECHO_NEW_STAGE))
def modify_iam(cluster_identifier: str, iam_role_names: tuple, interactive: bool, util: EchoUtil): """ Update the IAM role on the cluster If we add more modifications, consolidate them into one method so we can prompt the user only once """ if iam_role_names: iam_role_arn_list = [] for iam_name in iam_role_names: arn = util.construct_iam_arn(iam_name) iam_role_arn_list.append(arn) click.echo('{} IAM: {}'.format(log_prefix(), arn)) # pop out of the loop to ask if this is all good if interactive: click.confirm( '{} Ready to modify cluster with these settings?'.format( log_prefix()), abort=True) # exits entirely if no click.echo('{} Adding IAM to cluster...'.format(log_prefix())) for iam_role_arn in iam_role_arn_list: iam_response = rds.add_role_to_db_cluster( DBClusterIdentifier=cluster_identifier, RoleArn=iam_role_arn) else: # even if they didn't want an IAM added, it still successfully passed through this stage click.echo('{} No IAM roles provided. Nothing to do! {}'.format( log_prefix(), cluster_identifier))
def retire(aws_account_number: str, region: str, managed_name: str, interactive: bool): click.echo('{} Starting aurora-echo for {}'.format(log_prefix(), managed_name)) util = EchoUtil(region, aws_account_number) found_instance = util.find_instance_in_stage(managed_name, ECHO_RETIRE_STAGE) if found_instance: click.echo('{} Found instance ready for retirement: {}'.format( log_prefix(), found_instance['DBInstanceIdentifier'])) delete_instance(found_instance, interactive) click.echo('{} Done!'.format(log_prefix())) else: click.echo('{} No instance found in stage {}. Not proceeding.'.format( log_prefix(), ECHO_RETIRE_STAGE))
def new(aws_account_number: str, region: str, cluster_snapshot_name: str, managed_name: str, db_subnet_group_name: str, db_instance_class: str, engine: str, availability_zone: str, vpc_security_group_id: list, tag: list, minimum_age_hours: int, interactive: bool, suffix: str): click.echo('{} Starting aurora-echo for {}'.format(log_prefix(), managed_name)) util = EchoUtil(region, aws_account_number) if not util.instance_too_new(managed_name, minimum_age_hours): cluster_snapshot_identifier = find_snapshot(cluster_snapshot_name) if cluster_snapshot_identifier: restore_cluster_name = managed_name + '-' + today_string if suffix is not None: restore_cluster_name += '-' + suffix tag_set = util.construct_managed_tag_set(managed_name, ECHO_NEW_STAGE) user_tags = util.construct_user_tag_set(tag) if user_tags: tag_set.extend(user_tags) # collect parameters up front so we only have to prompt the user once cluster_params = collect_cluster_params( cluster_snapshot_identifier, restore_cluster_name, db_subnet_group_name, engine, vpc_security_group_id, tag_set) instance_params = collect_instance_params( restore_cluster_name, restore_cluster_name, engine, db_instance_class, availability_zone, tag_set) # instance and cluster names are the same create_cluster_and_instance(cluster_params, instance_params, interactive) else: click.echo( '{} No cluster snapshots found with name {}. Not proceeding.'. format(log_prefix(), cluster_snapshot_name)) else: click.echo( '{} Found managed instance created less than {} hours ago. Not proceeding.' .format(log_prefix(), minimum_age_hours))
def promote(aws_account_number: str, region: str, managed_name: str, hosted_zone_id: tuple, record_set: str, ttl: str, interactive: bool): click.echo('{} Starting aurora-echo for {}'.format(log_prefix(), managed_name)) util = EchoUtil(region, aws_account_number) # click doesn't allow mismatches between option and parameter names, so just for clarity, this is a tuple hosted_zone_ids = hosted_zone_id found_instance = util.find_instance_in_stage(managed_name, ECHO_MODIFY_STAGE) if found_instance and found_instance['DBInstanceStatus'] == 'available': click.echo('{} Found promotable instance: {}'.format( log_prefix(), found_instance['DBInstanceIdentifier'])) cluster_endpoint = found_instance['Endpoint']['Address'] update_dns(hosted_zone_ids, record_set, cluster_endpoint, ttl, interactive) old_promoted_instance = util.find_instance_in_stage( managed_name, ECHO_PROMOTE_STAGE) if old_promoted_instance: click.echo('{} Retiring old instance: {}'.format( log_prefix(), old_promoted_instance['DBInstanceIdentifier'])) util.add_stage_tag(managed_name, old_promoted_instance, ECHO_RETIRE_STAGE) click.echo('{} Updating tag for promoted instance: {}'.format( log_prefix(), found_instance['DBInstanceIdentifier'])) util.add_stage_tag(managed_name, found_instance, ECHO_PROMOTE_STAGE) click.echo('{} Done!'.format(log_prefix())) else: click.echo( '{} No instance found in stage {} with status \'available\'. Not proceeding.' .format(log_prefix(), ECHO_MODIFY_STAGE))