Allow user to change domain name, then determine the FQDN from it.

This commit is contained in:
James Valleroy 2014-12-20 14:01:59 -05:00 committed by Sunil Mohan Adapa
parent 87b860bff4
commit 3d7de0778b
5 changed files with 51 additions and 36 deletions

View File

@ -16,12 +16,12 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
fqdn="$1"
domainname="$1"
hostname=$(hostname)
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
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

View File

@ -23,8 +23,8 @@ Configuration helper for the ejabberd service
import argparse
import subprocess
import time
import os
import socket
import re
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.')
return
old_hostname = arguments.old_hostname
new_hostname = arguments.new_hostname
subprocess.call(['service', 'ejabberd', 'stop'])
subprocess.call(['pkill', '-u', 'ejabberd'])
@ -142,6 +139,7 @@ def subcommand_change_domainname(arguments):
return
domainname = arguments.domainname
fqdn = socket.gethostname() + '.' + domainname
# update jwchat's sitename, if it's installed
if os.path.exists(JWCHAT_CONFIG):
@ -150,7 +148,7 @@ def subcommand_change_domainname(arguments):
with open(JWCHAT_CONFIG, 'w') as conffile:
for line in lines:
if re.match(r'\s*var\s+SITENAME', line):
conffile.write('var SITENAME = "' + domainname + '";\n')
conffile.write('var SITENAME = "' + fqdn + '";\n')
else:
conffile.write(line)
else:
@ -160,14 +158,14 @@ def subcommand_change_domainname(arguments):
subprocess.call(['service', 'ejabberd', 'stop'])
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:
lines = conffile.readlines()
with open(EJABBERD_CONFIG, 'w') as conffile:
for line in lines:
conffile.write(line)
if re.match(r'\s*hosts:', line):
conffile.write(' - "' + domainname + '"\n')
conffile.write(' - "' + fqdn + '"\n')
subprocess.call(['service', 'ejabberd', 'start'])

View File

@ -32,7 +32,7 @@ import socket
from plinth import actions
from plinth import cfg
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__)
@ -43,9 +43,10 @@ def get_hostname():
return socket.gethostname()
def get_fqdn():
"""Return the fully qualified domain name"""
return socket.getfqdn()
def get_domainname():
"""Return the domainname"""
fqdn = socket.getfqdn()
return '.'.join(fqdn.split('.')[1:])
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}$',
_('Invalid hostname'))])
fqdn = TrimmedCharField(
label=_('FQDN'),
help_text=_('Your FQDN is the global name by which other machines \
on the Internet can reach you. It must consist of alphanumeric words \
domainname = TrimmedCharField(
label=_('Domain Name'),
help_text=_('Your domain name is the global name by which other \
machines on the Internet can reach you. It must consist of alphanumeric words \
separated by dots.'),
validators=[
validators.RegexValidator(r'^[a-zA-Z][a-zA-Z0-9.]*$',
_('Invalid FQDN'))])
_('Invalid domain name'))])
def __init__(self, *args, **kwargs):
# pylint: disable-msg=E1101, W0233
@ -140,7 +141,7 @@ def index(request):
def get_status():
"""Return the current status"""
return {'hostname': get_hostname(),
'fqdn': get_fqdn(),
'domainname': get_domainname(),
'time_zone': open('/etc/timezone').read().rstrip()}
@ -157,16 +158,16 @@ def _apply_changes(request, old_status, new_status):
else:
messages.info(request, _('Hostname is unchanged'))
if old_status['fqdn'] != new_status['fqdn']:
if old_status['domainname'] != new_status['domainname']:
try:
set_fqdn(new_status['fqdn'])
set_domainname(new_status['domainname'])
except Exception as exception:
messages.error(request, _('Error setting FQDN: %s') %
messages.error(request, _('Error setting domain name: %s') %
exception)
else:
messages.success(request, _('FQDN set'))
messages.success(request, _('Domain name set'))
else:
messages.info(request, _('FQDN is unchanged'))
messages.info(request, _('Domain name is unchanged'))
if old_status['time_zone'] != new_status['time_zone']:
try:
@ -200,16 +201,16 @@ def set_hostname(hostname):
new_hostname=hostname)
def set_fqdn(fqdn):
"""Sets machine FQDN to fqdn"""
old_fqdn = get_fqdn()
def set_domainname(domainname):
"""Sets machine domain name to domainname"""
old_domainname = get_domainname()
# FQDN should be ASCII. If it's unicode, convert to ASCII.
fqdn = str(fqdn)
# Domain name should be ASCII. If it's unicode, convert to ASCII.
domainname = str(domainname)
LOGGER.info('Changing FQDN to - %s', fqdn)
actions.superuser_run('fqdn-change', fqdn)
LOGGER.info('Changing domain name to - %s', domainname)
actions.superuser_run('domainname-change', domainname)
fqdn_change.send_robust(sender='config',
old_fqdn=old_fqdn,
new_fqdn=fqdn)
domainname_change.send_robust(sender='config',
old_domainname=old_domainname,
new_domainname=domainname)

View File

@ -27,6 +27,7 @@ from plinth import actions
from plinth import cfg
from plinth import service
from plinth.signals import pre_hostname_change, post_hostname_change
from plinth.signals import domainname_change
LOGGER = logging.getLogger(__name__)
@ -56,6 +57,7 @@ def init():
pre_hostname_change.connect(on_pre_hostname_change)
post_hostname_change.connect(on_post_hostname_change)
domainname_change.connect(on_domainname_change)
@login_required
@ -209,3 +211,17 @@ def on_post_hostname_change(sender, old_hostname, new_hostname, **kwargs):
'--old-hostname', old_hostname,
'--new-hostname', new_hostname],
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)

View File

@ -27,4 +27,4 @@ pre_module_loading = Signal()
post_module_loading = Signal()
pre_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'])