Commit b555101c authored by Thomas Thrainer's avatar Thomas Thrainer
Browse files

Convert node indices to UUIDs in configuration



cfgupgrade replaces the occurences of node names (where they are used as
index) with the node UUID.
It also supports the inverse replacement, so downgrades are supported
too.

Tests for cfgupgrade are adapted as well.
Signed-off-by: default avatarThomas Thrainer <thomasth@google.com>
Reviewed-by: default avatarKlaus Aehlig <aehlig@google.com>
parent 1c3231aa
......@@ -37,6 +37,23 @@ from ganeti import netutils
import testutils
def GetMinimalConfig():
return {
"version": constants.CONFIG_VERSION,
"cluster": {
"master_node": "node1-uuid"
},
"instances": {},
"nodegroups": {},
"nodes": {
"node1-uuid": {
"name": "node1",
"uuid": "node1-uuid"
}
},
}
def _RunUpgrade(path, dry_run, no_verify, ignore_hostname=True,
downgrade=False):
cmd = [sys.executable, "%s/tools/cfgupgrade" % testutils.GetSourceDir(),
......@@ -95,12 +112,8 @@ class TestCfgupgrade(unittest.TestCase):
def testWrongHostname(self):
self._CreateValidConfigDir()
utils.WriteFile(self.config_path, data=serializer.DumpJson({
"version": constants.CONFIG_VERSION,
"cluster": {},
"instances": {},
"nodegroups": {},
}))
utils.WriteFile(self.config_path,
data=serializer.DumpJson(GetMinimalConfig()))
hostname = netutils.GetHostname().name
assert hostname != utils.ReadOneLineFile(self.ss_master_node_path)
......@@ -111,12 +124,8 @@ class TestCfgupgrade(unittest.TestCase):
def testCorrectHostname(self):
self._CreateValidConfigDir()
utils.WriteFile(self.config_path, data=serializer.DumpJson({
"version": constants.CONFIG_VERSION,
"cluster": {},
"instances": {},
"nodegroups": {},
}))
utils.WriteFile(self.config_path,
data=serializer.DumpJson(GetMinimalConfig()))
utils.WriteFile(self.ss_master_node_path,
data="%s\n" % netutils.GetHostname().name)
......@@ -126,14 +135,9 @@ class TestCfgupgrade(unittest.TestCase):
def testInconsistentConfig(self):
self._CreateValidConfigDir()
# There should be no "config_version"
cfg = {
"version": 0,
"cluster": {
"config_version": 0,
},
"instances": {},
"nodegroups": {},
}
cfg = GetMinimalConfig()
cfg["version"] = 0
cfg["cluster"]["config_version"] = 0
utils.WriteFile(self.config_path, data=serializer.DumpJson(cfg))
self.assertRaises(Exception, _RunUpgrade, self.tmpdir, False, True)
......@@ -150,19 +154,15 @@ class TestCfgupgrade(unittest.TestCase):
def _TestSimpleUpgrade(self, from_version, dry_run,
file_storage_dir=None,
shared_file_storage_dir=None):
cluster = {}
cfg = GetMinimalConfig()
cfg["version"] = from_version
cluster = cfg["cluster"]
if file_storage_dir:
cluster["file_storage_dir"] = file_storage_dir
if shared_file_storage_dir:
cluster["shared_file_storage_dir"] = shared_file_storage_dir
cfg = {
"version": from_version,
"cluster": cluster,
"instances": {},
"nodegroups": {},
}
self._TestUpgradeFromData(cfg, dry_run)
def _TestUpgradeFromData(self, cfg, dry_run):
......
......@@ -278,6 +278,50 @@ def UpgradeFileStoragePaths(config_data):
backup=True)
def GetNewNodeIndex(nodes_by_old_key, old_key, new_key_field):
if old_key not in nodes_by_old_key:
logging.warning("Can't find node '%s' in configuration, assuming that it's"
" already up-to-date", old_key)
return old_key
return nodes_by_old_key[old_key][new_key_field]
def ChangeNodeIndices(config_data, old_key_field, new_key_field):
def ChangeDiskNodeIndices(disk):
if disk["dev_type"] in constants.LDS_DRBD:
for i in range(0, 2):
disk["logical_id"][i] = GetNewNodeIndex(nodes_by_old_key,
disk["logical_id"][i],
new_key_field)
if "children" in disk:
for child in disk["children"]:
ChangeDiskNodeIndices(child)
nodes_by_old_key = {}
nodes_by_new_key = {}
for (_, node) in config_data["nodes"].items():
nodes_by_old_key[node[old_key_field]] = node
nodes_by_new_key[node[new_key_field]] = node
config_data["nodes"] = nodes_by_new_key
cluster = config_data["cluster"]
cluster["master_node"] = GetNewNodeIndex(nodes_by_old_key,
cluster["master_node"],
new_key_field)
for inst in config_data["instances"].values():
inst["primary_node"] = GetNewNodeIndex(nodes_by_old_key,
inst["primary_node"],
new_key_field)
for disk in inst["disks"]:
ChangeDiskNodeIndices(disk)
def UpgradeNodeIndices(config_data):
ChangeNodeIndices(config_data, "name", "uuid")
def UpgradeAll(config_data):
config_data["version"] = constants.BuildVersion(TARGET_MAJOR,
TARGET_MINOR, 0)
......@@ -288,6 +332,7 @@ def UpgradeAll(config_data):
UpgradeCluster(config_data)
UpgradeGroups(config_data)
UpgradeInstances(config_data)
UpgradeNodeIndices(config_data)
def DowngradeDisks(disks, owner):
......@@ -309,10 +354,15 @@ def DowngradeInstances(config_data):
DowngradeDisks(iobj["disks"], iname)
def DowngradeNodeIndices(config_data):
ChangeNodeIndices(config_data, "uuid", "name")
def DowngradeAll(config_data):
# Any code specific to a particular version should be labeled that way, so
# it can be removed when updating to the next version.
DowngradeInstances(config_data)
DowngradeNodeIndices(config_data)
def main():
......
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