forked from xing/cloudinit-vmware-guestinfo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataSourceVmwareGuestinfo.py
155 lines (137 loc) · 5.03 KB
/
DataSourceVmwareGuestinfo.py
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
from __future__ import print_function
import subprocess
import os
import json
import xml.etree.ElementTree as ET
import cloudinit.util as util
LOG = None
try:
from cloudinit import log
LOG = log.getLogger(__name__)
except AttributeError:
LOG = log
DS = None
NETWORK_VIA_DISTRO = True
try:
# cloudinit 0.7.*
from cloudinit import sources
DS = sources.DataSource
except ImportError:
# cloudinit 0.6.*
from cloudinit import DataSource
NETWORK_VIA_DISTRO = False
DS = DataSource.DataSource
try:
from cloudinit import stages
except ImportError:
stages = None
class DataSourceVmwareGuestinfo(DS):
class CommunicationError(Exception):
pass
def get_data(self):
rpctool = self._which("vmware-rpctool")
if rpctool is None:
LOG.info("No vmware-rpctool found (PATH is %s)" % self._paths())
return False
try:
self.userdata_raw = self._guestinfo("cloudinit.userdata")
if self.userdata_raw is None:
return False
self.metadata = {}
meta = self._guestinfo("cloudinit.metadata")
if meta is not None:
try:
self.metadata = json.loads(meta)
except ValueError as e:
LOG.error("Failed to decode json %r: %r", e, meta)
return False
ovf = self._guestinfo("ovfEnv")
if ovf is not None:
try:
self.metadata.update( self._parse_ovf(ovf) )
except ValueError as e:
LOG.error("Failed to parse ovf %r: %r", e, ovf)
return False
except (CommunicationError, OSError) as e:
LOG.error(e)
return False
self._network_interfaces_from_metadata()
return True
def _network_interfaces_from_metadata(self):
"""Brings up the network"""
if 'network-interfaces' in self.metadata:
if NETWORK_VIA_DISTRO:
self._network_interfaces_via_distro()
else:
self._network_interfaces_direct()
def _network_interfaces_via_distro(self):
self.distro.apply_network(self.metadata['network-interfaces'])
def _network_interfaces_direct(self):
util.write_file("/etc/network/interfaces",
self.metadata['network-interfaces'])
try:
(out, err) = util.subp(['ifup', '--all'])
if len(out) or len(err):
LOG.warn("ifup --all had stderr: %s" % err)
except subprocess.CalledProcessError as exc:
LOG.warn("ifup --all failed: %s" % (exc.output[1]))
def _parse_ovf(self, ovf):
"""Parses ovfEnv guestinfo"""
if ovf == "":
return {}
tree = ET.fromstring(ovf)
rt = {}
for props in tree.findall("{http://schemas.dmtf.org/ovf/environment/1}PropertySection"):
for prop in props:
rt[ prop.attrib["{http://schemas.dmtf.org/ovf/environment/1}key"] ] = prop.attrib["{http://schemas.dmtf.org/ovf/environment/1}value"]
return rt
def _which(self, filename):
"""Finds an executable"""
candidates = []
for location in self._paths():
candidate = os.path.join(location, filename)
if os.path.isfile(candidate):
return candidate
return None
def _guestinfo(self, key):
rpctool = self._which("vmware-rpctool")
if rpctool is None:
LOG.info("No vmware-rpctool found (PATH is %s)" % self._paths())
return False
LOG.debug("Running %s 'info-get guestinfo.%s'", rpctool, key)
p1 = subprocess.Popen([rpctool,"info-get guestinfo."+key], stdout=subprocess.PIPE, stdin=None)
ud, _ = p1.communicate()
if p1.returncode == 1:
LOG.info("vmware-rpctool found no guestinfo.%s",key)
return None
if p1.returncode != 0:
raise CommunicationError("vmware-rpctool exited with %d" % p1.returncode)
return ud.decode(encoding="UTF-8")
def _paths(self):
locations = os.environ.get("PATH").split(os.pathsep)
if 'path' in self.ds_cfg:
locations = self.ds_cfg["path"] + locations
return locations
def get_instance_id(self):
if not self.metadata or 'instance-id' not in self.metadata:
# vmware puts a uuid in the bios
with open('/sys/class/dmi/id/product_uuid', 'r') as f:
return str(f.read()).rstrip()
return str(self.metadata['instance-id'])
def get_datasource_list(depends):
"""
Return a list of data sources that match this set of dependencies
"""
return [DataSourceVmwareGuestinfo]
def main():
log.setupLogging()
init = stages.Init()
s = DataSourceVmwareGuestinfo(init.cfg, init.distro, init.paths)
if s.get_data():
print("Found data")
else:
print("Didn't find data")
print("userdata_raw: %r" % s.userdata_raw)
print("metadata: %r" % s.metadata)
if __name__ == "__main__":
main()