Commit b02015aa authored by Antoine Catton's avatar Antoine Catton

Switch from config file to promise based container status

parent fd20a6d6
...@@ -22,34 +22,23 @@ def main(sr_directory, partition_list): ...@@ -22,34 +22,23 @@ def main(sr_directory, partition_list):
'.slapcontainer') '.slapcontainer')
if os.path.isfile(slapcontainer_filename): if os.path.isfile(slapcontainer_filename):
lock = lockfile.FileLock(slapcontainer_filename) lock = lockfile.FileLock(slapcontainer_filename)
lock.acquire(timeout=0)
slapcontainer_conf = ConfigParser.ConfigParser()
slapcontainer_conf.read(slapcontainer_filename)
try: try:
lock.acquire(timeout=0)
slapcontainer_conf = ConfigParser.ConfigParser() requested_status = slapcontainer_conf.get('requested',
slapcontainer_conf.read(slapcontainer_filename) 'status')
try:
if requested_status == 'started':
requested_status = slapcontainer_conf.get('requested', if not created(partition_path):
'status') create(sr_directory, partition_path,
slapcontainer_conf)
if not slapcontainer_conf.has_section('current'): if status(sr_directory, partition_path) == 'stopped':
slapcontainer_conf.add_section('current') start(sr_directory, partition_path)
if not slapcontainer_conf.has_option('current', 'created'): else:
slapcontainer_conf.set('current', 'created', 'no') if status(sr_directory, partition_path) == 'started':
if not slapcontainer_conf.has_option('current', 'status'): stop(sr_directory, partition_path)
slapcontainer_conf.set('current', 'status', 'stopped')
if requested_status == 'started':
if slapcontainer_conf.get('current', 'created') == 'no':
create(sr_directory, partition_path, slapcontainer_conf)
slapcontainer_conf.set('current', 'created', 'yes')
start(sr_directory, partition_path,
slapcontainer_conf)
else:
stop(sr_directory, partition_path,
slapcontainer_conf)
finally:
with open(slapcontainer_filename, 'w') as slapcontainer_fp:
slapcontainer_conf.write(slapcontainer_fp)
except lockfile.LockTimeout: except lockfile.LockTimeout:
# Can't do anything, we'll see on the next run # Can't do anything, we'll see on the next run
pass pass
...@@ -58,26 +47,30 @@ def main(sr_directory, partition_list): ...@@ -58,26 +47,30 @@ def main(sr_directory, partition_list):
def start(sr_directory, partition_path, conf): def start(sr_directory, partition_path):
lxc_start = os.path.join(sr_directory, lxc_start = os.path.join(sr_directory,
'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')
call([lxc_start, '-f', config_filename]) call([lxc_start, '-f', config_filename,
# TODO: Check if container is started, '-n', os.path.basename(partition_path),
# Start container '-d'])
conf.set('current', 'status', 'started')
def stop(sr_directory, partition_path, conf): def stop(sr_directory, partition_path):
# TODO : Check if container is stopped # TODO : Check if container is stopped
# Stop container # Stop container
pass
if conf.get('current', 'created') == 'yes':
destroy(partition_path, conf)
def created(partition_path):
# XXX: Hardcoded
should_exists = ['config', 'rootfs']
return all((os.path.exists(os.path.join(partition_path, f))
for f in should_exists))
...@@ -89,7 +82,7 @@ def create(sr_directory, partition_path, conf): ...@@ -89,7 +82,7 @@ def create(sr_directory, partition_path, conf):
lxc_filename = os.path.join(partition_path, 'config') lxc_filename = os.path.join(partition_path, 'config')
lxc = LXCConfig(lxc_filename) lxc = LXCConfig(lxc_filename)
lxc.utsname = os.path.basename(partition_path) lxc.utsname = os.path.basename(partition_path)
lxc.network.vlan.type = 'vlan' lxc.network.type = 'vlan'
lxc.network.link = conf.get('information', 'interface') lxc.network.link = conf.get('information', 'interface')
lxc.network.name = 'eth0' lxc.network.name = 'eth0'
# XXX: Hardcoded netmasks # XXX: Hardcoded netmasks
...@@ -101,15 +94,35 @@ def create(sr_directory, partition_path, conf): ...@@ -101,15 +94,35 @@ def create(sr_directory, partition_path, conf):
lxc_file.write(str(lxc)) lxc_file.write(str(lxc))
def destroy(partition_path, conf):
def destroy(partition_path):
# TODO: Destroy container # TODO: Destroy container
pass pass
def status(sr_directory, partition_path):
lxc_info = call([os.path.join(sr_directory, 'parts/lxc/bin/lxc-info'),
'-n', os.path.basename(partition_path)])
if 'RUNNING' in lxc_info:
return 'started'
else:
return 'stopped'
def call(command_line): def call(command_line):
process = subprocess.Popen(command_line, stdin=subprocess.PIPE) # for debug :
# print ' '.join(command_line)
process = subprocess.Popen(command_line, stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
process.stdin.flush() process.stdin.flush()
process.stdin.close() process.stdin.close()
if process.wait() != 0: if process.wait() != 0:
raise SlapContainerError("Subprocess call failed") raise SlapContainerError("Subprocess call failed")
out = process.stdout.read()
# for debug :
# print out
return out
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