Implement OpenVZ exporter

feature/node-rewrite
Sven Slootweg 12 years ago
parent 1466c23334
commit 3c92625cc7

@ -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(): def run():
# Check if the OpenVZ kernel is active # 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")

@ -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"): if os.path.exists("/etc/vz/vz.conf"):
sys.stdout.write("OpenVZ is already installed.\n") sys.stdout.write("OpenVZ is already installed.\n")
import exporter sys.stdout.write("I can export your current OpenVZ containers for you, so that you can import them into\n")
exporter.run() 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: else:
sys.stdout.write("Installing OpenVZ...\n") sys.stdout.write("Installing OpenVZ...\n")
packages = ["vzctl", "vzquota"] packages = ["vzctl", "vzquota"]

Loading…
Cancel
Save