def sync_terraform(config): """ Clone all terraform git repositories to the workdir. Args: config: dictionary containing all variable settings required to run terraform with Returns: nothing """ # if we've already run terraform here, we don't need to check it out again. if config.get('tf_root') and os.path.isdir( os.path.join(config['tf_root'], '.terraform')): log_msg = "Terraform code already checked out to this location: {}" logger.debug(log_msg.format(config['tf_root'])) return workdir.options.path = config['tmpdir'] workdir.create() (repo, branch, subdir) = utils.parse_git_url(config['terraform']) clone_cmd = utils.git_clone(repo, branch) utils.run_command(clone_cmd, config['tmpdir']) return
def test_has_file(tmpdir): with tmpdir.as_cwd(): workdir.options.path = 'test_has_file' workdir.create() with workdir.as_cwd(): open('myfile', 'w').close() assert workdir.has_file('myfile') is True assert workdir.has_file('notthere') is False
def preconfig(setup_teardown): # Make sure test environment exists so we can test cleanup print("\nIn preconfig()") workdir.options.path = setup_teardown['tmpdir'] workdir.create() print("passed_config['tmpdir'] = {}".format(setup_teardown['tmpdir'])) return setup_teardown
def test_sync_terraform(setup_teardown): workdir.options.path = setup_teardown['tmpdir'] workdir.create() here = os.path.dirname(__file__) os.path.abspath(os.path.join(here, os.pardir, os.pardir, 'terraform')) git_url = '[email protected]:group/project?branch=made_up_branch' setup_teardown['terraform'] = git_url with patch('deployer.utils.run_command', mock_run_cmd): preflight.sync_terraform(setup_teardown) return
def test_clean(tmpdir): with tmpdir.as_cwd(): workdir.options.path = 'test_clean' workdir.create() touchfile = os.path.join(workdir.options.path, 'touchfile') touchdir = os.path.join(workdir.options.path, 'touchdir') open(touchfile, 'w').close() os.mkdir(touchdir) assert os.path.isfile(touchfile) assert os.path.isdir(touchdir) workdir.clean() assert not os.path.exists(touchfile) assert os.path.isdir(workdir.options.path)
def download_staged_artifacts(config): """ Downloads staged artifacts from S3 so terraform can use them access them for placing in environment-specific staging location. Args: config: dictionary containing all variable settings required to run terraform with Returns: nothing Raises: MissingConfigurationParameterException if 'project_config' is undefined. """ s3 = boto3.resource('s3') bucket_name = config.get('project_config', None) if not bucket_name: msg = "project_config bucket is not defined. Can not proceed." raise MissingConfigurationParameterException(msg) if not config.get('staged_artifacts', None): logger.warn( "'staged_artifacts' not defined. Nothing to download from S3. ") return logmsg = "Downloading project files from s3 bucket {}" logger.debug(logmsg.format(bucket_name)) for bucket_key in config['staged_artifacts'].keys(): s3_file = os.path.basename(bucket_key) bucket_file = s3.Object(bucket_name, bucket_key) try: local_file = os.path.join(config['tmpdir'], s3_file) workdir.options.path = os.path.dirname(local_file) workdir.create() bucket_file.download_file(local_file) log_msg = "Downloading {}/{} to {}" logger.debug( log_msg.format(config['project_config'], bucket_key, local_file)) except: log_msg = "Error downloading {}/{} to {}" logger.debug( log_msg.format(config['project_config'], bucket_key, local_file)) return
def setup_teardown(scope="function"): # Make sure test environment doesn't exist mock_config = { "terraform": "[email protected]:group/project.git?branch=made_up_branch", "aws_profile": "veracode-random", "aws_region": "us-east-1", "availability_zones": ['us-east-1b', 'us-east-1c', 'us-east-1d', 'us-east-1e'], "account_id": "555828001259", "environment": { "name": "myenvname", "version": "a", }, "env_name": "myenvname-a", "tf_state": "555828001259-unittests-my_env_name-a-tfstate", "project_config": "555828001259-unittests-data", "project": 'myproj' } tmpdir = '/tmp/test_tmp_dir' tmpfile = os.path.join(tmpdir, "test_vars.json") workdir.options.path = tmpdir workdir.create() with open(tmpfile, 'w') as tfile: json.dump(mock_config, tfile, indent=2) yield {'mock_config': mock_config, 'filename': tmpfile} # Make sure we clean up if anything else actually creates it workdir.options.path = tmpdir workdir.remove()
def pre_setup(config, sync=True): config['public_zone_id'] = config.get('public_zone_id', "<computed>") #create tmp dir to stage a deployment from config['tmpdir'] = config.get('tmpdir', os.path.join('/tmp', str(uuid.uuid4()))) logger.debug("{}: Creating tmpdir: {}".format(__name__, config['tmpdir'])) workdir.options.path = config['tmpdir'] workdir.create() config['tfvars'] = os.path.join(workdir.options.path, config.get('tfvars_file', 'vars.tf')) config['tf_root'] = config.get('tf_root', utils.get_tf_location(config)) # If the tf location is set to a local directory, we need to deal # with things like the possibility of the actual tf location being # in a subdir and a specified branch. if not utils.git_url(config['terraform']): (repo, branch, subdir) = utils.parse_git_url(config['terraform']) config['tf_root'] = repo if branch: msg = "Setting branch to: {}".format(branch) logger.debug(msg) if sync: git_pull_cmd = utils.git_pull(repo) utils.run_command(git_pull_cmd, repo) git_set_branch_cmd = utils.git_set_branch(branch) utils.run_command(git_set_branch_cmd, repo) if subdir: tf_root = os.path.join(config.get('tf_root', repo), subdir) msg = "Setting tf_root to: {}".format(tf_root) logger.debug(msg) config['tf_root'] = tf_root return config
def test_create(tmpdir): with tmpdir.as_cwd(): workdir.options.path = 'test_create' assert not os.path.exists(workdir.options.path) workdir.create() assert os.path.exists(workdir.options.path)
def setup(config, sync=True): """ Get ready to run 'terraform apply' for this environment. Modifies the dictionary passed in, then returns it. Args: config: dictionary containing all variable settings required to run terraform with sync: boolean. Toggles setting the branch on/off. Defaults to on. Returns: config with updated values. Raises: MissingConfigurationParameterException for missing config file entries. """ #create tmp dir to stage a deployment from config['tmpdir'] = config.get('tmpdir', os.path.join('/tmp', str(uuid.uuid4()))) logger.debug("{}: Creating tmpdir: {}".format(__name__, config['tmpdir'])) workdir.options.path = config['tmpdir'] workdir.create() config['tfvars'] = os.path.join(workdir.options.path, config.get('tfvars_file', 'vars.tf')) # Create the per-environment S3 folder in pre-flight logmsg = "{}: Creating per-environment folder : {}:{}" logger.debug( logmsg.format(__name__, config['project_config'], config['env_folder'])) s3.create_folder(config['project_config'], config['env_folder']) if not config.get('route53_tld', None): msg = "route53_tld variable is not set in config file." raise MissingConfigurationParameterException(msg) zone_id = r53.get_zone_id(config['route53_tld']) if not zone_id: msg = "zone_id not set." raise MissingConfigurationParameterException(msg) config['public_zone_id'] = config.get('public_zone_id', zone_id) config['tf_root'] = config.get('tf_root', utils.get_tf_location(config)) # If the tf location is set to a local directory, we need to deal # with things like the possibility of the actual tf location being # in a subdir and a specified branch. if not utils.git_url(config['terraform']): (repo, branch, subdir) = utils.parse_git_url(config['terraform']) config['tf_root'] = repo if branch: msg = "Setting branch to: {}".format(branch) logger.debug(msg) if sync: git_pull_cmd = utils.git_pull(repo) utils.run_command(git_pull_cmd, repo) git_set_branch_cmd = utils.git_set_branch(branch) utils.run_command(git_set_branch_cmd, repo) if subdir: tf_root = os.path.join(config.get('tf_root', repo), subdir) msg = "Setting tf_root to: {}".format(tf_root) logger.debug(msg) config['tf_root'] = tf_root return config