From 906b39fe075a49c7127646fa1d004744248a8daa Mon Sep 17 00:00:00 2001 From: vanzhiganov Date: Tue, 15 Dec 2015 23:13:21 +0300 Subject: [PATCH] update --- lxc/__init__.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/lxc/__init__.py b/lxc/__init__.py index b774a20..a768ac3 100644 --- a/lxc/__init__.py +++ b/lxc/__init__.py @@ -31,7 +31,7 @@ class _LXCMonitor(threading.Thread): def run(self): master, slave = pty.openpty() - cmd = ['lxc-monitor', '-n', '.*'] + cmd = ['/usr/bin/lxc-monitor', '-n', '.*'] self._process = subprocess.Popen(cmd, stdout=slave, bufsize=1) stdout = os.fdopen(master) while self._process.poll() is None: @@ -134,7 +134,7 @@ class lxc(): # todo: add condition self.stop(name) - cmd = ['lxc-destroy', '-f', '-n', name] + cmd = ['/usr/bin/lxc-destroy', '-f', '-n', name] return subprocess.check_call(cmd) @@ -142,39 +142,51 @@ class lxc(): """ returns info dict about the specified container """ + # if not self.exists(name): raise ContainerNotExists("The container (%s) does not exist!" % name) - + # cmd = ['/usr/bin/lxc-info', '-n', name, "-H"] out = subprocess.check_output(cmd).splitlines() clean = [] info = {} - + # for line in out: - # print line if line not in clean: clean.append(line) - + # for line in clean: key, value = line.split(":") # strip key = key.lstrip() value = value.lstrip() - + # key = key.replace(" ", "_") info[key.lower()] = value + # get container size + info['container_size'] = self.__get_container_size(name) return info + def __get_container_size(self, name): + cmd = ['/usr/bin/du', '--total', '-s', '/var/lib/lxc/%s' % name] + out = subprocess.check_output(cmd).splitlines() + size = 0 + for l in out: + key, value = l.split('\t') + if value == 'total': + size = key + return int(key) + def freeze(self, name): """ freezes the container """ if not self.exists(name): raise ContainerNotExists("The container (%s) does not exist!" % name) - cmd = ['lxc-freeze', '-n', name] + cmd = ['/usr/bin/lxc-freeze', '-n', name] subprocess.check_call(cmd) def unfreeze(self, name):