From b60717443465fb94649acb96ebdbabe06e4a1446 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Thu, 1 Sep 2022 22:07:43 -0700 Subject: [PATCH] config: Use privileged decorator for set domainname action Tests: - Running flake8 as in .gitlab-ci.yml works. - Setting the domain name again to update /etc/hosts file after hostname change works - Setting the domain name from the text box works. New domain name is read back and shown properly. Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- .gitlab-ci.yml | 2 +- actions/domainname-change | 21 --------------------- plinth/modules/config/privileged.py | 27 +++++++++++++++++++++++++++ plinth/modules/config/views.py | 17 ++++++++--------- 4 files changed, 36 insertions(+), 31 deletions(-) delete mode 100755 actions/domainname-change diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index e54e7fc1b..ed2d2aa2b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -16,7 +16,7 @@ code-quality: stage: test needs: [] script: - - python3 -m flake8 --exclude actions/domainname-change,actions/networks container plinth actions/* + - python3 -m flake8 --exclude actions/networks container plinth actions/* unit-tests: stage: test diff --git a/actions/domainname-change b/actions/domainname-change deleted file mode 100755 index ef058297d..000000000 --- a/actions/domainname-change +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/sh -# SPDX-License-Identifier: AGPL-3.0-or-later - -domainname="$1" -hostname=$(hostname) - -if [ -z "$domainname" ] ; then - if grep -q 127.0.1.1 /etc/hosts ; then - sed -i "s/127.0.1.1.*/127.0.1.1 $hostname/" /etc/hosts - else - sed -i "/127.0.0.1.*/a \ - 127.0.1.1 $hostname" /etc/hosts - fi -else - if grep -q 127.0.1.1 /etc/hosts ; then - sed -i "s/127.0.1.1.*/127.0.1.1 $hostname.$domainname $hostname/" /etc/hosts - else - sed -i "/127.0.0.1.*/a \ - 127.0.1.1 $hostname.$domainname $hostname" /etc/hosts - fi -fi diff --git a/plinth/modules/config/privileged.py b/plinth/modules/config/privileged.py index 077a4a674..8dd170975 100644 --- a/plinth/modules/config/privileged.py +++ b/plinth/modules/config/privileged.py @@ -4,6 +4,7 @@ import os import pathlib import subprocess +from typing import Optional import augeas @@ -29,6 +30,32 @@ def set_hostname(hostname: str): action_utils.service_restart('avahi-daemon') +@privileged +def set_domainname(domainname: Optional[str] = None): + """Set system domainname in /etc/hosts.""" + hostname = subprocess.check_output(['hostname']).decode().strip() + hosts_path = pathlib.Path('/etc/hosts') + if domainname: + insert_line = f'127.0.1.1 {hostname}.{domainname} {hostname}\n' + else: + insert_line = f'127.0.1.1 {hostname}\n' + + lines = hosts_path.read_text(encoding='utf-8').splitlines(keepends=True) + new_lines = [] + found = False + for line in lines: + if '127.0.1.1' in line: + new_lines.append(insert_line) + found = True + else: + new_lines.append(line) + + if not found: + new_lines.append(insert_line) + + hosts_path.write_text(''.join(new_lines), encoding='utf-8') + + def load_augeas(): """Initialize Augeas.""" aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD + diff --git a/plinth/modules/config/views.py b/plinth/modules/config/views.py index 97d8dc6d0..3e5b6fd67 100644 --- a/plinth/modules/config/views.py +++ b/plinth/modules/config/views.py @@ -1,14 +1,12 @@ # SPDX-License-Identifier: AGPL-3.0-or-later -""" -FreedomBox views for basic system configuration. -""" +"""FreedomBox views for basic system configuration.""" import logging from django.contrib import messages from django.utils.translation import gettext as _ -from plinth import actions, views +from plinth import views from plinth.modules import config from plinth.signals import (domain_added, domain_removed, post_hostname_change, pre_hostname_change) @@ -21,11 +19,12 @@ LOGGER = logging.getLogger(__name__) class ConfigAppView(views.AppView): """Serve configuration page.""" + form_class = ConfigurationForm app_id = 'config' def get_initial(self): - """Return the current status""" + """Return the current status.""" return { 'hostname': config.get_hostname(), 'domainname': config.get_domainname(), @@ -35,7 +34,7 @@ class ConfigAppView(views.AppView): } def form_valid(self, form): - """Apply the form changes""" + """Apply the form changes.""" old_status = form.initial new_status = form.cleaned_data @@ -117,7 +116,7 @@ def set_hostname(hostname): privileged.set_hostname(hostname) LOGGER.info('Setting domain name after hostname change - %s', domainname) - actions.superuser_run('domainname-change', [domainname]) + privileged.set_domainname(domainname) post_hostname_change.send_robust(sender='config', old_hostname=old_hostname, @@ -125,7 +124,7 @@ def set_hostname(hostname): def set_domainname(domainname, old_domainname): - """Sets machine domain name to domainname""" + """Set machine domain name to domainname.""" old_domainname = config.get_domainname() # Domain name is not case sensitive, but Let's Encrypt certificate @@ -133,7 +132,7 @@ def set_domainname(domainname, old_domainname): domainname = domainname.lower() LOGGER.info('Changing domain name to - %s', domainname) - actions.superuser_run('domainname-change', [domainname]) + privileged.set_domainname(domainname) # Update domain registered with Name Services module. if old_domainname: