Page MenuHomeVyOS Platform

No OneTemporary

Size
7 KB
Referenced Files
None
Subscribers
None
diff --git a/op-mode-definitions/container.xml.in b/op-mode-definitions/container.xml.in
index fa66402dc..90bbc6889 100644
--- a/op-mode-definitions/container.xml.in
+++ b/op-mode-definitions/container.xml.in
@@ -1,176 +1,176 @@
<?xml version="1.0"?>
<interfaceDefinition>
<node name="add">
<children>
<node name="container">
<properties>
<help>Add container image</help>
</properties>
<children>
<tagNode name="image">
<properties>
<help>Pull a new image for container</help>
</properties>
<command>sudo podman image pull "${4}"</command>
</tagNode>
</children>
</node>
</children>
</node>
<node name="connect">
<children>
<tagNode name="container">
<properties>
<help>Attach to a running container</help>
<completionHelp>
<path>container name</path>
</completionHelp>
</properties>
<command>sudo podman exec --interactive --tty "$3" /bin/sh</command>
</tagNode>
</children>
</node>
<node name="delete">
<children>
<node name="container">
<properties>
<help>Delete container image</help>
</properties>
<children>
<tagNode name="image">
<properties>
<help>Delete container image</help>
<completionHelp>
<script>sudo podman image ls -q</script>
</completionHelp>
</properties>
<command>sudo podman image rm --force "${4}"</command>
</tagNode>
</children>
</node>
</children>
</node>
<node name="generate">
<children>
<node name="container">
<properties>
<help>Generate Container Image</help>
</properties>
<children>
<tagNode name="image">
<properties>
<help>Name of container image (tag)</help>
</properties>
<children>
<tagNode name="path">
<properties>
<help>Path to Dockerfile</help>
<completionHelp>
<list>&lt;filename&gt;</list>
</completionHelp>
</properties>
<command>sudo podman build --layers --force-rm --tag "$4" $6</command>
</tagNode>
</children>
</tagNode>
</children>
</node>
</children>
</node>
<node name="monitor">
<children>
<node name="log">
<children>
<tagNode name="container">
<properties>
<help>Monitor last lines of container logs</help>
<completionHelp>
<path>container name</path>
</completionHelp>
</properties>
<command>sudo podman logs --follow --names "$4"</command>
</tagNode>
</children>
</node>
</children>
</node>
<node name="show">
<children>
<node name="container">
<properties>
<help>Show containers</help>
</properties>
- <command>sudo podman ps --all</command>
+ <command>sudo ${vyos_op_scripts_dir}/container.py show_container</command>
<children>
<leafNode name="image">
<properties>
<help>Show container image</help>
</properties>
- <command>sudo podman image ls</command>
+ <command>sudo ${vyos_op_scripts_dir}/container.py show_image</command>
</leafNode>
<tagNode name="log">
<properties>
<help>Show logs from a given container</help>
<completionHelp>
<path>container name</path>
</completionHelp>
</properties>
<command>sudo podman logs --names "$4"</command>
</tagNode>
<leafNode name="network">
<properties>
<help>Show available container networks</help>
</properties>
- <command>sudo podman network ls</command>
+ <command>sudo ${vyos_op_scripts_dir}/container.py show_network</command>
</leafNode>
</children>
</node>
<node name="log">
<children>
<tagNode name="container">
<properties>
<help>Show logs from a given container</help>
<completionHelp>
<path>container name</path>
</completionHelp>
</properties>
<command>sudo podman logs --names "$4"</command>
</tagNode>
</children>
</node>
</children>
</node>
<node name="restart">
<children>
<tagNode name="container">
<properties>
<help>Restart a given container</help>
<completionHelp>
<path>container name</path>
</completionHelp>
</properties>
<command>sudo podman restart "$3"</command>
</tagNode>
</children>
</node>
<node name="update">
<children>
<node name="container">
<properties>
<help>Update a container image</help>
</properties>
<children>
<tagNode name="image">
<properties>
<help>Update container image</help>
<completionHelp>
<path>container name</path>
</completionHelp>
</properties>
<command>if cli-shell-api existsActive container name "$4"; then sudo podman pull $(cli-shell-api returnActiveValue container name "$4" image); else echo "Container $4 does not exist"; fi</command>
</tagNode>
</children>
</node>
</children>
</node>
</interfaceDefinition>
diff --git a/src/op_mode/container.py b/src/op_mode/container.py
new file mode 100755
index 000000000..02d4a13b7
--- /dev/null
+++ b/src/op_mode/container.py
@@ -0,0 +1,74 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2022 VyOS maintainers and contributors
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 or later as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+import json
+import sys
+
+from sys import exit
+
+from vyos.util import cmd
+
+import vyos.opmode
+
+
+def _get_json_data(command: str) -> list:
+ """
+ Get container command format JSON
+ """
+ return cmd(f'{command} --format json')
+
+
+def _get_raw_data(command: str) -> list:
+ json_data = _get_json_data(command)
+ data = json.loads(json_data)
+ return data
+
+
+def show_container(raw: bool):
+ command = 'sudo podman ps --all'
+ container_data = _get_raw_data(command)
+ if raw:
+ return container_data
+ else:
+ return cmd(command)
+
+
+def show_image(raw: bool):
+ command = 'sudo podman image ls'
+ container_data = _get_raw_data('sudo podman image ls')
+ if raw:
+ return container_data
+ else:
+ return cmd(command)
+
+
+def show_network(raw: bool):
+ command = 'sudo podman network ls'
+ container_data = _get_raw_data(command)
+ if raw:
+ return container_data
+ else:
+ return cmd(command)
+
+
+if __name__ == '__main__':
+ try:
+ res = vyos.opmode.run(sys.modules[__name__])
+ if res:
+ print(res)
+ except ValueError as e:
+ print(e)
+ sys.exit(1)

File Metadata

Mime Type
text/x-diff
Expires
Fri, Nov 6, 10:27 AM (1 h, 3 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4642505
Default Alt Text
(7 KB)

Event Timeline