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