def create_docker_task(templates: str = '', image_name: str = '', image_prefix: str = '', image_tag = '', container_name: str = '', service_name: str = ''): template_path_list = templates.split(':') task_template = get_docker_task_template(template_path_list, image_name) image_name = image_name if image_name != '' else 'nginx' container_name = container_name if container_name != '' else generator.get_container_name(image_name) run_task_name = 'run' + service_name.capitalize() if service_name != '' else generator.get_run_task_name(container_name) task_file_name = generator.get_task_file_name(run_task_name) task_file_content = generator.replace_str(generator.read_text(task_template), { 'zarubaRunTask': run_task_name, 'zarubaImageName': image_name, 'zarubaContainerName': container_name, }) generator.write_text(task_file_name, task_file_content) config = generator.read_config(task_file_name) run_docker_task = task.Task(config['tasks'][run_task_name]) generator.write_task_env('.', run_docker_task) generator.register_run_task(task_file_name, run_task_name) generator.register_run_container_task(task_file_name, run_task_name) if image_tag != '' or image_prefix != '': if image_tag != '': run_docker_task.set_config('imageTag', image_tag) if image_prefix != '': run_docker_task.set_config('imagePrefix', image_prefix) config['tasks'][run_task_name] = run_docker_task generator.write_config(task_file_name, config) print('Task {} is created successfully'.format(run_task_name))
def create_service_deployment(service_location: str = '', ports: str = ''): service_location = service_location if service_location != '' else '.' service_ports = [ service_port for service_port in ports.split(',') if service_port != '' ] service_name = generator.get_service_name(service_location) value_file_name = os.path.join('helm-deployments', 'values', '{}.yaml.gotmpl'.format(service_name)) value_dict = read_value_template_dict() value_dict['app']['name'] = service_name value_dict['app']['container']['image'] = service_name # add env env_dict = generator.get_env_in_location(service_location) for env_key, env_value in env_dict.items(): value_dict['app']['container']['env'].append({ 'name': env_key, 'value': env_value, }) # add ports if len(service_ports) == 0: possible_ports_env = port.get_possible_ports_env(env_dict) service_ports = list(possible_ports_env.values()) for service_port in service_ports: value_dict['app']['ports'].append({ 'containerPort': service_port, 'servicePort': service_port }) generator.write_config(value_file_name, value_dict) register_helmfile(service_name)
def update_helm_values(project_dir: str, runDockerTask: task.Task): helm_values_path = os.path.join(project_dir, 'helm-deployments', 'values') task_env = runDockerTask.get_all_env() for helm_values_root, helm_values_dir_names, helm_values_file_names in os.walk(helm_values_path): for helm_values_file_name in helm_values_file_names: abs_helm_values_file_name = os.path.join(helm_values_root, helm_values_file_name) helm_values = generator.read_config(abs_helm_values_file_name) if 'app' not in helm_values: continue if 'container' not in helm_values['app']: continue if 'image' not in helm_values['app']['container']: continue helm_values_image = helm_values['app']['container']['image'] if helm_values_image != runDockerTask.get_config('helm'): continue helm_env_list: List[Mapping[str, str]] = helm_values['app']['container']['env'] if 'env' in helm_values['app']['container'] else [] for env_key, env in task_env.items(): env_exists = False for helm_env in helm_env_list: if helm_env['name'] == env: env_exists = True pass if not env_exists: helm_env_list.append({'name': env_key, 'value': env.get_default()}) print('{yellow}Update helm values {abs_helm_values_file_name}{normal}'.format(yellow=decoration.yellow, normal=decoration.normal, abs_helm_values_file_name=abs_helm_values_file_name)) helm_values['app']['container']['env'] generator.write_config(abs_helm_values_file_name, helm_values) print('{yellow}Save helm values {abs_helm_values_file_name}{normal}'.format(yellow=decoration.yellow, normal=decoration.normal, abs_helm_values_file_name=abs_helm_values_file_name))
def update_start_service_task_env(project_dir:str): if not os.path.isabs(project_dir): project_dir = os.path.abspath(project_dir) for task_root, task_dir_names, task_file_names in os.walk(project_dir): for task_file_name in task_file_names: if not task_file_name.endswith('.zaruba.yaml'): continue task_file_name = os.path.join(task_root, task_file_name) dir_name = os.path.dirname(task_file_name) config = generator.read_config(task_file_name) if 'tasks' not in config: continue for task_name, task_dict in config['tasks'].items(): current_task = task.Task(task_dict) if current_task.get_location() == project_dir: continue if current_task.get_extend() != 'core.startService': continue print('{yellow}Update task {task_name} in {file_name}{normal}'.format(yellow=decoration.yellow, normal=decoration.normal, task_name=task_name, file_name=task_file_name)) generator.update_task_env(current_task, task_file_name) generator.write_task_env(project_dir, current_task) config['tasks'][task_name] = current_task.as_dict() generator.write_config(task_file_name, config) print('{yellow}Save updated task {task_name} in {file_name}{normal}'.format(yellow=decoration.yellow, normal=decoration.normal, task_name=task_name, file_name=task_file_name)) config = generator.read_config(task_file_name)
def register_helmfile(service_name: str): helmfile_location = os.path.join('helm-deployments', 'helmfile.yaml') helmfile_dict = generator.read_config(helmfile_location) helmfile_dict['releases'].append({ 'name': service_name, 'chart': './charts/app', 'values': ['./values/{}.yaml.gotmpl'.format(service_name)] }) generator.write_config(helmfile_location, helmfile_dict)
def add_link(source: str, destination: str, file_name='./default.values.yaml'): config = generator.read_config(file_name) print( '{yellow}Add link from "{source}" to "{destination}" on "{file_name}"{normal}' .format(yellow=decoration.yellow, normal=decoration.normal, source=source, destination=destination, file_name=file_name)) config['link::{}'.format(destination)] = source generator.write_config(file_name, config)
def create_service_task(templates: str = '', location: str = '.', service_name: str = '', service_type: str = '', ports: str = ''): template_path_list = templates.split(':') service_ports = [ service_port for service_port in ports.split(',') if service_port != '' ] location = location if location != '' else '.' service_type = service_type if service_type != '' else 'default' service_name = service_name if service_name != '' else generator.get_service_name( location) run_task_name = generator.get_run_task_name(service_name) task_location = location if os.path.isabs(location) else os.path.join( '..', location) build_image_task_name = generator.get_build_image_task_name(service_name) push_image_task_name = generator.get_push_image_task_name(service_name) run_container_task_name = generator.get_run_container_task_name( service_name) remove_container_task_name = generator.get_remove_container_task_name( service_name) task_template = get_service_task_template(template_path_list, service_type) task_file_name = generator.get_task_file_name(run_task_name) task_file_content = generator.replace_str( generator.read_text(task_template) + service_containerization_tasks, { 'zarubaRunTask': run_task_name, 'zarubaBuildImageTask': build_image_task_name, 'zarubaPushImageTask': push_image_task_name, 'zarubaRunContainerTask': run_container_task_name, 'zarubaRemoveContainerTask': remove_container_task_name, 'zarubaServiceName': service_name, 'zarubaTaskLocation': task_location, 'ZarubaServiceName': service_name.capitalize(), 'ZARUBA_ENV_PREFIX': generator.get_env_prefix(location), }) generator.write_text(task_file_name, task_file_content) config = generator.read_config(task_file_name) service_task = task.Task(config['tasks'][run_task_name]) generator.update_task_env(service_task, task_file_name) if len(service_ports) == 0: service_ports = service_task.get_possible_ports() service_task.add_lconfig_ports(service_ports) generator.write_task_env('.', service_task) config['tasks'][run_task_name] = service_task.as_dict() generator.write_config(task_file_name, config) generator.register_run_task(task_file_name, run_task_name) generator.register_build_image_task(task_file_name, build_image_task_name) generator.register_push_image_task(task_file_name, push_image_task_name) generator.register_run_container_task(task_file_name, run_container_task_name) generator.register_remove_container_task(task_file_name, remove_container_task_name) print('Task {} is created successfully'.format(run_task_name))
def create_helm_task(): main_file_name = 'main.zaruba.yaml' main_config = generator.read_config(main_file_name) if 'tasks' not in main_config: main_config['tasks'] = {} # helm apply helm_apply_task = task.Task( {}).set_icon('🚢').set_extend('core.helmApply').set_location( 'helm-deployments').set_description('Deploy helm charts') main_config['tasks']['helmApply'] = helm_apply_task.as_dict() # helm destroy helm_destroy_task = task.Task( {}).set_icon('🚢').set_extend('core.helmDestroy').set_location( 'helm-deployments').set_description('Destroy helm release') main_config['tasks']['helmDestroy'] = helm_destroy_task.as_dict() # save config generator.write_config(main_file_name, main_config)