def _run_terraform_init(self, base_dir, infra_dir): logger.debug( 'Getting Terraform modules defined in {}'.format(infra_dir) ) check_call([ TERRAFORM_BINARY, 'init', infra_dir ], cwd=base_dir)
def terraform_init(self, get=False): credentials = self.boto_session.get_credentials() logger.debug( f'Initialising in {self.boto_session.region_name} ' f'with bucket: {self.bucket}, ' f'key prefix: {self.workspace_key_prefix}, ' f'tfstate file: {self.tfstate_filename}, ' f'dynamodb table: {self.dynamodb_table}' ) check_call( [ TERRAFORM_BINARY, 'init', f'-get={"true" if get else "false"}', f'-get-plugins={"true" if get else "false"}', f'-backend-config=bucket={self.bucket}', f'-backend-config=region={self.boto_session.region_name}', f'-backend-config=key={self.tfstate_filename}', ( '-backend-config=workspace_key_prefix=' f'{self.workspace_key_prefix}' ), f'-backend-config=dynamodb_table={self.dynamodb_table}', f'-backend-config=access_key={credentials.access_key}', f'-backend-config=secret_key={credentials.secret_key}', f'-backend-config=token={credentials.token}', self.working_directory, ], cwd=self.base_directory, )
def terraform_select_workspace(self): check_call( [ TERRAFORM_BINARY, 'workspace', 'select', self.environment_name, self.working_directory, ], cwd=self.base_directory, )
def init(self, get_terraform_modules=False): with NamedTemporaryFile( prefix='cdflow_backend_', suffix='.tf', dir=self.working_directory, delete=False, mode='w+' ) as backend_file: logger.debug(f'Writing backend config to {backend_file.name}') backend_file.write(dedent(''' terraform { backend "s3" { } } ''').strip()) logger.debug( f'Registering {backend_file.name} to be removed at exit' ) atexit.register(remove_file, backend_file.name) logger.debug( f'Initialising backend in {self.working_directory} ' f'with {self.bucket}, {self.boto_session.region_name}, ' f'{self.state_file_key}, {self.dynamodb_table}' ) credentials = self.boto_session.get_credentials() check_call( [ TERRAFORM_BINARY, 'init', f'-get={"true" if get_terraform_modules else "false"}', f'-get-plugins={"true" if get_terraform_modules else "false"}', f'-backend-config=bucket={self.bucket}', f'-backend-config=region={self.boto_session.region_name}', f'-backend-config=key={self.state_file_key}', f'-backend-config=dynamodb_table={self.dynamodb_table}', f'-backend-config=access_key={credentials.access_key}', f'-backend-config=secret_key={credentials.secret_key}', f'-backend-config=token={credentials.token}', self.working_directory, ], cwd=self.base_directory, )
def _apply(self): check_call(self._build_parameters('apply'), cwd=self._release_path, env=env_with_aws_credetials(os.environ, self._boto_session))
def test_check_call(self, _check_call): # Given When check_call('echo', {}) # Then _check_call.assert_called_once_with('echo', {})