예제 #1
0
    def get_data(self):
        if not on_hetzner():
            return False
        nic = cloudnet.find_fallback_nic()
        with cloudnet.EphemeralIPv4Network(nic, "169.254.0.1", 16,
                                           "169.254.255.255"):
            md = hc_helper.read_metadata(self.metadata_address,
                                         timeout=self.timeout,
                                         sec_between=self.wait_retry,
                                         retries=self.retries)
            ud = hc_helper.read_userdata(self.userdata_address,
                                         timeout=self.timeout,
                                         sec_between=self.wait_retry,
                                         retries=self.retries)

        # Hetzner cloud does not support binary user-data. So here, do a
        # base64 decode of the data if we can. The end result being that a
        # user can provide base64 encoded (possibly gzipped) data as user-data.
        #
        # The fallout is that in the event of b64 encoded user-data,
        # /var/lib/cloud-init/cloud-config.txt will not be identical to the
        # user-data provided.  It will be decoded.
        self.userdata_raw = hc_helper.maybe_b64decode(ud)
        self.metadata_full = md

        # hostname is name provided by user at launch.  The API enforces it is
        # a valid hostname, but it is not guaranteed to be resolvable in dns or
        # fully qualified.
        self.metadata['instance-id'] = md['instance-id']
        self.metadata['local-hostname'] = md['hostname']
        self.metadata['network-config'] = md.get('network-config', None)
        self.metadata['public-keys'] = md.get('public-keys', None)
        self.vendordata_raw = md.get("vendor_data", None)

        return True
예제 #2
0
    def _get_data(self):
        (on_hetzner, serial) = get_hcloud_data()

        if not on_hetzner:
            return False

        try:
            with EphemeralDHCPv4(
                    iface=net.find_fallback_nic(),
                    connectivity_url_data={
                        "url": BASE_URL_V1 + "/metadata/instance-id",
                    },
            ):
                md = hc_helper.read_metadata(
                    self.metadata_address,
                    timeout=self.timeout,
                    sec_between=self.wait_retry,
                    retries=self.retries,
                )
                ud = hc_helper.read_userdata(
                    self.userdata_address,
                    timeout=self.timeout,
                    sec_between=self.wait_retry,
                    retries=self.retries,
                )
        except (NoDHCPLeaseError) as e:
            LOG.error("Bailing, DHCP Exception: %s", e)
            raise

        # Hetzner cloud does not support binary user-data. So here, do a
        # base64 decode of the data if we can. The end result being that a
        # user can provide base64 encoded (possibly gzipped) data as user-data.
        #
        # The fallout is that in the event of b64 encoded user-data,
        # /var/lib/cloud-init/cloud-config.txt will not be identical to the
        # user-data provided.  It will be decoded.
        self.userdata_raw = hc_helper.maybe_b64decode(ud)
        self.metadata_full = md

        # hostname is name provided by user at launch.  The API enforces it is
        # a valid hostname, but it is not guaranteed to be resolvable in dns or
        # fully qualified.
        self.metadata["instance-id"] = md["instance-id"]
        self.metadata["local-hostname"] = md["hostname"]
        self.metadata["network-config"] = md.get("network-config", None)
        self.metadata["public-keys"] = md.get("public-keys", None)
        self.vendordata_raw = md.get("vendor_data", None)

        # instance-id and serial from SMBIOS should be identical
        if self.get_instance_id() != serial:
            raise RuntimeError(
                "SMBIOS serial does not match instance ID from metadata")

        return True
예제 #3
0
 def test_happy_path(self, in_data, expected):
     assert expected == hc_helper.maybe_b64decode(in_data)
예제 #4
0
 def test_raises_error_on_non_bytes(self, invalid_input):
     """maybe_b64decode should raise error if data is not bytes."""
     with pytest.raises(TypeError):
         hc_helper.maybe_b64decode(invalid_input)