Allow spoc-container start to pass a command

This commit is contained in:
Disassembler 2020-04-05 20:34:40 +02:00
parent 5929a1ea6b
commit b3f2a4be70
No known key found for this signature in database
GPG Key ID: 524BD33A0EE29499
2 changed files with 9 additions and 7 deletions

View File

@ -87,9 +87,9 @@ def destroy(container_name):
# Remove container and its directory
Container(container_name, False).destroy()
def start(container_name):
def start(container_name, command):
# Start the container using init values from its definition
Container(container_name).start()
Container(container_name).start(command)
def stop(container_name):
# Stop the container using halt signal from its definition
@ -147,6 +147,7 @@ parser_destroy.add_argument('container')
parser_start = subparsers.add_parser('start')
parser_start.set_defaults(action=start)
parser_start.add_argument('container')
parser_start.add_argument('command', nargs=argparse.REMAINDER)
parser_stop = subparsers.add_parser('stop')
parser_stop.set_defaults(action=stop)
@ -174,7 +175,7 @@ elif args.action is modify:
elif args.action is destroy:
destroy(args.container)
elif args.action is start:
start(args.container)
start(args.container, args.command)
elif args.action is stop:
stop(args.container)
elif args.action is status:

View File

@ -151,17 +151,18 @@ class Container:
# Release the IP address from global hosts configuration
net.release_ip(self.name)
def start(self):
def start(self, command=None):
# Start the container including its dependencies
depsolver = DepSolver()
self.get_start_dependencies(depsolver)
for dependency in depsolver.solve():
if dependency.get_state() != ContainerState.RUNNING:
dependency.do_start()
dependency.do_start(command if dependency.name == self.name else None)
def do_start(self):
def do_start(self, command=None):
cmd = ['--']+command if command else []
# Start the current container, wait until it is reported as started and execute application readiness check
subprocess.Popen(['lxc-start', '-P', config.CONTAINERS_DIR, self.name])
subprocess.Popen(['lxc-start', '-P', config.CONTAINERS_DIR, self.name]+cmd)
self.await_state(ContainerState.RUNNING)
# Launch the readiness check in a separate thread, so it can be reliably cancelled after timeout
with ThreadPoolExecutor(max_workers=1) as pool: