Skip to content

Commit

Permalink
cc_keyboard: add Alpine support
Browse files Browse the repository at this point in the history
  • Loading branch information
dermotbradley committed Jul 22, 2023
1 parent 297d171 commit 0ed9f1f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
14 changes: 12 additions & 2 deletions cloudinit/config/cc_keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
from cloudinit.settings import PER_INSTANCE

# FIXME: setting keyboard layout should be supported by all OSes.
# But currently only implemented for Linux distributions that use systemd.
# But currently only implemented for Linux distributions that use systemd,
# plus Alpine Linux.

DEFAULT_KEYBOARD_MODEL = "pc105"

supported_distros = distros.Distro.expand_osfamily(
["arch", "debian", "redhat", "suse"]
["alpine", "arch", "debian", "redhat", "suse"]
)

meta: MetaSchema = {
Expand All @@ -48,6 +49,15 @@
options: compose:rwin
"""
),
dedent(
"""\
# For Alpine Linux set specific keyboard layout and variant.
# Model and options are ignored.
keyboard:
layout: gb
variant: gb-extd
"""
),
],
"frequency": PER_INSTANCE,
"activate_by_schema_keys": ["keyboard"],
Expand Down
20 changes: 19 additions & 1 deletion cloudinit/distros/alpine.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
#
# This file is part of cloud-init. See LICENSE file for license information.

from cloudinit import distros, helpers, subp, util
from cloudinit import distros, helpers
from cloudinit import log as logging
from cloudinit import subp, util
from cloudinit.distros.parsers.hostname import HostnameConf
from cloudinit.settings import PER_INSTANCE

LOG = logging.getLogger(__name__)

NETWORK_FILE_HEADER = """\
# This file is generated from information provided by the datasource. Changes
# to it will not persist across an instance reboot. To disable cloud-init's
Expand Down Expand Up @@ -103,6 +107,20 @@ def _read_hostname(self, filename, default=None):
def _get_localhost_ip(self):
return "127.0.1.1"

def set_keymap(self, layout, model, variant, options):
if not layout:
LOG.error("Keyboard layout not specified.")
return
if not variant:
LOG.error("Keyboard variant not specified.")
return
if model:
LOG.info("Keyboard model is ignored for Alpine Linux.")
if options:
LOG.info("Keyboard options are ignored for Alpine Linux.")

subp.subp(["setup-keymap", layout, variant])

def set_timezone(self, tz):
distros.set_etc_timezone(tz=tz, tz_file=self._find_tz_file(tz))

Expand Down
44 changes: 44 additions & 0 deletions tests/unittests/config/test_cc_keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@

import pytest

from cloudinit.config import cc_keyboard
from cloudinit.config.schema import (
SchemaValidationError,
get_schema,
validate_cloudconfig_schema,
)
from tests.unittests.helpers import skipUnlessJsonSchema
from tests.unittests.util import get_cloud
from unittest import mock


class TestKeyboard:
Expand Down Expand Up @@ -73,5 +76,46 @@ def test_schema_validation(self, config, error_msg):
with pytest.raises(SchemaValidationError, match=error_msg):
validate_cloudconfig_schema(config, schema, strict=True)

@mock.patch("cloudinit.distros.Distro.uses_systemd")
@mock.patch("cloudinit.distros.subp.subp")
def test_systemd_linux_cmd(self, m_subp, m_uses_systemd, *args):
"""Ubuntu runs localectl"""
cfg = {"keyboard": {"layout": "us", "variant": "us"}}
layout = "us"
model = "pc105"
variant = "us"
options = ""
m_uses_systemd.return_value = True
cloud = get_cloud("ubuntu")
cc_keyboard.handle("cc_keyboard", cfg, cloud, [])
locale_calls = [
mock.call(
[
"localectl",
"set-x11-keymap",
layout,
model,
variant,
options,
]
),
mock.call(
["systemctl", "restart", "console-setup"],
capture=True,
rcs=None,
),
]
m_subp.assert_has_calls(locale_calls)

@mock.patch("cloudinit.distros.subp.subp")
def test_alpine_linux_cmd(self, m_subp, *args):
"""Alpine Linux runs setup-keymap"""
cfg = {"keyboard": {"layout": "us", "variant": "us"}}
layout = "us"
variant = "us"
cloud = get_cloud("alpine")
cc_keyboard.handle("cc_keyboard", cfg, cloud, [])
m_subp.assert_called_once_with(["setup-keymap", layout, variant])


# vi: ts=4 expandtab

0 comments on commit 0ed9f1f

Please sign in to comment.