mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-09-19 04:59:01 +00:00
Allow user to change domain name, then determine the FQDN from it.
This commit is contained in:
parent
87b860bff4
commit
3d7de0778b
@ -16,12 +16,12 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
|
||||||
fqdn="$1"
|
domainname="$1"
|
||||||
hostname=$(hostname)
|
hostname=$(hostname)
|
||||||
|
|
||||||
if grep -q 127.0.1.1 /etc/hosts ; then
|
if grep -q 127.0.1.1 /etc/hosts ; then
|
||||||
sed -i "s/127.0.1.1.*/127.0.1.1 $fqdn $hostname/" /etc/hosts
|
sed -i "s/127.0.1.1.*/127.0.1.1 $hostname.$domainname $hostname/" /etc/hosts
|
||||||
else
|
else
|
||||||
sed -i "/127.0.0.1.*/a \
|
sed -i "/127.0.0.1.*/a \
|
||||||
127.0.1.1 $fqdn $hostname" /etc/hosts
|
127.0.1.1 $hostname.$domainname $hostname" /etc/hosts
|
||||||
fi
|
fi
|
||||||
12
actions/xmpp
12
actions/xmpp
@ -23,8 +23,8 @@ Configuration helper for the ejabberd service
|
|||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import subprocess
|
import subprocess
|
||||||
import time
|
|
||||||
import os
|
import os
|
||||||
|
import socket
|
||||||
import re
|
import re
|
||||||
|
|
||||||
JWCHAT_CONFIG = '/etc/jwchat/config.js'
|
JWCHAT_CONFIG = '/etc/jwchat/config.js'
|
||||||
@ -108,9 +108,6 @@ def subcommand_change_hostname(arguments):
|
|||||||
print('Failed to update XMPP hostname: ejabberd is not installed.')
|
print('Failed to update XMPP hostname: ejabberd is not installed.')
|
||||||
return
|
return
|
||||||
|
|
||||||
old_hostname = arguments.old_hostname
|
|
||||||
new_hostname = arguments.new_hostname
|
|
||||||
|
|
||||||
subprocess.call(['service', 'ejabberd', 'stop'])
|
subprocess.call(['service', 'ejabberd', 'stop'])
|
||||||
subprocess.call(['pkill', '-u', 'ejabberd'])
|
subprocess.call(['pkill', '-u', 'ejabberd'])
|
||||||
|
|
||||||
@ -142,6 +139,7 @@ def subcommand_change_domainname(arguments):
|
|||||||
return
|
return
|
||||||
|
|
||||||
domainname = arguments.domainname
|
domainname = arguments.domainname
|
||||||
|
fqdn = socket.gethostname() + '.' + domainname
|
||||||
|
|
||||||
# update jwchat's sitename, if it's installed
|
# update jwchat's sitename, if it's installed
|
||||||
if os.path.exists(JWCHAT_CONFIG):
|
if os.path.exists(JWCHAT_CONFIG):
|
||||||
@ -150,7 +148,7 @@ def subcommand_change_domainname(arguments):
|
|||||||
with open(JWCHAT_CONFIG, 'w') as conffile:
|
with open(JWCHAT_CONFIG, 'w') as conffile:
|
||||||
for line in lines:
|
for line in lines:
|
||||||
if re.match(r'\s*var\s+SITENAME', line):
|
if re.match(r'\s*var\s+SITENAME', line):
|
||||||
conffile.write('var SITENAME = "' + domainname + '";\n')
|
conffile.write('var SITENAME = "' + fqdn + '";\n')
|
||||||
else:
|
else:
|
||||||
conffile.write(line)
|
conffile.write(line)
|
||||||
else:
|
else:
|
||||||
@ -160,14 +158,14 @@ def subcommand_change_domainname(arguments):
|
|||||||
subprocess.call(['service', 'ejabberd', 'stop'])
|
subprocess.call(['service', 'ejabberd', 'stop'])
|
||||||
subprocess.call(['pkill', '-u', 'ejabberd'])
|
subprocess.call(['pkill', '-u', 'ejabberd'])
|
||||||
|
|
||||||
# add new domainname to top of ejabberd hosts list
|
# add updated FQDN to top of ejabberd hosts list
|
||||||
with open(EJABBERD_CONFIG, 'r') as conffile:
|
with open(EJABBERD_CONFIG, 'r') as conffile:
|
||||||
lines = conffile.readlines()
|
lines = conffile.readlines()
|
||||||
with open(EJABBERD_CONFIG, 'w') as conffile:
|
with open(EJABBERD_CONFIG, 'w') as conffile:
|
||||||
for line in lines:
|
for line in lines:
|
||||||
conffile.write(line)
|
conffile.write(line)
|
||||||
if re.match(r'\s*hosts:', line):
|
if re.match(r'\s*hosts:', line):
|
||||||
conffile.write(' - "' + domainname + '"\n')
|
conffile.write(' - "' + fqdn + '"\n')
|
||||||
|
|
||||||
subprocess.call(['service', 'ejabberd', 'start'])
|
subprocess.call(['service', 'ejabberd', 'start'])
|
||||||
|
|
||||||
|
|||||||
@ -32,7 +32,7 @@ import socket
|
|||||||
from plinth import actions
|
from plinth import actions
|
||||||
from plinth import cfg
|
from plinth import cfg
|
||||||
from plinth.signals import pre_hostname_change, post_hostname_change
|
from plinth.signals import pre_hostname_change, post_hostname_change
|
||||||
from plinth.signals import fqdn_change
|
from plinth.signals import domainname_change
|
||||||
|
|
||||||
|
|
||||||
LOGGER = logging.getLogger(__name__)
|
LOGGER = logging.getLogger(__name__)
|
||||||
@ -43,9 +43,10 @@ def get_hostname():
|
|||||||
return socket.gethostname()
|
return socket.gethostname()
|
||||||
|
|
||||||
|
|
||||||
def get_fqdn():
|
def get_domainname():
|
||||||
"""Return the fully qualified domain name"""
|
"""Return the domainname"""
|
||||||
return socket.getfqdn()
|
fqdn = socket.getfqdn()
|
||||||
|
return '.'.join(fqdn.split('.')[1:])
|
||||||
|
|
||||||
|
|
||||||
class TrimmedCharField(forms.CharField):
|
class TrimmedCharField(forms.CharField):
|
||||||
@ -75,14 +76,14 @@ and must not be greater than 63 characters in length.'),
|
|||||||
validators.RegexValidator(r'^[a-zA-Z][a-zA-Z0-9]{,62}$',
|
validators.RegexValidator(r'^[a-zA-Z][a-zA-Z0-9]{,62}$',
|
||||||
_('Invalid hostname'))])
|
_('Invalid hostname'))])
|
||||||
|
|
||||||
fqdn = TrimmedCharField(
|
domainname = TrimmedCharField(
|
||||||
label=_('FQDN'),
|
label=_('Domain Name'),
|
||||||
help_text=_('Your FQDN is the global name by which other machines \
|
help_text=_('Your domain name is the global name by which other \
|
||||||
on the Internet can reach you. It must consist of alphanumeric words \
|
machines on the Internet can reach you. It must consist of alphanumeric words \
|
||||||
separated by dots.'),
|
separated by dots.'),
|
||||||
validators=[
|
validators=[
|
||||||
validators.RegexValidator(r'^[a-zA-Z][a-zA-Z0-9.]*$',
|
validators.RegexValidator(r'^[a-zA-Z][a-zA-Z0-9.]*$',
|
||||||
_('Invalid FQDN'))])
|
_('Invalid domain name'))])
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
# pylint: disable-msg=E1101, W0233
|
# pylint: disable-msg=E1101, W0233
|
||||||
@ -140,7 +141,7 @@ def index(request):
|
|||||||
def get_status():
|
def get_status():
|
||||||
"""Return the current status"""
|
"""Return the current status"""
|
||||||
return {'hostname': get_hostname(),
|
return {'hostname': get_hostname(),
|
||||||
'fqdn': get_fqdn(),
|
'domainname': get_domainname(),
|
||||||
'time_zone': open('/etc/timezone').read().rstrip()}
|
'time_zone': open('/etc/timezone').read().rstrip()}
|
||||||
|
|
||||||
|
|
||||||
@ -157,16 +158,16 @@ def _apply_changes(request, old_status, new_status):
|
|||||||
else:
|
else:
|
||||||
messages.info(request, _('Hostname is unchanged'))
|
messages.info(request, _('Hostname is unchanged'))
|
||||||
|
|
||||||
if old_status['fqdn'] != new_status['fqdn']:
|
if old_status['domainname'] != new_status['domainname']:
|
||||||
try:
|
try:
|
||||||
set_fqdn(new_status['fqdn'])
|
set_domainname(new_status['domainname'])
|
||||||
except Exception as exception:
|
except Exception as exception:
|
||||||
messages.error(request, _('Error setting FQDN: %s') %
|
messages.error(request, _('Error setting domain name: %s') %
|
||||||
exception)
|
exception)
|
||||||
else:
|
else:
|
||||||
messages.success(request, _('FQDN set'))
|
messages.success(request, _('Domain name set'))
|
||||||
else:
|
else:
|
||||||
messages.info(request, _('FQDN is unchanged'))
|
messages.info(request, _('Domain name is unchanged'))
|
||||||
|
|
||||||
if old_status['time_zone'] != new_status['time_zone']:
|
if old_status['time_zone'] != new_status['time_zone']:
|
||||||
try:
|
try:
|
||||||
@ -200,16 +201,16 @@ def set_hostname(hostname):
|
|||||||
new_hostname=hostname)
|
new_hostname=hostname)
|
||||||
|
|
||||||
|
|
||||||
def set_fqdn(fqdn):
|
def set_domainname(domainname):
|
||||||
"""Sets machine FQDN to fqdn"""
|
"""Sets machine domain name to domainname"""
|
||||||
old_fqdn = get_fqdn()
|
old_domainname = get_domainname()
|
||||||
|
|
||||||
# FQDN should be ASCII. If it's unicode, convert to ASCII.
|
# Domain name should be ASCII. If it's unicode, convert to ASCII.
|
||||||
fqdn = str(fqdn)
|
domainname = str(domainname)
|
||||||
|
|
||||||
LOGGER.info('Changing FQDN to - %s', fqdn)
|
LOGGER.info('Changing domain name to - %s', domainname)
|
||||||
actions.superuser_run('fqdn-change', fqdn)
|
actions.superuser_run('domainname-change', domainname)
|
||||||
|
|
||||||
fqdn_change.send_robust(sender='config',
|
domainname_change.send_robust(sender='config',
|
||||||
old_fqdn=old_fqdn,
|
old_domainname=old_domainname,
|
||||||
new_fqdn=fqdn)
|
new_domainname=domainname)
|
||||||
|
|||||||
@ -27,6 +27,7 @@ from plinth import actions
|
|||||||
from plinth import cfg
|
from plinth import cfg
|
||||||
from plinth import service
|
from plinth import service
|
||||||
from plinth.signals import pre_hostname_change, post_hostname_change
|
from plinth.signals import pre_hostname_change, post_hostname_change
|
||||||
|
from plinth.signals import domainname_change
|
||||||
|
|
||||||
|
|
||||||
LOGGER = logging.getLogger(__name__)
|
LOGGER = logging.getLogger(__name__)
|
||||||
@ -56,6 +57,7 @@ def init():
|
|||||||
|
|
||||||
pre_hostname_change.connect(on_pre_hostname_change)
|
pre_hostname_change.connect(on_pre_hostname_change)
|
||||||
post_hostname_change.connect(on_post_hostname_change)
|
post_hostname_change.connect(on_post_hostname_change)
|
||||||
|
domainname_change.connect(on_domainname_change)
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
@ -209,3 +211,17 @@ def on_post_hostname_change(sender, old_hostname, new_hostname, **kwargs):
|
|||||||
'--old-hostname', old_hostname,
|
'--old-hostname', old_hostname,
|
||||||
'--new-hostname', new_hostname],
|
'--new-hostname', new_hostname],
|
||||||
async=True)
|
async=True)
|
||||||
|
|
||||||
|
|
||||||
|
def on_domainname_change(sender, old_domainname, new_domainname, **kwargs):
|
||||||
|
"""
|
||||||
|
Update ejabberd and jwchat config after domain name is changed.
|
||||||
|
"""
|
||||||
|
del sender # Unused
|
||||||
|
del old_domainname # Unused
|
||||||
|
del kwargs # Unused
|
||||||
|
|
||||||
|
actions.superuser_run('xmpp',
|
||||||
|
['change-domainname',
|
||||||
|
'--domainname', new_domainname],
|
||||||
|
async=True)
|
||||||
|
|||||||
@ -27,4 +27,4 @@ pre_module_loading = Signal()
|
|||||||
post_module_loading = Signal()
|
post_module_loading = Signal()
|
||||||
pre_hostname_change = Signal(providing_args=['old_hostname', 'new_hostname'])
|
pre_hostname_change = Signal(providing_args=['old_hostname', 'new_hostname'])
|
||||||
post_hostname_change = Signal(providing_args=['old_hostname', 'new_hostname'])
|
post_hostname_change = Signal(providing_args=['old_hostname', 'new_hostname'])
|
||||||
fqdn_change = Signal(providing_args=['old_fqdn', 'new_fqdn'])
|
domainname_change = Signal(providing_args=['old_domainname', 'new_domainname'])
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user