Revision 396e1b78 lib/cmdlib.py

b/lib/cmdlib.py
45 45
from ganeti import ssconf
46 46

  
47 47
class LogicalUnit(object):
48
  """Logical Unit base class..
48
  """Logical Unit base class.
49 49

  
50 50
  Subclasses must follow these rules:
51 51
    - implement CheckPrereq which also fills in the opcode instance
......
207 207
                                            difference(all_fields)))
208 208

  
209 209

  
210
def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os, status,
211
                          memory, vcpus, nics):
212
  """
213
  """
214
  env = {
215
    "INSTANCE_NAME": name,
216
    "INSTANCE_PRIMARY": primary_node,
217
    "INSTANCE_SECONDARIES": " ".join(secondary_nodes),
218
    "INSTANCE_OS": os,
219
    "INSTANCE_STATUS": status,
220
    "INSTANCE_MEMORY": memory,
221
    "INSTANCE_VCPUS": vcpus,
222
  }
223

  
224
  if nics:
225
    nic_count = len(nics)
226
    for idx, (ip, bridge) in enumerate(nics):
227
      if ip is None:
228
        ip = ""
229
      env["INSTANCE_NIC%d_IP" % idx] = ip
230
      env["INSTANCE_NIC%d_BRIDGE" % idx] = bridge
231
  else:
232
    nic_count = 0
233

  
234
  env["INSTANCE_NIC_COUNT"] = nic_count
235

  
236
  return env
237

  
238

  
239
def _BuildInstanceHookEnvByObject(instance, override=None):
240
  args = {
241
    'name': instance.name,
242
    'primary_node': instance.primary_node,
243
    'secondary_nodes': instance.secondary_nodes,
244
    'os': instance.os,
245
    'status': instance.os,
246
    'memory': instance.memory,
247
    'vcpus': instance.vcpus,
248
    'nics': [(nic.ip, nic.bridge) for nic in instance.nics],
249
  }
250
  if override:
251
    args.update(override)
252
  return _BuildInstanceHookEnv(**args)
253

  
254

  
210 255
def _UpdateEtcHosts(fullnode, ip):
211 256
  """Ensure a node has a correct entry in /etc/hosts.
212 257

  
......
466 511
    ourselves in the post-run node list.
467 512

  
468 513
    """
469
    env = {"CLUSTER": self.op.cluster_name,
470
           "MASTER": self.hostname['hostname_full']}
514
    env = {
515
      "CLUSTER": self.op.cluster_name,
516
      "MASTER": self.hostname['hostname_full'],
517
      }
471 518
    return env, [], [self.hostname['hostname_full']]
472 519

  
473 520
  def CheckPrereq(self):
......
975 1022
    node would not allows itself to run.
976 1023

  
977 1024
    """
1025
    env = {
1026
      "NODE_NAME": self.op.node_name,
1027
      }
978 1028
    all_nodes = self.cfg.GetNodeList()
979 1029
    all_nodes.remove(self.op.node_name)
980
    return {"NODE_NAME": self.op.node_name}, all_nodes, all_nodes
1030
    return env, all_nodes, all_nodes
981 1031

  
982 1032
  def CheckPrereq(self):
983 1033
    """Check prerequisites.
......
1733 1783

  
1734 1784
    """
1735 1785
    env = {
1736
      "INSTANCE_NAME": self.op.instance_name,
1737
      "INSTANCE_PRIMARY": self.instance.primary_node,
1738
      "INSTANCE_SECONDARIES": " ".join(self.instance.secondary_nodes),
1739 1786
      "FORCE": self.op.force,
1740 1787
      }
1788
    env.update(_BuildInstanceHookEnvByObject(self.instance))
1741 1789
    nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] +
1742 1790
          list(self.instance.secondary_nodes))
1743 1791
    return env, nl, nl
......
1811 1859
    This runs on master, primary and secondary nodes of the instance.
1812 1860

  
1813 1861
    """
1814
    env = {
1815
      "INSTANCE_NAME": self.op.instance_name,
1816
      "INSTANCE_PRIMARY": self.instance.primary_node,
1817
      "INSTANCE_SECONDARIES": " ".join(self.instance.secondary_nodes),
1818
      }
1862
    env = _BuildInstanceHookEnvByObject(self.instance)
1819 1863
    nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] +
1820 1864
          list(self.instance.secondary_nodes))
1821 1865
    return env, nl, nl
......
1860 1904
    This runs on master, primary and secondary nodes of the instance.
1861 1905

  
1862 1906
    """
1863
    env = {
1864
      "INSTANCE_NAME": self.op.instance_name,
1865
      "INSTANCE_PRIMARY": self.instance.primary_node,
1866
      "INSTANCE_SECONDARIES": " ".join(self.instance.secondary_nodes),
1867
      }
1907
    env = _BuildInstanceHookEnvByObject(self.instance)
1868 1908
    nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] +
1869 1909
          list(self.instance.secondary_nodes))
1870 1910
    return env, nl, nl
......
1943 1983
    This runs on master, primary and secondary nodes of the instance.
1944 1984

  
1945 1985
    """
1946
    env = {
1947
      "INSTANCE_NAME": self.op.instance_name,
1948
      "INSTANCE_PRIMARY": self.instance.primary_node,
1949
      "INSTANCE_SECONDARIES": " ".join(self.instance.secondary_nodes),
1950
      }
1986
    env = _BuildInstanceHookEnvByObject(self.instance)
1951 1987
    nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] +
1952 1988
          list(self.instance.secondary_nodes))
1953 1989
    return env, nl, nl
......
2099 2135

  
2100 2136
    """
2101 2137
    env = {
2102
      "INSTANCE_NAME": self.op.instance_name,
2103
      "INSTANCE_PRIMARY": self.instance.primary_node,
2104
      "INSTANCE_SECONDARIES": " ".join(self.instance.secondary_nodes),
2105 2138
      "IGNORE_CONSISTENCY": self.op.ignore_consistency,
2106 2139
      }
2140
    env.update(_BuildInstanceHookEnvByObject(self.instance))
2107 2141
    nl = [self.sstore.GetMasterNode()] + list(self.instance.secondary_nodes)
2108 2142
    return env, nl, nl
2109 2143

  
......
2408 2442

  
2409 2443
    """
2410 2444
    env = {
2411
      "INSTANCE_NAME": self.op.instance_name,
2412
      "INSTANCE_PRIMARY": self.op.pnode,
2413
      "INSTANCE_SECONDARIES": " ".join(self.secondaries),
2414
      "DISK_TEMPLATE": self.op.disk_template,
2415
      "MEM_SIZE": self.op.mem_size,
2416
      "DISK_SIZE": self.op.disk_size,
2417
      "SWAP_SIZE": self.op.swap_size,
2418
      "VCPUS": self.op.vcpus,
2419
      "BRIDGE": self.op.bridge,
2445
      "INSTANCE_DISK_TEMPLATE": self.op.disk_template,
2446
      "INSTANCE_DISK_SIZE": self.op.disk_size,
2447
      "INSTANCE_SWAP_SIZE": self.op.swap_size,
2420 2448
      "INSTANCE_ADD_MODE": self.op.mode,
2421 2449
      }
2422 2450
    if self.op.mode == constants.INSTANCE_IMPORT:
2423
      env["SRC_NODE"] = self.op.src_node
2424
      env["SRC_PATH"] = self.op.src_path
2425
      env["SRC_IMAGE"] = self.src_image
2426
    if self.inst_ip:
2427
      env["INSTANCE_IP"] = self.inst_ip
2451
      env["INSTANCE_SRC_NODE"] = self.op.src_node
2452
      env["INSTANCE_SRC_PATH"] = self.op.src_path
2453
      env["INSTANCE_SRC_IMAGE"] = self.src_image
2454

  
2455
    env.update(_BuildInstanceHookEnv(name=self.op.instance_name,
2456
      primary_node=self.op.pnode,
2457
      secondary_nodes=self.secondaries,
2458
      status=self.instance_status,
2459
      os=self.op.os_type,
2460
      memory=self.op.mem_size,
2461
      vcpus=self.op.vcpus,
2462
      nics=[(self.inst_ip, self.op.bridge)],
2463
    ))
2428 2464

  
2429 2465
    nl = ([self.sstore.GetMasterNode(), self.op.pnode] +
2430 2466
          self.secondaries)
......
2730 2766

  
2731 2767
    """
2732 2768
    env = {
2733
      "INSTANCE_NAME": self.op.instance_name,
2734 2769
      "NEW_SECONDARY": self.op.remote_node,
2735 2770
      "DISK_NAME": self.op.disk_name,
2736 2771
      }
2772
    env.update(_BuildInstanceHookEnvByObject(self.instance))
2737 2773
    nl = [self.sstore.GetMasterNode(), self.instance.primary_node,
2738 2774
          self.op.remote_node,] + list(self.instance.secondary_nodes)
2739 2775
    return env, nl, nl
......
2841 2877

  
2842 2878
    """
2843 2879
    env = {
2844
      "INSTANCE_NAME": self.op.instance_name,
2845 2880
      "DISK_NAME": self.op.disk_name,
2846 2881
      "DISK_ID": self.op.disk_id,
2847 2882
      "OLD_SECONDARY": self.old_secondary,
2848 2883
      }
2884
    env.update(_BuildInstanceHookEnvByObject(self.instance))
2849 2885
    nl = [self.sstore.GetMasterNode(),
2850 2886
          self.instance.primary_node] + list(self.instance.secondary_nodes)
2851 2887
    return env, nl, nl
......
2927 2963

  
2928 2964
    """
2929 2965
    env = {
2930
      "INSTANCE_NAME": self.op.instance_name,
2931 2966
      "NEW_SECONDARY": self.op.remote_node,
2932 2967
      "OLD_SECONDARY": self.instance.secondary_nodes[0],
2933 2968
      }
2969
    env.update(_BuildInstanceHookEnvByObject(self.instance))
2934 2970
    nl = [self.sstore.GetMasterNode(),
2935 2971
          self.instance.primary_node] + list(self.instance.secondary_nodes)
2936 2972
    return env, nl, nl
......
3205 3241
    This runs on the master, primary and secondaries.
3206 3242

  
3207 3243
    """
3208
    env = {
3209
      "INSTANCE_NAME": self.op.instance_name,
3210
      }
3244
    args = dict()
3211 3245
    if self.mem:
3212
      env["MEM_SIZE"] = self.mem
3246
      args['memory'] = self.mem
3213 3247
    if self.vcpus:
3214
      env["VCPUS"] = self.vcpus
3215
    if self.do_ip:
3216
      env["INSTANCE_IP"] = self.ip
3217
    if self.bridge:
3218
      env["BRIDGE"] = self.bridge
3219

  
3248
      args['vcpus'] = self.vcpus
3249
    if self.do_ip or self.do_bridge:
3250
      if self.do_ip:
3251
        ip = self.ip
3252
      else:
3253
        ip = self.instance.nics[0].ip
3254
      if self.bridge:
3255
        bridge = self.bridge
3256
      else:
3257
        bridge = self.instance.nics[0].bridge
3258
      args['nics'] = [(ip, bridge)]
3259
    env = _BuildInstanceHookEnvByObject(self.instance, override=args)
3220 3260
    nl = [self.sstore.GetMasterNode(),
3221 3261
          self.instance.primary_node] + list(self.instance.secondary_nodes)
3222

  
3223 3262
    return env, nl, nl
3224 3263

  
3225 3264
  def CheckPrereq(self):
......
3327 3366

  
3328 3367
    """
3329 3368
    env = {
3330
      "INSTANCE_NAME": self.op.instance_name,
3331 3369
      "EXPORT_NODE": self.op.target_node,
3332 3370
      "EXPORT_DO_SHUTDOWN": self.op.shutdown,
3333 3371
      }
3372
    env.update(_BuildInstanceHookEnvByObject(self.instance))
3334 3373
    nl = [self.sstore.GetMasterNode(), self.instance.primary_node,
3335 3374
          self.op.target_node]
3336 3375
    return env, nl, nl

Also available in: Unified diff