Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
faermanj committed Oct 12, 2023
1 parent 383350d commit 782ef6f
Show file tree
Hide file tree
Showing 17 changed files with 333 additions and 176 deletions.
Empty file added up_ansible/Containerfile
Empty file.
21 changes: 21 additions & 0 deletions up_ansible/poetry.lock

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

8 changes: 6 additions & 2 deletions up_ansible/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
[tool.poetry]
name = "up-ansible"
name = "up_ansible"
version = "0.1.0"
description = ""
authors = ["Julio Faerman <jufaerma@redhat.com>"]
readme = "README.md"

[project]
name = "up_ansible"

[tool.poetry.dependencies]
python = "^3.11"
python = "^3.12"
pluggy = "^1.3.0"


[build-system]
Expand Down
4 changes: 4 additions & 0 deletions up_ansible/up_ansible/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from up_cli import pm
from up_ansible import image_for_prompt

pm.register(image_for_prompt)
5 changes: 5 additions & 0 deletions up_ansible/up_ansible/image_for_prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from up_cli import hookimpl, match_prompt

@hookimpl
def image_for_prompt(prompt):
return match_prompt(prompt, "ansible", "cytopia/ansible")
232 changes: 231 additions & 1 deletion up_cli/poetry.lock

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions up_cli/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ description = ""
authors = ["Julio Faerman <jufaerma@redhat.com>"]
readme = "README.md"
packages = [
{ include = "up_cli", from="./src/"},
{ include = "up_splat", from="../up_splat"}
{ include = "up_cli", from="./src/"}
]

[tool.poetry.dependencies]
python = "^3.11"
pluggy = "^1.3.0"
docker = "^6.1.3"
up_ansible = { path = "../up_ansible", develop = true }
up_splat = { path = "../up_splat", develop = true }


[build-system]
Expand Down
11 changes: 10 additions & 1 deletion up_cli/src/up_cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import pluggy
from .containers import Containers

hookimpl = pluggy.HookimplMarker("up")
pm = pluggy.PluginManager("up")
pm = pluggy.PluginManager("up")
containers = Containers()

def match_prompt(prompt, head, image):
if not prompt:
return None
if prompt[0] == head:
return image
return None
31 changes: 31 additions & 0 deletions up_cli/src/up_cli/containers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import logging as log
from dataclasses import dataclass
import docker

@dataclass
class ContainerRun:
"""Specify the parameters for a container run"""
image: str
prompt: list[str]


class DockerContainers:
def run(self, run: ContainerRun):
log.debug("Running container: %s", run)
client = docker.from_env()
image = run.image
prompt = run.prompt
#TODO: Catch errors, print properly
result = client.containers.run(
image=image,
command=prompt,
auto_remove=True)
log.info("container result")
log.info("%s", result)
log.debug("Container run done")

class Containers:
delegate = DockerContainers()

def run(self, run: ContainerRun):
self.delegate.run(run)
5 changes: 2 additions & 3 deletions up_cli/src/up_cli/defaults/image_for_prompt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from up_cli import hookimpl

@hookimpl

def image_for_prompt(prompt):
print("inside Plugin_1.myhook()")
return "-".join(prompt)
return "fedora:37"
4 changes: 2 additions & 2 deletions up_cli/src/up_cli/hookspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
hookspec = pluggy.HookspecMarker("up")


@hookspec
@hookspec(firstresult=True)
def image_for_prompt(prompt):
"""My special little hook that you can customize."""
"""Present the image in which the prompt will be executed"""

23 changes: 10 additions & 13 deletions up_cli/src/up_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
import logging as log
from datetime import datetime

from up_cli import pm
from up_cli import pm, containers
from .plugins import load_plugins


# from up_splat import *
from .containers import *

def print_help():
log.debug("Please please help me")
Expand Down Expand Up @@ -49,15 +47,14 @@ def main():
def start_container(prompt):
log.info("Starting container")
log.info("PROMPT IS" + str(type(prompt)) )
results = pm.hook.image_for_prompt(prompt=prompt)
log.info(results)
if len(results) > 1:
log.warn("More than one image returned, using first")
image = results[0] if results else None
print(results)



result = pm.hook.image_for_prompt(prompt=prompt)
log.info(result)
if not result:
log.error("No image found for prompt: %s", prompt)
exit_cli("NO_IMAGE_FOR_PROMPT")
image = result
containers.run(ContainerRun(image=image, prompt=prompt))
log.info("Container started")

if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion up_cli/src/up_cli/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def load_plugins(context):
in pkgutil.iter_modules()
if name.startswith('up_')
}
log.info("Discovered plugins %s", discovered_plugins)
log.info("Discovered %s plugins: %s", len(discovered_plugins), discovered_plugins)
# create a manager and add the spec
pm.add_hookspecs(hookspec)
pm.load_setuptools_entrypoints("up")
Expand Down
139 changes: 0 additions & 139 deletions up_images/up_rh/Containerfile

This file was deleted.

11 changes: 4 additions & 7 deletions up_splat/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.poetry]
name = "up-splat"
name = "up_splat"
version = "0.1.0"
description = ""
authors = ["Julio Faerman <jufaerma@redhat.com>"]
Expand All @@ -10,13 +10,10 @@ python = "^3.12"
pluggy = "^1.3.0"


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

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



[project]
Expand Down
2 changes: 0 additions & 2 deletions up_splat/up_splat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from up_cli import pm

from up_splat import image_for_prompt

print("-- Registering plugin 2 --")
pm.register(image_for_prompt)
5 changes: 2 additions & 3 deletions up_splat/up_splat/image_for_prompt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from up_cli import hookimpl
from up_cli import hookimpl, match_prompt

@hookimpl
def image_for_prompt(prompt):
print("inside Plugin_2.myhook()")
return "*".join(prompt)
return match_prompt(prompt, "splat", "fedora:38")

0 comments on commit 782ef6f

Please sign in to comment.