Skip to content

Commit

Permalink
daemon: adjustments to support docker nodes using compose to identify…
Browse files Browse the repository at this point in the history
… the service they are looking to start
  • Loading branch information
bharnden committed Jan 16, 2024
1 parent 196c647 commit 6b8ad6f
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 3 deletions.
4 changes: 4 additions & 0 deletions daemon/core/api/grpc/grpcutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def add_node_data(
if isinstance(options, (DockerOptions, PodmanOptions)):
options.image = node_proto.image
options.compose = node_proto.compose
options.compose_name = node_proto.compose_name
position = Position()
position.set(node_proto.position.x, node_proto.position.y)
if node_proto.HasField("geo"):
Expand Down Expand Up @@ -307,9 +308,11 @@ def get_node_proto(
emane_model = node.wireless_model.name
image = None
compose = None
compose_name = None
if isinstance(node, (DockerNode, PodmanNode)):
image = node.image
compose = node.compose
compose_name = node.compose_name
# check for wlan config
wlan_config = session.mobility.get_configs(
node.id, config_type=BasicRangeModel.name
Expand Down Expand Up @@ -358,6 +361,7 @@ def get_node_proto(
icon=node.icon,
image=image,
compose=compose,
compose_name=compose_name,
services=services,
dir=node_dir,
channel=channel,
Expand Down
3 changes: 3 additions & 0 deletions daemon/core/api/grpc/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ class Node:
icon: str = None
image: str = None
compose: str = None
compose_name: str = None
server: str = None
geo: Geo = None
dir: str = None
Expand Down Expand Up @@ -645,6 +646,7 @@ def from_proto(cls, proto: core_pb2.Node) -> "Node":
icon=proto.icon,
image=proto.image,
compose=proto.compose,
compose_name=proto.compose_name,
server=proto.server,
geo=Geo.from_proto(proto.geo),
dir=proto.dir,
Expand Down Expand Up @@ -684,6 +686,7 @@ def to_proto(self) -> core_pb2.Node:
icon=self.icon,
image=self.image,
compose=self.compose,
compose_name=self.compose_name,
server=self.server,
dir=self.dir,
channel=self.channel,
Expand Down
23 changes: 21 additions & 2 deletions daemon/core/gui/dialogs/nodeconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ def __init__(self, app: "Application", canvas_node: "CanvasNode") -> None:
self.type: tk.StringVar = tk.StringVar(value=self.node.model)
self.container_image: tk.StringVar = tk.StringVar(value=self.node.image)
self.compose_file: tk.StringVar = tk.StringVar(value=self.node.compose)
self.compose_name: tk.StringVar = tk.StringVar(value=self.node.compose_name)
server = DEFAULT_SERVER
if self.node.server:
server = self.node.server
Expand Down Expand Up @@ -227,6 +228,7 @@ def draw(self) -> None:

overview_frame = ttk.Labelframe(frame, text="Overview", padding=FRAME_PAD)
overview_frame.grid(row=row, columnspan=2, sticky=tk.EW, pady=PADY)
overview_frame.columnconfigure(1, weight=1)
overview_row = 0
row += 1

Expand Down Expand Up @@ -263,22 +265,23 @@ def draw(self) -> None:

# container image field
if nutils.has_image(self.node.type):
# image name
label = ttk.Label(overview_frame, text="Image")
label.grid(row=overview_row, column=0, sticky=tk.EW, padx=PADX, pady=PADY)
entry = ttk.Entry(
overview_frame, textvariable=self.container_image, state=state
)
entry.grid(row=overview_row, column=1, sticky=tk.EW)
overview_row += 1

# compose file
compose_frame = ttk.Frame(overview_frame)
compose_frame.columnconfigure(0, weight=2)
compose_frame.columnconfigure(1, weight=1)
compose_frame.columnconfigure(2, weight=1)
entry = ttk.Entry(compose_frame, textvariable=self.compose_file)
entry.grid(row=0, column=0, sticky=tk.EW, padx=PADX)
button = ttk.Button(
compose_frame, text="Compose", command=self.click_compose
compose_frame, text="Compose File", command=self.click_compose
)
button.grid(row=0, column=1, sticky=tk.EW, padx=PADX)
button = ttk.Button(
Expand All @@ -289,6 +292,14 @@ def draw(self) -> None:
row=overview_row, column=0, columnspan=2, sticky=tk.EW, pady=PADY
)
overview_row += 1
# compose name
label = ttk.Label(overview_frame, text="Compose Name")
label.grid(row=overview_row, column=0, sticky=tk.EW, padx=PADX, pady=PADY)
entry = ttk.Entry(
overview_frame, textvariable=self.compose_name, state=state
)
entry.grid(row=overview_row, column=1, sticky=tk.EW)
overview_row += 1

if nutils.is_rj45(self.node):
ifaces = self.app.core.client.get_ifaces()
Expand Down Expand Up @@ -433,6 +444,14 @@ def click_apply(self) -> None:
if nutils.has_image(self.node.type):
self.node.image = self.container_image.get() or None
self.node.compose = self.compose_file.get() or None
self.node.compose_name = self.compose_name.get() or None
if not self.node.compose_name:
messagebox.showerror(
"Compose Error",
"Name required when using a compose file",
parent=self.top,
)
return
server = self.server.get()
if nutils.is_container(self.node):
if server == DEFAULT_SERVER:
Expand Down
14 changes: 13 additions & 1 deletion daemon/core/nodes/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class DockerOptions(CoreNodeOptions):
"""
Path to a compose file, if one should be used for this node.
"""
compose_name: str = None
"""
Service name to start, within the provided compose file.
"""


@dataclass
Expand Down Expand Up @@ -83,6 +87,7 @@ def __init__(
super().__init__(session, _id, name, server, options)
self.image: str = options.image
self.compose: Optional[str] = options.compose
self.compose_name: Optional[str] = options.compose_name
self.binds: list[tuple[str, str]] = options.binds
self.volumes: dict[str, DockerVolume] = {}
self.env: dict[str, str] = {}
Expand Down Expand Up @@ -193,13 +198,20 @@ def startup(self) -> None:
self.makenodedir()
hostname = self.name.replace("_", "-")
if self.compose:
if not self.compose_name:
raise CoreError(
"a compose name is required when using a compose file"
)
data = self.host_cmd(f"cat {self.compose}")
template = Template(data)
rendered = template.render_unicode(node=self, hostname=hostname)
rendered = "\\n".join(rendered.splitlines())
compose_path = self.directory / "docker-compose.yml"
self.host_cmd(f"printf '{rendered}' >> {compose_path}", shell=True)
self.host_cmd(f"{DOCKER_COMPOSE} up -d", cwd=self.directory)
self.host_cmd(
f"{DOCKER_COMPOSE} up -d {self.compose_name}",
cwd=self.directory,
)
else:
# setup commands for creating bind/volume mounts
binds = ""
Expand Down
5 changes: 5 additions & 0 deletions daemon/core/xml/corexml.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,16 @@ def add_class(self) -> None:
clazz = ""
image = ""
compose = ""
compose_name = ""
if isinstance(self.node, (DockerNode, PodmanNode)):
clazz = "docker" if isinstance(self.node, DockerNode) else "podman"
image = self.node.image
compose = self.node.compose
compose_name = self.node.compose_name
add_attribute(self.element, "class", clazz)
add_attribute(self.element, "image", image)
add_attribute(self.element, "compose", compose)
add_attribute(self.element, "compose_name", compose_name)

def add_services(self) -> None:
service_elements = etree.Element("services")
Expand Down Expand Up @@ -661,6 +664,7 @@ def read_device(self, device_element: etree.Element) -> None:
clazz = device_element.get("class")
image = device_element.get("image")
compose = device_element.get("compose")
compose_name = device_element.get("compose_name")
server = device_element.get("server")
canvas = get_int(device_element, "canvas")
node_type = NodeTypes.DEFAULT
Expand All @@ -685,6 +689,7 @@ def read_device(self, device_element: etree.Element) -> None:
if isinstance(options, (DockerOptions, PodmanOptions)):
options.image = image
options.compose = compose
options.compose_name = compose_name
# get position information
position_element = device_element.find("position")
position = None
Expand Down
1 change: 1 addition & 0 deletions daemon/proto/core/api/grpc/core.proto
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ message Node {
repeated emane.NodeEmaneConfig emane_configs = 18;
map<string, common.ConfigOption> wireless_config = 19;
string compose = 20;
string compose_name = 21;
}

message Link {
Expand Down

0 comments on commit 6b8ad6f

Please sign in to comment.