From 1013f44d4070d54b494c3a4709f91230353b2ffc Mon Sep 17 00:00:00 2001 From: dedic-one Date: Thu, 17 Feb 2022 14:24:36 +0300 Subject: [PATCH] add container arguments --- container.conf | 4 ++++ ctl.py | 40 +++++++++++++++++++++++++--------------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/container.conf b/container.conf index fa0d971..2e4b671 100644 --- a/container.conf +++ b/container.conf @@ -12,5 +12,9 @@ env: { PMA_ARBITRARY: 1 } + cmd: [ + arg1 + arg2 + ] } diff --git a/ctl.py b/ctl.py index 213cc77..9c554fd 100755 --- a/ctl.py +++ b/ctl.py @@ -11,9 +11,9 @@ import sys import os import pty -def di_syscall(cmd): +def di_syscall(cmd, workdir='.'): #print('[dbg] CMD: %s' % (cmd)) - process = subprocess.run(cmd, capture_output=True) + process = subprocess.run(cmd, capture_output=True, cwd=workdir) return (process.stdout.decode("utf-8").strip(), process.stderr.decode("utf-8").strip()) @@ -45,8 +45,9 @@ def get_config(path): # -------------------------- # class Container: - def __init__(self, conf): + def __init__(self, conf, workdir): self.conf = conf + self.workdir = workdir def status(self): @@ -71,12 +72,15 @@ class Container: for publish in self.conf['ports']: cmd.append('--publish') cmd.append(publish) - for env_name in self.conf['env'].keys(): - cmd.append('--env') - cmd.append('%s=%s' % (env_name, self.conf['env'][env_name])) + if self.conf.get('env', None) is not None: + for env_name in self.conf['env'].keys(): + cmd.append('--env') + cmd.append('%s=%s' % (env_name, self.conf['env'][env_name])) cmd.append(self.conf['image']) + for arg in self.conf['cmd']: + cmd.append('%s' % arg) - container_id, err = di_syscall(cmd) + container_id, err = di_syscall(cmd, self.workdir) if err: print('ERROR') print(err) @@ -142,17 +146,23 @@ class Container: if len(sys.argv) == 1: print_help_exit() -begin_index_commands = 2 -conf = get_config(sys.argv[1]) -if conf is None: +conf = None +begin_index_commands = None +workdir = None +if os.path.isfile(sys.argv[1]): + conf = get_config(sys.argv[1]) + begin_index_commands = 2 + workdir = os.path.dirname(sys.argv[1]) +else: conf = get_config('container.conf') - if conf is None: - print_help_exit() - else: - begin_index_commands = 1 + begin_index_commands = 1 + workdir = '.' + +if conf is None: + print_help_exit() conf = conf.with_fallback(create_default_config()) -container = Container(conf) +container = Container(conf, workdir) command = sys.argv[begin_index_commands] if command == 'start':