2013-09-23 23:29:55 -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.
|
|
|
|
"""
|
|
|
|
|
2014-01-10 16:13:47 -08:00
|
|
|
import json
|
2014-01-10 13:15:13 -08:00
|
|
|
import subprocess
|
2014-01-10 16:13:47 -08:00
|
|
|
import uuid
|
2014-01-10 13:15:13 -08:00
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2013-12-20 12:57:38 -08:00
|
|
|
from teeth_agent import base
|
2013-12-21 17:22:09 -08:00
|
|
|
from teeth_agent import errors
|
2013-12-17 15:13:30 -08:00
|
|
|
|
2013-09-23 23:29:55 -07:00
|
|
|
|
2014-01-10 13:15:13 -08:00
|
|
|
def _image_location(image_info):
|
|
|
|
return '/tmp/{}'.format(image_info['id'])
|
|
|
|
|
|
|
|
|
2014-01-10 15:12:24 -08:00
|
|
|
def _write_image(image_info, configdrive='configdrive', device='/dev/sda'):
|
|
|
|
# TODO(jimrollenhagen) don't hardcode these kwargs
|
2014-01-10 13:15:13 -08:00
|
|
|
image = _image_location(image_info)
|
2014-01-10 16:13:47 -08:00
|
|
|
filename = '{}/meta_data.json'.format(configdrive)
|
|
|
|
with open(filename, 'w') as f:
|
|
|
|
data = {
|
|
|
|
'uuid': str(uuid.uuid4()),
|
|
|
|
'admin_pass': 'password',
|
|
|
|
'name': 'teeth',
|
|
|
|
'random_seed': str(uuid.uuid4()).encode('base64'),
|
|
|
|
'hostname': 'teeth-test',
|
|
|
|
'availability_zone': 'teeth',
|
|
|
|
'launch_index': 0,
|
|
|
|
}
|
|
|
|
data = json.dumps(data)
|
|
|
|
f.write(data)
|
|
|
|
|
|
|
|
command = ['sh', 'shell/makefs.sh', configdrive, image, device]
|
2014-01-10 13:15:13 -08:00
|
|
|
return subprocess.call(command)
|
|
|
|
|
|
|
|
|
|
|
|
def _download_image(image_info):
|
2014-01-10 15:12:24 -08:00
|
|
|
resp = requests.get(image_info['urls'][0], stream=True)
|
2014-01-10 13:15:13 -08:00
|
|
|
if resp.status_code != 200:
|
|
|
|
# TODO(jimrollenhagen) define a better exception
|
|
|
|
raise Exception
|
|
|
|
image_location = _image_location(image_info)
|
|
|
|
with open(image_location, 'wb') as f:
|
|
|
|
for chunk in resp.iter_content():
|
|
|
|
f.write(chunk)
|
|
|
|
|
|
|
|
if not _verify_image(image_info, image_location):
|
|
|
|
# TODO(jimrollenhagen) better exception
|
|
|
|
# TODO(jimrollenhagen) retry?
|
|
|
|
raise Exception
|
|
|
|
|
|
|
|
|
|
|
|
def _verify_image(image_info, image_location):
|
|
|
|
# TODO(jimrollenhagen) verify the image checksum
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2014-01-09 13:44:08 -08:00
|
|
|
class CacheImagesCommand(base.AsyncCommandResult):
|
|
|
|
def execute(self):
|
|
|
|
# TODO(russellhaering): Actually cache images
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-01-10 13:15:13 -08:00
|
|
|
class PrepareImageCommand(base.AsyncCommandResult):
|
|
|
|
"""Downloads and writes an image and configdrive to a device."""
|
|
|
|
def execute(self):
|
|
|
|
image_info = self.command_params
|
|
|
|
_download_image(image_info)
|
|
|
|
_write_image(image_info)
|
|
|
|
|
|
|
|
|
|
|
|
class RunImageCommand(base.AsyncCommandResult):
|
|
|
|
def execute(self):
|
|
|
|
# TODO(jimrollenhagen): Actually run image, reboot/kexec/whatever
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2013-12-20 12:57:38 -08:00
|
|
|
class StandbyAgent(base.BaseTeethAgent):
|
2014-01-06 12:02:12 -08:00
|
|
|
def __init__(self, listen_host, listen_port, api_url):
|
|
|
|
super(StandbyAgent, self).__init__(listen_host,
|
|
|
|
listen_port,
|
|
|
|
api_url,
|
|
|
|
'STANDBY')
|
2013-12-21 17:22:09 -08:00
|
|
|
|
|
|
|
self.command_map = {
|
2014-01-10 15:12:24 -08:00
|
|
|
'cache_images': self.cache_images,
|
|
|
|
'prepare_image': self.prepare_image,
|
|
|
|
'run_image': self.run_image,
|
2013-12-21 17:22:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
def _validate_image_info(self, image_info):
|
|
|
|
for field in ['id', 'urls', 'hashes']:
|
|
|
|
if field not in image_info:
|
|
|
|
msg = 'Image is missing \'{}\' field.'.format(field)
|
|
|
|
raise errors.InvalidCommandParamsError(msg)
|
|
|
|
|
|
|
|
if type(image_info['urls']) != list:
|
|
|
|
raise errors.InvalidCommandParamsError(
|
|
|
|
'Image \'urls\' must be a list.')
|
|
|
|
|
|
|
|
if type(image_info['hashes']) != dict:
|
|
|
|
raise errors.InvalidCommandParamsError(
|
|
|
|
'Image \'hashes\' must be a dictionary.')
|
|
|
|
|
2013-12-31 11:49:32 -08:00
|
|
|
def cache_images(self, command_name, image_infos):
|
2013-12-21 17:22:09 -08:00
|
|
|
if type(image_infos) != list:
|
|
|
|
raise errors.InvalidCommandParamsError(
|
|
|
|
'\'image_infos\' parameter must be a list.')
|
|
|
|
|
|
|
|
for image_info in image_infos:
|
|
|
|
self._validate_image_info(image_info)
|
|
|
|
|
2014-01-09 13:44:08 -08:00
|
|
|
return CacheImagesCommand(command_name, image_infos).start()
|
2014-01-10 13:15:13 -08:00
|
|
|
|
2014-01-10 15:12:24 -08:00
|
|
|
def prepare_image(self, command_name, image_info):
|
2014-01-10 13:15:13 -08:00
|
|
|
self._validate_image_info(image_info)
|
|
|
|
|
|
|
|
return PrepareImageCommand(command_name, image_info).start()
|
|
|
|
|
2014-01-10 15:12:24 -08:00
|
|
|
def run_image(self, command_name, image_info):
|
2014-01-10 13:15:13 -08:00
|
|
|
self._validate_image_info(image_info)
|
|
|
|
|
|
|
|
return RunImageCommand(command_name, image_info).start()
|