update
This commit is contained in:
parent
8ec8bd1f29
commit
906b39fe07
1 changed files with 20 additions and 8 deletions
|
@ -31,7 +31,7 @@ class _LXCMonitor(threading.Thread):
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
master, slave = pty.openpty()
|
master, slave = pty.openpty()
|
||||||
cmd = ['lxc-monitor', '-n', '.*']
|
cmd = ['/usr/bin/lxc-monitor', '-n', '.*']
|
||||||
self._process = subprocess.Popen(cmd, stdout=slave, bufsize=1)
|
self._process = subprocess.Popen(cmd, stdout=slave, bufsize=1)
|
||||||
stdout = os.fdopen(master)
|
stdout = os.fdopen(master)
|
||||||
while self._process.poll() is None:
|
while self._process.poll() is None:
|
||||||
|
@ -134,7 +134,7 @@ class lxc():
|
||||||
# todo: add condition
|
# todo: add condition
|
||||||
self.stop(name)
|
self.stop(name)
|
||||||
|
|
||||||
cmd = ['lxc-destroy', '-f', '-n', name]
|
cmd = ['/usr/bin/lxc-destroy', '-f', '-n', name]
|
||||||
|
|
||||||
return subprocess.check_call(cmd)
|
return subprocess.check_call(cmd)
|
||||||
|
|
||||||
|
@ -142,39 +142,51 @@ class lxc():
|
||||||
"""
|
"""
|
||||||
returns info dict about the specified container
|
returns info dict about the specified container
|
||||||
"""
|
"""
|
||||||
|
#
|
||||||
if not self.exists(name):
|
if not self.exists(name):
|
||||||
raise ContainerNotExists("The container (%s) does not exist!" % name)
|
raise ContainerNotExists("The container (%s) does not exist!" % name)
|
||||||
|
#
|
||||||
cmd = ['/usr/bin/lxc-info', '-n', name, "-H"]
|
cmd = ['/usr/bin/lxc-info', '-n', name, "-H"]
|
||||||
out = subprocess.check_output(cmd).splitlines()
|
out = subprocess.check_output(cmd).splitlines()
|
||||||
clean = []
|
clean = []
|
||||||
info = {}
|
info = {}
|
||||||
|
#
|
||||||
for line in out:
|
for line in out:
|
||||||
# print line
|
|
||||||
if line not in clean:
|
if line not in clean:
|
||||||
clean.append(line)
|
clean.append(line)
|
||||||
|
#
|
||||||
for line in clean:
|
for line in clean:
|
||||||
key, value = line.split(":")
|
key, value = line.split(":")
|
||||||
|
|
||||||
# strip
|
# strip
|
||||||
key = key.lstrip()
|
key = key.lstrip()
|
||||||
value = value.lstrip()
|
value = value.lstrip()
|
||||||
|
#
|
||||||
key = key.replace(" ", "_")
|
key = key.replace(" ", "_")
|
||||||
|
|
||||||
info[key.lower()] = value
|
info[key.lower()] = value
|
||||||
|
|
||||||
|
# get container size
|
||||||
|
info['container_size'] = self.__get_container_size(name)
|
||||||
return info
|
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):
|
def freeze(self, name):
|
||||||
"""
|
"""
|
||||||
freezes the container
|
freezes the container
|
||||||
"""
|
"""
|
||||||
if not self.exists(name):
|
if not self.exists(name):
|
||||||
raise ContainerNotExists("The container (%s) does not exist!" % 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)
|
subprocess.check_call(cmd)
|
||||||
|
|
||||||
def unfreeze(self, name):
|
def unfreeze(self, name):
|
||||||
|
|
Loading…
Add table
Reference in a new issue