def main(args): """ This function will terminate the stale instances running the regions passed as parameter to the script. Instances which satisfy following conditions will be terminated. - Name of the instance starts with 'vertis_'. Instances starting with 'vertis_donotremove_' will not be terminated. - Instances which are running for more than three hours, and have blank 'Name' tag. """ logger = initPipelineLogger("janitor_ec2", logLevel=args.logLevel) awsAccessKeyId = os.environ.get("AWS_ACCESS_KEY_ID") awsScrectAccessKey = os.environ.get("AWS_SECRET_ACCESS_KEY") if not (awsAccessKeyId and awsScrectAccessKey): logger.error("Please set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY") raise MissingAWSKeysInEnvironment("AWS keys are not set") # required for terminateInstance function config = {} config["AWS_ACCESS_KEY_ID"] = awsAccessKeyId config["AWS_SECRET_ACCESS_KEY"] = awsScrectAccessKey for region in args.region: instances = [i.id for i in getInstances(region, awsAccessKeyId, awsScrectAccessKey, logger) if toBeTerminated(i, logger)] if instances: config["REGION"] = region logger.info("Deleting {}".format(", ".join(instances))) for instance in instances: terminateInstance(instance, config, logger) else: logger.info("None of the instances are stale.")
def postTestRunAction(instanceId, terminate=True, **config): """ Terminate or stop the instance. If the tests fail then just stop the instance or terminate it. :param instanceID: InstanceID of instance launched. :terminate: if True terminate the instance else stop instance. :config: dict of config values """ if terminate: g_logger.info("Terminating instance %s", instanceId) terminateInstance(instanceId, config, g_logger) else: g_logger.info("Stopping instance %s", instanceId) stopInstance(instanceId, config, g_logger)
def handleSignalInterrupt(signal, _frame): g_logger.error("Received interrupt signal %s", signal) if instanceId: g_logger.error("Terminating instance %s", instanceId) terminateInstance(instanceId, g_config, g_logger)