Newer
Older
'''
from cccli.exception import *
from sjrpc.core.exceptions import *
from cccli.printer import Printer, color
from cccli.command.command import TqlCommand
class Command_migrate(TqlCommand):
'''Migrate vm'''
def __init__(self, cli, argv0):
TqlCommand.__init__(self, cli, argv0)
self.set_usage("%prog [options] <source tql> <dest tql>")
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
self.remove_option("--direct")
self.remove_option("--raw")
self.remove_option("--print-tql")
self.add_option("-l", "--list", action="store_true", dest="list", default=False,
help="List migration types and algo")
self.add_option("-t", "--type", action="store", dest="type", default="",
help="Selection migration type")
self.add_option("-a", "--algo", action="store", dest="algo", default="",
help="Select migration algorithm")
def __call__(self, argv):
# Parse argline
self.parse_args(argv)
# Retrieve election types
self.etypes = self.get_electiontypes()
# Check args and do listings
if self.options.list:
self.list()
return
elif self.options.type not in self.etypes:
raise cmdBadArgument("No such type: %s"%self.options.type)
elif self.options.algo not in self.etypes[self.options.type]:
raise cmdBadArgument("No such algo: %s for type: %s"%(self.options.algo, self.options.type))
elif len(self.args) != 2:
raise cmdBadArgument()
stql = self.args[0]
dtql = self.args[1]
# election(query_vm, query_dest, mtype='cold', algo='fair', **kwargs)
scrutin = self.rpccall("election", stql, dtql,
mtype=self.options.type,
algo=self.options.algo,
_direct=True, _status=False)
# check election result
if len(scrutin) == 0:
raise cmdError("No migration plan found")
# print election
for (i,o) in enumerate(scrutin):
if self.options.index:
self.printer.out("[%d] "%i, nl="")
self.printer.out("%s%s %s-> %s%s%s (%s)"%(
self.tdc("id"),
o["sid"],
color["reset"],
self.tdc("id"),
o["did"],
color["reset"],
o["type"]))
# ask confirmation
if self.printer.ask("Do you confirm election? (Yes baby) ") != "Yes baby":
raise cmdWarning("User resign")
# run migration
self.rpccall("migrate", scrutin, _direct=True)
def get_electiontypes(self):
'''Return a list of migration type'''
try:
return self.cli.rpc.call("electiontypes")
except RpcError as e:
raise cmdError(e)
def list(self):
'''Print a list of migration type'''
self.printer.out("Migration types: Election Algo")
for t in self.etypes.keys():
self.printer.out("%s: %s"%(t, ",".join(self.etypes[t])))