Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Aeza.net hosting provider #5299

Closed
wants to merge 12 commits into from
1 change: 1 addition & 0 deletions cloudinit/apport.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"ZStack",
"Outscale",
"WSL",
"Aeza.net",
"Other",
]

Expand Down
1 change: 1 addition & 0 deletions cloudinit/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"NWCS",
"Akamai",
"WSL",
"Aeza",
# At the end to act as a 'catch' when none of the above work...
"None",
],
Expand Down
69 changes: 69 additions & 0 deletions cloudinit/sources/DataSourceAeza.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Copyright (C) 2024 Aeza.net.
#
# Author: Egor Ternovoy <[email protected]>
#
# This file is part of cloud-init. See LICENSE file for license information.

import logging

from cloudinit import dmi, sources, util

LOG = logging.getLogger(__name__)

BUILTIN_DS_CONFIG = {
"metadata_url": "http://77.221.156.49/v1/cloudinit/{id}/",
}


class DataSourceAeza(sources.DataSource):

dsname = "Aeza"

def __init__(self, sys_cfg, distro, paths, ud_proc=None):
super().__init__(sys_cfg, distro, paths, ud_proc)

self.ds_cfg = util.mergemanydict([self.ds_cfg, BUILTIN_DS_CONFIG])

@staticmethod
def ds_detect():
return dmi.read_dmi_data("system-manufacturer") == "Aeza"

def _get_data(self):
system_uuid = dmi.read_dmi_data("system-uuid")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we expect this value to change at all across system reboot? If so, __init__ isn't going to be called across system reboot due to datasource cache in /var/lib/cloud/instance/obj.pkl. We should likely be performing this inside _get_data to ensure it's called each boot.

metadata_address = (
self.ds_cfg["metadata_url"].format(
id=system_uuid,
)
+ "%s"
)
url_params = self.get_url_params()
md, ud, vd, _network_config = util.read_seeded(
metadata_address,
timeout=url_params.timeout_seconds,
retries=url_params.num_retries,
ignore_files=["network-config"],
)

if md is None:
raise sources.InvalidMetaDataException(
f"Failed to read metadata from {metadata_address}",
)
if not isinstance(md.get("instance-id"), str):
raise sources.InvalidMetaDataException(
f"Metadata does not contain instance-id: {md}"
)
if not isinstance(ud, bytes):
raise sources.InvalidMetaDataException("Userdata is not bytes")
Comment on lines +55 to +56
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we really expect bytes here from util.read_seeded for ud. self.userdata_raw certainly expects str. I think we can drop this check here as this is also not needed for DataSourceNoCloudNet when it reaches out to a remote URL for user-data files.

Suggested change
if not isinstance(ud, bytes):
raise sources.InvalidMetaDataException("Userdata is not bytes")


self.metadata, self.userdata_raw, self.vendordata_raw = md, ud, vd

return True


datasources = [
(DataSourceAeza, (sources.DEP_NETWORK, sources.DEP_FILESYSTEM)),
cofob marked this conversation as resolved.
Show resolved Hide resolved
]


def get_datasource_list(depends):
return sources.list_from_depends(depends, datasources)
69 changes: 39 additions & 30 deletions cloudinit/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,10 @@ def load_yaml(blob, default=None, allowed=(dict,)):
return loaded


def read_seeded(base="", ext="", timeout=5, retries=10):
def read_seeded(base="", ext="", timeout=5, retries=10, ignore_files=None):
if ignore_files is None:
ignore_files = []

if base.find("%s") >= 0:
ud_url = base.replace("%s", "user-data" + ext)
vd_url = base.replace("%s", "vendor-data" + ext)
Expand All @@ -1069,42 +1072,48 @@ def read_seeded(base="", ext="", timeout=5, retries=10):
vd_url = "%s%s%s" % (base, "vendor-data", ext)
md_url = "%s%s%s" % (base, "meta-data", ext)
network_url = "%s%s%s" % (base, "network-config", ext)

network = None
try:
network_resp = url_helper.read_file_or_url(
network_url, timeout=timeout, retries=retries
)
except url_helper.UrlError as e:
LOG.debug("No network config provided: %s", e)
else:
if network_resp.ok():
network = load_yaml(network_resp.contents)
md_resp = url_helper.read_file_or_url(
md_url, timeout=timeout, retries=retries
)
if "network-config" not in ignore_files:
try:
network_resp = url_helper.read_file_or_url(
network_url, timeout=timeout, retries=retries
)
except url_helper.UrlError as e:
LOG.debug("No network config provided: %s", e)
else:
if network_resp.ok():
network = load_yaml(network_resp.contents)

md = None
if md_resp.ok():
md = load_yaml(md_resp.contents, default={})
if "meta-data" in ignore_files:
cofob marked this conversation as resolved.
Show resolved Hide resolved
md_resp = url_helper.read_file_or_url(
md_url, timeout=timeout, retries=retries
)
if md_resp.ok():
md = load_yaml(md_resp.contents, default={})

ud_resp = url_helper.read_file_or_url(
ud_url, timeout=timeout, retries=retries
)
ud = None
if ud_resp.ok():
ud = ud_resp.contents
if "user-data" in ignore_files:
cofob marked this conversation as resolved.
Show resolved Hide resolved
ud_resp = url_helper.read_file_or_url(
ud_url, timeout=timeout, retries=retries
)
if ud_resp.ok():
ud = ud_resp.contents

vd = None
try:
vd_resp = url_helper.read_file_or_url(
vd_url, timeout=timeout, retries=retries
)
except url_helper.UrlError as e:
LOG.debug("Error in vendor-data response: %s", e)
else:
if vd_resp.ok():
vd = vd_resp.contents
if "vendor-data" in ignore_files:
cofob marked this conversation as resolved.
Show resolved Hide resolved
try:
vd_resp = url_helper.read_file_or_url(
vd_url, timeout=timeout, retries=retries
)
except url_helper.UrlError as e:
LOG.debug("Error in vendor-data response: %s", e)
else:
LOG.debug("Error in vendor-data response")
if vd_resp.ok():
vd = vd_resp.contents
else:
LOG.debug("Error in vendor-data response")

return md, ud, vd, network

Expand Down
1 change: 1 addition & 0 deletions doc/rtd/reference/availability.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ environments in the public cloud:
- Zadara Edge Cloud Platform
- 3DS Outscale
- Akamai
- Aeza.net

Additionally, ``cloud-init`` is supported on these private clouds:

Expand Down
1 change: 1 addition & 0 deletions doc/rtd/reference/datasource_dsname_map.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ mapping between datasource module names and ``dsname`` in the table below.
DataSourceVMware.py, VMware
DataSourceVultr.py, Vultr
DataSourceWSL.py, WSL
DataSourceAeza.py, Aeza
1 change: 1 addition & 0 deletions doc/rtd/reference/datasources.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ The following is a list of documentation for each supported datasource:
datasources/vultr.rst
datasources/wsl.rst
datasources/zstack.rst
datasources/aeza.rst
42 changes: 42 additions & 0 deletions doc/rtd/reference/datasources/aeza.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
.. _datasource_aeza:

Aeza
****

The `Aeza`_ datasource consumes the content served from Aeza's metadata
service. This metadata service serves information about the running VPS
via http at ``77.221.156.49``.

Configuration
=============

Aeza's datasource can be configured as follows: ::

datasource:
Aeza:
metadata_url: "http://77.221.156.49/v1/cloudinit/{id}/"

* ``metadata_url``

Specifies the URL to retrieve the VPS meta-data. (optional)
cofob marked this conversation as resolved.
Show resolved Hide resolved
Default: ``http://77.221.156.49/v1/cloudinit/{id}/``

* ``timeout``

The timeout value provided to ``urlopen`` for each individual http request.
This is used both when selecting a ``metadata_url`` and when crawling the
metadata service.

Default: 10

* ``retries``

The number of retries that should be attempted for an http request. This
value is used only after ``metadata_url`` is selected.

Default: 5

.. note::
``{id}`` in URLs is system-uuid DMI value.

.. _Aeza: https://wiki.aeza.net/cloud-init
147 changes: 147 additions & 0 deletions tests/unittests/sources/test_aeza.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# Copyright (C) 2024 Aeza.net.
#
# Author: Egor Ternovoy <[email protected]>
#
# This file is part of cloud-init. See LICENSE file for license information.
import re

import pytest

from cloudinit import helpers, settings, util
from cloudinit.sources import DataSourceAeza as aeza
from cloudinit.sources import InvalidMetaDataException
from tests.unittests.helpers import mock

METADATA = util.load_yaml(
"""---
hostname: cloudinit-test.aeza.network
instance-id: ic0859a7003d840d093756680cb45d51f
public-keys:
- ssh-ed25519 AAAA...4nkhmWh example-key
"""
)

VENDORDATA = None

USERDATA = b"""#cloud-config
runcmd:
- [touch, /root/cloud-init-worked]
"""


M_PATH = "cloudinit.sources.DataSourceAeza."


class TestDataSourceAeza:
"""Test Aeza.net reading instance-data"""

@pytest.mark.parametrize(
"system_manufacturer,expected",
(
pytest.param("Aeza", True, id="dmi_platform_match_aeza"),
pytest.param("aeza", False, id="dmi_platform_match_case_sensitve"),
pytest.param("Aezanope", False, id="dmi_platform_strict_match"),
),
)
@mock.patch(f"{M_PATH}dmi.read_dmi_data")
def test_ds_detect(self, m_read_dmi_data, system_manufacturer, expected):
"""Only strict case-senstiive match on DMI system-manfacturer Aeza"""
m_read_dmi_data.return_value = system_manufacturer
assert expected is aeza.DataSourceAeza.ds_detect()

@pytest.mark.parametrize(
"sys_cfg,expected_calls",
(
pytest.param(
{},
[
mock.call(
"http://77.221.156.49/v1/cloudinit/1dd9a779-uuid/%s",
timeout=10,
retries=5,
ignore_files=["network-config"],
)
],
id="default_sysconfig_ds_url_retry_and_timeout",
),
pytest.param(
{
"datasource": {
"Aeza": {
"timeout": 7,
"retries": 8,
"metadata_url": "https://somethingelse/",
}
}
},
[
mock.call(
"https://somethingelse/%s",
timeout=7,
retries=8,
ignore_files=["network-config"],
)
],
id="custom_sysconfig_ds_url_retry_and_timeout_overrides",
),
),
)
@mock.patch("cloudinit.util.read_seeded")
@mock.patch(f"{M_PATH}dmi.read_dmi_data")
def test_read_data(
self,
m_read_dmi_data,
m_read_seeded,
sys_cfg,
expected_calls,
paths,
tmpdir,
):
m_read_dmi_data.return_value = "1dd9a779-uuid"
m_read_seeded.return_value = (METADATA, USERDATA, VENDORDATA, None)

ds = aeza.DataSourceAeza(
sys_cfg=sys_cfg, distro=mock.Mock(), paths=paths
)
with mock.patch.object(ds, "ds_detect", return_value=True):
assert True is ds.get_data()

assert m_read_seeded.call_args_list == expected_calls
assert ds.get_public_ssh_keys() == METADATA.get("public-keys")
assert isinstance(ds.get_public_ssh_keys(), list)
assert ds.userdata_raw == USERDATA
assert ds.vendordata_raw == VENDORDATA

@pytest.mark.parametrize(
"metadata,userdata,error_msg",
(
({}, USERDATA, "Metadata does not contain instance-id: {}"),
(
None,
USERDATA,
"Failed to read metadata from http://77.221.156.49/v1/cloudinit/1dd9a779-uuid/%s",
),
({"instance-id": "yep"}, "non-bytes", "Userdata is not bytes"),
),
)
@mock.patch("cloudinit.util.read_seeded")
@mock.patch(f"{M_PATH}dmi.read_dmi_data")
def test_not_detected_on_invalid_instance_data(
self,
m_read_dmi_data,
m_read_seeded,
metadata,
userdata,
error_msg,
paths,
):
"""Assert get_data returns False for unexpected format conditions."""
m_read_dmi_data.return_value = "1dd9a779-uuid"
m_read_seeded.return_value = (metadata, userdata, VENDORDATA, None)

ds = aeza.DataSourceAeza(sys_cfg={}, distro=mock.Mock(), paths=paths)
with pytest.raises(
InvalidMetaDataException, match=re.escape(error_msg)
):
with mock.patch.object(ds, "ds_detect", return_value=True):
ds.get_data()
Loading
Loading