Skip to content

Commit

Permalink
Schema validation
Browse files Browse the repository at this point in the history
  • Loading branch information
luismedel committed Nov 17, 2024
1 parent b8e9d76 commit e15a306
Show file tree
Hide file tree
Showing 14 changed files with 456 additions and 116 deletions.
7 changes: 4 additions & 3 deletions src/bluish/actions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import bluish.contexts.step
import bluish.process
from bluish.contexts import Definition
from bluish.logging import debug


Expand All @@ -15,9 +16,9 @@ def __init__(self, param: str):
super().__init__(f"Missing required attribute: {param}")


def _key_exists(key: str, values: dict) -> bool:
def _key_exists(key: str, attrs: Definition) -> bool:
"""Checks if a key (or pipe-separated alternative keys) exists in a dictionary."""
return ("|" in key and any(i in values for i in key.split("|"))) or key in values
return ("|" in key and any(i in attrs for i in key.split("|"))) or key in attrs


class Action:
Expand All @@ -35,7 +36,7 @@ def execute(
self, step: bluish.contexts.step.StepContext
) -> bluish.process.ProcessResult:
for attr in self.REQUIRED_ATTRS:
if not _key_exists(attr, step.attrs.__dict__):
if not _key_exists(attr, step.attrs):
raise RequiredAttributeError(attr)

if self.REQUIRED_INPUTS and not step.attrs._with:
Expand Down
12 changes: 6 additions & 6 deletions src/bluish/actions/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class RunCommand(bluish.actions.base.Action):
def run(
self, step: bluish.contexts.step.StepContext
) -> bluish.process.ProcessResult:
env = ChainMap(step.env, step.job.env, step.workflow.env) # type: ignore
env = ChainMap(step.env, step.parent.env, step.parent.parent.env) # type: ignore

if env:
info("env:")
Expand All @@ -39,7 +39,7 @@ def run(
if echo_commands:
info(command)

return step.job.exec(command, step, env=env, stream_output=echo_output) # type: ignore
return step.parent.exec(command, step, env=env, stream_output=echo_output) # type: ignore


class ExpandTemplate(bluish.actions.base.Action):
Expand All @@ -51,7 +51,7 @@ def run(
) -> bluish.process.ProcessResult:
inputs = step.inputs

job = cast(bluish.contexts.job.JobContext, step.job)
job = cast(bluish.contexts.job.JobContext, step.parent)

template_content: str
if "input_file" in inputs:
Expand Down Expand Up @@ -98,8 +98,8 @@ def run(

destination_file = inputs.get("destination_file")
assert destination_file is not None
job = cast(bluish.contexts.job.JobContext, step.job)

job = cast(bluish.contexts.job.JobContext, step.parent)

info(f"Writing file to: {destination_file}...")
try:
Expand Down Expand Up @@ -132,7 +132,7 @@ def run(
source_file = inputs["source_file"]
info(f"Reading file: {source_file}...")
try:
job = cast(bluish.contexts.job.JobContext, step.job)
job = cast(bluish.contexts.job.JobContext, step.parent)
raw_contents = job.read_file(source_file)
except IOError as e:
error(f"Failed to read file: {str(e)}")
Expand Down
18 changes: 9 additions & 9 deletions src/bluish/actions/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def _is_valid_docker_id(id: str) -> bool:


def run_and_get_pid(command: str, step: bluish.contexts.step.StepContext) -> str:
job = cast(bluish.contexts.job.JobContext, step.job)
job = cast(bluish.contexts.job.JobContext, step.parent)
result = job.exec(command, step)
return result.stdout.strip() if result.returncode == 0 else ""

Expand All @@ -43,7 +43,7 @@ def docker_ps(
pid: str | None = None,
) -> bluish.process.ProcessResult:
filter = f"name={name}" if name else f"id={pid}"
job = cast(bluish.contexts.job.JobContext, step.job)
job = cast(bluish.contexts.job.JobContext, step.parent)
return job.exec(f"docker ps -f {filter} --all --quiet", step)


Expand All @@ -70,7 +70,7 @@ def run(
)
info(f"Docker login:\n -> {protected_command}")

job = cast(bluish.contexts.job.JobContext, step.job)
job = cast(bluish.contexts.job.JobContext, step.parent)
login_result = job.exec(command, step, stream_output=True)
if login_result.failed:
error(f"Login failed: {login_result.error}")
Expand All @@ -86,7 +86,7 @@ def run(
command = "docker logout"
if step.get_inherited_attr("echo_commands", True):
info("Logging out of Docker...")
job = cast(bluish.contexts.job.JobContext, step.job)
job = cast(bluish.contexts.job.JobContext, step.parent)
return job.exec(command, step)


Expand All @@ -109,7 +109,7 @@ def run(
if step.get_inherited_attr("echo_commands", True):
info(f"Building image:\n -> {command}")

job = cast(bluish.contexts.job.JobContext, step.job)
job = cast(bluish.contexts.job.JobContext, step.parent)
build_result = job.exec(command, step, stream_output=True)
if build_result.failed:
error(f"Failed to build image: {build_result.error}")
Expand Down Expand Up @@ -184,7 +184,7 @@ def run(
for flag in ["quiet"]:
options += _build_flag(f"--{flag}", inputs.get(flag))

job = cast(bluish.contexts.job.JobContext, step.job)
job = cast(bluish.contexts.job.JobContext, step.parent)
run_result = job.exec(f"docker run {options} {image}", step)
if run_result.failed:
error(f"Failed to start container with image {image}: {run_result.error}")
Expand Down Expand Up @@ -216,7 +216,7 @@ def run(
remove_container = inputs.get("remove", False)
stop_container = True

job = cast(bluish.contexts.job.JobContext, step.job)
job = cast(bluish.contexts.job.JobContext, step.parent)

info(f"Stopping container with {input_attr}...")

Expand Down Expand Up @@ -328,7 +328,7 @@ def run(
if echo_commands:
info(line)

job = cast(bluish.contexts.job.JobContext, step.job)
job = cast(bluish.contexts.job.JobContext, step.parent)
result = job.exec(f"docker exec {options} {container_pid} {line}", step)
output += result.stdout

Expand All @@ -355,7 +355,7 @@ def run(
inputs = step.inputs

name = inputs["name"]
job = cast(bluish.contexts.job.JobContext, step.job)
job = cast(bluish.contexts.job.JobContext, step.parent)

info(f"Creating network {name}...")

Expand Down
6 changes: 3 additions & 3 deletions src/bluish/actions/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def run_git_command(
if key_file:
preamble = f"export GIT_SSH_COMMAND='ssh -i {key_file} -o IdentitiesOnly=yes -o StrictHostKeychecking=no';"

job = cast(bluish.contexts.job.JobContext, step.job)
job = cast(bluish.contexts.job.JobContext, step.parent)
return job.exec(f"{preamble} {command}", step)


Expand All @@ -30,7 +30,7 @@ def prepare_environment(
"openssh-client": "ssh",
}

job = cast(bluish.contexts.job.JobContext, step.job)
job = cast(bluish.contexts.job.JobContext, step.parent)

required_packages = [
package
Expand Down Expand Up @@ -93,7 +93,7 @@ def run(
# Update the current job working dir to the newly cloned repo
info(f"Setting working directory to: {repo_name}...")
wd = step.get_inherited_attr("working_directory", ".")
step.job.set_attr("working_directory", f"{wd}/{repo_name}")
step.parent.set_attr("working_directory", f"{wd}/{repo_name}") # type: ignore

return clone_result
finally:
Expand Down
2 changes: 1 addition & 1 deletion src/bluish/actions/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def run(

info(f"Installing packages {package_str}...")

job = cast(bluish.contexts.job.JobContext, step.job)
job = cast(bluish.contexts.job.JobContext, step.parent)

result = bluish.process.install_package(
job.runs_on_host, step.inputs["packages"], flavor=flavor
Expand Down
2 changes: 1 addition & 1 deletion src/bluish/actions/macos.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def run(self, step: bluish.contexts.step.StepContext) -> ProcessResult:

info(f"Installing packages {package_str}...")

job = cast(bluish.contexts.job.JobContext, step.job)
job = cast(bluish.contexts.job.JobContext, step.parent)

result = install_package(
job.runs_on_host, step.inputs["packages"], flavor=flavor
Expand Down
4 changes: 3 additions & 1 deletion src/bluish/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from flask import Flask, abort, jsonify, request

from bluish.__main__ import PROJECT_VERSION
from bluish.contexts import WorkflowDefinition
from bluish.contexts.workflow import WorkflowContext
from bluish.core import (
init_commands,
Expand Down Expand Up @@ -80,7 +81,8 @@ def workflow_from_file(file: str) -> WorkflowContext:
if not yaml_contents:
fatal("No workflow file found.")

return WorkflowContext(yaml.safe_load(yaml_contents))
definition = WorkflowDefinition(yaml.safe_load(yaml_contents))
return WorkflowContext(definition)


@click.command("blu")
Expand Down
Loading

0 comments on commit e15a306

Please sign in to comment.