~lamont/maas/bug-1614584

« back to all changes in this revision

Viewing changes to src/provisioningserver/drivers/power/__init__.py

  • Committer: MAAS Lander
  • Author(s): Blake Rouse
  • Date: 2016-11-30 15:12:47 UTC
  • mfrom: (5570.1.7 reorg-power-drivers)
  • Revision ID: maas_lander-20161130151247-bkx57gqe5auighsd
[r=newell-jensen][bug=][author=blake-rouse] Remove the src/provisioningserver/power/schema.py. Place the schema for each driver in the driver class. This makes it easier to add drivers without requiring code in two places.

Remove all the diskless code, as that used some of the old schema stuff and it was never used.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
import sys
27
27
 
28
28
from jsonschema import validate
29
 
from provisioningserver.drivers import (
30
 
    JSON_SETTING_SCHEMA,
31
 
    validate_settings,
32
 
)
 
29
from provisioningserver.drivers import JSON_SETTING_SCHEMA
33
30
from provisioningserver.utils.registry import Registry
34
31
from provisioningserver.utils.twisted import pause
35
32
from twisted.internet import reactor
39
36
)
40
37
from twisted.internet.threads import deferToThread
41
38
 
 
39
# We specifically declare this here so that a node not knowing its own
 
40
# powertype won't fail to enlist. However, we don't want it in the list
 
41
# of power types since setting a node's power type to "I don't know"
 
42
# from another type doens't make any sense.
 
43
UNKNOWN_POWER_TYPE = ''
 
44
 
42
45
# A policy used when waiting between retries of power changes.
43
46
DEFAULT_WAITING_POLICY = (1, 2, 2, 4, 6, 8, 12)
44
47
 
114
117
 
115
118
    def __init__(self):
116
119
        super(PowerDriverBase, self).__init__()
117
 
        validate_settings(self.get_schema())
 
120
        validate(
 
121
            self.get_schema(detect_missing_packages=False),
 
122
            JSON_SETTING_SCHEMA)
118
123
 
119
124
    @abstractproperty
120
125
    def name(self):
133
138
        to the driver to read these options before performing the operation.
134
139
        """
135
140
 
 
141
    @abstractproperty
 
142
    def ip_extractor(self):
 
143
        """IP extractor.
 
144
 
 
145
        Name of the settings field and python REGEX pattern for extracting IP
 
146
        the address from the value.
 
147
        """
 
148
 
 
149
    @abstractproperty
 
150
    def queryable(self):
 
151
        """Whether or not the power driver is queryable."""
 
152
 
 
153
    @abstractmethod
 
154
    def detect_missing_packages(self):
 
155
        """Implement this method for the actual implementation
 
156
        of the check for the driver's missing support packages.
 
157
        """
 
158
 
136
159
    @abstractmethod
137
160
    def on(self, system_id, context):
138
161
        """Perform the power on action for `system_id`.
169
192
            calling function should ignore this error, and continue on.
170
193
        """
171
194
 
172
 
    def get_schema(self):
173
 
        """Returns the JSON schema for the driver."""
174
 
        return dict(
 
195
    def get_schema(self, detect_missing_packages=True):
 
196
        """Returns the JSON schema for the driver.
 
197
 
 
198
        Calculates the missing packages on each invoke.
 
199
        """
 
200
        schema = dict(
175
201
            name=self.name, description=self.description,
176
 
            fields=self.settings)
 
202
            fields=self.settings, queryable=self.queryable,
 
203
            missing_packages=(
 
204
                self.detect_missing_packages()
 
205
                if detect_missing_packages else []))
 
206
        if self.ip_extractor is not None:
 
207
            schema['ip_extractor'] = self.ip_extractor
 
208
        return schema
 
209
 
 
210
    def get_setting(self, name):
 
211
        """Return the setting field by its name."""
 
212
        for setting in self.settings:
 
213
            if setting['name'] == name:
 
214
                return setting
 
215
        return None
177
216
 
178
217
 
179
218
def get_error_message(err):
196
235
    """Default power driver logic."""
197
236
 
198
237
    wait_time = DEFAULT_WAITING_POLICY
 
238
    queryable = True
199
239
 
200
240
    def __init__(self, clock=reactor):
201
241
        self.clock = reactor
202
242
 
203
243
    @abstractmethod
204
 
    def detect_missing_packages(self):
205
 
        """Implement this method for the actual implementation
206
 
        of the check for the driver's missing support packages.
207
 
        """
208
 
 
209
 
    @abstractmethod
210
244
    def power_on(self, system_id, context):
211
245
        """Implement this method for the actual implementation
212
246
        of the power on command.
330
364
    """Registry for power drivers."""
331
365
 
332
366
    @classmethod
333
 
    def get_schema(cls):
 
367
    def get_schema(cls, detect_missing_packages=True):
334
368
        """Returns the full schema for the registry."""
335
 
        schemas = [drivers.get_schema() for _, drivers in cls]
 
369
        schemas = [
 
370
            driver.get_schema(detect_missing_packages=detect_missing_packages)
 
371
            for _, driver in cls
 
372
        ]
336
373
        validate(schemas, JSON_POWER_DRIVERS_SCHEMA)
337
374
        return schemas
338
375
 
374
411
]
375
412
for driver in power_drivers:
376
413
    PowerDriverRegistry.register_item(driver.name, driver)
377
 
 
378
 
power_drivers_by_name = {
379
 
    d.name: d for d in power_drivers
380
 
}