akutz | dd794a4 | 2018-09-18 10:04:21 -0500 | [diff] [blame] | 1 | # Cloud-Init Datasource for VMware Guestinfo |
| 2 | # |
| 3 | # Copyright (c) 2018 VMware, Inc. All Rights Reserved. |
| 4 | # |
| 5 | # This product is licensed to you under the Apache 2.0 license (the "License"). |
| 6 | # You may not use this product except in compliance with the Apache 2.0 License. |
| 7 | # |
| 8 | # This product may include a number of subcomponents with separate copyright |
| 9 | # notices and license terms. Your use of these subcomponents is subject to the |
| 10 | # terms and conditions of the subcomponent's license, as noted in the LICENSE |
| 11 | # file. |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 12 | # |
akutz | 6501f90 | 2018-08-24 12:19:05 -0500 | [diff] [blame] | 13 | # Authors: Anish Swaminathan <anishs@vmware.com> |
| 14 | # Andrew Kutz <akutz@vmware.com> |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 15 | # |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 16 | |
| 17 | ''' |
| 18 | A cloud init datasource for VMware GuestInfo. |
| 19 | ''' |
| 20 | |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 21 | import base64 |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 22 | import collections |
akutz | 84389c8 | 2019-06-02 19:24:38 -0500 | [diff] [blame] | 23 | import copy |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 24 | from distutils.spawn import find_executable |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 25 | import json |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 26 | import os |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 27 | import socket |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 28 | import string |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 29 | import zlib |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 30 | |
| 31 | from cloudinit import log as logging |
| 32 | from cloudinit import sources |
| 33 | from cloudinit import util |
akutz | 6501f90 | 2018-08-24 12:19:05 -0500 | [diff] [blame] | 34 | from cloudinit import safeyaml |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 35 | |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 36 | from deepmerge import always_merger |
| 37 | import netifaces |
| 38 | |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 39 | LOG = logging.getLogger(__name__) |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 40 | NOVAL = "No value found" |
yvespp | 8d58dfd | 2019-10-29 20:42:09 +0100 | [diff] [blame^] | 41 | VMWARE_RPCTOOL = find_executable("vmware-rpctool") |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 42 | VMX_GUESTINFO = "VMX_GUESTINFO" |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 43 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 44 | |
| 45 | class NetworkConfigError(Exception): |
| 46 | ''' |
| 47 | NetworkConfigError is raised when there is an issue getting or |
| 48 | applying network configuration. |
| 49 | ''' |
| 50 | pass |
| 51 | |
| 52 | |
Andrew Kutz | 4f66b8b | 2018-09-16 18:28:59 -0500 | [diff] [blame] | 53 | class DataSourceVMwareGuestInfo(sources.DataSource): |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 54 | ''' |
| 55 | This cloud-init datasource was designed for use with CentOS 7, |
| 56 | which uses cloud-init 0.7.9. However, this datasource should |
| 57 | work with any Linux distribution for which cloud-init is |
| 58 | avaialble. |
| 59 | |
| 60 | The documentation for cloud-init 0.7.9's datasource is |
| 61 | available at http://bit.ly/cloudinit-datasource-0-7-9. The |
| 62 | current documentation for cloud-init is found at |
| 63 | https://cloudinit.readthedocs.io/en/latest/. |
| 64 | |
| 65 | Setting the hostname: |
| 66 | The hostname is set by way of the metadata key "local-hostname". |
| 67 | |
| 68 | Setting the instance ID: |
| 69 | The instance ID may be set by way of the metadata key "instance-id". |
| 70 | However, if this value is absent then then the instance ID is |
| 71 | read from the file /sys/class/dmi/id/product_uuid. |
| 72 | |
| 73 | Configuring the network: |
| 74 | The network is configured by setting the metadata key "network" |
| 75 | with a value consistent with Network Config Versions 1 or 2, |
| 76 | depending on the Linux distro's version of cloud-init: |
| 77 | |
| 78 | Network Config Version 1 - http://bit.ly/cloudinit-net-conf-v1 |
| 79 | Network Config Version 2 - http://bit.ly/cloudinit-net-conf-v2 |
| 80 | |
| 81 | For example, CentOS 7's official cloud-init package is version |
| 82 | 0.7.9 and does not support Network Config Version 2. However, |
| 83 | this datasource still supports supplying Network Config Version 2 |
| 84 | data as long as the Linux distro's cloud-init package is new |
| 85 | enough to parse the data. |
| 86 | |
| 87 | The metadata key "network.encoding" may be used to indicate the |
| 88 | format of the metadata key "network". Valid encodings are base64 |
| 89 | and gzip+base64. |
| 90 | ''' |
| 91 | |
| 92 | dsname = 'VMwareGuestInfo' |
| 93 | |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 94 | def __init__(self, sys_cfg, distro, paths, ud_proc=None): |
| 95 | sources.DataSource.__init__(self, sys_cfg, distro, paths, ud_proc) |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 96 | if not get_data_access_method(): |
yvespp | 8d58dfd | 2019-10-29 20:42:09 +0100 | [diff] [blame^] | 97 | LOG.error("Failed to find vmware-rpctool") |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 98 | |
| 99 | def get_data(self): |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 100 | """ |
| 101 | This method should really be _get_data in accordance with the most |
| 102 | recent versions of cloud-init. However, because the datasource |
| 103 | supports as far back as cloud-init 0.7.9, get_data is still used. |
| 104 | |
| 105 | Because of this the method attempts to do some of the same things |
| 106 | that the get_data functions in newer versions of cloud-init do, |
| 107 | such as calling persist_instance_data. |
| 108 | """ |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 109 | if not get_data_access_method(): |
yvespp | 8d58dfd | 2019-10-29 20:42:09 +0100 | [diff] [blame^] | 110 | LOG.error("vmware-rpctool is required to fetch guestinfo value") |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 111 | return False |
akutz | 6501f90 | 2018-08-24 12:19:05 -0500 | [diff] [blame] | 112 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 113 | # Get the metadata. |
| 114 | self.metadata = load_metadata() |
akutz | 6501f90 | 2018-08-24 12:19:05 -0500 | [diff] [blame] | 115 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 116 | # Get the user data. |
| 117 | self.userdata_raw = guestinfo('userdata') |
akutz | 6501f90 | 2018-08-24 12:19:05 -0500 | [diff] [blame] | 118 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 119 | # Get the vendor data. |
| 120 | self.vendordata_raw = guestinfo('vendordata') |
akutz | 6501f90 | 2018-08-24 12:19:05 -0500 | [diff] [blame] | 121 | |
Keerthana K | f0d27ea | 2019-09-05 21:46:31 +0530 | [diff] [blame] | 122 | if self.metadata or self.userdata_raw or self.vendordata_raw: |
| 123 | return True |
| 124 | else: |
| 125 | return False |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 126 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 127 | def setup(self, is_new_instance): |
| 128 | """setup(is_new_instance) |
| 129 | |
| 130 | This is called before user-data and vendor-data have been processed. |
| 131 | |
| 132 | Unless the datasource has set mode to 'local', then networking |
| 133 | per 'fallback' or per 'network_config' will have been written and |
| 134 | brought up the OS at this point. |
| 135 | """ |
| 136 | |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 137 | # Get information about the host. |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 138 | host_info = get_host_info() |
| 139 | LOG.info("got host-info: %s", host_info) |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 140 | |
| 141 | # Ensure the metadata gets updated with information about the |
| 142 | # host, including the network interfaces, default IP addresses, |
| 143 | # etc. |
| 144 | self.metadata = always_merger.merge(self.metadata, host_info) |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 145 | |
| 146 | # Persist the instance data for versions of cloud-init that support |
| 147 | # doing so. This occurs here rather than in the get_data call in |
| 148 | # order to ensure that the network interfaces are up and can be |
| 149 | # persisted with the metadata. |
| 150 | try: |
| 151 | self.persist_instance_data() |
| 152 | except AttributeError: |
| 153 | pass |
| 154 | |
akutz | 6501f90 | 2018-08-24 12:19:05 -0500 | [diff] [blame] | 155 | @property |
| 156 | def network_config(self): |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 157 | if 'network' in self.metadata: |
| 158 | LOG.debug("using metadata network config") |
| 159 | else: |
| 160 | LOG.debug("using fallback network config") |
| 161 | self.metadata['network'] = { |
| 162 | 'config': self.distro.generate_fallback_config(), |
| 163 | } |
| 164 | return self.metadata['network']['config'] |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 165 | |
| 166 | def get_instance_id(self): |
akutz | 6501f90 | 2018-08-24 12:19:05 -0500 | [diff] [blame] | 167 | # Pull the instance ID out of the metadata if present. Otherwise |
| 168 | # read the file /sys/class/dmi/id/product_uuid for the instance ID. |
| 169 | if self.metadata and 'instance-id' in self.metadata: |
| 170 | return self.metadata['instance-id'] |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 171 | with open('/sys/class/dmi/id/product_uuid', 'r') as id_file: |
Andrey Klimentyev | e1c5ed4 | 2019-10-09 12:32:44 +0300 | [diff] [blame] | 172 | self.metadata['instance-id'] = str(id_file.read()).rstrip().lower() |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 173 | return self.metadata['instance-id'] |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 174 | |
Andrey Klimentyev | a23229d | 2019-10-17 15:30:00 +0300 | [diff] [blame] | 175 | def get_public_ssh_keys(self): |
| 176 | public_keys_data = "" |
| 177 | if 'public-keys-data' in self.metadata: |
| 178 | public_keys_data = self.metadata['public-keys-data'].splitlines() |
| 179 | |
| 180 | public_keys = [] |
| 181 | if not public_keys_data: |
| 182 | return public_keys |
| 183 | |
| 184 | for public_key in public_keys_data: |
| 185 | public_keys.append(public_key) |
| 186 | |
| 187 | return public_keys |
| 188 | |
akutz | 6501f90 | 2018-08-24 12:19:05 -0500 | [diff] [blame] | 189 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 190 | def decode(key, enc_type, data): |
| 191 | ''' |
| 192 | decode returns the decoded string value of data |
| 193 | key is a string used to identify the data being decoded in log messages |
| 194 | ---- |
| 195 | In py 2.7: |
| 196 | json.loads method takes string as input |
| 197 | zlib.decompress takes and returns a string |
| 198 | base64.b64decode takes and returns a string |
| 199 | ----- |
| 200 | In py 3.6 and newer: |
| 201 | json.loads method takes bytes or string as input |
| 202 | zlib.decompress takes and returns a bytes |
| 203 | base64.b64decode takes bytes or string and returns bytes |
| 204 | ----- |
| 205 | In py > 3, < 3.6: |
| 206 | json.loads method takes string as input |
| 207 | zlib.decompress takes and returns a bytes |
| 208 | base64.b64decode takes bytes or string and returns bytes |
| 209 | ----- |
| 210 | Given the above conditions the output from zlib.decompress and |
| 211 | base64.b64decode would be bytes with newer python and str in older |
| 212 | version. Thus we would covert the output to str before returning |
| 213 | ''' |
| 214 | LOG.debug("Getting encoded data for key=%s, enc=%s", key, enc_type) |
akutz | 6501f90 | 2018-08-24 12:19:05 -0500 | [diff] [blame] | 215 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 216 | raw_data = None |
| 217 | if enc_type == "gzip+base64" or enc_type == "gz+b64": |
| 218 | LOG.debug("Decoding %s format %s", enc_type, key) |
| 219 | raw_data = zlib.decompress(base64.b64decode(data), zlib.MAX_WBITS | 16) |
| 220 | elif enc_type == "base64" or enc_type == "b64": |
| 221 | LOG.debug("Decoding %s format %s", enc_type, key) |
| 222 | raw_data = base64.b64decode(data) |
| 223 | else: |
| 224 | LOG.debug("Plain-text data %s", key) |
| 225 | raw_data = data |
Sidharth Surana | 3a42168 | 2018-10-10 15:42:08 -0700 | [diff] [blame] | 226 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 227 | if isinstance(raw_data, bytes): |
| 228 | return raw_data.decode('utf-8') |
| 229 | return raw_data |
| 230 | |
| 231 | |
| 232 | def get_guestinfo_value(key): |
| 233 | ''' |
| 234 | Returns a guestinfo value for the specified key. |
| 235 | ''' |
| 236 | LOG.debug("Getting guestinfo value for key %s", key) |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 237 | |
| 238 | data_access_method = get_data_access_method() |
| 239 | |
| 240 | if data_access_method == VMX_GUESTINFO: |
| 241 | env_key = ("vmx.guestinfo." + key).upper().replace(".", "_", -1) |
| 242 | val = os.environ.get(env_key, "") |
| 243 | if val == "": |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 244 | LOG.debug("No value found for key %s", key) |
| 245 | else: |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 246 | return val |
| 247 | |
yvespp | 8d58dfd | 2019-10-29 20:42:09 +0100 | [diff] [blame^] | 248 | if data_access_method == VMWARE_RPCTOOL: |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 249 | try: |
| 250 | (stdout, stderr) = util.subp( |
yvespp | 8d58dfd | 2019-10-29 20:42:09 +0100 | [diff] [blame^] | 251 | [VMWARE_RPCTOOL, "info-get guestinfo." + key]) |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 252 | if stderr == NOVAL: |
| 253 | LOG.debug("No value found for key %s", key) |
| 254 | elif not stdout: |
| 255 | LOG.error("Failed to get guestinfo value for key %s", key) |
| 256 | else: |
| 257 | return stdout.rstrip() |
| 258 | except util.ProcessExecutionError as error: |
| 259 | if error.stderr == NOVAL: |
| 260 | LOG.debug("No value found for key %s", key) |
| 261 | else: |
| 262 | util.logexc( |
| 263 | LOG, "Failed to get guestinfo value for key %s: %s", key, error) |
| 264 | except Exception: |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 265 | util.logexc( |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 266 | LOG, "Unexpected error while trying to get guestinfo value for key %s", key) |
| 267 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 268 | return None |
akutz | 6501f90 | 2018-08-24 12:19:05 -0500 | [diff] [blame] | 269 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 270 | |
| 271 | def guestinfo(key): |
| 272 | ''' |
| 273 | guestinfo returns the guestinfo value for the provided key, decoding |
| 274 | the value when required |
| 275 | ''' |
| 276 | data = get_guestinfo_value(key) |
| 277 | if not data: |
akutz | 6501f90 | 2018-08-24 12:19:05 -0500 | [diff] [blame] | 278 | return None |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 279 | enc_type = get_guestinfo_value(key + '.encoding') |
| 280 | return decode('guestinfo.' + key, enc_type, data) |
| 281 | |
| 282 | |
| 283 | def load(data): |
| 284 | ''' |
| 285 | load first attempts to unmarshal the provided data as JSON, and if |
| 286 | that fails then attempts to unmarshal the data as YAML. If data is |
| 287 | None then a new dictionary is returned. |
| 288 | ''' |
| 289 | if not data: |
| 290 | return {} |
| 291 | try: |
| 292 | return json.loads(data) |
| 293 | except: |
| 294 | return safeyaml.load(data) |
| 295 | |
| 296 | |
| 297 | def load_metadata(): |
| 298 | ''' |
| 299 | load_metadata loads the metadata from the guestinfo data, optionally |
| 300 | decoding the network config when required |
| 301 | ''' |
| 302 | data = load(guestinfo('metadata')) |
akutz | 84389c8 | 2019-06-02 19:24:38 -0500 | [diff] [blame] | 303 | LOG.debug('loaded metadata %s', data) |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 304 | |
| 305 | network = None |
| 306 | if 'network' in data: |
| 307 | network = data['network'] |
| 308 | del data['network'] |
| 309 | |
| 310 | network_enc = None |
| 311 | if 'network.encoding' in data: |
| 312 | network_enc = data['network.encoding'] |
| 313 | del data['network.encoding'] |
| 314 | |
| 315 | if network: |
akutz | 84389c8 | 2019-06-02 19:24:38 -0500 | [diff] [blame] | 316 | LOG.debug('network data found') |
| 317 | if isinstance(network, collections.Mapping): |
| 318 | LOG.debug("network data copied to 'config' key") |
| 319 | network = { |
| 320 | 'config': copy.deepcopy(network) |
| 321 | } |
| 322 | else: |
| 323 | LOG.debug("network data to be decoded %s", network) |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 324 | dec_net = decode('metadata.network', network_enc, network) |
akutz | 84389c8 | 2019-06-02 19:24:38 -0500 | [diff] [blame] | 325 | network = { |
| 326 | 'config': load(dec_net), |
| 327 | } |
| 328 | |
| 329 | LOG.debug('network data %s', network) |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 330 | data['network'] = network |
| 331 | |
| 332 | return data |
| 333 | |
akutz | 6501f90 | 2018-08-24 12:19:05 -0500 | [diff] [blame] | 334 | |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 335 | def get_datasource_list(depends): |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 336 | ''' |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 337 | Return a list of data sources that match this set of dependencies |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 338 | ''' |
Andrew Kutz | 4f66b8b | 2018-09-16 18:28:59 -0500 | [diff] [blame] | 339 | return [DataSourceVMwareGuestInfo] |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 340 | |
| 341 | |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 342 | def get_default_ip_addrs(): |
| 343 | ''' |
| 344 | Returns the default IPv4 and IPv6 addresses based on the device(s) used for |
| 345 | the default route. Please note that None may be returned for either address |
| 346 | family if that family has no default route or if there are multiple |
| 347 | addresses associated with the device used by the default route for a given |
| 348 | address. |
| 349 | ''' |
| 350 | gateways = netifaces.gateways() |
| 351 | if 'default' not in gateways: |
| 352 | return None, None |
| 353 | |
| 354 | default_gw = gateways['default'] |
| 355 | if netifaces.AF_INET not in default_gw and netifaces.AF_INET6 not in default_gw: |
| 356 | return None, None |
| 357 | |
| 358 | ipv4 = None |
| 359 | ipv6 = None |
| 360 | |
| 361 | gw4 = default_gw.get(netifaces.AF_INET) |
| 362 | if gw4: |
| 363 | _, dev4 = gw4 |
akutz | 0b519f7 | 2019-06-02 14:58:57 -0500 | [diff] [blame] | 364 | addr4_fams = netifaces.ifaddresses(dev4) |
| 365 | if addr4_fams: |
| 366 | af_inet4 = addr4_fams.get(netifaces.AF_INET) |
| 367 | if af_inet4: |
| 368 | if len(af_inet4) > 1: |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 369 | LOG.warn( |
akutz | 0b519f7 | 2019-06-02 14:58:57 -0500 | [diff] [blame] | 370 | "device %s has more than one ipv4 address: %s", dev4, af_inet4) |
| 371 | elif 'addr' in af_inet4[0]: |
| 372 | ipv4 = af_inet4[0]['addr'] |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 373 | |
| 374 | # Try to get the default IPv6 address by first seeing if there is a default |
akutz | 0b519f7 | 2019-06-02 14:58:57 -0500 | [diff] [blame] | 375 | # IPv6 route. |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 376 | gw6 = default_gw.get(netifaces.AF_INET6) |
| 377 | if gw6: |
| 378 | _, dev6 = gw6 |
| 379 | addr6_fams = netifaces.ifaddresses(dev6) |
| 380 | if addr6_fams: |
| 381 | af_inet6 = addr6_fams.get(netifaces.AF_INET6) |
| 382 | if af_inet6: |
| 383 | if len(af_inet6) > 1: |
| 384 | LOG.warn( |
| 385 | "device %s has more than one ipv6 address: %s", dev6, af_inet6) |
| 386 | elif 'addr' in af_inet6[0]: |
| 387 | ipv6 = af_inet6[0]['addr'] |
akutz | 0b519f7 | 2019-06-02 14:58:57 -0500 | [diff] [blame] | 388 | |
| 389 | # If there is a default IPv4 address but not IPv6, then see if there is a |
| 390 | # single IPv6 address associated with the same device associated with the |
| 391 | # default IPv4 address. |
| 392 | if ipv4 and not ipv6: |
| 393 | af_inet6 = addr4_fams.get(netifaces.AF_INET6) |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 394 | if af_inet6: |
| 395 | if len(af_inet6) > 1: |
| 396 | LOG.warn( |
| 397 | "device %s has more than one ipv6 address: %s", dev4, af_inet6) |
| 398 | elif 'addr' in af_inet6[0]: |
| 399 | ipv6 = af_inet6[0]['addr'] |
| 400 | |
akutz | 0b519f7 | 2019-06-02 14:58:57 -0500 | [diff] [blame] | 401 | # If there is a default IPv6 address but not IPv4, then see if there is a |
| 402 | # single IPv4 address associated with the same device associated with the |
| 403 | # default IPv6 address. |
| 404 | if not ipv4 and ipv6: |
| 405 | af_inet4 = addr6_fams.get(netifaces.AF_INET4) |
| 406 | if af_inet4: |
| 407 | if len(af_inet4) > 1: |
| 408 | LOG.warn( |
| 409 | "device %s has more than one ipv4 address: %s", dev6, af_inet4) |
| 410 | elif 'addr' in af_inet4[0]: |
| 411 | ipv4 = af_inet4[0]['addr'] |
| 412 | |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 413 | return ipv4, ipv6 |
| 414 | |
Keerthana K | f0d27ea | 2019-09-05 21:46:31 +0530 | [diff] [blame] | 415 | # patched socket.getfqdn() - see https://bugs.python.org/issue5004 |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 416 | |
| 417 | |
Keerthana K | f0d27ea | 2019-09-05 21:46:31 +0530 | [diff] [blame] | 418 | def getfqdn(name=''): |
| 419 | """Get fully qualified domain name from name. |
| 420 | An empty argument is interpreted as meaning the local host. |
| 421 | """ |
| 422 | name = name.strip() |
| 423 | if not name or name == '0.0.0.0': |
| 424 | name = socket.gethostname() |
| 425 | try: |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 426 | addrs = socket.getaddrinfo( |
| 427 | name, None, 0, socket.SOCK_DGRAM, 0, socket.AI_CANONNAME) |
Keerthana K | f0d27ea | 2019-09-05 21:46:31 +0530 | [diff] [blame] | 428 | except socket.error: |
| 429 | pass |
| 430 | else: |
| 431 | for addr in addrs: |
| 432 | if addr[3]: |
| 433 | name = addr[3] |
| 434 | break |
| 435 | return name |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 436 | |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 437 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 438 | def get_host_info(): |
| 439 | ''' |
| 440 | Returns host information such as the host name and network interfaces. |
| 441 | ''' |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 442 | |
| 443 | host_info = { |
| 444 | 'network': { |
| 445 | 'interfaces': { |
| 446 | 'by-mac': collections.OrderedDict(), |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 447 | 'by-ipv4': collections.OrderedDict(), |
| 448 | 'by-ipv6': collections.OrderedDict(), |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 449 | }, |
| 450 | }, |
| 451 | } |
| 452 | |
Keerthana K | f0d27ea | 2019-09-05 21:46:31 +0530 | [diff] [blame] | 453 | hostname = getfqdn(socket.gethostname()) |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 454 | if hostname: |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 455 | host_info['hostname'] = hostname |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 456 | host_info['local-hostname'] = hostname |
| 457 | |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 458 | default_ipv4, default_ipv6 = get_default_ip_addrs() |
| 459 | if default_ipv4: |
| 460 | host_info['local-ipv4'] = default_ipv4 |
| 461 | if default_ipv6: |
| 462 | host_info['local-ipv6'] = default_ipv6 |
| 463 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 464 | by_mac = host_info['network']['interfaces']['by-mac'] |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 465 | by_ipv4 = host_info['network']['interfaces']['by-ipv4'] |
| 466 | by_ipv6 = host_info['network']['interfaces']['by-ipv6'] |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 467 | |
| 468 | ifaces = netifaces.interfaces() |
| 469 | for dev_name in ifaces: |
| 470 | addr_fams = netifaces.ifaddresses(dev_name) |
| 471 | af_link = addr_fams.get(netifaces.AF_LINK) |
akutz | 0b519f7 | 2019-06-02 14:58:57 -0500 | [diff] [blame] | 472 | af_inet4 = addr_fams.get(netifaces.AF_INET) |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 473 | af_inet6 = addr_fams.get(netifaces.AF_INET6) |
| 474 | |
| 475 | mac = None |
| 476 | if af_link and 'addr' in af_link[0]: |
| 477 | mac = af_link[0]['addr'] |
| 478 | |
| 479 | # Do not bother recording localhost |
| 480 | if mac == "00:00:00:00:00:00": |
| 481 | continue |
| 482 | |
akutz | 0b519f7 | 2019-06-02 14:58:57 -0500 | [diff] [blame] | 483 | if mac and (af_inet4 or af_inet6): |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 484 | key = mac |
| 485 | val = {} |
akutz | 0b519f7 | 2019-06-02 14:58:57 -0500 | [diff] [blame] | 486 | if af_inet4: |
| 487 | val["ipv4"] = af_inet4 |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 488 | if af_inet6: |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 489 | val["ipv6"] = af_inet6 |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 490 | by_mac[key] = val |
| 491 | |
akutz | 0b519f7 | 2019-06-02 14:58:57 -0500 | [diff] [blame] | 492 | if af_inet4: |
| 493 | for ip_info in af_inet4: |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 494 | key = ip_info['addr'] |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 495 | if key == '127.0.0.1': |
| 496 | continue |
akutz | 84389c8 | 2019-06-02 19:24:38 -0500 | [diff] [blame] | 497 | val = copy.deepcopy(ip_info) |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 498 | del val['addr'] |
| 499 | if mac: |
| 500 | val['mac'] = mac |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 501 | by_ipv4[key] = val |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 502 | |
| 503 | if af_inet6: |
| 504 | for ip_info in af_inet6: |
| 505 | key = ip_info['addr'] |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 506 | if key == '::1': |
| 507 | continue |
akutz | 84389c8 | 2019-06-02 19:24:38 -0500 | [diff] [blame] | 508 | val = copy.deepcopy(ip_info) |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 509 | del val['addr'] |
| 510 | if mac: |
| 511 | val['mac'] = mac |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 512 | by_ipv6[key] = val |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 513 | |
| 514 | return host_info |
| 515 | |
| 516 | |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 517 | def get_data_access_method(): |
| 518 | if os.environ.get(VMX_GUESTINFO, ""): |
| 519 | return VMX_GUESTINFO |
yvespp | 8d58dfd | 2019-10-29 20:42:09 +0100 | [diff] [blame^] | 520 | if VMWARE_RPCTOOL: |
| 521 | return VMWARE_RPCTOOL |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 522 | return None |
| 523 | |
| 524 | |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 525 | def main(): |
| 526 | ''' |
| 527 | Executed when this file is used as a program. |
| 528 | ''' |
| 529 | metadata = {'network': {'config': {'dhcp': True}}} |
| 530 | host_info = get_host_info() |
| 531 | metadata = always_merger.merge(metadata, host_info) |
| 532 | print(util.json_dumps(metadata)) |
| 533 | |
| 534 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 535 | if __name__ == "__main__": |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 536 | main() |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 537 | |
| 538 | # vi: ts=4 expandtab |