def destroy(config): """ Destroy the environment by running 'terraform destroy'. Args: config: dictionary containing all variable settings required to run terraform with Returns: True Raises: EnvironmentExistsException if environment does not exist. """ # Check if env already exists env_name = config['environment'].get('name') env_vers = config['environment'].get('version', None) env = env_name if env_vers: env = "-".join([env_name, env_vers]) system_type = config['tags'].get('system_type', None) if not aws.environment_exists(env_name, env_vers, system_type): msg = "No such environment with the name {} exists." if system_type: env = "-".join([system_type, env]) raise EnvironmentExistsException(msg.format(env)) tf_root = _precheck(config, 'destroy') # Tag the resources as ready to destroy aws.tag_resources(config) # Run destroy tf_command = tf.destroy(config) return_code = utils.run_command(tf_command, cwd=config['tf_root']) # Double check the make sure we don't have anything left running # before destroying the S3 resources. if not aws.environment_exists(env_name, env_vers, system_type) and return_code == 0: # Destroy the per-environment S3 folder in msg = "Destroying S3 env folder: {}".format(config['env_folder']) logger.debug(msg) s3.destroy_folder(config['project_config'], config['env_folder']) # Destroy the state file in S3 msg = "Destroying S3 State file: {}".format(config['tf_state']) logger.debug(msg) s3.delete_object(config['tf_state_bucket'], config['tf_state']) return True
def query(config): """ Query AWS to see if an env already exists. If so, provide a list of ARNS. Args: config: dictionary containing all variable settings required to run terraform with Returns: True or False """ # Check if env already exists env_name = config['environment'].get('name') env_vers = config['environment'].get('version', None) env = env_name if env_vers: env = "-".join([env_name, env_vers]) system_type = config['tags'].get('system_type', None) resources = aws.environment_exists(env_name, env_vers, system_type) if system_type: env = "-".join([system_type, env]) if (resources): msg = "{} exists." msg += "\n\n{}" resources_json = json.dumps(resources, indent=4) message = colored(msg.format(env, resources_json), 'red') print(message) return
def create(config): """ Create the environment by running 'terraform apply'. Args: config: dictionary containing all variable settings required to run terraform with Returns: True Raises: EnvironmentExistsException if environment already exists. """ # Check if env already exists env_name = config['environment'].get('name') env_vers = config['environment'].get('version', None) env = env_name if env_vers: env = "-".join([env_name, env_vers]) system_type = config['tags'].get('system_type', None) resources = aws.environment_exists(env_name, env_vers, system_type) if (resources): if system_type: env = "-".join([system_type, env]) from termcolor import colored msg = "\n\nAn environment with the name {} already exists." msg += "\nPlease tear it down before trying to rebuild." msg += "\n\n{}" resources_json = json.dumps(resources, indent=4) message = colored(msg.format(env, resources_json), 'red') raise EnvironmentExistsException(message) _precheck(config, 'create') # Run Apply tf_command = tf.apply(config) logger.debug("Command: {}".format(" ".join(tf_command))) logger.debug("In: {}".format(config['tf_root'])) try: return_code = utils.run_command(tf_command, cwd=config['tf_root']) except: aws.tag_resources(config) return False aws.tag_resources(config) return True
def get_next_version(env, ephemeral_env=None): """ Return the next available version letter not currently in use. Will skip over letters in use. Will loop back to 'a' if it reaches 'z' Args: env: string name of a particular environement to check for existing VPCs. e.g. coral, malachite, lapis Returns: character representing next available, unused environment version. """ next_env = 'a' while aws.environment_exists(env, next_env, ephemeral_env): # increment to next version. # If we're already at 'z', loop back to 'a' if ord(next_env.lower()) == 122: next_env = 'a' else: # otherwise increment next_env = chr(ord(next_env.lower()) + 1) return next_env
def test_environment_does_not_exist(mock_config): mock_config['environment']['name'] = 'myenvname' mock_config['environment']['version'] = 'z' with patch('deployer.aws.boto3', fake_boto3): assert not aws.environment_exists(mock_config) return