def add_git_config(files): try: origin = subprocess.run([ 'git', '--git-dir={}'.format( os.path.join(files['main_dir'], '.git')), 'config', '--get', 'remote.origin.url' ], stdout=subprocess.PIPE) origin = origin.stdout.decode('utf-8')[:-1] if origin == '': subprocess.run(['git', 'init', files['main_dir']]) print( colored( "Git origin is not set for this repository. Run 'afctl config global -o <origin>'", 'yellow')) else: print("Updating git origin.") Utility.update_config(files['project_name'], {'global': { 'git': { 'origin': origin } }}) print("Setting origin as : {}".format(origin)) print( colored( "Set personal access token for Github. Run 'afctl config global -t <token>'", 'yellow')) except Exception as e: raise AfctlParserException(e)
def generate_project(args, files): try: if args.name != '.': if not os.path.exists(files['main_dir']): os.mkdir(files['main_dir']) ParserHelpers.generate_all(files) else: print("Directory already exists.") return True else: # Initialising project in existing directory project_parent_dir = Utility.find_project(os.getcwd()) if project_parent_dir is None: # Not an afctl project. Generate all directories. ParserHelpers.generate_all(files) else: # Since its an afctl project. Just populate the config files. ParserHelpers.generate_config_file(files) if args.v != None: Utility.update_config(files['project_name'], {'global': { 'airflow_version': args.v }}) return False except Exception as e: raise AfctlParserException(e)
def test_add_and_update_configs(self, create_config_file): add_config = {'name': {'key1': 'val1', 'key2': 'val2'}} Utility.add_configs(['parent', 'child1'], PROJECT_NAME, add_config) config_file = os.path.join(PROJECT_CONFIG_DIR, PROJECT_NAME) + '.yml' expected_output = """parent: child1: name: key1: val1 key2: val2 child2: null """ current_output = open(config_file).read() expected_output = expected_output.replace(" ", "") current_output = current_output.replace(" ", "") assert expected_output == current_output add_config = {'name': {'key3': 'val3', 'key4': 'val4'}} Utility.add_configs(['parent', 'child2'], PROJECT_NAME, add_config) expected_output = """parent: child1: name: key1: val1 key2: val2 child2: name: key3: val3 key4: val4 """ current_output = open(config_file).read() expected_output = expected_output.replace(" ", "") current_output = current_output.replace(" ", "") assert expected_output == current_output update_config = {'parent': {'child2': {'name': {'key3': 'val100'}}}} Utility.update_config(PROJECT_NAME, update_config) expected_output = """parent: child1: name: key1: val1 key2: val2 child2: name: key3: val100 key4: val4 """ current_output = open(config_file).read() expected_output = expected_output.replace(" ", "") current_output = current_output.replace(" ", "") assert expected_output == current_output
def act_on_configs(cls, args, project_name): try: # Setting global values. if args.type == "global": origin = args.o token = args.t version = args.v if args.o is None and args.t is None and args.v is None: origin = input("Git origin for deployment : ") token = input("Personal access token : ") version = input("Input airflow version : ") if origin != '' and origin is not None: Utility.update_config( project_name, {'global': { 'git': { 'origin': origin } }}) if token != '' and token is not None: Utility.update_config( project_name, {'global': { 'git': { 'access-token': token } }}) if version != '' and version is not None: Utility.update_config( project_name, {'global': { 'airflow_version': version }}) # If adding or updating configs. elif args.type == 'add' or args.type == 'update': if args.d is None: cls.parser.error( colored( "-d argument is required. Check usage. Run 'afctl config -h'", 'red')) # Sanitize values. configs, flag, msg = DeploymentConfig.validate_configs(args) if flag: cls.parser.error(colored(msg, 'red')) else: if args.type == 'update': Utility.update_config(project_name, configs) if args.type == 'add': Utility.add_configs(['deployment', args.d], project_name, configs) # Showing configs elif args.type == 'show': Utility.print_file(Utility.project_config(project_name)) except Exception as e: AfctlParserException(e)
def generate_dirs(cls, main_dir, project_name): try: compose_file = docker_compose_template(project_name) deployment_compose_file = "{}/deployments/{}-docker-compose.yml".format( main_dir, project_name) with open(deployment_compose_file, 'w') as file: file.write(compose_file) print("Updating docker compose.") Utility.update_config( project_name, { 'deployment': { 'local': { 'compose': "{}/deployments/{}-docker-compose.yml".format( main_dir, project_name) } } }) except Exception as e: raise AfctlDeploymentException(e)