Implement spoc-container list

This commit is contained in:
Disassembler 2020-02-11 16:15:06 +01:00
parent 1c889fcaac
commit e4088372f4
No known key found for this signature in database
GPG Key ID: 524BD33A0EE29499

View File

@ -2,20 +2,32 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import argparse import argparse
import shlex
import os import os
import shlex
from spoc.container import Container from spoc import repo_local
from spoc.container import Container, STATE_RUNNING, STATE_STOPPED
from spoc.image import Image from spoc.image import Image
from spoc.paths import VOLUME_DIR from spoc.paths import VOLUME_DIR
ACTION_CREATE = 1 ACTION_LIST = 1
ACTION_MODIFY = 2 ACTION_CREATE = 2
ACTION_DESTROY = 3 ACTION_MODIFY = 3
ACTION_START = 4 ACTION_DESTROY = 4
ACTION_STOP = 5 ACTION_START = 5
ACTION_STATUS = 6 ACTION_STOP = 6
ACTION_EXEC = 7 ACTION_STATUS = 7
ACTION_EXEC = 8
def listing(state):
if state == 'all':
containers = repo_local.get_containers().keys()
elif state == 'running':
containers = [c for c in repo_local.get_containers() if Container(c).get_state() == STATE_RUNNING]
elif state == 'stopped':
containers = [c for c in repo_local.get_containers() if Container(c).get_state() == STATE_STOPPED]
for container in containers:
print(container)
def modify_depend(container, depend): def modify_depend(container, depend):
if depend.startswith('!'): if depend.startswith('!'):
@ -102,6 +114,10 @@ parser = argparse.ArgumentParser(description='SPOC container manager')
parser.set_defaults(action=None) parser.set_defaults(action=None)
subparsers = parser.add_subparsers() subparsers = parser.add_subparsers()
parser_list = subparsers.add_parser('list')
parser_list.set_defaults(action=ACTION_LIST)
parser_list.add_argument('type', choices=('all', 'running', 'stopped'), default='all', const='all', nargs='?')
parser_create = subparsers.add_parser('create') parser_create = subparsers.add_parser('create')
parser_create.set_defaults(action=ACTION_CREATE) parser_create.set_defaults(action=ACTION_CREATE)
parser_create.add_argument('-d', '--depends', action='append', default=[], help='Add another container as a start dependency') parser_create.add_argument('-d', '--depends', action='append', default=[], help='Add another container as a start dependency')
@ -156,7 +172,9 @@ parser_exec.add_argument('command', nargs=argparse.REMAINDER)
args = parser.parse_args() args = parser.parse_args()
if args.action == ACTION_CREATE: if args.action == ACTION_LIST:
listing(args.type)
elif args.action == ACTION_CREATE:
create(args.container, args.image, args.depends, args.mount, args.env, args.uid, args.gid, args.cmd, args.workdir, args.ready, args.stopsig, args.autostart) create(args.container, args.image, args.depends, args.mount, args.env, args.uid, args.gid, args.cmd, args.workdir, args.ready, args.stopsig, args.autostart)
elif args.action == ACTION_MODIFY: elif args.action == ACTION_MODIFY:
modify(args.container, args.depends, args.mount, args.env, args.uid, args.gid, args.cmd, args.workdir, args.ready, args.stopsig, args.autostart) modify(args.container, args.depends, args.mount, args.env, args.uid, args.gid, args.cmd, args.workdir, args.ready, args.stopsig, args.autostart)