Support configdrive from HTTP(S)

This commit adds support for passing an HTTP(S) URL as the
`configdrive` parameter for the `prepare_image` command.

This will load the entire image in memory. Although the configdrive
is limited to 64MB, this is not ideal and should be made better
in the future (likely when imaging gets refactored).

Change-Id: I6f14b9059920d2cef108c30d027c5486fa0b7e9a
Implements: blueprint expose-configdrive
This commit is contained in:
Jim Rollenhagen
2014-11-19 16:08:16 -08:00
parent ac75185342
commit c7c81b6374
2 changed files with 29 additions and 1 deletions

View File

@@ -60,6 +60,16 @@ def _write_image(image_info, device):
image, device, totaltime))
def _configdrive_is_url(configdrive):
return (configdrive.startswith('http://')
or configdrive.startswith('https://'))
def _download_configdrive_to_file(configdrive, filename):
content = requests.get(configdrive).content
_write_configdrive_to_file(content, filename)
def _write_configdrive_to_file(configdrive, filename):
LOG.debug('Writing configdrive to {0}'.format(filename))
# configdrive data is base64'd, decode it first
@@ -72,7 +82,10 @@ def _write_configdrive_to_file(configdrive, filename):
def _write_configdrive_to_partition(configdrive, device):
filename = _configdrive_location()
_write_configdrive_to_file(configdrive, filename)
if _configdrive_is_url(configdrive):
_download_configdrive_to_file(configdrive, filename)
else:
_write_configdrive_to_file(configdrive, filename)
# check configdrive size before writing it
filesize = os.stat(filename).st_size

View File

@@ -118,6 +118,21 @@ class TestStandbyExtension(test_base.BaseTestCase):
execute_mock.assert_called_once_with(*command, check_exit_code=[0])
def test_configdrive_is_url(self):
self.assertTrue(standby._configdrive_is_url('http://some/url'))
self.assertTrue(standby._configdrive_is_url('https://some/url'))
self.assertFalse(standby._configdrive_is_url('ftp://some/url'))
self.assertFalse(standby._configdrive_is_url('binary-blob'))
@mock.patch.object(standby, '_write_configdrive_to_file')
@mock.patch('requests.get', autospec=True)
def test_download_configdrive_to_file(self, get_mock, write_mock):
url = 'http://swift/configdrive'
get_mock.return_value.content = 'data'
standby._download_configdrive_to_file(url, 'filename')
get_mock.assert_called_once_with(url)
write_mock.assert_called_once_with('data', 'filename')
@mock.patch('gzip.GzipFile', autospec=True)
@mock.patch(OPEN_FUNCTION_NAME, autospec=True)
@mock.patch('base64.b64decode', autospec=True)