~smoser/cloud-init/snappy-fix-config

« back to all changes in this revision

Viewing changes to cloudinit/DataSourceOVF.py

  • Committer: Scott Moser
  • Date: 2012-01-17 19:41:29 UTC
  • Revision ID: smoser@ubuntu.com-20120117194129-nftss2ywh8hp9xpi
[PATCH] PEP8 coding style fixes.
From: Juerg Haefliger <juerg.haefliger@hp.com>

This pulls in the named patch for LP: #914739 with a few other changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
#    You should have received a copy of the GNU General Public License
17
17
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
18
 
19
 
import DataSource
 
19
import cloudinit.DataSource as DataSource
20
20
 
21
 
from cloudinit import seeddir, log
 
21
from cloudinit import seeddir as base_seeddir
 
22
from cloudinit import log
22
23
import cloudinit.util as util
23
24
import sys
24
25
import os.path
29
30
import tempfile
30
31
import subprocess
31
32
 
 
33
 
32
34
class DataSourceOVF(DataSource.DataSource):
33
35
    seed = None
34
 
    seeddir = seeddir + '/ovf'
 
36
    seeddir = base_seeddir + '/ovf'
35
37
    environment = None
36
 
    cfg = { }
 
38
    cfg = {}
37
39
    userdata_raw = None
38
40
    metadata = None
39
 
    supported_seed_starts = ( "/" , "file://" )
 
41
    supported_seed_starts = ("/", "file://")
40
42
 
41
43
    def __str__(self):
42
44
        mstr = "DataSourceOVF"
44
46
        return(mstr)
45
47
 
46
48
    def get_data(self):
47
 
        found = [ ]
48
 
        md = { }
 
49
        found = []
 
50
        md = {}
49
51
        ud = ""
50
52
 
51
 
        defaults = { 
52
 
            "instance-id" : "iid-dsovf"
 
53
        defaults = {
 
54
            "instance-id": "iid-dsovf"
53
55
        }
54
56
 
55
 
        (seedfile, contents) = get_ovf_env(seeddir)
 
57
        (seedfile, contents) = get_ovf_env(base_seeddir)
56
58
        if seedfile:
57
59
            # found a seed dir
58
 
            seed = "%s/%s" % (seeddir, seedfile)
 
60
            seed = "%s/%s" % (base_seeddir, seedfile)
59
61
            (md, ud, cfg) = read_ovf_environment(contents)
60
62
            self.environment = contents
61
63
 
62
64
            found.append(seed)
63
65
        else:
64
 
            np = { 'iso' : transport_iso9660, 
65
 
                   'vmware-guestd' : transport_vmware_guestd, }
 
66
            np = {'iso': transport_iso9660,
 
67
                  'vmware-guestd': transport_vmware_guestd, }
66
68
            for name, transfunc in np.iteritems():
67
69
                (contents, _dev, _fname) = transfunc()
68
70
                if contents:
94
96
 
95
97
            md = util.mergedict(md, md_seed)
96
98
            found.append(seedfrom)
97
 
                    
98
99
 
99
100
        md = util.mergedict(md, defaults)
100
101
        self.seed = ",".join(found)
106
107
    def get_public_ssh_keys(self):
107
108
        if not 'public-keys' in self.metadata:
108
109
            return([])
109
 
        return([self.metadata['public-keys'],])
110
 
        
 
110
        return([self.metadata['public-keys'], ])
 
111
 
111
112
    # the data sources' config_obj is a cloud-config formated
112
113
    # object that came to it from ways other than cloud-config
113
114
    # because cloud-config content would be handled elsewhere
114
115
    def get_config_obj(self):
115
116
        return(self.cfg)
116
117
 
 
118
 
117
119
class DataSourceOVFNet(DataSourceOVF):
118
 
    seeddir = seeddir + '/ovf-net'
119
 
    supported_seed_starts = ( "http://", "https://", "ftp://" )
 
120
    seeddir = base_seeddir + '/ovf-net'
 
121
    supported_seed_starts = ("http://", "https://", "ftp://")
 
122
 
120
123
 
121
124
# this will return a dict with some content
122
125
#  meta-data, user-data
123
126
def read_ovf_environment(contents):
124
127
    props = getProperties(contents)
125
 
    md = { }
126
 
    cfg = { }
 
128
    md = {}
 
129
    cfg = {}
127
130
    ud = ""
128
 
    cfg_props = [ 'password', ]
129
 
    md_props = [ 'seedfrom', 'local-hostname', 'public-keys', 'instance-id' ]
 
131
    cfg_props = ['password', ]
 
132
    md_props = ['seedfrom', 'local-hostname', 'public-keys', 'instance-id']
130
133
    for prop, val in props.iteritems():
131
134
        if prop == 'hostname':
132
135
            prop = "local-hostname"
140
143
            except:
141
144
                ud = val
142
145
    return(md, ud, cfg)
143
 
        
 
146
 
144
147
 
145
148
# returns tuple of filename (in 'dirname', and the contents of the file)
146
149
# on "not found", returns 'None' for filename and False for contents
147
150
def get_ovf_env(dirname):
148
 
    env_names = ("ovf-env.xml", "ovf_env.xml", "OVF_ENV.XML", "OVF-ENV.XML" )
 
151
    env_names = ("ovf-env.xml", "ovf_env.xml", "OVF_ENV.XML", "OVF-ENV.XML")
149
152
    for fname in env_names:
150
153
        if os.path.isfile("%s/%s" % (dirname, fname)):
151
154
            fp = open("%s/%s" % (dirname, fname))
154
157
            return(fname, contents)
155
158
    return(None, False)
156
159
 
 
160
 
157
161
# transport functions take no input and return
158
162
# a 3 tuple of content, path, filename
159
163
def transport_iso9660(require_iso=False):
160
164
 
161
 
    # default_regex matches values in 
 
165
    # default_regex matches values in
162
166
    # /lib/udev/rules.d/60-cdrom_id.rules
163
167
    # KERNEL!="sr[0-9]*|hd[a-z]|xvd*", GOTO="cdrom_end"
164
168
    envname = "CLOUD_INIT_CDROM_DEV_REGEX"
172
176
    mounts = fp.readlines()
173
177
    fp.close()
174
178
 
175
 
    mounted = { }
 
179
    mounted = {}
176
180
    for mpline in mounts:
177
181
        (dev, mp, fstype, _opts, _freq, _passno) = mpline.split()
178
182
        mounted[dev] = (dev, fstype, mp, False)
180
184
        if fstype != "iso9660" and require_iso:
181
185
            continue
182
186
 
183
 
        if cdmatch.match(dev[5:]) == None: # take off '/dev/'
 
187
        if cdmatch.match(dev[5:]) == None:  # take off '/dev/'
184
188
            continue
185
 
        
 
189
 
186
190
        (fname, contents) = get_ovf_env(mp)
187
191
        if contents is not False:
188
192
            return(contents, dev, fname)
217
221
            except:
218
222
                pass
219
223
 
220
 
        cmd = [ "mount", "-o", "ro", fullp, tmpd ]
 
224
        cmd = ["mount", "-o", "ro", fullp, tmpd]
221
225
        if require_iso:
222
226
            cmd.extend(('-t', 'iso9660'))
223
227
 
241
245
 
242
246
    return(False, None, None)
243
247
 
 
248
 
244
249
def transport_vmware_guestd():
245
250
    # pylint: disable=C0301
246
251
    # http://blogs.vmware.com/vapp/2009/07/selfconfiguration-and-the-ovf-environment.html
265
270
            ret.append(child)
266
271
    return(ret)
267
272
 
 
273
 
268
274
def getProperties(environString):
269
275
    dom = minidom.parseString(environString)
270
276
    if dom.documentElement.localName != "Environment":
275
281
 
276
282
    envNsURI = "http://schemas.dmtf.org/ovf/environment/1"
277
283
 
278
 
    # could also check here that elem.namespaceURI == 
 
284
    # could also check here that elem.namespaceURI ==
279
285
    #   "http://schemas.dmtf.org/ovf/environment/1"
280
286
    propSections = findChild(dom.documentElement,
281
287
        lambda n: n.localName == "PropertySection")
283
289
    if len(propSections) == 0:
284
290
        raise Exception("No 'PropertySection's")
285
291
 
286
 
    props = { }
 
292
    props = {}
287
293
    propElems = findChild(propSections[0], lambda n: n.localName == "Property")
288
294
 
289
295
    for elem in propElems:
293
299
 
294
300
    return(props)
295
301
 
 
302
 
296
303
datasources = (
297
 
  ( DataSourceOVF, ( DataSource.DEP_FILESYSTEM, ) ),
298
 
  ( DataSourceOVFNet, 
299
 
    ( DataSource.DEP_FILESYSTEM, DataSource.DEP_NETWORK ) ),
 
304
  (DataSourceOVF, (DataSource.DEP_FILESYSTEM, )),
 
305
  (DataSourceOVFNet,
 
306
    (DataSource.DEP_FILESYSTEM, DataSource.DEP_NETWORK)),
300
307
)
301
308
 
 
309
 
302
310
# return a list of data sources that match this set of dependencies
303
311
def get_datasource_list(depends):
304
312
    return(DataSource.list_from_depends(depends, datasources))
305
313
 
 
314
 
306
315
if __name__ == "__main__":
307
316
    import sys
308
317
    envStr = open(sys.argv[1]).read()
309
318
    props = getProperties(envStr)
310
319
    import pprint
311
320
    pprint.pprint(props)
312
 
    
 
321
 
313
322
    md, ud, cfg = read_ovf_environment(envStr)
314
323
    print "=== md ==="
315
324
    pprint.pprint(md)