Brad Bishop | cc103c4 | 2017-01-28 15:58:11 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | '''Generate PIM rules for ipmi-fru-parser. |
| 4 | ''' |
| 5 | |
| 6 | import argparse |
| 7 | import os |
| 8 | import sys |
| 9 | import yaml |
| 10 | from mako.template import Template |
| 11 | |
| 12 | tmpl = ''' |
| 13 | description: > |
| 14 | PIM rules for ipmi-fru-parser inventory objects. |
| 15 | events: |
| 16 | |
| 17 | - name: Host off at startup |
| 18 | description: > |
| 19 | Mark ipmi-fru-parser inventory items as cached when |
| 20 | the host is off at startup. |
| 21 | type: startup |
| 22 | actions: |
| 23 | - name: setProperty |
| 24 | interface: ${cacheable_iface} |
| 25 | property: ${cacheable_property} |
| 26 | paths: |
| 27 | %for i in cacheable: |
| 28 | - ${i} |
| 29 | %endfor |
| 30 | value: |
| 31 | type: ${cacheable_type} |
| 32 | value: ${cacheable_cached} |
| 33 | |
| 34 | - name: Host off event |
| 35 | description: > |
| 36 | Mark ipmi-fru-parser inventory items as cached when |
| 37 | the host goes away. |
| 38 | type: match |
| 39 | signatures: |
| 40 | - type: signal |
| 41 | interface: org.freedesktop.DBus.Properties |
| 42 | path: ${off_path} |
| 43 | member: PropertiesChanged |
| 44 | filters: |
| 45 | - name: propertyChangedTo |
| 46 | interface: ${off_iface} |
| 47 | property: ${off_property} |
| 48 | value: |
| 49 | value: ${off_value} |
| 50 | type: ${off_type} |
| 51 | actions: |
| 52 | - name: setProperty |
| 53 | interface: ${cacheable_iface} |
| 54 | property: ${cacheable_property} |
| 55 | paths: |
| 56 | %for i in cacheable: |
| 57 | - ${i} |
| 58 | %endfor |
| 59 | value: |
| 60 | type: ${cacheable_type} |
| 61 | value: ${cacheable_cached} |
| 62 | |
| 63 | - name: Host on at startup |
| 64 | description: > |
| 65 | Remove ipmi-fru-parser inventory items when the host is finished |
| 66 | sending inventory items and the item is still marked as cached. |
| 67 | type: startup |
| 68 | filters: |
| 69 | - name: propertyIs |
| 70 | path: ${on_path} |
| 71 | interface: ${on_iface} |
| 72 | property: ${on_property} |
| 73 | value: |
| 74 | value: ${on_value} |
| 75 | type: ${on_type} |
| 76 | actions: |
| 77 | - name: destroyObjects |
| 78 | paths: |
| 79 | %for i in cacheable: |
| 80 | - ${i} |
| 81 | %endfor |
| 82 | conditions: |
| 83 | - name: propertyIs |
| 84 | interface: ${cacheable_iface} |
| 85 | property: ${cacheable_property} |
| 86 | value: |
| 87 | type: ${cacheable_type} |
| 88 | value: ${cacheable_cached} |
| 89 | |
| 90 | - name: Host on event |
| 91 | description: > |
| 92 | Remove ipmi-fru-parser inventory items when the host is finished |
| 93 | sending inventory items and the item is still marked as cached. |
| 94 | type: match |
| 95 | signatures: |
| 96 | - type: signal |
| 97 | interface: org.freedesktop.DBus.Properties |
| 98 | path: ${on_path} |
| 99 | member: PropertiesChanged |
| 100 | filters: |
| 101 | - name: propertyChangedTo |
| 102 | interface: ${on_iface} |
| 103 | property: ${on_property} |
| 104 | value: |
| 105 | value: ${on_value} |
| 106 | type: ${on_type} |
| 107 | actions: |
| 108 | - name: destroyObjects |
| 109 | paths: |
| 110 | %for i in cacheable: |
| 111 | - ${i} |
| 112 | %endfor |
| 113 | conditions: |
| 114 | - name: propertyIs |
| 115 | interface: ${cacheable_iface} |
| 116 | property: ${cacheable_property} |
| 117 | value: |
| 118 | type: ${cacheable_type} |
| 119 | value: ${cacheable_cached} |
| 120 | ''' |
| 121 | |
| 122 | |
| 123 | def get_cacheable_objs(yaml): |
| 124 | cacheable = [] |
| 125 | |
| 126 | for objdata in data.itervalues(): |
| 127 | if not isinstance(objdata, dict): |
| 128 | continue |
| 129 | for path, ifaces in objdata.iteritems(): |
| 130 | if not isinstance(ifaces, dict): |
| 131 | continue |
| 132 | |
| 133 | if cacheable_iface_name in ifaces.keys(): |
| 134 | cacheable.append(path) |
| 135 | |
| 136 | return cacheable |
| 137 | |
| 138 | |
| 139 | if __name__ == '__main__': |
| 140 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 141 | |
| 142 | parser = argparse.ArgumentParser( |
| 143 | description='ipmi-fru-parser PIM rule generator.') |
| 144 | parser.add_argument( |
| 145 | '-o', '--output-dir', dest='outputdir', |
| 146 | default='.', help='Output directory.') |
| 147 | parser.add_argument( |
| 148 | 'inventory', metavar='INVENTORY', type=str, |
| 149 | help='Path to inventory YAML.') |
| 150 | |
| 151 | args = parser.parse_args() |
| 152 | |
| 153 | with open(args.inventory, 'r') as fd: |
| 154 | data = yaml.safe_load(fd.read()) |
| 155 | |
| 156 | cacheable_iface_name = 'xyz.openbmc_project.Inventory.Decorator.Cacheable' |
| 157 | target_file = os.path.join(args.outputdir, 'ipmi-fru-rules.yaml') |
| 158 | cacheable = [] |
| 159 | |
| 160 | if isinstance(data, dict): |
| 161 | cacheable = get_cacheable_objs(data) |
| 162 | if cacheable: |
| 163 | with open(target_file, 'w') as out: |
| 164 | out.write( |
| 165 | Template(tmpl).render( |
| 166 | cacheable_iface=cacheable_iface_name, |
| 167 | cacheable_property='Cached', |
| 168 | cacheable_cached='true', |
| 169 | cacheable_type='boolean', |
Adriana Kobylak | 25fbd43 | 2018-08-07 13:54:39 -0500 | [diff] [blame] | 170 | on_path='/xyz/openbmc_project/state/host0', |
| 171 | on_iface='xyz.openbmc_project.State.Boot.Progress', |
| 172 | on_property='BootProgress', |
| 173 | on_value='xyz.openbmc_project.State.Boot.Progress.ProgressStages.OSStart', |
Brad Bishop | cc103c4 | 2017-01-28 15:58:11 -0500 | [diff] [blame] | 174 | on_type='string', |
| 175 | off_path='/xyz/openbmc_project/state/host0', |
| 176 | off_iface='xyz.openbmc_project.State.Host', |
| 177 | off_property='CurrentHostState', |
| 178 | off_value='xyz.openbmc_project.State.Host.Off', |
| 179 | off_type='string', |
| 180 | cacheable=cacheable)) |
| 181 | |
| 182 | |
| 183 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |