Commit f413d47e authored by harshavardhan.c's avatar harshavardhan.c

Dev: Added functionality to register new module for docker environment.

parent 4db17f49
import argparse
import json
import sys
from dotenv import load_dotenv
from docker_automation.docker_automate_script import DockerVersionUpgrade
load_dotenv()
from docker_automation.docker_register import DockerRegistration
from scripts.config import EnvironmentVariables
from scripts.logging import logger
default_link = EnvironmentVariables.default_link
git_user_name = EnvironmentVariables.git_user_name
git_access_token = EnvironmentVariables.git_access_token
config_variables = EnvironmentVariables.config_variables
helm_repo = EnvironmentVariables.helm_repo
global_configmap = EnvironmentVariables.global_configmap
ap = argparse.ArgumentParser()
def func_mapper(operation_type, parameters):
func_mapper_dict = {
"docker_registration": DockerRegistration(arguments=parameters, git_user_name=git_user_name,
git_access_token=git_access_token).module_registration,
"docker_version_upgrade": DockerVersionUpgrade(arguments=parameters, git_user_name=git_user_name,
git_access_token=git_access_token).module_registration
}
return func_mapper_dict.get(operation_type)
if __name__ == '__main__':
ap.add_argument(
"--ilens_version",
"-iv",
required=False,
default=None,
help="ILens Version Tag",
)
ap.add_argument(
"--release_version",
"-rv",
required=False,
default=None,
help="ILens Release Tag",
)
ap.add_argument(
"--reference_client_name",
"-rcn",
required=False,
default=None,
help="Client Name Tag"
)
ap.add_argument(
"--client_name",
"-cn",
required=False,
default=None,
help="Client Name Tag"
)
ap.add_argument(
"--branch_name",
"-b",
required=False,
default=None,
help="Branch Name"
)
ap.add_argument(
"--git_repos",
"-gr",
required=False,
default=None,
help="Git repos to be added in docker compose",
nargs="+"
)
ap.add_argument(
"--module_names",
"-mn",
required=False,
default=None,
help="Module names to be added in docker compose",
nargs="+"
)
ap.add_argument(
"--repo_info",
"-ri",
required=False,
default=None,
help="Module names to be added in docker compose",
nargs="+"
)
ap.add_argument(
"--execution_type",
"-et",
required=False,
default=None,
help="Type of Execution will be selected.."
)
try:
arguments = vars(ap.parse_args())
_ilens_version = arguments["ilens_version"]
_release_version = arguments["release_version"]
_client_name = arguments['client_name']
_reference_branch = arguments['reference_client_name']
_git_repos = arguments["git_repos"]
_module_names = arguments["module_names"]
_branch_name = arguments['branch_name'] or "master"
if repo_info := arguments["repo_info"]:
repo_info = json.loads(repo_info)
func_type = arguments['execution_type']
temp_func = func_mapper(operation_type=func_type, parameters=arguments)
if not temp_func:
logger.exception('Execution Type is not Found')
sys.exit()
res = temp_func()
except Exception as e:
logger.exception(f'Exception Occurred while performing automation script preparation {e.args}')
if __name__ == "__main__":
from dotenv import load_dotenv
load_dotenv()
import argparse
import os
import sys
import time
from scripts.core import ILensVersionHandler
from scripts.core.docker_handler import DockerHandler
from scripts.core.git_handler import GitHandler
from scripts.logging import logging
from scripts.utils.common_utils import CommonUtils
common_util = CommonUtils()
variables_file_path = "docker-compose.yml"
_module_data = common_util.convert_yaml_to_define_obj(variables_file_path)
default_link = "https://gitlab-pm.knowledgelens.com/"
git_user_name = os.environ.get("GIT_USERNAME", default="harshavardhan.c")
git_access_token = os.environ.get("GIT_TOKEN", default="FEMA6PnP63fJCs6DrtZJ")
config_variables = os.environ.get("CONFIG_MAP_VARIABLES", default="").split(",")
docker_repo = os.environ.get("DOCKER_REPO",
default="https://gitlab-pm.knowledgelens.com/KnowledgeLens/Products/iLens-2.0/core/devops/docker-compose.git")
global_configmap = os.environ.get("GLOBAL_VARIABLES_FILE", default="ilens-env-spec-variables.yml")
git_handler_obj = GitHandler(user_name=git_user_name, access_token=git_access_token)
# for module in _module_data:
print(_module_data)
ap = argparse.ArgumentParser()
db_handler = ILensVersionHandler()
docker_handler = DockerHandler(git_user_name=git_user_name, git_access_token=git_access_token)
if __name__ == '__main__':
ap.add_argument(
"--ilens_version",
"-iv",
required=False,
default=None,
help="ILens Version Tag",
)
ap.add_argument(
"--release_version",
"-rv",
required=False,
default=None,
help="ILens Release Tag",
)
ap.add_argument(
"--client_name",
"-cn",
required=False,
default=None,
help="Client Name Tag"
)
ap.add_argument(
"--branch_name",
"-b",
required=False,
default=None,
help="Branch Name"
)
ap.add_argument(
"--git_repos",
"-gr",
required=False,
default=None,
help="Git repos to be added in helm",
nargs="+"
)
ap.add_argument(
"--module_names",
"-mn",
required=False,
default=None,
help="Module names to be added in helm",
nargs="+"
)
DOCKER_TEMP_PATH = f"{int(time.time())}_docker_tmp_path"
GENERAL_TEMP_PATH = f"{int(time.time())}_tmp"
OUTPUT_PATH = f"{int(time.time())}_docker-compose"
try:
arguments = vars(ap.parse_args())
_release_version = arguments["release_version"]
_client_name = arguments['client_name']
_git_repos = arguments["git_repos"]
_module_names = arguments["module_names"]
_branch_name = arguments['branch_name'] or "master"
_ilens_version = arguments["ilens_version"]
if not _ilens_version or not _release_version or not _client_name or not (
_git_repos or _module_names) or not global_configmap:
print(
"global_configmap, git_repos, module_names, client_name, ilens_version and release_version details not found!!!!!")
sys.exit()
_branch = f"{_client_name}_{_ilens_version}.{_release_version}"
variables_file = "variables.yml"
if not os.path.exists(DOCKER_TEMP_PATH):
os.makedirs(DOCKER_TEMP_PATH)
if not os.path.exists(OUTPUT_PATH):
os.makedirs(OUTPUT_PATH)
docker_compose_path = os.path.join(DOCKER_TEMP_PATH, "docker-compose")
if not git_handler_obj.clone_repository(repo_link=docker_repo, module_output_path=docker_compose_path,
clone_branch=_client_name):
logging.error(f"Cannot clone helm repo with branch: {_client_name}")
sys.exit()
files_info = os.listdir(docker_compose_path)
sorted_files = list(filter(lambda f: f.endswith(".yml") and "docker" in f, files_info))
global_config_data = common_util.convert_yaml_to_define_obj(os.path.join(docker_compose_path, global_configmap))
for _file in sorted_files:
docker_compose_data = common_util.convert_yaml_to_define_obj(os.path.join(docker_compose_path, _file),
load_type=None)
service_dict = docker_compose_data.get("services")
if not service_dict:
logging.debug(f'Services not found for current docker compose file - {_file}')
if response_data := docker_handler.process_module_request(compose_data=docker_compose_data,
variables_file=variables_file,
general_path=GENERAL_TEMP_PATH, **arguments):
compose_out_file_path = os.path.join(OUTPUT_PATH, _file)
common_util.convert_json_to_yaml(json_data=response_data, output_file_path=compose_out_file_path)
git_handler_obj.push_deployments_to_git(repo_link=docker_repo, private_token=git_access_token, branch=_branch,
folder_path=OUTPUT_PATH,helm_deployment=False,
base_path=docker_compose_path)
git_handler_obj.create_merge_request(repo_link=docker_repo, source_branch=_branch,
destination_branch=_client_name)
except Exception as e:
logging.error(f'Exception occurred while preparing the docker compose {e.args}')
from scripts.utils.common_utils import CommonUtils
if __name__ == "__main__":
from dotenv import load_dotenv
load_dotenv()
import os
import sys
import time
from scripts.config import EnvironmentVariables
from scripts.core import ILensVersionHandler
from scripts.core.docker_handler import DockerHandler
from scripts.core.git_handler import GitHandler
from scripts.logging import logging
docker_repo = EnvironmentVariables.docker_repo
global_configmap = EnvironmentVariables.global_configmap
class DockerVersionUpgrade:
def __init__(self, arguments: dict, git_user_name: str, git_access_token: str):
self.arguments = arguments
self.db_handler = ILensVersionHandler()
self.docker_handler = DockerHandler(git_user_name=git_user_name, git_access_token=git_access_token)
self.git_handler_obj = GitHandler(user_name=git_user_name, access_token=git_access_token)
self.common_util = CommonUtils()
def module_registration(self):
docker_temp_path = f"{int(time.time())}_docker_tmp_path"
general_temp_path = f"{int(time.time())}_tmp"
output_path = f"{int(time.time())}_docker-compose"
try:
_release_version = self.arguments["release_version"]
_client_name = self.arguments['client_name']
_git_repos = self.arguments["git_repos"]
_module_names = self.arguments["module_names"]
_branch_name = self.arguments['branch_name'] or "master"
_ilens_version = self.arguments["ilens_version"]
if not _ilens_version or not _release_version or not _client_name or not _git_repos and not _module_names or not global_configmap:
print(
"global_configmap, git_repos, module_names, client_name, ilens_version and release_version details "
"not found!!!!!")
sys.exit()
variables_file = "variables.yml"
if not os.path.exists(docker_temp_path):
os.makedirs(docker_temp_path)
if not os.path.exists(output_path):
os.makedirs(output_path)
docker_compose_path = os.path.join(docker_temp_path, "docker-compose")
_branch = f"{_client_name}_{_ilens_version}.{_release_version}"
if not self.git_handler_obj.clone_repository(repo_link=docker_repo, module_output_path=docker_compose_path,
clone_branch=_client_name):
logging.error(f"Cannot clone docker repo with branch: {_client_name}")
sys.exit()
self.docker_handler.process_compose_data_for_existing_files(destination_branch=_client_name,
source_branch=_branch,
variables_file=variables_file,
arguments=self.arguments,
tmp_path=general_temp_path,
output_path=output_path,
docker_compose_path=docker_compose_path)
except Exception as e:
logging.error(f'Exception occurred while preparing the docker compose {e.args}')
import json
if __name__ == "__main__":
from dotenv import load_dotenv
load_dotenv()
import os
import sys
import time
from scripts.core import ILensVersionHandler
from scripts.core.docker_handler import DockerHandler
from scripts.core.git_handler import GitHandler
from scripts.logging import logging
from scripts.utils.common_utils import CommonUtils
docker_repo = os.environ.get("DOCKER_REPO",
default="https://gitlab-pm.knowledgelens.com/KnowledgeLens/Products/iLens-2.0/core/devops/docker-compose.git")
global_configmap = os.environ.get("GLOBAL_VARIABLES_FILE", default="ilens-env-spec-variables.yml")
class DockerRegistration:
def __init__(self, arguments: dict, git_user_name: str, git_access_token: str):
self.arguments = arguments
self.db_handler = ILensVersionHandler()
self.docker_handler = DockerHandler(git_user_name=git_user_name, git_access_token=git_access_token)
self.git_handler_obj = GitHandler(user_name=git_user_name, access_token=git_access_token)
self.common_util = CommonUtils()
def module_registration(self):
docker_temp_path = f"{int(time.time())}_docker_tmp_path"
general_temp_path = f"{int(time.time())}_tmp"
output_path = f"{int(time.time())}_docker-compose"
try:
_ilens_version = self.arguments["ilens_version"]
_release_version = self.arguments["release_version"]
_client_name = self.arguments['client_name']
_reference_branch = self.arguments['reference_client_name']
_git_repos = self.arguments["git_repos"]
_module_names = self.arguments["module_names"]
_branch_name = self.arguments['branch_name'] or "master"
if repo_info := self.arguments["repo_info"]:
repo_info = json.loads(repo_info)
if not _ilens_version or not _release_version or not _client_name or not _git_repos and not _module_names or not global_configmap:
print(
"global_configmap, git_repos, module_names, client_name, ilens_version and release_version details not found!!!!!")
sys.exit()
_branch = f"{_client_name}_{_ilens_version}.{_release_version}"
if not os.path.exists(docker_temp_path):
os.makedirs(docker_temp_path)
if not os.path.exists(output_path):
os.makedirs(output_path)
docker_compose_path = os.path.join(docker_temp_path, "docker-compose")
if not self.git_handler_obj.clone_repository(repo_link=docker_repo, module_output_path=docker_compose_path,
clone_branch=_reference_branch or _client_name):
logging.error(f"Cannot clone docker repo with branch: {_reference_branch or _client_name}")
docker_compose_path = os.path.join(docker_temp_path, "docker-compose")
variables_file = "variables.yml"
if os.path.exists(docker_compose_path):
self.docker_handler.process_compose_data_for_existing_files(destination_branch=_client_name,
source_branch=_branch,
variables_file=variables_file,
arguments=self.arguments,
tmp_path=general_temp_path,
output_path=output_path,
docker_compose_path=docker_compose_path)
else:
self.docker_handler.process_compose_data_by_template(variables_file=variables_file,
arguments=self.arguments, compose_info=repo_info,
tmp_path=general_temp_path,
output_path=output_path, modules=_module_names)
except Exception as e:
logging.exception(f"Exception Occurred while generating the docker compose file {e.args}")
...@@ -15,13 +15,6 @@ from scripts.db.psql.databases import get_db_for_func ...@@ -15,13 +15,6 @@ from scripts.db.psql.databases import get_db_for_func
from scripts.schemas import GetRequest from scripts.schemas import GetRequest
from scripts.utils.common_utils import CommonUtils from scripts.utils.common_utils import CommonUtils
default_link = "https://gitlab-pm.knowledgelens.com/"
git_user_name = os.environ.get("GIT_USERNAME", default="harshavardhan.c")
git_access_token = os.environ.get("GIT_TOKEN", default="FEMA6PnP63fJCs6DrtZJ")
config_variables = os.environ.get("CONFIG_MAP_VARIABLES", default="").split(",")
helm_repo = os.environ.get("HELM_REPO", default="")
global_configmap = os.environ.get("GLOBAL_VARIABLES_FILE", default="ilens-env-spec-variables.yml")
HELM_PATH = "/ilens-core/ilens-modules" HELM_PATH = "/ilens-core/ilens-modules"
HELM_STORE_PATH = "./helm-charts" HELM_STORE_PATH = "./helm-charts"
...@@ -114,7 +107,7 @@ if __name__ == '__main__': ...@@ -114,7 +107,7 @@ if __name__ == '__main__':
# global_config_data = common_util.convert_yaml_to_json("ilens-global-configmap.yml") # global_config_data = common_util.convert_yaml_to_json("ilens-global-configmap.yml")
variables_file = "variables.yml" variables_file = "variables.yml"
template_path = os.path.join(GENERAL_TEMP_PATH, "templates") template_path = os.path.join(GENERAL_TEMP_PATH, "../templates")
if not os.path.exists(template_path): if not os.path.exists(template_path):
os.makedirs(template_path) os.makedirs(template_path)
for _module in _module_names: for _module in _module_names:
......
import json
from copy import deepcopy from copy import deepcopy
import shortuuid from scripts.config import EnvironmentVariables
if __name__ == "__main__": if __name__ == "__main__":
from dotenv import load_dotenv from dotenv import load_dotenv
...@@ -20,12 +21,12 @@ from scripts.core.git_handler import GitHandler ...@@ -20,12 +21,12 @@ from scripts.core.git_handler import GitHandler
from scripts.core.helm_handler import HelmHandler from scripts.core.helm_handler import HelmHandler
from scripts.utils.common_utils import CommonUtils from scripts.utils.common_utils import CommonUtils
default_link = "https://gitlab-pm.knowledgelens.com/" default_link = EnvironmentVariables.default_link
git_user_name = os.environ.get("GIT_USERNAME", default="harshavardhan.c") git_user_name = EnvironmentVariables.git_user_name
git_access_token = os.environ.get("GIT_TOKEN", default="FEMA6PnP63fJCs6DrtZJ") git_access_token = EnvironmentVariables.git_access_token
config_variables = os.environ.get("CONFIG_MAP_VARIABLES", default="").split(",") config_variables = EnvironmentVariables.config_variables
helm_repo = os.environ.get("HELM_REPO", default="") helm_repo = EnvironmentVariables.helm_repo
global_configmap = os.environ.get("GLOBAL_VARIABLES_FILE", default="ilens-env-spec-variables.yml") global_configmap = EnvironmentVariables.global_configmap
HELM_PATH = "/ilens-core/ilens-modules" HELM_PATH = "/ilens-core/ilens-modules"
HELM_STORE_PATH = "./helm-charts" HELM_STORE_PATH = "./helm-charts"
...@@ -109,6 +110,8 @@ if __name__ == '__main__': ...@@ -109,6 +110,8 @@ if __name__ == '__main__':
repo_info = arguments["repo_info"] repo_info = arguments["repo_info"]
_module_names = arguments["module_names"] _module_names = arguments["module_names"]
_branch_name = arguments['branch_name'] or "master" _branch_name = arguments['branch_name'] or "master"
if repo_info:
repo_info = json.loads(repo_info)
if not _ilens_version or not _release_version or not _client_name or not ( if not _ilens_version or not _release_version or not _client_name or not (
_git_repos or _module_names) or not global_configmap: _git_repos or _module_names) or not global_configmap:
print( print(
...@@ -130,13 +133,13 @@ if __name__ == '__main__': ...@@ -130,13 +133,13 @@ if __name__ == '__main__':
sorted_files = list(filter(lambda f: f.endswith(".yml"), files_info)) sorted_files = list(filter(lambda f: f.endswith(".yml"), files_info))
_module_names = [_each.replace(".yml", "") for _each in sorted_files] _module_names = [_each.replace(".yml", "") for _each in sorted_files]
global_config_data = common_util.convert_yaml_to_define_obj(os.path.join(base_helm_directory_path, global_configmap)) global_config_data = common_util.convert_yaml_to_define_obj(
os.path.join(base_helm_directory_path, global_configmap))
# global_config_data = common_util.convert_yaml_to_json("ilens-global-configmap.yml") # global_config_data = common_util.convert_yaml_to_json("ilens-global-configmap.yml")
if repo_info: if repo_info:
global_config_data.update(repo_info) global_config_data.update(repo_info)
shortuuid.uuid()
variables_file = "variables.yml" variables_file = "variables.yml"
template_path = os.path.join(GENERAL_TEMP_PATH, "templates") template_path = os.path.join(GENERAL_TEMP_PATH, "../templates")
if not os.path.exists(template_path): if not os.path.exists(template_path):
os.makedirs(template_path) os.makedirs(template_path)
for _module in _module_names: for _module in _module_names:
...@@ -174,14 +177,15 @@ if __name__ == '__main__': ...@@ -174,14 +177,15 @@ if __name__ == '__main__':
new_module = deepcopy(_module) new_module = deepcopy(_module)
worker_type = True if "worker" in _module or "celery" in _module else False worker_type = True if "worker" in _module or "celery" in _module else False
if not os.path.exists(existing_yml_path): if not os.path.exists(existing_yml_path):
existing_yml_path = os.path.join("templates", "helm_deployment.yml") existing_yml_path = os.path.join("../templates", "helm_deployment.yml")
new_module = "helm_deployment" new_module = "helm_deployment"
helm_handler.create_helm_deployment_file(template_yml_path=existing_yml_path, image_tag=image_url, helm_handler.create_helm_deployment_file(template_yml_path=existing_yml_path, image_tag=image_url,
module_env_variables=module_env_variables, module_env_variables=module_env_variables,
helm_out_file_path=helm_out_file_path, helm_out_file_path=helm_out_file_path,
global_config_data=global_config_data, module_name=new_module) global_config_data=global_config_data, module_name=new_module)
else: else:
helm_handler.create_existing_helm_deployment_file(template_yml_path=existing_yml_path, image_tag=image_url, helm_handler.create_existing_helm_deployment_file(template_yml_path=existing_yml_path,
image_tag=image_url,
module_env_variables=module_env_variables, module_env_variables=module_env_variables,
helm_out_file_path=helm_out_file_path, helm_out_file_path=helm_out_file_path,
global_config_data=global_config_data, global_config_data=global_config_data,
......
...@@ -41,6 +41,17 @@ class DBConf: ...@@ -41,6 +41,17 @@ class DBConf:
class Logging: class Logging:
level = config.get("LOGGING", "level", fallback="DEBUG") level = config.get("LOGGING", "level", fallback="DEBUG")
level = level if level else "INFO" level = level or "INFO"
tb_flag = config.getboolean("LOGGING", "traceback", fallback=True) tb_flag = config.getboolean("LOGGING", "traceback", fallback=True)
tb_flag = tb_flag if tb_flag is not None else True tb_flag = tb_flag if tb_flag is not None else True
class EnvironmentVariables:
default_link = os.environ.get("DEFAULT_GIT_LINK", default="https://gitlab-pm.knowledgelens.com/")
git_user_name = os.environ.get("GIT_USERNAME", default="harshavardhan.c")
git_access_token = os.environ.get("GIT_TOKEN", default="FEMA6PnP63fJCs6DrtZJ")
config_variables = os.environ.get("CONFIG_MAP_VARIABLES", default="").split(",")
docker_repo = os.environ.get("DOCKER_REPO",
default="https://gitlab-pm.knowledgelens.com/KnowledgeLens/Products/iLens-2.0/core/devops/docker-compose.git")
global_configmap = os.environ.get("GLOBAL_VARIABLES_FILE", default="ilens-env-spec-variables.yml")
helm_repo = os.environ.get("HELM_REPO", default="")
import os import os
from scripts.config import EnvironmentVariables
from scripts.core import ILensVersionHandler from scripts.core import ILensVersionHandler
from scripts.core.git_handler import GitHandler from scripts.core.git_handler import GitHandler
from scripts.logging import logging from scripts.logging import logging
from scripts.schemas import DockerComposeSchema
from scripts.utils.common_utils import CommonUtils from scripts.utils.common_utils import CommonUtils
...@@ -34,7 +36,7 @@ class DockerHandler: ...@@ -34,7 +36,7 @@ class DockerHandler:
clone_branch=branch_name, clone_branch=branch_name,
file_output_path=variables_file_path, file_output_path=variables_file_path,
clone_file_path=variables_file): clone_file_path=variables_file):
logging.debug("Failed to clone module!! Skipping Helm File Preparation") logging.debug("Failed to clone module!! Skipping Docker File Preparation")
continue continue
_module_data = self.common_util.convert_yaml_to_define_obj(variables_file_path) _module_data = self.common_util.convert_yaml_to_define_obj(variables_file_path)
# session_obj = get_db_for_func() # session_obj = get_db_for_func()
...@@ -65,3 +67,59 @@ class DockerHandler: ...@@ -65,3 +67,59 @@ class DockerHandler:
except Exception as e: except Exception as e:
logging.exception(f'Exception occurred while process each module {e.args}') logging.exception(f'Exception occurred while process each module {e.args}')
return False return False
def process_compose_data_for_existing_files(self, docker_compose_path, variables_file, arguments,
tmp_path, output_path, source_branch, destination_branch):
docker_repo = EnvironmentVariables.docker_repo
global_configmap = EnvironmentVariables.global_configmap
try:
files_info = os.listdir(docker_compose_path)
sorted_files = list(filter(lambda f: f.endswith(".yml") and "docker" in f, files_info))
global_config_data = self.common_util.convert_yaml_to_define_obj(
os.path.join(docker_compose_path, global_configmap))
for _file in sorted_files:
docker_compose_data = self.common_util.convert_yaml_to_define_obj(
os.path.join(docker_compose_path, _file),
load_type=None)
service_dict = docker_compose_data.get("services")
if not service_dict:
logging.debug(f'Services not found for current docker compose file - {_file}')
if response_data := self.process_module_request(compose_data=docker_compose_data,
variables_file=variables_file,
general_path=tmp_path, **arguments):
compose_out_file_path = os.path.join(output_path, _file)
self.common_util.convert_json_to_yaml(json_data=response_data,
output_file_path=compose_out_file_path)
self.git_handler_obj.push_deployments_to_git(repo_link=docker_repo,
private_token=EnvironmentVariables.git_access_token,
branch=source_branch,
folder_path=output_path, helm_deployment=False,
base_path=docker_compose_path)
self.git_handler_obj.create_merge_request(repo_link=docker_repo, source_branch=source_branch,
destination_branch=destination_branch)
except Exception as e:
logging.exception(f'Exception Occurred while processing the compose data info {e.args}')
def process_compose_data_by_template(self, modules, compose_info: dict, variables_file, arguments,
tmp_path, output_path):
compose_dict = {"services": {}}
try:
for module in modules:
compose_dict['services'].update({
module: DockerComposeSchema().dict()
})
compose_out_file_path = os.path.join(output_path, "docker-compose.yml")
jinja_template_file = 'docker_deployment.yml'
template_path = "./templates"
compose_data = self.process_module_request(compose_data=compose_dict,
variables_file=variables_file,
general_path=tmp_path, **arguments)
self.common_util.render_helm_chart(data_dict={"modules": compose_data.get('services', {})},
jinja_template_file=jinja_template_file, template_path=template_path,
outfile_path=compose_out_file_path)
except Exception as e:
logging.exception(f'Exception Occurred while processing the compose data by template {e.args}')
import os import os
import jinja2
from scripts.logging import logging from scripts.logging import logging
from scripts.utils.common_utils import CommonUtils from scripts.utils.common_utils import CommonUtils
...@@ -35,7 +33,7 @@ class HelmHandler: ...@@ -35,7 +33,7 @@ class HelmHandler:
existing_data['deployment']['imageName'] = image_tag or existing_data['deployment']['imageName'] existing_data['deployment']['imageName'] = image_tag or existing_data['deployment']['imageName']
self.common_utils.convert_json_to_yaml(json_data=existing_data, output_file_path=template_file) self.common_utils.convert_json_to_yaml(json_data=existing_data, output_file_path=template_file)
self.render_helm_chart(data_dict=global_config_vars, helm_template_file=f'{module_name}.yml', self.common_utils.render_helm_chart(data_dict=global_config_vars, jinja_template_file=f'{module_name}.yml',
outfile_path=helm_out_file_path, template_path=template_path, outfile_path=helm_out_file_path, template_path=template_path,
service_type=service_type) service_type=service_type)
return True return True
...@@ -43,20 +41,6 @@ class HelmHandler: ...@@ -43,20 +41,6 @@ class HelmHandler:
logging.exception(f'Exception occurred while preparing the helm deployment file {e.args}') logging.exception(f'Exception occurred while preparing the helm deployment file {e.args}')
return False return False
@staticmethod
def render_helm_chart(data_dict, helm_template_file, template_path, outfile_path, **kwargs):
try:
environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(searchpath=template_path),
trim_blocks=True,
variable_start_string='<{', variable_end_string='}>', autoescape=True)
_render = environment.get_template(helm_template_file).render(**kwargs, **data_dict)
with open(outfile_path, "w") as fp:
fp.write(_render)
except Exception as e:
logging.exception(f"Exception occurred while rendering the helm file - {e.args}")
def create_helm_deployment_file(self, template_yml_path, module_env_variables: dict, global_config_data: dict, def create_helm_deployment_file(self, template_yml_path, module_env_variables: dict, global_config_data: dict,
image_tag: str, module_name: str, helm_out_file_path: str, service_type=True): image_tag: str, module_name: str, helm_out_file_path: str, service_type=True):
try: try:
...@@ -77,9 +61,10 @@ class HelmHandler: ...@@ -77,9 +61,10 @@ class HelmHandler:
value_key = v.strip("<{ }>") value_key = v.strip("<{ }>")
global_config_vars.update({k: global_config_vars.get(value_key, v)}) global_config_vars.update({k: global_config_vars.get(value_key, v)})
self.render_helm_chart(data_dict={}, helm_template_file=f'{module_name}.yml', self.common_utils.render_helm_chart(data_dict={}, jinja_template_file=f'{module_name}.yml',
outfile_path=helm_out_file_path, template_path="templates", outfile_path=helm_out_file_path, template_path="templates",
service_type=service_type, image_url=image_tag, variables=global_config_vars, service_type=service_type, image_url=image_tag,
variables=global_config_vars,
module_port=module_port, node_port=node_port) module_port=module_port, node_port=node_port)
return True return True
except Exception as e: except Exception as e:
......
logger: logger:
name: helm-automation-script name: helm_automation-script
level: DEBUG level: DEBUG
handlers: handlers:
- type: RotatingFileHandler - type: RotatingFileHandler
...@@ -8,4 +8,4 @@ logger: ...@@ -8,4 +8,4 @@ logger:
back_up_count: 5 back_up_count: 5
- type: StreamHandler - type: StreamHandler
name: helm-automation-script name: helm_automation-script
from datetime import datetime from datetime import datetime
from typing import Optional from typing import Optional, Dict, List
from pydantic import BaseModel from pydantic import BaseModel
...@@ -32,3 +32,13 @@ class GetRequest(BaseModel): ...@@ -32,3 +32,13 @@ class GetRequest(BaseModel):
client: Optional[str] = 'iLens' client: Optional[str] = 'iLens'
ilens_version: Optional[int] ilens_version: Optional[int]
release_version: Optional[int] release_version: Optional[int]
class DockerComposeSchema(BaseModel):
image: Optional[str] = ''
container_name: Optional[str] = ''
environment: Optional[Dict] = {}
volumes: Optional[List] = []
env_file: Optional[List] = ['variables.env']
ports: Optional[List] = []
command: Optional[List] = []
import os import os
import jinja2
import ruamel.yaml import ruamel.yaml
from scripts.logging import logging from scripts.logging import logging
...@@ -33,4 +34,15 @@ class CommonUtils: ...@@ -33,4 +34,15 @@ class CommonUtils:
logging.exception(f"Exception Occurred while reading the yaml file {e.args}") logging.exception(f"Exception Occurred while reading the yaml file {e.args}")
return False return False
@staticmethod
def render_helm_chart(data_dict, jinja_template_file, template_path, outfile_path, **kwargs):
try:
environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(searchpath=template_path),
trim_blocks=True, autoescape=True)
_render = environment.get_template(jinja_template_file).render(**kwargs, **data_dict)
with open(outfile_path, "w") as fp:
fp.write(_render)
except Exception as e:
logging.exception(f"Exception occurred while rendering the helm file - {e.args}")
version: "2"
services:
{% for k,v in modules.items() %}
{{k}}:
image: {{v['image']}}
ports:
- {{v['ports']}}
environment:
{% for env_key,env_value in v['environment'].items() %}
{{env_key}}: {{env_value}}
{% endfor %}
env_file:
- {{ env_file | default("variables.yml") }}
volumes:
{% for volume in v['volumes'] %}
- {{volume}}
{% endfor %}
{% endfor %}
\ No newline at end of file
name: <{ module_name }> name: {{ module_name }}
codeType: backend codeType: backend
type: core type: core
{% if service_type %} {% if service_type %}
ports: ports:
name: port<{ module_port }> name: port{{ module_port }}
port: <{ module_port }> port: {{ module_port }}
targetPort: <{ module_port }> targetPort: {{ module_port }}
nodePort: <{ node_port }> nodePort: {{ node_port }}
{% endif %} {% endif %}
affinity: affinity:
...@@ -21,25 +21,25 @@ podAutoScaler: ...@@ -21,25 +21,25 @@ podAutoScaler:
scalePercentage: 85 scalePercentage: 85
deployment: deployment:
imageName: <{ image_url }> imageName: {{ image_url }}
command: <{ command }> command: {{ command }}
PullSecrets: <{ PullSecrets | default("ilens-azregistry") }> PullSecrets: {{ PullSecrets | default("ilens-azregistry") }}
PullPolicy: IfNotPresent <{ branch }> PullPolicy: IfNotPresent {{ branch }}
resources: resources:
requests: requests:
memory: <{ request_memory | default("250Mi") }> memory: {{ request_memory | default("250Mi") }}
cpu: <{ request_cpu | default("250m") }> cpu: {{ request_cpu | default("250m") }}
limits: limits:
memory: <{ limit_memory | default("500Mi") }> memory: {{ limit_memory | default("500Mi") }}
cpu: <{ limit_cpu | default("500m") }> cpu: {{ limit_cpu | default("500m") }}
livenessProbe: livenessProbe:
enabled: <{ enable_liveness | default(False)}> enabled: {{ enable_liveness | default(False) }}
path: <{ liveness_api }> path: {{ liveness_api }}
initialDelaySeconds: <{ liveness_delay_seconds | default(20) }> initialDelaySeconds: {{ liveness_delay_seconds | default(20) }}
timeoutSeconds: <{ liveness_timeout_seconds | default(5) }> timeoutSeconds: {{ liveness_timeout_seconds | default(5) }}
periodSeconds: <{ liveness_period_seconds | default(10) }> periodSeconds: {{ liveness_period_seconds | default(10) }}
failureThreshold: <{ liveness_failure_threshold | default(3) }> failureThreshold: {{ liveness_failure_threshold | default(3) }}
successThreshold: <{ liveness_success_threshold | default(1) }> successThreshold: {{ liveness_success_threshold | default(1) }}
environmentVar: environmentVar:
- name: MONGO_URI - name: MONGO_URI
...@@ -49,24 +49,24 @@ deployment: ...@@ -49,24 +49,24 @@ deployment:
key: MONGO_URI key: MONGO_URI
{% for k, v in variables.items() %} {% for k, v in variables.items() %}
{% if k and v %} {% if k and v %}
- name: '<{ k }>' - name: '{{ k }}'
value: '<{ v }>' value: '{{ v }}'
{% endif %} {% endif %}
{% endfor %} {% endfor %}
mountVolume: mountVolume:
enabled: <{ enableMountVolume | default(True) }> enabled: {{ enableMountVolume | default(True) }}
volumeMounts: volumeMounts:
- name: <{ mount_name | default("core-volumes") }> - name: {{ mount_name | default("core-volumes") }}
mountPath: "/code/data" mountPath: "/code/data"
volumes: volumes:
- name: <{ mount_name | default("core-volumes") }> - name: {{ mount_name | default("core-volumes") }}
persistentVolumeClaim: persistentVolumeClaim:
claimName: <{ claim_name | default("core-volumes") }> claimName: {{ claim_name | default("core-volumes") }}
{% if condition %} {% if condition %}
service: service:
name: <{ module_name }> name: {{ module_name }}
type: NodePort type: NodePort
{% endif %} {% endif %}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment