|
| 1 | +# Copyright 2015 Red Hat, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 | +# implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import logging |
| 17 | + |
| 18 | +from oslo_concurrency import processutils |
| 19 | +from oslo_config import cfg |
| 20 | +from oslo_utils import excutils |
| 21 | +from oslo_utils import units |
| 22 | +import requests |
| 23 | +import stevedore |
| 24 | + |
| 25 | +from ironic_python_agent import encoding |
| 26 | +from ironic_python_agent import errors |
| 27 | +from ironic_python_agent import hardware |
| 28 | +from ironic_python_agent import utils |
| 29 | + |
| 30 | + |
| 31 | +LOG = logging.getLogger(__name__) |
| 32 | +CONF = cfg.CONF |
| 33 | +DEFAULT_COLLECTOR = 'default' |
| 34 | +_COLLECTOR_NS = 'ironic_python_agent.inspector.collectors' |
| 35 | + |
| 36 | + |
| 37 | +def extension_manager(names): |
| 38 | + try: |
| 39 | + return stevedore.NamedExtensionManager(_COLLECTOR_NS, |
| 40 | + names=names, |
| 41 | + name_order=True) |
| 42 | + except KeyError as exc: |
| 43 | + raise errors.InspectionError('Failed to load collector %s' % exc) |
| 44 | + |
| 45 | + |
| 46 | +def inspect(): |
| 47 | + """Optionally run inspection on the current node. |
| 48 | +
|
| 49 | + If ``inspection_callback_url`` is set in the configuration, get |
| 50 | + the hardware inventory from the node and post it back to the inspector. |
| 51 | +
|
| 52 | + :return: node UUID if inspection was successful, None if associated node |
| 53 | + was not found in inspector cache. None is also returned if |
| 54 | + inspector support is not enabled. |
| 55 | + """ |
| 56 | + if not CONF.inspection_callback_url: |
| 57 | + LOG.info('Inspection is disabled, skipping') |
| 58 | + return |
| 59 | + collector_names = [x.strip() for x in CONF.inspection_collectors.split(',') |
| 60 | + if x.strip()] |
| 61 | + LOG.info('inspection is enabled with collectors %s', collector_names) |
| 62 | + |
| 63 | + # NOTE(dtantsur): inspection process tries to delay raising any exceptions |
| 64 | + # until after we posted some data back to inspector. This is because |
| 65 | + # inspection is run automatically on (mostly) unknown nodes, so if it |
| 66 | + # fails, we don't have much information for debugging. |
| 67 | + failures = utils.AccumulatedFailures(exc_class=errors.InspectionError) |
| 68 | + data = {} |
| 69 | + |
| 70 | + try: |
| 71 | + ext_mgr = extension_manager(collector_names) |
| 72 | + collectors = [(ext.name, ext.plugin) for ext in ext_mgr] |
| 73 | + except Exception as exc: |
| 74 | + with excutils.save_and_reraise_exception(): |
| 75 | + failures.add(exc) |
| 76 | + call_inspector(data, failures) |
| 77 | + |
| 78 | + for name, collector in collectors: |
| 79 | + try: |
| 80 | + collector(data, failures) |
| 81 | + except Exception as exc: |
| 82 | + # No reraise here, try to keep going |
| 83 | + failures.add('collector %s failed: %s', name, exc) |
| 84 | + |
| 85 | + resp = call_inspector(data, failures) |
| 86 | + |
| 87 | + # Now raise everything we were delaying |
| 88 | + failures.raise_if_needed() |
| 89 | + |
| 90 | + if resp is None: |
| 91 | + LOG.info('stopping inspection, as inspector returned an error') |
| 92 | + return |
| 93 | + |
| 94 | + # Optionally update IPMI credentials |
| 95 | + setup_ipmi_credentials(resp) |
| 96 | + |
| 97 | + LOG.info('inspection finished successfully') |
| 98 | + return resp.get('uuid') |
| 99 | + |
| 100 | + |
| 101 | +def call_inspector(data, failures): |
| 102 | + """Post data to inspector.""" |
| 103 | + data['error'] = failures.get_error() |
| 104 | + |
| 105 | + LOG.info('posting collected data to %s', CONF.inspection_callback_url) |
| 106 | + LOG.debug('collected data: %s', data) |
| 107 | + |
| 108 | + encoder = encoding.RESTJSONEncoder() |
| 109 | + data = encoder.encode(data) |
| 110 | + |
| 111 | + resp = requests.post(CONF.inspection_callback_url, data=data) |
| 112 | + if resp.status_code >= 400: |
| 113 | + LOG.error('inspector error %d: %s, proceeding with lookup', |
| 114 | + resp.status_code, resp.content.decode('utf-8')) |
| 115 | + return |
| 116 | + |
| 117 | + return resp.json() |
| 118 | + |
| 119 | + |
| 120 | +def setup_ipmi_credentials(resp): |
| 121 | + """Setup IPMI credentials, if requested. |
| 122 | +
|
| 123 | + :param resp: JSON response from inspector. |
| 124 | + """ |
| 125 | + if not resp.get('ipmi_setup_credentials'): |
| 126 | + LOG.info('setting IPMI credentials was not requested') |
| 127 | + return |
| 128 | + |
| 129 | + user, password = resp['ipmi_username'], resp['ipmi_password'] |
| 130 | + LOG.debug('setting IPMI credentials: user %s', user) |
| 131 | + |
| 132 | + commands = [ |
| 133 | + ('user', 'set', 'name', '2', user), |
| 134 | + ('user', 'set', 'password', '2', password), |
| 135 | + ('user', 'enable', '2'), |
| 136 | + ('channel', 'setaccess', '1', '2', |
| 137 | + 'link=on', 'ipmi=on', 'callin=on', 'privilege=4'), |
| 138 | + ] |
| 139 | + |
| 140 | + for cmd in commands: |
| 141 | + try: |
| 142 | + utils.execute('ipmitool', *cmd) |
| 143 | + except processutils.ProcessExecutionError: |
| 144 | + LOG.exception('failed to update IPMI credentials') |
| 145 | + raise errors.InspectionError('failed to update IPMI credentials') |
| 146 | + |
| 147 | + LOG.info('successfully set IPMI credentials: user %s', user) |
| 148 | + |
| 149 | + |
| 150 | +def discover_network_properties(inventory, data, failures): |
| 151 | + """Discover network and BMC related properties. |
| 152 | +
|
| 153 | + Populates 'boot_interface', 'ipmi_address' and 'interfaces' keys. |
| 154 | + """ |
| 155 | + # Both boot interface and IPMI address might not be present, |
| 156 | + # we don't count it as failure |
| 157 | + data['boot_interface'] = utils.get_agent_params().get('BOOTIF') |
| 158 | + LOG.info('boot devices was %s', data['boot_interface']) |
| 159 | + data['ipmi_address'] = inventory.get('bmc_address') |
| 160 | + LOG.info('BMC IP address: %s', data['ipmi_address']) |
| 161 | + |
| 162 | + data.setdefault('interfaces', {}) |
| 163 | + for iface in inventory['interfaces']: |
| 164 | + if iface.name == 'lo' or iface.ipv4_address == '127.0.0.1': |
| 165 | + LOG.debug('ignoring local network interface %s', iface.name) |
| 166 | + continue |
| 167 | + |
| 168 | + LOG.debug('found network interface %s', iface.name) |
| 169 | + |
| 170 | + if not iface.mac_address: |
| 171 | + LOG.debug('no link information for interface %s', iface.name) |
| 172 | + continue |
| 173 | + |
| 174 | + if not iface.ipv4_address: |
| 175 | + LOG.debug('no IP address for interface %s', iface.name) |
| 176 | + |
| 177 | + data['interfaces'][iface.name] = {'mac': iface.mac_address, |
| 178 | + 'ip': iface.ipv4_address} |
| 179 | + |
| 180 | + if data['interfaces']: |
| 181 | + LOG.info('network interfaces: %s', data['interfaces']) |
| 182 | + else: |
| 183 | + failures.add('no network interfaces found') |
| 184 | + |
| 185 | + |
| 186 | +def discover_scheduling_properties(inventory, data): |
| 187 | + data['cpus'] = inventory['cpu'].count |
| 188 | + data['cpu_arch'] = inventory['cpu'].architecture |
| 189 | + data['memory_mb'] = inventory['memory'].physical_mb |
| 190 | + |
| 191 | + # Replicate the same logic as in deploy. This logic will be moved to |
| 192 | + # inspector itself, but we need it for backward compatibility. |
| 193 | + try: |
| 194 | + disk = utils.guess_root_disk(inventory['disks']) |
| 195 | + except errors.DeviceNotFound: |
| 196 | + LOG.warn('no suitable root device detected') |
| 197 | + else: |
| 198 | + # -1 is required to give Ironic some spacing for partitioning |
| 199 | + data['local_gb'] = disk.size / units.Gi - 1 |
| 200 | + |
| 201 | + for key in ('cpus', 'local_gb', 'memory_mb'): |
| 202 | + try: |
| 203 | + data[key] = int(data[key]) |
| 204 | + except (KeyError, ValueError, TypeError): |
| 205 | + LOG.warn('value for %s is missing or malformed: %s', |
| 206 | + key, data.get(key)) |
| 207 | + else: |
| 208 | + LOG.info('value for %s is %s', key, data[key]) |
| 209 | + |
| 210 | + |
| 211 | +def collect_default(data, failures): |
| 212 | + inventory = hardware.dispatch_to_managers('list_hardware_info') |
| 213 | + # These 2 calls are required for backward compatibility and should be |
| 214 | + # dropped after inspector is ready (probably in Mitaka cycle). |
| 215 | + discover_network_properties(inventory, data, failures) |
| 216 | + discover_scheduling_properties(inventory, data) |
| 217 | + # In the future we will only need the current version of inventory, |
| 218 | + # everything else will be done by inspector itself and its plugins |
| 219 | + data['inventory'] = inventory |
0 commit comments