Commit 396e1b78 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

Unify environment variables for instance related hooks.

Reviewed-by: iustinp
parent db915bd1
...@@ -45,7 +45,7 @@ from ganeti import opcodes ...@@ -45,7 +45,7 @@ from ganeti import opcodes
from ganeti import ssconf from ganeti import ssconf
class LogicalUnit(object): class LogicalUnit(object):
"""Logical Unit base class.. """Logical Unit base class.
Subclasses must follow these rules: Subclasses must follow these rules:
- implement CheckPrereq which also fills in the opcode instance - implement CheckPrereq which also fills in the opcode instance
...@@ -207,6 +207,51 @@ def _CheckOutputFields(static, dynamic, selected): ...@@ -207,6 +207,51 @@ def _CheckOutputFields(static, dynamic, selected):
difference(all_fields))) difference(all_fields)))
def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os, status,
memory, vcpus, nics):
"""
"""
env = {
"INSTANCE_NAME": name,
"INSTANCE_PRIMARY": primary_node,
"INSTANCE_SECONDARIES": " ".join(secondary_nodes),
"INSTANCE_OS": os,
"INSTANCE_STATUS": status,
"INSTANCE_MEMORY": memory,
"INSTANCE_VCPUS": vcpus,
}
if nics:
nic_count = len(nics)
for idx, (ip, bridge) in enumerate(nics):
if ip is None:
ip = ""
env["INSTANCE_NIC%d_IP" % idx] = ip
env["INSTANCE_NIC%d_BRIDGE" % idx] = bridge
else:
nic_count = 0
env["INSTANCE_NIC_COUNT"] = nic_count
return env
def _BuildInstanceHookEnvByObject(instance, override=None):
args = {
'name': instance.name,
'primary_node': instance.primary_node,
'secondary_nodes': instance.secondary_nodes,
'os': instance.os,
'status': instance.os,
'memory': instance.memory,
'vcpus': instance.vcpus,
'nics': [(nic.ip, nic.bridge) for nic in instance.nics],
}
if override:
args.update(override)
return _BuildInstanceHookEnv(**args)
def _UpdateEtcHosts(fullnode, ip): def _UpdateEtcHosts(fullnode, ip):
"""Ensure a node has a correct entry in /etc/hosts. """Ensure a node has a correct entry in /etc/hosts.
...@@ -466,8 +511,10 @@ class LUInitCluster(LogicalUnit): ...@@ -466,8 +511,10 @@ class LUInitCluster(LogicalUnit):
ourselves in the post-run node list. ourselves in the post-run node list.
""" """
env = {"CLUSTER": self.op.cluster_name, env = {
"MASTER": self.hostname['hostname_full']} "CLUSTER": self.op.cluster_name,
"MASTER": self.hostname['hostname_full'],
}
return env, [], [self.hostname['hostname_full']] return env, [], [self.hostname['hostname_full']]
def CheckPrereq(self): def CheckPrereq(self):
...@@ -975,9 +1022,12 @@ class LURemoveNode(LogicalUnit): ...@@ -975,9 +1022,12 @@ class LURemoveNode(LogicalUnit):
node would not allows itself to run. node would not allows itself to run.
""" """
env = {
"NODE_NAME": self.op.node_name,
}
all_nodes = self.cfg.GetNodeList() all_nodes = self.cfg.GetNodeList()
all_nodes.remove(self.op.node_name) all_nodes.remove(self.op.node_name)
return {"NODE_NAME": self.op.node_name}, all_nodes, all_nodes return env, all_nodes, all_nodes
def CheckPrereq(self): def CheckPrereq(self):
"""Check prerequisites. """Check prerequisites.
...@@ -1733,11 +1783,9 @@ class LUStartupInstance(LogicalUnit): ...@@ -1733,11 +1783,9 @@ class LUStartupInstance(LogicalUnit):
""" """
env = { env = {
"INSTANCE_NAME": self.op.instance_name,
"INSTANCE_PRIMARY": self.instance.primary_node,
"INSTANCE_SECONDARIES": " ".join(self.instance.secondary_nodes),
"FORCE": self.op.force, "FORCE": self.op.force,
} }
env.update(_BuildInstanceHookEnvByObject(self.instance))
nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] + nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] +
list(self.instance.secondary_nodes)) list(self.instance.secondary_nodes))
return env, nl, nl return env, nl, nl
...@@ -1811,11 +1859,7 @@ class LUShutdownInstance(LogicalUnit): ...@@ -1811,11 +1859,7 @@ class LUShutdownInstance(LogicalUnit):
This runs on master, primary and secondary nodes of the instance. This runs on master, primary and secondary nodes of the instance.
""" """
env = { env = _BuildInstanceHookEnvByObject(self.instance)
"INSTANCE_NAME": self.op.instance_name,
"INSTANCE_PRIMARY": self.instance.primary_node,
"INSTANCE_SECONDARIES": " ".join(self.instance.secondary_nodes),
}
nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] + nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] +
list(self.instance.secondary_nodes)) list(self.instance.secondary_nodes))
return env, nl, nl return env, nl, nl
...@@ -1860,11 +1904,7 @@ class LUReinstallInstance(LogicalUnit): ...@@ -1860,11 +1904,7 @@ class LUReinstallInstance(LogicalUnit):
This runs on master, primary and secondary nodes of the instance. This runs on master, primary and secondary nodes of the instance.
""" """
env = { env = _BuildInstanceHookEnvByObject(self.instance)
"INSTANCE_NAME": self.op.instance_name,
"INSTANCE_PRIMARY": self.instance.primary_node,
"INSTANCE_SECONDARIES": " ".join(self.instance.secondary_nodes),
}
nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] + nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] +
list(self.instance.secondary_nodes)) list(self.instance.secondary_nodes))
return env, nl, nl return env, nl, nl
...@@ -1943,11 +1983,7 @@ class LURemoveInstance(LogicalUnit): ...@@ -1943,11 +1983,7 @@ class LURemoveInstance(LogicalUnit):
This runs on master, primary and secondary nodes of the instance. This runs on master, primary and secondary nodes of the instance.
""" """
env = { env = _BuildInstanceHookEnvByObject(self.instance)
"INSTANCE_NAME": self.op.instance_name,
"INSTANCE_PRIMARY": self.instance.primary_node,
"INSTANCE_SECONDARIES": " ".join(self.instance.secondary_nodes),
}
nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] + nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] +
list(self.instance.secondary_nodes)) list(self.instance.secondary_nodes))
return env, nl, nl return env, nl, nl
...@@ -2099,11 +2135,9 @@ class LUFailoverInstance(LogicalUnit): ...@@ -2099,11 +2135,9 @@ class LUFailoverInstance(LogicalUnit):
""" """
env = { env = {
"INSTANCE_NAME": self.op.instance_name,
"INSTANCE_PRIMARY": self.instance.primary_node,
"INSTANCE_SECONDARIES": " ".join(self.instance.secondary_nodes),
"IGNORE_CONSISTENCY": self.op.ignore_consistency, "IGNORE_CONSISTENCY": self.op.ignore_consistency,
} }
env.update(_BuildInstanceHookEnvByObject(self.instance))
nl = [self.sstore.GetMasterNode()] + list(self.instance.secondary_nodes) nl = [self.sstore.GetMasterNode()] + list(self.instance.secondary_nodes)
return env, nl, nl return env, nl, nl
...@@ -2408,23 +2442,25 @@ class LUCreateInstance(LogicalUnit): ...@@ -2408,23 +2442,25 @@ class LUCreateInstance(LogicalUnit):
""" """
env = { env = {
"INSTANCE_NAME": self.op.instance_name, "INSTANCE_DISK_TEMPLATE": self.op.disk_template,
"INSTANCE_PRIMARY": self.op.pnode, "INSTANCE_DISK_SIZE": self.op.disk_size,
"INSTANCE_SECONDARIES": " ".join(self.secondaries), "INSTANCE_SWAP_SIZE": self.op.swap_size,
"DISK_TEMPLATE": self.op.disk_template,
"MEM_SIZE": self.op.mem_size,
"DISK_SIZE": self.op.disk_size,
"SWAP_SIZE": self.op.swap_size,
"VCPUS": self.op.vcpus,
"BRIDGE": self.op.bridge,
"INSTANCE_ADD_MODE": self.op.mode, "INSTANCE_ADD_MODE": self.op.mode,
} }
if self.op.mode == constants.INSTANCE_IMPORT: if self.op.mode == constants.INSTANCE_IMPORT:
env["SRC_NODE"] = self.op.src_node env["INSTANCE_SRC_NODE"] = self.op.src_node
env["SRC_PATH"] = self.op.src_path env["INSTANCE_SRC_PATH"] = self.op.src_path
env["SRC_IMAGE"] = self.src_image env["INSTANCE_SRC_IMAGE"] = self.src_image
if self.inst_ip:
env["INSTANCE_IP"] = self.inst_ip env.update(_BuildInstanceHookEnv(name=self.op.instance_name,
primary_node=self.op.pnode,
secondary_nodes=self.secondaries,
status=self.instance_status,
os=self.op.os_type,
memory=self.op.mem_size,
vcpus=self.op.vcpus,
nics=[(self.inst_ip, self.op.bridge)],
))
nl = ([self.sstore.GetMasterNode(), self.op.pnode] + nl = ([self.sstore.GetMasterNode(), self.op.pnode] +
self.secondaries) self.secondaries)
...@@ -2730,10 +2766,10 @@ class LUAddMDDRBDComponent(LogicalUnit): ...@@ -2730,10 +2766,10 @@ class LUAddMDDRBDComponent(LogicalUnit):
""" """
env = { env = {
"INSTANCE_NAME": self.op.instance_name,
"NEW_SECONDARY": self.op.remote_node, "NEW_SECONDARY": self.op.remote_node,
"DISK_NAME": self.op.disk_name, "DISK_NAME": self.op.disk_name,
} }
env.update(_BuildInstanceHookEnvByObject(self.instance))
nl = [self.sstore.GetMasterNode(), self.instance.primary_node, nl = [self.sstore.GetMasterNode(), self.instance.primary_node,
self.op.remote_node,] + list(self.instance.secondary_nodes) self.op.remote_node,] + list(self.instance.secondary_nodes)
return env, nl, nl return env, nl, nl
...@@ -2841,11 +2877,11 @@ class LURemoveMDDRBDComponent(LogicalUnit): ...@@ -2841,11 +2877,11 @@ class LURemoveMDDRBDComponent(LogicalUnit):
""" """
env = { env = {
"INSTANCE_NAME": self.op.instance_name,
"DISK_NAME": self.op.disk_name, "DISK_NAME": self.op.disk_name,
"DISK_ID": self.op.disk_id, "DISK_ID": self.op.disk_id,
"OLD_SECONDARY": self.old_secondary, "OLD_SECONDARY": self.old_secondary,
} }
env.update(_BuildInstanceHookEnvByObject(self.instance))
nl = [self.sstore.GetMasterNode(), nl = [self.sstore.GetMasterNode(),
self.instance.primary_node] + list(self.instance.secondary_nodes) self.instance.primary_node] + list(self.instance.secondary_nodes)
return env, nl, nl return env, nl, nl
...@@ -2927,10 +2963,10 @@ class LUReplaceDisks(LogicalUnit): ...@@ -2927,10 +2963,10 @@ class LUReplaceDisks(LogicalUnit):
""" """
env = { env = {
"INSTANCE_NAME": self.op.instance_name,
"NEW_SECONDARY": self.op.remote_node, "NEW_SECONDARY": self.op.remote_node,
"OLD_SECONDARY": self.instance.secondary_nodes[0], "OLD_SECONDARY": self.instance.secondary_nodes[0],
} }
env.update(_BuildInstanceHookEnvByObject(self.instance))
nl = [self.sstore.GetMasterNode(), nl = [self.sstore.GetMasterNode(),
self.instance.primary_node] + list(self.instance.secondary_nodes) self.instance.primary_node] + list(self.instance.secondary_nodes)
return env, nl, nl return env, nl, nl
...@@ -3205,21 +3241,24 @@ class LUSetInstanceParms(LogicalUnit): ...@@ -3205,21 +3241,24 @@ class LUSetInstanceParms(LogicalUnit):
This runs on the master, primary and secondaries. This runs on the master, primary and secondaries.
""" """
env = { args = dict()
"INSTANCE_NAME": self.op.instance_name,
}
if self.mem: if self.mem:
env["MEM_SIZE"] = self.mem args['memory'] = self.mem
if self.vcpus: if self.vcpus:
env["VCPUS"] = self.vcpus args['vcpus'] = self.vcpus
if self.do_ip: if self.do_ip or self.do_bridge:
env["INSTANCE_IP"] = self.ip if self.do_ip:
if self.bridge: ip = self.ip
env["BRIDGE"] = self.bridge else:
ip = self.instance.nics[0].ip
if self.bridge:
bridge = self.bridge
else:
bridge = self.instance.nics[0].bridge
args['nics'] = [(ip, bridge)]
env = _BuildInstanceHookEnvByObject(self.instance, override=args)
nl = [self.sstore.GetMasterNode(), nl = [self.sstore.GetMasterNode(),
self.instance.primary_node] + list(self.instance.secondary_nodes) self.instance.primary_node] + list(self.instance.secondary_nodes)
return env, nl, nl return env, nl, nl
def CheckPrereq(self): def CheckPrereq(self):
...@@ -3327,10 +3366,10 @@ class LUExportInstance(LogicalUnit): ...@@ -3327,10 +3366,10 @@ class LUExportInstance(LogicalUnit):
""" """
env = { env = {
"INSTANCE_NAME": self.op.instance_name,
"EXPORT_NODE": self.op.target_node, "EXPORT_NODE": self.op.target_node,
"EXPORT_DO_SHUTDOWN": self.op.shutdown, "EXPORT_DO_SHUTDOWN": self.op.shutdown,
} }
env.update(_BuildInstanceHookEnvByObject(self.instance))
nl = [self.sstore.GetMasterNode(), self.instance.primary_node, nl = [self.sstore.GetMasterNode(), self.instance.primary_node,
self.op.target_node] self.op.target_node]
return env, nl, nl return env, nl, nl
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment