~lamont/maas/bug-1599223-2.0

« back to all changes in this revision

Viewing changes to src/metadataserver/models/commissioningscript.py

  • Committer: LaMont Jones
  • Date: 2016-05-12 19:07:37 UTC
  • mfrom: (5017 maas)
  • mto: This revision was merged to the branch mainline in revision 5021.
  • Revision ID: lamont@canonical.com-20160512190737-00g34satnuo0tk8v
mergeĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
import logging
20
20
import math
21
21
import os.path
 
22
import re
22
23
import tarfile
23
24
from time import time as now
24
25
 
29
30
)
30
31
from lxml import etree
31
32
from maasserver.models import Fabric
 
33
from maasserver.models.blockdevice import MIN_BLOCK_DEVICE_SIZE
32
34
from maasserver.models.interface import PhysicalInterface
33
35
from maasserver.models.physicalblockdevice import PhysicalBlockDevice
34
36
from maasserver.models.tag import Tag
175
177
    else:
176
178
        # Same document, many queries: use XPathEvaluator.
177
179
        evaluator = etree.XPathEvaluator(doc)
178
 
        cpu_count = evaluator(_xpath_processor_count)
 
180
        cpu_count = evaluator(_xpath_processor_count) or 0
179
181
        memory = evaluator(_xpath_memory_bytes)
180
182
        if not memory or math.isnan(memory):
181
183
            memory = 0
182
 
        node.cpu_count = cpu_count or 0
 
184
        # XXX ltrager 2016-05-09 - Work around for LP:1579996. On some
 
185
        # CPU's lshw doesn't detect all CPU cores. MAAS captures and
 
186
        # processes /proc/cpuinfo so MAAS chooses the highest number.
 
187
        if node.cpu_count is None or cpu_count > node.cpu_count:
 
188
            node.cpu_count = cpu_count
183
189
        node.memory = memory
184
190
        node.save()
185
191
 
186
192
 
 
193
def parse_cpuinfo(node, output, exit_status):
 
194
    """Parse the output of /proc/cpuinfo."""
 
195
    assert isinstance(output, bytes)
 
196
    if exit_status != 0:
 
197
        return
 
198
    decoded_output = output.decode('ascii')
 
199
    cpu_count = len([
 
200
        m.start()
 
201
        for m in re.finditer('processor\t:', decoded_output)
 
202
    ])
 
203
    if node.cpu_count is None or cpu_count > node.cpu_count:
 
204
        node.cpu_count = cpu_count
 
205
        node.save()
 
206
 
 
207
 
187
208
def set_virtual_tag(node, output, exit_status):
188
209
    """Process the results of `VIRTUALITY_SCRIPT`.
189
210
 
312
333
            block_device.tags = tags
313
334
            block_device.save()
314
335
        else:
 
336
            # MAAS doesn't allow disks smaller than 4MiB so skip them
 
337
            if size <= MIN_BLOCK_DEVICE_SIZE:
 
338
                continue
 
339
            # Skip loopback devices as they won't be available on next boot
 
340
            if id_path.startswith('/dev/loop'):
 
341
                continue
 
342
 
 
343
            # First check if there is an existing device with the same name.
 
344
            # If so, we need to rename it. Its name will be changed back later,
 
345
            # when we loop around to it.
 
346
            existing = PhysicalBlockDevice.objects.filter(
 
347
                node=node, name=name).all()
 
348
            for device in existing:
 
349
                # Use the device ID to ensure a unique temporary name.
 
350
                device.name = "%s.%d" % (device.name, device.id)
 
351
                device.save()
315
352
            # New block device. Create it on the node.
316
353
            PhysicalBlockDevice.objects.create(
317
354
                node=node,
345
382
 
346
383
# Register the post processing hooks.
347
384
NODE_INFO_SCRIPTS[LSHW_OUTPUT_NAME]['hook'] = update_hardware_details
 
385
NODE_INFO_SCRIPTS['00-maas-01-cpuinfo.out']['hook'] = parse_cpuinfo
348
386
NODE_INFO_SCRIPTS['00-maas-02-virtuality.out']['hook'] = set_virtual_tag
349
387
NODE_INFO_SCRIPTS['00-maas-07-block-devices.out']['hook'] = (
350
388
    update_node_physical_block_devices)