[fix] Backward if can't restart

This commit is contained in:
ljf 2018-08-26 20:20:35 +02:00 committed by Alexandre Aubin
parent c2b225d376
commit 4e92a36322
2 changed files with 21 additions and 14 deletions

View file

@ -294,6 +294,8 @@
"migration_0005_not_enough_space": "Not enough space is available in {path} to run the migration right now :(.",
"migration_0006_disclaimer": "Yunohost now expects admin and root passwords to be synchronized. By running this migration, your root password is going to be replaced by the admin password.",
"migration_0006_done": "Your root password have been replaced by your admin password.",
"migration_0006_cancelled": "YunoHost has failed to improve the way your ssh conf is managed.",
"migration_0006_cannot_restart": "SSH can't be restarted after we tried to cancel the migration 6.",
"migration_0007_general_warning": "To ensure a global security of your server, YunoHost recommends to let it manage the SSH configuration of your server. Your current SSH configuration differs from common default configuration. If you let YunoHost reconfigure it, the way to access with SSH to your server could change after this migration:",
"migration_0007_port": "- you will have to connect using port 22 instead of your custom SSH port. Feel free to reconfigure it",
"migration_0007_root": "- you will not be able to connect with root user, instead you will have to use admin user.",

View file

@ -31,31 +31,36 @@ class MyMigration(Migration):
# Manage SSHd in all case
if os.path.exists('/etc/yunohost/from_script'):
rm('/etc/yunohost/from_script')
copyfile('/etc/ssh/sshd_config', '/etc/ssh/sshd_config.restore')
copyfile('/etc/ssh/sshd_config', '/etc/ssh/sshd_config.bkp')
service_regen_conf(names=['ssh'], force=True)
os.rename('/etc/ssh/sshd_config.restore', '/etc/ssh/sshd_config')
copyfile('/etc/ssh/sshd_config.bkp', '/etc/ssh/sshd_config')
# If custom conf, add 'Include' instruction
ynh_hash = _get_conf_hashes('ssh')['/etc/ssh/sshd_config']
current_hash = _calculate_hash('/etc/ssh/sshd_config')
if ynh_hash == current_hash:
return
if ynh_hash != current_hash:
add_include = False
include_rgx = r'^[ \t]*Include[ \t]+sshd_config\.d/\*[ \t]*(?:#.*)?$'
for line in open('/etc/ssh/sshd_config'):
if re.match(include_rgx, line) is not None:
add_include = True
break
add_include = False
include_rgx = r'^[ \t]*Include[ \t]+sshd_config\.d/\*[ \t]*(?:#.*)?$'
for line in open('/etc/ssh/sshd_config'):
if re.match(include_rgx, line) is not None:
add_include = True
break
if add_include:
with open("/etc/ssh/sshd_config", "a") as conf:
conf.write('Include sshd_config.d/*')
if add_include:
with open("/etc/ssh/sshd_config", "a") as conf:
conf.write('Include sshd_config.d/*')
# Restart ssh and backward if it fail
if not _run_service_command('restart', 'ssh'):
self.backward()
raise MoulinetteError(m18n.n("migration_0006_cancel"))
def backward(self):
# We don't backward completely but it should be enough
raise MoulinetteError(m18n.n("migration_0006_backward_impossible"))
copyfile('/etc/ssh/sshd_config.bkp', '/etc/ssh/sshd_config')
if not _run_service_command('restart', 'ssh'):
raise MoulinetteError(m18n.n("migration_0006_cannot_restart"))