Wraps the hostname setting into a context manager
To have more deterministic set/reset behavior.
This commit is contained in:
parent
706359ea6f
commit
5c059dd6ab
|
@ -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,14 +51,12 @@ 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(
|
||||||
|
@ -63,16 +75,11 @@ class AdJoinStrategy():
|
||||||
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):
|
||||||
|
|
Loading…
Reference in New Issue