Commit 9ff3e2af authored by Antoine Catton's avatar Antoine Catton

Switch from return code to exception (my bad)

parent 64ad7b16
...@@ -6,14 +6,19 @@ import subprocess ...@@ -6,14 +6,19 @@ import subprocess
import lockfile import lockfile
class SlapContainerError(Exception):
"""This exception is thrown, if there is
any failure during slapcontainer preparation,
starting or stopping process"""
def main(sr_directory, partition_list): def main(sr_directory, partition_list):
overall_failure = 0
for partition_path in partition_list: for partition_path in partition_list:
slapcontainer_filename = os.path.join(partition_path, slapcontainer_filename = os.path.join(partition_path,
'.slapcontainer') '.slapcontainer')
if os.path.isfile(slapcontainer_filename): if os.path.isfile(slapcontainer_filename):
failure = 0
lock = lockfile.FileLock(slapcontainer_filename) lock = lockfile.FileLock(slapcontainer_filename)
try: try:
lock.acquire(timeout=0) lock.acquire(timeout=0)
...@@ -30,10 +35,10 @@ def main(sr_directory, partition_list): ...@@ -30,10 +35,10 @@ def main(sr_directory, partition_list):
slapcontainer_conf.set('current', 'status', 'stopped') slapcontainer_conf.set('current', 'status', 'stopped')
if requested_status == 'started': if requested_status == 'started':
failure |= start(sr_directory, partition_path, start(sr_directory, partition_path,
slapcontainer_conf) slapcontainer_conf)
else: else:
failure |= stop(sr_directory, partition_path, stop(sr_directory, partition_path,
slapcontainer_conf) slapcontainer_conf)
with open(slapcontainer_filename, 'w') as slapcontainer_fp: with open(slapcontainer_filename, 'w') as slapcontainer_fp:
...@@ -44,19 +49,11 @@ def main(sr_directory, partition_list): ...@@ -44,19 +49,11 @@ def main(sr_directory, partition_list):
finally: finally:
lock.release() lock.release()
overall_failure |= failure
return overall_failure
def start(sr_directory, partition_path, conf): def start(sr_directory, partition_path, conf):
failure = 0
if conf.get('current', 'created') == 'no': if conf.get('current', 'created') == 'no':
failure |= create(sr_directory, partition_path, conf) create(sr_directory, partition_path, conf)
if failure:
return failure
conf.set('current', 'created', 'yes') conf.set('current', 'created', 'yes')
...@@ -64,26 +61,21 @@ def start(sr_directory, partition_path, conf): ...@@ -64,26 +61,21 @@ def start(sr_directory, partition_path, conf):
'parts/lxc/bin/lxc-start') 'parts/lxc/bin/lxc-start')
config_filename = os.path.join(partition_path, 'config') config_filename = os.path.join(partition_path, 'config')
failure |= call([lxc_start, '-f', config_filename]) call([lxc_start, '-f', config_filename])
# TODO: Check if container is started, # TODO: Check if container is started,
# Start container # Start container
conf.set('current', 'status', 'started') conf.set('current', 'status', 'started')
return failure
def stop(sr_directory, partition_path, conf): def stop(sr_directory, partition_path, conf):
failure = 0
# TODO : Check if container is stopped # TODO : Check if container is stopped
# Stop container # Stop container
if conf.get('current', 'created') == 'yes': if conf.get('current', 'created') == 'yes':
failure |= destroy(partition_path, conf) destroy(partition_path, conf)
return failure
...@@ -94,15 +86,14 @@ def create(sr_directory, partition_path, conf): ...@@ -94,15 +86,14 @@ def create(sr_directory, partition_path, conf):
def destroy(partition_path, conf): def destroy(partition_path, conf):
failure = 0
# TODO: Destroy container # TODO: Destroy container
pass
return failure
def call(command_line): def call(command_line):
process = subprocess.Popen(command_line, stdin=subprocess.PIPE) process = subprocess.Popen(command_line, stdin=subprocess.PIPE)
process.stdin.flush() process.stdin.flush()
process.stdin.close() process.stdin.close()
return process.wait()
if process.wait() != 0:
raise SlapContainerError("Subprocess call failed")
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment