def test_git_url_with_nongit_url_with_subdir(): url = "/some/local/path//subdir" assert not utils.git_url(url) expected_branch = None expected_subdir = 'subdir' (repo, branch, subdir) = utils.parse_git_url(url)
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_git_url_with_nongit_url_with_branch_and_subdir(): url = "/some/local/path?branch=BRANCH_NAME//subdir" assert not utils.git_url(url) expected_repo = '/some/local/path' expected_branch = 'BRANCH_NAME' expected_subdir = 'subdir' (repo, branch, subdir) = utils.parse_git_url(url)
def test_git_url_with_nongit_url(): url = "/some/local/path" assert not utils.git_url(url) expected_repo = '/some/local/path' expected_branch = None expected_subdir = None (repo, branch, subdir) = utils.parse_git_url(url)
def test_git_set_branch(): url = "/some/local/path?branch=BRANCH_NAME" assert not utils.git_url(url) expected_cmd = ['git', 'checkout', 'BRANCH_NAME'] (repo, branch, subdir) = utils.parse_git_url(url) returnd_cmd = utils.git_set_branch(branch) assert expected_cmd == returnd_cmd
def test_git_url_with_nongit_url_with_dotgit(): # should fail url = "/some/local/path.git" #assert not utils.git_url(url) url = "/some/local/path?branch=BRANCH_NAME//subdir" expected_repo = '/some/local/path' expected_branch = 'BRANCH_NAME' expected_subdir = 'subdir' (repo, branch, subdir) = utils.parse_git_url(url)
def ttest_git_http_url_with_branch_and_subdir(): url = '[email protected]:group/project.git?branch=made_up_branch//subdir' expected_repo = '[email protected]:group/project.git' expected_branch = 'made_up_branch' expected_subdir = 'subdir' (repo, branch, subdir) = utils.parse_git_url(url) assert expected_repo == repo assert expected_branch == branch assert expected_subdir == subdir return
def test_git_http_url_with_subdir_no_branch(): url = 'https://[email protected]:group/project.git//subdir' assert utils.git_url(url) expected_repo = 'https://[email protected]:group/project.git' expected_branch = None expected_subdir = 'subdir' (repo, branch, subdir) = utils.parse_git_url(url) assert expected_repo == repo assert expected_branch == branch assert expected_subdir == subdir
def test_git_http_url_with_branch_no_subdir(): url = 'https://[email protected]:group/project.git?branch=made_up_branch' assert utils.git_url(url) expected_repo = 'https://[email protected]:group/project.git' expected_branch = 'made_up_branch' expected_subdir = None (repo, branch, subdir) = utils.parse_git_url(url) assert expected_repo == repo assert expected_branch == branch assert expected_subdir == subdir
def plan(config): """ Run 'terraform plan'. Args: config: dictionary containing all variable settings required to run terraform with Returns: True """ (repo, branch, subdir) = utils.parse_git_url(config['terraform']) _precheck(config, 'plan') return True
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 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