From da69d8b44343cf528a0c92eb5410c615d4ea8cb8 Mon Sep 17 00:00:00 2001
From: Seblu <sebastien.luttringer@smartjog.com>
Date: Fri, 6 May 2011 14:44:53 +0200
Subject: [PATCH] Add clone command

---
 cccli/command/command.py |  1 +
 cccli/command/vm.py      | 46 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)

diff --git a/cccli/command/command.py b/cccli/command/command.py
index 90f42ad..61bafac 100644
--- a/cccli/command/command.py
+++ b/cccli/command/command.py
@@ -127,6 +127,7 @@ class TqlCommand(OptionCommand):
         _tql_index: is index in args where filter should be appended (def: 1)
         _tql_print: print tql with filter
         _exception: catch or not RPCError exception
+        _direct: call directly, no listing before
         '''
 
         # set rpccall default option value
diff --git a/cccli/command/vm.py b/cccli/command/vm.py
index f5db632..d9d44c4 100644
--- a/cccli/command/vm.py
+++ b/cccli/command/vm.py
@@ -103,3 +103,49 @@ class Command_undefine(TqlCommand):
             raise cmdBadArgument()
         # rpc call
         self.rpccall("undefine", self.args[0], self.options.clean)
+
+class Command_clone(TqlCommand):
+    '''Clone vm'''
+
+    def __init__(self, cli, argv0):
+        TqlCommand.__init__(self, cli, argv0)
+        self.set_usage("%prog [options] <source tql> <dest tql> <name>")
+
+    def __call__(self, argv):
+        # Parse argline
+        self.parse_args(argv)
+        # Check args count
+        if len(self.args) != 3:
+            raise cmdBadArgument()
+        stql = self.args[0]
+        dtql = self.args[1]
+        newname = self.args[2]
+        if self.options.direct:
+            self.rpccall("clone", stql, dtql, newname)
+        else:
+            # list stql
+            self.printer.out("<= Source VM:")
+            if not self.options.raw:
+                self.tql_filter = "&r=vm$status"
+            o = self.rpccall("list", stql, _direct=True, _status=True)
+            if len(o["objects"]) != 1:
+                raise cmdError("You must select only one VM")
+            # list dtql
+            self.printer.out("=> Destination HV:")
+            if not self.options.raw:
+                self.tql_filter = "&r=hv$nvm"
+            o = self.rpccall("list", dtql, _direct=True, _status=True)
+            if len(o["objects"]) != 1:
+                raise cmdError("You must select only one HV")
+            # check if destination vm does not exist
+            o = self.rpccall("list", "%s&h:%s"%(dtql,newname), _direct=True, _status=False)
+            if not self.options.raw:
+                self.tql_filter = "&r=vm"
+            if len(o["objects"]) != 0:
+                raise cmdError("Destination VM exist on HV")
+            # ask confirmation
+            if self.printer.ask("Do you confirm? (I'm god) ") != "I'm god":
+                raise cmdWarning("User resign")
+            # run migration
+            self.tql_filter = ""
+            self.rpccall("clone", stql, dtql, newname, _direct=True)
-- 
GitLab