Wraps the hostname setting into a context manager

To have more deterministic set/reset behavior.
This commit is contained in:
Carlos Nihelton 2023-02-24 08:55:52 -03:00
parent 706359ea6f
commit 5c059dd6ab
No known key found for this signature in database
GPG Key ID: 6FE346D245197E9A
1 changed files with 37 additions and 30 deletions

View File

@ -14,9 +14,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import asyncio import asyncio
from contextlib import contextmanager
import logging import logging
from socket import gethostname from socket import gethostname
from subiquitycore.utils import arun_command from subiquitycore.utils import run_command
from subiquity.server.curtin import run_curtin_command from subiquity.server.curtin import run_curtin_command
from subiquity.common.types import ( from subiquity.common.types import (
ADConnectionInfo, ADConnectionInfo,
@ -26,6 +27,19 @@ from subiquity.common.types import (
log = logging.getLogger('subiquity.server.ad_joiner') log = logging.getLogger('subiquity.server.ad_joiner')
@contextmanager
def hostname_context(hostname: str):
""" Temporarily adjusts the host name to [hostname] and restores it
back in the end of the caller scope. """
hostname_current = gethostname()
hostname_process = run_command(['hostname', hostname])
yield hostname_process
# Restoring the live session hostname.
hostname_process = run_command(['hostname', hostname_current])
if hostname_process.returncode:
log.info("Failed to restore live session hostname")
class AdJoinStrategy(): class AdJoinStrategy():
realm = "/usr/sbin/realm" realm = "/usr/sbin/realm"
pam = "/usr/sbin/pam-auth-update" pam = "/usr/sbin/pam-auth-update"
@ -37,42 +51,35 @@ class AdJoinStrategy():
-> AdJoinResult: -> AdJoinResult:
""" This method changes the hostname and perform a real AD join, thus """ This method changes the hostname and perform a real AD join, thus
should only run in a live session. """ should only run in a live session. """
result = AdJoinResult.JOIN_ERROR
hostname_current = gethostname()
# Set hostname for AD to determine FQDN (no FQDN option in realm join, # Set hostname for AD to determine FQDN (no FQDN option in realm join,
# only adcli, which only understands the live system, but not chroot) # only adcli, which only understands the live system, but not chroot)
cp = await arun_command(['hostname', hostname]) with hostname_context(hostname) as host_process:
if cp.returncode: if host_process.returncode:
log.info("Failed to set live session hostname for adcli") log.info("Failed to set live session hostname for adcli")
return result return AdJoinResult.JOIN_ERROR
root_dir = self.app.root root_dir = self.app.root
cp = await run_curtin_command( cp = await run_curtin_command(
self.app, context, "in-target", "-t", root_dir, self.app, context, "in-target", "-t", root_dir,
"--", self.realm, "join", "--install", root_dir, "--user", "--", self.realm, "join", "--install", root_dir, "--user",
info.admin_name, "--computer-name", hostname, "--unattended", info.admin_name, "--computer-name", hostname, "--unattended",
info.domain_name, private_mounts=True, input=info.password, info.domain_name, private_mounts=True, input=info.password,
timeout=60) timeout=60)
if not cp.returncode: if not cp.returncode:
# Enable pam_mkhomedir # Enable pam_mkhomedir
cp = await run_curtin_command(self.app, context, "in-target", cp = await run_curtin_command(self.app, context, "in-target",
"-t", root_dir, "--", "-t", root_dir, "--",
self.pam, "--package", self.pam, "--package",
"--enable", "mkhomedir", "--enable", "mkhomedir",
private_mounts=True) private_mounts=True)
if cp.returncode: if cp.returncode:
result = AdJoinResult.PAM_ERROR return AdJoinResult.PAM_ERROR
else: else:
result = AdJoinResult.OK return AdJoinResult.OK
# Restoring the live session hostname. return AdJoinResult.JOIN_ERROR
cp = await arun_command(['hostname', hostname_current])
if cp.returncode:
log.info("Failed to restore live session hostname")
return result
class StubStrategy(AdJoinStrategy): class StubStrategy(AdJoinStrategy):