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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
|
# Copyright 2019 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import functools
import json
import os
import shutil
import tempfile
from urllib import parse as urlparse
from ironic_lib import utils as ironic_utils
from oslo_log import log
from ironic.common import exception
from ironic.common.i18n import _
from ironic.common import images
from ironic.common import swift
from ironic.conf import CONF
from ironic.drivers.modules import boot_mode_utils
from ironic.drivers.modules import deploy_utils
LOG = log.getLogger(__name__)
class ImageHandler(object):
_SWIFT_MAP = {
"redfish": {
"swift_enabled": CONF.redfish.use_swift,
"container": CONF.redfish.swift_container,
"timeout": CONF.redfish.swift_object_expiry_timeout,
"image_subdir": "redfish",
"file_permission": CONF.redfish.file_permission,
"kernel_params": CONF.redfish.kernel_append_params
},
"idrac": {
"swift_enabled": CONF.redfish.use_swift,
"container": CONF.redfish.swift_container,
"timeout": CONF.redfish.swift_object_expiry_timeout,
"image_subdir": "redfish",
"file_permission": CONF.redfish.file_permission,
"kernel_params": CONF.redfish.kernel_append_params
},
"ilo5": {
"swift_enabled": not CONF.ilo.use_web_server_for_images,
"container": CONF.ilo.swift_ilo_container,
"timeout": CONF.ilo.swift_object_expiry_timeout,
"image_subdir": "ilo",
"file_permission": CONF.ilo.file_permission,
"kernel_params": CONF.pxe.pxe_append_params
},
"ilo": {
"swift_enabled": not CONF.ilo.use_web_server_for_images,
"container": CONF.ilo.swift_ilo_container,
"timeout": CONF.ilo.swift_object_expiry_timeout,
"image_subdir": "ilo",
"file_permission": CONF.ilo.file_permission,
"kernel_params": CONF.pxe.pxe_append_params
},
}
def __init__(self, driver):
self._driver = driver
self._container = self._SWIFT_MAP[driver].get("container")
self._timeout = self._SWIFT_MAP[driver].get("timeout")
self._image_subdir = self._SWIFT_MAP[driver].get("image_subdir")
self._file_permission = self._SWIFT_MAP[driver].get("file_permission")
# To get the kernel parameters
self.kernel_params = self._SWIFT_MAP[driver].get("kernel_params")
def _is_swift_enabled(self):
try:
return self._SWIFT_MAP[self._driver].get("swift_enabled")
except KeyError:
return False
def unpublish_image(self, object_name):
"""Withdraw the image previously made downloadable.
Depending on ironic settings, removes previously published file
from where it has been published - Swift or local HTTP server's
document root.
:param object_name: name of the published file (optional)
"""
if self._is_swift_enabled():
container = self._container
swift_api = swift.SwiftAPI()
LOG.debug("Cleaning up image %(name)s from Swift container "
"%(container)s", {'name': object_name,
'container': container})
try:
swift_api.delete_object(container, object_name)
except exception.SwiftOperationError as exc:
LOG.warning("Failed to clean up image %(image)s. Error: "
"%(error)s.", {'image': object_name,
'error': exc})
else:
published_file = os.path.join(
CONF.deploy.http_root, self._image_subdir, object_name)
ironic_utils.unlink_without_raise(published_file)
def _append_filename_param(self, url, filename):
"""Append 'filename=<file>' parameter to given URL.
Some BMCs seem to validate boot image URL requiring the URL to end
with something resembling ISO image file name.
This function tries to add, hopefully, meaningless 'filename'
parameter to URL's query string in hope to make the entire boot image
URL looking more convincing to the BMC.
However, `url` with fragments might not get cured by this hack.
:param url: a URL to work on
:param filename: name of the file to append to the URL
:returns: original URL with 'filename' parameter appended
"""
parsed_url = urlparse.urlparse(url)
parsed_qs = urlparse.parse_qsl(parsed_url.query)
has_filename = [x for x in parsed_qs if x[0].lower() == 'filename']
if has_filename:
return url
parsed_qs.append(('filename', filename))
parsed_url = list(parsed_url)
parsed_url[4] = urlparse.urlencode(parsed_qs)
return urlparse.urlunparse(parsed_url)
def publish_image(self, image_file, object_name):
"""Make image file downloadable.
Depending on ironic settings, pushes given file into Swift or copies
it over to local HTTP server's document root and returns publicly
accessible URL leading to the given file.
:param image_file: path to file to publish
:param object_name: name of the published file
:return: a URL to download published file
"""
if self._is_swift_enabled():
container = self._container
timeout = self._timeout
object_headers = {'X-Delete-After': str(timeout)}
swift_api = swift.SwiftAPI()
swift_api.create_object(container, object_name, image_file,
object_headers=object_headers)
image_url = swift_api.get_temp_url(container, object_name, timeout)
else:
public_dir = os.path.join(CONF.deploy.http_root,
self._image_subdir)
if not os.path.exists(public_dir):
os.mkdir(public_dir, 0o755)
published_file = os.path.join(public_dir, object_name)
try:
os.link(image_file, published_file)
os.chmod(image_file, self._file_permission)
except OSError as exc:
LOG.debug(
"Could not hardlink image file %(image)s to public "
"location %(public)s (will copy it over): "
"%(error)s", {'image': image_file,
'public': published_file,
'error': exc})
shutil.copyfile(image_file, published_file)
os.chmod(published_file, self._file_permission)
image_url = os.path.join(
CONF.deploy.http_url, self._image_subdir, object_name)
image_url = self._append_filename_param(
image_url, os.path.basename(image_file))
return image_url
def _get_floppy_image_name(node):
"""Returns the floppy image name for a given node.
:param node: the node for which image name is to be provided.
"""
return "image-%s" % node.uuid
def _get_iso_image_name(node):
"""Returns the boot iso image name for a given node.
:param node: the node for which image name is to be provided.
"""
return "boot-%s.iso" % node.uuid
def cleanup_iso_image(task):
"""Deletes the ISO if it was created for the instance.
:param task: A task from TaskManager.
"""
iso_object_name = _get_iso_image_name(task.node)
img_handler = ImageHandler(task.node.driver)
img_handler.unpublish_image(iso_object_name)
def prepare_floppy_image(task, params=None):
"""Prepares the floppy image for passing the parameters.
This method prepares a temporary VFAT filesystem image and adds
a file into the image which contains parameters to be passed to
the ramdisk. Then this method uploads built image to Swift
'[redfish]swift_container', setting it to auto expire after
'[redfish]swift_object_expiry_timeout' seconds. Finally, a
temporary Swift URL is returned addressing Swift object just
created.
:param task: a TaskManager instance containing the node to act on.
:param params: a dictionary containing 'parameter name'->'value'
mapping to be passed to deploy or rescue image via floppy image.
:raises: ImageCreationFailed, if it failed while creating the floppy
image.
:raises: SwiftOperationError, if any operation with Swift fails.
:returns: image URL for the floppy image.
"""
object_name = _get_floppy_image_name(task.node)
LOG.debug("Trying to create floppy image for node "
"%(node)s", {'node': task.node.uuid})
with tempfile.NamedTemporaryFile(
dir=CONF.tempdir, suffix='.img') as vfat_image_tmpfile_obj:
vfat_image_tmpfile = vfat_image_tmpfile_obj.name
images.create_vfat_image(vfat_image_tmpfile, parameters=params)
img_handler = ImageHandler(task.node.driver)
image_url = img_handler.publish_image(vfat_image_tmpfile, object_name)
LOG.debug("Created floppy image %(name)s in Swift for node %(node)s, "
"exposed as temporary URL "
"%(url)s", {'node': task.node.uuid,
'name': object_name,
'url': image_url})
return image_url
def cleanup_floppy_image(task):
"""Deletes the floppy image if it was created for the node.
:param task: an ironic node object.
"""
floppy_object_name = _get_floppy_image_name(task.node)
img_handler = ImageHandler(task.node.driver)
img_handler.unpublish_image(floppy_object_name)
def _prepare_iso_image(task, kernel_href, ramdisk_href,
bootloader_href=None, root_uuid=None, params=None,
base_iso=None, inject_files=None):
"""Prepare an ISO to boot the node.
Build bootable ISO out of `kernel_href` and `ramdisk_href` (and
`bootloader` if it's UEFI boot), then push built image up to Swift and
return a temporary URL.
:param task: a TaskManager instance containing the node to act on.
:param kernel_href: URL or Glance UUID of the kernel to use
:param ramdisk_href: URL or Glance UUID of the ramdisk to use
:param bootloader_href: URL or Glance UUID of the EFI bootloader
image to use when creating UEFI bootbable ISO
:param root_uuid: optional uuid of the root partition.
:param params: a dictionary containing 'parameter name'->'value'
mapping to be passed to kernel command line.
:param inject_files: Mapping of local source file paths to their location
on the final ISO image.
:returns: bootable ISO HTTP URL.
:raises: MissingParameterValue, if any of the required parameters are
missing.
:raises: InvalidParameterValue, if any of the parameters have invalid
value.
:raises: ImageCreationFailed, if creating ISO image failed.
"""
if (not kernel_href or not ramdisk_href) and not base_iso:
raise exception.InvalidParameterValue(_(
"Unable to find kernel, ramdisk for "
"building ISO, or explicit ISO for %(node)s") %
{'node': task.node.uuid})
img_handler = ImageHandler(task.node.driver)
k_param = img_handler.kernel_params
i_info = task.node.instance_info
# NOTE(TheJulia): Until we support modifying a base iso, most of
# this logic actually does nothing in the end. But it should!
if deploy_utils.get_boot_option(task.node) == "ramdisk":
if not base_iso:
kernel_params = "root=/dev/ram0 text "
kernel_params += i_info.get("ramdisk_kernel_arguments", "")
else:
kernel_params = None
else:
kernel_params = i_info.get('kernel_append_params', k_param)
if params and not base_iso:
kernel_params = ' '.join(
(kernel_params, ' '.join(
'%s=%s' % kv for kv in params.items())))
boot_mode = boot_mode_utils.get_boot_mode(task.node)
LOG.debug("Trying to create %(boot_mode)s ISO image for node %(node)s "
"with kernel %(kernel_href)s, ramdisk %(ramdisk_href)s, "
"bootloader %(bootloader_href)s and kernel params %(params)s"
"", {'node': task.node.uuid,
'boot_mode': boot_mode,
'kernel_href': kernel_href,
'ramdisk_href': ramdisk_href,
'bootloader_href': bootloader_href,
'params': kernel_params})
with tempfile.NamedTemporaryFile(
dir=CONF.tempdir, suffix='.iso') as boot_fileobj:
boot_iso_tmp_file = boot_fileobj.name
images.create_boot_iso(
task.context, boot_iso_tmp_file,
kernel_href, ramdisk_href,
esp_image_href=bootloader_href,
root_uuid=root_uuid,
kernel_params=kernel_params,
boot_mode=boot_mode,
base_iso=base_iso,
inject_files=inject_files)
iso_object_name = _get_iso_image_name(task.node)
image_url = img_handler.publish_image(
boot_iso_tmp_file, iso_object_name)
LOG.debug("Created ISO %(name)s in object store for node %(node)s, "
"exposed as temporary URL "
"%(url)s", {'node': task.node.uuid,
'name': iso_object_name,
'url': image_url})
return image_url
def _find_param(param_str, param_dict):
val = None
for param_key in param_dict:
if param_str in param_key:
val = param_dict.get(param_key)
return val
def prepare_deploy_iso(task, params, mode, d_info):
"""Prepare deploy or rescue ISO image
Build bootable ISO out of
`[driver_info]/deploy_kernel`/`[driver_info]/deploy_ramdisk` or
`[driver_info]/rescue_kernel`/`[driver_info]/rescue_ramdisk`
and `[driver_info]/bootloader`, then push built image up to Glance
and return temporary Swift URL to the image.
If network interface supplies network configuration (`network_data`),
a `network_data.json` will be written into an appropriate location on
the final ISO.
:param task: a TaskManager instance containing the node to act on.
:param params: a dictionary containing 'parameter name'->'value'
mapping to be passed to kernel command line.
:param mode: either 'deploy' or 'rescue'.
:param d_info: Deployment information of the node
:returns: bootable ISO HTTP URL.
:raises: MissingParameterValue, if any of the required parameters are
missing.
:raises: InvalidParameterValue, if any of the parameters have invalid
value.
:raises: ImageCreationFailed, if creating ISO image failed.
"""
kernel_str = '%s_kernel' % mode
ramdisk_str = '%s_ramdisk' % mode
bootloader_str = 'bootloader'
kernel_href = _find_param(kernel_str, d_info)
ramdisk_href = _find_param(ramdisk_str, d_info)
bootloader_href = _find_param(bootloader_str, d_info)
# TODO(TheJulia): At some point we should support something like
# boot_iso for the deploy interface, perhaps when we support config
# injection.
prepare_iso_image = functools.partial(
_prepare_iso_image, task, kernel_href, ramdisk_href,
bootloader_href=bootloader_href, params=params)
inject_files = {}
network_data = task.driver.network.get_node_network_data(task)
if network_data:
LOG.debug('Injecting custom network data for node %s',
task.node.uuid)
network_data = json.dumps(network_data, indent=2).encode('utf-8')
inject_files[network_data] = (
'openstack/latest/network_data.json'
)
return prepare_iso_image(inject_files=inject_files)
def prepare_boot_iso(task, d_info, root_uuid=None):
"""Prepare boot ISO image
Build bootable ISO out of `[instance_info]/kernel`,
`[instance_info]/ramdisk` and `[driver_info]/bootloader` if present.
Otherwise, read `kernel_id` and `ramdisk_id` from
`[instance_info]/image_source` Glance image metadata.
Push produced ISO image up to Glance and return temporary Swift
URL to the image.
:param task: a TaskManager instance containing the node to act on.
:param d_info: Deployment information of the node
:param root_uuid: Root UUID
:returns: bootable ISO HTTP URL.
:raises: MissingParameterValue, if any of the required parameters are
missing.
:raises: InvalidParameterValue, if any of the parameters have invalid
value.
:raises: ImageCreationFailed, if creating ISO image failed.
"""
node = task.node
kernel_href = node.instance_info.get('kernel')
ramdisk_href = node.instance_info.get('ramdisk')
base_iso = node.instance_info.get('boot_iso')
if (not kernel_href or not ramdisk_href) and not base_iso:
image_href = d_info['image_source']
image_properties = (
images.get_image_properties(
task.context, image_href, ['kernel_id', 'ramdisk_id']))
if not kernel_href:
kernel_href = image_properties.get('kernel_id')
if not ramdisk_href:
ramdisk_href = image_properties.get('ramdisk_id')
if (not kernel_href or not ramdisk_href):
raise exception.InvalidParameterValue(_(
"Unable to find kernel or ramdisk for "
"to generate boot ISO for %(node)s") %
{'node': task.node.uuid})
bootloader_str = 'bootloader'
bootloader_href = _find_param(bootloader_str, d_info)
return _prepare_iso_image(
task, kernel_href, ramdisk_href, bootloader_href,
root_uuid=root_uuid, base_iso=base_iso)
|