diff options
author | Christian Poessinger <christian@poessinger.com> | 2021-08-23 14:42:54 +0200 |
---|---|---|
committer | Christian Poessinger <christian@poessinger.com> | 2021-08-23 14:42:54 +0200 |
commit | a1f82a06e1a1788164f52ef291a1275568912b9b (patch) | |
tree | b02513b3e119ba44e0488f47b10a0bab007bd0da | |
parent | 501e9a9a53aac71c451a8dfbbe2508ff59740a0b (diff) | |
download | vyos-1x-a1f82a06e1a1788164f52ef291a1275568912b9b.tar.gz vyos-1x-a1f82a06e1a1788164f52ef291a1275568912b9b.zip |
container: T2216: op-mode now supports updating the image for a given container
-rw-r--r-- | op-mode-definitions/containers.xml.in | 20 | ||||
-rwxr-xr-x | src/op_mode/containers_op.py | 49 |
2 files changed, 54 insertions, 15 deletions
diff --git a/op-mode-definitions/containers.xml.in b/op-mode-definitions/containers.xml.in index e93487105..efa72e1a4 100644 --- a/op-mode-definitions/containers.xml.in +++ b/op-mode-definitions/containers.xml.in @@ -109,4 +109,24 @@ </tagNode> </children> </node> + <node name="update"> + <children> + <node name="container"> + <properties> + <help>Update a container image</help> + </properties> + <children> + <tagNode name="image"> + <properties> + <help>Delete container image</help> + <completionHelp> + <path>container name</path> + </completionHelp> + </properties> + <command>sudo ${vyos_op_scripts_dir}/containers_op.py --update "${4}"</command> + </tagNode> + </children> + </node> + </children> + </node> </interfaceDefinition> diff --git a/src/op_mode/containers_op.py b/src/op_mode/containers_op.py index 1e3fc3a8f..bc317029c 100755 --- a/src/op_mode/containers_op.py +++ b/src/op_mode/containers_op.py @@ -15,10 +15,10 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. import argparse -from vyos.configquery import query_context, ConfigQueryError -from vyos.util import cmd -config, op = query_context() +from getpass import getuser +from vyos.configquery import ConfigTreeQuery +from vyos.util import cmd parser = argparse.ArgumentParser() parser.add_argument("-a", "--all", action="store_true", help="Show all containers") @@ -26,34 +26,53 @@ parser.add_argument("-i", "--image", action="store_true", help="Show container i parser.add_argument("-n", "--networks", action="store_true", help="Show container images") parser.add_argument("-p", "--pull", action="store", help="Pull image for container") parser.add_argument("-d", "--remove", action="store", help="Delete container image") +parser.add_argument("-u", "--update", action="store", help="Update given container image") -if not config.exists(['container']): +config = ConfigTreeQuery() +base = ['container'] +if not config.exists(base): print('Containers not configured') exit(0) +if getuser() != 'root': + raise OSError('This functions needs to be run as root to return correct results!') + if __name__ == '__main__': args = parser.parse_args() if args.all: print(cmd('podman ps --all')) - exit(0) - if args.image: + + elif args.image: print(cmd('podman image ls')) - exit(0) - if args.networks: + + elif args.networks: print(cmd('podman network ls')) - exit(0) - if args.pull: + + elif args.pull: image = args.pull try: - print(cmd(f'sudo podman image pull {image}')) + print(cmd(f'podman image pull {image}')) except: print(f'Can\'t find or download image "{image}"') - exit(0) - if args.remove: + + elif args.remove: image = args.remove try: - print(cmd(f'sudo podman image rm {image}')) + print(cmd(f'podman image rm {image}')) except: print(f'Can\'t delete image "{image}"') - exit(0) + + elif args.update: + tmp = config.get_config_dict(base + ['name', args.update], + key_mangling=('-', '_'), get_first_key=True) + try: + image = tmp['image'] + print(cmd(f'podman image pull {image}')) + except: + print(f'Can\'t find or download image "{image}"') + else: + parser.print_help() + exit(1) + + exit(0) |