Skip to content

Commit

Permalink
init volumes
Browse files Browse the repository at this point in the history
  • Loading branch information
stavros-k committed Oct 9, 2024
1 parent 8462b81 commit d1c1630
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 10 deletions.
13 changes: 12 additions & 1 deletion library/2.0.0/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def __init__(self, render_instance, name: str, image: str):
self._network_mode: str = ""
self._entrypoint: list[str] = []
self._command: list[str] = []
self._grace_period: int | None = None
self.deploy: Deploy = Deploy(self._render_instance)
self.networks: set[str] = set()
self.devices: Devices = Devices(self._render_instance)
Expand All @@ -70,7 +71,9 @@ def _auto_set_network_mode(self):
def _resolve_image(self, image: str):
images = self._render_instance.values["images"]
if image not in images:
raise RenderError(f"Image [{image}] not found in values")
raise RenderError(
f"Image [{image}] not found in values. " f"Available images: [{', '.join(images.keys())}]"
)
repo = images[image].get("repository", "")
tag = images[image].get("tag", "")

Expand All @@ -93,6 +96,11 @@ def set_tty(self, enabled: bool = False):
def set_stdin(self, enabled: bool = False):
self._stdin_open = enabled

def set_grace_period(self, grace_period: int):
if grace_period < 0:
raise RenderError(f"Grace period [{grace_period}] cannot be negative")
self._grace_period = grace_period

def add_caps(self, caps: list[str]):
for c in caps:
if c in self._cap_add:
Expand Down Expand Up @@ -129,6 +137,9 @@ def render(self) -> dict[str, Any]:
"healthcheck": self.healthcheck.render(),
}

if self._grace_period is not None:
result["stop_grace_period"] = self._grace_period

if self._user:
result["user"] = self._user

Expand Down
5 changes: 4 additions & 1 deletion library/2.0.0/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ def add_dependency(self, name: str, condition: str):
if name in self._dependencies.keys():
raise RenderError(f"Dependency [{name}] already added")
if name not in self._render_instance.container_names():
raise RenderError(f"Dependency [{name}] not found in defined containers")
raise RenderError(
f"Dependency [{name}] not found in defined containers. "
f"Available containers: [{', '.join(self._render_instance.container_names())}]"
)
self._dependencies[name] = condition

def has_dependencies(self):
Expand Down
14 changes: 6 additions & 8 deletions library/2.0.0/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@
from .functions import Functions
from .notes import Notes
from .portal import Portals
from .volumes import Volumes
except ImportError:
from container import Container
from error import RenderError
from functions import Functions
from notes import Notes
from portal import Portals
from volumes import Volumes


class Render(object):
def __init__(self, values):
self._original_values: dict = values
self._containers: dict[str, Container] = {}
self.values: dict = copy.deepcopy(values)
self.funcs = Functions(render_instance=self).func_map()
self.portals: Portals = Portals(render_instance=self)
self.notes: Notes = Notes(render_instance=self)
self.funcs = Functions(render_instance=self).func_map()
self.volumes = Volumes(render_instance=self)

# self.volumes = {}
# self.networks = {}

def container_names(self):
Expand All @@ -36,10 +38,6 @@ def add_container(self, name: str, image: str):
self._containers[name] = container
return container

# def add_volume(self, volume):
# # TODO: Make sure no dupes are added.
# pass

def render(self):
if self.values != self._original_values:
raise RenderError("Values have been modified since the renderer was created.")
Expand All @@ -53,8 +51,8 @@ def render(self):
"services": {c._name: c.render() for c in self._containers.values()},
}

# if self.volumes:
# result["volumes"] = {volume.name: volume.render() for volume in self.volumes.values()}
if self.volumes.has_volumes():
result["volumes"] = self.volumes.render()

# if self.networks:
# result["networks"] = {...}
Expand Down
9 changes: 9 additions & 0 deletions library/2.0.0/tests/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ def test_stdin(mock_values):
assert output["services"]["test_container"]["stdin_open"] is True


def test_grace_period(mock_values):
render = Render(mock_values)
c1 = render.add_container("test_container", "test_image")
c1.set_grace_period(10)
c1.healthcheck.disable_healthcheck()
output = render.render()
assert output["services"]["test_container"]["stop_grace_period"] == 10


def test_user(mock_values):
render = Render(mock_values)
c1 = render.add_container("test_container", "test_image")
Expand Down
61 changes: 61 additions & 0 deletions library/2.0.0/volumes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
try:
from .error import RenderError
except ImportError:
from error import RenderError


class Volumes:
def __init__(self, render_instance):
self._render_instance = render_instance
self._volumes: dict[str, Volume] = {}

def has_volumes(self):
for v in self._volumes.values():
if v.is_top_level_volume():
return True
return

def add_volume(self, name: str, config: dict):
if name == "":
raise RenderError("Volume name cannot be empty")
if name in self._volumes.keys():
raise RenderError(f"Volume [{name}] already added")
self._volumes[name] = Volume(self._render_instance, name, config)

def render(self):
result: dict = {}
for v in self._volumes.values():
if v.is_top_level_volume():
result[v.get_name()] = v.render()
pass


class Volume:
def __init__(self, render_instance, name: str, config: dict):
self._render_instance = render_instance
self._generated_name: str = ""
self._volume_data: dict = {}
self._is_top_level: bool = False

self._create_volume(name, config)

def _create_volume(self, name: str, config: dict):
# Parse config
# make sure its valid
# generate a name
# update self._volume_data
# set the _is_volume when applicable
pass

# Not all volumes need to be defined
# in the top level volumes section
def is_top_level_volume(self):
return self._is_top_level

def get_name(self):
return self._generated_name

def render(self):
if self.is_top_level_volume():
return self._volume_data
return {}

0 comments on commit d1c1630

Please sign in to comment.