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/>.
19
import cloudinit.DataSource as DataSource
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
32
34
class DataSourceOVF(DataSource.DataSource):
34
seeddir = seeddir + '/ovf'
36
seeddir = base_seeddir + '/ovf'
37
39
userdata_raw = None
39
supported_seed_starts = ( "/" , "file://" )
41
supported_seed_starts = ("/", "file://")
42
44
mstr = "DataSourceOVF"
46
48
def get_data(self):
52
"instance-id" : "iid-dsovf"
54
"instance-id": "iid-dsovf"
55
(seedfile, contents) = get_ovf_env(seeddir)
57
(seedfile, contents) = get_ovf_env(base_seeddir)
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
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()
106
107
def get_public_ssh_keys(self):
107
108
if not 'public-keys' in self.metadata:
109
return([self.metadata['public-keys'],])
110
return([self.metadata['public-keys'], ])
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):
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://")
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)
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"
142
145
return(md, ud, cfg)
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)
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):
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()
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:
183
if cdmatch.match(dev[5:]) == None: # take off '/dev/'
187
if cdmatch.match(dev[5:]) == None: # take off '/dev/'
186
190
(fname, contents) = get_ovf_env(mp)
187
191
if contents is not False:
188
192
return(contents, dev, fname)
220
cmd = [ "mount", "-o", "ro", fullp, tmpd ]
224
cmd = ["mount", "-o", "ro", fullp, tmpd]
222
226
cmd.extend(('-t', 'iso9660'))
276
282
envNsURI = "http://schemas.dmtf.org/ovf/environment/1"
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")
297
( DataSourceOVF, ( DataSource.DEP_FILESYSTEM, ) ),
299
( DataSource.DEP_FILESYSTEM, DataSource.DEP_NETWORK ) ),
304
(DataSourceOVF, (DataSource.DEP_FILESYSTEM, )),
306
(DataSource.DEP_FILESYSTEM, DataSource.DEP_NETWORK)),
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))
306
315
if __name__ == "__main__":
308
317
envStr = open(sys.argv[1]).read()
309
318
props = getProperties(envStr)
311
320
pprint.pprint(props)
313
322
md, ud, cfg = read_ovf_environment(envStr)
314
323
print "=== md ==="
315
324
pprint.pprint(md)