Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import sys |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 4 | import dbus |
Norman James | facbca4 | 2015-11-09 15:58:14 -0600 | [diff] [blame] | 5 | import json |
Brad Bishop | 3cec296 | 2016-06-21 20:51:54 -0400 | [diff] [blame] | 6 | import xml.etree.ElementTree as ET |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 7 | |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 8 | dbus_objects = { |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 9 | 'power': { |
| 10 | 'bus_name': 'org.openbmc.control.Power', |
| 11 | 'object_name': '/org/openbmc/control/power0', |
| 12 | 'interface_name': 'org.openbmc.control.Power' |
| 13 | }, |
Andrew Geissler | 2cbbb67 | 2017-05-17 10:52:49 -0500 | [diff] [blame] | 14 | 'chassison': { |
| 15 | 'bus_name': 'xyz.openbmc_project.State.Chassis', |
| 16 | 'object_name': '/xyz/openbmc_project/state/chassis0', |
| 17 | 'interface_name': 'xyz.openbmc_project.State.Chassis', |
| 18 | 'property': 'RequestedPowerTransition', |
| 19 | 'value': 'xyz.openbmc_project.State.Chassis.Transition.On' |
| 20 | }, |
| 21 | 'chassisoff': { |
| 22 | 'bus_name': 'xyz.openbmc_project.State.Chassis', |
| 23 | 'object_name': '/xyz/openbmc_project/state/chassis0', |
| 24 | 'interface_name': 'xyz.openbmc_project.State.Chassis', |
| 25 | 'property': 'RequestedPowerTransition', |
| 26 | 'value': 'xyz.openbmc_project.State.Chassis.Transition.Off' |
| 27 | }, |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 28 | 'poweron': { |
Andrew Geissler | 25b9a85 | 2016-12-19 16:14:09 -0600 | [diff] [blame] | 29 | 'bus_name': 'xyz.openbmc_project.State.Host', |
| 30 | 'object_name': '/xyz/openbmc_project/state/host0', |
| 31 | 'interface_name': 'xyz.openbmc_project.State.Host', |
| 32 | 'property': 'RequestedHostTransition', |
| 33 | 'value': 'xyz.openbmc_project.State.Host.Transition.On' |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 34 | }, |
| 35 | 'poweroff': { |
Andrew Geissler | 25b9a85 | 2016-12-19 16:14:09 -0600 | [diff] [blame] | 36 | 'bus_name': 'xyz.openbmc_project.State.Host', |
| 37 | 'object_name': '/xyz/openbmc_project/state/host0', |
| 38 | 'interface_name': 'xyz.openbmc_project.State.Host', |
| 39 | 'property': 'RequestedHostTransition', |
| 40 | 'value': 'xyz.openbmc_project.State.Host.Transition.Off' |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 41 | }, |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 42 | 'bmcstate': { |
| 43 | 'bus_name': 'xyz.openbmc_project.State.BMC', |
| 44 | 'object_name': '/xyz/openbmc_project/state/bmc0', |
| 45 | 'interface_name': 'xyz.openbmc_project.State.BMC', |
| 46 | 'property': 'CurrentBMCState', |
| 47 | }, |
| 48 | 'chassisstate': { |
| 49 | 'bus_name': 'xyz.openbmc_project.State.Chassis', |
| 50 | 'object_name': '/xyz/openbmc_project/state/chassis0', |
| 51 | 'interface_name': 'xyz.openbmc_project.State.Chassis', |
| 52 | 'property': 'CurrentPowerState', |
| 53 | }, |
| 54 | 'hoststate': { |
| 55 | 'bus_name': 'xyz.openbmc_project.State.Host', |
| 56 | 'object_name': '/xyz/openbmc_project/state/host0', |
| 57 | 'interface_name': 'xyz.openbmc_project.State.Host', |
| 58 | 'property': 'CurrentHostState', |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 59 | }, |
| 60 | 'bootprogress': { |
George Keishing | 3be0995 | 2017-08-18 09:48:03 -0500 | [diff] [blame] | 61 | 'bus_name': 'xyz.openbmc_project.State.Host', |
| 62 | 'object_name': '/xyz/openbmc_project/state/host0', |
| 63 | 'interface_name': 'xyz.openbmc_project.State.Boot.Progress', |
| 64 | 'property': 'BootProgress', |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 65 | }, |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 66 | } |
| 67 | |
Andrew Jeffery | e4dcaef | 2017-10-11 18:08:01 +1030 | [diff] [blame] | 68 | def run_one_command(dbus_bus, objinfo): |
| 69 | bus = objinfo['bus_name'] |
| 70 | obj = objinfo['object_name'] |
| 71 | iface = objinfo['interface_name'] |
| 72 | dbus_obj = dbus_bus.get_object(bus, obj) |
| 73 | |
| 74 | if (objinfo.has_key('property')): |
| 75 | prop = objinfo['property'] |
| 76 | dbus_iface = dbus.Interface(dbus_obj, "org.freedesktop.DBus.Properties") |
| 77 | if objinfo.has_key('value'): |
| 78 | dbus_iface.Set(iface, prop, objinfo['value']) |
| 79 | else: |
| 80 | dbus_prop = dbus_iface.Get(iface, prop) |
| 81 | print '{:<20}: {}'.format(prop, str(dbus_prop)) |
| 82 | else: |
| 83 | dbus_iface = dbus.Interface(dbus_obj, "org.freedesktop.DBus.Properties") |
| 84 | props = dbus_iface.GetAll(iface) |
| 85 | for p in props: |
| 86 | print p + " = " + str(props[p]) |
| 87 | |
Andrew Jeffery | 7b376e7 | 2017-10-11 18:12:05 +1030 | [diff] [blame^] | 88 | def run_all_commands(dbus_bus, commands): |
| 89 | if isinstance(commands, dict): |
| 90 | run_one_command(dbus_bus, commands) |
| 91 | return |
| 92 | |
| 93 | assert isinstance(commands, list) |
| 94 | for command in commands: |
| 95 | run_one_command(dbus_bus, dbus_objects[command]) |
| 96 | |
Andrew Jeffery | 44e6d9c | 2017-10-11 16:57:31 +1030 | [diff] [blame] | 97 | def main(): |
| 98 | # Commands that need to run multiple objects above |
| 99 | multicmd_objects = { 'state' : ['bmcstate', 'chassisstate', 'hoststate'] } |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 100 | |
Andrew Jeffery | bdb7435 | 2017-10-11 17:29:48 +1030 | [diff] [blame] | 101 | dbus_bus = dbus.SystemBus() |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 102 | |
Andrew Jeffery | 44e6d9c | 2017-10-11 16:57:31 +1030 | [diff] [blame] | 103 | if (len(sys.argv) == 1 or sys.argv[1] == "-h" or |
| 104 | (not(dbus_objects.has_key(sys.argv[1])) and |
| 105 | (not(multicmd_objects.has_key(sys.argv[1]))))): |
| 106 | print "Usage: obmcutil [command]" |
| 107 | print "Available commands:" |
| 108 | for name in sorted(dbus_objects.keys()): |
| 109 | m = "" |
| 110 | if (dbus_objects[name].has_key('property') == True): |
| 111 | m = " (" + dbus_objects[name]['interface_name'] + "->" + \ |
| 112 | dbus_objects[name]['property'] + ")" |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 113 | |
Andrew Jeffery | 44e6d9c | 2017-10-11 16:57:31 +1030 | [diff] [blame] | 114 | print "\t" + name + m |
| 115 | print "Multi-Commands:" |
| 116 | for name in sorted(multicmd_objects.keys()): |
| 117 | print "\t" + name + " -> " + ",".join(multicmd_objects[name]) |
| 118 | exit(0) |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 119 | |
Andrew Jeffery | 44e6d9c | 2017-10-11 16:57:31 +1030 | [diff] [blame] | 120 | sys.argv.pop(0) |
Andrew Jeffery | 7b376e7 | 2017-10-11 18:12:05 +1030 | [diff] [blame^] | 121 | recipe = sys.argv.pop(0) |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 122 | |
Andrew Jeffery | 44e6d9c | 2017-10-11 16:57:31 +1030 | [diff] [blame] | 123 | # Check if this is a multicmd command and update if it is |
Andrew Jeffery | 7b376e7 | 2017-10-11 18:12:05 +1030 | [diff] [blame^] | 124 | if(multicmd_objects.has_key(recipe)): |
| 125 | recipe = multicmd_objects[recipe] |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 126 | |
Andrew Jeffery | 7b376e7 | 2017-10-11 18:12:05 +1030 | [diff] [blame^] | 127 | run_all_commands(dbus_bus, recipe) |
Andrew Jeffery | 44e6d9c | 2017-10-11 16:57:31 +1030 | [diff] [blame] | 128 | |
| 129 | if __name__ == "__main__": |
| 130 | main() |