From b6ed73f8501b3c7ac5b648d4eacd0af7b6e21b2c Mon Sep 17 00:00:00 2001 From: Ah-Lun Tang Date: Wed, 8 May 2013 15:15:49 +0200 Subject: [PATCH] add clone support --- lxc/__init__.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/lxc/__init__.py b/lxc/__init__.py index 60d6fc5..d0e1c00 100644 --- a/lxc/__init__.py +++ b/lxc/__init__.py @@ -90,6 +90,32 @@ def create(name, config_file=None, template=None, backing_store=None, template_o return 1 +def clone(name, original, snapshot=False): + ''' + Clones a container. + ''' + + if exists(name): + raise ContainerAlreadyExists("The Container %s is already created!" % name) + if not exists(original): + raise ContainerNotExists("The container (%s) does not exist!" % original) + + cmd = "lxc-clone -n %s -o %s" % (name, original) + + if snapshot: + cmd += ' -s' + + if subprocess.check_call('%s >> /dev/null' % cmd, shell=True) == 0: + if not exists(name): + _logger.critical("The Container %s doesn't seem to be cloned! (name: %s)", original, name) + raise ContainerNotExists("The container (%s) does not exist!" % name) + + _logger.info("Container %s has been cloned to %s", original, name) + return 0 + else: + return 1 + + def exists(name): ''' checks if a given container is defined or not @@ -321,4 +347,4 @@ def th(): subprocess.check_call(cmd) callback() _logger.info("Waiting on states %s for container %s", states, name) - threading.Thread(target=th).start() \ No newline at end of file + threading.Thread(target=th).start()