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
|
# vi: ts=4 expandtab
#
# Copyright (C) 2012 Yahoo! Inc.
#
# Author: Joshua Harlow <harlowja@yahoo-inc.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import json
import urllib
from cloudinit import log as logging
from cloudinit import url_helper as uh
from cloudinit import util
LOG = logging.getLogger(__name__)
# For now take this and fix it...
class LazyLoadMetadata(dict):
def __init__(self, url, fetch_timeout, num_retries, ssl_details):
self._url = url
self._num_retries = num_retries
self._ssl_details = ssl_details
self._fetch_timeout = fetch_timeout
self._leaves = {}
self._dicts = []
response = uh.readurl(url, timeout=fetch_timeout,
retries=num_retries, ssl_details=ssl_details)
data = str(response)
if data:
fields = data.split('\n')
for field in fields:
if field.endswith('/'):
key = field[0:-1]
self._dicts.append(key)
else:
p = field.find('=')
if p > 0:
key = field[p + 1:]
resource = field[0:p] + '/openssh-key'
else:
key = resource = field
self._leaves[key] = resource
self[key] = None
def _materialize(self):
for key in self:
self[key]
def __getitem__(self, key):
if key not in self:
# Allow dict to throw the KeyError
return super(LazyLoadMetadata, self).__getitem__(key)
# Already loaded
val = super(LazyLoadMetadata, self).__getitem__(key)
if val is not None:
return val
if key in self._leaves:
resource = self._leaves[key]
new_url = self._url + urllib.quote(resource, safe="/:")
response = uh.readurl(new_url, retries=self._num_retries,
timeout=self._fetch_timeout,
ssl_details=self._ssl_details)
val = str(response)
if val and val[0] == '{':
val = json.loads(val)
else:
p = val.find('\n')
if p > 0:
val = val.split('\n')
self[key] = val
elif key in self._dicts:
new_url = self._url + key + '/'
self[key] = LazyLoadMetadata(new_url,
num_retries=self._num_retries,
fetch_timeout=self._fetch_timeout,
ssl_details=self._ssl_details)
return super(LazyLoadMetadata, self).__getitem__(key)
def get(self, key, default=None):
try:
return self[key]
except KeyError:
return default
def values(self):
self._materialize()
return super(LazyLoadMetadata, self).values()
def items(self):
self._materialize()
return super(LazyLoadMetadata, self).items()
def __str__(self):
self._materialize()
return super(LazyLoadMetadata, self).__str__()
def __repr__(self):
self._materialize()
return super(LazyLoadMetadata, self).__repr__()
def get_instance_userdata(url, version='latest', ssl_details=None):
ud_url = '%s/%s/user-data' % (url, version)
try:
response = uh.readurl(ud_url, timeout=5,
retries=10, ssl_details=ssl_details)
return str(response)
except Exception as e:
util.logexc(LOG, "Failed fetching url %s", ud_url)
return None
def get_instance_metadata(url, version='latest', ssl_details=None):
md_url = '%s/%s/meta-data' % (url, version)
try:
return LazyLoadMetadata(md_url, timeout=5,
retries=10, ssl_details=ssl_details)
except Exception as e:
util.logexc(LOG, "Failed fetching url %s", md_url)
return None
|