Merge pull request #1882 from Chris-Peterson444/link2doc-ai-user-data-FR-5802

This commit is contained in:
Chris Peterson 2024-01-05 07:35:54 -08:00 committed by GitHub
commit e3a0f6d215
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 15 deletions

View File

@ -28,6 +28,7 @@ switch.
""" """
import argparse import argparse
import io
import json import json
import jsonschema import jsonschema
@ -53,13 +54,28 @@ def main() -> None:
args = vars(parser.parse_args()) args = vars(parser.parse_args())
if args["expect-cloudconfig"]: user_data: io.TextIOWrapper = args["input"]
assert args["input"].readline() == "#cloud-config\n"
get_autoinstall_data = lambda data: data["autoinstall"]
else:
get_autoinstall_data = lambda data: data
data = yaml.safe_load(args["input"]) if args["expect-cloudconfig"]:
assert user_data.readline() == "#cloud-config\n"
def get_autoinstall_data(data): return data["autoinstall"]
else:
def get_autoinstall_data(data): return data
# Verify autoinstall doc link is in the file
stream_pos: int = user_data.tell()
data: str = user_data.read()
link: str = "https://canonical-subiquity.readthedocs-hosted.com/en/latest/reference/autoinstall-reference.html" # noqa: E501
assert link in data
# Verify autoinstall schema
user_data.seek(stream_pos)
data = yaml.safe_load(user_data)
jsonschema.validate(get_autoinstall_data(data), jsonschema.validate(get_autoinstall_data(data),
json.load(args["json_schema"])) json.load(args["json_schema"]))

View File

@ -166,6 +166,20 @@ class InstallController(SubiquityController):
and self.model.source.current.variant != "core" and self.model.source.current.variant != "core"
) )
def write_autoinstall_config(self) -> None:
autoinstall_path = os.path.join(
self.app.root, "var/log/installer/autoinstall-user-data"
)
autoinstall_config = (
"#cloud-config\n"
"# See the auto install documentation at:"
"# https://canonical-subiquity.readthedocs-hosted.com/en/latest/reference/autoinstall-reference.html\n" # noqa: E501
+ yaml.dump({"autoinstall": self.app.make_autoinstall()})
)
# As autoinstall-user-data contains a password hash, we want this file
# to have a very restrictive mode and ownership.
write_file(autoinstall_path, autoinstall_config, mode=0o400, group="root")
@with_context(description="configuring apt", level="INFO", childlevel="DEBUG") @with_context(description="configuring apt", level="INFO", childlevel="DEBUG")
async def configure_apt(self, *, context): async def configure_apt(self, *, context):
mirror = self.app.controllers.Mirror mirror = self.app.controllers.Mirror
@ -683,15 +697,7 @@ class InstallController(SubiquityController):
description="final system configuration", level="INFO", childlevel="DEBUG" description="final system configuration", level="INFO", childlevel="DEBUG"
) )
async def postinstall(self, *, context): async def postinstall(self, *, context):
autoinstall_path = os.path.join( self.write_autoinstall_config()
self.app.root, "var/log/installer/autoinstall-user-data"
)
autoinstall_config = "#cloud-config\n" + yaml.dump(
{"autoinstall": self.app.make_autoinstall()}
)
# As autoinstall-user-data contains a password hash, we want this file
# to have a very restrictive mode and ownership.
write_file(autoinstall_path, autoinstall_config, mode=0o400, group="root")
try: try:
if self.supports_apt(): if self.supports_apt():
packages = await self.get_target_packages(context=context) packages = await self.get_target_packages(context=context)

View File

@ -198,6 +198,7 @@ class TestInstallController(unittest.IsolatedAsyncioTestCase):
def setUp(self): def setUp(self):
self.controller = InstallController(make_app()) self.controller = InstallController(make_app())
self.controller.model.target = tempfile.mkdtemp() self.controller.model.target = tempfile.mkdtemp()
self.controller.app.root = tempfile.mkdtemp()
os.makedirs(os.path.join(self.controller.model.target, "etc/grub.d")) os.makedirs(os.path.join(self.controller.model.target, "etc/grub.d"))
self.addCleanup(shutil.rmtree, self.controller.model.target) self.addCleanup(shutil.rmtree, self.controller.model.target)
@ -384,3 +385,15 @@ class TestInstallController(unittest.IsolatedAsyncioTestCase):
async def test_postinstall_platform_amd64(self, arun, machine): async def test_postinstall_platform_amd64(self, arun, machine):
await self.controller.platform_postinstall() await self.controller.platform_postinstall()
arun.assert_not_called() arun.assert_not_called()
async def test_write_autoinstall_config(self):
self.controller.write_autoinstall_config()
user_data = (
self.controller.app.root + "/var/log/installer/autoinstall-user-data"
)
with open(user_data) as file:
data = file.read()
self.assertIn(
"https://canonical-subiquity.readthedocs-hosted.com/en/latest/reference/autoinstall-reference.html", # noqa: E501
data,
)

View File

@ -25,6 +25,9 @@ class MockedApplication:
answers = {} answers = {}
opts = None opts = None
def make_autoinstall(self):
return {"mock_key": "mock_data"}
def make_app(model=None): def make_app(model=None):
app = MockedApplication() app = MockedApplication()