From 3c92625cc781d1f2d7c37abde14596d5e6ddc62e Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Mon, 24 Dec 2012 21:38:22 +0100 Subject: [PATCH] Implement OpenVZ exporter --- installer/slave/exporter.py | 100 +++++++++++++++++++++++++++++++++++- installer/slave/install.py | 17 +++++- 2 files changed, 114 insertions(+), 3 deletions(-) diff --git a/installer/slave/exporter.py b/installer/slave/exporter.py index 2ce9d16..1ebf856 100644 --- a/installer/slave/exporter.py +++ b/installer/slave/exporter.py @@ -1,3 +1,101 @@ +import os, subprocess, sys, re, json, setuplib, time + +BOLD_START = '\033[1m' +BOLD_END = '\033[0m' + +beancounter_keys = ["KMEMSIZE", "LOCKEDPAGES", "PRIVVMPAGES", "SHMPAGES", "NUMPROC", "PHYSPAGES", + "VMGUARPAGES", "OOMGUARPAGES", "NUMTCPSOCK", "NUMFLOCK", "NUMPTY", "NUMSIGINFO", + "TCPSNDBUF", "TCPRCVBUF", "OTHERSOCKBUF", "DGRAMRCVBUF", "NUMOTHERSOCK", + "DCACHESIZE", "NUMFILE", "AVNUMPROC", "NUMIPTENT", "DISKSPACE", "DISKINODES", + "QUOTATIME", "CPUUNITS"] + def run(): # Check if the OpenVZ kernel is active - pass + r, w = os.pipe() + subprocess.call(["ps", "ax"], stdout=w) + found = False + + for process in os.read(r, 134217728).splitlines(): + if "vzmond" in process: + found = True + + if found == False: + sys.stderr.write("WARNING: OpenVZ kernel not detected as being active. Restart the\n") + sys.stderr.write(" system with the OpenVZ kernel enabled and manually run the exporter.\n") + return False + + # Start exporting + + containers = {} + + r, w = os.pipe() + subprocess.call(["vzlist", "-Ha", "-o", "ctid,status"], stdout=w) + + for line in os.read(r, 134217728).splitlines(): + ctid, status = line.split() + + iplist = [] + nameservers = [] + beancounters = {} + parameters = {} + + autorun = False + hostname = None + template = None + + for line in open("/etc/vz/conf/%s.conf" % ctid, "r"): + if line.startswith("#"): + continue + + if line.strip() == "": + continue + + key, value = line.split("=", 1) + + key = key.strip() + value = value.strip()[1:-1] + + if key in ["VE_ROOT", "VE_PRIVATE", "ORIGIN_SAMPLE"]: + # We don't really care about these, so we can ignore them. + continue + + elif key in beancounter_keys: + # This is a beancounter. + beancounters[key.lower()] = value + + elif key == "ONBOOT": + autorun = (value == "yes") + + elif key == "NAMESERVER": + nameservers += value.split() + + elif key == "IP_ADDRESS": + iplist += value.split() + + elif key == "HOSTNAME": + hostname = value + + elif key == "OSTEMPLATE": + template = value + + else: + parameters[key] = value + + containers[ctid] = { + 'ip_addresses': iplist, + 'beancounters': beancounters, + 'hostname': hostname, + 'autorun': autorun, + 'nameservers': nameservers, + 'parameters': parameters, + 'status': status, + 'template': template + } + + setuplib.create_directory("/etc/cvm/exported", True, 0, 0, "u+rwx") + + export_path = "/etc/cvm/exported/%s.openvz" % time.strftime("%Y%m%d_%H%M%S") + + setuplib.create_file(export_path, json.dumps({"openvz": containers}), 0, 0, "u+rwx") + + sys.stdout.write("\n" + BOLD_START + "Your exported data has been stored in %s." % export_path + BOLD_END + "\n") diff --git a/installer/slave/install.py b/installer/slave/install.py index 3040476..2154bf1 100644 --- a/installer/slave/install.py +++ b/installer/slave/install.py @@ -276,8 +276,21 @@ setuplib.create_file("/etc/cvm/version", "slave-0.1\n", cvm_uid, cvm_gid, "u+rwx if os.path.exists("/etc/vz/vz.conf"): sys.stdout.write("OpenVZ is already installed.\n") - import exporter - exporter.run() + sys.stdout.write("I can export your current OpenVZ containers for you, so that you can import them into\n") + sys.stdout.write("your CVM panel. If you are already using another panel (such as HyperVM or SolusVM),\n") + sys.stdout.write("however, you should answer 'no' and use the exporter specific for that panel instead.\n") + sys.stdout.write(BOLD_START + "Do you wish to export existing OpenVZ containers?" + BOLD_END + " (Y/n)\n") + q = raw_input() + + if q.lower() in ["n", "no"]: + sys.stdout.write(BOLD_START + "CVM slave node installation successfully finished!" + BOLD_END + "\n") + else: + import exporter + + if exporter.run() == True: + sys.stdout.write(BOLD_START + "CVM slave node installation successfully finished!" + BOLD_END + "\n") + else: + sys.stdout.write(BOLD_START + "Something went wrong during exporting. Otherwise, the CVM slave node installation successfully finished!" + BOLD_END + "\n") else: sys.stdout.write("Installing OpenVZ...\n") packages = ["vzctl", "vzquota"]