From 1cd2fa5857acbf2bbd1e929385fefd2b779785c8 Mon Sep 17 00:00:00 2001 From: Ashwinh Date: Mon, 1 Jul 2024 10:01:54 +0000 Subject: [PATCH] CP-49900: Moved scripts/templates to python3/templates directory - Modified code to using 2to3 - Fixed except issue - Removed templates from scripts/Makefile Signed-off-by: Ashwinh --- python3/Makefile | 3 + {scripts => python3}/templates/debian | 83 ++++++++++++++++++--------- {scripts => python3}/templates/debug | 0 scripts/Makefile | 3 - 4 files changed, 58 insertions(+), 31 deletions(-) rename {scripts => python3}/templates/debian (69%) rename {scripts => python3}/templates/debug (100%) diff --git a/python3/Makefile b/python3/Makefile index 8f34cb8e107..3f724d972c7 100644 --- a/python3/Makefile +++ b/python3/Makefile @@ -29,6 +29,9 @@ install: $(IPROG) plugins/disk-space $(DESTDIR)$(PLUGINDIR) $(IPROG) plugins/install-supp-pack $(DESTDIR)$(PLUGINDIR) $(IPROG) plugins/echo.py $(DESTDIR)$(PLUGINDIR)/echo +# templates + $(IPROG) templates/debian $(DESTDIR)$(OPTDIR)/packages/post-install-scripts/debian-etch + $(IPROG) templates/debug $(DESTDIR)$(OPTDIR)/packages/post-install-scripts # poweron $(IPROG) poweron/wlan.py $(DESTDIR)$(PLUGINDIR)/wlan.py diff --git a/scripts/templates/debian b/python3/templates/debian similarity index 69% rename from scripts/templates/debian rename to python3/templates/debian index 9350a40a57d..4e9b12a8714 100644 --- a/scripts/templates/debian +++ b/python3/templates/debian @@ -1,30 +1,46 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # Copyright (c) 2005-2007 XenSource, Inc # Code ripped out of 'xgt' script for now from __future__ import print_function -import commands, xmlrpclib, os, sys, httplib, socket, urllib2, signal + +import os +import signal +import socket +import sys + +import commands +import httplib +import urllib2 +import xmlrpclib verbose = True + ##### begin hack. Provide xmlrpc over UNIX domain socket (cut+pasted from eliloader): class UDSHTTPConnection(httplib.HTTPConnection): - """ Stupid hacked up HTTPConnection subclass to allow HTTP over Unix domain - sockets. """ + """Stupid hacked up HTTPConnection subclass to allow HTTP over Unix domain + sockets.""" + def connect(self): path = self.host.replace("_", "/") self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self.sock.connect(path) + class UDSHTTP(httplib.HTTP): _connection_class = UDSHTTPConnection + class UDSTransport(xmlrpclib.Transport): def make_connection(self, host): return UDSHTTP(host) + def xapi_local(): return xmlrpclib.Server("http://_var_xapi_xapi/", transport=UDSTransport()) + + ##### end hack. @@ -36,43 +52,47 @@ def run(cmd, *args): debug("+ " + cmd % args) (ret, out) = commands.getstatusoutput(cmd % args) if verbose: - try: + try: for line in out.split("\n"): log("| " + line) except TypeError as e: pass if ret != 0: - debug ("run - command %s failed with %d" , cmd, ret) + debug("run - command %s failed with %d", cmd, ret) raise CommandException(out) return out + def log(fmt, *args): print(fmt % args) + def debug(msg, *args): if verbose: print(msg % args) + def create_partition(lvpath): # 1. write a partition table: - pipe = os.popen('/sbin/fdisk %s' % lvpath, 'w') + pipe = os.popen("/sbin/fdisk %s" % lvpath, "w") - pipe.write('n\n') # new partition - pipe.write('p\n') # primary - pipe.write("1\n") # 1st partition - pipe.write('\n') # default start cylinder - pipe.write('\n') # size: as big as image - pipe.write('w\n') # write partition table + pipe.write("n\n") # new partition + pipe.write("p\n") # primary + pipe.write("1\n") # 1st partition + pipe.write("\n") # default start cylinder + pipe.write("\n") # size: as big as image + pipe.write("w\n") # write partition table # XXX we must ignore certain errors here as fdisk will # sometimes return non-zero signalling error conditions # we don't care about. Should fix to detect these cases # specifically. rc = pipe.close() - if rc == None: + if rc == None: rc = 0 log("fdisk exited with rc %d (some non-zero exits can be ignored safely)." % rc) + def map_partitions(lvpath): run("/sbin/kpartx -a %s", lvpath) ps = [] @@ -80,37 +100,42 @@ def map_partitions(lvpath): ps.append("/dev/mapper/" + line.split()[0]) return ps + def unmap_partitions(lvpath): run("/sbin/kpartx -d %s", lvpath) + def umount(mountpoint): - run("umount -l %s",mountpoint) + run("umount -l %s", mountpoint) + if __name__ == "__main__": - #os.setpgrp() + # os.setpgrp() xvda = os.getenv("xvda") xvdb = os.getenv("xvdb") debug("Guest's xvda is on %s" % xvda) debug("Guest's xvdb is on %s" % xvdb) if xvda == None or xvdb == None: - raise "Need to pass in device names for xvda and xvdb through the environment" - + raise ValueError ("Need to pass in device names for xvda and xvdb through the environment") + vm = os.getenv("vm") - server = xapi_local () + server = xapi_local() try: - session_id = server.session.login_with_password('','','1.0','xen-api-scripts-debian')['Value'] - uuid = server.VM.get_uuid(session_id, vm)['Value'] + session_id = server.session.login_with_password( + "", "", "1.0", "xen-api-scripts-debian" + )["Value"] + uuid = server.VM.get_uuid(session_id, vm)["Value"] mountpoint = "/tmp/installer/%s" % (uuid) finally: server.session.logout(session_id) def sighandler(signum, frame): - umount(mountpoint) - os.killpg(0,signal.SIGKILL) - exit(1) + umount(mountpoint) + os.killpg(0, signal.SIGKILL) + exit(1) - signal.signal(signal.SIGTERM,sighandler) + signal.signal(signal.SIGTERM, sighandler) create_partition(xvda) create_partition(xvdb) @@ -132,10 +157,12 @@ if __name__ == "__main__": run("/usr/bin/unzip -p %s swap.img | dd of=%s oflag=direct bs=1M", xgt, xvdb) try: - session_id = server.session.login_with_password('','','1.0','xen-api-scripts-debian')['Value'] - vbds = server.VM.get_VBDs(session_id, vm)['Value'] + session_id = server.session.login_with_password( + "", "", "1.0", "xen-api-scripts-debian" + )["Value"] + vbds = server.VM.get_VBDs(session_id, vm)["Value"] for i in vbds: - dev = server.VBD.get_userdevice(session_id, i)['Value'] + dev = server.VBD.get_userdevice(session_id, i)["Value"] if dev == "0": server.VBD.set_bootable(session_id, i, True) finally: diff --git a/scripts/templates/debug b/python3/templates/debug similarity index 100% rename from scripts/templates/debug rename to python3/templates/debug diff --git a/scripts/Makefile b/scripts/Makefile index 705b161158a..b068e7a8959 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -145,9 +145,6 @@ install: mkdir -p $(DESTDIR)/etc/cron.d $(IDATA) xapi-tracing-log-trim.cron $(DESTDIR)/etc/cron.d/xapi-tracing-log-trim.cron mkdir -p $(DESTDIR)/opt/xensource/gpg -# templates - $(IPROG) templates/debian $(DESTDIR)$(OPTDIR)/packages/post-install-scripts/debian-etch - $(IPROG) templates/debug $(DESTDIR)$(OPTDIR)/packages/post-install-scripts # host-backup-restore $(IPROG) host-backup-restore/host-backup $(DESTDIR)$(LIBEXECDIR) $(IPROG) host-backup-restore/host-restore $(DESTDIR)$(LIBEXECDIR)