config: Use privileged decorator for set-hostname action

Tests:

- Running flake8 as in .gitlab-ci.yml works.
- Changes the hostname works and it is updated in /etc/hostname
  - Avahi daemon is restarted

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2022-09-01 14:22:05 -07:00 committed by James Valleroy
parent 06b5639986
commit 40bf6add75
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
4 changed files with 13 additions and 11 deletions

View File

@ -16,7 +16,7 @@ code-quality:
stage: test
needs: []
script:
- python3 -m flake8 --exclude actions/domainname-change,actions/hostname-change,actions/networks container plinth actions/*
- python3 -m flake8 --exclude actions/domainname-change,actions/networks container plinth actions/*
unit-tests:
stage: test

View File

@ -1,8 +0,0 @@
#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-or-later
hostname="$1"
hostnamectl set-hostname --transient --static "$hostname"
service avahi-daemon restart

View File

@ -3,6 +3,7 @@
import os
import pathlib
import subprocess
import augeas
@ -19,6 +20,15 @@ FREEDOMBOX_APACHE_CONFIG = os.path.join(APACHE_CONF_ENABLED_DIR,
JOURNALD_FILE = pathlib.Path('/etc/systemd/journald.conf.d/50-freedombox.conf')
@privileged
def set_hostname(hostname: str):
"""Set system hostname using hostnamectl."""
subprocess.run(
['hostnamectl', 'set-hostname', '--transient', '--static', hostname],
check=True)
action_utils.service_restart('avahi-daemon')
def load_augeas():
"""Initialize Augeas."""
aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +

View File

@ -102,7 +102,7 @@ class ConfigAppView(views.AppView):
def set_hostname(hostname):
"""Sets machine hostname to hostname"""
"""Set machine hostname and send signals before and after."""
old_hostname = config.get_hostname()
domainname = config.get_domainname()
@ -114,7 +114,7 @@ def set_hostname(hostname):
new_hostname=hostname)
LOGGER.info('Changing hostname to - %s', hostname)
actions.superuser_run('hostname-change', [hostname])
privileged.set_hostname(hostname)
LOGGER.info('Setting domain name after hostname change - %s', domainname)
actions.superuser_run('domainname-change', [domainname])