Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Babylon/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
from .namespace import namespace
from .macro.apply import apply
from .macro.destroy import destroy
from .macro.init import init

list_groups = [api, azure, powerbi, webapp, vault, github, namespace, apply, destroy]
list_groups = [api, azure, powerbi, webapp, vault, github, namespace, apply, destroy, init]
21 changes: 17 additions & 4 deletions Babylon/commands/macro/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from Babylon.commands.macro.deploy_solution import deploy_solution
from Babylon.commands.macro.deploy_workspace import deploy_workspace
from Babylon.commands.macro.deploy_organization import deploy_organization
from Babylon.commands.macro.deploy_runner import deploy_runner

logger = getLogger("Babylon")
env = Environment()
Expand All @@ -33,6 +34,7 @@
@option("--workspace", is_flag=True, help="Deploy or update a workspace.")
@option("--webapp", is_flag=True, help="Deploy or update a webapp.")
@option("--dataset", is_flag=True, help="Deploy or update a dataset.")
@option("--runner", is_flag=True, help="Deploy or update a runner.")
@option("--payload-only", is_flag=True, help="Specify if you want to specify payload only")
def apply(
deploy_dir: pathlib.Path,
Expand All @@ -41,6 +43,7 @@ def apply(
workspace: bool,
webapp: bool,
dataset: bool,
runner: bool,
payload_only: bool,
variables_files: Iterable[pathlib.Path],
): # type: ignore
Expand All @@ -65,9 +68,10 @@ def apply(
workspaces = list(filter(lambda x: x.get("kind") == "Workspace", resources))
webapps = list(filter(lambda x: x.get("kind") == "WebApp", resources))
datasets = list(filter(lambda x: x.get("kind") == "Dataset", resources))
runners = list(filter(lambda x: x.get("kind") == "Runner", resources))
_ret = [""]
final_datasets = []
if not (organization or solution or workspace or webapp or dataset):
if not (organization or solution or workspace or webapp or dataset or runner):
for o in organizations:
content = o.get("content")
namespace = o.get("namespace")
Expand Down Expand Up @@ -121,6 +125,12 @@ def apply(
file_content=content,
deploy_dir=deploy_dir,
payload_only=payload_only)
elif runner:
for r in runners:
content = r.get("content")
namespace = r.get("namespace")
deploy_runner(namespace=namespace, file_content=content)

elif webapp:
for swa in webapps:
content = swa.get("content")
Expand All @@ -144,11 +154,14 @@ def apply(
_ret.append(f" * Organization : {services.get('api').get('organization_id', '')}")
_ret.append(f" * Solution : {services.get('api').get('solution_id', '')}")
_ret.append(f" * Workspace : {services.get('api').get('workspace_id', '')}")
if runner:
_ret.append(f" * Runner : {services.get('api').get('runner_id', '')}")
for id in final_datasets:
_ret.append(f" * Dataset : {id}")
if services.get("webapp").get("static_domain", ""):
_ret.append(" * WebApp ")
_ret.append(f" * Hostname : https://{services.get('webapp').get('static_domain', '')}")
# TODO: When the Helm chart for the web app is implemented in Babylon !
# if services.get("webapp").get("static_domain", ""):
# _ret.append(" * WebApp ")
# _ret.append(f" * Hostname : https://{services.get('webapp').get('static_domain', '')}")
vars = env.get_variables()
current_working_directory = os.getcwd()
logfile_path = os.path.join(current_working_directory, "babylon.log")
Expand Down
21 changes: 13 additions & 8 deletions Babylon/commands/macro/deploy_organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from click import echo, style
from Babylon.utils.environment import Environment
from Babylon.utils.credentials import get_keycloak_token
from Babylon.utils.response import CommandResponse
from Babylon.commands.api.organizations.services.organization_api_svc import OrganizationService

logger = getLogger("Babylon")
Expand All @@ -20,7 +21,6 @@ def deploy_organization(namespace: str, file_content: str):
platform_url = env.get_ns_from_text(content=namespace)
state = env.retrieve_state_func(state_id=env.state_id)
state["services"]["api"]["url"] = platform_url
state['services']['azure']['tenant_id'] = env.tenant_id
content = env.fill_template(data=file_content, state=state)
keycloak_token = get_keycloak_token()
payload: dict = content.get("spec").get("payload", {})
Expand All @@ -32,12 +32,16 @@ def deploy_organization(namespace: str, file_content: str):
if not state["services"]["api"]["organization_id"]:
logger.info("[api] Creating organization")
response = organization_service.create()
if response is None:
return CommandResponse.fail()
organization = response.json()
logger.info(json.dumps(organization, indent=2))
logger.info(f"[api] Organization {organization['id']} successfully created")
else:
logger.info(f"[api] Updating organization {state['services']['api']['organization_id']}")
response = organization_service.update()
if response is None:
return CommandResponse.fail()
response_json = response.json()
old_security = response_json.get("security")
security_spec = organization_service.update_security(old_security=old_security)
Expand All @@ -49,10 +53,11 @@ def deploy_organization(namespace: str, file_content: str):
env.store_state_in_local(state)
if env.remote:
env.store_state_in_cloud(state)
run_scripts = sidecars.get("run_scripts")
if run_scripts:
data = run_scripts.get("post_deploy.sh", "")
if data:
os.system(data)
if not organization.get("id"):
sys.exit(1)
if sidecars:
run_scripts = sidecars.get("run_scripts")
if run_scripts:
data = run_scripts.get("post_deploy.sh", "")
if data:
os.system(data)
if not organization.get("id"):
sys.exit(1)
63 changes: 63 additions & 0 deletions Babylon/commands/macro/deploy_runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import os
import sys
import json

from logging import getLogger
from click import echo, style
from Babylon.utils.environment import Environment
from Babylon.utils.credentials import get_keycloak_token
from Babylon.utils.response import CommandResponse
from Babylon.commands.api.runners.services.runner_api_svc import RunnerService

logger = getLogger("Babylon")
env = Environment()


def deploy_runner(namespace: str, file_content: str):
_ret = [""]
_ret.append("Runner deployment")
_ret.append("")
echo(style("\n".join(_ret), bold=True, fg="green"))
platform_url = env.get_ns_from_text(content=namespace)
state = env.retrieve_state_func(state_id=env.state_id)
state["services"]["api"]["url"] = platform_url
content = env.fill_template(data=file_content, state=state)
keycloak_token = get_keycloak_token()
payload: dict = content.get("spec").get("payload", {})
state["services"]["api"]["runner_id"] = (payload.get("id") or state["services"]["api"]["runner_id"])
spec = dict()
spec["payload"] = json.dumps(payload, indent=2, ensure_ascii=True)
runner_service = RunnerService(keycloak_token=keycloak_token, spec=spec, state=state["services"])
sidecars = content.get("spec").get("sidecars", {})
if not state["services"]["api"]["runner_id"]:
logger.info("[api] Creating runner")
response = runner_service.create()
if response is None:
return CommandResponse.fail()
organization = response.json()
logger.info(json.dumps(organization, indent=2))
logger.info(f"[api] Runner {organization['id']} successfully created")
else:
logger.info(f"[api] Updating runner {state['services']['api']['runner_id']}")
response = runner_service.update()
if response is None:
return CommandResponse.fail()
response_json = response.json()
old_security = response_json.get("security")
security_spec = runner_service.update_security(old_security=old_security)
response_json["security"] = security_spec
organization = response_json
logger.info(json.dumps(organization, indent=2))
logger.info(f"[api] Runner {organization['id']} successfully updated")
state["services"]["api"]["runner_id"] = organization.get("id")
env.store_state_in_local(state)
if env.remote:
env.store_state_in_cloud(state)
if sidecars:
run_scripts = sidecars.get("run_scripts")
if run_scripts:
data = run_scripts.get("post_deploy.sh", "")
if data:
os.system(data)
if not organization.get("id"):
sys.exit(1)
18 changes: 9 additions & 9 deletions Babylon/commands/macro/deploy_solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def deploy_solution(namespace: str, file_content: str) -> bool:
platform_url = env.get_ns_from_text(content=namespace)
state = env.retrieve_state_func(state_id=env.state_id)
state["services"]["api"]["url"] = platform_url
state["services"]["azure"]["tenant_id"] = env.tenant_id
vars = env.get_variables()
metadata = env.get_metadata(vars=vars, content=file_content, state=state)
workspace_key = metadata.get(
Expand All @@ -43,6 +42,7 @@ def deploy_solution(namespace: str, file_content: str) -> bool:
spec = dict()
spec["payload"] = json.dumps(payload, indent=2, ensure_ascii=True)
solution_svc = SolutionService(keycloak_token=keycloak_token, spec=spec, state=state["services"])
sidecars = content.get("spec").get("sidecars", {})
if not state["services"]["api"]["solution_id"]:
logger.info("[api] Creating solution")
response = solution_svc.create()
Expand All @@ -68,11 +68,11 @@ def deploy_solution(namespace: str, file_content: str) -> bool:
if env.remote:
env.store_state_in_cloud(state)
# update run_templates
sidecars = content.get("spec").get("sidecars", {})
run_scripts = sidecars.get("run_scripts")
if run_scripts:
data = run_scripts.get("post_deploy.sh", "")
if data:
os.system(data)
if not solution.get("id"):
sys.exit(1)
if sidecars:
run_scripts = sidecars.get("run_scripts")
if run_scripts:
data = run_scripts.get("post_deploy.sh", "")
if data:
os.system(data)
if not solution.get("id"):
sys.exit(1)
25 changes: 13 additions & 12 deletions Babylon/commands/macro/deploy_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from click import echo, style
from logging import getLogger
from Babylon.utils.response import CommandResponse
from Babylon.utils.environment import Environment
from Babylon.commands.api.workspaces.services.workspaces_api_svc import WorkspaceService
from Babylon.commands.powerbi.report.service.powerbi_report_api_svc import (
Expand Down Expand Up @@ -34,7 +35,6 @@ def deploy_workspace(namespace: str, file_content: str, deploy_dir: pathlib.Path
platform_url = env.get_ns_from_text(content=namespace)
state = env.retrieve_state_func(state_id=env.state_id)
state["services"]["api"]["url"] = platform_url
state["services"]["azure"]["tenant_id"] = env.tenant_id
vars = env.get_variables()
metadata = env.get_metadata(vars=vars, content=file_content, state=state)
workspace_key = metadata.get(
Expand All @@ -59,15 +59,17 @@ def deploy_workspace(namespace: str, file_content: str, deploy_dir: pathlib.Path
if not state["services"]["api"]["workspace_id"]:
logger.info("[api] Creating workspace")
response = workspace_svc.create()
if not response:
sys.exit(1)
if response is None:
return CommandResponse.fail()
workspace = response.json()
logger.info(json.dumps(workspace, indent=2))
logger.info(f"[api] Workspace {[workspace.get('id')]} successfully created")
state["services"]["api"]["workspace_id"] = workspace.get("id")
else:
logger.info(f"[api] Updating workspace {[state['services']['api']['workspace_id']]}")
response = workspace_svc.update()
if response is None:
return CommandResponse.fail()
response_json = response.json()
old_security = response_json.get("security")
security_spec = workspace_svc.update_security(old_security=old_security)
Expand Down Expand Up @@ -247,12 +249,11 @@ def deploy_workspace(namespace: str, file_content: str, deploy_dir: pathlib.Path
specUpdated = dict()
specUpdated["payload"] = json.dumps(payloadUpdated, indent=2, ensure_ascii=True)
workspace_svc.update_with_payload(specUpdated)
run_scripts = sidecars.get("run_scripts")
if run_scripts:
data = run_scripts.get("post_deploy.sh", "")
if data:
print(data)
os.system(data)
if not workspace.get("id"):
logger.error("workspace id not found")
sys.exit(1)
if sidecars:
run_scripts = sidecars.get("run_scripts")
if run_scripts:
data = run_scripts.get("post_deploy.sh", "")
if data:
os.system(data)
if not workspace.get("id"):
sys.exit(1)
39 changes: 39 additions & 0 deletions Babylon/commands/macro/init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os
import pathlib
import shutil

from logging import getLogger
from click import command, option
from Babylon.utils.environment import Environment

logger = getLogger("Babylon")
env = Environment()


@command()
@option("--project-folder", default="project", help="Name of the project folder to create (default: 'project').")
@option("--variables-file", default="variables.yaml", help="Name of the variables file (default: 'variables.yaml').")
def init(project_folder: str, variables_file: str):
"""
Create a Babylon project structure using YAML templates.
"""
project_path = pathlib.Path(os.getcwd()) / project_folder
variables_path = pathlib.Path(os.getcwd()) / variables_file
if project_path.exists():
logger.info(f"[babylon] The directory '{project_path}' already exists")
return None
if variables_path.exists():
logger.info(f"[babylon] 'variables.yaml' already exists at '{variables_path}'")
return None
project_yaml_files = ["Organization.yaml", "Solution.yaml", "Workspace.yaml", "Dataset.yaml", "Runner.yaml"]
try:
project_path.mkdir(parents=True, exist_ok=True)
for file in project_yaml_files:
deploy_file = pathlib.Path(env.convert_template_path(f"%templates%/yaml/{file}"))
destination = project_path / file
shutil.copy(deploy_file, destination)
variables_template = pathlib.Path(env.convert_template_path("%templates%/yaml/variables.yaml"))
shutil.copy(variables_template, variables_path)
logger.info(f"\n[babylon] Project successfully initialized at: {project_path}")
except Exception as e:
logger.error(f"[babylon] Error while initializing the project: {e}")
4 changes: 2 additions & 2 deletions Babylon/commands/namespace/get_contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ def get_contexts() -> CommandResponse:
col_widths = [max(len(h), len(v)) + 2 for h, v in zip(headers, values)]
header_line = "".join(h.ljust(w) for h, w in zip(headers, col_widths))
value_line = "".join(v.ljust(w) for v, w in zip(values, col_widths))
logger.info(header_line)
logger.info(value_line)
print(header_line)
print(value_line)
return CommandResponse.success()
4 changes: 1 addition & 3 deletions Babylon/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
from pathlib import Path
from dynaconf import Dynaconf

config_files = [
'acr', 'adt', 'adx', 'api', 'app', 'keycloak', 'azure', 'babylon', 'github', 'platform', 'powerbi', 'webapp'
]
config_files = ['acr', 'api', 'keycloak', 'babylon', 'powerbi']

pwd = Path(os.getcwd())

Expand Down
28 changes: 0 additions & 28 deletions Babylon/templates/working_dir/.templates/api/connector.adt.yaml

This file was deleted.

Loading
Loading