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 | 33dbfc2 | 2020-03-23 13:43:07 -0500 | [diff] [blame] | 25 | from distutils.util import strtobool |
akutz | 1cce7fa | 2020-03-24 11:01:45 -0500 | [diff] [blame] | 26 | import ipaddress |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 27 | import json |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 28 | import os |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 29 | import socket |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 30 | import string |
akutz | 33dbfc2 | 2020-03-23 13:43:07 -0500 | [diff] [blame] | 31 | import time |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 32 | import zlib |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 33 | |
| 34 | from cloudinit import log as logging |
| 35 | from cloudinit import sources |
| 36 | from cloudinit import util |
akutz | 6501f90 | 2018-08-24 12:19:05 -0500 | [diff] [blame] | 37 | from cloudinit import safeyaml |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 38 | |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 39 | from deepmerge import always_merger |
| 40 | import netifaces |
| 41 | |
Yves Peter | 444519c | 2020-09-10 17:19:46 +0200 | [diff] [blame] | 42 | # in cloud init >= 20.3 subp is in its own module |
| 43 | try: |
| 44 | import subp |
| 45 | except ImportError: |
| 46 | subp_module = util |
| 47 | else: |
| 48 | # early versions of the subp module don't have the subp function |
| 49 | if hasattr(subp, 'subp'): |
| 50 | subp_module = subp |
| 51 | else: |
| 52 | subp_module = util |
| 53 | |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 54 | LOG = logging.getLogger(__name__) |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 55 | NOVAL = "No value found" |
yvespp | 8d58dfd | 2019-10-29 20:42:09 +0100 | [diff] [blame] | 56 | VMWARE_RPCTOOL = find_executable("vmware-rpctool") |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 57 | VMX_GUESTINFO = "VMX_GUESTINFO" |
akutz | daf81f1 | 2019-12-08 11:20:33 -0600 | [diff] [blame] | 58 | GUESTINFO_EMPTY_YAML_VAL = "---" |
akutz | 909cf9a | 2019-12-09 09:17:00 -0600 | [diff] [blame] | 59 | LOCAL_IPV4 = 'local-ipv4' |
| 60 | LOCAL_IPV6 = 'local-ipv6' |
Yves Peter | 59c869d | 2019-12-11 13:09:14 +0100 | [diff] [blame] | 61 | CLEANUP_GUESTINFO = 'cleanup-guestinfo' |
akutz | 33dbfc2 | 2020-03-23 13:43:07 -0500 | [diff] [blame] | 62 | WAIT_ON_NETWORK = 'wait-on-network' |
| 63 | WAIT_ON_NETWORK_IPV4 = 'ipv4' |
| 64 | WAIT_ON_NETWORK_IPV6 = 'ipv6' |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 65 | |
| 66 | class NetworkConfigError(Exception): |
| 67 | ''' |
| 68 | NetworkConfigError is raised when there is an issue getting or |
| 69 | applying network configuration. |
| 70 | ''' |
| 71 | pass |
| 72 | |
| 73 | |
Andrew Kutz | 4f66b8b | 2018-09-16 18:28:59 -0500 | [diff] [blame] | 74 | class DataSourceVMwareGuestInfo(sources.DataSource): |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 75 | ''' |
| 76 | This cloud-init datasource was designed for use with CentOS 7, |
| 77 | which uses cloud-init 0.7.9. However, this datasource should |
| 78 | work with any Linux distribution for which cloud-init is |
| 79 | avaialble. |
| 80 | |
| 81 | The documentation for cloud-init 0.7.9's datasource is |
| 82 | available at http://bit.ly/cloudinit-datasource-0-7-9. The |
| 83 | current documentation for cloud-init is found at |
| 84 | https://cloudinit.readthedocs.io/en/latest/. |
| 85 | |
| 86 | Setting the hostname: |
| 87 | The hostname is set by way of the metadata key "local-hostname". |
| 88 | |
| 89 | Setting the instance ID: |
| 90 | The instance ID may be set by way of the metadata key "instance-id". |
| 91 | However, if this value is absent then then the instance ID is |
| 92 | read from the file /sys/class/dmi/id/product_uuid. |
| 93 | |
| 94 | Configuring the network: |
| 95 | The network is configured by setting the metadata key "network" |
| 96 | with a value consistent with Network Config Versions 1 or 2, |
| 97 | depending on the Linux distro's version of cloud-init: |
| 98 | |
| 99 | Network Config Version 1 - http://bit.ly/cloudinit-net-conf-v1 |
| 100 | Network Config Version 2 - http://bit.ly/cloudinit-net-conf-v2 |
| 101 | |
| 102 | For example, CentOS 7's official cloud-init package is version |
| 103 | 0.7.9 and does not support Network Config Version 2. However, |
| 104 | this datasource still supports supplying Network Config Version 2 |
| 105 | data as long as the Linux distro's cloud-init package is new |
| 106 | enough to parse the data. |
| 107 | |
| 108 | The metadata key "network.encoding" may be used to indicate the |
| 109 | format of the metadata key "network". Valid encodings are base64 |
| 110 | and gzip+base64. |
| 111 | ''' |
| 112 | |
| 113 | dsname = 'VMwareGuestInfo' |
| 114 | |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 115 | def __init__(self, sys_cfg, distro, paths, ud_proc=None): |
| 116 | sources.DataSource.__init__(self, sys_cfg, distro, paths, ud_proc) |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 117 | if not get_data_access_method(): |
yvespp | 8d58dfd | 2019-10-29 20:42:09 +0100 | [diff] [blame] | 118 | LOG.error("Failed to find vmware-rpctool") |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 119 | |
| 120 | def get_data(self): |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 121 | """ |
| 122 | This method should really be _get_data in accordance with the most |
| 123 | recent versions of cloud-init. However, because the datasource |
| 124 | supports as far back as cloud-init 0.7.9, get_data is still used. |
| 125 | |
| 126 | Because of this the method attempts to do some of the same things |
| 127 | that the get_data functions in newer versions of cloud-init do, |
| 128 | such as calling persist_instance_data. |
| 129 | """ |
akutz | dbce3d9 | 2019-12-08 00:09:11 -0600 | [diff] [blame] | 130 | data_access_method = get_data_access_method() |
| 131 | if not data_access_method: |
yvespp | 8d58dfd | 2019-10-29 20:42:09 +0100 | [diff] [blame] | 132 | LOG.error("vmware-rpctool is required to fetch guestinfo value") |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 133 | return False |
akutz | 6501f90 | 2018-08-24 12:19:05 -0500 | [diff] [blame] | 134 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 135 | # Get the metadata. |
| 136 | self.metadata = load_metadata() |
akutz | 6501f90 | 2018-08-24 12:19:05 -0500 | [diff] [blame] | 137 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 138 | # Get the user data. |
| 139 | self.userdata_raw = guestinfo('userdata') |
akutz | 6501f90 | 2018-08-24 12:19:05 -0500 | [diff] [blame] | 140 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 141 | # Get the vendor data. |
| 142 | self.vendordata_raw = guestinfo('vendordata') |
akutz | 6501f90 | 2018-08-24 12:19:05 -0500 | [diff] [blame] | 143 | |
akutz | dbce3d9 | 2019-12-08 00:09:11 -0600 | [diff] [blame] | 144 | # Check to see if any of the guestinfo data should be removed. |
Yves Peter | 59c869d | 2019-12-11 13:09:14 +0100 | [diff] [blame] | 145 | if data_access_method == VMWARE_RPCTOOL and CLEANUP_GUESTINFO in self.metadata: |
| 146 | clear_guestinfo_keys(self.metadata[CLEANUP_GUESTINFO]) |
akutz | dbce3d9 | 2019-12-08 00:09:11 -0600 | [diff] [blame] | 147 | |
Keerthana K | f0d27ea | 2019-09-05 21:46:31 +0530 | [diff] [blame] | 148 | if self.metadata or self.userdata_raw or self.vendordata_raw: |
| 149 | return True |
| 150 | else: |
| 151 | return False |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 152 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 153 | def setup(self, is_new_instance): |
| 154 | """setup(is_new_instance) |
| 155 | |
| 156 | This is called before user-data and vendor-data have been processed. |
| 157 | |
| 158 | Unless the datasource has set mode to 'local', then networking |
| 159 | per 'fallback' or per 'network_config' will have been written and |
| 160 | brought up the OS at this point. |
| 161 | """ |
| 162 | |
akutz | 33dbfc2 | 2020-03-23 13:43:07 -0500 | [diff] [blame] | 163 | host_info = wait_on_network(self.metadata) |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 164 | LOG.info("got host-info: %s", host_info) |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 165 | |
akutz | 909cf9a | 2019-12-09 09:17:00 -0600 | [diff] [blame] | 166 | # Reflect any possible local IPv4 or IPv6 addresses in the guest |
| 167 | # info. |
| 168 | advertise_local_ip_addrs(host_info) |
| 169 | |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 170 | # Ensure the metadata gets updated with information about the |
| 171 | # host, including the network interfaces, default IP addresses, |
| 172 | # etc. |
| 173 | self.metadata = always_merger.merge(self.metadata, host_info) |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 174 | |
| 175 | # Persist the instance data for versions of cloud-init that support |
| 176 | # doing so. This occurs here rather than in the get_data call in |
| 177 | # order to ensure that the network interfaces are up and can be |
| 178 | # persisted with the metadata. |
| 179 | try: |
| 180 | self.persist_instance_data() |
| 181 | except AttributeError: |
| 182 | pass |
| 183 | |
akutz | 6501f90 | 2018-08-24 12:19:05 -0500 | [diff] [blame] | 184 | @property |
| 185 | def network_config(self): |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 186 | if 'network' in self.metadata: |
| 187 | LOG.debug("using metadata network config") |
| 188 | else: |
| 189 | LOG.debug("using fallback network config") |
| 190 | self.metadata['network'] = { |
| 191 | 'config': self.distro.generate_fallback_config(), |
| 192 | } |
| 193 | return self.metadata['network']['config'] |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 194 | |
| 195 | def get_instance_id(self): |
akutz | 6501f90 | 2018-08-24 12:19:05 -0500 | [diff] [blame] | 196 | # Pull the instance ID out of the metadata if present. Otherwise |
| 197 | # read the file /sys/class/dmi/id/product_uuid for the instance ID. |
| 198 | if self.metadata and 'instance-id' in self.metadata: |
| 199 | return self.metadata['instance-id'] |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 200 | with open('/sys/class/dmi/id/product_uuid', 'r') as id_file: |
Andrey Klimentyev | e1c5ed4 | 2019-10-09 12:32:44 +0300 | [diff] [blame] | 201 | self.metadata['instance-id'] = str(id_file.read()).rstrip().lower() |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 202 | return self.metadata['instance-id'] |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 203 | |
Andrey Klimentyev | a23229d | 2019-10-17 15:30:00 +0300 | [diff] [blame] | 204 | def get_public_ssh_keys(self): |
| 205 | public_keys_data = "" |
| 206 | if 'public-keys-data' in self.metadata: |
| 207 | public_keys_data = self.metadata['public-keys-data'].splitlines() |
| 208 | |
| 209 | public_keys = [] |
| 210 | if not public_keys_data: |
| 211 | return public_keys |
| 212 | |
| 213 | for public_key in public_keys_data: |
| 214 | public_keys.append(public_key) |
| 215 | |
| 216 | return public_keys |
| 217 | |
akutz | 6501f90 | 2018-08-24 12:19:05 -0500 | [diff] [blame] | 218 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 219 | def decode(key, enc_type, data): |
| 220 | ''' |
| 221 | decode returns the decoded string value of data |
| 222 | key is a string used to identify the data being decoded in log messages |
| 223 | ---- |
| 224 | In py 2.7: |
| 225 | json.loads method takes string as input |
| 226 | zlib.decompress takes and returns a string |
| 227 | base64.b64decode takes and returns a string |
| 228 | ----- |
| 229 | In py 3.6 and newer: |
| 230 | json.loads method takes bytes or string as input |
| 231 | zlib.decompress takes and returns a bytes |
| 232 | base64.b64decode takes bytes or string and returns bytes |
| 233 | ----- |
| 234 | In py > 3, < 3.6: |
| 235 | json.loads method takes string as input |
| 236 | zlib.decompress takes and returns a bytes |
| 237 | base64.b64decode takes bytes or string and returns bytes |
| 238 | ----- |
| 239 | Given the above conditions the output from zlib.decompress and |
| 240 | base64.b64decode would be bytes with newer python and str in older |
| 241 | version. Thus we would covert the output to str before returning |
| 242 | ''' |
| 243 | LOG.debug("Getting encoded data for key=%s, enc=%s", key, enc_type) |
akutz | 6501f90 | 2018-08-24 12:19:05 -0500 | [diff] [blame] | 244 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 245 | raw_data = None |
| 246 | if enc_type == "gzip+base64" or enc_type == "gz+b64": |
| 247 | LOG.debug("Decoding %s format %s", enc_type, key) |
| 248 | raw_data = zlib.decompress(base64.b64decode(data), zlib.MAX_WBITS | 16) |
| 249 | elif enc_type == "base64" or enc_type == "b64": |
| 250 | LOG.debug("Decoding %s format %s", enc_type, key) |
| 251 | raw_data = base64.b64decode(data) |
| 252 | else: |
| 253 | LOG.debug("Plain-text data %s", key) |
| 254 | raw_data = data |
Sidharth Surana | 3a42168 | 2018-10-10 15:42:08 -0700 | [diff] [blame] | 255 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 256 | if isinstance(raw_data, bytes): |
| 257 | return raw_data.decode('utf-8') |
| 258 | return raw_data |
| 259 | |
| 260 | |
akutz | daf81f1 | 2019-12-08 11:20:33 -0600 | [diff] [blame] | 261 | def get_none_if_empty_val(val): |
| 262 | ''' |
| 263 | get_none_if_empty_val returns None if the provided value, once stripped |
| 264 | of its trailing whitespace, is empty or equal to GUESTINFO_EMPTY_YAML_VAL. |
| 265 | |
| 266 | The return value is always a string, regardless of whether the input is |
| 267 | a bytes class or a string. |
| 268 | ''' |
| 269 | |
| 270 | # If the provided value is a bytes class, convert it to a string to |
| 271 | # simplify the rest of this function's logic. |
| 272 | if isinstance(val, bytes): |
| 273 | val = val.decode() |
| 274 | |
| 275 | val = val.rstrip() |
| 276 | if len(val) == 0 or val == GUESTINFO_EMPTY_YAML_VAL: |
| 277 | return None |
| 278 | return val |
| 279 | |
| 280 | |
akutz | 909cf9a | 2019-12-09 09:17:00 -0600 | [diff] [blame] | 281 | def advertise_local_ip_addrs(host_info): |
| 282 | ''' |
| 283 | advertise_local_ip_addrs gets the local IP address information from |
| 284 | the provided host_info map and sets the addresses in the guestinfo |
| 285 | namespace |
| 286 | ''' |
| 287 | if not host_info: |
| 288 | return |
| 289 | |
| 290 | # Reflect any possible local IPv4 or IPv6 addresses in the guest |
| 291 | # info. |
Yves Peter | 9dcf3dc | 2019-12-11 13:12:34 +0100 | [diff] [blame] | 292 | local_ipv4 = host_info.get(LOCAL_IPV4) |
akutz | 909cf9a | 2019-12-09 09:17:00 -0600 | [diff] [blame] | 293 | if local_ipv4: |
| 294 | set_guestinfo_value(LOCAL_IPV4, local_ipv4) |
| 295 | LOG.info("advertised local ipv4 address %s in guestinfo", local_ipv4) |
| 296 | |
Yves Peter | 9dcf3dc | 2019-12-11 13:12:34 +0100 | [diff] [blame] | 297 | local_ipv6 = host_info.get(LOCAL_IPV6) |
akutz | 909cf9a | 2019-12-09 09:17:00 -0600 | [diff] [blame] | 298 | if local_ipv6: |
| 299 | set_guestinfo_value(LOCAL_IPV6, local_ipv6) |
| 300 | LOG.info("advertised local ipv6 address %s in guestinfo", local_ipv6) |
| 301 | |
| 302 | |
akutz | daf81f1 | 2019-12-08 11:20:33 -0600 | [diff] [blame] | 303 | def handle_returned_guestinfo_val(key, val): |
| 304 | ''' |
| 305 | handle_returned_guestinfo_val returns the provided value if it is |
| 306 | not empty or set to GUESTINFO_EMPTY_YAML_VAL, otherwise None is |
| 307 | returned |
| 308 | ''' |
| 309 | val = get_none_if_empty_val(val) |
| 310 | if val: |
| 311 | return val |
| 312 | LOG.debug("No value found for key %s", key) |
| 313 | return None |
| 314 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 315 | def get_guestinfo_value(key): |
| 316 | ''' |
| 317 | Returns a guestinfo value for the specified key. |
| 318 | ''' |
| 319 | LOG.debug("Getting guestinfo value for key %s", key) |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 320 | |
| 321 | data_access_method = get_data_access_method() |
| 322 | |
| 323 | if data_access_method == VMX_GUESTINFO: |
| 324 | env_key = ("vmx.guestinfo." + key).upper().replace(".", "_", -1) |
akutz | daf81f1 | 2019-12-08 11:20:33 -0600 | [diff] [blame] | 325 | return handle_returned_guestinfo_val(key, os.environ.get(env_key, "")) |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 326 | |
yvespp | 8d58dfd | 2019-10-29 20:42:09 +0100 | [diff] [blame] | 327 | if data_access_method == VMWARE_RPCTOOL: |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 328 | try: |
Yves Peter | 444519c | 2020-09-10 17:19:46 +0200 | [diff] [blame] | 329 | (stdout, stderr) = subp_module.subp( |
yvespp | 8d58dfd | 2019-10-29 20:42:09 +0100 | [diff] [blame] | 330 | [VMWARE_RPCTOOL, "info-get guestinfo." + key]) |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 331 | if stderr == NOVAL: |
| 332 | LOG.debug("No value found for key %s", key) |
| 333 | elif not stdout: |
| 334 | LOG.error("Failed to get guestinfo value for key %s", key) |
| 335 | else: |
akutz | daf81f1 | 2019-12-08 11:20:33 -0600 | [diff] [blame] | 336 | return handle_returned_guestinfo_val(key, stdout) |
Yves Peter | 444519c | 2020-09-10 17:19:46 +0200 | [diff] [blame] | 337 | except subp_module.ProcessExecutionError as error: |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 338 | if error.stderr == NOVAL: |
| 339 | LOG.debug("No value found for key %s", key) |
| 340 | else: |
| 341 | util.logexc( |
| 342 | LOG, "Failed to get guestinfo value for key %s: %s", key, error) |
| 343 | except Exception: |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 344 | util.logexc( |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 345 | LOG, "Unexpected error while trying to get guestinfo value for key %s", key) |
| 346 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 347 | return None |
akutz | 6501f90 | 2018-08-24 12:19:05 -0500 | [diff] [blame] | 348 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 349 | |
akutz | dbce3d9 | 2019-12-08 00:09:11 -0600 | [diff] [blame] | 350 | def set_guestinfo_value(key, value): |
| 351 | ''' |
| 352 | Sets a guestinfo value for the specified key. Set value to an empty string |
| 353 | to clear an existing guestinfo key. |
| 354 | ''' |
| 355 | |
| 356 | # If value is an empty string then set it to a single space as it is not |
| 357 | # possible to set a guestinfo key to an empty string. Setting a guestinfo |
| 358 | # key to a single space is as close as it gets to clearing an existing |
| 359 | # guestinfo key. |
| 360 | if value == "": |
| 361 | value = " " |
| 362 | |
| 363 | LOG.debug("Setting guestinfo key=%s to value=%s", key, value) |
| 364 | |
| 365 | data_access_method = get_data_access_method() |
| 366 | |
| 367 | if data_access_method == VMX_GUESTINFO: |
| 368 | return True |
| 369 | |
| 370 | if data_access_method == VMWARE_RPCTOOL: |
| 371 | try: |
Yves Peter | 444519c | 2020-09-10 17:19:46 +0200 | [diff] [blame] | 372 | subp_module.subp( |
akutz | dbce3d9 | 2019-12-08 00:09:11 -0600 | [diff] [blame] | 373 | [VMWARE_RPCTOOL, ("info-set guestinfo.%s %s" % (key, value))]) |
| 374 | return True |
Yves Peter | 444519c | 2020-09-10 17:19:46 +0200 | [diff] [blame] | 375 | except subp_module.ProcessExecutionError as error: |
akutz | dbce3d9 | 2019-12-08 00:09:11 -0600 | [diff] [blame] | 376 | util.logexc( |
| 377 | LOG, "Failed to set guestinfo key=%s to value=%s: %s", key, value, error) |
| 378 | except Exception: |
| 379 | util.logexc( |
| 380 | LOG, "Unexpected error while trying to set guestinfo key=%s to value=%s", key, value) |
| 381 | |
| 382 | return None |
| 383 | |
| 384 | |
| 385 | def clear_guestinfo_keys(keys): |
| 386 | ''' |
akutz | daf81f1 | 2019-12-08 11:20:33 -0600 | [diff] [blame] | 387 | clear_guestinfo_keys clears guestinfo of all of the keys in the given list. |
| 388 | each key will have its value set to "---". Since the value is valid YAML, |
| 389 | cloud-init can still read it if it tries. |
akutz | dbce3d9 | 2019-12-08 00:09:11 -0600 | [diff] [blame] | 390 | ''' |
| 391 | if not keys: |
| 392 | return |
akutz | daf81f1 | 2019-12-08 11:20:33 -0600 | [diff] [blame] | 393 | if not type(keys) in (list, tuple): |
| 394 | keys = [keys] |
akutz | dbce3d9 | 2019-12-08 00:09:11 -0600 | [diff] [blame] | 395 | for key in keys: |
akutz | daf81f1 | 2019-12-08 11:20:33 -0600 | [diff] [blame] | 396 | LOG.info("clearing guestinfo.%s", key) |
| 397 | if not set_guestinfo_value(key, GUESTINFO_EMPTY_YAML_VAL): |
| 398 | LOG.error("failed to clear guestinfo.%s", key) |
| 399 | LOG.info("clearing guestinfo.%s.encoding", key) |
| 400 | if not set_guestinfo_value(key + ".encoding", ""): |
| 401 | LOG.error("failed to clear guestinfo.%s.encoding", key) |
akutz | dbce3d9 | 2019-12-08 00:09:11 -0600 | [diff] [blame] | 402 | |
| 403 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 404 | def guestinfo(key): |
| 405 | ''' |
| 406 | guestinfo returns the guestinfo value for the provided key, decoding |
| 407 | the value when required |
| 408 | ''' |
| 409 | data = get_guestinfo_value(key) |
| 410 | if not data: |
akutz | 6501f90 | 2018-08-24 12:19:05 -0500 | [diff] [blame] | 411 | return None |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 412 | enc_type = get_guestinfo_value(key + '.encoding') |
| 413 | return decode('guestinfo.' + key, enc_type, data) |
| 414 | |
| 415 | |
| 416 | def load(data): |
| 417 | ''' |
| 418 | load first attempts to unmarshal the provided data as JSON, and if |
| 419 | that fails then attempts to unmarshal the data as YAML. If data is |
| 420 | None then a new dictionary is returned. |
| 421 | ''' |
| 422 | if not data: |
| 423 | return {} |
| 424 | try: |
| 425 | return json.loads(data) |
| 426 | except: |
| 427 | return safeyaml.load(data) |
| 428 | |
| 429 | |
| 430 | def load_metadata(): |
| 431 | ''' |
| 432 | load_metadata loads the metadata from the guestinfo data, optionally |
| 433 | decoding the network config when required |
| 434 | ''' |
| 435 | data = load(guestinfo('metadata')) |
akutz | 84389c8 | 2019-06-02 19:24:38 -0500 | [diff] [blame] | 436 | LOG.debug('loaded metadata %s', data) |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 437 | |
| 438 | network = None |
| 439 | if 'network' in data: |
| 440 | network = data['network'] |
| 441 | del data['network'] |
| 442 | |
| 443 | network_enc = None |
| 444 | if 'network.encoding' in data: |
| 445 | network_enc = data['network.encoding'] |
| 446 | del data['network.encoding'] |
| 447 | |
| 448 | if network: |
akutz | 84389c8 | 2019-06-02 19:24:38 -0500 | [diff] [blame] | 449 | LOG.debug('network data found') |
| 450 | if isinstance(network, collections.Mapping): |
| 451 | LOG.debug("network data copied to 'config' key") |
| 452 | network = { |
| 453 | 'config': copy.deepcopy(network) |
| 454 | } |
| 455 | else: |
| 456 | LOG.debug("network data to be decoded %s", network) |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 457 | dec_net = decode('metadata.network', network_enc, network) |
akutz | 84389c8 | 2019-06-02 19:24:38 -0500 | [diff] [blame] | 458 | network = { |
| 459 | 'config': load(dec_net), |
| 460 | } |
| 461 | |
| 462 | LOG.debug('network data %s', network) |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 463 | data['network'] = network |
| 464 | |
| 465 | return data |
| 466 | |
akutz | 6501f90 | 2018-08-24 12:19:05 -0500 | [diff] [blame] | 467 | |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 468 | def get_datasource_list(depends): |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 469 | ''' |
akutz | 77457a6 | 2018-08-22 16:07:21 -0500 | [diff] [blame] | 470 | Return a list of data sources that match this set of dependencies |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 471 | ''' |
Andrew Kutz | 4f66b8b | 2018-09-16 18:28:59 -0500 | [diff] [blame] | 472 | return [DataSourceVMwareGuestInfo] |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 473 | |
| 474 | |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 475 | def get_default_ip_addrs(): |
| 476 | ''' |
| 477 | Returns the default IPv4 and IPv6 addresses based on the device(s) used for |
| 478 | the default route. Please note that None may be returned for either address |
| 479 | family if that family has no default route or if there are multiple |
| 480 | addresses associated with the device used by the default route for a given |
| 481 | address. |
| 482 | ''' |
| 483 | gateways = netifaces.gateways() |
| 484 | if 'default' not in gateways: |
| 485 | return None, None |
| 486 | |
| 487 | default_gw = gateways['default'] |
| 488 | if netifaces.AF_INET not in default_gw and netifaces.AF_INET6 not in default_gw: |
| 489 | return None, None |
| 490 | |
| 491 | ipv4 = None |
| 492 | ipv6 = None |
| 493 | |
| 494 | gw4 = default_gw.get(netifaces.AF_INET) |
| 495 | if gw4: |
| 496 | _, dev4 = gw4 |
akutz | 0b519f7 | 2019-06-02 14:58:57 -0500 | [diff] [blame] | 497 | addr4_fams = netifaces.ifaddresses(dev4) |
| 498 | if addr4_fams: |
| 499 | af_inet4 = addr4_fams.get(netifaces.AF_INET) |
| 500 | if af_inet4: |
| 501 | if len(af_inet4) > 1: |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 502 | LOG.warn( |
akutz | 0b519f7 | 2019-06-02 14:58:57 -0500 | [diff] [blame] | 503 | "device %s has more than one ipv4 address: %s", dev4, af_inet4) |
| 504 | elif 'addr' in af_inet4[0]: |
| 505 | ipv4 = af_inet4[0]['addr'] |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 506 | |
| 507 | # 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] | 508 | # IPv6 route. |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 509 | gw6 = default_gw.get(netifaces.AF_INET6) |
| 510 | if gw6: |
| 511 | _, dev6 = gw6 |
| 512 | addr6_fams = netifaces.ifaddresses(dev6) |
| 513 | if addr6_fams: |
| 514 | af_inet6 = addr6_fams.get(netifaces.AF_INET6) |
| 515 | if af_inet6: |
| 516 | if len(af_inet6) > 1: |
| 517 | LOG.warn( |
| 518 | "device %s has more than one ipv6 address: %s", dev6, af_inet6) |
| 519 | elif 'addr' in af_inet6[0]: |
| 520 | ipv6 = af_inet6[0]['addr'] |
akutz | 0b519f7 | 2019-06-02 14:58:57 -0500 | [diff] [blame] | 521 | |
| 522 | # If there is a default IPv4 address but not IPv6, then see if there is a |
| 523 | # single IPv6 address associated with the same device associated with the |
| 524 | # default IPv4 address. |
| 525 | if ipv4 and not ipv6: |
| 526 | af_inet6 = addr4_fams.get(netifaces.AF_INET6) |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 527 | if af_inet6: |
| 528 | if len(af_inet6) > 1: |
| 529 | LOG.warn( |
| 530 | "device %s has more than one ipv6 address: %s", dev4, af_inet6) |
| 531 | elif 'addr' in af_inet6[0]: |
| 532 | ipv6 = af_inet6[0]['addr'] |
| 533 | |
akutz | 0b519f7 | 2019-06-02 14:58:57 -0500 | [diff] [blame] | 534 | # If there is a default IPv6 address but not IPv4, then see if there is a |
| 535 | # single IPv4 address associated with the same device associated with the |
| 536 | # default IPv6 address. |
| 537 | if not ipv4 and ipv6: |
Hieu | 47f5c7f | 2020-05-20 10:11:08 +0700 | [diff] [blame] | 538 | af_inet4 = addr6_fams.get(netifaces.AF_INET) |
akutz | 0b519f7 | 2019-06-02 14:58:57 -0500 | [diff] [blame] | 539 | if af_inet4: |
| 540 | if len(af_inet4) > 1: |
| 541 | LOG.warn( |
| 542 | "device %s has more than one ipv4 address: %s", dev6, af_inet4) |
| 543 | elif 'addr' in af_inet4[0]: |
| 544 | ipv4 = af_inet4[0]['addr'] |
| 545 | |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 546 | return ipv4, ipv6 |
| 547 | |
Keerthana K | f0d27ea | 2019-09-05 21:46:31 +0530 | [diff] [blame] | 548 | # patched socket.getfqdn() - see https://bugs.python.org/issue5004 |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 549 | |
| 550 | |
Keerthana K | f0d27ea | 2019-09-05 21:46:31 +0530 | [diff] [blame] | 551 | def getfqdn(name=''): |
| 552 | """Get fully qualified domain name from name. |
| 553 | An empty argument is interpreted as meaning the local host. |
| 554 | """ |
| 555 | name = name.strip() |
| 556 | if not name or name == '0.0.0.0': |
| 557 | name = socket.gethostname() |
| 558 | try: |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 559 | addrs = socket.getaddrinfo( |
| 560 | name, None, 0, socket.SOCK_DGRAM, 0, socket.AI_CANONNAME) |
Keerthana K | f0d27ea | 2019-09-05 21:46:31 +0530 | [diff] [blame] | 561 | except socket.error: |
| 562 | pass |
| 563 | else: |
| 564 | for addr in addrs: |
| 565 | if addr[3]: |
| 566 | name = addr[3] |
| 567 | break |
| 568 | return name |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 569 | |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 570 | |
akutz | 1cce7fa | 2020-03-24 11:01:45 -0500 | [diff] [blame] | 571 | def is_valid_ip_addr(val): |
| 572 | """ |
| 573 | Returns false if the address is loopback, link local or unspecified; |
| 574 | otherwise true is returned. |
| 575 | """ |
| 576 | addr = None |
| 577 | try: |
Promaethius | 8626bab | 2020-09-22 11:04:48 -0600 | [diff] [blame^] | 578 | try: |
| 579 | addr = ipaddress.ip_address(val) |
| 580 | except ipaddress.AddressValueError: |
| 581 | addr = ipaddress.ip_address(val.encode('utf-8')) |
akutz | 1cce7fa | 2020-03-24 11:01:45 -0500 | [diff] [blame] | 582 | except: |
| 583 | return False |
| 584 | if addr.is_link_local or addr.is_loopback or addr.is_unspecified: |
| 585 | return False |
| 586 | return True |
| 587 | |
| 588 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 589 | def get_host_info(): |
| 590 | ''' |
| 591 | Returns host information such as the host name and network interfaces. |
| 592 | ''' |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 593 | |
| 594 | host_info = { |
| 595 | 'network': { |
| 596 | 'interfaces': { |
| 597 | 'by-mac': collections.OrderedDict(), |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 598 | 'by-ipv4': collections.OrderedDict(), |
| 599 | 'by-ipv6': collections.OrderedDict(), |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 600 | }, |
| 601 | }, |
| 602 | } |
| 603 | |
Keerthana K | f0d27ea | 2019-09-05 21:46:31 +0530 | [diff] [blame] | 604 | hostname = getfqdn(socket.gethostname()) |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 605 | if hostname: |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 606 | host_info['hostname'] = hostname |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 607 | host_info['local-hostname'] = hostname |
akutz | b7a193d | 2019-12-09 11:36:32 -0600 | [diff] [blame] | 608 | host_info['local_hostname'] = hostname |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 609 | |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 610 | default_ipv4, default_ipv6 = get_default_ip_addrs() |
| 611 | if default_ipv4: |
akutz | 909cf9a | 2019-12-09 09:17:00 -0600 | [diff] [blame] | 612 | host_info[LOCAL_IPV4] = default_ipv4 |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 613 | if default_ipv6: |
akutz | 909cf9a | 2019-12-09 09:17:00 -0600 | [diff] [blame] | 614 | host_info[LOCAL_IPV6] = default_ipv6 |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 615 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 616 | by_mac = host_info['network']['interfaces']['by-mac'] |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 617 | by_ipv4 = host_info['network']['interfaces']['by-ipv4'] |
| 618 | by_ipv6 = host_info['network']['interfaces']['by-ipv6'] |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 619 | |
| 620 | ifaces = netifaces.interfaces() |
| 621 | for dev_name in ifaces: |
| 622 | addr_fams = netifaces.ifaddresses(dev_name) |
| 623 | af_link = addr_fams.get(netifaces.AF_LINK) |
akutz | 0b519f7 | 2019-06-02 14:58:57 -0500 | [diff] [blame] | 624 | af_inet4 = addr_fams.get(netifaces.AF_INET) |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 625 | af_inet6 = addr_fams.get(netifaces.AF_INET6) |
| 626 | |
| 627 | mac = None |
| 628 | if af_link and 'addr' in af_link[0]: |
| 629 | mac = af_link[0]['addr'] |
| 630 | |
| 631 | # Do not bother recording localhost |
| 632 | if mac == "00:00:00:00:00:00": |
| 633 | continue |
| 634 | |
akutz | 0b519f7 | 2019-06-02 14:58:57 -0500 | [diff] [blame] | 635 | if mac and (af_inet4 or af_inet6): |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 636 | key = mac |
| 637 | val = {} |
akutz | 0b519f7 | 2019-06-02 14:58:57 -0500 | [diff] [blame] | 638 | if af_inet4: |
akutz | 1cce7fa | 2020-03-24 11:01:45 -0500 | [diff] [blame] | 639 | af_inet4_vals = [] |
| 640 | for ip_info in af_inet4: |
| 641 | if not is_valid_ip_addr(ip_info['addr']): |
| 642 | continue |
| 643 | af_inet4_vals.append(ip_info) |
| 644 | val["ipv4"] = af_inet4_vals |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 645 | if af_inet6: |
akutz | 1cce7fa | 2020-03-24 11:01:45 -0500 | [diff] [blame] | 646 | af_inet6_vals = [] |
| 647 | for ip_info in af_inet6: |
| 648 | if not is_valid_ip_addr(ip_info['addr']): |
| 649 | continue |
| 650 | af_inet6_vals.append(ip_info) |
| 651 | val["ipv6"] = af_inet6_vals |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 652 | by_mac[key] = val |
| 653 | |
akutz | 0b519f7 | 2019-06-02 14:58:57 -0500 | [diff] [blame] | 654 | if af_inet4: |
| 655 | for ip_info in af_inet4: |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 656 | key = ip_info['addr'] |
akutz | 1cce7fa | 2020-03-24 11:01:45 -0500 | [diff] [blame] | 657 | if not is_valid_ip_addr(key): |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 658 | continue |
akutz | 84389c8 | 2019-06-02 19:24:38 -0500 | [diff] [blame] | 659 | val = copy.deepcopy(ip_info) |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 660 | del val['addr'] |
| 661 | if mac: |
| 662 | val['mac'] = mac |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 663 | by_ipv4[key] = val |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 664 | |
| 665 | if af_inet6: |
| 666 | for ip_info in af_inet6: |
| 667 | key = ip_info['addr'] |
akutz | 1cce7fa | 2020-03-24 11:01:45 -0500 | [diff] [blame] | 668 | if not is_valid_ip_addr(key): |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 669 | continue |
akutz | 84389c8 | 2019-06-02 19:24:38 -0500 | [diff] [blame] | 670 | val = copy.deepcopy(ip_info) |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 671 | del val['addr'] |
| 672 | if mac: |
| 673 | val['mac'] = mac |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 674 | by_ipv6[key] = val |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 675 | |
| 676 | return host_info |
| 677 | |
| 678 | |
akutz | 33dbfc2 | 2020-03-23 13:43:07 -0500 | [diff] [blame] | 679 | def wait_on_network(metadata): |
| 680 | # Determine whether we need to wait on the network coming online. |
| 681 | wait_on_ipv4 = False |
| 682 | wait_on_ipv6 = False |
| 683 | if WAIT_ON_NETWORK in metadata: |
| 684 | wait_on_network = metadata[WAIT_ON_NETWORK] |
| 685 | if WAIT_ON_NETWORK_IPV4 in wait_on_network: |
| 686 | wait_on_ipv4_val = wait_on_network[WAIT_ON_NETWORK_IPV4] |
| 687 | if isinstance(wait_on_ipv4_val, bool): |
| 688 | wait_on_ipv4 = wait_on_ipv4_val |
| 689 | else: |
| 690 | wait_on_ipv4 = bool(strtobool(wait_on_ipv4_val)) |
| 691 | if WAIT_ON_NETWORK_IPV6 in wait_on_network: |
| 692 | wait_on_ipv6_val = wait_on_network[WAIT_ON_NETWORK_IPV6] |
| 693 | if isinstance(wait_on_ipv6_val, bool): |
| 694 | wait_on_ipv6 = wait_on_ipv6_val |
| 695 | else: |
| 696 | wait_on_ipv6 = bool(strtobool(wait_on_ipv6_val)) |
| 697 | |
| 698 | # Get information about the host. |
| 699 | host_info = None |
| 700 | while host_info == None: |
| 701 | host_info = get_host_info() |
akutz | e16f5e8 | 2020-03-24 11:11:07 -0500 | [diff] [blame] | 702 | if wait_on_ipv4: |
| 703 | ipv4_ready = False |
| 704 | if 'network' in host_info: |
| 705 | if 'interfaces' in host_info['network']: |
| 706 | if 'by-ipv4' in host_info['network']['interfaces']: |
| 707 | if len(host_info['network']['interfaces']['by-ipv4']) > 0: |
| 708 | ipv4_ready = True |
| 709 | if not ipv4_ready: |
| 710 | LOG.info("ipv4 not ready") |
| 711 | host_info = None |
| 712 | if wait_on_ipv6: |
| 713 | ipv6_ready = False |
| 714 | if 'network' in host_info: |
| 715 | if 'interfaces' in host_info['network']: |
| 716 | if 'by-ipv6' in host_info['network']['interfaces']: |
| 717 | if len(host_info['network']['interfaces']['by-ipv6']) > 0: |
| 718 | ipv6_ready = True |
| 719 | if not ipv6_ready: |
| 720 | LOG.info("ipv6 not ready") |
| 721 | host_info = None |
akutz | 33dbfc2 | 2020-03-23 13:43:07 -0500 | [diff] [blame] | 722 | if host_info == None: |
| 723 | LOG.info("waiting on network") |
| 724 | time.sleep(1) |
| 725 | |
| 726 | return host_info |
| 727 | |
| 728 | |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 729 | def get_data_access_method(): |
| 730 | if os.environ.get(VMX_GUESTINFO, ""): |
| 731 | return VMX_GUESTINFO |
yvespp | 8d58dfd | 2019-10-29 20:42:09 +0100 | [diff] [blame] | 732 | if VMWARE_RPCTOOL: |
| 733 | return VMWARE_RPCTOOL |
akutz | 10cd140 | 2019-10-23 18:06:39 -0500 | [diff] [blame] | 734 | return None |
| 735 | |
| 736 | |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 737 | def main(): |
| 738 | ''' |
| 739 | Executed when this file is used as a program. |
| 740 | ''' |
akutz | 33dbfc2 | 2020-03-23 13:43:07 -0500 | [diff] [blame] | 741 | metadata = {'wait-on-network': {'ipv4': True, 'ipv6': "false"}, |
| 742 | 'network': {'config': {'dhcp': True}}} |
| 743 | host_info = wait_on_network(metadata) |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 744 | metadata = always_merger.merge(metadata, host_info) |
| 745 | print(util.json_dumps(metadata)) |
| 746 | |
| 747 | |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 748 | if __name__ == "__main__": |
akutz | ffc4dd5 | 2019-06-02 11:34:55 -0500 | [diff] [blame] | 749 | main() |
akutz | 0d1fce5 | 2019-06-01 18:54:29 -0500 | [diff] [blame] | 750 | |
| 751 | # vi: ts=4 expandtab |