Skip to content

Commit

Permalink
Initial "aws subs" code
Browse files Browse the repository at this point in the history
  • Loading branch information
faermanj committed Nov 17, 2023
1 parent 8de2ed3 commit b1b4ad0
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 19 deletions.
43 changes: 43 additions & 0 deletions up_aws/Containerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# docker build --no-cache --progress=plain -t caravanacloud/$(basename $PWD) -f Containerfile .
FROM fedora

# System Updates
# RUN bash -c "sudo dnf -y update"
RUN bash -c "sudo dnf -y install unzip zip pipx wget python3 python3-pip"

# AWS CLIs
RUN bash -c "curl 'https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip' -o 'awscliv2.zip' && unzip awscliv2.zip \
&& sudo ./aws/install \
&& aws --version \
"

# RUN bash -c "npm install -g aws-cdk"

# ARG SAM_URL="https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-linux-x86_64.zip"
# RUN bash -c "curl -Ls '${SAM_URL}' -o '/tmp/aws-sam-cli-linux-x86_64.zip' \
# && unzip '/tmp/aws-sam-cli-linux-x86_64.zip' -d '/tmp/sam-installation' \
# && sudo '/tmp/sam-installation/install' \
# && sam --version"

# RUN bash -c "pip install cloudformation-cli cloudformation-cli-java-plugin cloudformation-cli-go-plugin cloudformation-cli-python-plugin cloudformation-cli-typescript-plugin"

# COPY
# Creates user up_user inside container
ARG USER=up_user
ARG UID=1000
ARG GID=1000
RUN groupadd -g $GID $USER && \
useradd -l -u $UID -g $USER -m -d /home/$USER $USER && \
chown -R $USER:$USER /home/$USER

USER up_user
# Python Poetry
RUN bash -c "pipx install poetry && python3 -m pipx ensurepath"
RUN bash -c "/home/up_user/.local/bin/poetry --version"

COPY ./opt /opt
# RUN bash -c "find /opt"

# Done :)
RUN bash -c "echo 'done.'"

1 change: 1 addition & 0 deletions up_aws/opt/aws_utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is some sample code to run inside the container
Empty file.
14 changes: 14 additions & 0 deletions up_aws/opt/aws_utils/aws_utils/subs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def subs():
print("*** Generating Subtitles using AWS ***")
print("* Validate credentials (sts get client identity)")
print("* Find files to translate (python glob)")
print("* Find or create data bucket (aws s3)")
print("* Upload videos to bucket (aws s3)")
print("* Start and wait transcribe job")
print("* Start and wait translate job")
print("* Download translated subtitles")
print("* ...")

#main invoke
if __name__ == "__main__":
main()
114 changes: 114 additions & 0 deletions up_aws/opt/aws_utils/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions up_aws/opt/aws_utils/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[tool.poetry]
name = "aws-utils"
version = "0.1.0"
description = ""
authors = ["Julio Faerman <[email protected]>"]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.12"
boto3 = "^1.29.2"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.poetry.scripts]
subs = "aws_utils.subs:subs"
Empty file.
2 changes: 2 additions & 0 deletions up_aws/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ python = "^3.12"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"


28 changes: 23 additions & 5 deletions up_aws/up_aws/containers_for_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,34 @@
def get_homedir():
return os.environ['HOME']

def run_container(prompt):
def run_container(prompt, command=None, working_dir=None):
_command = command if command else prompt
_working_dir = working_dir if working_dir else "/"
return up.ContainerRun(
image="amazon/aws-cli",
image="caravanacloud/up_aws",
bash_wrap=False,
volumes = {
get_homedir()+"/.aws":"/root/.aws"
get_homedir()+"/.aws":{
"bind":"/root/.aws",
"mode":"ro"
}
},
command=prompt[1:])
working_dir = _working_dir,
command=_command
)

def lookup_container(prompt):
match prompt:
case ["subs", *rest]:
return run_container(rest,
working_dir="/opt/aws_utils/",
command=["/home/up_user/.local/bin/poetry", "run", "subs"])
return run_container(prompt)


@up.hookimpl
def containers_for_prompt(prompt):
return up.if_prompt_matches(run_container, prompt, "aws")
match prompt:
case ["aws", *rest]:
return lookup_container(rest)
return None
13 changes: 12 additions & 1 deletion uplib/uplib/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class ContainerRun:
volumes: dict[str, str] = field(kw_only=True, default_factory=dict)
auto_remove: bool = field(kw_only=True, default=True)
network_mode: str = field(kw_only=True, default="host")
# TODO: user
working_dir: str = field(kw_only=True, default="/")
# Feature Flags
bash_wrap: bool = field(kw_only=True, default=False)

Expand All @@ -45,7 +47,9 @@ def volumes_of(cls, run:ContainerRun):
"mode": "rw"
}
}
result = settings_vols | default_vols
print(type(run.volumes))
print(run.volumes)
result = run.volumes | settings_vols | default_vols
return result

def run(self, run: ContainerRun):
Expand All @@ -60,6 +64,8 @@ def run(self, run: ContainerRun):
name = run.name if run.name else generate_container_name(run)
volumes = DockerContainers.volumes_of(run)
ports = settings_maps.get("ports")
user = "up_user"
working_dir = run.working_dir
console = Console()
console.log(f"Running container: {name}")
console.log({
Expand All @@ -69,6 +75,8 @@ def run(self, run: ContainerRun):
"auto_remove": run.auto_remove,
"volumes": volumes,
"ports": ports,
"user": user,
"working_dir": working_dir,
"detach": True
})
try:
Expand All @@ -79,6 +87,8 @@ def run(self, run: ContainerRun):
auto_remove=run.auto_remove,
volumes=volumes,
ports=ports,
user=user,
working_dir=working_dir,
detach=True
)
for line in container.logs(stream=True):
Expand All @@ -91,6 +101,7 @@ def run(self, run: ContainerRun):
log.error("Failed to run container")
log.debug("%s", run)
log.error("%s", e)
raise e

_container_name_pattern = '[^a-zA-Z0-9_.-]'
def generate_container_name(run):
Expand Down
18 changes: 5 additions & 13 deletions uplib/uplib/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def up_main(context: Context, prompt: Prompt):
if not container_runs:
log.error("No containers found, using defaults")
container_runs = [default_container(prompt)]
for container in container_runs:
containers.run(container)
for container_run in container_runs:
containers.run(container_run)


def default_container(prompt):
Expand All @@ -33,18 +33,10 @@ def default_container(prompt):

def containers_for_prompt(prompt) -> list[ContainerRun]:
from_plugins = containers_from_plugins(prompt)
from_configs = containers_from_dynaconf(prompt)
result = from_plugins + from_configs
return result


def containers_from_dynaconf(prompt: list[str]) -> list[ContainerRun]:
return []

return from_plugins

def containers_from_plugins(prompt: list[str]) -> list[ContainerRun]:
results = pm.hook.containers_for_prompt(prompt=prompt)
result = sum(results, [])
result = pm.hook.containers_for_prompt(prompt=prompt)
if not result:
result = []
return []
return result

0 comments on commit b1b4ad0

Please sign in to comment.