summaryrefslogtreecommitdiffstats
path: root/scripts/gen_pimrules.py
blob: a529e2343b47c3565b0ecb629d802f0488ba3337 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python

'''Generate PIM rules for ipmi-fru-parser.
'''

import argparse
import os
import sys
import yaml
from mako.template import Template

tmpl = '''
description: >
    PIM rules for ipmi-fru-parser inventory objects.
events:

    - name: Host off at startup
      description: >
          Mark ipmi-fru-parser inventory items as cached when
          the host is off at startup.
      type: startup
      actions:
          - name: setProperty
            interface: ${cacheable_iface}
            property: ${cacheable_property}
            paths:
%for i in cacheable:
                - ${i}
%endfor
            value:
              type: ${cacheable_type}
              value: ${cacheable_cached}

    - name: Host off event
      description: >
          Mark ipmi-fru-parser inventory items as cached when
          the host goes away.
      type: match
      signatures:
          - type: signal
            interface: org.freedesktop.DBus.Properties
            path: ${off_path}
            member: PropertiesChanged
      filters:
          - name: propertyChangedTo
            interface: ${off_iface}
            property: ${off_property}
            value:
              value: ${off_value}
              type: ${off_type}
      actions:
          - name: setProperty
            interface: ${cacheable_iface}
            property: ${cacheable_property}
            paths:
%for i in cacheable:
                - ${i}
%endfor
            value:
              type: ${cacheable_type}
              value: ${cacheable_cached}

    - name: Host on at startup
      description: >
          Remove ipmi-fru-parser inventory items when the host is finished
          sending inventory items and the item is still marked as cached.
      type: startup
      filters:
          - name: propertyIs
            path: ${on_path}
            interface: ${on_iface}
            property: ${on_property}
            value:
              value: ${on_value}
              type: ${on_type}
      actions:
          - name: destroyObjects
            paths:
%for i in cacheable:
                - ${i}
%endfor
            conditions:
              - name: propertyIs
                interface: ${cacheable_iface}
                property: ${cacheable_property}
                value:
                  type: ${cacheable_type}
                  value: ${cacheable_cached}

    - name: Host on event
      description: >
          Remove ipmi-fru-parser inventory items when the host is finished
          sending inventory items and the item is still marked as cached.
      type: match
      signatures:
          - type: signal
            interface: org.freedesktop.DBus.Properties
            path: ${on_path}
            member: PropertiesChanged
      filters:
          - name: propertyChangedTo
            interface: ${on_iface}
            property: ${on_property}
            value:
              value: ${on_value}
              type: ${on_type}
      actions:
          - name: destroyObjects
            paths:
%for i in cacheable:
                - ${i}
%endfor
            conditions:
              - name: propertyIs
                interface: ${cacheable_iface}
                property: ${cacheable_property}
                value:
                  type: ${cacheable_type}
                  value: ${cacheable_cached}
'''


def get_cacheable_objs(yaml):
    cacheable = []

    for objdata in data.itervalues():
        if not isinstance(objdata, dict):
            continue
        for path, ifaces in objdata.iteritems():
            if not isinstance(ifaces, dict):
                continue

            if cacheable_iface_name in ifaces.keys():
                cacheable.append(path)

    return cacheable


if __name__ == '__main__':
    script_dir = os.path.dirname(os.path.realpath(__file__))

    parser = argparse.ArgumentParser(
        description='ipmi-fru-parser PIM rule generator.')
    parser.add_argument(
        '-o', '--output-dir', dest='outputdir',
        default='.', help='Output directory.')
    parser.add_argument(
        'inventory', metavar='INVENTORY', type=str,
        help='Path to inventory YAML.')

    args = parser.parse_args()

    with open(args.inventory, 'r') as fd:
        data = yaml.safe_load(fd.read())

    cacheable_iface_name = 'xyz.openbmc_project.Inventory.Decorator.Cacheable'
    target_file = os.path.join(args.outputdir, 'ipmi-fru-rules.yaml')
    cacheable = []

    if isinstance(data, dict):
        cacheable = get_cacheable_objs(data)
    if cacheable:
        with open(target_file, 'w') as out:
            out.write(
                Template(tmpl).render(
                    cacheable_iface=cacheable_iface_name,
                    cacheable_property='Cached',
                    cacheable_cached='true',
                    cacheable_type='boolean',
                    on_path='/xyz/openbmc_project/state/host0',
                    on_iface='xyz.openbmc_project.State.Boot.Progress',
                    on_property='BootProgress',
                    on_value='xyz.openbmc_project.State.Boot.Progress.ProgressStages.OSStart',
                    on_type='string',
                    off_path='/xyz/openbmc_project/state/host0',
                    off_iface='xyz.openbmc_project.State.Host',
                    off_property='CurrentHostState',
                    off_value='xyz.openbmc_project.State.Host.Off',
                    off_type='string',
                    cacheable=cacheable))


# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
OpenPOWER on IntegriCloud