Move ec2 code into its own module.

This commit is contained in:
Clint Byrum 2013-06-27 09:23:21 -07:00
parent 7f2546ff26
commit ae75926f47
4 changed files with 180 additions and 132 deletions

View File

@ -13,61 +13,24 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import httplib2
import json
from openstack.common import log
from os_collect_config import exc
from os_collect_config import ec2
from oslo.config import cfg
EC2_METADATA_URL = 'http://169.254.169.254/latest/meta-data'
h = httplib2.Http()
ec2_opts = [
cfg.StrOpt('metadata_url',
default=EC2_METADATA_URL,
help='URL to query for EC2 Metadata')
]
def _fetch_metadata(fetch_url):
global h
try:
(resp, content) = h.request(fetch_url)
except httplib2.socks.HTTPError as e:
log.getLogger().warn(e)
raise exc.Ec2MetadataNotAvailable
if fetch_url[-1] == '/':
new_content = {}
for subkey in content.split("\n"):
if '=' in subkey:
subkey = subkey[:subkey.index('=')] + '/'
sub_fetch_url = fetch_url + subkey
if subkey[-1] == '/':
subkey = subkey[:-1]
new_content[subkey] = _fetch_metadata(sub_fetch_url)
content = new_content
return content
def collect_ec2(ec2_url):
root_url = '%s/' % (ec2_url)
return _fetch_metadata(root_url)
def __main__():
def setup_conf():
ec2_group = cfg.OptGroup(name='ec2',
title='EC2 Metadata options')
conf = cfg.ConfigOpts()
conf.register_group(ec2_group)
conf.register_opts(ec2_opts, group='ec2')
conf.register_opts(ec2.opts, group='ec2')
return conf
def __main__():
conf = setup_conf()
log.setup("os-collect-config")
print json.dumps(collect_ec2(conf.ec2.metadata_url), indent=1)
if __name__ == '__main__':
__main__()
print json.dumps(ec2.collect(conf), indent=1)

55
os_collect_config/ec2.py Normal file
View File

@ -0,0 +1,55 @@
# Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
#
# 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 httplib2
from oslo.config import cfg
from openstack.common import log
from os_collect_config import exc
EC2_METADATA_URL = 'http://169.254.169.254/latest/meta-data'
h = httplib2.Http()
opts = [
cfg.StrOpt('metadata_url',
default=EC2_METADATA_URL,
help='URL to query for EC2 Metadata')
]
def _fetch_metadata(fetch_url):
global h
try:
(resp, content) = h.request(fetch_url)
except httplib2.socks.HTTPError as e:
log.getLogger().warn(e)
raise exc.Ec2MetadataNotAvailable
if fetch_url[-1] == '/':
new_content = {}
for subkey in content.split("\n"):
if '=' in subkey:
subkey = subkey[:subkey.index('=')] + '/'
sub_fetch_url = fetch_url + subkey
if subkey[-1] == '/':
subkey = subkey[:-1]
new_content[subkey] = _fetch_metadata(sub_fetch_url)
content = new_content
return content
def collect(conf):
root_url = '%s/' % (conf.ec2.metadata_url)
return _fetch_metadata(root_url)

View File

@ -14,106 +14,27 @@
# limitations under the License.
import fixtures
import httplib2
import json
from oslo.config import cfg
import testtools
from testtools import matchers
import urlparse
import uuid
from os_collect_config import collect
from os_collect_config import exc
META_DATA = {'local-ipv4': '192.0.2.1',
'reservation-id': str(uuid.uuid1()),
'local-hostname': 'foo',
'ami-launch-index': '0',
'public-hostname': 'foo',
'hostname': 'foo',
'ami-id': str(uuid.uuid1()),
'instance-action': 'none',
'public-ipv4': '192.0.2.1',
'instance-type': 'flavor.small',
'placement/': 'availability-zone',
'placement/availability-zone': 'foo-az',
'mpi/': 'foo-keypair',
'mpi/foo-keypair': '192.0.2.1 slots=1',
'block-device-mapping/': "ami\nroot\nephemeral0",
'block-device-mapping/ami': 'vda',
'block-device-mapping/root': '/dev/vda',
'block-device-mapping/ephemeral0': '/dev/vdb',
'public-keys/': '0=foo-keypair',
'public-keys/0': 'openssh-key',
'public-keys/0/': 'openssh-key',
'public-keys/0/openssh-key': 'ssh-rsa AAAAAAAAABBBBBBBBCCCCCCCC',
'instance-id': str(uuid.uuid1())}
class FakeResponse(dict):
status = 200
class FakeHttp(object):
def request(self, url):
url = urlparse.urlparse(url)
if url.path == '/latest/meta-data/':
# Remove keys which have anything after /
ks = [x for x in META_DATA.keys() if ('/' not in x
or not len(x.split('/')[1]))]
return (FakeResponse(), "\n".join(ks))
path = url.path
path = path.replace('/latest/meta-data/', '')
return (FakeResponse(), META_DATA[path])
class FakeFailHttp(object):
def request(self, url):
raise httplib2.socks.HTTPError(403, 'Forbidden')
from os_collect_config.tests import test_ec2
class TestCollect(testtools.TestCase):
def setUp(self):
super(TestCollect, self).setUp()
self.log = self.useFixture(fixtures.FakeLogger())
def test_collect_ec2(self):
self.useFixture(
fixtures.MonkeyPatch('os_collect_config.collect.h', FakeHttp()))
ec2 = collect.collect_ec2(collect.EC2_METADATA_URL)
self.assertThat(ec2, matchers.IsInstance(dict))
for k in ('public-ipv4', 'instance-id', 'hostname'):
self.assertIn(k, ec2)
self.assertEquals(ec2[k], META_DATA[k])
self.assertEquals(ec2['block-device-mapping']['ami'], 'vda')
# SSH keys are special cases
self.assertEquals(
{'0': {'openssh-key': 'ssh-rsa AAAAAAAAABBBBBBBBCCCCCCCC'}},
ec2['public-keys'])
self.assertEquals('', self.log.output)
def test_collect_ec2_fail(self):
self.useFixture(
fixtures.MonkeyPatch(
'os_collect_config.collect.h', FakeFailHttp()))
self.assertRaises(exc.Ec2MetadataNotAvailable,
collect.collect_ec2, collect.EC2_METADATA_URL)
self.assertIn('Forbidden', self.log.output)
class TestMain(testtools.TestCase):
def test_main(self):
self.useFixture(
fixtures.MonkeyPatch('os_collect_config.collect.h', FakeHttp()))
fixtures.MonkeyPatch(
'os_collect_config.ec2.h', test_ec2.FakeHttp()))
out = self.useFixture(fixtures.ByteStream('stdout'))
self.useFixture(fixtures.MonkeyPatch('sys.stdout', out.stream))
collect.__main__()
result = json.loads(out.stream.getvalue())
self.assertIn("local-ipv4", result)
self.assertIn("reservation-id", result)
def test_setup_conf(self):
conf = collect.setup_conf()
self.assertThat(conf, matchers.IsInstance(cfg.ConfigOpts))

View File

@ -0,0 +1,109 @@
# Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
#
# 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 fixtures
import httplib2
import testtools
from testtools import matchers
import urlparse
import uuid
from os_collect_config import collect
from os_collect_config import ec2
from os_collect_config import exc
META_DATA = {'local-ipv4': '192.0.2.1',
'reservation-id': str(uuid.uuid1()),
'local-hostname': 'foo',
'ami-launch-index': '0',
'public-hostname': 'foo',
'hostname': 'foo',
'ami-id': str(uuid.uuid1()),
'instance-action': 'none',
'public-ipv4': '192.0.2.1',
'instance-type': 'flavor.small',
'placement/': 'availability-zone',
'placement/availability-zone': 'foo-az',
'mpi/': 'foo-keypair',
'mpi/foo-keypair': '192.0.2.1 slots=1',
'block-device-mapping/': "ami\nroot\nephemeral0",
'block-device-mapping/ami': 'vda',
'block-device-mapping/root': '/dev/vda',
'block-device-mapping/ephemeral0': '/dev/vdb',
'public-keys/': '0=foo-keypair',
'public-keys/0': 'openssh-key',
'public-keys/0/': 'openssh-key',
'public-keys/0/openssh-key': 'ssh-rsa AAAAAAAAABBBBBBBBCCCCCCCC',
'instance-id': str(uuid.uuid1())}
class FakeResponse(dict):
status = 200
class FakeHttp(object):
def request(self, url):
url = urlparse.urlparse(url)
if url.path == '/latest/meta-data/':
# Remove keys which have anything after /
ks = [x for x in META_DATA.keys() if ('/' not in x
or not len(x.split('/')[1]))]
return (FakeResponse(), "\n".join(ks))
path = url.path
path = path.replace('/latest/meta-data/', '')
return (FakeResponse(), META_DATA[path])
class FakeFailHttp(object):
def request(self, url):
raise httplib2.socks.HTTPError(403, 'Forbidden')
class TestCollect(testtools.TestCase):
def setUp(self):
super(TestCollect, self).setUp()
self.log = self.useFixture(fixtures.FakeLogger())
def test_collect_ec2(self):
self.useFixture(
fixtures.MonkeyPatch('os_collect_config.ec2.h', FakeHttp()))
conf = collect.setup_conf()
ec2_md = ec2.collect(conf)
self.assertThat(ec2_md, matchers.IsInstance(dict))
for k in ('public-ipv4', 'instance-id', 'hostname'):
self.assertIn(k, ec2_md)
self.assertEquals(ec2_md[k], META_DATA[k])
self.assertEquals(ec2_md['block-device-mapping']['ami'], 'vda')
# SSH keys are special cases
self.assertEquals(
{'0': {'openssh-key': 'ssh-rsa AAAAAAAAABBBBBBBBCCCCCCCC'}},
ec2_md['public-keys'])
self.assertEquals('', self.log.output)
def test_collect_ec2_fail(self):
self.useFixture(
fixtures.MonkeyPatch(
'os_collect_config.collect.h', FakeFailHttp()))
conf = collect.setup_conf()
self.assertRaises(exc.Ec2MetadataNotAvailable,
ec2.collect, conf)
self.assertIn('Forbidden', self.log.output)