Track which section of ejabberd config we are in, and only edit the hosts section.

Check for jwchat config and ejabberd dumpfile before trying to modify them.
This commit is contained in:
James Valleroy 2014-12-03 21:10:01 -05:00 committed by Sunil Mohan Adapa
parent 6fc721a727
commit 10b234921b

View File

@ -69,37 +69,53 @@ def subcommand_get_installed(_):
def subcommand_change_hostname(arguments): def subcommand_change_hostname(arguments):
"""Update ejabberd and jwchat with new hostname""" """Update ejabberd and jwchat with new hostname"""
if not get_installed(): if not get_installed():
print('Failed to update XMPP hostname: ejabberd is not installed.') return
old_hostname = arguments.old_hostname old_hostname = arguments.old_hostname
new_hostname = arguments.new_hostname new_hostname = arguments.new_hostname
with open(JWCHAT_CONFIG, 'r') as conffile: # update jwchat's sitename, if it's installed
lines = conffile.readlines() if os.path.exists(JWCHAT_CONFIG):
with open(JWCHAT_CONFIG, 'w') as conffile: with open(JWCHAT_CONFIG, 'r') as conffile:
for line in lines: lines = conffile.readlines()
conffile.write(re.sub('var SITENAME = "' + old_hostname + '";', with open(JWCHAT_CONFIG, 'w') as conffile:
'var SITENAME = "' + new_hostname + '";', for line in lines:
line)) conffile.write(re.sub('var SITENAME = "' + old_hostname + '";',
'var SITENAME = "' + new_hostname + '";',
line))
# update ejabberd hosts
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:
in_hosts_section = False
for line in lines: for line in lines:
conffile.write(re.sub(old_hostname, new_hostname, line)) if in_hosts_section:
if line.startswith(' - "'):
conffile.write(re.sub(old_hostname, new_hostname, line))
else:
in_hosts_section = False
conffile.write(line)
else:
if line.startswith('hosts:'):
in_hosts_section = True
conffile.write(line)
with open(EJABBERD_BACKUP, 'r') as dumpfile: # update ejabberd backup database
lines = dumpfile.readlines() if os.path.exists(EJABBERD_BACKUP):
with open(EJABBERD_BACKUP, 'w') as dumpfile: with open(EJABBERD_BACKUP, 'r') as dumpfile:
for line in lines: lines = dumpfile.readlines()
dumpfile.write(re.sub(old_hostname, new_hostname, line)) with open(EJABBERD_BACKUP, 'w') as dumpfile:
for line in lines:
dumpfile.write(re.sub(old_hostname, new_hostname, line))
subprocess.call(['service', 'ejabberd', 'restart']) subprocess.call(['service', 'ejabberd', 'restart'])
# load backup database # load backup database
time.sleep(10) if os.path.exists(EJABBERD_BACKUP):
subprocess.call(['ejabberdctl', 'load', EJABBERD_BACKUP]) time.sleep(10)
os.remove(EJABBERD_BACKUP) subprocess.call(['ejabberdctl', 'load', EJABBERD_BACKUP])
os.remove(EJABBERD_BACKUP)
def subcommand_register(arguments): def subcommand_register(arguments):