def test_switch_default_repo_and_branch__default_branch_environment_is_absent__branch_and_repository_name_are_stored_as_global_parameters( self, get_branch_mock): get_branch_mock.return_value = mock.MagicMock('new-codecommit-branch') useops.switch_default_repo_and_branch( repo_name='new-codecommit-repository', branch_name='new-codecommit-branch', ) self.assertEqual('new-codecommit-branch', fileoperations.get_config_setting('global', 'branch')) self.assertEqual( 'new-codecommit-repository', fileoperations.get_config_setting('global', 'repository'))
def do_zip(self, location, staged=False): cwd = os.getcwd() try: # must be in project root for git archive to work. fileoperations._traverse_to_project_root() if staged: commit_id, stderr, exitcode = self._run_cmd(['git', 'write-tree']) else: commit_id = 'HEAD' io.log_info('creating zip using git archive {0}'.format(commit_id)) stdout, stderr, exitcode = self._run_cmd( ['git', 'archive', '-v', '--format=zip', '-o', location, commit_id]) io.log_info('git archive output: {0}'.format(stderr)) project_root = os.getcwd() must_zip_submodules = fileoperations.get_config_setting('global', 'include_git_submodules') if must_zip_submodules: # individually zip submodules if there are any stdout, stderr, exitcode = self._run_cmd(['git', 'submodule', 'foreach', '--recursive']) for index, line in enumerate(stdout.splitlines()): submodule_dir = line.split(' ')[1].strip('\'') os.chdir(os.path.join(project_root, submodule_dir)) self.do_zip_submodule(location, "{0}_{1}".format(location, str(index)), staged=staged, submodule_dir=submodule_dir) finally: os.chdir(cwd)
def get_setting_from_current_environment(keyname): env_name = commonops.get_current_branch_environment() env_dict = fileoperations.get_config_setting('environment-defaults', env_name) if env_dict: return env_dict.get(keyname)
def get_config_setting_from_branch_or_default(key_name, default=_marker): setting = get_setting_from_current_branch(key_name) if setting is not None: return setting else: return fileoperations.get_config_setting('global', key_name, default=default)
def cleanup_ignore_file(): sc = fileoperations.get_config_setting('global', 'sc') if sc: source_control = SourceControl.get_source_control() source_control.clean_up_ignore_file() fileoperations.write_config_setting('global', 'sc', None)
def test_create_config_file_file_exists(self): fileoperations.write_config_setting('global', 'randomKey', 'val') fileoperations.write_config_setting('test', 'application_name', 'app1') app_name = 'ebcli-test' region = 'us-east-1' solution = 'my-solution-stack' fileoperations.create_config_file(app_name, region, solution) key = fileoperations.get_config_setting('global', 'randomKey') app = fileoperations.get_config_setting('global', 'application_name') test = fileoperations.get_config_setting('test', 'application_name') self.assertEqual(key, 'val') self.assertEqual(app, app_name) self.assertEqual(test, 'app1')
def test_dissociate_environment_from_branch(self): fileoperations.write_config_setting('branch-defaults', 'default', {'environment': 'my-environment'}) terminateops.dissociate_environment_from_branch('my-environment') self.assertIsNotNone( fileoperations.get_config_setting('branch-defaults', 'default'))
def setup_ignore_file(): io.log_info('Setting up ignore file for source control') sc = fileoperations.get_config_setting('global', 'sc') if not sc: source_control = SourceControl.get_source_control() source_control.set_up_ignore_file() sc_name = source_control.get_name() fileoperations.write_config_setting('global', 'sc', sc_name)
def test_dissociate_environment_from_branch__environment_is_not_default_env( self): fileoperations.write_config_setting( 'branch-defaults', 'default', {'environment': 'my-environment-2'}) terminateops.dissociate_environment_from_branch('my-environment') self.assertEqual({'environment': 'my-environment-2'}, fileoperations.get_config_setting( 'branch-defaults', 'default'))
def test_get_config_setting_no_global(self): if os.path.exists(fileoperations.global_config_file): os.remove(fileoperations.global_config_file) self.assertFalse(os.path.exists(fileoperations.global_config_file)) fileoperations.create_config_file('ebcli-test', 'us-east-1', 'my-solution-stack') result = fileoperations.get_config_setting('global', 'application_name') self.assertEqual(result, 'ebcli-test')
def test_switch_default_environment__redundantly_switch_to_present_environment( self, get_environment_mock): fileoperations.write_config_setting('branch-defaults', 'default', {'environment': 'my-old-env'}) get_environment_mock.return_value = mock.MagicMock('some-environment') useops.switch_default_environment('my-old-env') branch_defaults = fileoperations.get_config_setting( 'branch-defaults', 'default') self.assertEqual('my-old-env', branch_defaults['environment'])
def test_get_config_setting_no_files(self): if os.path.exists(fileoperations.local_config_file): os.remove(fileoperations.local_config_file) self.assertFalse(os.path.exists(fileoperations.local_config_file)) if os.path.exists(fileoperations.global_config_file): os.remove(fileoperations.global_config_file) self.assertFalse(os.path.exists(fileoperations.global_config_file)) result = fileoperations.get_config_setting('global', 'application_name') self.assertEqual(result, None)
def get_source_control(): try: git_installed = fileoperations.get_config_setting('global', 'sc') except NotInitializedError: git_installed = False if not git_installed: if Git().is_setup(): return Git() else: return NoSC() return Git()
def test_get_config_setting_merge(self): config = {'global': {'application_name': 'myApp'}} with open(fileoperations.global_config_file, 'w') as f: f.write(yaml.dump(config, default_flow_style=False)) self.assertTrue(os.path.exists(fileoperations.global_config_file)) fileoperations.create_config_file('ebcli-test', 'us-east-1', 'my-solution-stack') result = fileoperations.get_config_setting('global', 'application_name') self.assertEqual(result, 'ebcli-test')
def test_create_config_file_no_file(self): if os.path.exists(fileoperations.local_config_file): os.remove(fileoperations.local_config_file) self.assertFalse(os.path.exists(fileoperations.local_config_file)) app_name = 'ebcli-test' region = 'us-east-1' solution = 'my-solution-stack' fileoperations.create_config_file(app_name, region, solution) self.assertTrue(os.path.exists(fileoperations.local_config_file)) rslt = fileoperations.get_config_setting('global', 'application_name') self.assertEqual(app_name, rslt)
def test_switch_default_environment__environment_not_found( self, get_environment_mock): fileoperations.write_config_setting('branch-defaults', 'default', {'environment': 'my-old-env'}) get_environment_mock.side_effect = NotFoundError( 'Environment "my-new-env" not Found.') with self.assertRaises(NotFoundError): useops.switch_default_environment('my-new-env') branch_defaults = fileoperations.get_config_setting( 'branch-defaults', 'default') self.assertEqual('my-old-env', branch_defaults['environment'])
def get_source_control(): # First check for setting in config file try: git_installed = fileoperations.get_config_setting('global', 'sc') except NotInitializedError: git_installed = False if not git_installed: if Git().is_setup(): return Git() else: return NoSC() return Git()
def test_get_config_setting_no_local(self): config = {'global': {'application_name': 'myApp'}} with open(fileoperations.global_config_file, 'w') as f: f.write(yaml.dump(config, default_flow_style=False)) self.assertTrue(os.path.exists(fileoperations.global_config_file)) if os.path.exists(fileoperations.local_config_file): os.remove(fileoperations.local_config_file) self.assertFalse(os.path.exists(fileoperations.local_config_file)) result = fileoperations.get_config_setting('global', 'application_name') self.assertEqual(result, 'myApp')
def test_create_config_file_no_dir(self): if os.path.exists(fileoperations.beanstalk_directory): shutil.rmtree(fileoperations.beanstalk_directory, ignore_errors=True) self.assertFalse(os.path.exists(fileoperations.beanstalk_directory)) app_name = 'ebcli-test' region = 'us-east-1' solution = 'my-solution-stack' fileoperations.create_config_file(app_name, region, solution) self.assertTrue(os.path.exists(fileoperations.beanstalk_directory)) self.assertTrue(os.path.exists(fileoperations.local_config_file)) rslt = fileoperations.get_config_setting('global', 'application_name') self.assertEqual(app_name, rslt)
def get_setting_from_current_branch(keyname): try: source_control = SourceControl.get_source_control() branch_name = source_control.get_current_branch() except CommandError: LOG.debug("Git is not installed returning None for setting: %s".format(keyname)) return None branch_dict = fileoperations.get_config_setting('branch-defaults', branch_name) if branch_dict is None: return None else: try: return branch_dict[keyname] except KeyError: return None
def test_switch_default_repo_and_branch__default_branch_environment_are_absent__writes_new( self, get_branch_mock): fileoperations.write_config_setting('branch-defaults', 'default', {'environment': 'my-old-env'}) get_branch_mock.return_value = mock.MagicMock('new-codecommit-branch') useops.switch_default_repo_and_branch( repo_name='new-codecommit-repository', branch_name='new-codecommit-branch', ) environment_defaults = fileoperations.get_config_setting( 'environment-defaults', 'my-old-env') self.assertEqual('new-codecommit-branch', environment_defaults['branch']) self.assertEqual('new-codecommit-repository', environment_defaults['repository'])
def test_switch_default_repo_and_branch__remote_codecommit_repository_absent__exception_is_raised__present_values_left_intact( self, get_branch_mock): get_branch_mock.side_effect = ServiceError fileoperations.write_config_setting('branch-defaults', 'default', {'environment': 'my-old-env'}) gitops.write_setting_to_current_environment_or_default( 'branch', 'old-codecommit-branch') gitops.write_setting_to_current_environment_or_default( 'repository', 'old-codecommit-repository') with self.assertRaises(NotFoundError): useops.switch_default_repo_and_branch( repo_name='nonexistent-codecommit-repository', branch_name='nonexistent-codecommit-branch', ) environment_defaults = fileoperations.get_config_setting( 'environment-defaults', 'my-old-env') self.assertEqual('old-codecommit-branch', environment_defaults['branch']) self.assertEqual('old-codecommit-repository', environment_defaults['repository'])
def get_config_setting_from_current_environment_or_default(key_name): setting = get_setting_from_current_environment(key_name) return setting or fileoperations.get_config_setting('global', key_name)
def create_app_version(app_name, process=False, label=None, message=None, staged=False, build_config=None): cwd = os.getcwd() fileoperations.ProjectRoot.traverse() try: if heuristics.directory_is_empty(): io.echo('NOTE: {}'.format(strings['appversion.none'])) return None finally: os.chdir(cwd) source_control = SourceControl.get_source_control() if source_control.untracked_changes_exist(): io.log_warning(strings['sc.unstagedchanges']) if label: version_label = label else: version_label = source_control.get_version_label() if staged: timestamp = datetime.now().strftime("%y%m%d_%H%M%S") version_label = version_label + '-stage-' + timestamp if message: description = message else: description = source_control.get_message() if len(description) > 200: description = description[:195] + '...' artifact = fileoperations.get_config_setting('deploy', 'artifact') if artifact: file_name, file_extension = os.path.splitext(artifact) file_name = version_label + file_extension file_path = artifact s3_key = None s3_bucket = None else: s3_bucket, s3_key = get_app_version_s3_location(app_name, version_label) if s3_bucket is None and s3_key is None: file_name, file_path = _zip_up_project( version_label, source_control, staged=staged) else: file_name = None file_path = None bucket = elasticbeanstalk.get_storage_location() if s3_bucket is None else s3_bucket key = app_name + '/' + file_name if s3_key is None else s3_key try: s3.get_object_info(bucket, key) io.log_info('S3 Object already exists. Skipping upload.') except NotFoundError: if file_name is None and file_path is None: raise NotFoundError('Application Version does not exist in the S3 bucket.' ' Try uploading the Application Version again.') io.log_info('Uploading archive to s3 location: ' + key) s3.upload_application_version(bucket, key, file_path) fileoperations.delete_app_versions() io.log_info('Creating AppVersion ' + version_label) return _create_application_version(app_name, version_label, description, bucket, key, process, build_config=build_config)
def create_app_version(app_name, process=False, label=None, message=None, staged=False, build_config=None): cwd = os.getcwd() fileoperations._traverse_to_project_root() try: if heuristics.directory_is_empty(): io.echo('NOTE: {}'.format(strings['appversion.none'])) return None finally: os.chdir(cwd) source_control = SourceControl.get_source_control() if source_control.untracked_changes_exist(): io.log_warning(strings['sc.unstagedchanges']) #get version_label if label: version_label = label else: version_label = source_control.get_version_label() if staged: # Make a unique version label timestamp = datetime.now().strftime("%y%m%d_%H%M%S") version_label = version_label + '-stage-' + timestamp # get description if message: description = message else: description = source_control.get_message() if len(description) > 200: description = description[:195] + '...' # Check for zip or artifact deploy artifact = fileoperations.get_config_setting('deploy', 'artifact') if artifact: file_name, file_extension = os.path.splitext(artifact) file_name = version_label + file_extension file_path = artifact s3_key = None s3_bucket = None else: # Check if the app version already exists s3_bucket, s3_key = get_app_version_s3_location( app_name, version_label) # Create zip file if the application version doesn't exist if s3_bucket is None and s3_key is None: file_name, file_path = _zip_up_project(version_label, source_control, staged=staged) else: file_name = None file_path = None # Get s3 location bucket = elasticbeanstalk.get_storage_location( ) if s3_bucket is None else s3_bucket key = app_name + '/' + file_name if s3_key is None else s3_key # Upload to S3 if needed try: s3.get_object_info(bucket, key) io.log_info('S3 Object already exists. Skipping upload.') except NotFoundError: # If we got the bucket/key from the app version describe call and it doesn't exist then # the application version must have been deleted out-of-band and we should throw an exception if file_name is None and file_path is None: raise NotFoundError( 'Application Version does not exist in the S3 bucket.' ' Try uploading the Application Version again.') # Otherwise attempt to upload the local application version io.log_info('Uploading archive to s3 location: ' + key) s3.upload_application_version(bucket, key, file_path) fileoperations.delete_app_versions() io.log_info('Creating AppVersion ' + version_label) return _create_application_version(app_name, version_label, description, bucket, key, process, build_config=build_config)
def test_cleanup_ignore_file__sc_not_specified(self): fileoperations.write_config_setting('global', 'sc', None) terminateops.cleanup_ignore_file() self.assertIsNone(fileoperations.get_config_setting('global', 'sc'))
def test_write_config_setting_standard(self): fileoperations.write_config_setting('global', 'mysetting', 'value') result = fileoperations.get_config_setting('global', 'mysetting') self.assertEqual(result, 'value')
def set_ssl(noverify): if not noverify: noverify = fileoperations.get_config_setting( 'global', 'no-verify-ssl', default=False) if noverify: aws.no_verify_ssl()