This commit is contained in:
Vyacheslav Anzhiganov 2016-05-12 03:07:53 +03:00
parent a17bd53183
commit 07de84e764

View file

@ -1,407 +1,407 @@
# coding: utf-8 # coding: utf-8
import subprocess import subprocess
import logging import logging
import threading import threading
import select import select
import pty import pty
import os import os
import signal import signal
class ContainerAlreadyExists(Exception): class ContainerAlreadyExists(Exception):
pass pass
class ContainerAlreadyRunning(Exception): class ContainerAlreadyRunning(Exception):
pass pass
class ContainerNotExists(Exception): class ContainerNotExists(Exception):
pass pass
_logger = logging.getLogger("pylxc") _logger = logging.getLogger("pylxc")
_monitor = None _monitor = None
class _LXCMonitor(threading.Thread): class _LXCMonitor(threading.Thread):
def __init__(self): def __init__(self):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self._process = None self._process = None
self._monitors = {} self._monitors = {}
def run(self): def run(self):
master, slave = pty.openpty() master, slave = pty.openpty()
cmd = ['/usr/bin/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:
ready, _, _ = select.select([stdout], [], [], 0.1) ready, _, _ = select.select([stdout], [], [], 0.1)
if ready: if ready:
logging.debug("Waiting for state change") logging.debug("Waiting for state change")
state = stdout.readline() state = stdout.readline()
inf = state.strip().split() inf = state.strip().split()
container = inf[0].strip("'") container = inf[0].strip("'")
state = inf[-1].strip('[]') state = inf[-1].strip('[]')
if container in self._monitors: if container in self._monitors:
logging.debug("State of container '%s' changed to '%s'", container, state) logging.debug("State of container '%s' changed to '%s'", container, state)
self._monitors[container](state) self._monitors[container](state)
_logger.info("LXC Monitor stopped!") _logger.info("LXC Monitor stopped!")
def add_monitor(self, name, callback): def add_monitor(self, name, callback):
self._monitors[name] = callback self._monitors[name] = callback
def rm_monitor(self, name): def rm_monitor(self, name):
self._monitors.pop(name) self._monitors.pop(name)
def is_monitored(self, name): def is_monitored(self, name):
return name in self._monitors return name in self._monitors
def kill(self): def kill(self):
try: try:
self._process.terminate() self._process.terminate()
self._process.wait() self._process.wait()
except: except:
pass pass
self.join() self.join()
class lxc(): class lxc():
def __init__(self): def __init__(self):
logging.debug("") logging.debug("")
def list(self, status=None): def list(self, status=None):
""" """
:return: ['container_first', 'container_second'] :return: ['container_first', 'container_second']
""" """
if status in ['active', 'frozen', 'running', 'stopped', 'nesting']: if status in ['active', 'frozen', 'running', 'stopped', 'nesting']:
path = "--%s" % status path = "--%s" % status
else: else:
path = "" path = ""
cmd = ['/usr/bin/lxc-ls', path] cmd = ['/usr/bin/lxc-ls', path]
out = subprocess.check_output(cmd).splitlines() out = subprocess.check_output(cmd).splitlines()
# print out # print out
return out return out
def exists(self, name): def exists(self, name):
""" """
checks if a given container is defined or not checks if a given container is defined or not
""" """
if name in self.list(): if name in self.list():
return True return True
return False return False
def start(self, name, config_file=None): def start(self, name, config_file=None):
""" """
starts a container in daemon mode starts a container in daemon mode
""" """
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)
if name in self.list("running"): if name in self.list("running"):
raise ContainerAlreadyRunning('The container %s is already started!' % name) raise ContainerAlreadyRunning('The container %s is already started!' % name)
cmd = ['lxc-start', '-n', name, '-d'] cmd = ['lxc-start', '-n', name, '-d']
if config_file: if config_file:
cmd += ['-f', config_file] cmd += ['-f', config_file]
return subprocess.check_call(cmd) return subprocess.check_call(cmd)
def stop(self, name): def stop(self, name):
""" """
stops a container stops a 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-stop', '-n', name] cmd = ['/usr/bin/lxc-stop', '-n', name]
try: try:
result = subprocess.check_call(cmd) result = subprocess.check_call(cmd)
return True return True
except Exception as e: except Exception as e:
return False return False
def destroy(self, name): def destroy(self, name):
""" """
removes a container [stops a container if it's running and] removes a container [stops a container if it's running and]
raises ContainerNotExists exception if the specified name is not created raises ContainerNotExists exception if the specified name is not created
""" """
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)
# todo: check status. If status not STOPPED - run method self.stop(name) # todo: check status. If status not STOPPED - run method self.stop(name)
# todo: add condition # todo: add condition
self.stop(name) self.stop(name)
cmd = ['/usr/bin/lxc-destroy', '-f', '-n', name] cmd = ['/usr/bin/lxc-destroy', '-f', '-n', name]
return subprocess.check_call(cmd) return subprocess.check_call(cmd)
def info(self, name): def info(self, name):
""" """
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:
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 # get container size
info['size'] = self.__get_container_size(name) info['size'] = self.__get_container_size(name)
return info return info
def __get_container_size(self, name): def __get_container_size(self, name):
cmd = ['/usr/bin/du', '--total', '-s', '/var/lib/lxc/%s' % name] cmd = ['/usr/bin/du', '--total', '-s', '/var/lib/lxc/%s' % name]
out = subprocess.check_output(cmd).splitlines() out = subprocess.check_output(cmd).splitlines()
size = 0 size = 0
for l in out: for l in out:
key, value = l.split('\t') key, value = l.split('\t')
if value == 'total': if value == 'total':
size = key size = key
return int(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 = ['/usr/bin/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):
""" """
unfreezes the container unfreezes 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-unfreeze', '-n', name] cmd = ['lxc-unfreeze', '-n', name]
subprocess.check_call(cmd) subprocess.check_call(cmd)
def notify(self, name, states, callback): def notify(self, name, states, callback):
""" """
executes the callback function with no parameters when the container reaches the specified state or states executes the callback function with no parameters when the container reaches the specified state or states
states can be or-ed or and-ed states can be or-ed or and-ed
notify('test', 'STOPPED', letmeknow) notify('test', 'STOPPED', letmeknow)
notify('test', 'STOPPED|RUNNING', letmeknow) notify('test', 'STOPPED|RUNNING', letmeknow)
""" """
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-wait', '-n', name, '-s', states] cmd = ['lxc-wait', '-n', name, '-s', states]
def th(): def th():
subprocess.check_call(cmd) subprocess.check_call(cmd)
callback() callback()
_logger.info("Waiting on states %s for container %s", states, name) _logger.info("Waiting on states %s for container %s", states, name)
threading.Thread(target=th).start() threading.Thread(target=th).start()
def checkconfig(self): def checkconfig(self):
""" """
returns the output of lxc-checkconfig returns the output of lxc-checkconfig
""" """
cmd = ['lxc-checkconfig'] cmd = ['lxc-checkconfig']
return subprocess.check_output(cmd).replace('[1;32m', '').replace('[1;33m', '').replace('[0;39m', '').replace('[1;32m', '').replace(' ', '').split('\n') return subprocess.check_output(cmd).replace('[1;32m', '').replace('[1;33m', '').replace('[0;39m', '').replace('[1;32m', '').replace(' ', '').split('\n')
def create(self, name, config_file=None, template=None, backing_store=None, template_options=None): def create(self, name, config_file=None, template=None, backing_store=None, template_options=None):
""" """
Create a new container Create a new container
raises ContainerAlreadyExists exception if the container name is reserved already. raises ContainerAlreadyExists exception if the container name is reserved already.
:param template_options: Options passed to the specified template :param template_options: Options passed to the specified template
:type template_options: list or None :type template_options: list or None
""" """
if self.exists(name): if self.exists(name):
raise ContainerAlreadyExists("The Container %s is already created!" % name) raise ContainerAlreadyExists("The Container %s is already created!" % name)
command = list() command = list()
command.append("lxc-create -n %s" % name) command.append("lxc-create -n %s" % name)
if config_file: if config_file:
command.append(' -f %s' % config_file) command.append(' -f %s' % config_file)
if template: if template:
command.append(' -t %s' % template) command.append(' -t %s' % template)
if backing_store: if backing_store:
command.append(' -B %s' % backing_store) command.append(' -B %s' % backing_store)
if template_options: if template_options:
command.append(' -- %s' % template_options) command.append(' -- %s' % template_options)
print " ".join(command) print " ".join(command)
print print
# create = subprocess.check_call(command, shell=True) # create = subprocess.check_call(command, shell=True)
create = subprocess.check_call(" ".join(command), shell=True) create = subprocess.check_call(" ".join(command), shell=True)
print print
print create print create
print print
# if create == 0: # if create == 0:
# if not self.exists(name): # if not self.exists(name):
# _logger.critical("The Container %s doesn't seem to be created! (options: %s)", name, command[3:]) # _logger.critical("The Container %s doesn't seem to be created! (options: %s)", name, command[3:])
# raise ContainerNotExists("The container (%s) does not exist!" % name) # raise ContainerNotExists("The container (%s) does not exist!" % name)
# #
# _logger.info("Container %s has been created with options %s", name, command[3:]) # _logger.info("Container %s has been created with options %s", name, command[3:])
# return False # return False
return True return True
def reset_password(self, container_name, username, password): def reset_password(self, container_name, username, password):
call = [ call = [
'echo', 'echo',
'"%s:${PASSWORD:-%s}"' % (username, password), '"%s:${PASSWORD:-%s}"' % (username, password),
"|", "|",
"chroot", "chroot",
"/var/lib/lxc/%s/rootfs/ chpasswd" % container_name "/var/lib/lxc/%s/rootfs/ chpasswd" % container_name
] ]
subprocess.check_call(call, shell=True) subprocess.check_call(call, shell=True)
# subprocess.call("echo \"ubuntu:${PASSWORD:-%(password)s}\" | chroot /var/lib/lxc/%(hostname)s/rootfs/ chpasswd" % task['parameters'], shell=True) # subprocess.call("echo \"ubuntu:${PASSWORD:-%(password)s}\" | chroot /var/lib/lxc/%(hostname)s/rootfs/ chpasswd" % task['parameters'], shell=True)
return True return True
# def running(): # def running():
# ''' # '''
# returns a list of the currently running containers # returns a list of the currently running containers
# ''' # '''
# return all_as_dict()['Running'] # return all_as_dict()['Running']
# def stopped(): # def stopped():
# ''' # '''
# returns a list of the stopped containers # returns a list of the stopped containers
# ''' # '''
# return all_as_dict()['Stopped'] # return all_as_dict()['Stopped']
# def all_as_dict(): # def all_as_dict():
# ''' # '''
# returns a dict {'Running': ['cont1', 'cont2'], # returns a dict {'Running': ['cont1', 'cont2'],
# 'Stopped': ['cont3', 'cont4'] # 'Stopped': ['cont3', 'cont4']
# } # }
# #
# ''' # '''
# cmd = ['lxc-ls'] # cmd = ['lxc-ls']
# out = subprocess.check_output(cmd).splitlines() # out = subprocess.check_output(cmd).splitlines()
# print out # print out
# stopped = [] # stopped = []
# running = [] # running = []
# frozen = [] # frozen = []
# current = None # current = None
# for c in out: # for c in out:
# c = c.strip() # c = c.strip()
# if c == 'RUNNING': # if c == 'RUNNING':
# current = running # current = running
# continue # continue
# if c == 'STOPPED': # if c == 'STOPPED':
# current = stopped # current = stopped
# continue # continue
# if c == 'FROZEN': # if c == 'FROZEN':
# current = frozen # current = frozen
# continue # continue
# if not len(c): # if not len(c):
# continue # continue
# current.append(c) # current.append(c)
# return {'Running': running, # return {'Running': running,
# 'Stopped': stopped, # 'Stopped': stopped,
# 'Frozen': frozen} # 'Frozen': frozen}
# def all_as_list(): # def all_as_list():
# ''' # '''
# returns a list of all defined containers # returns a list of all defined containers
# ''' # '''
# as_dict = all_as_dict() # as_dict = all_as_dict()
# containers = as_dict['Running'] + as_dict['Frozen'] + as_dict['Stopped'] # containers = as_dict['Running'] + as_dict['Frozen'] + as_dict['Stopped']
# containers_list = [] # containers_list = []
# for i in containers: # for i in containers:
# i = i.replace(' (auto)', '') # i = i.replace(' (auto)', '')
# containers_list.append(i) # containers_list.append(i)
# return containers_list # return containers_list
# def kill(name, signal): # def kill(name, signal):
# ''' # '''
# sends a kill signal to process 1 of ths container <name> # sends a kill signal to process 1 of ths container <name>
# :param signal: numeric signal # :param signal: numeric signal
# ''' # '''
# if not exists(name): # if not exists(name):
# raise ContainerNotExists("The container (%s) does not exist!" % name) # raise ContainerNotExists("The container (%s) does not exist!" % name)
# cmd = ['lxc-kill', '--name=%s' % name, signal] # cmd = ['lxc-kill', '--name=%s' % name, signal]
# subprocess.check_call(cmd) # subprocess.check_call(cmd)
# def shutdown(name, wait=False, reboot=False): # def shutdown(name, wait=False, reboot=False):
# ''' # '''
# graceful shutdown sent to the container # graceful shutdown sent to the container
# :param wait: should we wait for the shutdown to complete? # :param wait: should we wait for the shutdown to complete?
# :param reboot: reboot a container, ignores wait # :param reboot: reboot a container, ignores wait
# ''' # '''
# if not exists(name): # if not exists(name):
# raise ContainerNotExists("The container (%s) does not exist!" % name) # raise ContainerNotExists("The container (%s) does not exist!" % name)
# cmd = ['lxc-shutdown', '-n', name] # cmd = ['lxc-shutdown', '-n', name]
# if wait: # if wait:
# cmd += ['-w'] # cmd += ['-w']
# if reboot: # if reboot:
# cmd += ['-r'] # cmd += ['-r']
# #
# subprocess.check_call(cmd) # subprocess.check_call(cmd)
# def monitor(name, callback): # def monitor(name, callback):
# ''' # '''
# monitors actions on the specified container, # monitors actions on the specified container,
# callback is a function to be called on # callback is a function to be called on
# ''' # '''
# global _monitor # global _monitor
# if not exists(name): # if not exists(name):
# raise ContainerNotExists("The container (%s) does not exist!" % name) # raise ContainerNotExists("The container (%s) does not exist!" % name)
# if _monitor: # if _monitor:
# if _monitor.is_monitored(name): # if _monitor.is_monitored(name):
# raise Exception("You are already monitoring this container (%s)" % name) # raise Exception("You are already monitoring this container (%s)" % name)
# else: # else:
# _monitor = _LXCMonitor() # _monitor = _LXCMonitor()
# logging.info("Starting LXC Monitor") # logging.info("Starting LXC Monitor")
# _monitor.start() # _monitor.start()
# def kill_handler(sg, fr): # def kill_handler(sg, fr):
# stop_monitor() # stop_monitor()
# signal.signal(signal.SIGTERM, kill_handler) # signal.signal(signal.SIGTERM, kill_handler)
# signal.signal(signal.SIGINT, kill_handler) # signal.signal(signal.SIGINT, kill_handler)
# _monitor.add_monitor(name, callback) # _monitor.add_monitor(name, callback)
# def unmonitor(name): # def unmonitor(name):
# if not exists(name): # if not exists(name):
# raise ContainerNotExists("The container (%s) does not exist!" % name) # raise ContainerNotExists("The container (%s) does not exist!" % name)
# if not _monitor: # if not _monitor:
# raise Exception("LXC Monitor is not started!") # raise Exception("LXC Monitor is not started!")
# if not _monitor.is_monitored(name): # if not _monitor.is_monitored(name):
# raise Exception("This container (%s) is not monitored!" % name) # raise Exception("This container (%s) is not monitored!" % name)
# _monitor.rm_monitor(name) # _monitor.rm_monitor(name)
# def stop_monitor(): # def stop_monitor():
# global _monitor # global _monitor
# if _monitor: # if _monitor:
# logging.info("Killing LXC Monitor") # logging.info("Killing LXC Monitor")
# _monitor.kill() # _monitor.kill()
# _monitor = None # _monitor = None
# signal.signal(signal.SIGTERM, signal.SIG_DFL) # signal.signal(signal.SIGTERM, signal.SIG_DFL)
# signal.signal(signal.SIGINT, signal.SIG_DFL) # signal.signal(signal.SIGINT, signal.SIG_DFL)