blob: 5b82cdcd0fbc553a83eb0ca6aa2b4109bfe814af [file] [log] [blame]
Patrick Williams09682372020-04-03 15:10:31 -05001#!/usr/bin/env python3
Brad Bishopcc103c42017-01-28 15:58:11 -05002
Patrick Williamsc5c17372022-12-08 06:18:06 -06003"""Generate PIM rules for ipmi-fru-parser.
4"""
Brad Bishopcc103c42017-01-28 15:58:11 -05005
6import argparse
7import os
8import sys
Patrick Williamsc5c17372022-12-08 06:18:06 -06009
Brad Bishopcc103c42017-01-28 15:58:11 -050010import yaml
11from mako.template import Template
12
Patrick Williamsc5c17372022-12-08 06:18:06 -060013tmpl = """
Brad Bishopcc103c42017-01-28 15:58:11 -050014description: >
15 PIM rules for ipmi-fru-parser inventory objects.
16events:
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 Williamsc5c17372022-12-08 06:18:06 -0600121"""
Brad Bishopcc103c42017-01-28 15:58:11 -0500122
123
124def get_cacheable_objs(yaml):
125 cacheable = []
126
Patrick Williams09682372020-04-03 15:10:31 -0500127 for objdata in data.values():
Brad Bishopcc103c42017-01-28 15:58:11 -0500128 if not isinstance(objdata, dict):
129 continue
Patrick Williams09682372020-04-03 15:10:31 -0500130 for path, ifaces in objdata.items():
Brad Bishopcc103c42017-01-28 15:58:11 -0500131 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 Williamsc5c17372022-12-08 06:18:06 -0600140if __name__ == "__main__":
Brad Bishopcc103c42017-01-28 15:58:11 -0500141 script_dir = os.path.dirname(os.path.realpath(__file__))
142
143 parser = argparse.ArgumentParser(
Patrick Williamsc5c17372022-12-08 06:18:06 -0600144 description="ipmi-fru-parser PIM rule generator."
145 )
Brad Bishopcc103c42017-01-28 15:58:11 -0500146 parser.add_argument(
Patrick Williamsc5c17372022-12-08 06:18:06 -0600147 "-o",
148 "--output-dir",
149 dest="outputdir",
150 default=".",
151 help="Output directory.",
152 )
Brad Bishopcc103c42017-01-28 15:58:11 -0500153 parser.add_argument(
Patrick Williamsc5c17372022-12-08 06:18:06 -0600154 "inventory",
155 metavar="INVENTORY",
156 type=str,
157 help="Path to inventory YAML.",
158 )
Brad Bishopcc103c42017-01-28 15:58:11 -0500159
160 args = parser.parse_args()
161
Patrick Williamsc5c17372022-12-08 06:18:06 -0600162 with open(args.inventory, "r") as fd:
Brad Bishopcc103c42017-01-28 15:58:11 -0500163 data = yaml.safe_load(fd.read())
164
Patrick Williamsc5c17372022-12-08 06:18:06 -0600165 cacheable_iface_name = "xyz.openbmc_project.Inventory.Decorator.Cacheable"
166 target_file = os.path.join(args.outputdir, "ipmi-fru-rules.yaml")
Brad Bishopcc103c42017-01-28 15:58:11 -0500167 cacheable = []
168
169 if isinstance(data, dict):
170 cacheable = get_cacheable_objs(data)
171 if cacheable:
Patrick Williamsc5c17372022-12-08 06:18:06 -0600172 with open(target_file, "w") as out:
Brad Bishopcc103c42017-01-28 15:58:11 -0500173 out.write(
174 Template(tmpl).render(
175 cacheable_iface=cacheable_iface_name,
Patrick Williamsc5c17372022-12-08 06:18:06 -0600176 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 Bishopcc103c42017-01-28 15:58:11 -0500192
193
194# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4