def change_dir(self, argument_list): """ Change the current working directory to another directory Input: - argument_list: Arguments interepred from user input Output: - A print message indicate whether the function runs smoothly or has any error. """ # Change the current working directory to the directory whose path is # the 1st argument after "cd". If there is none of them, change it to # the directory that is recorded as HOME variable in the environ try: new_dir = (argument_list[1] if len(argument_list) > 1 else self.environ_dict["HOME"]) chdir(new_dir if not new_dir.startswith("~") else expanduser(new_dir)) self.environ_dict["PWD"] = getcwd() return "" except PermissionError: return get_error_message(new_dir, PermissionError, "cd") except FileNotFoundError: return get_error_message(new_dir, FileNotFoundError, "cd") except NotADirectoryError: return get_error_message(new_dir, NotADirectoryError, "cd") # When the HOME environ variable is not set, this error will raise except KeyError: return "intek-sh: cd: HOME not set"
def lambda_rename_dbinstance(event, context): """Handles rename of a DB instance""" region = os.environ['Region'] rds = boto3.client('rds', region) result = {} original_instance_identifier = '' modified_instance_identifier = '' try: if event.get('Error') == 'InstanceRestoreException' and \ 'Identifier' in event.get('Cause'): #rename revert scenario in case of db restore failure response = util.get_identifier_from_error(event) modified_instance_identifier = response["modified_identifier"] original_instance_identifier = response["original_identifier"] else: original_instance_identifier = event['identifier'] modified_instance_identifier = event['identifier'] + '-temp' rds.modify_db_instance( DBInstanceIdentifier = original_instance_identifier, ApplyImmediately = True, NewDBInstanceIdentifier = modified_instance_identifier) result['taskname'] = constants.RENAME result['identifier'] = modified_instance_identifier return result except Exception as error: error_message = util.get_error_message(original_instance_identifier, error) if constants.RATE_EXCEEDED in str(error): raise custom_exceptions.RateExceededException(error_message) else: raise custom_exceptions.RenameException(error_message)
def lambda_delete_dbcluster(event, context): """Handles deletion of a DB cluster and its corresponding readers and writers""" region = os.environ['Region'] rds = boto3.client('rds', region) result = {} cluster_id = event['identifier'] + constants.TEMP_POSTFIX try: describe_db_response = rds.describe_db_clusters( DBClusterIdentifier=cluster_id) for db_cluster_member in describe_db_response['DBClusters'][0][ 'DBClusterMembers']: rds.delete_db_instance( DBInstanceIdentifier=db_cluster_member['DBInstanceIdentifier'], SkipFinalSnapshot=True) rds.delete_db_cluster(DBClusterIdentifier=cluster_id, SkipFinalSnapshot=True) result['taskname'] = constants.DELETE result['identifier'] = cluster_id return result except Exception as error: error_message = util.get_error_message(cluster_id, error) if constants.RATE_EXCEEDED in str(error): raise custom_exceptions.RateExceededException(error_message) else: raise custom_exceptions.DeletionException(error_message)
def lambda_restore_dbinstance(event, context): """Handles restore of a db instance from its snapshot""" region = os.environ['Region'] rds = boto3.client('rds', region) result = {} response = util.get_modified_identifier(event['identifier']) try: describe_db_response = rds.describe_db_instances( DBInstanceIdentifier=event['identifier']) vpc_security_groups = describe_db_response['DBInstances'][0][ 'VpcSecurityGroups'] vpc_security_group_ids = [] for vpc_security_group in vpc_security_groups: vpc_security_group_ids.append( vpc_security_group['VpcSecurityGroupId']) rds.restore_db_instance_from_db_snapshot( DBInstanceIdentifier=response["instance_id"], DBSnapshotIdentifier=response["snapshot_id"], DBSubnetGroupName=describe_db_response['DBInstances'][0] ['DBSubnetGroup']['DBSubnetGroupName'], VpcSecurityGroupIds=vpc_security_group_ids) result['taskname'] = constants.DB_RESTORE result['identifier'] = response["instance_id"] return result except Exception as error: error_message = util.get_error_message(response["instance_id"], error) if constants.RATE_EXCEEDED in str(error): raise custom_exceptions.RateExceededException(error_message) else: raise custom_exceptions.InstanceRestoreException(error_message)
def lambda_restore_dbcluster(event, context): """Handles restore of a db cluster from its snapshot""" region = os.environ['Region'] result = {} rds = boto3.client('rds', region) old_cluster_id = event['identifier'] response = util.get_modified_identifier(event['identifier']) cluster_id = response["instance_id"] cluster_snapshot_id = response["snapshot_id"] try: describe_db_response = rds.describe_db_clusters( DBClusterIdentifier=old_cluster_id) vpc_security_groups = describe_db_response['DBClusters'][0][ 'VpcSecurityGroups'] engine = describe_db_response['DBClusters'][0]['Engine'] engine_version = describe_db_response['DBClusters'][0]['EngineVersion'] vpc_security_groups_ids = [] for vpc_security_group in vpc_security_groups: vpc_security_groups_ids.append( vpc_security_group['VpcSecurityGroupId']) rds.restore_db_cluster_from_snapshot( DBClusterIdentifier=cluster_id, SnapshotIdentifier=cluster_snapshot_id, Engine=engine, EngineVersion=engine_version, DBSubnetGroupName=describe_db_response['DBClusters'][0] ['DBSubnetGroup'], Port=describe_db_response['DBClusters'][0]['Port'], DatabaseName=describe_db_response['DBClusters'][0]['DatabaseName'], VpcSecurityGroupIds=vpc_security_groups_ids) for db_cluster_member in describe_db_response['DBClusters'][0][ 'DBClusterMembers']: desc_db_response = rds.describe_db_instances( DBInstanceIdentifier=db_cluster_member['DBInstanceIdentifier']) dbinstance_identifier = util.get_modified_identifier( db_cluster_member['DBInstanceIdentifier'])["instance_id"] rds.create_db_instance( DBInstanceIdentifier=dbinstance_identifier, DBInstanceClass=desc_db_response['DBInstances'][0] ['DBInstanceClass'], Engine=engine, DBClusterIdentifier=cluster_id) result['taskname'] = constants.CLUSTER_RESTORE result['identifier'] = cluster_id return result except Exception as error: error_message = util.get_error_message(cluster_id, error) if constants.RATE_EXCEEDED in str(error): raise custom_exceptions.RateExceededException(error_message) else: raise custom_exceptions.ClusterRestoreException(error_message)
def lambda_delete_dbinstance(event, context): """Handles deletion of a RDS db instance""" region = os.environ['Region'] rds = boto3.client('rds', region) result = {} instance_id = event['identifier'] + constants.TEMP_POSTFIX try: rds.delete_db_instance(DBInstanceIdentifier=instance_id, SkipFinalSnapshot=True) result['taskname'] = constants.DELETE result['identifier'] = instance_id return result except Exception as error: error_message = util.get_error_message(instance_id, error) if constants.RATE_EXCEEDED in str(error): raise custom_exceptions.RateExceededException(error_message) else: raise custom_exceptions.DeletionException(error_message)
def cluster_instance_rename_reversal(event, rds): """in case of failure at the Restore step, revert the initial rename of cluster readers and writers to unblock any activity on the db""" response = {} response = util.get_identifier_from_error(event) try: describe_response = rds.describe_db_clusters( DBClusterIdentifier = response["original_identifier"] ) # revert the rename of the reader and writer instances before renaming the cluster for db_cluster_member in describe_response['DBClusters'][0]['DBClusterMembers']: old_dbinstance_id = db_cluster_member['DBInstanceIdentifier'] new_dbinstance_id = util.get_modified_identifier(old_dbinstance_id)["instance_id"] rds.modify_db_instance( DBInstanceIdentifier = old_dbinstance_id, ApplyImmediately = True, NewDBInstanceIdentifier = new_dbinstance_id ) response["original_cluster_identifier"] = response["original_identifier"] response["modified_cluster_identifier"] = response["modified_identifier"] return response except Exception as error: error_message = util.get_error_message(response["modified_identifier"], error) raise Exception(error_message)
def cluster_instance_rename(event,rds): """rename of the reader and writer instances in a cluster""" response = {} original_cluster_identifier = event['identifier'] modified_cluster_identifier = event['identifier'] + constants.TEMP_POSTFIX try: describe_response = rds.describe_db_clusters( DBClusterIdentifier = original_cluster_identifier ) #rename the reader and writer instances before renaming the cluster for db_cluster_member in describe_response['DBClusters'][0]['DBClusterMembers']: old_dbinstance_id = db_cluster_member['DBInstanceIdentifier'] new_dbinstance_id = old_dbinstance_id + constants.TEMP_POSTFIX rds.modify_db_instance( DBInstanceIdentifier = old_dbinstance_id, ApplyImmediately = True, NewDBInstanceIdentifier = new_dbinstance_id ) response["original_cluster_identifier"] = original_cluster_identifier response["modified_cluster_identifier"] = modified_cluster_identifier return response except Exception as error: error_message = util.get_error_message(modified_cluster_identifier, error) raise Exception(error_message)