[fix] Pep8 and define SSHD_CONF

This commit is contained in:
ljf 2018-08-26 20:29:11 +02:00 committed by Alexandre Aubin
parent 4e92a36322
commit 4602439c01
2 changed files with 14 additions and 19 deletions

View file

@ -1,4 +1,3 @@
import subprocess
import os import os
import re import re
@ -15,6 +14,8 @@ from yunohost.service import service_regen_conf, _get_conf_hashes, \
logger = getActionLogger('yunohost.migration') logger = getActionLogger('yunohost.migration')
SSHD_CONF = '/etc/ssh/sshd_config'
class MyMigration(Migration): class MyMigration(Migration):
""" """
@ -25,30 +26,30 @@ class MyMigration(Migration):
def migrate(self): def migrate(self):
# Create sshd_config.d dir # Create sshd_config.d dir
if not os.path.exists('/etc/ssh/sshd_config.d'): if not os.path.exists(SSHD_CONF + '.d'):
mkdir('/etc/ssh/sshd_config.d', 0755, uid='root', gid='root') mkdir(SSHD_CONF + '.d', 0755, uid='root', gid='root')
# Manage SSHd in all case # Manage SSHd in all case
if os.path.exists('/etc/yunohost/from_script'): if os.path.exists('/etc/yunohost/from_script'):
rm('/etc/yunohost/from_script') rm('/etc/yunohost/from_script')
copyfile('/etc/ssh/sshd_config', '/etc/ssh/sshd_config.bkp') copyfile(SSHD_CONF, '/etc/ssh/sshd_config.bkp')
service_regen_conf(names=['ssh'], force=True) service_regen_conf(names=['ssh'], force=True)
copyfile('/etc/ssh/sshd_config.bkp', '/etc/ssh/sshd_config') copyfile('/etc/ssh/sshd_config.bkp', SSHD_CONF)
# If custom conf, add 'Include' instruction # If custom conf, add 'Include' instruction
ynh_hash = _get_conf_hashes('ssh')['/etc/ssh/sshd_config'] ynh_hash = _get_conf_hashes('ssh')[SSHD_CONF]
current_hash = _calculate_hash('/etc/ssh/sshd_config') current_hash = _calculate_hash(SSHD_CONF)
include_rgx = r'^[ \t]*Include[ \t]+sshd_config\.d/\*[ \t]*(?:#.*)?$'
if ynh_hash != current_hash: if ynh_hash != current_hash:
add_include = False add_include = False
include_rgx = r'^[ \t]*Include[ \t]+sshd_config\.d/\*[ \t]*(?:#.*)?$' for line in open(SSHD_CONF):
for line in open('/etc/ssh/sshd_config'):
if re.match(include_rgx, line) is not None: if re.match(include_rgx, line) is not None:
add_include = True add_include = True
break break
if add_include: if add_include:
with open("/etc/ssh/sshd_config", "a") as conf: with open(SSHD_CONF, "a") as conf:
conf.write('Include sshd_config.d/*') conf.write('Include sshd_config.d/*')
# Restart ssh and backward if it fail # Restart ssh and backward if it fail
@ -56,11 +57,9 @@ class MyMigration(Migration):
self.backward() self.backward()
raise MoulinetteError(m18n.n("migration_0006_cancel")) raise MoulinetteError(m18n.n("migration_0006_cancel"))
def backward(self): def backward(self):
# We don't backward completely but it should be enough # We don't backward completely but it should be enough
copyfile('/etc/ssh/sshd_config.bkp', '/etc/ssh/sshd_config') copyfile('/etc/ssh/sshd_config.bkp', SSHD_CONF)
if not _run_service_command('restart', 'ssh'): if not _run_service_command('restart', 'ssh'):
raise MoulinetteError(m18n.n("migration_0006_cannot_restart")) raise MoulinetteError(m18n.n("migration_0006_cannot_restart"))

View file

@ -1,15 +1,12 @@
import subprocess
import os
import re import re
from shutil import copyfile
from moulinette import m18n from moulinette import m18n
from moulinette.core import MoulinetteError from moulinette.core import MoulinetteError
from moulinette.utils.log import getActionLogger from moulinette.utils.log import getActionLogger
from yunohost.tools import Migration from yunohost.tools import Migration
from yunohost.service import service_regen_conf, _get_conf_hashes, _calculate_hash from yunohost.service import service_regen_conf, _get_conf_hashes, \
_calculate_hash
logger = getActionLogger('yunohost.migration') logger = getActionLogger('yunohost.migration')
@ -37,7 +34,6 @@ class MyMigration(Migration):
return "manual" return "manual"
@property @property
def disclaimer(self): def disclaimer(self):