-
Notifications
You must be signed in to change notification settings - Fork 882
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
Closed
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a865bb9
Add Aeza.net datasource
cofob cabcedd
Add missing parameters to docs
cofob 58f5f89
Update link to Aeza docs
cofob 9124231
Fix the code according to the suggested edits
cofob d27c0dd
Apply changes in accordance with review
cofob 7bd7d57
Return False in get_ds if md/ud invalid
cofob 2deca90
Update doc/rtd/reference/datasources/aeza.rst
cofob 7eb11ef
Apply patch from blackboxsw
cofob b2b7175
Fix test_common
cofob 9f81320
Ignore network-config
cofob c622375
Fix tests
cofob 61f1301
Fix read_seeded ignore_files error
cofob File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,7 @@ | |
"ZStack", | ||
"Outscale", | ||
"WSL", | ||
"Aeza.net", | ||
"Other", | ||
] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||
|
||||||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.