diff options
author | George Kraft <george.kraft@calxeda.com> | 2012-07-18 11:49:30 -0500 |
---|---|---|
committer | George Kraft <george.kraft@calxeda.com> | 2012-07-18 11:49:30 -0500 |
commit | 3d5250a7a50db8bb29ba0f2896710419149b8e38 (patch) | |
tree | eedeae7a0de11fcbfd5be550b0fa1b1d4d255a09 | |
parent | 96f915a39bb58dfda705d29a76d4bebe0d792aa9 (diff) | |
download | cxmanage-3d5250a7a50db8bb29ba0f2896710419149b8e38.tar.gz |
target: Make work_dir be a class variable
So we don't have to pass it as an argument between a million
methods.
-rw-r--r-- | cxmanage/controller.py | 21 | ||||
-rw-r--r-- | cxmanage/infodump.py | 88 | ||||
-rw-r--r-- | cxmanage/target.py | 74 | ||||
-rw-r--r-- | cxmanage_test/controller_test.py | 12 | ||||
-rw-r--r-- | cxmanage_test/target_test.py | 10 |
5 files changed, 110 insertions, 95 deletions
diff --git a/cxmanage/controller.py b/cxmanage/controller.py index e247c8d..6a75f71 100644 --- a/cxmanage/controller.py +++ b/cxmanage/controller.py @@ -193,7 +193,7 @@ class Controller: target = self.target_class(address, username, password, self.verbosity) if all_nodes: - for entry in target.get_ipinfo(self.work_dir, self.tftp): + for entry in target.get_ipinfo(self.tftp): self.add_target(entry[1], username, password) else: self.targets.append(target) @@ -330,7 +330,7 @@ class Controller: # Get results and errors results, errors = self._run_command("update_firmware", - self.work_dir, self.tftp, self.images, slot_arg) + self.tftp, self.images, slot_arg) if self.verbosity >= 1 and len(errors) == 0: print "Command completed successfully.\n" @@ -390,8 +390,7 @@ class Controller: def get_ipinfo(self): """ Get IP info from all targets """ - results, errors = self._run_command("get_ipinfo", - self.work_dir, self.tftp) + results, errors = self._run_command("get_ipinfo", self.tftp) # Print results if len(results) > 0: @@ -409,8 +408,7 @@ class Controller: def get_macaddrs(self): """ Get mac addresses from all targets """ - results, errors = self._run_command("get_macaddrs", - self.work_dir, self.tftp) + results, errors = self._run_command("get_macaddrs", self.tftp) # Print results if len(results) > 0: @@ -436,8 +434,7 @@ class Controller: print target.address print - results, errors = self._run_command("config_reset", - self.work_dir, self.tftp) + results, errors = self._run_command("config_reset", self.tftp) if self.verbosity >= 1 and len(errors) == 0: print "Command completed successfully.\n" @@ -461,8 +458,7 @@ class Controller: print target.address print - results, errors = self._run_command("config_boot", - self.work_dir, self.tftp, boot_args) + results, errors = self._run_command("config_boot", self.tftp, boot_args) if self.verbosity >= 1 and len(errors) == 0: print "Command completed successfully.\n" @@ -472,8 +468,7 @@ class Controller: def config_boot_status(self): """ Get boot order from all targets """ - results, errors = self._run_command("config_boot_status", - self.work_dir, self.tftp) + results, errors = self._run_command("config_boot_status", self.tftp) # Print results if len(results) > 0: @@ -492,7 +487,7 @@ class Controller: def info_dump(self): """ Dump info from all targets """ for target in self.targets: - target.info_dump(self.work_dir, self.tftp) + target.info_dump(self.tftp) return False diff --git a/cxmanage/infodump.py b/cxmanage/infodump.py index c0454df..f1664fb 100644 --- a/cxmanage/infodump.py +++ b/cxmanage/infodump.py @@ -30,26 +30,26 @@ from pkg_resources import resource_string -def print_info_dump(work_dir, tftp, target): +def print_info_dump(tftp, target): """ Print the IPMI data available from the SoC. """ # See if we need a tftp server - chassis(work_dir, tftp, target) - lan(work_dir, tftp, target) - power(work_dir, tftp, target) - event(work_dir, tftp, target) - controller(work_dir, tftp, target) - sdr(work_dir, tftp, target) - sensor(work_dir, tftp, target) - fru(work_dir, tftp, target) - sel(work_dir, tftp, target) - pef(work_dir, tftp, target) - user(work_dir, tftp, target) - channel(work_dir, tftp, target) - session(work_dir, tftp, target) - firmware(work_dir, tftp, target) - fabric(work_dir, tftp, target) - - print_ubootenv(work_dir, tftp, target) + chassis(tftp, target) + lan(tftp, target) + power(tftp, target) + event(tftp, target) + controller(tftp, target) + sdr(tftp, target) + sensor(tftp, target) + fru(tftp, target) + sel(tftp, target) + pef(tftp, target) + user(tftp, target) + channel(tftp, target) + session(tftp, target) + firmware(tftp, target) + fabric(tftp, target) + + print_ubootenv(tftp, target) print_cdb(target) print_registers(target) @@ -62,7 +62,7 @@ def ipmitool(target, cmd): except Exception as e: return "%s: %s" % (e.__class__.__name__, e) -def chassis(work_dir, tftp, target): +def chassis(tftp, target): """ Print Chassis information. """ print '[ IPMI Chassis status ]' print ipmitool(target, 'chassis status') + '\n' @@ -85,7 +85,7 @@ def chassis(work_dir, tftp, target): print ipmitool(target, 'chassis bootparam get 6') + '\n' print ipmitool(target, 'chassis bootparam get 7') + '\n' -def lan(work_dir, tftp, target): +def lan(tftp, target): """ Print LAN information. """ print '[ IPMI LAN Configuration ]' print ipmitool(target, 'lan print') + '\n' @@ -94,17 +94,17 @@ def lan(work_dir, tftp, target): print '[ IPMI LAN Stats ]' print ipmitool(target, 'lan stats get') + '\n' -def power(work_dir, tftp, target): +def power(tftp, target): """ If there were power commands that it makes sense to issue, they would be here. """ pass -def event(work_dir, tftp, target): +def event(tftp, target): """ If there were event commands that it makes sense to issue, they would be here. """ pass -def controller(work_dir, tftp, target): +def controller(tftp, target): """ Print management controller information. """ print '[ IPMI BMC GUID ]' print ipmitool(target, 'mc guid') + '\n' @@ -113,42 +113,42 @@ def controller(work_dir, tftp, target): print '[ IPMI BMC Global Enables ]' print ipmitool(target, 'mc getenables') + '\n' -def sdr(work_dir, tftp, target): +def sdr(tftp, target): """ Print sensor data record information. """ print '[ IPMI Sensor Description Records ]' print ipmitool(target, 'sdr') + '\n' -def sensor(work_dir, tftp, target): +def sensor(tftp, target): """ Print sensor information """ print '[ IPMI Sensors ]' print ipmitool(target, 'sensor') + '\n' -def fru(work_dir, tftp, target): +def fru(tftp, target): """ Print FRU information. """ print '[ IPMI FRU data records ]' print ipmitool(target, 'fru') + '\n' -def sel(work_dir, tftp, target): +def sel(tftp, target): """ Print event log. """ print '[ IPMI System Event Log ]' print ipmitool(target, 'sel') + '\n' -def pef(work_dir, tftp, target): +def pef(tftp, target): """ Print PEF information. """ print '[ IPMI Platform Event Filters ]' print ipmitool(target, 'pef') + '\n' -def user(work_dir, tftp, target): +def user(tftp, target): """ Print user information. """ print '[ IPMI Users ]' print ipmitool(target, 'user list') + '\n' -def session(work_dir, tftp, target): +def session(tftp, target): """ Print session information. """ print '[ IPMI Sessions Info ]' print ipmitool(target, 'session info all') + '\n' -def channel(work_dir, tftp, target): +def channel(tftp, target): """ Print channel information. """ #print '[ IPMI Channel Access ]' #print ipmitool(target, 'channel getaccess') @@ -157,25 +157,25 @@ def channel(work_dir, tftp, target): #print '[ IPMI Channel Ciphers ]' #print ipmitool(target, 'channel getciphers') -def firmware(work_dir, tftp, target): +def firmware(tftp, target): """ Print firmware information. """ print '[ IPMI Firmware Info ]' print ipmitool(target, 'cxoem fw info') + '\n' -def fabric(work_dir, tftp, target): +def fabric(tftp, target): """ Print the fabric-related data. """ print '[ CXOEM Fabric Data ]' ipinfo = [] try: - ipinfo = target.get_ipinfo(work_dir, tftp) + ipinfo = target.get_ipinfo(tftp) for entry in ipinfo: print 'Node %i: %s' % entry except Exception as e: print '%s: %s' % (e.__class__.__name__, e) try: - macaddrs = target.get_macaddrs(work_dir, tftp) + macaddrs = target.get_macaddrs(tftp) for entry in macaddrs: print 'Node %i, Port %i: %s' % entry except Exception as e: @@ -190,13 +190,13 @@ def fabric(work_dir, tftp, target): print -def print_ubootenv(work_dir, tftp, target): +def print_ubootenv(tftp, target): """ Print u-boot environment variables """ print '[ U-Boot Environment ]' try: fwinfo = target.get_firmware_info() slot = target._get_slot(fwinfo, "UBOOTENV", "ACTIVE") - ubootenv = target._download_ubootenv(work_dir, tftp, slot) + ubootenv = target._download_ubootenv(tftp, slot) for variable in sorted(ubootenv.variables): print '%s=%s' % (variable, ubootenv.variables[variable]) except Exception as e: @@ -210,7 +210,19 @@ def print_cdb(target, cids=None): for cid in cids: print '[ CDB %s ]' % cid - print ipmitool(target, 'cxoem data cdb read 64 %s' % cid) + '\n' + output = ipmitool(target, 'cxoem data cdb read 64 %s' % cid) + size_lines = [x for x in output.split('\n') + if x.startswith('CID size')] + value_lines = [x for x in output.split('\n') if x.startswith('Value')] + + if len(size_lines) == 1 and len(value_lines) == 1: + print ('CID size : %s' % + size_lines[0].partition(':')[2].rstrip().lstrip()) + print ('Value : %s' % + value_lines[0].partition(':')[2].rstrip().lstrip()) + else: + print output + print def print_registers(target, registers=None): diff --git a/cxmanage/target.py b/cxmanage/target.py index 346645d..8652651 100644 --- a/cxmanage/target.py +++ b/cxmanage/target.py @@ -31,7 +31,9 @@ """ Target objects used by the cxmanage controller """ +import atexit import os +import shutil import subprocess import tempfile import time @@ -56,16 +58,24 @@ class Target: self.verbosity = verbosity self.ubootenv_class = ubootenv_class + self.work_dir = tempfile.mkdtemp(prefix="cxmanage-target-") + atexit.register(self._cleanup) + verbose = verbosity >= 2 self.bmc = make_bmc(bmc_class, hostname=address, username=username, password=password, verbose=verbose) - def get_ipinfo(self, work_dir, tftp): + def _cleanup(self): + """ Clean up temporary files """ + if os.path.exists(self.work_dir): + shutil.rmtree(self.work_dir) + + def get_ipinfo(self, tftp): """ Download IP info from this target """ tftp_address = "%s:%s" % (tftp.get_address(self.address), tftp.get_port()) - filename = tempfile.mkstemp(prefix="%s/ip_" % work_dir)[1] + filename = tempfile.mkstemp(prefix="%s/ip_" % self.work_dir)[1] basename = os.path.basename(filename) # Send ipinfo command @@ -105,12 +115,12 @@ class Target: return results - def get_macaddrs(self, work_dir, tftp): + def get_macaddrs(self, tftp): """ Download mac addresses from this target """ tftp_address = "%s:%s" % (tftp.get_address(self.address), tftp.get_port()) - filename = tempfile.mkstemp(prefix="%s/mac_" % work_dir)[1] + filename = tempfile.mkstemp(prefix="%s/mac_" % self.work_dir)[1] basename = os.path.basename(filename) # Send ipinfo command @@ -218,7 +228,7 @@ class Target: except IpmiError: raise CxmanageError("Failed to retrieve firmware info") - def update_firmware(self, work_dir, tftp, images, slot_arg="INACTIVE"): + def update_firmware(self, tftp, images, slot_arg="INACTIVE"): """ Update firmware on this target. """ fwinfo = self.get_firmware_info() @@ -241,19 +251,17 @@ class Target: factory_slot = self._get_slot(fwinfo, image.type, "SECOND") # Update running ubootenv - boot_order = self._download_ubootenv(work_dir, - tftp, running_slot).get_boot_order() + boot_order = self._download_ubootenv(tftp, + running_slot).get_boot_order() contents = open(image.filename).read() if image.simg: contents = contents[28:] ubootenv = self.ubootenv_class(contents) ubootenv.set_boot_order(boot_order) - self._upload_ubootenv(work_dir, tftp, - ubootenv, running_slot, version) + self._upload_ubootenv(tftp, ubootenv, running_slot, version) # Update factory ubootenv - self._upload_image(work_dir, tftp, image, - factory_slot, version) + self._upload_image(tftp, image, factory_slot, version) else: # Get the slots @@ -265,9 +273,9 @@ class Target: # Update the image for slot in slots: - self._upload_image(work_dir, tftp, image, slot, version) + self._upload_image(tftp, image, slot, version) - def config_reset(self, work_dir, tftp): + def config_reset(self, tftp): """ Reset configuration to factory default """ try: # Reset CDB @@ -279,8 +287,8 @@ class Target: fwinfo = self.get_firmware_info() running_slot = self._get_slot(fwinfo, "UBOOTENV", "FIRST") factory_slot = self._get_slot(fwinfo, "UBOOTENV", "SECOND") - image = self._download_image(work_dir, tftp, factory_slot) - self._upload_image(work_dir, tftp, image, running_slot) + image = self._download_image(tftp, factory_slot) + self._upload_image(tftp, image, running_slot) # Clear SEL self.bmc.sel_clear() @@ -288,28 +296,28 @@ class Target: except IpmiError: raise CxmanageError("Failed to reset configuration") - def config_boot(self, work_dir, tftp, boot_args): + def config_boot(self, tftp, boot_args): """ Configure boot order """ fwinfo = self.get_firmware_info() first_slot = self._get_slot(fwinfo, "UBOOTENV", "FIRST") active_slot = self._get_slot(fwinfo, "UBOOTENV", "ACTIVE") # Download active ubootenv, modify, then upload to first slot - ubootenv = self._download_ubootenv(work_dir, tftp, active_slot) + ubootenv = self._download_ubootenv(tftp, active_slot) ubootenv.set_boot_order(boot_args) version = max(int(x.version, 16) for x in [first_slot, active_slot]) - self._upload_ubootenv(work_dir, tftp, ubootenv, first_slot, version) + self._upload_ubootenv(tftp, ubootenv, first_slot, version) - def config_boot_status(self, work_dir, tftp): + def config_boot_status(self, tftp): """ Get boot order """ fwinfo = self.get_firmware_info() active_slot = self._get_slot(fwinfo, "UBOOTENV", "ACTIVE") - ubootenv = self._download_ubootenv(work_dir, tftp, active_slot) + ubootenv = self._download_ubootenv(tftp, active_slot) return ubootenv.get_boot_order() - def info_dump(self, work_dir, tftp): + def info_dump(self, tftp): """ Dump info from this target """ - print_info_dump(work_dir, tftp, self) + print_info_dump(tftp, self) def ipmitool_command(self, ipmitool_args): """ Execute an arbitrary ipmitool command """ @@ -323,12 +331,12 @@ class Target: output = subprocess.check_output(command, stderr=subprocess.STDOUT) return output.rstrip().lstrip() - def get_ubootenv(self, work_dir, tftp): + def get_ubootenv(self, tftp): """ Get the active u-boot environment """ fwinfo = self.get_firmware_info() slot = self._get_slot(fwinfo, "UBOOTENV", "ACTIVE") - return self._download_ubootenv(work_dir, tftp, slot) + return self._download_ubootenv(tftp, slot) def _get_slot(self, fwinfo, image_type, slot_arg): """ Get a slot for this image type based on the slot argument """ @@ -384,7 +392,7 @@ class Target: else: raise ValueError("Invalid slot argument: %s" % slot_arg) - def _upload_image(self, work_dir, tftp, image, slot, version=None): + def _upload_image(self, tftp, image, slot, version=None): """ Upload a single image. This includes uploading the image, performing the firmware update, crc32 check, and activation.""" tftp_address = "%s:%s" % (tftp.get_address(self.address), @@ -400,7 +408,7 @@ class Target: image.type, int(slot.slot)) # Upload image to tftp server - filename = image.upload(work_dir, tftp, version, daddr) + filename = image.upload(self.work_dir, tftp, version, daddr) # Send firmware update command slot_id = int(slot.slot) @@ -420,7 +428,7 @@ class Target: else: raise CxmanageError("Node reported crc32 check failure") - def _download_image(self, work_dir, tftp, slot): + def _download_image(self, tftp, slot): """ Download an image from the target. Returns the filename. """ @@ -428,7 +436,7 @@ class Target: tftp.get_port()) # Download the image - filename = tempfile.mkstemp(prefix="%s/img_" % work_dir)[1] + filename = tempfile.mkstemp(prefix="%s/img_" % self.work_dir)[1] basename = os.path.basename(filename) image_type = slot.type.split()[1][1:-1] handle = self.bmc.retrieve_firmware(basename, @@ -438,16 +446,16 @@ class Target: return Image(filename, image_type) - def _upload_ubootenv(self, work_dir, tftp, ubootenv, slot, version=None): + def _upload_ubootenv(self, tftp, ubootenv, slot, version=None): """ Upload a uboot environment to the target """ - filename = tempfile.mkstemp(prefix="%s/env_" % work_dir)[1] + filename = tempfile.mkstemp(prefix="%s/env_" % self.work_dir)[1] open(filename, "w").write(ubootenv.get_contents()) image = Image(filename, "UBOOTENV") - self._upload_image(work_dir, tftp, image, slot, version) + self._upload_image(tftp, image, slot, version) - def _download_ubootenv(self, work_dir, tftp, slot): + def _download_ubootenv(self, tftp, slot): """ Download a uboot environment from the target """ - image = self._download_image(work_dir, tftp, slot) + image = self._download_image(tftp, slot) # Open the file simg = open(image.filename).read() diff --git a/cxmanage_test/controller_test.py b/cxmanage_test/controller_test.py index c645cc5..ea9e5f4 100644 --- a/cxmanage_test/controller_test.py +++ b/cxmanage_test/controller_test.py @@ -285,11 +285,11 @@ class DummyTarget: self.address = address self.executed = [] - def get_ipinfo(self, work_dir, tftp): + def get_ipinfo(self, tftp): self.executed.append("get_ipinfo") return list(enumerate(ADDRESSES)) - def get_macaddrs(self, work_dir, tftp): + def get_macaddrs(self, tftp): self.executed.append("get_macaddrs") # TODO: return real mac addresses return [(a, 0, ADDRESSES[a]) for a in range(NUM_NODES)] @@ -311,7 +311,7 @@ class DummyTarget: def mc_reset(self): self.executed.append("mc_reset") - def update_firmware(self, work_dir, tftp, images, slot_arg): + def update_firmware(self, tftp, images, slot_arg): self.executed.append(("update_firmware", images)) time.sleep(random.randint(0, 2)) @@ -325,13 +325,13 @@ class DummyTarget: ] return sensors - def config_reset(self, work_dir, tftp): + def config_reset(self, tftp): self.executed.append("config_reset") - def config_boot(self, work_dir, tftp, boot_args): + def config_boot(self, tftp, boot_args): self.executed.append(("config_boot", boot_args)) - def config_boot_status(self, work_dir, tftp): + def config_boot_status(self, tftp): self.executed.append("config_boot_status") return ["disk", "pxe"] diff --git a/cxmanage_test/target_test.py b/cxmanage_test/target_test.py index c646848..de66576 100644 --- a/cxmanage_test/target_test.py +++ b/cxmanage_test/target_test.py @@ -64,7 +64,7 @@ class TargetTest(unittest.TestCase): def test_get_ipinfo(self): """ Test get_ipinfo command """ for target in self.targets: - result = target.get_ipinfo(self.work_dir, self.tftp) + result = target.get_ipinfo(self.tftp) executed = target.bmc.executed self.assertEqual(len(executed), 1) @@ -145,7 +145,7 @@ class TargetTest(unittest.TestCase): ] for target in self.targets: - target.update_firmware(self.work_dir, self.tftp, images) + target.update_firmware(self.tftp, images) partitions = target.bmc.partitions unchanged_partitions = [partitions[x] for x in [0, 1, 4]] @@ -172,7 +172,7 @@ class TargetTest(unittest.TestCase): def test_config_reset(self): """ Test config_reset command """ for target in self.targets: - target.config_reset(self.work_dir, self.tftp) + target.config_reset(self.tftp) # Assert config reset executed = target.bmc.executed @@ -199,7 +199,7 @@ class TargetTest(unittest.TestCase): """ Test config_boot command """ boot_args = ["disk", "pxe", "retry"] for target in self.targets: - target.config_boot(self.work_dir, self.tftp, boot_args) + target.config_boot(self.tftp, boot_args) partitions = target.bmc.partitions ubootenv_partition = partitions[5] @@ -220,7 +220,7 @@ class TargetTest(unittest.TestCase): def test_config_boot_status(self): """ Test config_boot_status command """ for target in self.targets: - result = target.config_boot_status(self.work_dir, self.tftp) + result = target.config_boot_status(self.tftp) partitions = target.bmc.partitions ubootenv_partition = partitions[5] |