cd0af25c8ea05b01c1f587309541b1340bb24e32
[ganeti-local] / lib / cmdlib.py
1 #
2 #
3
4 # Copyright (C) 2006, 2007 Google Inc.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
20
21
22 """Module implementing the master-side code."""
23
24 # pylint: disable-msg=W0613,W0201
25
26 import os
27 import os.path
28 import sha
29 import time
30 import tempfile
31 import re
32 import platform
33
34 from ganeti import rpc
35 from ganeti import ssh
36 from ganeti import logger
37 from ganeti import utils
38 from ganeti import errors
39 from ganeti import hypervisor
40 from ganeti import config
41 from ganeti import constants
42 from ganeti import objects
43 from ganeti import opcodes
44 from ganeti import ssconf
45
46 class LogicalUnit(object):
47   """Logical Unit base class.
48
49   Subclasses must follow these rules:
50     - implement CheckPrereq which also fills in the opcode instance
51       with all the fields (even if as None)
52     - implement Exec
53     - implement BuildHooksEnv
54     - redefine HPATH and HTYPE
55     - optionally redefine their run requirements (REQ_CLUSTER,
56       REQ_MASTER); note that all commands require root permissions
57
58   """
59   HPATH = None
60   HTYPE = None
61   _OP_REQP = []
62   REQ_CLUSTER = True
63   REQ_MASTER = True
64
65   def __init__(self, processor, op, cfg, sstore):
66     """Constructor for LogicalUnit.
67
68     This needs to be overriden in derived classes in order to check op
69     validity.
70
71     """
72     self.proc = processor
73     self.op = op
74     self.cfg = cfg
75     self.sstore = sstore
76     for attr_name in self._OP_REQP:
77       attr_val = getattr(op, attr_name, None)
78       if attr_val is None:
79         raise errors.OpPrereqError("Required parameter '%s' missing" %
80                                    attr_name)
81     if self.REQ_CLUSTER:
82       if not cfg.IsCluster():
83         raise errors.OpPrereqError("Cluster not initialized yet,"
84                                    " use 'gnt-cluster init' first.")
85       if self.REQ_MASTER:
86         master = sstore.GetMasterNode()
87         if master != utils.HostInfo().name:
88           raise errors.OpPrereqError("Commands must be run on the master"
89                                      " node %s" % master)
90
91   def CheckPrereq(self):
92     """Check prerequisites for this LU.
93
94     This method should check that the prerequisites for the execution
95     of this LU are fulfilled. It can do internode communication, but
96     it should be idempotent - no cluster or system changes are
97     allowed.
98
99     The method should raise errors.OpPrereqError in case something is
100     not fulfilled. Its return value is ignored.
101
102     This method should also update all the parameters of the opcode to
103     their canonical form; e.g. a short node name must be fully
104     expanded after this method has successfully completed (so that
105     hooks, logging, etc. work correctly).
106
107     """
108     raise NotImplementedError
109
110   def Exec(self, feedback_fn):
111     """Execute the LU.
112
113     This method should implement the actual work. It should raise
114     errors.OpExecError for failures that are somewhat dealt with in
115     code, or expected.
116
117     """
118     raise NotImplementedError
119
120   def BuildHooksEnv(self):
121     """Build hooks environment for this LU.
122
123     This method should return a three-node tuple consisting of: a dict
124     containing the environment that will be used for running the
125     specific hook for this LU, a list of node names on which the hook
126     should run before the execution, and a list of node names on which
127     the hook should run after the execution.
128
129     The keys of the dict must not have 'GANETI_' prefixed as this will
130     be handled in the hooks runner. Also note additional keys will be
131     added by the hooks runner. If the LU doesn't define any
132     environment, an empty dict (and not None) should be returned.
133
134     As for the node lists, the master should not be included in the
135     them, as it will be added by the hooks runner in case this LU
136     requires a cluster to run on (otherwise we don't have a node
137     list). No nodes should be returned as an empty list (and not
138     None).
139
140     Note that if the HPATH for a LU class is None, this function will
141     not be called.
142
143     """
144     raise NotImplementedError
145
146
147 class NoHooksLU(LogicalUnit):
148   """Simple LU which runs no hooks.
149
150   This LU is intended as a parent for other LogicalUnits which will
151   run no hooks, in order to reduce duplicate code.
152
153   """
154   HPATH = None
155   HTYPE = None
156
157   def BuildHooksEnv(self):
158     """Build hooks env.
159
160     This is a no-op, since we don't run hooks.
161
162     """
163     return {}, [], []
164
165
166 def _AddHostToEtcHosts(hostname):
167   """Wrapper around utils.SetEtcHostsEntry.
168
169   """
170   hi = utils.HostInfo(name=hostname)
171   utils.SetEtcHostsEntry(constants.ETC_HOSTS, hi.ip, hi.name, [hi.ShortName()])
172
173
174 def _RemoveHostFromEtcHosts(hostname):
175   """Wrapper around utils.RemoveEtcHostsEntry.
176
177   """
178   hi = utils.HostInfo(name=hostname)
179   utils.RemoveEtcHostsEntry(constants.ETC_HOSTS, hi.name)
180   utils.RemoveEtcHostsEntry(constants.ETC_HOSTS, hi.ShortName())
181
182
183 def _GetWantedNodes(lu, nodes):
184   """Returns list of checked and expanded node names.
185
186   Args:
187     nodes: List of nodes (strings) or None for all
188
189   """
190   if not isinstance(nodes, list):
191     raise errors.OpPrereqError("Invalid argument type 'nodes'")
192
193   if nodes:
194     wanted = []
195
196     for name in nodes:
197       node = lu.cfg.ExpandNodeName(name)
198       if node is None:
199         raise errors.OpPrereqError("No such node name '%s'" % name)
200       wanted.append(node)
201
202   else:
203     wanted = lu.cfg.GetNodeList()
204   return utils.NiceSort(wanted)
205
206
207 def _GetWantedInstances(lu, instances):
208   """Returns list of checked and expanded instance names.
209
210   Args:
211     instances: List of instances (strings) or None for all
212
213   """
214   if not isinstance(instances, list):
215     raise errors.OpPrereqError("Invalid argument type 'instances'")
216
217   if instances:
218     wanted = []
219
220     for name in instances:
221       instance = lu.cfg.ExpandInstanceName(name)
222       if instance is None:
223         raise errors.OpPrereqError("No such instance name '%s'" % name)
224       wanted.append(instance)
225
226   else:
227     wanted = lu.cfg.GetInstanceList()
228   return utils.NiceSort(wanted)
229
230
231 def _CheckOutputFields(static, dynamic, selected):
232   """Checks whether all selected fields are valid.
233
234   Args:
235     static: Static fields
236     dynamic: Dynamic fields
237
238   """
239   static_fields = frozenset(static)
240   dynamic_fields = frozenset(dynamic)
241
242   all_fields = static_fields | dynamic_fields
243
244   if not all_fields.issuperset(selected):
245     raise errors.OpPrereqError("Unknown output fields selected: %s"
246                                % ",".join(frozenset(selected).
247                                           difference(all_fields)))
248
249
250 def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
251                           memory, vcpus, nics):
252   """Builds instance related env variables for hooks from single variables.
253
254   Args:
255     secondary_nodes: List of secondary nodes as strings
256   """
257   env = {
258     "OP_TARGET": name,
259     "INSTANCE_NAME": name,
260     "INSTANCE_PRIMARY": primary_node,
261     "INSTANCE_SECONDARIES": " ".join(secondary_nodes),
262     "INSTANCE_OS_TYPE": os_type,
263     "INSTANCE_STATUS": status,
264     "INSTANCE_MEMORY": memory,
265     "INSTANCE_VCPUS": vcpus,
266   }
267
268   if nics:
269     nic_count = len(nics)
270     for idx, (ip, bridge) in enumerate(nics):
271       if ip is None:
272         ip = ""
273       env["INSTANCE_NIC%d_IP" % idx] = ip
274       env["INSTANCE_NIC%d_BRIDGE" % idx] = bridge
275   else:
276     nic_count = 0
277
278   env["INSTANCE_NIC_COUNT"] = nic_count
279
280   return env
281
282
283 def _BuildInstanceHookEnvByObject(instance, override=None):
284   """Builds instance related env variables for hooks from an object.
285
286   Args:
287     instance: objects.Instance object of instance
288     override: dict of values to override
289   """
290   args = {
291     'name': instance.name,
292     'primary_node': instance.primary_node,
293     'secondary_nodes': instance.secondary_nodes,
294     'os_type': instance.os,
295     'status': instance.os,
296     'memory': instance.memory,
297     'vcpus': instance.vcpus,
298     'nics': [(nic.ip, nic.bridge) for nic in instance.nics],
299   }
300   if override:
301     args.update(override)
302   return _BuildInstanceHookEnv(**args)
303
304
305 def _UpdateKnownHosts(fullnode, ip, pubkey):
306   """Ensure a node has a correct known_hosts entry.
307
308   Args:
309     fullnode - Fully qualified domain name of host. (str)
310     ip       - IPv4 address of host (str)
311     pubkey   - the public key of the cluster
312
313   """
314   if os.path.exists(constants.SSH_KNOWN_HOSTS_FILE):
315     f = open(constants.SSH_KNOWN_HOSTS_FILE, 'r+')
316   else:
317     f = open(constants.SSH_KNOWN_HOSTS_FILE, 'w+')
318
319   inthere = False
320
321   save_lines = []
322   add_lines = []
323   removed = False
324
325   for rawline in f:
326     logger.Debug('read %s' % (repr(rawline),))
327
328     parts = rawline.rstrip('\r\n').split()
329
330     # Ignore unwanted lines
331     if len(parts) >= 3 and not rawline.lstrip()[0] == '#':
332       fields = parts[0].split(',')
333       key = parts[2]
334
335       haveall = True
336       havesome = False
337       for spec in [ ip, fullnode ]:
338         if spec not in fields:
339           haveall = False
340         if spec in fields:
341           havesome = True
342
343       logger.Debug("key, pubkey = %s." % (repr((key, pubkey)),))
344       if haveall and key == pubkey:
345         inthere = True
346         save_lines.append(rawline)
347         logger.Debug("Keeping known_hosts '%s'." % (repr(rawline),))
348         continue
349
350       if havesome and (not haveall or key != pubkey):
351         removed = True
352         logger.Debug("Discarding known_hosts '%s'." % (repr(rawline),))
353         continue
354
355     save_lines.append(rawline)
356
357   if not inthere:
358     add_lines.append('%s,%s ssh-rsa %s\n' % (fullnode, ip, pubkey))
359     logger.Debug("Adding known_hosts '%s'." % (repr(add_lines[-1]),))
360
361   if removed:
362     save_lines = save_lines + add_lines
363
364     # Write a new file and replace old.
365     fd, tmpname = tempfile.mkstemp('.tmp', 'known_hosts.',
366                                    constants.DATA_DIR)
367     newfile = os.fdopen(fd, 'w')
368     try:
369       newfile.write(''.join(save_lines))
370     finally:
371       newfile.close()
372     logger.Debug("Wrote new known_hosts.")
373     os.rename(tmpname, constants.SSH_KNOWN_HOSTS_FILE)
374
375   elif add_lines:
376     # Simply appending a new line will do the trick.
377     f.seek(0, 2)
378     for add in add_lines:
379       f.write(add)
380
381   f.close()
382
383
384 def _HasValidVG(vglist, vgname):
385   """Checks if the volume group list is valid.
386
387   A non-None return value means there's an error, and the return value
388   is the error message.
389
390   """
391   vgsize = vglist.get(vgname, None)
392   if vgsize is None:
393     return "volume group '%s' missing" % vgname
394   elif vgsize < 20480:
395     return ("volume group '%s' too small (20480MiB required, %dMib found)" %
396             (vgname, vgsize))
397   return None
398
399
400 def _InitSSHSetup(node):
401   """Setup the SSH configuration for the cluster.
402
403
404   This generates a dsa keypair for root, adds the pub key to the
405   permitted hosts and adds the hostkey to its own known hosts.
406
407   Args:
408     node: the name of this host as a fqdn
409
410   """
411   priv_key, pub_key, auth_keys = ssh.GetUserFiles(constants.GANETI_RUNAS)
412
413   for name in priv_key, pub_key:
414     if os.path.exists(name):
415       utils.CreateBackup(name)
416     utils.RemoveFile(name)
417
418   result = utils.RunCmd(["ssh-keygen", "-t", "dsa",
419                          "-f", priv_key,
420                          "-q", "-N", ""])
421   if result.failed:
422     raise errors.OpExecError("Could not generate ssh keypair, error %s" %
423                              result.output)
424
425   f = open(pub_key, 'r')
426   try:
427     utils.AddAuthorizedKey(auth_keys, f.read(8192))
428   finally:
429     f.close()
430
431
432 def _InitGanetiServerSetup(ss):
433   """Setup the necessary configuration for the initial node daemon.
434
435   This creates the nodepass file containing the shared password for
436   the cluster and also generates the SSL certificate.
437
438   """
439   # Create pseudo random password
440   randpass = sha.new(os.urandom(64)).hexdigest()
441   # and write it into sstore
442   ss.SetKey(ss.SS_NODED_PASS, randpass)
443
444   result = utils.RunCmd(["openssl", "req", "-new", "-newkey", "rsa:1024",
445                          "-days", str(365*5), "-nodes", "-x509",
446                          "-keyout", constants.SSL_CERT_FILE,
447                          "-out", constants.SSL_CERT_FILE, "-batch"])
448   if result.failed:
449     raise errors.OpExecError("could not generate server ssl cert, command"
450                              " %s had exitcode %s and error message %s" %
451                              (result.cmd, result.exit_code, result.output))
452
453   os.chmod(constants.SSL_CERT_FILE, 0400)
454
455   result = utils.RunCmd([constants.NODE_INITD_SCRIPT, "restart"])
456
457   if result.failed:
458     raise errors.OpExecError("Could not start the node daemon, command %s"
459                              " had exitcode %s and error %s" %
460                              (result.cmd, result.exit_code, result.output))
461
462
463 def _CheckInstanceBridgesExist(instance):
464   """Check that the brigdes needed by an instance exist.
465
466   """
467   # check bridges existance
468   brlist = [nic.bridge for nic in instance.nics]
469   if not rpc.call_bridges_exist(instance.primary_node, brlist):
470     raise errors.OpPrereqError("one or more target bridges %s does not"
471                                " exist on destination node '%s'" %
472                                (brlist, instance.primary_node))
473
474
475 class LUInitCluster(LogicalUnit):
476   """Initialise the cluster.
477
478   """
479   HPATH = "cluster-init"
480   HTYPE = constants.HTYPE_CLUSTER
481   _OP_REQP = ["cluster_name", "hypervisor_type", "vg_name", "mac_prefix",
482               "def_bridge", "master_netdev"]
483   REQ_CLUSTER = False
484
485   def BuildHooksEnv(self):
486     """Build hooks env.
487
488     Notes: Since we don't require a cluster, we must manually add
489     ourselves in the post-run node list.
490
491     """
492     env = {"OP_TARGET": self.op.cluster_name}
493     return env, [], [self.hostname.name]
494
495   def CheckPrereq(self):
496     """Verify that the passed name is a valid one.
497
498     """
499     if config.ConfigWriter.IsCluster():
500       raise errors.OpPrereqError("Cluster is already initialised")
501
502     if self.op.hypervisor_type == constants.HT_XEN_HVM31:
503       if not os.path.exists(constants.VNC_PASSWORD_FILE):
504         raise errors.OpPrereqError("Please prepare the cluster VNC"
505                                    "password file %s" %
506                                    constants.VNC_PASSWORD_FILE)
507
508     self.hostname = hostname = utils.HostInfo()
509
510     if hostname.ip.startswith("127."):
511       raise errors.OpPrereqError("This host's IP resolves to the private"
512                                  " range (%s). Please fix DNS or /etc/hosts." %
513                                  (hostname.ip,))
514
515     self.clustername = clustername = utils.HostInfo(self.op.cluster_name)
516
517     if not utils.TcpPing(constants.LOCALHOST_IP_ADDRESS, hostname.ip,
518                          constants.DEFAULT_NODED_PORT):
519       raise errors.OpPrereqError("Inconsistency: this host's name resolves"
520                                  " to %s,\nbut this ip address does not"
521                                  " belong to this host."
522                                  " Aborting." % hostname.ip)
523
524     secondary_ip = getattr(self.op, "secondary_ip", None)
525     if secondary_ip and not utils.IsValidIP(secondary_ip):
526       raise errors.OpPrereqError("Invalid secondary ip given")
527     if (secondary_ip and
528         secondary_ip != hostname.ip and
529         (not utils.TcpPing(constants.LOCALHOST_IP_ADDRESS, secondary_ip,
530                            constants.DEFAULT_NODED_PORT))):
531       raise errors.OpPrereqError("You gave %s as secondary IP,"
532                                  " but it does not belong to this host." %
533                                  secondary_ip)
534     self.secondary_ip = secondary_ip
535
536     # checks presence of the volume group given
537     vgstatus = _HasValidVG(utils.ListVolumeGroups(), self.op.vg_name)
538
539     if vgstatus:
540       raise errors.OpPrereqError("Error: %s" % vgstatus)
541
542     if not re.match("^[0-9a-z]{2}:[0-9a-z]{2}:[0-9a-z]{2}$",
543                     self.op.mac_prefix):
544       raise errors.OpPrereqError("Invalid mac prefix given '%s'" %
545                                  self.op.mac_prefix)
546
547     if self.op.hypervisor_type not in constants.HYPER_TYPES:
548       raise errors.OpPrereqError("Invalid hypervisor type given '%s'" %
549                                  self.op.hypervisor_type)
550
551     result = utils.RunCmd(["ip", "link", "show", "dev", self.op.master_netdev])
552     if result.failed:
553       raise errors.OpPrereqError("Invalid master netdev given (%s): '%s'" %
554                                  (self.op.master_netdev,
555                                   result.output.strip()))
556
557     if not (os.path.isfile(constants.NODE_INITD_SCRIPT) and
558             os.access(constants.NODE_INITD_SCRIPT, os.X_OK)):
559       raise errors.OpPrereqError("Init.d script '%s' missing or not"
560                                  " executable." % constants.NODE_INITD_SCRIPT)
561
562   def Exec(self, feedback_fn):
563     """Initialize the cluster.
564
565     """
566     clustername = self.clustername
567     hostname = self.hostname
568
569     # set up the simple store
570     self.sstore = ss = ssconf.SimpleStore()
571     ss.SetKey(ss.SS_HYPERVISOR, self.op.hypervisor_type)
572     ss.SetKey(ss.SS_MASTER_NODE, hostname.name)
573     ss.SetKey(ss.SS_MASTER_IP, clustername.ip)
574     ss.SetKey(ss.SS_MASTER_NETDEV, self.op.master_netdev)
575     ss.SetKey(ss.SS_CLUSTER_NAME, clustername.name)
576
577     # set up the inter-node password and certificate
578     _InitGanetiServerSetup(ss)
579
580     # start the master ip
581     rpc.call_node_start_master(hostname.name)
582
583     # set up ssh config and /etc/hosts
584     f = open(constants.SSH_HOST_RSA_PUB, 'r')
585     try:
586       sshline = f.read()
587     finally:
588       f.close()
589     sshkey = sshline.split(" ")[1]
590
591     _AddHostToEtcHosts(hostname.name)
592
593     _UpdateKnownHosts(hostname.name, hostname.ip, sshkey)
594
595     _InitSSHSetup(hostname.name)
596
597     # init of cluster config file
598     self.cfg = cfgw = config.ConfigWriter()
599     cfgw.InitConfig(hostname.name, hostname.ip, self.secondary_ip,
600                     sshkey, self.op.mac_prefix,
601                     self.op.vg_name, self.op.def_bridge)
602
603
604 class LUDestroyCluster(NoHooksLU):
605   """Logical unit for destroying the cluster.
606
607   """
608   _OP_REQP = []
609
610   def CheckPrereq(self):
611     """Check prerequisites.
612
613     This checks whether the cluster is empty.
614
615     Any errors are signalled by raising errors.OpPrereqError.
616
617     """
618     master = self.sstore.GetMasterNode()
619
620     nodelist = self.cfg.GetNodeList()
621     if len(nodelist) != 1 or nodelist[0] != master:
622       raise errors.OpPrereqError("There are still %d node(s) in"
623                                  " this cluster." % (len(nodelist) - 1))
624     instancelist = self.cfg.GetInstanceList()
625     if instancelist:
626       raise errors.OpPrereqError("There are still %d instance(s) in"
627                                  " this cluster." % len(instancelist))
628
629   def Exec(self, feedback_fn):
630     """Destroys the cluster.
631
632     """
633     master = self.sstore.GetMasterNode()
634     priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
635     utils.CreateBackup(priv_key)
636     utils.CreateBackup(pub_key)
637     rpc.call_node_leave_cluster(master)
638
639
640 class LUVerifyCluster(NoHooksLU):
641   """Verifies the cluster status.
642
643   """
644   _OP_REQP = []
645
646   def _VerifyNode(self, node, file_list, local_cksum, vglist, node_result,
647                   remote_version, feedback_fn):
648     """Run multiple tests against a node.
649
650     Test list:
651       - compares ganeti version
652       - checks vg existance and size > 20G
653       - checks config file checksum
654       - checks ssh to other nodes
655
656     Args:
657       node: name of the node to check
658       file_list: required list of files
659       local_cksum: dictionary of local files and their checksums
660
661     """
662     # compares ganeti version
663     local_version = constants.PROTOCOL_VERSION
664     if not remote_version:
665       feedback_fn(" - ERROR: connection to %s failed" % (node))
666       return True
667
668     if local_version != remote_version:
669       feedback_fn("  - ERROR: sw version mismatch: master %s, node(%s) %s" %
670                       (local_version, node, remote_version))
671       return True
672
673     # checks vg existance and size > 20G
674
675     bad = False
676     if not vglist:
677       feedback_fn("  - ERROR: unable to check volume groups on node %s." %
678                       (node,))
679       bad = True
680     else:
681       vgstatus = _HasValidVG(vglist, self.cfg.GetVGName())
682       if vgstatus:
683         feedback_fn("  - ERROR: %s on node %s" % (vgstatus, node))
684         bad = True
685
686     # checks config file checksum
687     # checks ssh to any
688
689     if 'filelist' not in node_result:
690       bad = True
691       feedback_fn("  - ERROR: node hasn't returned file checksum data")
692     else:
693       remote_cksum = node_result['filelist']
694       for file_name in file_list:
695         if file_name not in remote_cksum:
696           bad = True
697           feedback_fn("  - ERROR: file '%s' missing" % file_name)
698         elif remote_cksum[file_name] != local_cksum[file_name]:
699           bad = True
700           feedback_fn("  - ERROR: file '%s' has wrong checksum" % file_name)
701
702     if 'nodelist' not in node_result:
703       bad = True
704       feedback_fn("  - ERROR: node hasn't returned node connectivity data")
705     else:
706       if node_result['nodelist']:
707         bad = True
708         for node in node_result['nodelist']:
709           feedback_fn("  - ERROR: communication with node '%s': %s" %
710                           (node, node_result['nodelist'][node]))
711     hyp_result = node_result.get('hypervisor', None)
712     if hyp_result is not None:
713       feedback_fn("  - ERROR: hypervisor verify failure: '%s'" % hyp_result)
714     return bad
715
716   def _VerifyInstance(self, instance, node_vol_is, node_instance, feedback_fn):
717     """Verify an instance.
718
719     This function checks to see if the required block devices are
720     available on the instance's node.
721
722     """
723     bad = False
724
725     instancelist = self.cfg.GetInstanceList()
726     if not instance in instancelist:
727       feedback_fn("  - ERROR: instance %s not in instance list %s" %
728                       (instance, instancelist))
729       bad = True
730
731     instanceconfig = self.cfg.GetInstanceInfo(instance)
732     node_current = instanceconfig.primary_node
733
734     node_vol_should = {}
735     instanceconfig.MapLVsByNode(node_vol_should)
736
737     for node in node_vol_should:
738       for volume in node_vol_should[node]:
739         if node not in node_vol_is or volume not in node_vol_is[node]:
740           feedback_fn("  - ERROR: volume %s missing on node %s" %
741                           (volume, node))
742           bad = True
743
744     if not instanceconfig.status == 'down':
745       if not instance in node_instance[node_current]:
746         feedback_fn("  - ERROR: instance %s not running on node %s" %
747                         (instance, node_current))
748         bad = True
749
750     for node in node_instance:
751       if (not node == node_current):
752         if instance in node_instance[node]:
753           feedback_fn("  - ERROR: instance %s should not run on node %s" %
754                           (instance, node))
755           bad = True
756
757     return bad
758
759   def _VerifyOrphanVolumes(self, node_vol_should, node_vol_is, feedback_fn):
760     """Verify if there are any unknown volumes in the cluster.
761
762     The .os, .swap and backup volumes are ignored. All other volumes are
763     reported as unknown.
764
765     """
766     bad = False
767
768     for node in node_vol_is:
769       for volume in node_vol_is[node]:
770         if node not in node_vol_should or volume not in node_vol_should[node]:
771           feedback_fn("  - ERROR: volume %s on node %s should not exist" %
772                       (volume, node))
773           bad = True
774     return bad
775
776   def _VerifyOrphanInstances(self, instancelist, node_instance, feedback_fn):
777     """Verify the list of running instances.
778
779     This checks what instances are running but unknown to the cluster.
780
781     """
782     bad = False
783     for node in node_instance:
784       for runninginstance in node_instance[node]:
785         if runninginstance not in instancelist:
786           feedback_fn("  - ERROR: instance %s on node %s should not exist" %
787                           (runninginstance, node))
788           bad = True
789     return bad
790
791   def CheckPrereq(self):
792     """Check prerequisites.
793
794     This has no prerequisites.
795
796     """
797     pass
798
799   def Exec(self, feedback_fn):
800     """Verify integrity of cluster, performing various test on nodes.
801
802     """
803     bad = False
804     feedback_fn("* Verifying global settings")
805     for msg in self.cfg.VerifyConfig():
806       feedback_fn("  - ERROR: %s" % msg)
807
808     vg_name = self.cfg.GetVGName()
809     nodelist = utils.NiceSort(self.cfg.GetNodeList())
810     instancelist = utils.NiceSort(self.cfg.GetInstanceList())
811     node_volume = {}
812     node_instance = {}
813
814     # FIXME: verify OS list
815     # do local checksums
816     file_names = list(self.sstore.GetFileList())
817     file_names.append(constants.SSL_CERT_FILE)
818     file_names.append(constants.CLUSTER_CONF_FILE)
819     local_checksums = utils.FingerprintFiles(file_names)
820
821     feedback_fn("* Gathering data (%d nodes)" % len(nodelist))
822     all_volumeinfo = rpc.call_volume_list(nodelist, vg_name)
823     all_instanceinfo = rpc.call_instance_list(nodelist)
824     all_vglist = rpc.call_vg_list(nodelist)
825     node_verify_param = {
826       'filelist': file_names,
827       'nodelist': nodelist,
828       'hypervisor': None,
829       }
830     all_nvinfo = rpc.call_node_verify(nodelist, node_verify_param)
831     all_rversion = rpc.call_version(nodelist)
832
833     for node in nodelist:
834       feedback_fn("* Verifying node %s" % node)
835       result = self._VerifyNode(node, file_names, local_checksums,
836                                 all_vglist[node], all_nvinfo[node],
837                                 all_rversion[node], feedback_fn)
838       bad = bad or result
839
840       # node_volume
841       volumeinfo = all_volumeinfo[node]
842
843       if isinstance(volumeinfo, basestring):
844         feedback_fn("  - ERROR: LVM problem on node %s: %s" %
845                     (node, volumeinfo[-400:].encode('string_escape')))
846         bad = True
847         node_volume[node] = {}
848       elif not isinstance(volumeinfo, dict):
849         feedback_fn("  - ERROR: connection to %s failed" % (node,))
850         bad = True
851         continue
852       else:
853         node_volume[node] = volumeinfo
854
855       # node_instance
856       nodeinstance = all_instanceinfo[node]
857       if type(nodeinstance) != list:
858         feedback_fn("  - ERROR: connection to %s failed" % (node,))
859         bad = True
860         continue
861
862       node_instance[node] = nodeinstance
863
864     node_vol_should = {}
865
866     for instance in instancelist:
867       feedback_fn("* Verifying instance %s" % instance)
868       result =  self._VerifyInstance(instance, node_volume, node_instance,
869                                      feedback_fn)
870       bad = bad or result
871
872       inst_config = self.cfg.GetInstanceInfo(instance)
873
874       inst_config.MapLVsByNode(node_vol_should)
875
876     feedback_fn("* Verifying orphan volumes")
877     result = self._VerifyOrphanVolumes(node_vol_should, node_volume,
878                                        feedback_fn)
879     bad = bad or result
880
881     feedback_fn("* Verifying remaining instances")
882     result = self._VerifyOrphanInstances(instancelist, node_instance,
883                                          feedback_fn)
884     bad = bad or result
885
886     return int(bad)
887
888
889 class LUVerifyDisks(NoHooksLU):
890   """Verifies the cluster disks status.
891
892   """
893   _OP_REQP = []
894
895   def CheckPrereq(self):
896     """Check prerequisites.
897
898     This has no prerequisites.
899
900     """
901     pass
902
903   def Exec(self, feedback_fn):
904     """Verify integrity of cluster disks.
905
906     """
907     result = res_nodes, res_nlvm, res_instances, res_missing = [], {}, [], {}
908
909     vg_name = self.cfg.GetVGName()
910     nodes = utils.NiceSort(self.cfg.GetNodeList())
911     instances = [self.cfg.GetInstanceInfo(name)
912                  for name in self.cfg.GetInstanceList()]
913
914     nv_dict = {}
915     for inst in instances:
916       inst_lvs = {}
917       if (inst.status != "up" or
918           inst.disk_template not in constants.DTS_NET_MIRROR):
919         continue
920       inst.MapLVsByNode(inst_lvs)
921       # transform { iname: {node: [vol,],},} to {(node, vol): iname}
922       for node, vol_list in inst_lvs.iteritems():
923         for vol in vol_list:
924           nv_dict[(node, vol)] = inst
925
926     if not nv_dict:
927       return result
928
929     node_lvs = rpc.call_volume_list(nodes, vg_name)
930
931     to_act = set()
932     for node in nodes:
933       # node_volume
934       lvs = node_lvs[node]
935
936       if isinstance(lvs, basestring):
937         logger.Info("error enumerating LVs on node %s: %s" % (node, lvs))
938         res_nlvm[node] = lvs
939       elif not isinstance(lvs, dict):
940         logger.Info("connection to node %s failed or invalid data returned" %
941                     (node,))
942         res_nodes.append(node)
943         continue
944
945       for lv_name, (_, lv_inactive, lv_online) in lvs.iteritems():
946         inst = nv_dict.pop((node, lv_name), None)
947         if (not lv_online and inst is not None
948             and inst.name not in res_instances):
949             res_instances.append(inst.name)
950
951     # any leftover items in nv_dict are missing LVs, let's arrange the
952     # data better
953     for key, inst in nv_dict.iteritems():
954       if inst.name not in res_missing:
955         res_missing[inst.name] = []
956       res_missing[inst.name].append(key)
957
958     return result
959
960
961 class LURenameCluster(LogicalUnit):
962   """Rename the cluster.
963
964   """
965   HPATH = "cluster-rename"
966   HTYPE = constants.HTYPE_CLUSTER
967   _OP_REQP = ["name"]
968
969   def BuildHooksEnv(self):
970     """Build hooks env.
971
972     """
973     env = {
974       "OP_TARGET": self.op.sstore.GetClusterName(),
975       "NEW_NAME": self.op.name,
976       }
977     mn = self.sstore.GetMasterNode()
978     return env, [mn], [mn]
979
980   def CheckPrereq(self):
981     """Verify that the passed name is a valid one.
982
983     """
984     hostname = utils.HostInfo(self.op.name)
985
986     new_name = hostname.name
987     self.ip = new_ip = hostname.ip
988     old_name = self.sstore.GetClusterName()
989     old_ip = self.sstore.GetMasterIP()
990     if new_name == old_name and new_ip == old_ip:
991       raise errors.OpPrereqError("Neither the name nor the IP address of the"
992                                  " cluster has changed")
993     if new_ip != old_ip:
994       result = utils.RunCmd(["fping", "-q", new_ip])
995       if not result.failed:
996         raise errors.OpPrereqError("The given cluster IP address (%s) is"
997                                    " reachable on the network. Aborting." %
998                                    new_ip)
999
1000     self.op.name = new_name
1001
1002   def Exec(self, feedback_fn):
1003     """Rename the cluster.
1004
1005     """
1006     clustername = self.op.name
1007     ip = self.ip
1008     ss = self.sstore
1009
1010     # shutdown the master IP
1011     master = ss.GetMasterNode()
1012     if not rpc.call_node_stop_master(master):
1013       raise errors.OpExecError("Could not disable the master role")
1014
1015     try:
1016       # modify the sstore
1017       ss.SetKey(ss.SS_MASTER_IP, ip)
1018       ss.SetKey(ss.SS_CLUSTER_NAME, clustername)
1019
1020       # Distribute updated ss config to all nodes
1021       myself = self.cfg.GetNodeInfo(master)
1022       dist_nodes = self.cfg.GetNodeList()
1023       if myself.name in dist_nodes:
1024         dist_nodes.remove(myself.name)
1025
1026       logger.Debug("Copying updated ssconf data to all nodes")
1027       for keyname in [ss.SS_CLUSTER_NAME, ss.SS_MASTER_IP]:
1028         fname = ss.KeyToFilename(keyname)
1029         result = rpc.call_upload_file(dist_nodes, fname)
1030         for to_node in dist_nodes:
1031           if not result[to_node]:
1032             logger.Error("copy of file %s to node %s failed" %
1033                          (fname, to_node))
1034     finally:
1035       if not rpc.call_node_start_master(master):
1036         logger.Error("Could not re-enable the master role on the master,"
1037                      " please restart manually.")
1038
1039
1040 def _WaitForSync(cfgw, instance, proc, oneshot=False, unlock=False):
1041   """Sleep and poll for an instance's disk to sync.
1042
1043   """
1044   if not instance.disks:
1045     return True
1046
1047   if not oneshot:
1048     proc.LogInfo("Waiting for instance %s to sync disks." % instance.name)
1049
1050   node = instance.primary_node
1051
1052   for dev in instance.disks:
1053     cfgw.SetDiskID(dev, node)
1054
1055   retries = 0
1056   while True:
1057     max_time = 0
1058     done = True
1059     cumul_degraded = False
1060     rstats = rpc.call_blockdev_getmirrorstatus(node, instance.disks)
1061     if not rstats:
1062       proc.LogWarning("Can't get any data from node %s" % node)
1063       retries += 1
1064       if retries >= 10:
1065         raise errors.RemoteError("Can't contact node %s for mirror data,"
1066                                  " aborting." % node)
1067       time.sleep(6)
1068       continue
1069     retries = 0
1070     for i in range(len(rstats)):
1071       mstat = rstats[i]
1072       if mstat is None:
1073         proc.LogWarning("Can't compute data for node %s/%s" %
1074                         (node, instance.disks[i].iv_name))
1075         continue
1076       # we ignore the ldisk parameter
1077       perc_done, est_time, is_degraded, _ = mstat
1078       cumul_degraded = cumul_degraded or (is_degraded and perc_done is None)
1079       if perc_done is not None:
1080         done = False
1081         if est_time is not None:
1082           rem_time = "%d estimated seconds remaining" % est_time
1083           max_time = est_time
1084         else:
1085           rem_time = "no time estimate"
1086         proc.LogInfo("- device %s: %5.2f%% done, %s" %
1087                      (instance.disks[i].iv_name, perc_done, rem_time))
1088     if done or oneshot:
1089       break
1090
1091     if unlock:
1092       utils.Unlock('cmd')
1093     try:
1094       time.sleep(min(60, max_time))
1095     finally:
1096       if unlock:
1097         utils.Lock('cmd')
1098
1099   if done:
1100     proc.LogInfo("Instance %s's disks are in sync." % instance.name)
1101   return not cumul_degraded
1102
1103
1104 def _CheckDiskConsistency(cfgw, dev, node, on_primary, ldisk=False):
1105   """Check that mirrors are not degraded.
1106
1107   The ldisk parameter, if True, will change the test from the
1108   is_degraded attribute (which represents overall non-ok status for
1109   the device(s)) to the ldisk (representing the local storage status).
1110
1111   """
1112   cfgw.SetDiskID(dev, node)
1113   if ldisk:
1114     idx = 6
1115   else:
1116     idx = 5
1117
1118   result = True
1119   if on_primary or dev.AssembleOnSecondary():
1120     rstats = rpc.call_blockdev_find(node, dev)
1121     if not rstats:
1122       logger.ToStderr("Can't get any data from node %s" % node)
1123       result = False
1124     else:
1125       result = result and (not rstats[idx])
1126   if dev.children:
1127     for child in dev.children:
1128       result = result and _CheckDiskConsistency(cfgw, child, node, on_primary)
1129
1130   return result
1131
1132
1133 class LUDiagnoseOS(NoHooksLU):
1134   """Logical unit for OS diagnose/query.
1135
1136   """
1137   _OP_REQP = []
1138
1139   def CheckPrereq(self):
1140     """Check prerequisites.
1141
1142     This always succeeds, since this is a pure query LU.
1143
1144     """
1145     return
1146
1147   def Exec(self, feedback_fn):
1148     """Compute the list of OSes.
1149
1150     """
1151     node_list = self.cfg.GetNodeList()
1152     node_data = rpc.call_os_diagnose(node_list)
1153     if node_data == False:
1154       raise errors.OpExecError("Can't gather the list of OSes")
1155     return node_data
1156
1157
1158 class LURemoveNode(LogicalUnit):
1159   """Logical unit for removing a node.
1160
1161   """
1162   HPATH = "node-remove"
1163   HTYPE = constants.HTYPE_NODE
1164   _OP_REQP = ["node_name"]
1165
1166   def BuildHooksEnv(self):
1167     """Build hooks env.
1168
1169     This doesn't run on the target node in the pre phase as a failed
1170     node would not allows itself to run.
1171
1172     """
1173     env = {
1174       "OP_TARGET": self.op.node_name,
1175       "NODE_NAME": self.op.node_name,
1176       }
1177     all_nodes = self.cfg.GetNodeList()
1178     all_nodes.remove(self.op.node_name)
1179     return env, all_nodes, all_nodes
1180
1181   def CheckPrereq(self):
1182     """Check prerequisites.
1183
1184     This checks:
1185      - the node exists in the configuration
1186      - it does not have primary or secondary instances
1187      - it's not the master
1188
1189     Any errors are signalled by raising errors.OpPrereqError.
1190
1191     """
1192     node = self.cfg.GetNodeInfo(self.cfg.ExpandNodeName(self.op.node_name))
1193     if node is None:
1194       raise errors.OpPrereqError, ("Node '%s' is unknown." % self.op.node_name)
1195
1196     instance_list = self.cfg.GetInstanceList()
1197
1198     masternode = self.sstore.GetMasterNode()
1199     if node.name == masternode:
1200       raise errors.OpPrereqError("Node is the master node,"
1201                                  " you need to failover first.")
1202
1203     for instance_name in instance_list:
1204       instance = self.cfg.GetInstanceInfo(instance_name)
1205       if node.name == instance.primary_node:
1206         raise errors.OpPrereqError("Instance %s still running on the node,"
1207                                    " please remove first." % instance_name)
1208       if node.name in instance.secondary_nodes:
1209         raise errors.OpPrereqError("Instance %s has node as a secondary,"
1210                                    " please remove first." % instance_name)
1211     self.op.node_name = node.name
1212     self.node = node
1213
1214   def Exec(self, feedback_fn):
1215     """Removes the node from the cluster.
1216
1217     """
1218     node = self.node
1219     logger.Info("stopping the node daemon and removing configs from node %s" %
1220                 node.name)
1221
1222     rpc.call_node_leave_cluster(node.name)
1223
1224     ssh.SSHCall(node.name, 'root', "%s stop" % constants.NODE_INITD_SCRIPT)
1225
1226     logger.Info("Removing node %s from config" % node.name)
1227
1228     self.cfg.RemoveNode(node.name)
1229
1230     _RemoveHostFromEtcHosts(node.name)
1231
1232
1233 class LUQueryNodes(NoHooksLU):
1234   """Logical unit for querying nodes.
1235
1236   """
1237   _OP_REQP = ["output_fields", "names"]
1238
1239   def CheckPrereq(self):
1240     """Check prerequisites.
1241
1242     This checks that the fields required are valid output fields.
1243
1244     """
1245     self.dynamic_fields = frozenset(["dtotal", "dfree",
1246                                      "mtotal", "mnode", "mfree",
1247                                      "bootid"])
1248
1249     _CheckOutputFields(static=["name", "pinst_cnt", "sinst_cnt",
1250                                "pinst_list", "sinst_list",
1251                                "pip", "sip"],
1252                        dynamic=self.dynamic_fields,
1253                        selected=self.op.output_fields)
1254
1255     self.wanted = _GetWantedNodes(self, self.op.names)
1256
1257   def Exec(self, feedback_fn):
1258     """Computes the list of nodes and their attributes.
1259
1260     """
1261     nodenames = self.wanted
1262     nodelist = [self.cfg.GetNodeInfo(name) for name in nodenames]
1263
1264     # begin data gathering
1265
1266     if self.dynamic_fields.intersection(self.op.output_fields):
1267       live_data = {}
1268       node_data = rpc.call_node_info(nodenames, self.cfg.GetVGName())
1269       for name in nodenames:
1270         nodeinfo = node_data.get(name, None)
1271         if nodeinfo:
1272           live_data[name] = {
1273             "mtotal": utils.TryConvert(int, nodeinfo['memory_total']),
1274             "mnode": utils.TryConvert(int, nodeinfo['memory_dom0']),
1275             "mfree": utils.TryConvert(int, nodeinfo['memory_free']),
1276             "dtotal": utils.TryConvert(int, nodeinfo['vg_size']),
1277             "dfree": utils.TryConvert(int, nodeinfo['vg_free']),
1278             "bootid": nodeinfo['bootid'],
1279             }
1280         else:
1281           live_data[name] = {}
1282     else:
1283       live_data = dict.fromkeys(nodenames, {})
1284
1285     node_to_primary = dict([(name, set()) for name in nodenames])
1286     node_to_secondary = dict([(name, set()) for name in nodenames])
1287
1288     inst_fields = frozenset(("pinst_cnt", "pinst_list",
1289                              "sinst_cnt", "sinst_list"))
1290     if inst_fields & frozenset(self.op.output_fields):
1291       instancelist = self.cfg.GetInstanceList()
1292
1293       for instance_name in instancelist:
1294         inst = self.cfg.GetInstanceInfo(instance_name)
1295         if inst.primary_node in node_to_primary:
1296           node_to_primary[inst.primary_node].add(inst.name)
1297         for secnode in inst.secondary_nodes:
1298           if secnode in node_to_secondary:
1299             node_to_secondary[secnode].add(inst.name)
1300
1301     # end data gathering
1302
1303     output = []
1304     for node in nodelist:
1305       node_output = []
1306       for field in self.op.output_fields:
1307         if field == "name":
1308           val = node.name
1309         elif field == "pinst_list":
1310           val = list(node_to_primary[node.name])
1311         elif field == "sinst_list":
1312           val = list(node_to_secondary[node.name])
1313         elif field == "pinst_cnt":
1314           val = len(node_to_primary[node.name])
1315         elif field == "sinst_cnt":
1316           val = len(node_to_secondary[node.name])
1317         elif field == "pip":
1318           val = node.primary_ip
1319         elif field == "sip":
1320           val = node.secondary_ip
1321         elif field in self.dynamic_fields:
1322           val = live_data[node.name].get(field, None)
1323         else:
1324           raise errors.ParameterError(field)
1325         node_output.append(val)
1326       output.append(node_output)
1327
1328     return output
1329
1330
1331 class LUQueryNodeVolumes(NoHooksLU):
1332   """Logical unit for getting volumes on node(s).
1333
1334   """
1335   _OP_REQP = ["nodes", "output_fields"]
1336
1337   def CheckPrereq(self):
1338     """Check prerequisites.
1339
1340     This checks that the fields required are valid output fields.
1341
1342     """
1343     self.nodes = _GetWantedNodes(self, self.op.nodes)
1344
1345     _CheckOutputFields(static=["node"],
1346                        dynamic=["phys", "vg", "name", "size", "instance"],
1347                        selected=self.op.output_fields)
1348
1349
1350   def Exec(self, feedback_fn):
1351     """Computes the list of nodes and their attributes.
1352
1353     """
1354     nodenames = self.nodes
1355     volumes = rpc.call_node_volumes(nodenames)
1356
1357     ilist = [self.cfg.GetInstanceInfo(iname) for iname
1358              in self.cfg.GetInstanceList()]
1359
1360     lv_by_node = dict([(inst, inst.MapLVsByNode()) for inst in ilist])
1361
1362     output = []
1363     for node in nodenames:
1364       if node not in volumes or not volumes[node]:
1365         continue
1366
1367       node_vols = volumes[node][:]
1368       node_vols.sort(key=lambda vol: vol['dev'])
1369
1370       for vol in node_vols:
1371         node_output = []
1372         for field in self.op.output_fields:
1373           if field == "node":
1374             val = node
1375           elif field == "phys":
1376             val = vol['dev']
1377           elif field == "vg":
1378             val = vol['vg']
1379           elif field == "name":
1380             val = vol['name']
1381           elif field == "size":
1382             val = int(float(vol['size']))
1383           elif field == "instance":
1384             for inst in ilist:
1385               if node not in lv_by_node[inst]:
1386                 continue
1387               if vol['name'] in lv_by_node[inst][node]:
1388                 val = inst.name
1389                 break
1390             else:
1391               val = '-'
1392           else:
1393             raise errors.ParameterError(field)
1394           node_output.append(str(val))
1395
1396         output.append(node_output)
1397
1398     return output
1399
1400
1401 class LUAddNode(LogicalUnit):
1402   """Logical unit for adding node to the cluster.
1403
1404   """
1405   HPATH = "node-add"
1406   HTYPE = constants.HTYPE_NODE
1407   _OP_REQP = ["node_name"]
1408
1409   def BuildHooksEnv(self):
1410     """Build hooks env.
1411
1412     This will run on all nodes before, and on all nodes + the new node after.
1413
1414     """
1415     env = {
1416       "OP_TARGET": self.op.node_name,
1417       "NODE_NAME": self.op.node_name,
1418       "NODE_PIP": self.op.primary_ip,
1419       "NODE_SIP": self.op.secondary_ip,
1420       }
1421     nodes_0 = self.cfg.GetNodeList()
1422     nodes_1 = nodes_0 + [self.op.node_name, ]
1423     return env, nodes_0, nodes_1
1424
1425   def CheckPrereq(self):
1426     """Check prerequisites.
1427
1428     This checks:
1429      - the new node is not already in the config
1430      - it is resolvable
1431      - its parameters (single/dual homed) matches the cluster
1432
1433     Any errors are signalled by raising errors.OpPrereqError.
1434
1435     """
1436     node_name = self.op.node_name
1437     cfg = self.cfg
1438
1439     dns_data = utils.HostInfo(node_name)
1440
1441     node = dns_data.name
1442     primary_ip = self.op.primary_ip = dns_data.ip
1443     secondary_ip = getattr(self.op, "secondary_ip", None)
1444     if secondary_ip is None:
1445       secondary_ip = primary_ip
1446     if not utils.IsValidIP(secondary_ip):
1447       raise errors.OpPrereqError("Invalid secondary IP given")
1448     self.op.secondary_ip = secondary_ip
1449     node_list = cfg.GetNodeList()
1450     if node in node_list:
1451       raise errors.OpPrereqError("Node %s is already in the configuration"
1452                                  % node)
1453
1454     for existing_node_name in node_list:
1455       existing_node = cfg.GetNodeInfo(existing_node_name)
1456       if (existing_node.primary_ip == primary_ip or
1457           existing_node.secondary_ip == primary_ip or
1458           existing_node.primary_ip == secondary_ip or
1459           existing_node.secondary_ip == secondary_ip):
1460         raise errors.OpPrereqError("New node ip address(es) conflict with"
1461                                    " existing node %s" % existing_node.name)
1462
1463     # check that the type of the node (single versus dual homed) is the
1464     # same as for the master
1465     myself = cfg.GetNodeInfo(self.sstore.GetMasterNode())
1466     master_singlehomed = myself.secondary_ip == myself.primary_ip
1467     newbie_singlehomed = secondary_ip == primary_ip
1468     if master_singlehomed != newbie_singlehomed:
1469       if master_singlehomed:
1470         raise errors.OpPrereqError("The master has no private ip but the"
1471                                    " new node has one")
1472       else:
1473         raise errors.OpPrereqError("The master has a private ip but the"
1474                                    " new node doesn't have one")
1475
1476     # checks reachablity
1477     if not utils.TcpPing(utils.HostInfo().name,
1478                          primary_ip,
1479                          constants.DEFAULT_NODED_PORT):
1480       raise errors.OpPrereqError("Node not reachable by ping")
1481
1482     if not newbie_singlehomed:
1483       # check reachability from my secondary ip to newbie's secondary ip
1484       if not utils.TcpPing(myself.secondary_ip,
1485                            secondary_ip,
1486                            constants.DEFAULT_NODED_PORT):
1487         raise errors.OpPrereqError("Node secondary ip not reachable by TCP"
1488                                    " based ping to noded port")
1489
1490     self.new_node = objects.Node(name=node,
1491                                  primary_ip=primary_ip,
1492                                  secondary_ip=secondary_ip)
1493
1494     if self.sstore.GetHypervisorType() == constants.HT_XEN_HVM31:
1495       if not os.path.exists(constants.VNC_PASSWORD_FILE):
1496         raise errors.OpPrereqError("Cluster VNC password file %s missing" %
1497                                    constants.VNC_PASSWORD_FILE)
1498
1499   def Exec(self, feedback_fn):
1500     """Adds the new node to the cluster.
1501
1502     """
1503     new_node = self.new_node
1504     node = new_node.name
1505
1506     # set up inter-node password and certificate and restarts the node daemon
1507     gntpass = self.sstore.GetNodeDaemonPassword()
1508     if not re.match('^[a-zA-Z0-9.]{1,64}$', gntpass):
1509       raise errors.OpExecError("ganeti password corruption detected")
1510     f = open(constants.SSL_CERT_FILE)
1511     try:
1512       gntpem = f.read(8192)
1513     finally:
1514       f.close()
1515     # in the base64 pem encoding, neither '!' nor '.' are valid chars,
1516     # so we use this to detect an invalid certificate; as long as the
1517     # cert doesn't contain this, the here-document will be correctly
1518     # parsed by the shell sequence below
1519     if re.search('^!EOF\.', gntpem, re.MULTILINE):
1520       raise errors.OpExecError("invalid PEM encoding in the SSL certificate")
1521     if not gntpem.endswith("\n"):
1522       raise errors.OpExecError("PEM must end with newline")
1523     logger.Info("copy cluster pass to %s and starting the node daemon" % node)
1524
1525     # and then connect with ssh to set password and start ganeti-noded
1526     # note that all the below variables are sanitized at this point,
1527     # either by being constants or by the checks above
1528     ss = self.sstore
1529     mycommand = ("umask 077 && "
1530                  "echo '%s' > '%s' && "
1531                  "cat > '%s' << '!EOF.' && \n"
1532                  "%s!EOF.\n%s restart" %
1533                  (gntpass, ss.KeyToFilename(ss.SS_NODED_PASS),
1534                   constants.SSL_CERT_FILE, gntpem,
1535                   constants.NODE_INITD_SCRIPT))
1536
1537     result = ssh.SSHCall(node, 'root', mycommand, batch=False, ask_key=True)
1538     if result.failed:
1539       raise errors.OpExecError("Remote command on node %s, error: %s,"
1540                                " output: %s" %
1541                                (node, result.fail_reason, result.output))
1542
1543     # check connectivity
1544     time.sleep(4)
1545
1546     result = rpc.call_version([node])[node]
1547     if result:
1548       if constants.PROTOCOL_VERSION == result:
1549         logger.Info("communication to node %s fine, sw version %s match" %
1550                     (node, result))
1551       else:
1552         raise errors.OpExecError("Version mismatch master version %s,"
1553                                  " node version %s" %
1554                                  (constants.PROTOCOL_VERSION, result))
1555     else:
1556       raise errors.OpExecError("Cannot get version from the new node")
1557
1558     # setup ssh on node
1559     logger.Info("copy ssh key to node %s" % node)
1560     priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
1561     keyarray = []
1562     keyfiles = [constants.SSH_HOST_DSA_PRIV, constants.SSH_HOST_DSA_PUB,
1563                 constants.SSH_HOST_RSA_PRIV, constants.SSH_HOST_RSA_PUB,
1564                 priv_key, pub_key]
1565
1566     for i in keyfiles:
1567       f = open(i, 'r')
1568       try:
1569         keyarray.append(f.read())
1570       finally:
1571         f.close()
1572
1573     result = rpc.call_node_add(node, keyarray[0], keyarray[1], keyarray[2],
1574                                keyarray[3], keyarray[4], keyarray[5])
1575
1576     if not result:
1577       raise errors.OpExecError("Cannot transfer ssh keys to the new node")
1578
1579     # Add node to our /etc/hosts, and add key to known_hosts
1580     _AddHostToEtcHosts(new_node.name)
1581
1582     _UpdateKnownHosts(new_node.name, new_node.primary_ip,
1583                       self.cfg.GetHostKey())
1584
1585     if new_node.secondary_ip != new_node.primary_ip:
1586       if not rpc.call_node_tcp_ping(new_node.name,
1587                                     constants.LOCALHOST_IP_ADDRESS,
1588                                     new_node.secondary_ip,
1589                                     constants.DEFAULT_NODED_PORT,
1590                                     10, False):
1591         raise errors.OpExecError("Node claims it doesn't have the secondary ip"
1592                                  " you gave (%s). Please fix and re-run this"
1593                                  " command." % new_node.secondary_ip)
1594
1595     success, msg = ssh.VerifyNodeHostname(node)
1596     if not success:
1597       raise errors.OpExecError("Node '%s' claims it has a different hostname"
1598                                " than the one the resolver gives: %s."
1599                                " Please fix and re-run this command." %
1600                                (node, msg))
1601
1602     # Distribute updated /etc/hosts and known_hosts to all nodes,
1603     # including the node just added
1604     myself = self.cfg.GetNodeInfo(self.sstore.GetMasterNode())
1605     dist_nodes = self.cfg.GetNodeList() + [node]
1606     if myself.name in dist_nodes:
1607       dist_nodes.remove(myself.name)
1608
1609     logger.Debug("Copying hosts and known_hosts to all nodes")
1610     for fname in ("/etc/hosts", constants.SSH_KNOWN_HOSTS_FILE):
1611       result = rpc.call_upload_file(dist_nodes, fname)
1612       for to_node in dist_nodes:
1613         if not result[to_node]:
1614           logger.Error("copy of file %s to node %s failed" %
1615                        (fname, to_node))
1616
1617     to_copy = ss.GetFileList()
1618     if self.sstore.GetHypervisorType() == constants.HT_XEN_HVM31:
1619       to_copy.append(constants.VNC_PASSWORD_FILE)
1620     for fname in to_copy:
1621       if not ssh.CopyFileToNode(node, fname):
1622         logger.Error("could not copy file %s to node %s" % (fname, node))
1623
1624     logger.Info("adding node %s to cluster.conf" % node)
1625     self.cfg.AddNode(new_node)
1626
1627
1628 class LUMasterFailover(LogicalUnit):
1629   """Failover the master node to the current node.
1630
1631   This is a special LU in that it must run on a non-master node.
1632
1633   """
1634   HPATH = "master-failover"
1635   HTYPE = constants.HTYPE_CLUSTER
1636   REQ_MASTER = False
1637   _OP_REQP = []
1638
1639   def BuildHooksEnv(self):
1640     """Build hooks env.
1641
1642     This will run on the new master only in the pre phase, and on all
1643     the nodes in the post phase.
1644
1645     """
1646     env = {
1647       "OP_TARGET": self.new_master,
1648       "NEW_MASTER": self.new_master,
1649       "OLD_MASTER": self.old_master,
1650       }
1651     return env, [self.new_master], self.cfg.GetNodeList()
1652
1653   def CheckPrereq(self):
1654     """Check prerequisites.
1655
1656     This checks that we are not already the master.
1657
1658     """
1659     self.new_master = utils.HostInfo().name
1660     self.old_master = self.sstore.GetMasterNode()
1661
1662     if self.old_master == self.new_master:
1663       raise errors.OpPrereqError("This commands must be run on the node"
1664                                  " where you want the new master to be."
1665                                  " %s is already the master" %
1666                                  self.old_master)
1667
1668   def Exec(self, feedback_fn):
1669     """Failover the master node.
1670
1671     This command, when run on a non-master node, will cause the current
1672     master to cease being master, and the non-master to become new
1673     master.
1674
1675     """
1676     #TODO: do not rely on gethostname returning the FQDN
1677     logger.Info("setting master to %s, old master: %s" %
1678                 (self.new_master, self.old_master))
1679
1680     if not rpc.call_node_stop_master(self.old_master):
1681       logger.Error("could disable the master role on the old master"
1682                    " %s, please disable manually" % self.old_master)
1683
1684     ss = self.sstore
1685     ss.SetKey(ss.SS_MASTER_NODE, self.new_master)
1686     if not rpc.call_upload_file(self.cfg.GetNodeList(),
1687                                 ss.KeyToFilename(ss.SS_MASTER_NODE)):
1688       logger.Error("could not distribute the new simple store master file"
1689                    " to the other nodes, please check.")
1690
1691     if not rpc.call_node_start_master(self.new_master):
1692       logger.Error("could not start the master role on the new master"
1693                    " %s, please check" % self.new_master)
1694       feedback_fn("Error in activating the master IP on the new master,"
1695                   " please fix manually.")
1696
1697
1698
1699 class LUQueryClusterInfo(NoHooksLU):
1700   """Query cluster configuration.
1701
1702   """
1703   _OP_REQP = []
1704   REQ_MASTER = False
1705
1706   def CheckPrereq(self):
1707     """No prerequsites needed for this LU.
1708
1709     """
1710     pass
1711
1712   def Exec(self, feedback_fn):
1713     """Return cluster config.
1714
1715     """
1716     result = {
1717       "name": self.sstore.GetClusterName(),
1718       "software_version": constants.RELEASE_VERSION,
1719       "protocol_version": constants.PROTOCOL_VERSION,
1720       "config_version": constants.CONFIG_VERSION,
1721       "os_api_version": constants.OS_API_VERSION,
1722       "export_version": constants.EXPORT_VERSION,
1723       "master": self.sstore.GetMasterNode(),
1724       "architecture": (platform.architecture()[0], platform.machine()),
1725       }
1726
1727     return result
1728
1729
1730 class LUClusterCopyFile(NoHooksLU):
1731   """Copy file to cluster.
1732
1733   """
1734   _OP_REQP = ["nodes", "filename"]
1735
1736   def CheckPrereq(self):
1737     """Check prerequisites.
1738
1739     It should check that the named file exists and that the given list
1740     of nodes is valid.
1741
1742     """
1743     if not os.path.exists(self.op.filename):
1744       raise errors.OpPrereqError("No such filename '%s'" % self.op.filename)
1745
1746     self.nodes = _GetWantedNodes(self, self.op.nodes)
1747
1748   def Exec(self, feedback_fn):
1749     """Copy a file from master to some nodes.
1750
1751     Args:
1752       opts - class with options as members
1753       args - list containing a single element, the file name
1754     Opts used:
1755       nodes - list containing the name of target nodes; if empty, all nodes
1756
1757     """
1758     filename = self.op.filename
1759
1760     myname = utils.HostInfo().name
1761
1762     for node in self.nodes:
1763       if node == myname:
1764         continue
1765       if not ssh.CopyFileToNode(node, filename):
1766         logger.Error("Copy of file %s to node %s failed" % (filename, node))
1767
1768
1769 class LUDumpClusterConfig(NoHooksLU):
1770   """Return a text-representation of the cluster-config.
1771
1772   """
1773   _OP_REQP = []
1774
1775   def CheckPrereq(self):
1776     """No prerequisites.
1777
1778     """
1779     pass
1780
1781   def Exec(self, feedback_fn):
1782     """Dump a representation of the cluster config to the standard output.
1783
1784     """
1785     return self.cfg.DumpConfig()
1786
1787
1788 class LURunClusterCommand(NoHooksLU):
1789   """Run a command on some nodes.
1790
1791   """
1792   _OP_REQP = ["command", "nodes"]
1793
1794   def CheckPrereq(self):
1795     """Check prerequisites.
1796
1797     It checks that the given list of nodes is valid.
1798
1799     """
1800     self.nodes = _GetWantedNodes(self, self.op.nodes)
1801
1802   def Exec(self, feedback_fn):
1803     """Run a command on some nodes.
1804
1805     """
1806     data = []
1807     for node in self.nodes:
1808       result = ssh.SSHCall(node, "root", self.op.command)
1809       data.append((node, result.output, result.exit_code))
1810
1811     return data
1812
1813
1814 class LUActivateInstanceDisks(NoHooksLU):
1815   """Bring up an instance's disks.
1816
1817   """
1818   _OP_REQP = ["instance_name"]
1819
1820   def CheckPrereq(self):
1821     """Check prerequisites.
1822
1823     This checks that the instance is in the cluster.
1824
1825     """
1826     instance = self.cfg.GetInstanceInfo(
1827       self.cfg.ExpandInstanceName(self.op.instance_name))
1828     if instance is None:
1829       raise errors.OpPrereqError("Instance '%s' not known" %
1830                                  self.op.instance_name)
1831     self.instance = instance
1832
1833
1834   def Exec(self, feedback_fn):
1835     """Activate the disks.
1836
1837     """
1838     disks_ok, disks_info = _AssembleInstanceDisks(self.instance, self.cfg)
1839     if not disks_ok:
1840       raise errors.OpExecError("Cannot activate block devices")
1841
1842     return disks_info
1843
1844
1845 def _AssembleInstanceDisks(instance, cfg, ignore_secondaries=False):
1846   """Prepare the block devices for an instance.
1847
1848   This sets up the block devices on all nodes.
1849
1850   Args:
1851     instance: a ganeti.objects.Instance object
1852     ignore_secondaries: if true, errors on secondary nodes won't result
1853                         in an error return from the function
1854
1855   Returns:
1856     false if the operation failed
1857     list of (host, instance_visible_name, node_visible_name) if the operation
1858          suceeded with the mapping from node devices to instance devices
1859   """
1860   device_info = []
1861   disks_ok = True
1862   for inst_disk in instance.disks:
1863     master_result = None
1864     for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
1865       cfg.SetDiskID(node_disk, node)
1866       is_primary = node == instance.primary_node
1867       result = rpc.call_blockdev_assemble(node, node_disk,
1868                                           instance.name, is_primary)
1869       if not result:
1870         logger.Error("could not prepare block device %s on node %s"
1871                      " (is_primary=%s)" %
1872                      (inst_disk.iv_name, node, is_primary))
1873         if is_primary or not ignore_secondaries:
1874           disks_ok = False
1875       if is_primary:
1876         master_result = result
1877     device_info.append((instance.primary_node, inst_disk.iv_name,
1878                         master_result))
1879
1880   # leave the disks configured for the primary node
1881   # this is a workaround that would be fixed better by
1882   # improving the logical/physical id handling
1883   for disk in instance.disks:
1884     cfg.SetDiskID(disk, instance.primary_node)
1885
1886   return disks_ok, device_info
1887
1888
1889 def _StartInstanceDisks(cfg, instance, force):
1890   """Start the disks of an instance.
1891
1892   """
1893   disks_ok, dummy = _AssembleInstanceDisks(instance, cfg,
1894                                            ignore_secondaries=force)
1895   if not disks_ok:
1896     _ShutdownInstanceDisks(instance, cfg)
1897     if force is not None and not force:
1898       logger.Error("If the message above refers to a secondary node,"
1899                    " you can retry the operation using '--force'.")
1900     raise errors.OpExecError("Disk consistency error")
1901
1902
1903 class LUDeactivateInstanceDisks(NoHooksLU):
1904   """Shutdown an instance's disks.
1905
1906   """
1907   _OP_REQP = ["instance_name"]
1908
1909   def CheckPrereq(self):
1910     """Check prerequisites.
1911
1912     This checks that the instance is in the cluster.
1913
1914     """
1915     instance = self.cfg.GetInstanceInfo(
1916       self.cfg.ExpandInstanceName(self.op.instance_name))
1917     if instance is None:
1918       raise errors.OpPrereqError("Instance '%s' not known" %
1919                                  self.op.instance_name)
1920     self.instance = instance
1921
1922   def Exec(self, feedback_fn):
1923     """Deactivate the disks
1924
1925     """
1926     instance = self.instance
1927     ins_l = rpc.call_instance_list([instance.primary_node])
1928     ins_l = ins_l[instance.primary_node]
1929     if not type(ins_l) is list:
1930       raise errors.OpExecError("Can't contact node '%s'" %
1931                                instance.primary_node)
1932
1933     if self.instance.name in ins_l:
1934       raise errors.OpExecError("Instance is running, can't shutdown"
1935                                " block devices.")
1936
1937     _ShutdownInstanceDisks(instance, self.cfg)
1938
1939
1940 def _ShutdownInstanceDisks(instance, cfg, ignore_primary=False):
1941   """Shutdown block devices of an instance.
1942
1943   This does the shutdown on all nodes of the instance.
1944
1945   If the ignore_primary is false, errors on the primary node are
1946   ignored.
1947
1948   """
1949   result = True
1950   for disk in instance.disks:
1951     for node, top_disk in disk.ComputeNodeTree(instance.primary_node):
1952       cfg.SetDiskID(top_disk, node)
1953       if not rpc.call_blockdev_shutdown(node, top_disk):
1954         logger.Error("could not shutdown block device %s on node %s" %
1955                      (disk.iv_name, node))
1956         if not ignore_primary or node != instance.primary_node:
1957           result = False
1958   return result
1959
1960
1961 def _CheckNodeFreeMemory(cfg, node, reason, requested):
1962   """Checks if a node has enough free memory.
1963
1964   This function check if a given node has the needed amount of free
1965   memory. In case the node has less memory or we cannot get the
1966   information from the node, this function raise an OpPrereqError
1967   exception.
1968
1969   Args:
1970     - cfg: a ConfigWriter instance
1971     - node: the node name
1972     - reason: string to use in the error message
1973     - requested: the amount of memory in MiB
1974
1975   """
1976   nodeinfo = rpc.call_node_info([node], cfg.GetVGName())
1977   if not nodeinfo or not isinstance(nodeinfo, dict):
1978     raise errors.OpPrereqError("Could not contact node %s for resource"
1979                              " information" % (node,))
1980
1981   free_mem = nodeinfo[node].get('memory_free')
1982   if not isinstance(free_mem, int):
1983     raise errors.OpPrereqError("Can't compute free memory on node %s, result"
1984                              " was '%s'" % (node, free_mem))
1985   if requested > free_mem:
1986     raise errors.OpPrereqError("Not enough memory on node %s for %s:"
1987                              " needed %s MiB, available %s MiB" %
1988                              (node, reason, requested, free_mem))
1989
1990
1991 class LUStartupInstance(LogicalUnit):
1992   """Starts an instance.
1993
1994   """
1995   HPATH = "instance-start"
1996   HTYPE = constants.HTYPE_INSTANCE
1997   _OP_REQP = ["instance_name", "force"]
1998
1999   def BuildHooksEnv(self):
2000     """Build hooks env.
2001
2002     This runs on master, primary and secondary nodes of the instance.
2003
2004     """
2005     env = {
2006       "FORCE": self.op.force,
2007       }
2008     env.update(_BuildInstanceHookEnvByObject(self.instance))
2009     nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] +
2010           list(self.instance.secondary_nodes))
2011     return env, nl, nl
2012
2013   def CheckPrereq(self):
2014     """Check prerequisites.
2015
2016     This checks that the instance is in the cluster.
2017
2018     """
2019     instance = self.cfg.GetInstanceInfo(
2020       self.cfg.ExpandInstanceName(self.op.instance_name))
2021     if instance is None:
2022       raise errors.OpPrereqError("Instance '%s' not known" %
2023                                  self.op.instance_name)
2024
2025     # check bridges existance
2026     _CheckInstanceBridgesExist(instance)
2027
2028     _CheckNodeFreeMemory(self.cfg, instance.primary_node,
2029                          "starting instance %s" % instance.name,
2030                          instance.memory)
2031
2032     self.instance = instance
2033     self.op.instance_name = instance.name
2034
2035   def Exec(self, feedback_fn):
2036     """Start the instance.
2037
2038     """
2039     instance = self.instance
2040     force = self.op.force
2041     extra_args = getattr(self.op, "extra_args", "")
2042
2043     node_current = instance.primary_node
2044
2045     _StartInstanceDisks(self.cfg, instance, force)
2046
2047     if not rpc.call_instance_start(node_current, instance, extra_args):
2048       _ShutdownInstanceDisks(instance, self.cfg)
2049       raise errors.OpExecError("Could not start instance")
2050
2051     self.cfg.MarkInstanceUp(instance.name)
2052
2053
2054 class LURebootInstance(LogicalUnit):
2055   """Reboot an instance.
2056
2057   """
2058   HPATH = "instance-reboot"
2059   HTYPE = constants.HTYPE_INSTANCE
2060   _OP_REQP = ["instance_name", "ignore_secondaries", "reboot_type"]
2061
2062   def BuildHooksEnv(self):
2063     """Build hooks env.
2064
2065     This runs on master, primary and secondary nodes of the instance.
2066
2067     """
2068     env = {
2069       "IGNORE_SECONDARIES": self.op.ignore_secondaries,
2070       }
2071     env.update(_BuildInstanceHookEnvByObject(self.instance))
2072     nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] +
2073           list(self.instance.secondary_nodes))
2074     return env, nl, nl
2075
2076   def CheckPrereq(self):
2077     """Check prerequisites.
2078
2079     This checks that the instance is in the cluster.
2080
2081     """
2082     instance = self.cfg.GetInstanceInfo(
2083       self.cfg.ExpandInstanceName(self.op.instance_name))
2084     if instance is None:
2085       raise errors.OpPrereqError("Instance '%s' not known" %
2086                                  self.op.instance_name)
2087
2088     # check bridges existance
2089     _CheckInstanceBridgesExist(instance)
2090
2091     self.instance = instance
2092     self.op.instance_name = instance.name
2093
2094   def Exec(self, feedback_fn):
2095     """Reboot the instance.
2096
2097     """
2098     instance = self.instance
2099     ignore_secondaries = self.op.ignore_secondaries
2100     reboot_type = self.op.reboot_type
2101     extra_args = getattr(self.op, "extra_args", "")
2102
2103     node_current = instance.primary_node
2104
2105     if reboot_type not in [constants.INSTANCE_REBOOT_SOFT,
2106                            constants.INSTANCE_REBOOT_HARD,
2107                            constants.INSTANCE_REBOOT_FULL]:
2108       raise errors.ParameterError("reboot type not in [%s, %s, %s]" %
2109                                   (constants.INSTANCE_REBOOT_SOFT,
2110                                    constants.INSTANCE_REBOOT_HARD,
2111                                    constants.INSTANCE_REBOOT_FULL))
2112
2113     if reboot_type in [constants.INSTANCE_REBOOT_SOFT,
2114                        constants.INSTANCE_REBOOT_HARD]:
2115       if not rpc.call_instance_reboot(node_current, instance,
2116                                       reboot_type, extra_args):
2117         raise errors.OpExecError("Could not reboot instance")
2118     else:
2119       if not rpc.call_instance_shutdown(node_current, instance):
2120         raise errors.OpExecError("could not shutdown instance for full reboot")
2121       _ShutdownInstanceDisks(instance, self.cfg)
2122       _StartInstanceDisks(self.cfg, instance, ignore_secondaries)
2123       if not rpc.call_instance_start(node_current, instance, extra_args):
2124         _ShutdownInstanceDisks(instance, self.cfg)
2125         raise errors.OpExecError("Could not start instance for full reboot")
2126
2127     self.cfg.MarkInstanceUp(instance.name)
2128
2129
2130 class LUShutdownInstance(LogicalUnit):
2131   """Shutdown an instance.
2132
2133   """
2134   HPATH = "instance-stop"
2135   HTYPE = constants.HTYPE_INSTANCE
2136   _OP_REQP = ["instance_name"]
2137
2138   def BuildHooksEnv(self):
2139     """Build hooks env.
2140
2141     This runs on master, primary and secondary nodes of the instance.
2142
2143     """
2144     env = _BuildInstanceHookEnvByObject(self.instance)
2145     nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] +
2146           list(self.instance.secondary_nodes))
2147     return env, nl, nl
2148
2149   def CheckPrereq(self):
2150     """Check prerequisites.
2151
2152     This checks that the instance is in the cluster.
2153
2154     """
2155     instance = self.cfg.GetInstanceInfo(
2156       self.cfg.ExpandInstanceName(self.op.instance_name))
2157     if instance is None:
2158       raise errors.OpPrereqError("Instance '%s' not known" %
2159                                  self.op.instance_name)
2160     self.instance = instance
2161
2162   def Exec(self, feedback_fn):
2163     """Shutdown the instance.
2164
2165     """
2166     instance = self.instance
2167     node_current = instance.primary_node
2168     if not rpc.call_instance_shutdown(node_current, instance):
2169       logger.Error("could not shutdown instance")
2170
2171     self.cfg.MarkInstanceDown(instance.name)
2172     _ShutdownInstanceDisks(instance, self.cfg)
2173
2174
2175 class LUReinstallInstance(LogicalUnit):
2176   """Reinstall an instance.
2177
2178   """
2179   HPATH = "instance-reinstall"
2180   HTYPE = constants.HTYPE_INSTANCE
2181   _OP_REQP = ["instance_name"]
2182
2183   def BuildHooksEnv(self):
2184     """Build hooks env.
2185
2186     This runs on master, primary and secondary nodes of the instance.
2187
2188     """
2189     env = _BuildInstanceHookEnvByObject(self.instance)
2190     nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] +
2191           list(self.instance.secondary_nodes))
2192     return env, nl, nl
2193
2194   def CheckPrereq(self):
2195     """Check prerequisites.
2196
2197     This checks that the instance is in the cluster and is not running.
2198
2199     """
2200     instance = self.cfg.GetInstanceInfo(
2201       self.cfg.ExpandInstanceName(self.op.instance_name))
2202     if instance is None:
2203       raise errors.OpPrereqError("Instance '%s' not known" %
2204                                  self.op.instance_name)
2205     if instance.disk_template == constants.DT_DISKLESS:
2206       raise errors.OpPrereqError("Instance '%s' has no disks" %
2207                                  self.op.instance_name)
2208     if instance.status != "down":
2209       raise errors.OpPrereqError("Instance '%s' is marked to be up" %
2210                                  self.op.instance_name)
2211     remote_info = rpc.call_instance_info(instance.primary_node, instance.name)
2212     if remote_info:
2213       raise errors.OpPrereqError("Instance '%s' is running on the node %s" %
2214                                  (self.op.instance_name,
2215                                   instance.primary_node))
2216
2217     self.op.os_type = getattr(self.op, "os_type", None)
2218     if self.op.os_type is not None:
2219       # OS verification
2220       pnode = self.cfg.GetNodeInfo(
2221         self.cfg.ExpandNodeName(instance.primary_node))
2222       if pnode is None:
2223         raise errors.OpPrereqError("Primary node '%s' is unknown" %
2224                                    self.op.pnode)
2225       os_obj = rpc.call_os_get(pnode.name, self.op.os_type)
2226       if not os_obj:
2227         raise errors.OpPrereqError("OS '%s' not in supported OS list for"
2228                                    " primary node"  % self.op.os_type)
2229
2230     self.instance = instance
2231
2232   def Exec(self, feedback_fn):
2233     """Reinstall the instance.
2234
2235     """
2236     inst = self.instance
2237
2238     if self.op.os_type is not None:
2239       feedback_fn("Changing OS to '%s'..." % self.op.os_type)
2240       inst.os = self.op.os_type
2241       self.cfg.AddInstance(inst)
2242
2243     _StartInstanceDisks(self.cfg, inst, None)
2244     try:
2245       feedback_fn("Running the instance OS create scripts...")
2246       if not rpc.call_instance_os_add(inst.primary_node, inst, "sda", "sdb"):
2247         raise errors.OpExecError("Could not install OS for instance %s"
2248                                  " on node %s" %
2249                                  (inst.name, inst.primary_node))
2250     finally:
2251       _ShutdownInstanceDisks(inst, self.cfg)
2252
2253
2254 class LURenameInstance(LogicalUnit):
2255   """Rename an instance.
2256
2257   """
2258   HPATH = "instance-rename"
2259   HTYPE = constants.HTYPE_INSTANCE
2260   _OP_REQP = ["instance_name", "new_name"]
2261
2262   def BuildHooksEnv(self):
2263     """Build hooks env.
2264
2265     This runs on master, primary and secondary nodes of the instance.
2266
2267     """
2268     env = _BuildInstanceHookEnvByObject(self.instance)
2269     env["INSTANCE_NEW_NAME"] = self.op.new_name
2270     nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] +
2271           list(self.instance.secondary_nodes))
2272     return env, nl, nl
2273
2274   def CheckPrereq(self):
2275     """Check prerequisites.
2276
2277     This checks that the instance is in the cluster and is not running.
2278
2279     """
2280     instance = self.cfg.GetInstanceInfo(
2281       self.cfg.ExpandInstanceName(self.op.instance_name))
2282     if instance is None:
2283       raise errors.OpPrereqError("Instance '%s' not known" %
2284                                  self.op.instance_name)
2285     if instance.status != "down":
2286       raise errors.OpPrereqError("Instance '%s' is marked to be up" %
2287                                  self.op.instance_name)
2288     remote_info = rpc.call_instance_info(instance.primary_node, instance.name)
2289     if remote_info:
2290       raise errors.OpPrereqError("Instance '%s' is running on the node %s" %
2291                                  (self.op.instance_name,
2292                                   instance.primary_node))
2293     self.instance = instance
2294
2295     # new name verification
2296     name_info = utils.HostInfo(self.op.new_name)
2297
2298     self.op.new_name = new_name = name_info.name
2299     if not getattr(self.op, "ignore_ip", False):
2300       command = ["fping", "-q", name_info.ip]
2301       result = utils.RunCmd(command)
2302       if not result.failed:
2303         raise errors.OpPrereqError("IP %s of instance %s already in use" %
2304                                    (name_info.ip, new_name))
2305
2306
2307   def Exec(self, feedback_fn):
2308     """Reinstall the instance.
2309
2310     """
2311     inst = self.instance
2312     old_name = inst.name
2313
2314     self.cfg.RenameInstance(inst.name, self.op.new_name)
2315
2316     # re-read the instance from the configuration after rename
2317     inst = self.cfg.GetInstanceInfo(self.op.new_name)
2318
2319     _StartInstanceDisks(self.cfg, inst, None)
2320     try:
2321       if not rpc.call_instance_run_rename(inst.primary_node, inst, old_name,
2322                                           "sda", "sdb"):
2323         msg = ("Could run OS rename script for instance %s on node %s (but the"
2324                " instance has been renamed in Ganeti)" %
2325                (inst.name, inst.primary_node))
2326         logger.Error(msg)
2327     finally:
2328       _ShutdownInstanceDisks(inst, self.cfg)
2329
2330
2331 class LURemoveInstance(LogicalUnit):
2332   """Remove an instance.
2333
2334   """
2335   HPATH = "instance-remove"
2336   HTYPE = constants.HTYPE_INSTANCE
2337   _OP_REQP = ["instance_name"]
2338
2339   def BuildHooksEnv(self):
2340     """Build hooks env.
2341
2342     This runs on master, primary and secondary nodes of the instance.
2343
2344     """
2345     env = _BuildInstanceHookEnvByObject(self.instance)
2346     nl = [self.sstore.GetMasterNode()]
2347     return env, nl, nl
2348
2349   def CheckPrereq(self):
2350     """Check prerequisites.
2351
2352     This checks that the instance is in the cluster.
2353
2354     """
2355     instance = self.cfg.GetInstanceInfo(
2356       self.cfg.ExpandInstanceName(self.op.instance_name))
2357     if instance is None:
2358       raise errors.OpPrereqError("Instance '%s' not known" %
2359                                  self.op.instance_name)
2360     self.instance = instance
2361
2362   def Exec(self, feedback_fn):
2363     """Remove the instance.
2364
2365     """
2366     instance = self.instance
2367     logger.Info("shutting down instance %s on node %s" %
2368                 (instance.name, instance.primary_node))
2369
2370     if not rpc.call_instance_shutdown(instance.primary_node, instance):
2371       if self.op.ignore_failures:
2372         feedback_fn("Warning: can't shutdown instance")
2373       else:
2374         raise errors.OpExecError("Could not shutdown instance %s on node %s" %
2375                                  (instance.name, instance.primary_node))
2376
2377     logger.Info("removing block devices for instance %s" % instance.name)
2378
2379     if not _RemoveDisks(instance, self.cfg):
2380       if self.op.ignore_failures:
2381         feedback_fn("Warning: can't remove instance's disks")
2382       else:
2383         raise errors.OpExecError("Can't remove instance's disks")
2384
2385     logger.Info("removing instance %s out of cluster config" % instance.name)
2386
2387     self.cfg.RemoveInstance(instance.name)
2388
2389
2390 class LUQueryInstances(NoHooksLU):
2391   """Logical unit for querying instances.
2392
2393   """
2394   _OP_REQP = ["output_fields", "names"]
2395
2396   def CheckPrereq(self):
2397     """Check prerequisites.
2398
2399     This checks that the fields required are valid output fields.
2400
2401     """
2402     self.dynamic_fields = frozenset(["oper_state", "oper_ram"])
2403     _CheckOutputFields(static=["name", "os", "pnode", "snodes",
2404                                "admin_state", "admin_ram",
2405                                "disk_template", "ip", "mac", "bridge",
2406                                "sda_size", "sdb_size"],
2407                        dynamic=self.dynamic_fields,
2408                        selected=self.op.output_fields)
2409
2410     self.wanted = _GetWantedInstances(self, self.op.names)
2411
2412   def Exec(self, feedback_fn):
2413     """Computes the list of nodes and their attributes.
2414
2415     """
2416     instance_names = self.wanted
2417     instance_list = [self.cfg.GetInstanceInfo(iname) for iname
2418                      in instance_names]
2419
2420     # begin data gathering
2421
2422     nodes = frozenset([inst.primary_node for inst in instance_list])
2423
2424     bad_nodes = []
2425     if self.dynamic_fields.intersection(self.op.output_fields):
2426       live_data = {}
2427       node_data = rpc.call_all_instances_info(nodes)
2428       for name in nodes:
2429         result = node_data[name]
2430         if result:
2431           live_data.update(result)
2432         elif result == False:
2433           bad_nodes.append(name)
2434         # else no instance is alive
2435     else:
2436       live_data = dict([(name, {}) for name in instance_names])
2437
2438     # end data gathering
2439
2440     output = []
2441     for instance in instance_list:
2442       iout = []
2443       for field in self.op.output_fields:
2444         if field == "name":
2445           val = instance.name
2446         elif field == "os":
2447           val = instance.os
2448         elif field == "pnode":
2449           val = instance.primary_node
2450         elif field == "snodes":
2451           val = list(instance.secondary_nodes)
2452         elif field == "admin_state":
2453           val = (instance.status != "down")
2454         elif field == "oper_state":
2455           if instance.primary_node in bad_nodes:
2456             val = None
2457           else:
2458             val = bool(live_data.get(instance.name))
2459         elif field == "admin_ram":
2460           val = instance.memory
2461         elif field == "oper_ram":
2462           if instance.primary_node in bad_nodes:
2463             val = None
2464           elif instance.name in live_data:
2465             val = live_data[instance.name].get("memory", "?")
2466           else:
2467             val = "-"
2468         elif field == "disk_template":
2469           val = instance.disk_template
2470         elif field == "ip":
2471           val = instance.nics[0].ip
2472         elif field == "bridge":
2473           val = instance.nics[0].bridge
2474         elif field == "mac":
2475           val = instance.nics[0].mac
2476         elif field == "sda_size" or field == "sdb_size":
2477           disk = instance.FindDisk(field[:3])
2478           if disk is None:
2479             val = None
2480           else:
2481             val = disk.size
2482         else:
2483           raise errors.ParameterError(field)
2484         iout.append(val)
2485       output.append(iout)
2486
2487     return output
2488
2489
2490 class LUFailoverInstance(LogicalUnit):
2491   """Failover an instance.
2492
2493   """
2494   HPATH = "instance-failover"
2495   HTYPE = constants.HTYPE_INSTANCE
2496   _OP_REQP = ["instance_name", "ignore_consistency"]
2497
2498   def BuildHooksEnv(self):
2499     """Build hooks env.
2500
2501     This runs on master, primary and secondary nodes of the instance.
2502
2503     """
2504     env = {
2505       "IGNORE_CONSISTENCY": self.op.ignore_consistency,
2506       }
2507     env.update(_BuildInstanceHookEnvByObject(self.instance))
2508     nl = [self.sstore.GetMasterNode()] + list(self.instance.secondary_nodes)
2509     return env, nl, nl
2510
2511   def CheckPrereq(self):
2512     """Check prerequisites.
2513
2514     This checks that the instance is in the cluster.
2515
2516     """
2517     instance = self.cfg.GetInstanceInfo(
2518       self.cfg.ExpandInstanceName(self.op.instance_name))
2519     if instance is None:
2520       raise errors.OpPrereqError("Instance '%s' not known" %
2521                                  self.op.instance_name)
2522
2523     if instance.disk_template not in constants.DTS_NET_MIRROR:
2524       raise errors.OpPrereqError("Instance's disk layout is not"
2525                                  " network mirrored, cannot failover.")
2526
2527     secondary_nodes = instance.secondary_nodes
2528     if not secondary_nodes:
2529       raise errors.ProgrammerError("no secondary node but using "
2530                                    "DT_REMOTE_RAID1 template")
2531
2532     target_node = secondary_nodes[0]
2533     # check memory requirements on the secondary node
2534     _CheckNodeFreeMemory(self.cfg, target_node, "failing over instance %s" %
2535                          instance.name, instance.memory)
2536
2537     # check bridge existance
2538     brlist = [nic.bridge for nic in instance.nics]
2539     if not rpc.call_bridges_exist(target_node, brlist):
2540       raise errors.OpPrereqError("One or more target bridges %s does not"
2541                                  " exist on destination node '%s'" %
2542                                  (brlist, target_node))
2543
2544     self.instance = instance
2545
2546   def Exec(self, feedback_fn):
2547     """Failover an instance.
2548
2549     The failover is done by shutting it down on its present node and
2550     starting it on the secondary.
2551
2552     """
2553     instance = self.instance
2554
2555     source_node = instance.primary_node
2556     target_node = instance.secondary_nodes[0]
2557
2558     feedback_fn("* checking disk consistency between source and target")
2559     for dev in instance.disks:
2560       # for remote_raid1, these are md over drbd
2561       if not _CheckDiskConsistency(self.cfg, dev, target_node, False):
2562         if not self.op.ignore_consistency:
2563           raise errors.OpExecError("Disk %s is degraded on target node,"
2564                                    " aborting failover." % dev.iv_name)
2565
2566     feedback_fn("* shutting down instance on source node")
2567     logger.Info("Shutting down instance %s on node %s" %
2568                 (instance.name, source_node))
2569
2570     if not rpc.call_instance_shutdown(source_node, instance):
2571       if self.op.ignore_consistency:
2572         logger.Error("Could not shutdown instance %s on node %s. Proceeding"
2573                      " anyway. Please make sure node %s is down"  %
2574                      (instance.name, source_node, source_node))
2575       else:
2576         raise errors.OpExecError("Could not shutdown instance %s on node %s" %
2577                                  (instance.name, source_node))
2578
2579     feedback_fn("* deactivating the instance's disks on source node")
2580     if not _ShutdownInstanceDisks(instance, self.cfg, ignore_primary=True):
2581       raise errors.OpExecError("Can't shut down the instance's disks.")
2582
2583     instance.primary_node = target_node
2584     # distribute new instance config to the other nodes
2585     self.cfg.AddInstance(instance)
2586
2587     feedback_fn("* activating the instance's disks on target node")
2588     logger.Info("Starting instance %s on node %s" %
2589                 (instance.name, target_node))
2590
2591     disks_ok, dummy = _AssembleInstanceDisks(instance, self.cfg,
2592                                              ignore_secondaries=True)
2593     if not disks_ok:
2594       _ShutdownInstanceDisks(instance, self.cfg)
2595       raise errors.OpExecError("Can't activate the instance's disks")
2596
2597     feedback_fn("* starting the instance on the target node")
2598     if not rpc.call_instance_start(target_node, instance, None):
2599       _ShutdownInstanceDisks(instance, self.cfg)
2600       raise errors.OpExecError("Could not start instance %s on node %s." %
2601                                (instance.name, target_node))
2602
2603
2604 def _CreateBlockDevOnPrimary(cfg, node, instance, device, info):
2605   """Create a tree of block devices on the primary node.
2606
2607   This always creates all devices.
2608
2609   """
2610   if device.children:
2611     for child in device.children:
2612       if not _CreateBlockDevOnPrimary(cfg, node, instance, child, info):
2613         return False
2614
2615   cfg.SetDiskID(device, node)
2616   new_id = rpc.call_blockdev_create(node, device, device.size,
2617                                     instance.name, True, info)
2618   if not new_id:
2619     return False
2620   if device.physical_id is None:
2621     device.physical_id = new_id
2622   return True
2623
2624
2625 def _CreateBlockDevOnSecondary(cfg, node, instance, device, force, info):
2626   """Create a tree of block devices on a secondary node.
2627
2628   If this device type has to be created on secondaries, create it and
2629   all its children.
2630
2631   If not, just recurse to children keeping the same 'force' value.
2632
2633   """
2634   if device.CreateOnSecondary():
2635     force = True
2636   if device.children:
2637     for child in device.children:
2638       if not _CreateBlockDevOnSecondary(cfg, node, instance,
2639                                         child, force, info):
2640         return False
2641
2642   if not force:
2643     return True
2644   cfg.SetDiskID(device, node)
2645   new_id = rpc.call_blockdev_create(node, device, device.size,
2646                                     instance.name, False, info)
2647   if not new_id:
2648     return False
2649   if device.physical_id is None:
2650     device.physical_id = new_id
2651   return True
2652
2653
2654 def _GenerateUniqueNames(cfg, exts):
2655   """Generate a suitable LV name.
2656
2657   This will generate a logical volume name for the given instance.
2658
2659   """
2660   results = []
2661   for val in exts:
2662     new_id = cfg.GenerateUniqueID()
2663     results.append("%s%s" % (new_id, val))
2664   return results
2665
2666
2667 def _GenerateMDDRBDBranch(cfg, primary, secondary, size, names):
2668   """Generate a drbd device complete with its children.
2669
2670   """
2671   port = cfg.AllocatePort()
2672   vgname = cfg.GetVGName()
2673   dev_data = objects.Disk(dev_type=constants.LD_LV, size=size,
2674                           logical_id=(vgname, names[0]))
2675   dev_meta = objects.Disk(dev_type=constants.LD_LV, size=128,
2676                           logical_id=(vgname, names[1]))
2677   drbd_dev = objects.Disk(dev_type=constants.LD_DRBD7, size=size,
2678                           logical_id = (primary, secondary, port),
2679                           children = [dev_data, dev_meta])
2680   return drbd_dev
2681
2682
2683 def _GenerateDRBD8Branch(cfg, primary, secondary, size, names, iv_name):
2684   """Generate a drbd8 device complete with its children.
2685
2686   """
2687   port = cfg.AllocatePort()
2688   vgname = cfg.GetVGName()
2689   dev_data = objects.Disk(dev_type=constants.LD_LV, size=size,
2690                           logical_id=(vgname, names[0]))
2691   dev_meta = objects.Disk(dev_type=constants.LD_LV, size=128,
2692                           logical_id=(vgname, names[1]))
2693   drbd_dev = objects.Disk(dev_type=constants.LD_DRBD8, size=size,
2694                           logical_id = (primary, secondary, port),
2695                           children = [dev_data, dev_meta],
2696                           iv_name=iv_name)
2697   return drbd_dev
2698
2699 def _GenerateDiskTemplate(cfg, template_name,
2700                           instance_name, primary_node,
2701                           secondary_nodes, disk_sz, swap_sz):
2702   """Generate the entire disk layout for a given template type.
2703
2704   """
2705   #TODO: compute space requirements
2706
2707   vgname = cfg.GetVGName()
2708   if template_name == "diskless":
2709     disks = []
2710   elif template_name == "plain":
2711     if len(secondary_nodes) != 0:
2712       raise errors.ProgrammerError("Wrong template configuration")
2713
2714     names = _GenerateUniqueNames(cfg, [".sda", ".sdb"])
2715     sda_dev = objects.Disk(dev_type=constants.LD_LV, size=disk_sz,
2716                            logical_id=(vgname, names[0]),
2717                            iv_name = "sda")
2718     sdb_dev = objects.Disk(dev_type=constants.LD_LV, size=swap_sz,
2719                            logical_id=(vgname, names[1]),
2720                            iv_name = "sdb")
2721     disks = [sda_dev, sdb_dev]
2722   elif template_name == "local_raid1":
2723     if len(secondary_nodes) != 0:
2724       raise errors.ProgrammerError("Wrong template configuration")
2725
2726
2727     names = _GenerateUniqueNames(cfg, [".sda_m1", ".sda_m2",
2728                                        ".sdb_m1", ".sdb_m2"])
2729     sda_dev_m1 = objects.Disk(dev_type=constants.LD_LV, size=disk_sz,
2730                               logical_id=(vgname, names[0]))
2731     sda_dev_m2 = objects.Disk(dev_type=constants.LD_LV, size=disk_sz,
2732                               logical_id=(vgname, names[1]))
2733     md_sda_dev = objects.Disk(dev_type=constants.LD_MD_R1, iv_name = "sda",
2734                               size=disk_sz,
2735                               children = [sda_dev_m1, sda_dev_m2])
2736     sdb_dev_m1 = objects.Disk(dev_type=constants.LD_LV, size=swap_sz,
2737                               logical_id=(vgname, names[2]))
2738     sdb_dev_m2 = objects.Disk(dev_type=constants.LD_LV, size=swap_sz,
2739                               logical_id=(vgname, names[3]))
2740     md_sdb_dev = objects.Disk(dev_type=constants.LD_MD_R1, iv_name = "sdb",
2741                               size=swap_sz,
2742                               children = [sdb_dev_m1, sdb_dev_m2])
2743     disks = [md_sda_dev, md_sdb_dev]
2744   elif template_name == constants.DT_REMOTE_RAID1:
2745     if len(secondary_nodes) != 1:
2746       raise errors.ProgrammerError("Wrong template configuration")
2747     remote_node = secondary_nodes[0]
2748     names = _GenerateUniqueNames(cfg, [".sda_data", ".sda_meta",
2749                                        ".sdb_data", ".sdb_meta"])
2750     drbd_sda_dev = _GenerateMDDRBDBranch(cfg, primary_node, remote_node,
2751                                          disk_sz, names[0:2])
2752     md_sda_dev = objects.Disk(dev_type=constants.LD_MD_R1, iv_name="sda",
2753                               children = [drbd_sda_dev], size=disk_sz)
2754     drbd_sdb_dev = _GenerateMDDRBDBranch(cfg, primary_node, remote_node,
2755                                          swap_sz, names[2:4])
2756     md_sdb_dev = objects.Disk(dev_type=constants.LD_MD_R1, iv_name="sdb",
2757                               children = [drbd_sdb_dev], size=swap_sz)
2758     disks = [md_sda_dev, md_sdb_dev]
2759   elif template_name == constants.DT_DRBD8:
2760     if len(secondary_nodes) != 1:
2761       raise errors.ProgrammerError("Wrong template configuration")
2762     remote_node = secondary_nodes[0]
2763     names = _GenerateUniqueNames(cfg, [".sda_data", ".sda_meta",
2764                                        ".sdb_data", ".sdb_meta"])
2765     drbd_sda_dev = _GenerateDRBD8Branch(cfg, primary_node, remote_node,
2766                                          disk_sz, names[0:2], "sda")
2767     drbd_sdb_dev = _GenerateDRBD8Branch(cfg, primary_node, remote_node,
2768                                          swap_sz, names[2:4], "sdb")
2769     disks = [drbd_sda_dev, drbd_sdb_dev]
2770   else:
2771     raise errors.ProgrammerError("Invalid disk template '%s'" % template_name)
2772   return disks
2773
2774
2775 def _GetInstanceInfoText(instance):
2776   """Compute that text that should be added to the disk's metadata.
2777
2778   """
2779   return "originstname+%s" % instance.name
2780
2781
2782 def _CreateDisks(cfg, instance):
2783   """Create all disks for an instance.
2784
2785   This abstracts away some work from AddInstance.
2786
2787   Args:
2788     instance: the instance object
2789
2790   Returns:
2791     True or False showing the success of the creation process
2792
2793   """
2794   info = _GetInstanceInfoText(instance)
2795
2796   for device in instance.disks:
2797     logger.Info("creating volume %s for instance %s" %
2798               (device.iv_name, instance.name))
2799     #HARDCODE
2800     for secondary_node in instance.secondary_nodes:
2801       if not _CreateBlockDevOnSecondary(cfg, secondary_node, instance,
2802                                         device, False, info):
2803         logger.Error("failed to create volume %s (%s) on secondary node %s!" %
2804                      (device.iv_name, device, secondary_node))
2805         return False
2806     #HARDCODE
2807     if not _CreateBlockDevOnPrimary(cfg, instance.primary_node,
2808                                     instance, device, info):
2809       logger.Error("failed to create volume %s on primary!" %
2810                    device.iv_name)
2811       return False
2812   return True
2813
2814
2815 def _RemoveDisks(instance, cfg):
2816   """Remove all disks for an instance.
2817
2818   This abstracts away some work from `AddInstance()` and
2819   `RemoveInstance()`. Note that in case some of the devices couldn't
2820   be removed, the removal will continue with the other ones (compare
2821   with `_CreateDisks()`).
2822
2823   Args:
2824     instance: the instance object
2825
2826   Returns:
2827     True or False showing the success of the removal proces
2828
2829   """
2830   logger.Info("removing block devices for instance %s" % instance.name)
2831
2832   result = True
2833   for device in instance.disks:
2834     for node, disk in device.ComputeNodeTree(instance.primary_node):
2835       cfg.SetDiskID(disk, node)
2836       if not rpc.call_blockdev_remove(node, disk):
2837         logger.Error("could not remove block device %s on node %s,"
2838                      " continuing anyway" %
2839                      (device.iv_name, node))
2840         result = False
2841   return result
2842
2843
2844 class LUCreateInstance(LogicalUnit):
2845   """Create an instance.
2846
2847   """
2848   HPATH = "instance-add"
2849   HTYPE = constants.HTYPE_INSTANCE
2850   _OP_REQP = ["instance_name", "mem_size", "disk_size", "pnode",
2851               "disk_template", "swap_size", "mode", "start", "vcpus",
2852               "wait_for_sync", "ip_check", "mac"]
2853
2854   def BuildHooksEnv(self):
2855     """Build hooks env.
2856
2857     This runs on master, primary and secondary nodes of the instance.
2858
2859     """
2860     env = {
2861       "INSTANCE_DISK_TEMPLATE": self.op.disk_template,
2862       "INSTANCE_DISK_SIZE": self.op.disk_size,
2863       "INSTANCE_SWAP_SIZE": self.op.swap_size,
2864       "INSTANCE_ADD_MODE": self.op.mode,
2865       }
2866     if self.op.mode == constants.INSTANCE_IMPORT:
2867       env["INSTANCE_SRC_NODE"] = self.op.src_node
2868       env["INSTANCE_SRC_PATH"] = self.op.src_path
2869       env["INSTANCE_SRC_IMAGE"] = self.src_image
2870
2871     env.update(_BuildInstanceHookEnv(name=self.op.instance_name,
2872       primary_node=self.op.pnode,
2873       secondary_nodes=self.secondaries,
2874       status=self.instance_status,
2875       os_type=self.op.os_type,
2876       memory=self.op.mem_size,
2877       vcpus=self.op.vcpus,
2878       nics=[(self.inst_ip, self.op.bridge)],
2879     ))
2880
2881     nl = ([self.sstore.GetMasterNode(), self.op.pnode] +
2882           self.secondaries)
2883     return env, nl, nl
2884
2885
2886   def CheckPrereq(self):
2887     """Check prerequisites.
2888
2889     """
2890     for attr in ["kernel_path", "initrd_path", "hvm_boot_order"]:
2891       if not hasattr(self.op, attr):
2892         setattr(self.op, attr, None)
2893
2894     if self.op.mode not in (constants.INSTANCE_CREATE,
2895                             constants.INSTANCE_IMPORT):
2896       raise errors.OpPrereqError("Invalid instance creation mode '%s'" %
2897                                  self.op.mode)
2898
2899     if self.op.mode == constants.INSTANCE_IMPORT:
2900       src_node = getattr(self.op, "src_node", None)
2901       src_path = getattr(self.op, "src_path", None)
2902       if src_node is None or src_path is None:
2903         raise errors.OpPrereqError("Importing an instance requires source"
2904                                    " node and path options")
2905       src_node_full = self.cfg.ExpandNodeName(src_node)
2906       if src_node_full is None:
2907         raise errors.OpPrereqError("Unknown source node '%s'" % src_node)
2908       self.op.src_node = src_node = src_node_full
2909
2910       if not os.path.isabs(src_path):
2911         raise errors.OpPrereqError("The source path must be absolute")
2912
2913       export_info = rpc.call_export_info(src_node, src_path)
2914
2915       if not export_info:
2916         raise errors.OpPrereqError("No export found in dir %s" % src_path)
2917
2918       if not export_info.has_section(constants.INISECT_EXP):
2919         raise errors.ProgrammerError("Corrupted export config")
2920
2921       ei_version = export_info.get(constants.INISECT_EXP, 'version')
2922       if (int(ei_version) != constants.EXPORT_VERSION):
2923         raise errors.OpPrereqError("Wrong export version %s (wanted %d)" %
2924                                    (ei_version, constants.EXPORT_VERSION))
2925
2926       if int(export_info.get(constants.INISECT_INS, 'disk_count')) > 1:
2927         raise errors.OpPrereqError("Can't import instance with more than"
2928                                    " one data disk")
2929
2930       # FIXME: are the old os-es, disk sizes, etc. useful?
2931       self.op.os_type = export_info.get(constants.INISECT_EXP, 'os')
2932       diskimage = os.path.join(src_path, export_info.get(constants.INISECT_INS,
2933                                                          'disk0_dump'))
2934       self.src_image = diskimage
2935     else: # INSTANCE_CREATE
2936       if getattr(self.op, "os_type", None) is None:
2937         raise errors.OpPrereqError("No guest OS specified")
2938
2939     # check primary node
2940     pnode = self.cfg.GetNodeInfo(self.cfg.ExpandNodeName(self.op.pnode))
2941     if pnode is None:
2942       raise errors.OpPrereqError("Primary node '%s' is unknown" %
2943                                  self.op.pnode)
2944     self.op.pnode = pnode.name
2945     self.pnode = pnode
2946     self.secondaries = []
2947     # disk template and mirror node verification
2948     if self.op.disk_template not in constants.DISK_TEMPLATES:
2949       raise errors.OpPrereqError("Invalid disk template name")
2950
2951     if self.op.disk_template in constants.DTS_NET_MIRROR:
2952       if getattr(self.op, "snode", None) is None:
2953         raise errors.OpPrereqError("The networked disk templates need"
2954                                    " a mirror node")
2955
2956       snode_name = self.cfg.ExpandNodeName(self.op.snode)
2957       if snode_name is None:
2958         raise errors.OpPrereqError("Unknown secondary node '%s'" %
2959                                    self.op.snode)
2960       elif snode_name == pnode.name:
2961         raise errors.OpPrereqError("The secondary node cannot be"
2962                                    " the primary node.")
2963       self.secondaries.append(snode_name)
2964
2965     # Check lv size requirements
2966     nodenames = [pnode.name] + self.secondaries
2967     nodeinfo = rpc.call_node_info(nodenames, self.cfg.GetVGName())
2968
2969     # Required free disk space as a function of disk and swap space
2970     req_size_dict = {
2971       constants.DT_DISKLESS: 0,
2972       constants.DT_PLAIN: self.op.disk_size + self.op.swap_size,
2973       constants.DT_LOCAL_RAID1: (self.op.disk_size + self.op.swap_size) * 2,
2974       # 256 MB are added for drbd metadata, 128MB for each drbd device
2975       constants.DT_REMOTE_RAID1: self.op.disk_size + self.op.swap_size + 256,
2976       constants.DT_DRBD8: self.op.disk_size + self.op.swap_size + 256,
2977     }
2978
2979     if self.op.disk_template not in req_size_dict:
2980       raise errors.ProgrammerError("Disk template '%s' size requirement"
2981                                    " is unknown" %  self.op.disk_template)
2982
2983     req_size = req_size_dict[self.op.disk_template]
2984
2985     for node in nodenames:
2986       info = nodeinfo.get(node, None)
2987       if not info:
2988         raise errors.OpPrereqError("Cannot get current information"
2989                                    " from node '%s'" % nodeinfo)
2990       if req_size > info['vg_free']:
2991         raise errors.OpPrereqError("Not enough disk space on target node %s."
2992                                    " %d MB available, %d MB required" %
2993                                    (node, info['vg_free'], req_size))
2994
2995     # os verification
2996     os_obj = rpc.call_os_get(pnode.name, self.op.os_type)
2997     if not os_obj:
2998       raise errors.OpPrereqError("OS '%s' not in supported os list for"
2999                                  " primary node"  % self.op.os_type)
3000
3001     if self.op.kernel_path == constants.VALUE_NONE:
3002       raise errors.OpPrereqError("Can't set instance kernel to none")
3003
3004     # instance verification
3005     hostname1 = utils.HostInfo(self.op.instance_name)
3006
3007     self.op.instance_name = instance_name = hostname1.name
3008     instance_list = self.cfg.GetInstanceList()
3009     if instance_name in instance_list:
3010       raise errors.OpPrereqError("Instance '%s' is already in the cluster" %
3011                                  instance_name)
3012
3013     ip = getattr(self.op, "ip", None)
3014     if ip is None or ip.lower() == "none":
3015       inst_ip = None
3016     elif ip.lower() == "auto":
3017       inst_ip = hostname1.ip
3018     else:
3019       if not utils.IsValidIP(ip):
3020         raise errors.OpPrereqError("given IP address '%s' doesn't look"
3021                                    " like a valid IP" % ip)
3022       inst_ip = ip
3023     self.inst_ip = inst_ip
3024
3025     if self.op.start and not self.op.ip_check:
3026       raise errors.OpPrereqError("Cannot ignore IP address conflicts when"
3027                                  " adding an instance in start mode")
3028
3029     if self.op.ip_check:
3030       if utils.TcpPing(utils.HostInfo().name, hostname1.ip,
3031                        constants.DEFAULT_NODED_PORT):
3032         raise errors.OpPrereqError("IP %s of instance %s already in use" %
3033                                    (hostname1.ip, instance_name))
3034
3035     # MAC address verification
3036     if self.op.mac != "auto":
3037       if not utils.IsValidMac(self.op.mac.lower()):
3038         raise errors.OpPrereqError("invalid MAC address specified: %s" %
3039                                    self.op.mac)
3040
3041     # bridge verification
3042     bridge = getattr(self.op, "bridge", None)
3043     if bridge is None:
3044       self.op.bridge = self.cfg.GetDefBridge()
3045     else:
3046       self.op.bridge = bridge
3047
3048     if not rpc.call_bridges_exist(self.pnode.name, [self.op.bridge]):
3049       raise errors.OpPrereqError("target bridge '%s' does not exist on"
3050                                  " destination node '%s'" %
3051                                  (self.op.bridge, pnode.name))
3052
3053     # boot order verification
3054     if self.op.hvm_boot_order is not None:
3055       if len(self.op.hvm_boot_order.strip("acdn")) != 0:
3056              raise errors.OpPrereqError("invalid boot order specified,"
3057                                         " must be one or more of [acdn]")
3058
3059     if self.op.start:
3060       self.instance_status = 'up'
3061     else:
3062       self.instance_status = 'down'
3063
3064   def Exec(self, feedback_fn):
3065     """Create and add the instance to the cluster.
3066
3067     """
3068     instance = self.op.instance_name
3069     pnode_name = self.pnode.name
3070
3071     if self.op.mac == "auto":
3072       mac_address = self.cfg.GenerateMAC()
3073     else:
3074       mac_address = self.op.mac
3075
3076     nic = objects.NIC(bridge=self.op.bridge, mac=mac_address)
3077     if self.inst_ip is not None:
3078       nic.ip = self.inst_ip
3079
3080     ht_kind = self.sstore.GetHypervisorType()
3081     if ht_kind in constants.HTS_REQ_PORT:
3082       network_port = self.cfg.AllocatePort()
3083     else:
3084       network_port = None
3085
3086     disks = _GenerateDiskTemplate(self.cfg,
3087                                   self.op.disk_template,
3088                                   instance, pnode_name,
3089                                   self.secondaries, self.op.disk_size,
3090                                   self.op.swap_size)
3091
3092     iobj = objects.Instance(name=instance, os=self.op.os_type,
3093                             primary_node=pnode_name,
3094                             memory=self.op.mem_size,
3095                             vcpus=self.op.vcpus,
3096                             nics=[nic], disks=disks,
3097                             disk_template=self.op.disk_template,
3098                             status=self.instance_status,
3099                             network_port=network_port,
3100                             kernel_path=self.op.kernel_path,
3101                             initrd_path=self.op.initrd_path,
3102                             hvm_boot_order=self.op.hvm_boot_order,
3103                             )
3104
3105     feedback_fn("* creating instance disks...")
3106     if not _CreateDisks(self.cfg, iobj):
3107       _RemoveDisks(iobj, self.cfg)
3108       raise errors.OpExecError("Device creation failed, reverting...")
3109
3110     feedback_fn("adding instance %s to cluster config" % instance)
3111
3112     self.cfg.AddInstance(iobj)
3113
3114     if self.op.wait_for_sync:
3115       disk_abort = not _WaitForSync(self.cfg, iobj, self.proc)
3116     elif iobj.disk_template in constants.DTS_NET_MIRROR:
3117       # make sure the disks are not degraded (still sync-ing is ok)
3118       time.sleep(15)
3119       feedback_fn("* checking mirrors status")
3120       disk_abort = not _WaitForSync(self.cfg, iobj, self.proc, oneshot=True)
3121     else:
3122       disk_abort = False
3123
3124     if disk_abort:
3125       _RemoveDisks(iobj, self.cfg)
3126       self.cfg.RemoveInstance(iobj.name)
3127       raise errors.OpExecError("There are some degraded disks for"
3128                                " this instance")
3129
3130     feedback_fn("creating os for instance %s on node %s" %
3131                 (instance, pnode_name))
3132
3133     if iobj.disk_template != constants.DT_DISKLESS:
3134       if self.op.mode == constants.INSTANCE_CREATE:
3135         feedback_fn("* running the instance OS create scripts...")
3136         if not rpc.call_instance_os_add(pnode_name, iobj, "sda", "sdb"):
3137           raise errors.OpExecError("could not add os for instance %s"
3138                                    " on node %s" %
3139                                    (instance, pnode_name))
3140
3141       elif self.op.mode == constants.INSTANCE_IMPORT:
3142         feedback_fn("* running the instance OS import scripts...")
3143         src_node = self.op.src_node
3144         src_image = self.src_image
3145         if not rpc.call_instance_os_import(pnode_name, iobj, "sda", "sdb",
3146                                                 src_node, src_image):
3147           raise errors.OpExecError("Could not import os for instance"
3148                                    " %s on node %s" %
3149                                    (instance, pnode_name))
3150       else:
3151         # also checked in the prereq part
3152         raise errors.ProgrammerError("Unknown OS initialization mode '%s'"
3153                                      % self.op.mode)
3154
3155     if self.op.start:
3156       logger.Info("starting instance %s on node %s" % (instance, pnode_name))
3157       feedback_fn("* starting instance...")
3158       if not rpc.call_instance_start(pnode_name, iobj, None):
3159         raise errors.OpExecError("Could not start instance")
3160
3161
3162 class LUConnectConsole(NoHooksLU):
3163   """Connect to an instance's console.
3164
3165   This is somewhat special in that it returns the command line that
3166   you need to run on the master node in order to connect to the
3167   console.
3168
3169   """
3170   _OP_REQP = ["instance_name"]
3171
3172   def CheckPrereq(self):
3173     """Check prerequisites.
3174
3175     This checks that the instance is in the cluster.
3176
3177     """
3178     instance = self.cfg.GetInstanceInfo(
3179       self.cfg.ExpandInstanceName(self.op.instance_name))
3180     if instance is None:
3181       raise errors.OpPrereqError("Instance '%s' not known" %
3182                                  self.op.instance_name)
3183     self.instance = instance
3184
3185   def Exec(self, feedback_fn):
3186     """Connect to the console of an instance
3187
3188     """
3189     instance = self.instance
3190     node = instance.primary_node
3191
3192     node_insts = rpc.call_instance_list([node])[node]
3193     if node_insts is False:
3194       raise errors.OpExecError("Can't connect to node %s." % node)
3195
3196     if instance.name not in node_insts:
3197       raise errors.OpExecError("Instance %s is not running." % instance.name)
3198
3199     logger.Debug("connecting to console of %s on %s" % (instance.name, node))
3200
3201     hyper = hypervisor.GetHypervisor()
3202     console_cmd = hyper.GetShellCommandForConsole(instance)
3203     # build ssh cmdline
3204     argv = ["ssh", "-q", "-t"]
3205     argv.extend(ssh.KNOWN_HOSTS_OPTS)
3206     argv.extend(ssh.BATCH_MODE_OPTS)
3207     argv.append(node)
3208     argv.append(console_cmd)
3209     return "ssh", argv
3210
3211
3212 class LUAddMDDRBDComponent(LogicalUnit):
3213   """Adda new mirror member to an instance's disk.
3214
3215   """
3216   HPATH = "mirror-add"
3217   HTYPE = constants.HTYPE_INSTANCE
3218   _OP_REQP = ["instance_name", "remote_node", "disk_name"]
3219
3220   def BuildHooksEnv(self):
3221     """Build hooks env.
3222
3223     This runs on the master, the primary and all the secondaries.
3224
3225     """
3226     env = {
3227       "NEW_SECONDARY": self.op.remote_node,
3228       "DISK_NAME": self.op.disk_name,
3229       }
3230     env.update(_BuildInstanceHookEnvByObject(self.instance))
3231     nl = [self.sstore.GetMasterNode(), self.instance.primary_node,
3232           self.op.remote_node,] + list(self.instance.secondary_nodes)
3233     return env, nl, nl
3234
3235   def CheckPrereq(self):
3236     """Check prerequisites.
3237
3238     This checks that the instance is in the cluster.
3239
3240     """
3241     instance = self.cfg.GetInstanceInfo(
3242       self.cfg.ExpandInstanceName(self.op.instance_name))
3243     if instance is None:
3244       raise errors.OpPrereqError("Instance '%s' not known" %
3245                                  self.op.instance_name)
3246     self.instance = instance
3247
3248     remote_node = self.cfg.ExpandNodeName(self.op.remote_node)
3249     if remote_node is None:
3250       raise errors.OpPrereqError("Node '%s' not known" % self.op.remote_node)
3251     self.remote_node = remote_node
3252
3253     if remote_node == instance.primary_node:
3254       raise errors.OpPrereqError("The specified node is the primary node of"
3255                                  " the instance.")
3256
3257     if instance.disk_template != constants.DT_REMOTE_RAID1:
3258       raise errors.OpPrereqError("Instance's disk layout is not"
3259                                  " remote_raid1.")
3260     for disk in instance.disks:
3261       if disk.iv_name == self.op.disk_name:
3262         break
3263     else:
3264       raise errors.OpPrereqError("Can't find this device ('%s') in the"
3265                                  " instance." % self.op.disk_name)
3266     if len(disk.children) > 1:
3267       raise errors.OpPrereqError("The device already has two slave devices."
3268                                  " This would create a 3-disk raid1 which we"
3269                                  " don't allow.")
3270     self.disk = disk
3271
3272   def Exec(self, feedback_fn):
3273     """Add the mirror component
3274
3275     """
3276     disk = self.disk
3277     instance = self.instance
3278
3279     remote_node = self.remote_node
3280     lv_names = [".%s_%s" % (disk.iv_name, suf) for suf in ["data", "meta"]]
3281     names = _GenerateUniqueNames(self.cfg, lv_names)
3282     new_drbd = _GenerateMDDRBDBranch(self.cfg, instance.primary_node,
3283                                      remote_node, disk.size, names)
3284
3285     logger.Info("adding new mirror component on secondary")
3286     #HARDCODE
3287     if not _CreateBlockDevOnSecondary(self.cfg, remote_node, instance,
3288                                       new_drbd, False,
3289                                       _GetInstanceInfoText(instance)):
3290       raise errors.OpExecError("Failed to create new component on secondary"
3291                                " node %s" % remote_node)
3292
3293     logger.Info("adding new mirror component on primary")
3294     #HARDCODE
3295     if not _CreateBlockDevOnPrimary(self.cfg, instance.primary_node,
3296                                     instance, new_drbd,
3297                                     _GetInstanceInfoText(instance)):
3298       # remove secondary dev
3299       self.cfg.SetDiskID(new_drbd, remote_node)
3300       rpc.call_blockdev_remove(remote_node, new_drbd)
3301       raise errors.OpExecError("Failed to create volume on primary")
3302
3303     # the device exists now
3304     # call the primary node to add the mirror to md
3305     logger.Info("adding new mirror component to md")
3306     if not rpc.call_blockdev_addchildren(instance.primary_node,
3307                                          disk, [new_drbd]):
3308       logger.Error("Can't add mirror compoment to md!")
3309       self.cfg.SetDiskID(new_drbd, remote_node)
3310       if not rpc.call_blockdev_remove(remote_node, new_drbd):
3311         logger.Error("Can't rollback on secondary")
3312       self.cfg.SetDiskID(new_drbd, instance.primary_node)
3313       if not rpc.call_blockdev_remove(instance.primary_node, new_drbd):
3314         logger.Error("Can't rollback on primary")
3315       raise errors.OpExecError("Can't add mirror component to md array")
3316
3317     disk.children.append(new_drbd)
3318
3319     self.cfg.AddInstance(instance)
3320
3321     _WaitForSync(self.cfg, instance, self.proc)
3322
3323     return 0
3324
3325
3326 class LURemoveMDDRBDComponent(LogicalUnit):
3327   """Remove a component from a remote_raid1 disk.
3328
3329   """
3330   HPATH = "mirror-remove"
3331   HTYPE = constants.HTYPE_INSTANCE
3332   _OP_REQP = ["instance_name", "disk_name", "disk_id"]
3333
3334   def BuildHooksEnv(self):
3335     """Build hooks env.
3336
3337     This runs on the master, the primary and all the secondaries.
3338
3339     """
3340     env = {
3341       "DISK_NAME": self.op.disk_name,
3342       "DISK_ID": self.op.disk_id,
3343       "OLD_SECONDARY": self.old_secondary,
3344       }
3345     env.update(_BuildInstanceHookEnvByObject(self.instance))
3346     nl = [self.sstore.GetMasterNode(),
3347           self.instance.primary_node] + list(self.instance.secondary_nodes)
3348     return env, nl, nl
3349
3350   def CheckPrereq(self):
3351     """Check prerequisites.
3352
3353     This checks that the instance is in the cluster.
3354
3355     """
3356     instance = self.cfg.GetInstanceInfo(
3357       self.cfg.ExpandInstanceName(self.op.instance_name))
3358     if instance is None:
3359       raise errors.OpPrereqError("Instance '%s' not known" %
3360                                  self.op.instance_name)
3361     self.instance = instance
3362
3363     if instance.disk_template != constants.DT_REMOTE_RAID1:
3364       raise errors.OpPrereqError("Instance's disk layout is not"
3365                                  " remote_raid1.")
3366     for disk in instance.disks:
3367       if disk.iv_name == self.op.disk_name:
3368         break
3369     else:
3370       raise errors.OpPrereqError("Can't find this device ('%s') in the"
3371                                  " instance." % self.op.disk_name)
3372     for child in disk.children:
3373       if (child.dev_type == constants.LD_DRBD7 and
3374           child.logical_id[2] == self.op.disk_id):
3375         break
3376     else:
3377       raise errors.OpPrereqError("Can't find the device with this port.")
3378
3379     if len(disk.children) < 2:
3380       raise errors.OpPrereqError("Cannot remove the last component from"
3381                                  " a mirror.")
3382     self.disk = disk
3383     self.child = child
3384     if self.child.logical_id[0] == instance.primary_node:
3385       oid = 1
3386     else:
3387       oid = 0
3388     self.old_secondary = self.child.logical_id[oid]
3389
3390   def Exec(self, feedback_fn):
3391     """Remove the mirror component
3392
3393     """
3394     instance = self.instance
3395     disk = self.disk
3396     child = self.child
3397     logger.Info("remove mirror component")
3398     self.cfg.SetDiskID(disk, instance.primary_node)
3399     if not rpc.call_blockdev_removechildren(instance.primary_node,
3400                                             disk, [child]):
3401       raise errors.OpExecError("Can't remove child from mirror.")
3402
3403     for node in child.logical_id[:2]:
3404       self.cfg.SetDiskID(child, node)
3405       if not rpc.call_blockdev_remove(node, child):
3406         logger.Error("Warning: failed to remove device from node %s,"
3407                      " continuing operation." % node)
3408
3409     disk.children.remove(child)
3410     self.cfg.AddInstance(instance)
3411
3412
3413 class LUReplaceDisks(LogicalUnit):
3414   """Replace the disks of an instance.
3415
3416   """
3417   HPATH = "mirrors-replace"
3418   HTYPE = constants.HTYPE_INSTANCE
3419   _OP_REQP = ["instance_name", "mode", "disks"]
3420
3421   def BuildHooksEnv(self):
3422     """Build hooks env.
3423
3424     This runs on the master, the primary and all the secondaries.
3425
3426     """
3427     env = {
3428       "MODE": self.op.mode,
3429       "NEW_SECONDARY": self.op.remote_node,
3430       "OLD_SECONDARY": self.instance.secondary_nodes[0],
3431       }
3432     env.update(_BuildInstanceHookEnvByObject(self.instance))
3433     nl = [
3434       self.sstore.GetMasterNode(),
3435       self.instance.primary_node,
3436       ]
3437     if self.op.remote_node is not None:
3438       nl.append(self.op.remote_node)
3439     return env, nl, nl
3440
3441   def CheckPrereq(self):
3442     """Check prerequisites.
3443
3444     This checks that the instance is in the cluster.
3445
3446     """
3447     instance = self.cfg.GetInstanceInfo(
3448       self.cfg.ExpandInstanceName(self.op.instance_name))
3449     if instance is None:
3450       raise errors.OpPrereqError("Instance '%s' not known" %
3451                                  self.op.instance_name)
3452     self.instance = instance
3453     self.op.instance_name = instance.name
3454
3455     if instance.disk_template not in constants.DTS_NET_MIRROR:
3456       raise errors.OpPrereqError("Instance's disk layout is not"
3457                                  " network mirrored.")
3458
3459     if len(instance.secondary_nodes) != 1:
3460       raise errors.OpPrereqError("The instance has a strange layout,"
3461                                  " expected one secondary but found %d" %
3462                                  len(instance.secondary_nodes))
3463
3464     self.sec_node = instance.secondary_nodes[0]
3465
3466     remote_node = getattr(self.op, "remote_node", None)
3467     if remote_node is not None:
3468       remote_node = self.cfg.ExpandNodeName(remote_node)
3469       if remote_node is None:
3470         raise errors.OpPrereqError("Node '%s' not known" %
3471                                    self.op.remote_node)
3472       self.remote_node_info = self.cfg.GetNodeInfo(remote_node)
3473     else:
3474       self.remote_node_info = None
3475     if remote_node == instance.primary_node:
3476       raise errors.OpPrereqError("The specified node is the primary node of"
3477                                  " the instance.")
3478     elif remote_node == self.sec_node:
3479       if self.op.mode == constants.REPLACE_DISK_SEC:
3480         # this is for DRBD8, where we can't execute the same mode of
3481         # replacement as for drbd7 (no different port allocated)
3482         raise errors.OpPrereqError("Same secondary given, cannot execute"
3483                                    " replacement")
3484       # the user gave the current secondary, switch to
3485       # 'no-replace-secondary' mode for drbd7
3486       remote_node = None
3487     if (instance.disk_template == constants.DT_REMOTE_RAID1 and
3488         self.op.mode != constants.REPLACE_DISK_ALL):
3489       raise errors.OpPrereqError("Template 'remote_raid1' only allows all"
3490                                  " disks replacement, not individual ones")
3491     if instance.disk_template == constants.DT_DRBD8:
3492       if (self.op.mode == constants.REPLACE_DISK_ALL and
3493           remote_node is not None):
3494         # switch to replace secondary mode
3495         self.op.mode = constants.REPLACE_DISK_SEC
3496
3497       if self.op.mode == constants.REPLACE_DISK_ALL:
3498         raise errors.OpPrereqError("Template 'drbd' only allows primary or"
3499                                    " secondary disk replacement, not"
3500                                    " both at once")
3501       elif self.op.mode == constants.REPLACE_DISK_PRI:
3502         if remote_node is not None:
3503           raise errors.OpPrereqError("Template 'drbd' does not allow changing"
3504                                      " the secondary while doing a primary"
3505                                      " node disk replacement")
3506         self.tgt_node = instance.primary_node
3507         self.oth_node = instance.secondary_nodes[0]
3508       elif self.op.mode == constants.REPLACE_DISK_SEC:
3509         self.new_node = remote_node # this can be None, in which case
3510                                     # we don't change the secondary
3511         self.tgt_node = instance.secondary_nodes[0]
3512         self.oth_node = instance.primary_node
3513       else:
3514         raise errors.ProgrammerError("Unhandled disk replace mode")
3515
3516     for name in self.op.disks:
3517       if instance.FindDisk(name) is None:
3518         raise errors.OpPrereqError("Disk '%s' not found for instance '%s'" %
3519                                    (name, instance.name))
3520     self.op.remote_node = remote_node
3521
3522   def _ExecRR1(self, feedback_fn):
3523     """Replace the disks of an instance.
3524
3525     """
3526     instance = self.instance
3527     iv_names = {}
3528     # start of work
3529     if self.op.remote_node is None:
3530       remote_node = self.sec_node
3531     else:
3532       remote_node = self.op.remote_node
3533     cfg = self.cfg
3534     for dev in instance.disks:
3535       size = dev.size
3536       lv_names = [".%s_%s" % (dev.iv_name, suf) for suf in ["data", "meta"]]
3537       names = _GenerateUniqueNames(cfg, lv_names)
3538       new_drbd = _GenerateMDDRBDBranch(cfg, instance.primary_node,
3539                                        remote_node, size, names)
3540       iv_names[dev.iv_name] = (dev, dev.children[0], new_drbd)
3541       logger.Info("adding new mirror component on secondary for %s" %
3542                   dev.iv_name)
3543       #HARDCODE
3544       if not _CreateBlockDevOnSecondary(cfg, remote_node, instance,
3545                                         new_drbd, False,
3546                                         _GetInstanceInfoText(instance)):
3547         raise errors.OpExecError("Failed to create new component on secondary"
3548                                  " node %s. Full abort, cleanup manually!" %
3549                                  remote_node)
3550
3551       logger.Info("adding new mirror component on primary")
3552       #HARDCODE
3553       if not _CreateBlockDevOnPrimary(cfg, instance.primary_node,
3554                                       instance, new_drbd,
3555                                       _GetInstanceInfoText(instance)):
3556         # remove secondary dev
3557         cfg.SetDiskID(new_drbd, remote_node)
3558         rpc.call_blockdev_remove(remote_node, new_drbd)
3559         raise errors.OpExecError("Failed to create volume on primary!"
3560                                  " Full abort, cleanup manually!!")
3561
3562       # the device exists now
3563       # call the primary node to add the mirror to md
3564       logger.Info("adding new mirror component to md")
3565       if not rpc.call_blockdev_addchildren(instance.primary_node, dev,
3566                                            [new_drbd]):
3567         logger.Error("Can't add mirror compoment to md!")
3568         cfg.SetDiskID(new_drbd, remote_node)
3569         if not rpc.call_blockdev_remove(remote_node, new_drbd):
3570           logger.Error("Can't rollback on secondary")
3571         cfg.SetDiskID(new_drbd, instance.primary_node)
3572         if not rpc.call_blockdev_remove(instance.primary_node, new_drbd):
3573           logger.Error("Can't rollback on primary")
3574         raise errors.OpExecError("Full abort, cleanup manually!!")
3575
3576       dev.children.append(new_drbd)
3577       cfg.AddInstance(instance)
3578
3579     # this can fail as the old devices are degraded and _WaitForSync
3580     # does a combined result over all disks, so we don't check its
3581     # return value
3582     _WaitForSync(cfg, instance, self.proc, unlock=True)
3583
3584     # so check manually all the devices
3585     for name in iv_names:
3586       dev, child, new_drbd = iv_names[name]
3587       cfg.SetDiskID(dev, instance.primary_node)
3588       is_degr = rpc.call_blockdev_find(instance.primary_node, dev)[5]
3589       if is_degr:
3590         raise errors.OpExecError("MD device %s is degraded!" % name)
3591       cfg.SetDiskID(new_drbd, instance.primary_node)
3592       is_degr = rpc.call_blockdev_find(instance.primary_node, new_drbd)[5]
3593       if is_degr:
3594         raise errors.OpExecError("New drbd device %s is degraded!" % name)
3595
3596     for name in iv_names:
3597       dev, child, new_drbd = iv_names[name]
3598       logger.Info("remove mirror %s component" % name)
3599       cfg.SetDiskID(dev, instance.primary_node)
3600       if not rpc.call_blockdev_removechildren(instance.primary_node,
3601                                               dev, [child]):
3602         logger.Error("Can't remove child from mirror, aborting"
3603                      " *this device cleanup*.\nYou need to cleanup manually!!")
3604         continue
3605
3606       for node in child.logical_id[:2]:
3607         logger.Info("remove child device on %s" % node)
3608         cfg.SetDiskID(child, node)
3609         if not rpc.call_blockdev_remove(node, child):
3610           logger.Error("Warning: failed to remove device from node %s,"
3611                        " continuing operation." % node)
3612
3613       dev.children.remove(child)
3614
3615       cfg.AddInstance(instance)
3616
3617   def _ExecD8DiskOnly(self, feedback_fn):
3618     """Replace a disk on the primary or secondary for dbrd8.
3619
3620     The algorithm for replace is quite complicated:
3621       - for each disk to be replaced:
3622         - create new LVs on the target node with unique names
3623         - detach old LVs from the drbd device
3624         - rename old LVs to name_replaced.<time_t>
3625         - rename new LVs to old LVs
3626         - attach the new LVs (with the old names now) to the drbd device
3627       - wait for sync across all devices
3628       - for each modified disk:
3629         - remove old LVs (which have the name name_replaces.<time_t>)
3630
3631     Failures are not very well handled.
3632
3633     """
3634     steps_total = 6
3635     warning, info = (self.proc.LogWarning, self.proc.LogInfo)
3636     instance = self.instance
3637     iv_names = {}
3638     vgname = self.cfg.GetVGName()
3639     # start of work
3640     cfg = self.cfg
3641     tgt_node = self.tgt_node
3642     oth_node = self.oth_node
3643
3644     # Step: check device activation
3645     self.proc.LogStep(1, steps_total, "check device existence")
3646     info("checking volume groups")
3647     my_vg = cfg.GetVGName()
3648     results = rpc.call_vg_list([oth_node, tgt_node])
3649     if not results:
3650       raise errors.OpExecError("Can't list volume groups on the nodes")
3651     for node in oth_node, tgt_node:
3652       res = results.get(node, False)
3653       if not res or my_vg not in res:
3654         raise errors.OpExecError("Volume group '%s' not found on %s" %
3655                                  (my_vg, node))
3656     for dev in instance.disks:
3657       if not dev.iv_name in self.op.disks:
3658         continue
3659       for node in tgt_node, oth_node:
3660         info("checking %s on %s" % (dev.iv_name, node))
3661         cfg.SetDiskID(dev, node)
3662         if not rpc.call_blockdev_find(node, dev):
3663           raise errors.OpExecError("Can't find device %s on node %s" %
3664                                    (dev.iv_name, node))
3665
3666     # Step: check other node consistency
3667     self.proc.LogStep(2, steps_total, "check peer consistency")
3668     for dev in instance.disks:
3669       if not dev.iv_name in self.op.disks:
3670         continue
3671       info("checking %s consistency on %s" % (dev.iv_name, oth_node))
3672       if not _CheckDiskConsistency(self.cfg, dev, oth_node,
3673                                    oth_node==instance.primary_node):
3674         raise errors.OpExecError("Peer node (%s) has degraded storage, unsafe"
3675                                  " to replace disks on this node (%s)" %
3676                                  (oth_node, tgt_node))
3677
3678     # Step: create new storage
3679     self.proc.LogStep(3, steps_total, "allocate new storage")
3680     for dev in instance.disks:
3681       if not dev.iv_name in self.op.disks:
3682         continue
3683       size = dev.size
3684       cfg.SetDiskID(dev, tgt_node)
3685       lv_names = [".%s_%s" % (dev.iv_name, suf) for suf in ["data", "meta"]]
3686       names = _GenerateUniqueNames(cfg, lv_names)
3687       lv_data = objects.Disk(dev_type=constants.LD_LV, size=size,
3688                              logical_id=(vgname, names[0]))
3689       lv_meta = objects.Disk(dev_type=constants.LD_LV, size=128,
3690                              logical_id=(vgname, names[1]))
3691       new_lvs = [lv_data, lv_meta]
3692       old_lvs = dev.children
3693       iv_names[dev.iv_name] = (dev, old_lvs, new_lvs)
3694       info("creating new local storage on %s for %s" %
3695            (tgt_node, dev.iv_name))
3696       # since we *always* want to create this LV, we use the
3697       # _Create...OnPrimary (which forces the creation), even if we
3698       # are talking about the secondary node
3699       for new_lv in new_lvs:
3700         if not _CreateBlockDevOnPrimary(cfg, tgt_node, instance, new_lv,
3701                                         _GetInstanceInfoText(instance)):
3702           raise errors.OpExecError("Failed to create new LV named '%s' on"
3703                                    " node '%s'" %
3704                                    (new_lv.logical_id[1], tgt_node))
3705
3706     # Step: for each lv, detach+rename*2+attach
3707     self.proc.LogStep(4, steps_total, "change drbd configuration")
3708     for dev, old_lvs, new_lvs in iv_names.itervalues():
3709       info("detaching %s drbd from local storage" % dev.iv_name)
3710       if not rpc.call_blockdev_removechildren(tgt_node, dev, old_lvs):
3711         raise errors.OpExecError("Can't detach drbd from local storage on node"
3712                                  " %s for device %s" % (tgt_node, dev.iv_name))
3713       #dev.children = []
3714       #cfg.Update(instance)
3715
3716       # ok, we created the new LVs, so now we know we have the needed
3717       # storage; as such, we proceed on the target node to rename
3718       # old_lv to _old, and new_lv to old_lv; note that we rename LVs
3719       # using the assumption than logical_id == physical_id (which in
3720       # turn is the unique_id on that node)
3721
3722       # FIXME(iustin): use a better name for the replaced LVs
3723       temp_suffix = int(time.time())
3724       ren_fn = lambda d, suff: (d.physical_id[0],
3725                                 d.physical_id[1] + "_replaced-%s" % suff)
3726       # build the rename list based on what LVs exist on the node
3727       rlist = []
3728       for to_ren in old_lvs:
3729         find_res = rpc.call_blockdev_find(tgt_node, to_ren)
3730         if find_res is not None: # device exists
3731           rlist.append((to_ren, ren_fn(to_ren, temp_suffix)))
3732
3733       info("renaming the old LVs on the target node")
3734       if not rpc.call_blockdev_rename(tgt_node, rlist):
3735         raise errors.OpExecError("Can't rename old LVs on node %s" % tgt_node)
3736       # now we rename the new LVs to the old LVs
3737       info("renaming the new LVs on the target node")
3738       rlist = [(new, old.physical_id) for old, new in zip(old_lvs, new_lvs)]
3739       if not rpc.call_blockdev_rename(tgt_node, rlist):
3740         raise errors.OpExecError("Can't rename new LVs on node %s" % tgt_node)
3741
3742       for old, new in zip(old_lvs, new_lvs):
3743         new.logical_id = old.logical_id
3744         cfg.SetDiskID(new, tgt_node)
3745
3746       for disk in old_lvs:
3747         disk.logical_id = ren_fn(disk, temp_suffix)
3748         cfg.SetDiskID(disk, tgt_node)
3749
3750       # now that the new lvs have the old name, we can add them to the device
3751       info("adding new mirror component on %s" % tgt_node)
3752       if not rpc.call_blockdev_addchildren(tgt_node, dev, new_lvs):
3753         for new_lv in new_lvs:
3754           if not rpc.call_blockdev_remove(tgt_node, new_lv):
3755             warning("Can't rollback device %s", hint="manually cleanup unused"
3756                     " logical volumes")
3757         raise errors.OpExecError("Can't add local storage to drbd")
3758
3759       dev.children = new_lvs
3760       cfg.Update(instance)
3761
3762     # Step: wait for sync
3763
3764     # this can fail as the old devices are degraded and _WaitForSync
3765     # does a combined result over all disks, so we don't check its
3766     # return value
3767     self.proc.LogStep(5, steps_total, "sync devices")
3768     _WaitForSync(cfg, instance, self.proc, unlock=True)
3769
3770     # so check manually all the devices
3771     for name, (dev, old_lvs, new_lvs) in iv_names.iteritems():
3772       cfg.SetDiskID(dev, instance.primary_node)
3773       is_degr = rpc.call_blockdev_find(instance.primary_node, dev)[5]
3774       if is_degr:
3775         raise errors.OpExecError("DRBD device %s is degraded!" % name)
3776
3777     # Step: remove old storage
3778     self.proc.LogStep(6, steps_total, "removing old storage")
3779     for name, (dev, old_lvs, new_lvs) in iv_names.iteritems():
3780       info("remove logical volumes for %s" % name)
3781       for lv in old_lvs:
3782         cfg.SetDiskID(lv, tgt_node)
3783         if not rpc.call_blockdev_remove(tgt_node, lv):
3784           warning("Can't remove old LV", hint="manually remove unused LVs")
3785           continue
3786
3787   def _ExecD8Secondary(self, feedback_fn):
3788     """Replace the secondary node for drbd8.
3789
3790     The algorithm for replace is quite complicated:
3791       - for all disks of the instance:
3792         - create new LVs on the new node with same names
3793         - shutdown the drbd device on the old secondary
3794         - disconnect the drbd network on the primary
3795         - create the drbd device on the new secondary
3796         - network attach the drbd on the primary, using an artifice:
3797           the drbd code for Attach() will connect to the network if it
3798           finds a device which is connected to the good local disks but
3799           not network enabled
3800       - wait for sync across all devices
3801       - remove all disks from the old secondary
3802
3803     Failures are not very well handled.
3804
3805     """
3806     steps_total = 6
3807     warning, info = (self.proc.LogWarning, self.proc.LogInfo)
3808     instance = self.instance
3809     iv_names = {}
3810     vgname = self.cfg.GetVGName()
3811     # start of work
3812     cfg = self.cfg
3813     old_node = self.tgt_node
3814     new_node = self.new_node
3815     pri_node = instance.primary_node
3816
3817     # Step: check device activation
3818     self.proc.LogStep(1, steps_total, "check device existence")
3819     info("checking volume groups")
3820     my_vg = cfg.GetVGName()
3821     results = rpc.call_vg_list([pri_node, new_node])
3822     if not results:
3823       raise errors.OpExecError("Can't list volume groups on the nodes")
3824     for node in pri_node, new_node:
3825       res = results.get(node, False)
3826       if not res or my_vg not in res:
3827         raise errors.OpExecError("Volume group '%s' not found on %s" %
3828                                  (my_vg, node))
3829     for dev in instance.disks:
3830       if not dev.iv_name in self.op.disks:
3831         continue
3832       info("checking %s on %s" % (dev.iv_name, pri_node))
3833       cfg.SetDiskID(dev, pri_node)
3834       if not rpc.call_blockdev_find(pri_node, dev):
3835         raise errors.OpExecError("Can't find device %s on node %s" %
3836                                  (dev.iv_name, pri_node))
3837
3838     # Step: check other node consistency
3839     self.proc.LogStep(2, steps_total, "check peer consistency")
3840     for dev in instance.disks:
3841       if not dev.iv_name in self.op.disks:
3842         continue
3843       info("checking %s consistency on %s" % (dev.iv_name, pri_node))
3844       if not _CheckDiskConsistency(self.cfg, dev, pri_node, True, ldisk=True):
3845         raise errors.OpExecError("Primary node (%s) has degraded storage,"
3846                                  " unsafe to replace the secondary" %
3847                                  pri_node)
3848
3849     # Step: create new storage
3850     self.proc.LogStep(3, steps_total, "allocate new storage")
3851     for dev in instance.disks:
3852       size = dev.size
3853       info("adding new local storage on %s for %s" % (new_node, dev.iv_name))
3854       # since we *always* want to create this LV, we use the
3855       # _Create...OnPrimary (which forces the creation), even if we
3856       # are talking about the secondary node
3857       for new_lv in dev.children:
3858         if not _CreateBlockDevOnPrimary(cfg, new_node, instance, new_lv,
3859                                         _GetInstanceInfoText(instance)):
3860           raise errors.OpExecError("Failed to create new LV named '%s' on"
3861                                    " node '%s'" %
3862                                    (new_lv.logical_id[1], new_node))
3863
3864       iv_names[dev.iv_name] = (dev, dev.children)
3865
3866     self.proc.LogStep(4, steps_total, "changing drbd configuration")
3867     for dev in instance.disks:
3868       size = dev.size
3869       info("activating a new drbd on %s for %s" % (new_node, dev.iv_name))
3870       # create new devices on new_node
3871       new_drbd = objects.Disk(dev_type=constants.LD_DRBD8,
3872                               logical_id=(pri_node, new_node,
3873                                           dev.logical_id[2]),
3874                               children=dev.children)
3875       if not _CreateBlockDevOnSecondary(cfg, new_node, instance,
3876                                         new_drbd, False,
3877                                       _GetInstanceInfoText(instance)):
3878         raise errors.OpExecError("Failed to create new DRBD on"
3879                                  " node '%s'" % new_node)
3880
3881     for dev in instance.disks:
3882       # we have new devices, shutdown the drbd on the old secondary
3883       info("shutting down drbd for %s on old node" % dev.iv_name)
3884       cfg.SetDiskID(dev, old_node)
3885       if not rpc.call_blockdev_shutdown(old_node, dev):
3886         warning("Failed to shutdown drbd for %s on old node" % dev.iv_name,
3887                 hint="Please cleanup this device manually as soon as possible")
3888
3889     info("detaching primary drbds from the network (=> standalone)")
3890     done = 0
3891     for dev in instance.disks:
3892       cfg.SetDiskID(dev, pri_node)
3893       # set the physical (unique in bdev terms) id to None, meaning
3894       # detach from network
3895       dev.physical_id = (None,) * len(dev.physical_id)
3896       # and 'find' the device, which will 'fix' it to match the
3897       # standalone state
3898       if rpc.call_blockdev_find(pri_node, dev):
3899         done += 1
3900       else:
3901         warning("Failed to detach drbd %s from network, unusual case" %
3902                 dev.iv_name)
3903
3904     if not done:
3905       # no detaches succeeded (very unlikely)
3906       raise errors.OpExecError("Can't detach at least one DRBD from old node")
3907
3908     # if we managed to detach at least one, we update all the disks of
3909     # the instance to point to the new secondary
3910     info("updating instance configuration")
3911     for dev in instance.disks:
3912       dev.logical_id = (pri_node, new_node) + dev.logical_id[2:]
3913       cfg.SetDiskID(dev, pri_node)
3914     cfg.Update(instance)
3915
3916     # and now perform the drbd attach
3917     info("attaching primary drbds to new secondary (standalone => connected)")
3918     failures = []
3919     for dev in instance.disks:
3920       info("attaching primary drbd for %s to new secondary node" % dev.iv_name)
3921       # since the attach is smart, it's enough to 'find' the device,
3922       # it will automatically activate the network, if the physical_id
3923       # is correct
3924       cfg.SetDiskID(dev, pri_node)
3925       if not rpc.call_blockdev_find(pri_node, dev):
3926         warning("can't attach drbd %s to new secondary!" % dev.iv_name,
3927                 "please do a gnt-instance info to see the status of disks")
3928
3929     # this can fail as the old devices are degraded and _WaitForSync
3930     # does a combined result over all disks, so we don't check its
3931     # return value
3932     self.proc.LogStep(5, steps_total, "sync devices")
3933     _WaitForSync(cfg, instance, self.proc, unlock=True)
3934
3935     # so check manually all the devices
3936     for name, (dev, old_lvs) in iv_names.iteritems():
3937       cfg.SetDiskID(dev, pri_node)
3938       is_degr = rpc.call_blockdev_find(pri_node, dev)[5]
3939       if is_degr:
3940         raise errors.OpExecError("DRBD device %s is degraded!" % name)
3941
3942     self.proc.LogStep(6, steps_total, "removing old storage")
3943     for name, (dev, old_lvs) in iv_names.iteritems():
3944       info("remove logical volumes for %s" % name)
3945       for lv in old_lvs:
3946         cfg.SetDiskID(lv, old_node)
3947         if not rpc.call_blockdev_remove(old_node, lv):
3948           warning("Can't remove LV on old secondary",
3949                   hint="Cleanup stale volumes by hand")
3950
3951   def Exec(self, feedback_fn):
3952     """Execute disk replacement.
3953
3954     This dispatches the disk replacement to the appropriate handler.
3955
3956     """
3957     instance = self.instance
3958     if instance.disk_template == constants.DT_REMOTE_RAID1:
3959       fn = self._ExecRR1
3960     elif instance.disk_template == constants.DT_DRBD8:
3961       if self.op.remote_node is None:
3962         fn = self._ExecD8DiskOnly
3963       else:
3964         fn = self._ExecD8Secondary
3965     else:
3966       raise errors.ProgrammerError("Unhandled disk replacement case")
3967     return fn(feedback_fn)
3968
3969
3970 class LUQueryInstanceData(NoHooksLU):
3971   """Query runtime instance data.
3972
3973   """
3974   _OP_REQP = ["instances"]
3975
3976   def CheckPrereq(self):
3977     """Check prerequisites.
3978
3979     This only checks the optional instance list against the existing names.
3980
3981     """
3982     if not isinstance(self.op.instances, list):
3983       raise errors.OpPrereqError("Invalid argument type 'instances'")
3984     if self.op.instances:
3985       self.wanted_instances = []
3986       names = self.op.instances
3987       for name in names:
3988         instance = self.cfg.GetInstanceInfo(self.cfg.ExpandInstanceName(name))
3989         if instance is None:
3990           raise errors.OpPrereqError("No such instance name '%s'" % name)
3991       self.wanted_instances.append(instance)
3992     else:
3993       self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name
3994                                in self.cfg.GetInstanceList()]
3995     return
3996
3997
3998   def _ComputeDiskStatus(self, instance, snode, dev):
3999     """Compute block device status.
4000
4001     """
4002     self.cfg.SetDiskID(dev, instance.primary_node)
4003     dev_pstatus = rpc.call_blockdev_find(instance.primary_node, dev)
4004     if dev.dev_type in constants.LDS_DRBD:
4005       # we change the snode then (otherwise we use the one passed in)
4006       if dev.logical_id[0] == instance.primary_node:
4007         snode = dev.logical_id[1]
4008       else:
4009         snode = dev.logical_id[0]
4010
4011     if snode:
4012       self.cfg.SetDiskID(dev, snode)
4013       dev_sstatus = rpc.call_blockdev_find(snode, dev)
4014     else:
4015       dev_sstatus = None
4016
4017     if dev.children:
4018       dev_children = [self._ComputeDiskStatus(instance, snode, child)
4019                       for child in dev.children]
4020     else:
4021       dev_children = []
4022
4023     data = {
4024       "iv_name": dev.iv_name,
4025       "dev_type": dev.dev_type,
4026       "logical_id": dev.logical_id,
4027       "physical_id": dev.physical_id,
4028       "pstatus": dev_pstatus,
4029       "sstatus": dev_sstatus,
4030       "children": dev_children,
4031       }
4032
4033     return data
4034
4035   def Exec(self, feedback_fn):
4036     """Gather and return data"""
4037     result = {}
4038     for instance in self.wanted_instances:
4039       remote_info = rpc.call_instance_info(instance.primary_node,
4040                                                 instance.name)
4041       if remote_info and "state" in remote_info:
4042         remote_state = "up"
4043       else:
4044         remote_state = "down"
4045       if instance.status == "down":
4046         config_state = "down"
4047       else:
4048         config_state = "up"
4049
4050       disks = [self._ComputeDiskStatus(instance, None, device)
4051                for device in instance.disks]
4052
4053       idict = {
4054         "name": instance.name,
4055         "config_state": config_state,
4056         "run_state": remote_state,
4057         "pnode": instance.primary_node,
4058         "snodes": instance.secondary_nodes,
4059         "os": instance.os,
4060         "memory": instance.memory,
4061         "nics": [(nic.mac, nic.ip, nic.bridge) for nic in instance.nics],
4062         "disks": disks,
4063         "network_port": instance.network_port,
4064         "vcpus": instance.vcpus,
4065         "kernel_path": instance.kernel_path,
4066         "initrd_path": instance.initrd_path,
4067         "hvm_boot_order": instance.hvm_boot_order,
4068         }
4069
4070       result[instance.name] = idict
4071
4072     return result
4073
4074
4075 class LUSetInstanceParms(LogicalUnit):
4076   """Modifies an instances's parameters.
4077
4078   """
4079   HPATH = "instance-modify"
4080   HTYPE = constants.HTYPE_INSTANCE
4081   _OP_REQP = ["instance_name"]
4082
4083   def BuildHooksEnv(self):
4084     """Build hooks env.
4085
4086     This runs on the master, primary and secondaries.
4087
4088     """
4089     args = dict()
4090     if self.mem:
4091       args['memory'] = self.mem
4092     if self.vcpus:
4093       args['vcpus'] = self.vcpus
4094     if self.do_ip or self.do_bridge:
4095       if self.do_ip:
4096         ip = self.ip
4097       else:
4098         ip = self.instance.nics[0].ip
4099       if self.bridge:
4100         bridge = self.bridge
4101       else:
4102         bridge = self.instance.nics[0].bridge
4103       args['nics'] = [(ip, bridge)]
4104     env = _BuildInstanceHookEnvByObject(self.instance, override=args)
4105     nl = [self.sstore.GetMasterNode(),
4106           self.instance.primary_node] + list(self.instance.secondary_nodes)
4107     return env, nl, nl
4108
4109   def CheckPrereq(self):
4110     """Check prerequisites.
4111
4112     This only checks the instance list against the existing names.
4113
4114     """
4115     self.mem = getattr(self.op, "mem", None)
4116     self.vcpus = getattr(self.op, "vcpus", None)
4117     self.ip = getattr(self.op, "ip", None)
4118     self.mac = getattr(self.op, "mac", None)
4119     self.bridge = getattr(self.op, "bridge", None)
4120     self.kernel_path = getattr(self.op, "kernel_path", None)
4121     self.initrd_path = getattr(self.op, "initrd_path", None)
4122     self.hvm_boot_order = getattr(self.op, "hvm_boot_order", None)
4123     all_parms = [self.mem, self.vcpus, self.ip, self.bridge, self.mac,
4124                  self.kernel_path, self.initrd_path, self.hvm_boot_order]
4125     if all_parms.count(None) == len(all_parms):
4126       raise errors.OpPrereqError("No changes submitted")
4127     if self.mem is not None:
4128       try:
4129         self.mem = int(self.mem)
4130       except ValueError, err:
4131         raise errors.OpPrereqError("Invalid memory size: %s" % str(err))
4132     if self.vcpus is not None:
4133       try:
4134         self.vcpus = int(self.vcpus)
4135       except ValueError, err:
4136         raise errors.OpPrereqError("Invalid vcpus number: %s" % str(err))
4137     if self.ip is not None:
4138       self.do_ip = True
4139       if self.ip.lower() == "none":
4140         self.ip = None
4141       else:
4142         if not utils.IsValidIP(self.ip):
4143           raise errors.OpPrereqError("Invalid IP address '%s'." % self.ip)
4144     else:
4145       self.do_ip = False
4146     self.do_bridge = (self.bridge is not None)
4147     if self.mac is not None:
4148       if self.cfg.IsMacInUse(self.mac):
4149         raise errors.OpPrereqError('MAC address %s already in use in cluster' %
4150                                    self.mac)
4151       if not utils.IsValidMac(self.mac):
4152         raise errors.OpPrereqError('Invalid MAC address %s' % self.mac)
4153
4154     if self.kernel_path is not None:
4155       self.do_kernel_path = True
4156       if self.kernel_path == constants.VALUE_NONE:
4157         raise errors.OpPrereqError("Can't set instance to no kernel")
4158
4159       if self.kernel_path != constants.VALUE_DEFAULT:
4160         if not os.path.isabs(self.kernel_path):
4161           raise errors.OpPrereqError("The kernel path must be an absolute"
4162                                     " filename")
4163     else:
4164       self.do_kernel_path = False
4165
4166     if self.initrd_path is not None:
4167       self.do_initrd_path = True
4168       if self.initrd_path not in (constants.VALUE_NONE,
4169                                   constants.VALUE_DEFAULT):
4170         if not os.path.isabs(self.initrd_path):
4171           raise errors.OpPrereqError("The initrd path must be an absolute"
4172                                     " filename")
4173     else:
4174       self.do_initrd_path = False
4175
4176     # boot order verification
4177     if self.hvm_boot_order is not None:
4178       if self.hvm_boot_order != constants.VALUE_DEFAULT:
4179         if len(self.hvm_boot_order.strip("acdn")) != 0:
4180           raise errors.OpPrereqError("invalid boot order specified,"
4181                                      " must be one or more of [acdn]"
4182                                      " or 'default'")
4183
4184     instance = self.cfg.GetInstanceInfo(
4185       self.cfg.ExpandInstanceName(self.op.instance_name))
4186     if instance is None:
4187       raise errors.OpPrereqError("No such instance name '%s'" %
4188                                  self.op.instance_name)
4189     self.op.instance_name = instance.name
4190     self.instance = instance
4191     return
4192
4193   def Exec(self, feedback_fn):
4194     """Modifies an instance.
4195
4196     All parameters take effect only at the next restart of the instance.
4197     """
4198     result = []
4199     instance = self.instance
4200     if self.mem:
4201       instance.memory = self.mem
4202       result.append(("mem", self.mem))
4203     if self.vcpus:
4204       instance.vcpus = self.vcpus
4205       result.append(("vcpus",  self.vcpus))
4206     if self.do_ip:
4207       instance.nics[0].ip = self.ip
4208       result.append(("ip", self.ip))
4209     if self.bridge:
4210       instance.nics[0].bridge = self.bridge
4211       result.append(("bridge", self.bridge))
4212     if self.mac:
4213       instance.nics[0].mac = self.mac
4214       result.append(("mac", self.mac))
4215     if self.do_kernel_path:
4216       instance.kernel_path = self.kernel_path
4217       result.append(("kernel_path", self.kernel_path))
4218     if self.do_initrd_path:
4219       instance.initrd_path = self.initrd_path
4220       result.append(("initrd_path", self.initrd_path))
4221     if self.hvm_boot_order:
4222       if self.hvm_boot_order == constants.VALUE_DEFAULT:
4223         instance.hvm_boot_order = None
4224       else:
4225         instance.hvm_boot_order = self.hvm_boot_order
4226       result.append(("hvm_boot_order", self.hvm_boot_order))
4227
4228     self.cfg.AddInstance(instance)
4229
4230     return result
4231
4232
4233 class LUQueryExports(NoHooksLU):
4234   """Query the exports list
4235
4236   """
4237   _OP_REQP = []
4238
4239   def CheckPrereq(self):
4240     """Check that the nodelist contains only existing nodes.
4241
4242     """
4243     self.nodes = _GetWantedNodes(self, getattr(self.op, "nodes", None))
4244
4245   def Exec(self, feedback_fn):
4246     """Compute the list of all the exported system images.
4247
4248     Returns:
4249       a dictionary with the structure node->(export-list)
4250       where export-list is a list of the instances exported on
4251       that node.
4252
4253     """
4254     return rpc.call_export_list(self.nodes)
4255
4256
4257 class LUExportInstance(LogicalUnit):
4258   """Export an instance to an image in the cluster.
4259
4260   """
4261   HPATH = "instance-export"
4262   HTYPE = constants.HTYPE_INSTANCE
4263   _OP_REQP = ["instance_name", "target_node", "shutdown"]
4264
4265   def BuildHooksEnv(self):
4266     """Build hooks env.
4267
4268     This will run on the master, primary node and target node.
4269
4270     """
4271     env = {
4272       "EXPORT_NODE": self.op.target_node,
4273       "EXPORT_DO_SHUTDOWN": self.op.shutdown,
4274       }
4275     env.update(_BuildInstanceHookEnvByObject(self.instance))
4276     nl = [self.sstore.GetMasterNode(), self.instance.primary_node,
4277           self.op.target_node]
4278     return env, nl, nl
4279
4280   def CheckPrereq(self):
4281     """Check prerequisites.
4282
4283     This checks that the instance name is a valid one.
4284
4285     """
4286     instance_name = self.cfg.ExpandInstanceName(self.op.instance_name)
4287     self.instance = self.cfg.GetInstanceInfo(instance_name)
4288     if self.instance is None:
4289       raise errors.OpPrereqError("Instance '%s' not found" %
4290                                  self.op.instance_name)
4291
4292     # node verification
4293     dst_node_short = self.cfg.ExpandNodeName(self.op.target_node)
4294     self.dst_node = self.cfg.GetNodeInfo(dst_node_short)
4295
4296     if self.dst_node is None:
4297       raise errors.OpPrereqError("Destination node '%s' is unknown." %
4298                                  self.op.target_node)
4299     self.op.target_node = self.dst_node.name
4300
4301   def Exec(self, feedback_fn):
4302     """Export an instance to an image in the cluster.
4303
4304     """
4305     instance = self.instance
4306     dst_node = self.dst_node
4307     src_node = instance.primary_node
4308     # shutdown the instance, unless requested not to do so
4309     if self.op.shutdown:
4310       op = opcodes.OpShutdownInstance(instance_name=instance.name)
4311       self.proc.ChainOpCode(op)
4312
4313     vgname = self.cfg.GetVGName()
4314
4315     snap_disks = []
4316
4317     try:
4318       for disk in instance.disks:
4319         if disk.iv_name == "sda":
4320           # new_dev_name will be a snapshot of an lvm leaf of the one we passed
4321           new_dev_name = rpc.call_blockdev_snapshot(src_node, disk)
4322
4323           if not new_dev_name:
4324             logger.Error("could not snapshot block device %s on node %s" %
4325                          (disk.logical_id[1], src_node))
4326           else:
4327             new_dev = objects.Disk(dev_type=constants.LD_LV, size=disk.size,
4328                                       logical_id=(vgname, new_dev_name),
4329                                       physical_id=(vgname, new_dev_name),
4330                                       iv_name=disk.iv_name)
4331             snap_disks.append(new_dev)
4332
4333     finally:
4334       if self.op.shutdown:
4335         op = opcodes.OpStartupInstance(instance_name=instance.name,
4336                                        force=False)
4337         self.proc.ChainOpCode(op)
4338
4339     # TODO: check for size
4340
4341     for dev in snap_disks:
4342       if not rpc.call_snapshot_export(src_node, dev, dst_node.name,
4343                                            instance):
4344         logger.Error("could not export block device %s from node"
4345                      " %s to node %s" %
4346                      (dev.logical_id[1], src_node, dst_node.name))
4347       if not rpc.call_blockdev_remove(src_node, dev):
4348         logger.Error("could not remove snapshot block device %s from"
4349                      " node %s" % (dev.logical_id[1], src_node))
4350
4351     if not rpc.call_finalize_export(dst_node.name, instance, snap_disks):
4352       logger.Error("could not finalize export for instance %s on node %s" %
4353                    (instance.name, dst_node.name))
4354
4355     nodelist = self.cfg.GetNodeList()
4356     nodelist.remove(dst_node.name)
4357
4358     # on one-node clusters nodelist will be empty after the removal
4359     # if we proceed the backup would be removed because OpQueryExports
4360     # substitutes an empty list with the full cluster node list.
4361     if nodelist:
4362       op = opcodes.OpQueryExports(nodes=nodelist)
4363       exportlist = self.proc.ChainOpCode(op)
4364       for node in exportlist:
4365         if instance.name in exportlist[node]:
4366           if not rpc.call_export_remove(node, instance.name):
4367             logger.Error("could not remove older export for instance %s"
4368                          " on node %s" % (instance.name, node))
4369
4370
4371 class TagsLU(NoHooksLU):
4372   """Generic tags LU.
4373
4374   This is an abstract class which is the parent of all the other tags LUs.
4375
4376   """
4377   def CheckPrereq(self):
4378     """Check prerequisites.
4379
4380     """
4381     if self.op.kind == constants.TAG_CLUSTER:
4382       self.target = self.cfg.GetClusterInfo()
4383     elif self.op.kind == constants.TAG_NODE:
4384       name = self.cfg.ExpandNodeName(self.op.name)
4385       if name is None:
4386         raise errors.OpPrereqError("Invalid node name (%s)" %
4387                                    (self.op.name,))
4388       self.op.name = name
4389       self.target = self.cfg.GetNodeInfo(name)
4390     elif self.op.kind == constants.TAG_INSTANCE:
4391       name = self.cfg.ExpandInstanceName(self.op.name)
4392       if name is None:
4393         raise errors.OpPrereqError("Invalid instance name (%s)" %
4394                                    (self.op.name,))
4395       self.op.name = name
4396       self.target = self.cfg.GetInstanceInfo(name)
4397     else:
4398       raise errors.OpPrereqError("Wrong tag type requested (%s)" %
4399                                  str(self.op.kind))
4400
4401
4402 class LUGetTags(TagsLU):
4403   """Returns the tags of a given object.
4404
4405   """
4406   _OP_REQP = ["kind", "name"]
4407
4408   def Exec(self, feedback_fn):
4409     """Returns the tag list.
4410
4411     """
4412     return self.target.GetTags()
4413
4414
4415 class LUSearchTags(NoHooksLU):
4416   """Searches the tags for a given pattern.
4417
4418   """
4419   _OP_REQP = ["pattern"]
4420
4421   def CheckPrereq(self):
4422     """Check prerequisites.
4423
4424     This checks the pattern passed for validity by compiling it.
4425
4426     """
4427     try:
4428       self.re = re.compile(self.op.pattern)
4429     except re.error, err:
4430       raise errors.OpPrereqError("Invalid search pattern '%s': %s" %
4431                                  (self.op.pattern, err))
4432
4433   def Exec(self, feedback_fn):
4434     """Returns the tag list.
4435
4436     """
4437     cfg = self.cfg
4438     tgts = [("/cluster", cfg.GetClusterInfo())]
4439     ilist = [cfg.GetInstanceInfo(name) for name in cfg.GetInstanceList()]
4440     tgts.extend([("/instances/%s" % i.name, i) for i in ilist])
4441     nlist = [cfg.GetNodeInfo(name) for name in cfg.GetNodeList()]
4442     tgts.extend([("/nodes/%s" % n.name, n) for n in nlist])
4443     results = []
4444     for path, target in tgts:
4445       for tag in target.GetTags():
4446         if self.re.search(tag):
4447           results.append((path, tag))
4448     return results
4449
4450
4451 class LUAddTags(TagsLU):
4452   """Sets a tag on a given object.
4453
4454   """
4455   _OP_REQP = ["kind", "name", "tags"]
4456
4457   def CheckPrereq(self):
4458     """Check prerequisites.
4459
4460     This checks the type and length of the tag name and value.
4461
4462     """
4463     TagsLU.CheckPrereq(self)
4464     for tag in self.op.tags:
4465       objects.TaggableObject.ValidateTag(tag)
4466
4467   def Exec(self, feedback_fn):
4468     """Sets the tag.
4469
4470     """
4471     try:
4472       for tag in self.op.tags:
4473         self.target.AddTag(tag)
4474     except errors.TagError, err:
4475       raise errors.OpExecError("Error while setting tag: %s" % str(err))
4476     try:
4477       self.cfg.Update(self.target)
4478     except errors.ConfigurationError:
4479       raise errors.OpRetryError("There has been a modification to the"
4480                                 " config file and the operation has been"
4481                                 " aborted. Please retry.")
4482
4483
4484 class LUDelTags(TagsLU):
4485   """Delete a list of tags from a given object.
4486
4487   """
4488   _OP_REQP = ["kind", "name", "tags"]
4489
4490   def CheckPrereq(self):
4491     """Check prerequisites.
4492
4493     This checks that we have the given tag.
4494
4495     """
4496     TagsLU.CheckPrereq(self)
4497     for tag in self.op.tags:
4498       objects.TaggableObject.ValidateTag(tag)
4499     del_tags = frozenset(self.op.tags)
4500     cur_tags = self.target.GetTags()
4501     if not del_tags <= cur_tags:
4502       diff_tags = del_tags - cur_tags
4503       diff_names = ["'%s'" % tag for tag in diff_tags]
4504       diff_names.sort()
4505       raise errors.OpPrereqError("Tag(s) %s not found" %
4506                                  (",".join(diff_names)))
4507
4508   def Exec(self, feedback_fn):
4509     """Remove the tag from the object.
4510
4511     """
4512     for tag in self.op.tags:
4513       self.target.RemoveTag(tag)
4514     try:
4515       self.cfg.Update(self.target)
4516     except errors.ConfigurationError:
4517       raise errors.OpRetryError("There has been a modification to the"
4518                                 " config file and the operation has been"
4519                                 " aborted. Please retry.")