summaryrefslogtreecommitdiff
path: root/cloudinit/DataSourceMaaS.py
blob: 88686b13b1e94913bfaa4b0c7043a6df4f882874 (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# vi: ts=4 expandtab
#
#    Copyright (C) 2009-2010 Canonical Ltd.
#    Copyright (C) 2012 Hewlett-Packard Development Company, L.P.
#
#    Author: Scott Moser <scott.moser@canonical.com>
#    Author: Juerg Hafliger <juerg.haefliger@hp.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 cloudinit.DataSource as DataSource

from cloudinit import seeddir as base_seeddir
from cloudinit import log
import cloudinit.util as util
import errno
import oauth.oauth as oauth
import os.path
import socket
import urllib2
import time


class DataSourceMaaS(DataSource.DataSource):
    """
    DataSourceMaaS reads instance information from MaaS.
    Given a config metadata_url, and oauth tokens, it expects to find
    files under the root named:
      instance-id
      user-data
      hostname
    """
    seeddir = base_seeddir + '/maas'
    baseurl = None

    def __str__(self):
        return("DataSourceMaaS[%s]" % self.baseurl)

    def get_data(self):
        mcfg = self.ds_cfg

        try:
            (userdata, metadata) = read_maas_seed_dir(self.seeddir)
            self.userdata_raw = userdata
            self.metadata = metadata
            self.baseurl = self.seeddir
            return True
        except MaasSeedDirNone:
            pass
        except MaasSeedDirMalformed as exc:
            log.warn("%s was malformed: %s\n" % (self.seeddir, exc))
            raise

        try:
            # if there is no metadata_url, then we're not configured
            url = mcfg.get('metadata_url', None)
            if url == None:
                return False

            if not self.wait_for_metadata_service(url):
                return False

            self.baseurl = url

            (userdata, metadata) = read_maas_seed_url(self.baseurl,
                self.md_headers)
            return True
        except Exception:
            util.logexc(log)
            return False

    def md_headers(self, url):
        mcfg = self.ds_cfg

        # if we are missing token_key, token_secret or consumer_key
        # then just do non-authed requests
        for required in ('token_key', 'token_secret', 'consumer_key'):
            if required not in mcfg:
                return({})

        consumer_secret = mcfg.get('consumer_secret', "")

        return(oauth_headers(url=url, consumer_key=mcfg['consumer_key'],
            token_key=mcfg['token_key'], token_secret=mcfg['token_secret'],
            consumer_secret=consumer_secret))

    def wait_for_metadata_service(self, url):
        mcfg = self.ds_cfg

        max_wait = 120
        try:
            max_wait = int(mcfg.get("max_wait", max_wait))
        except Exception:
            util.logexc(log)
            log.warn("Failed to get max wait. using %s" % max_wait)

        if max_wait == 0:
            return False

        timeout = 50
        try:
            timeout = int(mcfg.get("timeout", timeout))
        except Exception:
            util.logexc(log)
            log.warn("Failed to get timeout, using %s" % timeout)

        starttime = time.time()
        check_url = "%s/instance-id" % url
        url = util.wait_for_url(urls=[check_url], max_wait=max_wait,
            timeout=timeout, status_cb=log.warn,
            headers_cb=self.md_headers)

        if url:
            log.debug("Using metadata source: '%s'" % url)
        else:
            log.critical("giving up on md after %i seconds\n" %
                         int(time.time() - starttime))

        return (bool(url))


def read_maas_seed_dir(seed_d):
    """
    Return user-data and metadata for a maas seed dir in seed_d.
    Expected format of seed_d are the following files:
      * instance-id
      * hostname
      * user-data
    """
    files = ('hostname', 'instance-id', 'user-data')
    md = {}

    if not os.path.isdir(seed_d):
        raise MaasSeedDirNone("%s: not a directory")

    for fname in files:
        try:
            with open(os.path.join(seed_d, fname)) as fp:
                md[fname] = fp.read()
                fp.close()
        except IOError as e:
            if e.errno != errno.ENOENT:
                raise

    return(check_seed_contents(md, seed_d))


def read_maas_seed_url(seed_url, header_cb=None, timeout=None):
    """
    Read the maas datasource at seed_url.
    header_cb is a method that should return a headers dictionary that will
    be given to urllib2.Request()

    Expected format of seed_url is are the following files:
      * <seed_url>/instance-id
      * <seed_url>/hostname
      * <seed_url>/user-data
    """
    files = ('hostname', 'instance-id', 'user-data')

    md = {}
    for fname in files:
        url = "%s/%s" % (seed_url, fname)
        if header_cb:
            headers = header_cb(url)
        else:
            headers = {}

        req = urllib2.Request(url, data=None, headers=headers)
        resp = urllib2.urlopen(req, timeout=timeout)

        md[fname] = resp.read()

    return(check_seed_contents(md, seed_url))


def check_seed_contents(content, seed):
    """Validate if content is Is the content a dict that is valid as a 
       return for a datasource.
       Either return a (userdata, metadata) tuple or
       Raise MaasSeedDirMalformed or MaasSeedDirNone
    """
    md_required = ('user-data', 'instance-id', 'hostname')
    found = content.keys()

    if len(content) == 0:
        raise MaasSeedDirNone("%s: no data files found" % seed)

    if 'user-data' not in content:
        raise MaasSeedDirMalformed("%s: missing user-data" % seed)

    missing = [k for k in md_required if k not in found]
    if len(missing):
        raise MaasSeedDirMalformed("%s: missing files %s" % (seed, missing))

    userdata = content['user-data']
    md = { }
    for (key, val) in content.iteritems():
        if key == 'user-data':
            continue
        md[key] = val
            
    return(userdata, md)


def oauth_headers(url, consumer_key, token_key, token_secret, consumer_secret):
    consumer = oauth.OAuthConsumer(consumer_key, consumer_secret)
    token = oauth.OAuthToken(token_key, token_secret)
    params = {
        'oauth_version': "1.0",
        'oauth_nonce': oauth.generate_nonce(),
        'oauth_timestamp': int(time.time()),
        'oauth_token': token.key,
        'oauth_consumer_key': consumer.key,
    }
    req = oauth.OAuthRequest(http_url=url, parameters=params)
    req.sign_request(oauth.OAuthSignatureMethod_PLAINTEXT(),
        consumer, token)
    return(req.to_header())


class MaasSeedDirNone(Exception):
    pass


class MaasSeedDirMalformed(Exception):
    pass


datasources = [
  (DataSourceMaaS, (DataSource.DEP_FILESYSTEM, DataSource.DEP_NETWORK)),
]


# return a list of data sources that match this set of dependencies
def get_datasource_list(depends):
    return(DataSource.list_from_depends(depends, datasources))


if __name__ == "__main__":
    def main():
        import sys
        import pprint
        seed = sys.argv[1]
        if seed.startswith("http://") or seed.startswith("https://"):
            (userdata, metadata) = read_maas_seed_url(seed)
        else:
            (userdata, metadata) = read_maas_seed_dir(seed)

        print "=== userdata ==="
        print userdata
        print "=== metadata ==="
        pprint.pprint(metadata)

    main()