mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
[enh] Add and remove only one domain and catch creation error
This commit is contained in:
parent
81215f7f12
commit
b932844459
4 changed files with 78 additions and 89 deletions
|
@ -243,9 +243,8 @@ domain:
|
||||||
configuration:
|
configuration:
|
||||||
authenticate: all
|
authenticate: all
|
||||||
arguments:
|
arguments:
|
||||||
domains:
|
domain:
|
||||||
help: Domain name to add
|
help: Domain name to add
|
||||||
nargs: '+'
|
|
||||||
extra:
|
extra:
|
||||||
pattern:
|
pattern:
|
||||||
- '^([a-zA-Z0-9]{1}([a-zA-Z0-9\-]*[a-zA-Z0-9])*)(\.[a-zA-Z0-9]{1}([a-zA-Z0-9\-]*[a-zA-Z0-9])*)*(\.[a-zA-Z]{1}([a-zA-Z0-9\-]*[a-zA-Z0-9])*)$'
|
- '^([a-zA-Z0-9]{1}([a-zA-Z0-9\-]*[a-zA-Z0-9])*)(\.[a-zA-Z0-9]{1}([a-zA-Z0-9\-]*[a-zA-Z0-9])*)*(\.[a-zA-Z]{1}([a-zA-Z0-9\-]*[a-zA-Z0-9])*)$'
|
||||||
|
@ -266,9 +265,8 @@ domain:
|
||||||
configuration:
|
configuration:
|
||||||
authenticate: all
|
authenticate: all
|
||||||
arguments:
|
arguments:
|
||||||
domains:
|
domain:
|
||||||
help: Domain(s) to delete
|
help: Domain to delete
|
||||||
nargs: "+"
|
|
||||||
extra:
|
extra:
|
||||||
pattern:
|
pattern:
|
||||||
- '^([a-zA-Z0-9]{1}([a-zA-Z0-9\-]*[a-zA-Z0-9])*)(\.[a-zA-Z0-9]{1}([a-zA-Z0-9\-]*[a-zA-Z0-9])*)*(\.[a-zA-Z]{1}([a-zA-Z0-9\-]*[a-zA-Z0-9])*)$'
|
- '^([a-zA-Z0-9]{1}([a-zA-Z0-9\-]*[a-zA-Z0-9])*)(\.[a-zA-Z0-9]{1}([a-zA-Z0-9\-]*[a-zA-Z0-9])*)*(\.[a-zA-Z]{1}([a-zA-Z0-9\-]*[a-zA-Z0-9])*)$'
|
||||||
|
|
157
domain.py
157
domain.py
|
@ -64,12 +64,12 @@ def domain_list(auth, filter=None, limit=None, offset=None):
|
||||||
return { 'domains': result_list }
|
return { 'domains': result_list }
|
||||||
|
|
||||||
|
|
||||||
def domain_add(auth, domains, main=False, dyndns=False):
|
def domain_add(auth, domain, main=False, dyndns=False):
|
||||||
"""
|
"""
|
||||||
Create a custom domain
|
Create a custom domain
|
||||||
|
|
||||||
Keyword argument:
|
Keyword argument:
|
||||||
domains -- Domain name to add
|
domain -- Domain name to add
|
||||||
main -- Is the main domain
|
main -- Is the main domain
|
||||||
dyndns -- Subscribe to DynDNS
|
dyndns -- Subscribe to DynDNS
|
||||||
|
|
||||||
|
@ -81,38 +81,34 @@ def domain_add(auth, domains, main=False, dyndns=False):
|
||||||
ip = "127.0.0.1"
|
ip = "127.0.0.1"
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
timestamp = str(now.year) + str(now.month) + str(now.day)
|
timestamp = str(now.year) + str(now.month) + str(now.day)
|
||||||
result = []
|
|
||||||
|
|
||||||
if not isinstance(domains, list):
|
if domain in domain_list(auth)['domains']:
|
||||||
domains = [ domains ]
|
raise MoulinetteError(errno.EEXIST, m18n.n('domain_exists'))
|
||||||
|
|
||||||
for domain in domains:
|
# DynDNS domain
|
||||||
if domain in domain_list(auth)['domains']:
|
if dyndns:
|
||||||
continue
|
if len(domain.split('.')) < 3:
|
||||||
|
raise MoulinetteError(errno.EINVAL, m18n.n('domain_dyndns_invalid'))
|
||||||
|
import requests
|
||||||
|
from yunohost.dyndns import dyndns_subscribe
|
||||||
|
|
||||||
# DynDNS domain
|
try:
|
||||||
if dyndns:
|
r = requests.get('http://dyndns.yunohost.org/domains')
|
||||||
if len(domain.split('.')) < 3:
|
except ConnectionError:
|
||||||
raise MoulinetteError(errno.EINVAL, m18n.n('domain_dyndns_invalid'))
|
pass
|
||||||
import requests
|
else:
|
||||||
from yunohost.dyndns import dyndns_subscribe
|
dyndomains = json.loads(r.text)
|
||||||
|
dyndomain = '.'.join(domain.split('.')[1:])
|
||||||
try:
|
if dyndomain in dyndomains:
|
||||||
r = requests.get('http://dyndns.yunohost.org/domains')
|
if os.path.exists('/etc/cron.d/yunohost-dyndns'):
|
||||||
except ConnectionError:
|
raise MoulinetteError(errno.EPERM,
|
||||||
pass
|
m18n.n('domain_dyndns_already_subscribed'))
|
||||||
|
dyndns_subscribe(domain=domain)
|
||||||
else:
|
else:
|
||||||
dyndomains = json.loads(r.text)
|
raise MoulinetteError(errno.EINVAL,
|
||||||
dyndomain = '.'.join(domain.split('.')[1:])
|
m18n.n('domain_dyndns_root_unknown'))
|
||||||
if dyndomain in dyndomains:
|
|
||||||
if os.path.exists('/etc/cron.d/yunohost-dyndns'):
|
|
||||||
raise MoulinetteError(errno.EPERM,
|
|
||||||
m18n.n('domain_dyndns_already_subscribed'))
|
|
||||||
dyndns_subscribe(domain=domain)
|
|
||||||
else:
|
|
||||||
raise MoulinetteError(errno.EINVAL,
|
|
||||||
m18n.n('domain_dyndns_root_unknown'))
|
|
||||||
|
|
||||||
|
try:
|
||||||
# Commands
|
# Commands
|
||||||
ssl_dir = '/usr/share/yunohost/yunohost-config/ssl/yunoCA'
|
ssl_dir = '/usr/share/yunohost/yunohost-config/ssl/yunoCA'
|
||||||
ssl_domain_path = '/etc/yunohost/certs/%s' % domain
|
ssl_domain_path = '/etc/yunohost/certs/%s' % domain
|
||||||
|
@ -236,75 +232,69 @@ def domain_add(auth, domains, main=False, dyndns=False):
|
||||||
os.system('sed -i s/yunohost.org/%s/g /etc/nginx/conf.d/%s.conf' % (domain, domain))
|
os.system('sed -i s/yunohost.org/%s/g /etc/nginx/conf.d/%s.conf' % (domain, domain))
|
||||||
os.system('service nginx reload')
|
os.system('service nginx reload')
|
||||||
|
|
||||||
if auth.add('virtualdomain=%s,ou=domains' % domain, attr_dict):
|
if not auth.add('virtualdomain=%s,ou=domains' % domain, attr_dict):
|
||||||
result.append(domain)
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
raise MoulinetteError(errno.EIO, m18n.n('domain_creation_failed'))
|
raise MoulinetteError(errno.EIO, m18n.n('domain_creation_failed'))
|
||||||
|
|
||||||
|
os.system('yunohost app ssowatconf > /dev/null 2>&1')
|
||||||
os.system('yunohost app ssowatconf > /dev/null 2>&1')
|
except:
|
||||||
|
# Force domain removal
|
||||||
|
domain_remove(auth, domain, True)
|
||||||
|
raise
|
||||||
|
|
||||||
msignals.display(m18n.n('domain_created'), 'success')
|
msignals.display(m18n.n('domain_created'), 'success')
|
||||||
return { 'domains': result }
|
|
||||||
|
|
||||||
|
|
||||||
def domain_remove(auth, domains):
|
def domain_remove(auth, domain, force=False):
|
||||||
"""
|
"""
|
||||||
Delete domains
|
Delete domains
|
||||||
|
|
||||||
Keyword argument:
|
Keyword argument:
|
||||||
domains -- Domain(s) to delete
|
domain -- Domain to delete
|
||||||
|
force -- Force the domain removal
|
||||||
|
|
||||||
"""
|
"""
|
||||||
result = []
|
if not force and domain not in domain_list(auth)['domains']:
|
||||||
domains_list = domain_list(auth)['domains']
|
raise MoulinetteError(errno.EINVAL, m18n.n('domain_unknown'))
|
||||||
|
|
||||||
if not isinstance(domains, list):
|
# Check if apps are installed on the domain
|
||||||
domains = [ domains ]
|
for app in os.listdir('/etc/yunohost/apps/'):
|
||||||
|
with open('/etc/yunohost/apps/' + app +'/settings.yml') as f:
|
||||||
for domain in domains:
|
|
||||||
if domain not in domains_list:
|
|
||||||
raise MoulinetteError(errno.EINVAL, m18n.n('domain_unknown'))
|
|
||||||
|
|
||||||
# Check if apps are installed on the domain
|
|
||||||
for app in os.listdir('/etc/yunohost/apps/'):
|
|
||||||
with open('/etc/yunohost/apps/' + app +'/settings.yml') as f:
|
|
||||||
try:
|
|
||||||
app_domain = yaml.load(f)['domain']
|
|
||||||
except:
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
if app_domain == domain:
|
|
||||||
raise MoulinetteError(errno.EPERM,
|
|
||||||
m18n.n('domain_uninstall_app_first'))
|
|
||||||
|
|
||||||
if auth.remove('virtualdomain=' + domain + ',ou=domains'):
|
|
||||||
try:
|
try:
|
||||||
shutil.rmtree('/etc/yunohost/certs/%s' % domain)
|
app_domain = yaml.load(f)['domain']
|
||||||
os.remove('/var/lib/bind/%s.zone' % domain)
|
|
||||||
shutil.rmtree('/var/lib/metronome/%s' % domain.replace('.', '%2e'))
|
|
||||||
os.remove('/etc/metronome/conf.d/%s.cfg.lua' % domain)
|
|
||||||
shutil.rmtree('/etc/nginx/conf.d/%s.d' % domain)
|
|
||||||
os.remove('/etc/nginx/conf.d/%s.conf' % domain)
|
|
||||||
except:
|
except:
|
||||||
pass
|
continue
|
||||||
with open('/etc/bind/named.conf.local', 'r') as conf:
|
else:
|
||||||
conf_lines = conf.readlines()
|
if app_domain == domain:
|
||||||
with open('/etc/bind/named.conf.local', 'w') as conf:
|
raise MoulinetteError(errno.EPERM,
|
||||||
in_block = False
|
m18n.n('domain_uninstall_app_first'))
|
||||||
for line in conf_lines:
|
|
||||||
if re.search(r'^zone "%s' % domain, line):
|
if auth.remove('virtualdomain=' + domain + ',ou=domains') or force:
|
||||||
in_block = True
|
command_list = [
|
||||||
if in_block:
|
'rm -rf /etc/yunohost/certs/%s' % domain,
|
||||||
if re.search(r'^};$', line):
|
'rm -f /var/lib/bind/%s.zone' % domain,
|
||||||
in_block = False
|
'rm -rf /var/lib/metronome/%s' % domain.replace('.', '%2e'),
|
||||||
else:
|
'rm -f /etc/metronome/conf.d/%s.cfg.lua' % domain,
|
||||||
conf.write(line)
|
'rm -rf /etc/nginx/conf.d/%s.d' % domain,
|
||||||
result.append(domain)
|
'rm -f /etc/nginx/conf.d/%s.conf' % domain,
|
||||||
continue
|
]
|
||||||
else:
|
for command in command_list:
|
||||||
raise MoulinetteError(errno.EIO, m18n.n('domain_deletion_failed'))
|
if os.system(command) != 0:
|
||||||
|
msignals.display(m18n.n('path_removal_failed', command[7:]),
|
||||||
|
'warning')
|
||||||
|
with open('/etc/bind/named.conf.local', 'r') as conf:
|
||||||
|
conf_lines = conf.readlines()
|
||||||
|
with open('/etc/bind/named.conf.local', 'w') as conf:
|
||||||
|
in_block = False
|
||||||
|
for line in conf_lines:
|
||||||
|
if re.search(r'^zone "%s' % domain, line):
|
||||||
|
in_block = True
|
||||||
|
if in_block:
|
||||||
|
if re.search(r'^};$', line):
|
||||||
|
in_block = False
|
||||||
|
else:
|
||||||
|
conf.write(line)
|
||||||
|
else:
|
||||||
|
raise MoulinetteError(errno.EIO, m18n.n('domain_deletion_failed'))
|
||||||
|
|
||||||
os.system('yunohost app ssowatconf > /dev/null 2>&1')
|
os.system('yunohost app ssowatconf > /dev/null 2>&1')
|
||||||
os.system('service nginx reload')
|
os.system('service nginx reload')
|
||||||
|
@ -312,4 +302,3 @@ def domain_remove(auth, domains):
|
||||||
os.system('service metronome restart')
|
os.system('service metronome restart')
|
||||||
|
|
||||||
msignals.display(m18n.n('domain_deleted'), 'success')
|
msignals.display(m18n.n('domain_deleted'), 'success')
|
||||||
return { 'domains': result }
|
|
||||||
|
|
|
@ -38,6 +38,7 @@
|
||||||
"executing_script": "Executing script...",
|
"executing_script": "Executing script...",
|
||||||
"done" : "Done.",
|
"done" : "Done.",
|
||||||
|
|
||||||
|
"path_removal_failed" : "Unable to remove path {:s}",
|
||||||
"domain_unknown" : "Unknown domain",
|
"domain_unknown" : "Unknown domain",
|
||||||
"domain_dyndns_invalid" : "Invalid domain to use with DynDNS",
|
"domain_dyndns_invalid" : "Invalid domain to use with DynDNS",
|
||||||
"domain_dyndns_already_subscribed" : "You already have subscribed to a DynDNS domain",
|
"domain_dyndns_already_subscribed" : "You already have subscribed to a DynDNS domain",
|
||||||
|
|
|
@ -38,6 +38,7 @@
|
||||||
"executing_script": "Exécution du script...",
|
"executing_script": "Exécution du script...",
|
||||||
"done" : "Terminé.",
|
"done" : "Terminé.",
|
||||||
|
|
||||||
|
"path_removal_failed" : "Impossible de supprimer le chemin {:s}",
|
||||||
"domain_unknown" : "Domaine inconnu",
|
"domain_unknown" : "Domaine inconnu",
|
||||||
"domain_dyndns_invalid" : "Domaine incorrect pour un usage avec DynDNS",
|
"domain_dyndns_invalid" : "Domaine incorrect pour un usage avec DynDNS",
|
||||||
"domain_dyndns_already_subscribed" : "Vous avez déjà souscris à un domaine DynDNS",
|
"domain_dyndns_already_subscribed" : "Vous avez déjà souscris à un domaine DynDNS",
|
||||||
|
|
Loading…
Add table
Reference in a new issue