Skip to content
Snippets Groups Projects
Commit bb553e5a authored by Bernardo Dal Seno's avatar Bernardo Dal Seno
Browse files

cfgupgrade: Refactor code


All the upgrading code is now in one function. No functionality has been
changed.

Signed-off-by: default avatarBernardo Dal Seno <bdalseno@google.com>
Reviewed-by: default avatarHelga Velroyen <helgav@google.com>
parent 04db1880
No related branches found
No related tags found
No related merge requests found
...@@ -114,8 +114,11 @@ def UpgradeGroups(config_data): ...@@ -114,8 +114,11 @@ def UpgradeGroups(config_data):
def UpgradeInstances(config_data): def UpgradeInstances(config_data):
network2uuid = dict((n["name"], n["uuid"]) network2uuid = dict((n["name"], n["uuid"])
for n in config_data["networks"].values()) for n in config_data["networks"].values())
for inst in config_data["instances"].values(): if "instances" not in config_data:
for nic in inst["nics"]: raise Error("Can't find the 'instances' key in the configuration!")
for instance, iobj in config_data["instances"].items():
for nic in iobj["nics"]:
name = nic.get("network", None) name = nic.get("network", None)
if name: if name:
uuid = network2uuid.get(name, None) uuid = network2uuid.get(name, None)
...@@ -124,6 +127,93 @@ def UpgradeInstances(config_data): ...@@ -124,6 +127,93 @@ def UpgradeInstances(config_data):
" Substituting with uuid %s." % (name, uuid)) " Substituting with uuid %s." % (name, uuid))
nic["network"] = uuid nic["network"] = uuid
if "disks" not in iobj:
raise Error("Instance '%s' doesn't have a disks entry?!" % instance)
disks = iobj["disks"]
for idx, dobj in enumerate(disks):
expected = "disk/%s" % idx
current = dobj.get("iv_name", "")
if current != expected:
logging.warning("Updating iv_name for instance %s/disk %s"
" from '%s' to '%s'",
instance, idx, current, expected)
dobj["iv_name"] = expected
def UpgradeRapiUsers():
if (os.path.isfile(options.RAPI_USERS_FILE_PRE24) and
not os.path.islink(options.RAPI_USERS_FILE_PRE24)):
if os.path.exists(options.RAPI_USERS_FILE):
raise Error("Found pre-2.4 RAPI users file at %s, but another file"
" already exists at %s" %
(options.RAPI_USERS_FILE_PRE24, options.RAPI_USERS_FILE))
logging.info("Found pre-2.4 RAPI users file at %s, renaming to %s",
options.RAPI_USERS_FILE_PRE24, options.RAPI_USERS_FILE)
if not options.dry_run:
utils.RenameFile(options.RAPI_USERS_FILE_PRE24, options.RAPI_USERS_FILE,
mkdir=True, mkdir_mode=0750)
# Create a symlink for RAPI users file
if (not (os.path.islink(options.RAPI_USERS_FILE_PRE24) or
os.path.isfile(options.RAPI_USERS_FILE_PRE24)) and
os.path.isfile(options.RAPI_USERS_FILE)):
logging.info("Creating symlink from %s to %s",
options.RAPI_USERS_FILE_PRE24, options.RAPI_USERS_FILE)
if not options.dry_run:
os.symlink(options.RAPI_USERS_FILE, options.RAPI_USERS_FILE_PRE24)
def UpgradeWatcher():
# Remove old watcher state file if it exists
if os.path.exists(options.WATCHER_STATEFILE):
logging.info("Removing watcher state file %s", options.WATCHER_STATEFILE)
if not options.dry_run:
utils.RemoveFile(options.WATCHER_STATEFILE)
def UpgradeFileStoragePaths(config_data):
# Write file storage paths
if not os.path.exists(options.FILE_STORAGE_PATHS_FILE):
cluster = config_data["cluster"]
file_storage_dir = cluster.get("file_storage_dir")
shared_file_storage_dir = cluster.get("shared_file_storage_dir")
del cluster
logging.info("Ganeti 2.7 and later only allow whitelisted directories"
" for file storage; writing existing configuration values"
" into '%s'",
options.FILE_STORAGE_PATHS_FILE)
if file_storage_dir:
logging.info("File storage directory: %s", file_storage_dir)
if shared_file_storage_dir:
logging.info("Shared file storage directory: %s",
shared_file_storage_dir)
buf = StringIO()
buf.write("# List automatically generated from configuration by\n")
buf.write("# cfgupgrade at %s\n" % time.asctime())
if file_storage_dir:
buf.write("%s\n" % file_storage_dir)
if shared_file_storage_dir:
buf.write("%s\n" % shared_file_storage_dir)
utils.WriteFile(file_name=options.FILE_STORAGE_PATHS_FILE,
data=buf.getvalue(),
mode=0600,
dry_run=options.dry_run,
backup=True)
def UpgradeAll(config_data):
config_data["version"] = constants.BuildVersion(TARGET_MAJOR,
TARGET_MINOR, 0)
UpgradeRapiUsers()
UpgradeWatcher()
UpgradeFileStoragePaths(config_data)
UpgradeNetworks(config_data)
UpgradeGroups(config_data)
UpgradeInstances(config_data)
def main(): def main():
"""Main program. """Main program.
...@@ -222,24 +312,7 @@ def main(): ...@@ -222,24 +312,7 @@ def main():
if config_major == 2 and config_minor in (0, 1, 2, 3, 4, 5, 6): if config_major == 2 and config_minor in (0, 1, 2, 3, 4, 5, 6):
if config_revision != 0: if config_revision != 0:
logging.warning("Config revision is %s, not 0", config_revision) logging.warning("Config revision is %s, not 0", config_revision)
UpgradeAll(config_data)
config_data["version"] = constants.BuildVersion(TARGET_MAJOR,
TARGET_MINOR, 0)
if "instances" not in config_data:
raise Error("Can't find the 'instances' key in the configuration!")
for instance, iobj in config_data["instances"].items():
if "disks" not in iobj:
raise Error("Instance '%s' doesn't have a disks entry?!" % instance)
disks = iobj["disks"]
for idx, dobj in enumerate(disks):
expected = "disk/%s" % idx
current = dobj.get("iv_name", "")
if current != expected:
logging.warning("Updating iv_name for instance %s/disk %s"
" from '%s' to '%s'",
instance, idx, current, expected)
dobj["iv_name"] = expected
elif config_major == TARGET_MAJOR and config_minor == TARGET_MINOR: elif config_major == TARGET_MAJOR and config_minor == TARGET_MINOR:
logging.info("No changes necessary") logging.info("No changes necessary")
...@@ -248,68 +321,6 @@ def main(): ...@@ -248,68 +321,6 @@ def main():
raise Error("Configuration version %d.%d.%d not supported by this tool" % raise Error("Configuration version %d.%d.%d not supported by this tool" %
(config_major, config_minor, config_revision)) (config_major, config_minor, config_revision))
if (os.path.isfile(options.RAPI_USERS_FILE_PRE24) and
not os.path.islink(options.RAPI_USERS_FILE_PRE24)):
if os.path.exists(options.RAPI_USERS_FILE):
raise Error("Found pre-2.4 RAPI users file at %s, but another file"
" already exists at %s" %
(options.RAPI_USERS_FILE_PRE24, options.RAPI_USERS_FILE))
logging.info("Found pre-2.4 RAPI users file at %s, renaming to %s",
options.RAPI_USERS_FILE_PRE24, options.RAPI_USERS_FILE)
if not options.dry_run:
utils.RenameFile(options.RAPI_USERS_FILE_PRE24, options.RAPI_USERS_FILE,
mkdir=True, mkdir_mode=0750)
# Create a symlink for RAPI users file
if (not (os.path.islink(options.RAPI_USERS_FILE_PRE24) or
os.path.isfile(options.RAPI_USERS_FILE_PRE24)) and
os.path.isfile(options.RAPI_USERS_FILE)):
logging.info("Creating symlink from %s to %s",
options.RAPI_USERS_FILE_PRE24, options.RAPI_USERS_FILE)
if not options.dry_run:
os.symlink(options.RAPI_USERS_FILE, options.RAPI_USERS_FILE_PRE24)
# Remove old watcher state file if it exists
if os.path.exists(options.WATCHER_STATEFILE):
logging.info("Removing watcher state file %s", options.WATCHER_STATEFILE)
if not options.dry_run:
utils.RemoveFile(options.WATCHER_STATEFILE)
# Write file storage paths
if not os.path.exists(options.FILE_STORAGE_PATHS_FILE):
cluster = config_data["cluster"]
file_storage_dir = cluster.get("file_storage_dir")
shared_file_storage_dir = cluster.get("shared_file_storage_dir")
del cluster
logging.info("Ganeti 2.7 and later only allow whitelisted directories"
" for file storage; writing existing configuration values"
" into '%s'",
options.FILE_STORAGE_PATHS_FILE)
if file_storage_dir:
logging.info("File storage directory: %s", file_storage_dir)
if shared_file_storage_dir:
logging.info("Shared file storage directory: %s",
shared_file_storage_dir)
buf = StringIO()
buf.write("# List automatically generated from configuration by\n")
buf.write("# cfgupgrade at %s\n" % time.asctime())
if file_storage_dir:
buf.write("%s\n" % file_storage_dir)
if shared_file_storage_dir:
buf.write("%s\n" % shared_file_storage_dir)
utils.WriteFile(file_name=options.FILE_STORAGE_PATHS_FILE,
data=buf.getvalue(),
mode=0600,
dry_run=options.dry_run,
backup=True)
UpgradeNetworks(config_data)
UpgradeGroups(config_data)
UpgradeInstances(config_data)
try: try:
logging.info("Writing configuration file to %s", options.CONFIG_DATA_PATH) logging.info("Writing configuration file to %s", options.CONFIG_DATA_PATH)
utils.WriteFile(file_name=options.CONFIG_DATA_PATH, utils.WriteFile(file_name=options.CONFIG_DATA_PATH,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment