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 |
Andrew Jeffery | 0b6883d | 2017-10-11 22:08:55 +1030 | [diff] [blame] | 5 | import argparse |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 6 | |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 7 | from dbus.mainloop.glib import DBusGMainLoop |
| 8 | import gobject |
Andrew Jeffery | b43f7ed | 2017-10-12 01:33:59 +1030 | [diff] [blame] | 9 | import os |
| 10 | import signal |
| 11 | import time |
| 12 | from subprocess import Popen |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 13 | |
Andrew Jeffery | c6b5deb | 2017-10-11 22:18:27 +1030 | [diff] [blame] | 14 | descriptors = { |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 15 | 'power': { |
| 16 | 'bus_name': 'org.openbmc.control.Power', |
| 17 | 'object_name': '/org/openbmc/control/power0', |
| 18 | 'interface_name': 'org.openbmc.control.Power' |
| 19 | }, |
Andrew Geissler | 2cbbb67 | 2017-05-17 10:52:49 -0500 | [diff] [blame] | 20 | 'chassison': { |
| 21 | 'bus_name': 'xyz.openbmc_project.State.Chassis', |
| 22 | 'object_name': '/xyz/openbmc_project/state/chassis0', |
| 23 | 'interface_name': 'xyz.openbmc_project.State.Chassis', |
| 24 | 'property': 'RequestedPowerTransition', |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 25 | 'value': 'xyz.openbmc_project.State.Chassis.Transition.On', |
| 26 | 'monitor': 'obmc-chassis-poweron@0.target', |
Andrew Geissler | 2cbbb67 | 2017-05-17 10:52:49 -0500 | [diff] [blame] | 27 | }, |
| 28 | 'chassisoff': { |
| 29 | 'bus_name': 'xyz.openbmc_project.State.Chassis', |
| 30 | 'object_name': '/xyz/openbmc_project/state/chassis0', |
| 31 | 'interface_name': 'xyz.openbmc_project.State.Chassis', |
| 32 | 'property': 'RequestedPowerTransition', |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 33 | 'value': 'xyz.openbmc_project.State.Chassis.Transition.Off', |
| 34 | 'monitor': 'obmc-chassis-hard-poweroff@0.target', |
Andrew Geissler | 2cbbb67 | 2017-05-17 10:52:49 -0500 | [diff] [blame] | 35 | }, |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 36 | 'poweron': { |
Andrew Geissler | 25b9a85 | 2016-12-19 16:14:09 -0600 | [diff] [blame] | 37 | 'bus_name': 'xyz.openbmc_project.State.Host', |
| 38 | 'object_name': '/xyz/openbmc_project/state/host0', |
| 39 | 'interface_name': 'xyz.openbmc_project.State.Host', |
| 40 | 'property': 'RequestedHostTransition', |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 41 | 'value': 'xyz.openbmc_project.State.Host.Transition.On', |
| 42 | 'monitor': 'obmc-host-start@0.target', |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 43 | }, |
| 44 | 'poweroff': { |
Andrew Geissler | 25b9a85 | 2016-12-19 16:14:09 -0600 | [diff] [blame] | 45 | 'bus_name': 'xyz.openbmc_project.State.Host', |
| 46 | 'object_name': '/xyz/openbmc_project/state/host0', |
| 47 | 'interface_name': 'xyz.openbmc_project.State.Host', |
| 48 | 'property': 'RequestedHostTransition', |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 49 | 'value': 'xyz.openbmc_project.State.Host.Transition.Off', |
| 50 | 'monitor': 'obmc-host-stop@0.target', |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 51 | }, |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 52 | 'bmcstate': { |
| 53 | 'bus_name': 'xyz.openbmc_project.State.BMC', |
| 54 | 'object_name': '/xyz/openbmc_project/state/bmc0', |
| 55 | 'interface_name': 'xyz.openbmc_project.State.BMC', |
| 56 | 'property': 'CurrentBMCState', |
| 57 | }, |
| 58 | 'chassisstate': { |
| 59 | 'bus_name': 'xyz.openbmc_project.State.Chassis', |
| 60 | 'object_name': '/xyz/openbmc_project/state/chassis0', |
| 61 | 'interface_name': 'xyz.openbmc_project.State.Chassis', |
| 62 | 'property': 'CurrentPowerState', |
| 63 | }, |
| 64 | 'hoststate': { |
| 65 | 'bus_name': 'xyz.openbmc_project.State.Host', |
| 66 | 'object_name': '/xyz/openbmc_project/state/host0', |
| 67 | 'interface_name': 'xyz.openbmc_project.State.Host', |
| 68 | 'property': 'CurrentHostState', |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 69 | }, |
| 70 | 'bootprogress': { |
George Keishing | 3be0995 | 2017-08-18 09:48:03 -0500 | [diff] [blame] | 71 | 'bus_name': 'xyz.openbmc_project.State.Host', |
| 72 | 'object_name': '/xyz/openbmc_project/state/host0', |
| 73 | 'interface_name': 'xyz.openbmc_project.State.Boot.Progress', |
| 74 | 'property': 'BootProgress', |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 75 | }, |
Andrew Jeffery | 0b6883d | 2017-10-11 22:08:55 +1030 | [diff] [blame] | 76 | 'state' : ['bmcstate', 'chassisstate', 'hoststate'] |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 77 | } |
| 78 | |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 79 | def run_set_property(dbus_bus, dbus_iface, descriptor, args): |
| 80 | mainloop = gobject.MainLoop() |
| 81 | |
| 82 | iface = descriptor['interface_name'] |
| 83 | prop = descriptor['property'] |
| 84 | |
| 85 | if 'monitor' not in descriptor: |
| 86 | dbus_iface.Set(iface, prop, descriptor['value']) |
| 87 | return True |
| 88 | |
| 89 | def property_listener(job, path, unit, state): |
| 90 | if descriptor['monitor'] != unit: |
| 91 | return |
| 92 | |
| 93 | property_listener.success = (state == 'done') |
| 94 | mainloop.quit() |
| 95 | |
| 96 | property_listener.success = True |
| 97 | |
Andrew Jeffery | b43f7ed | 2017-10-12 01:33:59 +1030 | [diff] [blame] | 98 | if args.wait and args.verbose: |
| 99 | pid = Popen(["/bin/journalctl", "-f", "--no-pager"]).pid |
| 100 | |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 101 | if args.wait: |
| 102 | sig_match = dbus_bus.add_signal_receiver(property_listener, "JobRemoved") |
| 103 | |
| 104 | dbus_iface.Set(iface, prop, descriptor['value']) |
| 105 | |
| 106 | if args.wait: |
| 107 | mainloop.run() |
| 108 | sig_match.remove() |
| 109 | |
Andrew Jeffery | b43f7ed | 2017-10-12 01:33:59 +1030 | [diff] [blame] | 110 | if args.wait and args.verbose: |
| 111 | # wait some time for the journal output |
| 112 | time.sleep(args.wait_tune) |
| 113 | os.kill(pid, signal.SIGTERM) |
| 114 | |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 115 | return property_listener.success |
| 116 | |
| 117 | def run_one_command(dbus_bus, descriptor, args): |
Andrew Jeffery | c6b5deb | 2017-10-11 22:18:27 +1030 | [diff] [blame] | 118 | bus = descriptor['bus_name'] |
| 119 | obj = descriptor['object_name'] |
| 120 | iface = descriptor['interface_name'] |
Andrew Jeffery | e4dcaef | 2017-10-11 18:08:01 +1030 | [diff] [blame] | 121 | dbus_obj = dbus_bus.get_object(bus, obj) |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 122 | result = None |
Andrew Jeffery | e4dcaef | 2017-10-11 18:08:01 +1030 | [diff] [blame] | 123 | |
Andrew Jeffery | 81707a1 | 2017-10-12 01:35:42 +1030 | [diff] [blame^] | 124 | if descriptor.has_key('property'): |
Andrew Jeffery | e4dcaef | 2017-10-11 18:08:01 +1030 | [diff] [blame] | 125 | dbus_iface = dbus.Interface(dbus_obj, "org.freedesktop.DBus.Properties") |
Andrew Jeffery | c6b5deb | 2017-10-11 22:18:27 +1030 | [diff] [blame] | 126 | if descriptor.has_key('value'): |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 127 | result = run_set_property(dbus_bus, dbus_iface, descriptor, args) |
Andrew Jeffery | e4dcaef | 2017-10-11 18:08:01 +1030 | [diff] [blame] | 128 | else: |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 129 | prop = descriptor['property'] |
Andrew Jeffery | e4dcaef | 2017-10-11 18:08:01 +1030 | [diff] [blame] | 130 | dbus_prop = dbus_iface.Get(iface, prop) |
| 131 | print '{:<20}: {}'.format(prop, str(dbus_prop)) |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 132 | result = True |
Andrew Jeffery | e4dcaef | 2017-10-11 18:08:01 +1030 | [diff] [blame] | 133 | else: |
| 134 | dbus_iface = dbus.Interface(dbus_obj, "org.freedesktop.DBus.Properties") |
| 135 | props = dbus_iface.GetAll(iface) |
| 136 | for p in props: |
Andrew Jeffery | bba82bd | 2017-10-11 22:20:30 +1030 | [diff] [blame] | 137 | print "{} = {}".format(p, str(props[p])) |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 138 | result = True |
Andrew Jeffery | e4dcaef | 2017-10-11 18:08:01 +1030 | [diff] [blame] | 139 | |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 140 | return result |
| 141 | |
| 142 | def run_all_commands(dbus_bus, recipe, args): |
Andrew Jeffery | c6b5deb | 2017-10-11 22:18:27 +1030 | [diff] [blame] | 143 | if isinstance(recipe, dict): |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 144 | return run_one_command(dbus_bus, recipe, args) |
Andrew Jeffery | 7b376e7 | 2017-10-11 18:12:05 +1030 | [diff] [blame] | 145 | |
Andrew Jeffery | c6b5deb | 2017-10-11 22:18:27 +1030 | [diff] [blame] | 146 | assert isinstance(recipe, list) |
| 147 | for command in recipe: |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 148 | descriptor = descriptors[command] |
| 149 | if not run_one_command(dbus_bus, descriptor, args): |
| 150 | print "Failed to execute command: {}".format(descriptor) |
| 151 | return False |
| 152 | |
| 153 | return True |
Andrew Jeffery | 7b376e7 | 2017-10-11 18:12:05 +1030 | [diff] [blame] | 154 | |
Andrew Jeffery | 44e6d9c | 2017-10-11 16:57:31 +1030 | [diff] [blame] | 155 | def main(): |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 156 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 157 | |
Andrew Jeffery | 0b6883d | 2017-10-11 22:08:55 +1030 | [diff] [blame] | 158 | parser = argparse.ArgumentParser() |
Andrew Jeffery | b43f7ed | 2017-10-12 01:33:59 +1030 | [diff] [blame] | 159 | parser.add_argument('--verbose', '-v', action='store_true', |
| 160 | help="Verbose output") |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 161 | parser.add_argument('--wait', '-w', action='store_true', |
| 162 | help='Block until the state transition succeeds or fails') |
Andrew Jeffery | b43f7ed | 2017-10-12 01:33:59 +1030 | [diff] [blame] | 163 | parser.add_argument('--wait-tune', '-t', nargs='?', default=8, type=float, |
| 164 | # help='Seconds to wait for journal output to complete after receiving DBus signal', |
| 165 | help=argparse.SUPPRESS) |
Andrew Jeffery | c6b5deb | 2017-10-11 22:18:27 +1030 | [diff] [blame] | 166 | parser.add_argument('recipe', choices=sorted(descriptors.keys())) |
Andrew Jeffery | 0b6883d | 2017-10-11 22:08:55 +1030 | [diff] [blame] | 167 | args = parser.parse_args() |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 168 | |
Andrew Jeffery | bdb7435 | 2017-10-11 17:29:48 +1030 | [diff] [blame] | 169 | dbus_bus = dbus.SystemBus() |
Andrew Jeffery | 0b6883d | 2017-10-11 22:08:55 +1030 | [diff] [blame] | 170 | try: |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 171 | return run_all_commands(dbus_bus, descriptors[args.recipe], args) |
Andrew Jeffery | 0b6883d | 2017-10-11 22:08:55 +1030 | [diff] [blame] | 172 | finally: |
| 173 | dbus_bus.close() |
Andrew Jeffery | 44e6d9c | 2017-10-11 16:57:31 +1030 | [diff] [blame] | 174 | |
| 175 | if __name__ == "__main__": |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 176 | sys.exit(0 if main() else 1) |