2014-04-09 14:31:31 -07:00
|
|
|
# Copyright 2013 Rackspace, Inc.
|
|
|
|
#
|
|
|
|
# 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.
|
2013-09-23 23:29:55 -07:00
|
|
|
|
2014-01-15 12:48:33 -08:00
|
|
|
import hashlib
|
2014-01-13 16:36:55 -08:00
|
|
|
import os
|
2014-01-10 13:15:13 -08:00
|
|
|
import requests
|
2014-01-24 14:20:14 -08:00
|
|
|
import subprocess
|
|
|
|
import time
|
2014-01-10 13:15:13 -08:00
|
|
|
|
2014-03-19 16:19:52 -07:00
|
|
|
from ironic_python_agent import base
|
|
|
|
from ironic_python_agent import configdrive
|
|
|
|
from ironic_python_agent import decorators
|
|
|
|
from ironic_python_agent import errors
|
|
|
|
from ironic_python_agent import hardware
|
|
|
|
from ironic_python_agent.openstack.common import log
|
2013-12-17 15:13:30 -08:00
|
|
|
|
2014-03-17 15:17:27 -07:00
|
|
|
LOG = log.getLogger(__name__)
|
2014-01-24 14:20:14 -08:00
|
|
|
|
2013-09-23 23:29:55 -07:00
|
|
|
|
2014-01-17 12:40:29 -08:00
|
|
|
def _configdrive_location():
|
|
|
|
return '/tmp/configdrive'
|
|
|
|
|
|
|
|
|
2014-01-10 13:15:13 -08:00
|
|
|
def _image_location(image_info):
|
2014-03-11 13:31:19 -07:00
|
|
|
return '/tmp/{0}'.format(image_info['id'])
|
2014-01-10 13:15:13 -08:00
|
|
|
|
|
|
|
|
2014-01-17 15:59:56 -08:00
|
|
|
def _path_to_script(script):
|
|
|
|
cwd = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
return os.path.join(cwd, script)
|
|
|
|
|
|
|
|
|
2014-01-28 15:55:07 -08:00
|
|
|
def _write_image(image_info, device):
|
2014-01-24 14:20:14 -08:00
|
|
|
starttime = time.time()
|
2014-01-10 13:15:13 -08:00
|
|
|
image = _image_location(image_info)
|
2014-01-10 16:13:47 -08:00
|
|
|
|
2014-01-28 15:55:07 -08:00
|
|
|
script = _path_to_script('shell/write_image.sh')
|
|
|
|
command = ['/bin/bash', script, image, device]
|
2014-03-17 15:17:27 -07:00
|
|
|
LOG.info('Writing image with command: {0}'.format(' '.join(command)))
|
2014-01-15 11:20:05 -08:00
|
|
|
exit_code = subprocess.call(command)
|
|
|
|
if exit_code != 0:
|
|
|
|
raise errors.ImageWriteError(exit_code, device)
|
2014-01-24 15:02:11 -08:00
|
|
|
totaltime = time.time() - starttime
|
2014-03-17 15:17:27 -07:00
|
|
|
LOG.info('Image {0} written to device {1} in {2} seconds'.format(
|
|
|
|
image, device, totaltime))
|
2014-01-10 13:15:13 -08:00
|
|
|
|
|
|
|
|
2014-01-28 15:55:07 -08:00
|
|
|
def _copy_configdrive_to_disk(configdrive_dir, device):
|
|
|
|
starttime = time.time()
|
|
|
|
script = _path_to_script('shell/copy_configdrive_to_disk.sh')
|
|
|
|
command = ['/bin/bash', script, configdrive_dir, device]
|
2014-03-17 15:17:27 -07:00
|
|
|
LOG.info('copying configdrive to disk with command {0}'.format(
|
|
|
|
' '.join(command)))
|
2014-01-28 15:55:07 -08:00
|
|
|
exit_code = subprocess.call(command)
|
|
|
|
|
|
|
|
if exit_code != 0:
|
|
|
|
raise errors.ConfigDriveWriteError(exit_code, device)
|
|
|
|
|
|
|
|
totaltime = time.time() - starttime
|
2014-03-17 15:17:27 -07:00
|
|
|
LOG.info('configdrive copied from {0} to {1} in {2} seconds'.format(
|
|
|
|
configdrive_dir,
|
|
|
|
device,
|
|
|
|
totaltime))
|
2014-01-28 15:55:07 -08:00
|
|
|
|
|
|
|
|
2014-01-14 16:53:46 -08:00
|
|
|
def _request_url(image_info, url):
|
|
|
|
resp = requests.get(url, stream=True)
|
|
|
|
if resp.status_code != 200:
|
|
|
|
raise errors.ImageDownloadError(image_info['id'])
|
|
|
|
return resp
|
|
|
|
|
|
|
|
|
2014-01-10 13:15:13 -08:00
|
|
|
def _download_image(image_info):
|
2014-01-24 14:20:14 -08:00
|
|
|
starttime = time.time()
|
2014-01-14 16:53:46 -08:00
|
|
|
resp = None
|
|
|
|
for url in image_info['urls']:
|
2014-01-14 13:50:57 -08:00
|
|
|
try:
|
2014-03-17 15:17:27 -07:00
|
|
|
LOG.info("Attempting to download image from {0}".format(url))
|
2014-01-14 16:53:46 -08:00
|
|
|
resp = _request_url(image_info, url)
|
|
|
|
except errors.ImageDownloadError:
|
2014-01-24 14:55:33 -08:00
|
|
|
failtime = time.time() - starttime
|
2014-03-17 15:17:27 -07:00
|
|
|
log_msg = "Image download failed. URL: {0}; time: {1} seconds"
|
|
|
|
LOG.warning(log_msg.format(url, failtime))
|
2014-01-14 13:50:57 -08:00
|
|
|
continue
|
|
|
|
else:
|
2014-01-14 16:53:46 -08:00
|
|
|
break
|
|
|
|
if resp is None:
|
|
|
|
raise errors.ImageDownloadError(image_info['id'])
|
|
|
|
|
|
|
|
image_location = _image_location(image_info)
|
|
|
|
with open(image_location, 'wb') as f:
|
2014-01-15 17:01:19 -08:00
|
|
|
try:
|
|
|
|
for chunk in resp.iter_content(1024 * 1024):
|
|
|
|
f.write(chunk)
|
|
|
|
except Exception:
|
|
|
|
raise errors.ImageDownloadError(image_info['id'])
|
2014-01-14 16:53:46 -08:00
|
|
|
|
2014-01-24 15:02:11 -08:00
|
|
|
totaltime = time.time() - starttime
|
2014-03-17 15:17:27 -07:00
|
|
|
LOG.info("Image downloaded from {0} in {1} seconds".format(image_location,
|
|
|
|
totaltime))
|
2014-01-24 14:20:14 -08:00
|
|
|
|
2014-01-14 16:53:46 -08:00
|
|
|
if not _verify_image(image_info, image_location):
|
|
|
|
raise errors.ImageChecksumError(image_info['id'])
|
2014-01-10 13:15:13 -08:00
|
|
|
|
|
|
|
|
|
|
|
def _verify_image(image_info, image_location):
|
2014-01-15 12:48:33 -08:00
|
|
|
hashes = image_info['hashes']
|
2014-04-04 13:30:42 -07:00
|
|
|
for k, v in hashes.items():
|
2014-01-15 12:48:33 -08:00
|
|
|
algo = getattr(hashlib, k, None)
|
|
|
|
if algo is None:
|
|
|
|
continue
|
2014-03-17 15:17:27 -07:00
|
|
|
log_msg = 'Verifying image at {0} with algorithm {1} against hash {2}'
|
|
|
|
LOG.debug(log_msg.format(image_location, k, v))
|
2014-01-16 14:53:49 -08:00
|
|
|
hash_ = algo(open(image_location).read()).hexdigest()
|
2014-01-15 12:48:33 -08:00
|
|
|
if hash_ == v:
|
|
|
|
return True
|
2014-01-24 14:20:14 -08:00
|
|
|
else:
|
2014-03-17 15:17:27 -07:00
|
|
|
log_msg = ('Image verification failed. Location: {0};'
|
|
|
|
'algorithm: {1}; image hash: {2};'
|
|
|
|
'verification hash: {3}')
|
|
|
|
LOG.warning(log_msg.format(image_location, k, hash_, v))
|
2014-01-15 12:48:33 -08:00
|
|
|
return False
|
2014-01-10 13:15:13 -08:00
|
|
|
|
|
|
|
|
2014-04-14 15:36:59 +04:00
|
|
|
def _validate_image_info(ext, image_info=None, **kwargs):
|
2014-02-20 12:06:24 -08:00
|
|
|
image_info = image_info or {}
|
|
|
|
|
2014-02-06 16:40:01 -08:00
|
|
|
for field in ['id', 'urls', 'hashes']:
|
|
|
|
if field not in image_info:
|
2014-03-11 13:31:19 -07:00
|
|
|
msg = 'Image is missing \'{0}\' field.'.format(field)
|
2014-02-06 16:40:01 -08:00
|
|
|
raise errors.InvalidCommandParamsError(msg)
|
|
|
|
|
|
|
|
if type(image_info['urls']) != list or not image_info['urls']:
|
|
|
|
raise errors.InvalidCommandParamsError(
|
|
|
|
'Image \'urls\' must be a list with at least one element.')
|
|
|
|
|
|
|
|
if type(image_info['hashes']) != dict or not image_info['hashes']:
|
|
|
|
raise errors.InvalidCommandParamsError(
|
|
|
|
'Image \'hashes\' must be a dictionary with at least one '
|
|
|
|
'element.')
|
|
|
|
|
|
|
|
|
2014-03-25 18:00:10 +04:00
|
|
|
class StandbyExtension(base.BaseAgentExtension):
|
2014-01-14 21:27:34 -08:00
|
|
|
def __init__(self):
|
2014-03-25 18:00:10 +04:00
|
|
|
super(StandbyExtension, self).__init__('STANDBY')
|
2014-01-28 13:04:00 -08:00
|
|
|
self.command_map['cache_image'] = self.cache_image
|
2014-01-21 10:42:43 -08:00
|
|
|
self.command_map['prepare_image'] = self.prepare_image
|
|
|
|
self.command_map['run_image'] = self.run_image
|
2013-12-21 17:22:09 -08:00
|
|
|
|
2014-02-20 12:11:01 -08:00
|
|
|
self.cached_image_id = None
|
|
|
|
|
2014-02-06 16:40:01 -08:00
|
|
|
@decorators.async_command(_validate_image_info)
|
2014-02-20 13:04:26 -08:00
|
|
|
def cache_image(self, command_name, image_info=None, force=False):
|
2014-02-05 15:41:48 -08:00
|
|
|
device = hardware.get_manager().get_os_install_device()
|
|
|
|
|
2014-02-20 13:04:26 -08:00
|
|
|
if self.cached_image_id != image_info['id'] or force:
|
|
|
|
_download_image(image_info)
|
|
|
|
_write_image(image_info, device)
|
|
|
|
self.cached_image_id = image_info['id']
|
2014-02-20 12:11:01 -08:00
|
|
|
|
2014-02-06 16:40:01 -08:00
|
|
|
@decorators.async_command(_validate_image_info)
|
2014-02-20 12:37:27 -08:00
|
|
|
def prepare_image(self,
|
|
|
|
command_name,
|
|
|
|
image_info=None,
|
|
|
|
metadata=None,
|
|
|
|
files=None):
|
2014-02-05 15:41:48 -08:00
|
|
|
location = _configdrive_location()
|
|
|
|
device = hardware.get_manager().get_os_install_device()
|
|
|
|
|
2014-02-20 12:11:01 -08:00
|
|
|
# don't write image again if already cached
|
|
|
|
if self.cached_image_id != image_info['id']:
|
|
|
|
_download_image(image_info)
|
|
|
|
_write_image(image_info, device)
|
2014-02-20 12:22:13 -08:00
|
|
|
self.cached_image_id = image_info['id']
|
2014-02-05 15:41:48 -08:00
|
|
|
|
2014-03-17 15:17:27 -07:00
|
|
|
LOG.debug('Writing configdrive to {0}'.format(location))
|
2014-02-05 15:41:48 -08:00
|
|
|
configdrive.write_configdrive(location, metadata, files)
|
|
|
|
_copy_configdrive_to_disk(location, device)
|
2014-02-06 11:04:44 -08:00
|
|
|
|
2014-02-06 16:40:01 -08:00
|
|
|
@decorators.async_command()
|
2014-02-20 12:37:27 -08:00
|
|
|
def run_image(self, command_name):
|
2014-02-06 11:04:44 -08:00
|
|
|
script = _path_to_script('shell/reboot.sh')
|
2014-03-17 15:17:27 -07:00
|
|
|
LOG.info('Rebooting system')
|
2014-02-06 11:04:44 -08:00
|
|
|
command = ['/bin/bash', script]
|
|
|
|
# this should never return if successful
|
|
|
|
exit_code = subprocess.call(command)
|
|
|
|
if exit_code != 0:
|
|
|
|
raise errors.SystemRebootError(exit_code)
|