[fix] Adapt yunohost_app.py

This commit is contained in:
kload 2014-04-23 10:52:41 +00:00
parent 871884ea85
commit 18eb0f9406
2 changed files with 256 additions and 244 deletions

View file

@ -359,6 +359,8 @@ app:
install: install:
action_help: Install apps action_help: Install apps
api: POST /app api: POST /app
configuration:
authenticate: all
arguments: arguments:
app: app:
help: App to install help: App to install
@ -446,6 +448,8 @@ app:
checkurl: checkurl:
action_help: Check availability of a web path action_help: Check availability of a web path
api: GET /app/checkurl api: GET /app/checkurl
configuration:
authenticate: all
arguments: arguments:
url: url:
help: Url to check help: Url to check
@ -474,11 +478,15 @@ app:
ssowatconf: ssowatconf:
action_help: Regenerate SSOwat configuration file action_help: Regenerate SSOwat configuration file
api: PUT /ssowatconf api: PUT /ssowatconf
configuration:
authenticate: all
### app_addaccess() TODO: Write help ### app_addaccess() TODO: Write help
addaccess: addaccess:
action_help: Grant access right to users (everyone by default) action_help: Grant access right to users (everyone by default)
api: PUT /app/access api: PUT /app/access
configuration:
authenticate: all
arguments: arguments:
apps: apps:
nargs: "+" nargs: "+"
@ -490,6 +498,8 @@ app:
removeaccess: removeaccess:
action_help: Revoke access right to users (everyone by default) action_help: Revoke access right to users (everyone by default)
api: DELETE /app/access api: DELETE /app/access
configuration:
authenticate: all
arguments: arguments:
apps: apps:
nargs: "+" nargs: "+"
@ -501,6 +511,8 @@ app:
clearaccess: clearaccess:
action_help: Reset access rights for the app action_help: Reset access rights for the app
api: POST /app/access api: POST /app/access
configuration:
authenticate: all
arguments: arguments:
apps: apps:
nargs: "+" nargs: "+"

View file

@ -23,9 +23,6 @@
Manage apps Manage apps
""" """
import logging
logging.warning('the module yunohost.app has not been revisited and updated yet')
import os import os
import sys import sys
import json import json
@ -36,11 +33,9 @@ import time
import re import re
import socket import socket
import urlparse import urlparse
from domain import domain_list, domain_add
from user import user_info, user_list
from hook import hook_exec, hook_add, hook_remove
from moulinette.helpers import YunoHostError, YunoHostLDAP, win_msg, random_password, is_true, validate from moulinette.helpers import win_msg, random_password, is_true, validate
from moulinette.core import MoulinetteError
repo_path = '/var/cache/yunohost/repo' repo_path = '/var/cache/yunohost/repo'
apps_path = '/usr/share/yunohost/apps' apps_path = '/usr/share/yunohost/apps'
@ -60,7 +55,7 @@ def app_listlists():
if '.json' in filename: if '.json' in filename:
list_list.append(filename[:len(filename)-5]) list_list.append(filename[:len(filename)-5])
except OSError: except OSError:
raise YunoHostError(1, _("No list found")) raise MoulinetteError(1, _("No list found"))
return { 'Lists' : list_list } return { 'Lists' : list_list }
@ -82,20 +77,20 @@ def app_fetchlist(url=None, name=None):
url = 'http://app.yunohost.org/list.json' url = 'http://app.yunohost.org/list.json'
name = 'yunohost' name = 'yunohost'
else: else:
if name is None: raise YunoHostError(22, _("You must indicate a name for your custom list")) if name is None: raise MoulinetteError(22, _("You must indicate a name for your custom list"))
list_file = repo_path +'/'+ name +'.json' list_file = '%s/%s.json' % (repo_path, name)
if os.system('wget "'+ url +'" -O "'+ list_file +'.tmp"') != 0: if os.system('wget "%s" -O "%s.tmp"' % (url, list_file)) != 0:
os.remove(list_file +'.tmp') os.remove('%s.tmp' % list_file)
raise YunoHostError(1, _("List server connection failed")) raise MoulinetteError(1, _("List server connection failed"))
# Rename fetched temp list # Rename fetched temp list
os.rename(list_file +'.tmp', list_file) os.rename('%s.tmp' % list_file, list_file)
os.system("touch /etc/cron.d/yunohost-applist-"+ name) os.system("touch /etc/cron.d/yunohost-applist-%s" % name)
os.system("echo '00 00 * * * root yunohost app fetchlist -u "+ url +" -n "+ name +" --no-ldap > /dev/null 2>&1' >/etc/cron.d/yunohost-applist-"+ name) os.system("echo '00 00 * * * root yunohost app fetchlist -u %s -n %s --no-ldap > /dev/null 2>&1' >/etc/cron.d/yunohost-applist-%s" % (url, name, name))
win_msg(_("List successfully fetched")) msignals.display(_("List successfully fetched"), 'success')
def app_removelist(name): def app_removelist(name):
@ -107,12 +102,12 @@ def app_removelist(name):
""" """
try: try:
os.remove(repo_path +'/'+ name + '.json') os.remove('%s/%s.json' % (repo_path, name))
os.remove("/etc/cron.d/yunohost-applist-"+ name) os.remove("/etc/cron.d/yunohost-applist-%s" % name)
except OSError: except OSError:
raise YunoHostError(22, _("Unknown list")) raise MoulinetteError(22, _("Unknown list"))
win_msg(_("List successfully removed")) msignals.display(_("List successfully removed"), 'success')
def app_list(offset=None, limit=None, filter=None, raw=False): def app_list(offset=None, limit=None, filter=None, raw=False):
@ -126,8 +121,6 @@ def app_list(offset=None, limit=None, filter=None, raw=False):
raw -- Return the full app_dict raw -- Return the full app_dict
""" """
# TODO: List installed applications
if offset: offset = int(offset) if offset: offset = int(offset)
else: offset = 0 else: offset = 0
if limit: limit = int(limit) if limit: limit = int(limit)
@ -270,148 +263,60 @@ def app_upgrade(app, url=None, file=None):
url -- Git url to fetch for upgrade url -- Git url to fetch for upgrade
""" """
with YunoHostLDAP() as yldap: from yunohost.hook import hook_add, hook_exec
try:
app_list()
except YunoHostError:
raise YunoHostError(1, _("No app to upgrade"))
upgraded_apps = [] try:
app_list()
except MoulinetteError:
raise MoulinetteError(1, _("No app to upgrade"))
# If no app is specified, upgrade all apps upgraded_apps = []
if not app:
app = os.listdir(apps_setting_path)
elif not isinstance(app, list):
app = [ app ]
for app_id in app: # If no app is specified, upgrade all apps
installed = _is_installed(app_id) if not app:
if not installed: app = os.listdir(apps_setting_path)
raise YunoHostError(1, app_id + _(" is not installed")) elif not isinstance(app, list):
app = [ app ]
if app_id in upgraded_apps: for app_id in app:
continue installed = _is_installed(app_id)
if not installed:
raise MoulinetteError(1, _("%s is not installed") % app_id)
if '__' in app_id: if app_id in upgraded_apps:
original_app_id = app_id[:app_id.index('__')] continue
else:
original_app_id = app_id
current_app_dict = app_info(app_id, raw=True) if '__' in app_id:
new_app_dict = app_info(original_app_id, raw=True) original_app_id = app_id[:app_id.index('__')]
if file:
manifest = _extract_app_from_file(file)
elif url:
manifest = _fetch_app_from_git(url)
elif 'lastUpdate' not in new_app_dict or 'git' not in new_app_dict:
raise YunoHostError(22, app_id + _(" is a custom app, please provide an URL manually in order to upgrade it"))
elif (new_app_dict['lastUpdate'] > current_app_dict['lastUpdate']) \
or ('update_time' not in current_app_dict['settings'] \
and (new_app_dict['lastUpdate'] > current_app_dict['settings']['install_time'])) \
or ('update_time' in current_app_dict['settings'] \
and (new_app_dict['lastUpdate'] > current_app_dict['settings']['update_time'])):
manifest = _fetch_app_from_git(app_id)
else:
continue
# Check min version
if 'min_version' in manifest and __version__ < manifest['min_version']:
raise YunoHostError(1, _("%s requires a more recent version of the moulinette") % app_id)
app_setting_path = apps_setting_path +'/'+ app_id
if original_app_id != app_id:
# Replace original_app_id with the forked one in scripts
for file in os.listdir(app_tmp_folder +'/scripts'):
#TODO: add hooks directory to the list
#TODO: do it with sed ?
if file[:1] != '.':
with open(app_tmp_folder +'/scripts/'+ file, "r") as sources:
lines = sources.readlines()
with open(app_tmp_folder +'/scripts/'+ file, "w") as sources:
for line in lines:
sources.write(re.sub(r''+ original_app_id +'', app_id, line))
if 'hooks' in os.listdir(app_tmp_folder):
for file in os.listdir(app_tmp_folder +'/hooks'):
#TODO: do it with sed ?
if file[:1] != '.':
with open(app_tmp_folder +'/hooks/'+ file, "r") as sources:
lines = sources.readlines()
with open(app_tmp_folder +'/hooks/'+ file, "w") as sources:
for line in lines:
sources.write(re.sub(r''+ original_app_id +'', app_id, line))
# Add hooks
if 'hooks' in os.listdir(app_tmp_folder):
for file in os.listdir(app_tmp_folder +'/hooks'):
hook_add(app_id, app_tmp_folder +'/hooks/'+ file)
# Execute App upgrade script
os.system('chown -hR admin: '+ install_tmp)
if hook_exec(app_tmp_folder +'/scripts/upgrade') != 0:
#TODO: display fail messages from script
pass
else:
app_setting(app_id, 'update_time', int(time.time()))
# Replace scripts and manifest
os.system('rm -rf "'+ app_setting_path +'/scripts" "'+ app_setting_path +'/manifest.json"')
os.system('mv "'+ app_tmp_folder +'/manifest.json" "'+ app_tmp_folder +'/scripts" '+ app_setting_path)
# So much win
upgraded_apps.append(app_id)
win_msg(app_id + _(" upgraded successfully"))
if not upgraded_apps:
raise YunoHostError(1, _("No app to upgrade"))
win_msg(_("Upgrade complete"))
def app_install(app, label=None, args=None):
"""
Install apps
Keyword argument:
label
app -- App to install
args -- Serialize arguments of installation
"""
#TODO: Create tool for nginx (check path availability & stuff)
with YunoHostLDAP() as yldap:
# Fetch or extract sources
try: os.listdir(install_tmp)
except OSError: os.makedirs(install_tmp)
if app in app_list(raw=True) or ('@' in app) or ('http://' in app) or ('https://' in app):
manifest = _fetch_app_from_git(app)
else: else:
manifest = _extract_app_from_file(app) original_app_id = app_id
# Check ID current_app_dict = app_info(app_id, raw=True)
if 'id' not in manifest or '__' in manifest['id']: new_app_dict = app_info(original_app_id, raw=True)
raise YunoHostError(22, _("App id is invalid"))
app_id = manifest['id'] if file:
manifest = _extract_app_from_file(file)
elif url:
manifest = _fetch_app_from_git(url)
elif 'lastUpdate' not in new_app_dict or 'git' not in new_app_dict:
raise MoulinetteError(22, _("%s is a custom app, please provide an URL manually in order to upgrade it") % app_id)
elif (new_app_dict['lastUpdate'] > current_app_dict['lastUpdate']) \
or ('update_time' not in current_app_dict['settings'] \
and (new_app_dict['lastUpdate'] > current_app_dict['settings']['install_time'])) \
or ('update_time' in current_app_dict['settings'] \
and (new_app_dict['lastUpdate'] > current_app_dict['settings']['update_time'])):
manifest = _fetch_app_from_git(app_id)
else:
continue
# Check min version # Check min version
if 'min_version' in manifest and __version__ < manifest['min_version']: if 'min_version' in manifest and __version__ < manifest['min_version']:
raise YunoHostError(1, _("%s requires a more recent version of the moulinette") % app_id) raise MoulinetteError(1, _("%s requires a more recent version of the moulinette") % app_id)
# Check if app can be forked app_setting_path = apps_setting_path +'/'+ app_id
instance_number = _installed_instance_number(app_id, last=True) + 1
if instance_number > 1 :
if 'multi_instance' not in manifest or not is_true(manifest['multi_instance']):
raise YunoHostError(1, _("App is already installed"))
app_id_forked = app_id + '__' + str(instance_number) if original_app_id != app_id:
# Replace original_app_id with the forked one in scripts
# Replace app_id with the new one in scripts
for file in os.listdir(app_tmp_folder +'/scripts'): for file in os.listdir(app_tmp_folder +'/scripts'):
#TODO: do it with sed ? #TODO: do it with sed ?
if file[:1] != '.': if file[:1] != '.':
@ -419,7 +324,7 @@ def app_install(app, label=None, args=None):
lines = sources.readlines() lines = sources.readlines()
with open(app_tmp_folder +'/scripts/'+ file, "w") as sources: with open(app_tmp_folder +'/scripts/'+ file, "w") as sources:
for line in lines: for line in lines:
sources.write(re.sub(r''+ app_id +'', app_id_forked, line)) sources.write(re.sub(r''+ original_app_id +'', app_id, line))
if 'hooks' in os.listdir(app_tmp_folder): if 'hooks' in os.listdir(app_tmp_folder):
for file in os.listdir(app_tmp_folder +'/hooks'): for file in os.listdir(app_tmp_folder +'/hooks'):
@ -429,65 +334,151 @@ def app_install(app, label=None, args=None):
lines = sources.readlines() lines = sources.readlines()
with open(app_tmp_folder +'/hooks/'+ file, "w") as sources: with open(app_tmp_folder +'/hooks/'+ file, "w") as sources:
for line in lines: for line in lines:
sources.write(re.sub(r''+ app_id +'', app_id_forked, line)) sources.write(re.sub(r''+ original_app_id +'', app_id, line))
# Change app_id for the rest of the process
app_id = app_id_forked
# Prepare App settings
app_setting_path = apps_setting_path +'/'+ app_id
#TMP: Remove old settings
if os.path.exists(app_setting_path): shutil.rmtree(app_setting_path)
os.makedirs(app_setting_path)
os.system('touch '+ app_setting_path +'/settings.yml')
# Add hooks # Add hooks
if 'hooks' in os.listdir(app_tmp_folder): if 'hooks' in os.listdir(app_tmp_folder):
for file in os.listdir(app_tmp_folder +'/hooks'): for file in os.listdir(app_tmp_folder +'/hooks'):
hook_add(app_id, app_tmp_folder +'/hooks/'+ file) hook_add(app_id, app_tmp_folder +'/hooks/'+ file)
app_setting(app_id, 'id', app_id) # Execute App upgrade script
app_setting(app_id, 'install_time', int(time.time())) os.system('chown -hR admin: %s' % install_tmp)
if hook_exec(app_tmp_folder +'/scripts/upgrade') != 0:
if label: #TODO: display fail messages from script
app_setting(app_id, 'label', label) pass
else: else:
app_setting(app_id, 'label', manifest['name']) app_setting(app_id, 'update_time', int(time.time()))
os.system('chown -R admin: '+ app_tmp_folder) # Replace scripts and manifest
os.system('rm -rf "%s/scripts" "%s/manifest.json"' % (app_setting_path, app_setting_path))
os.system('mv "%s/manifest.json" "%s/scripts" %s' % (app_tmp_folder, app_tmp_folder, app_setting_path))
try: # So much win
if args is None: upgraded_apps.append(app_id)
args = '' msignals.display(_("%s upgraded successfully") % app_id, 'success')
args_dict = dict(urlparse.parse_qsl(args))
except:
args_dict = {}
# Execute App install script if not upgraded_apps:
os.system('chown -hR admin: '+ install_tmp) raise MoulinetteError(1, _("No app to upgrade"))
# Move scripts and manifest to the right place
os.system('cp '+ app_tmp_folder +'/manifest.json ' + app_setting_path) msignals.display(_("Upgrade complete"), 'success')
os.system('cp -R ' + app_tmp_folder +'/scripts '+ app_setting_path)
try:
if hook_exec(app_tmp_folder + '/scripts/install', args_dict) == 0: def app_install(auth, app, label=None, args=None):
shutil.rmtree(app_tmp_folder) """
os.system('chmod -R 400 '+ app_setting_path) Install apps
os.system('chown -R root: '+ app_setting_path)
os.system('chown -R admin: '+ app_setting_path +'/scripts') Keyword argument:
app_ssowatconf() label
win_msg(_("Installation complete")) app -- App to install
else: args -- Serialize arguments of installation
#TODO: display script fail messages
hook_remove(app_id) """
shutil.rmtree(app_setting_path) from yunohost.hook import hook_add, hook_remove, hook_exec
shutil.rmtree(app_tmp_folder)
raise YunoHostError(1, _("Installation failed")) # Fetch or extract sources
except KeyboardInterrupt, EOFError: try: os.listdir(install_tmp)
except OSError: os.makedirs(install_tmp)
if app in app_list(raw=True) or ('@' in app) or ('http://' in app) or ('https://' in app):
manifest = _fetch_app_from_git(app)
else:
manifest = _extract_app_from_file(app)
# Check ID
if 'id' not in manifest or '__' in manifest['id']:
raise MoulinetteError(22, _("App id is invalid"))
app_id = manifest['id']
# Check min version
if 'min_version' in manifest and __version__ < manifest['min_version']:
raise MoulinetteError(1, _("%s requires a more recent version of the moulinette") % app_id)
# Check if app can be forked
instance_number = _installed_instance_number(app_id, last=True) + 1
if instance_number > 1 :
if 'multi_instance' not in manifest or not is_true(manifest['multi_instance']):
raise MoulinetteError(1, _("App is already installed"))
app_id_forked = app_id + '__' + str(instance_number)
# Replace app_id with the new one in scripts
for file in os.listdir(app_tmp_folder +'/scripts'):
#TODO: do it with sed ?
if file[:1] != '.':
with open(app_tmp_folder +'/scripts/'+ file, "r") as sources:
lines = sources.readlines()
with open(app_tmp_folder +'/scripts/'+ file, "w") as sources:
for line in lines:
sources.write(re.sub(r''+ app_id +'', app_id_forked, line))
if 'hooks' in os.listdir(app_tmp_folder):
for file in os.listdir(app_tmp_folder +'/hooks'):
#TODO: do it with sed ?
if file[:1] != '.':
with open(app_tmp_folder +'/hooks/'+ file, "r") as sources:
lines = sources.readlines()
with open(app_tmp_folder +'/hooks/'+ file, "w") as sources:
for line in lines:
sources.write(re.sub(r''+ app_id +'', app_id_forked, line))
# Change app_id for the rest of the process
app_id = app_id_forked
# Prepare App settings
app_setting_path = apps_setting_path +'/'+ app_id
#TMP: Remove old settings
if os.path.exists(app_setting_path): shutil.rmtree(app_setting_path)
os.makedirs(app_setting_path)
os.system('touch %s/settings.yml' % app_setting_path)
# Add hooks
if 'hooks' in os.listdir(app_tmp_folder):
for file in os.listdir(app_tmp_folder +'/hooks'):
hook_add(app_id, app_tmp_folder +'/hooks/'+ file)
app_setting(app_id, 'id', app_id)
app_setting(app_id, 'install_time', int(time.time()))
if label:
app_setting(app_id, 'label', label)
else:
app_setting(app_id, 'label', manifest['name'])
os.system('chown -R admin: '+ app_tmp_folder)
try:
if args is None:
args = ''
args_dict = dict(urlparse.parse_qsl(args))
except:
args_dict = {}
# Execute App install script
os.system('chown -hR admin: %s' % install_tmp)
# Move scripts and manifest to the right place
os.system('cp %s/manifest.json %s' % (app_tmp_folder, app_setting_path))
os.system('cp -R %s/scripts %s' % (app_tmp_folder, app_setting_path))
try:
if hook_exec(app_tmp_folder + '/scripts/install', args_dict) == 0:
shutil.rmtree(app_tmp_folder)
os.system('chmod -R 400 %s' % app_setting_path)
os.system('chown -R root: %s' % app_setting_path)
os.system('chown -R admin: %s/scripts' % app_setting_path)
app_ssowatconf(auth)
msignals.display(_("Installation complete"), 'success')
else:
#TODO: display script fail messages
hook_remove(app_id) hook_remove(app_id)
shutil.rmtree(app_setting_path) shutil.rmtree(app_setting_path)
shutil.rmtree(app_tmp_folder) shutil.rmtree(app_tmp_folder)
raise YunoHostError(125, _("Interrupted")) raise MoulinetteError(1, _("Installation failed"))
except KeyboardInterrupt, EOFError:
hook_remove(app_id)
shutil.rmtree(app_setting_path)
shutil.rmtree(app_tmp_folder)
raise MoulinetteError(125, _("Interrupted"))
def app_remove(app): def app_remove(app):
@ -498,9 +489,10 @@ def app_remove(app):
app -- App(s) to delete app -- App(s) to delete
""" """
from yunohost.hook import hook_exec, hook_remove
if not _is_installed(app): if not _is_installed(app):
raise YunoHostError(22, _("App is not installed")) raise MoulinetteError(22, _("App is not installed"))
app_setting_path = apps_setting_path + app app_setting_path = apps_setting_path + app
@ -509,7 +501,7 @@ def app_remove(app):
shutil.rmtree('/tmp/yunohost_remove') shutil.rmtree('/tmp/yunohost_remove')
except: pass except: pass
os.system('cp -a '+ app_setting_path + ' /tmp/yunohost_remove && chown -hR admin: /tmp/yunohost_remove') os.system('cp -a %s /tmp/yunohost_remove && chown -hR admin: /tmp/yunohost_remove' % app_setting_path)
os.system('chown -R admin: /tmp/yunohost_remove') os.system('chown -R admin: /tmp/yunohost_remove')
os.system('chmod -R u+rX /tmp/yunohost_remove') os.system('chmod -R u+rX /tmp/yunohost_remove')
@ -520,10 +512,10 @@ def app_remove(app):
shutil.rmtree('/tmp/yunohost_remove') shutil.rmtree('/tmp/yunohost_remove')
hook_remove(app) hook_remove(app)
app_ssowatconf() app_ssowatconf()
win_msg(_("App removed: ")+ app) msignals.display(_("App removed: %s") % app, 'success')
def app_addaccess(apps, users): def app_addaccess(auth, apps, users):
""" """
Grant access right to users (everyone by default) Grant access right to users (everyone by default)
@ -532,9 +524,11 @@ def app_addaccess(apps, users):
apps apps
""" """
from yunohost.user import user_list, user_info
if not users: if not users:
users = [] users = []
for user in user_list(YunoHostLDAP())['users']: for user in user_list(auth)['users']:
users.append(user['username']) users.append(user['username'])
if not isinstance(users, list): users = [users] if not isinstance(users, list): users = [users]
@ -542,7 +536,7 @@ def app_addaccess(apps, users):
for app in apps: for app in apps:
if not _is_installed(app): if not _is_installed(app):
raise YunoHostError(22, _("App is not installed")) raise MoulinetteError(22, _("App is not installed"))
with open(apps_setting_path + app +'/settings.yml') as f: with open(apps_setting_path + app +'/settings.yml') as f:
app_settings = yaml.load(f) app_settings = yaml.load(f)
@ -560,8 +554,8 @@ def app_addaccess(apps, users):
for allowed_user in users: for allowed_user in users:
if allowed_user not in new_users.split(','): if allowed_user not in new_users.split(','):
try: try:
user_info(allowed_user) user_info(auth, allowed_user)
except YunoHostError: except MoulinetteError:
continue continue
if new_users == '': if new_users == '':
new_users = allowed_user new_users = allowed_user
@ -570,12 +564,12 @@ def app_addaccess(apps, users):
app_setting(app, 'allowed_users', new_users.strip()) app_setting(app, 'allowed_users', new_users.strip())
app_ssowatconf() app_ssowatconf(auth)
return { 'allowed_users': new_users.split(',') } return { 'allowed_users': new_users.split(',') }
def app_removeaccess(apps, users): def app_removeaccess(auth, apps, users):
""" """
Revoke access right to users (everyone by default) Revoke access right to users (everyone by default)
@ -584,6 +578,8 @@ def app_removeaccess(apps, users):
apps apps
""" """
from yunohost.user import user_list
remove_all = False remove_all = False
if not users: if not users:
remove_all = True remove_all = True
@ -593,7 +589,7 @@ def app_removeaccess(apps, users):
new_users = '' new_users = ''
if not _is_installed(app): if not _is_installed(app):
raise YunoHostError(22, _("App is not installed")) raise MoulinetteError(22, _("App is not installed"))
with open(apps_setting_path + app +'/settings.yml') as f: with open(apps_setting_path + app +'/settings.yml') as f:
app_settings = yaml.load(f) app_settings = yaml.load(f)
@ -610,7 +606,7 @@ def app_removeaccess(apps, users):
new_users = new_users +','+ allowed_user new_users = new_users +','+ allowed_user
else: else:
new_users='' new_users=''
for user in user_list(YunoHostLDAP())['users']: for user in user_list(auth)['users']:
if user['username'] not in users: if user['username'] not in users:
if new_users == '': if new_users == '':
new_users = user['username'] new_users = user['username']
@ -618,12 +614,12 @@ def app_removeaccess(apps, users):
app_setting(app, 'allowed_users', new_users.strip()) app_setting(app, 'allowed_users', new_users.strip())
app_ssowatconf() app_ssowatconf(auth)
return { 'allowed_users': new_users.split(',') } return { 'allowed_users': new_users.split(',') }
def app_clearaccess(apps): def app_clearaccess(auth, apps):
""" """
Reset access rights for the app Reset access rights for the app
@ -635,7 +631,7 @@ def app_clearaccess(apps):
for app in apps: for app in apps:
if not _is_installed(app): if not _is_installed(app):
raise YunoHostError(22, _("App is not installed")) raise MoulinetteError(22, _("App is not installed"))
with open(apps_setting_path + app +'/settings.yml') as f: with open(apps_setting_path + app +'/settings.yml') as f:
app_settings = yaml.load(f) app_settings = yaml.load(f)
@ -646,7 +642,7 @@ def app_clearaccess(apps):
if 'allowed_users' in app_settings: if 'allowed_users' in app_settings:
app_setting(app, 'allowed_users', delete=True) app_setting(app, 'allowed_users', delete=True)
app_ssowatconf() app_ssowatconf(auth)
def app_setting(app, key, value=None, delete=False): def app_setting(app, key, value=None, delete=False):
@ -739,13 +735,13 @@ def app_checkport(port):
s.connect(("localhost", int(port))) s.connect(("localhost", int(port)))
s.close() s.close()
except socket.error: except socket.error:
win_msg(_("Port available: ")+ str(port)) msignals.display(_("Port available: %s") % str(port), 'success')
else: else:
raise YunoHostError(22, _("Port not available")) raise MoulinetteError(22, _("Port not available: %s") % str(port))
def app_checkurl(url, app=None): def app_checkurl(auth, url, app=None):
""" """
Check availability of a web path Check availability of a web path
@ -754,6 +750,8 @@ def app_checkurl(url, app=None):
app -- Write domain & path to app settings for further checks app -- Write domain & path to app settings for further checks
""" """
from yunohost.domain import domain_list
if "https://" == url[:8]: if "https://" == url[:8]:
url = url[8:] url = url[8:]
elif "http://" == url[:7]: elif "http://" == url[:7]:
@ -771,15 +769,15 @@ def app_checkurl(url, app=None):
apps_map = app_map(raw=True) apps_map = app_map(raw=True)
validate(r'^([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])*)$', domain) validate(r'^([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])*)$', domain)
if domain not in domain_list(YunoHostLDAP())['domains']: if domain not in domain_list(auth)['domains']:
raise YunoHostError(22, _("Domain doesn't exists")) raise MoulinetteError(22, _("Domain doesn't exists"))
if domain in apps_map: if domain in apps_map:
if path in apps_map[domain]: if path in apps_map[domain]:
raise YunoHostError(1, _("An app is already installed on this location")) raise MoulinetteError(1, _("An app is already installed on this location"))
for app_path, v in apps_map[domain].items(): for app_path, v in apps_map[domain].items():
if app_path in path and app_path.count('/') < path.count('/'): if app_path in path and app_path.count('/') < path.count('/'):
raise YunoHostError(1, _("Unable to install app at this location")) raise MoulinetteError(1, _("Unable to install app at this location"))
if app is not None: if app is not None:
app_setting(app, 'domain', value=domain) app_setting(app, 'domain', value=domain)
@ -807,31 +805,33 @@ def app_initdb(user, password=None, db=None, sql=None):
print(password) print(password)
mysql_root_pwd = open('/etc/yunohost/mysql').read().rstrip() mysql_root_pwd = open('/etc/yunohost/mysql').read().rstrip()
mysql_command = 'mysql -u root -p'+ mysql_root_pwd +' -e "CREATE DATABASE '+ db +' ; GRANT ALL PRIVILEGES ON '+ db +'.* TO \''+ user +'\'@localhost IDENTIFIED BY \''+ password +'\';"' mysql_command = 'mysql -u root -p%s -e "CREATE DATABASE %s ; GRANT ALL PRIVILEGES ON %s.* TO \'%s\'@localhost IDENTIFIED BY \'%s\';"' % (mysql_root_pwd, db, db, user, password)
if os.system(mysql_command) != 0: if os.system(mysql_command) != 0:
raise YunoHostError(1, _("MySQL DB creation failed")) raise MoulinetteError(1, _("MySQL DB creation failed"))
if sql is not None: if sql is not None:
if os.system('mysql -u '+ user +' -p'+ password +' '+ db +' < '+ sql) != 0: if os.system('mysql -u %s -p%s %s < %s' % (user, password, db, sql)) != 0:
raise YunoHostError(1, _("MySQL DB init failed")) raise MoulinetteError(1, _("MySQL DB init failed"))
if not return_pwd: if not return_pwd:
win_msg(_("Database initiliazed")) msignals.display(_("Database initiliazed"), 'success')
def app_ssowatconf(): def app_ssowatconf(auth):
""" """
Regenerate SSOwat configuration file Regenerate SSOwat configuration file
""" """
from yunohost.domain import domain_list
from yunohost.user import user_list
with open('/etc/yunohost/current_host', 'r') as f: with open('/etc/yunohost/current_host', 'r') as f:
main_domain = f.readline().rstrip() main_domain = f.readline().rstrip()
domains = domain_list(YunoHostLDAP())['domains'] domains = domain_list(auth)['domains']
users = {} users = {}
for user in user_list(YunoHostLDAP())['users']: for user in user_list(auth)['users']:
users[user['username']] = app_map(user=user['username']) users[user['username']] = app_map(user=user['username'])
skipped_urls = [] skipped_urls = []
@ -904,7 +904,7 @@ def app_ssowatconf():
with open('/etc/ssowat/conf.json', 'wb') as f: with open('/etc/ssowat/conf.json', 'wb') as f:
json.dump(conf_dict, f) json.dump(conf_dict, f)
win_msg(_('SSOwat configuration generated')) msignals.display(_('SSOwat configuration generated'), 'success')
def _extract_app_from_file(path, remove=False): def _extract_app_from_file(path, remove=False):
@ -927,21 +927,21 @@ def _extract_app_from_file(path, remove=False):
os.makedirs(app_tmp_folder) os.makedirs(app_tmp_folder)
if ".zip" in path: if ".zip" in path:
extract_result = os.system('cd '+ os.getcwd() +' && unzip '+ path +' -d '+ app_tmp_folder +' > /dev/null 2>&1') extract_result = os.system('cd %s && unzip %s -d %s > /dev/null 2>&1' % (os.getcwd(), path, app_tmp_folder))
if remove: os.remove(path) if remove: os.remove(path)
elif ".tar" in path: elif ".tar" in path:
extract_result = os.system('cd '+ os.getcwd() +' && tar -xf '+ path +' -C '+ app_tmp_folder +' > /dev/null 2>&1') extract_result = os.system('cd %s && tar -xf %s -C %s > /dev/null 2>&1' % (os.getcwd(), path, app_tmp_folder))
if remove: os.remove(path) if remove: os.remove(path)
elif (path[:1] == '/' and os.path.exists(path)) or (os.system('cd '+ os.getcwd() +'/'+ path) == 0): elif (path[:1] == '/' and os.path.exists(path)) or (os.system('cd %s/%s' % (os.getcwd(), path)) == 0):
shutil.rmtree(app_tmp_folder) shutil.rmtree(app_tmp_folder)
if path[len(path)-1:] != '/': if path[len(path)-1:] != '/':
path = path + '/' path = path + '/'
extract_result = os.system('cd '+ os.getcwd() +' && cp -a "'+ path +'" '+ app_tmp_folder) extract_result = os.system('cd %s && cp -a "%s" %s' % (os.getcwd(), path, app_tmp_folder))
else: else:
extract_result = 1 extract_result = 1
if extract_result != 0: if extract_result != 0:
raise YunoHostError(22, _("Invalid install file")) raise MoulinetteError(22, _("Invalid install file"))
try: try:
if len(os.listdir(app_tmp_folder)) == 1: if len(os.listdir(app_tmp_folder)) == 1:
@ -951,7 +951,7 @@ def _extract_app_from_file(path, remove=False):
manifest = json.loads(str(json_manifest.read())) manifest = json.loads(str(json_manifest.read()))
manifest['lastUpdate'] = int(time.time()) manifest['lastUpdate'] = int(time.time())
except IOError: except IOError:
raise YunoHostError(1, _("Invalid App file")) raise MoulinetteError(1, _("Invalid App file"))
print(_('OK')) print(_('OK'))
@ -979,17 +979,17 @@ def _fetch_app_from_git(app):
if ".git" in url[-4:]: url = url[:-4] if ".git" in url[-4:]: url = url[:-4]
if "/" in url [-1:]: url = url[:-1] if "/" in url [-1:]: url = url[:-1]
url = url + "/archive/master.zip" url = url + "/archive/master.zip"
if os.system('wget "'+ url +'" -O "'+ app_tmp_folder +'.zip" > /dev/null 2>&1') == 0: if os.system('wget "%s" -O "%s.zip" > /dev/null 2>&1' % (url, app_tmp_folder)) == 0:
return _extract_app_from_file(app_tmp_folder +'.zip', remove=True) return _extract_app_from_file(app_tmp_folder +'.zip', remove=True)
git_result = os.system('git clone '+ app +' '+ app_tmp_folder) git_result = os.system('git clone %s %s' % (app, app_tmp_folder))
git_result_2 = 0 git_result_2 = 0
try: try:
with open(app_tmp_folder + '/manifest.json') as json_manifest: with open(app_tmp_folder + '/manifest.json') as json_manifest:
manifest = json.loads(str(json_manifest.read())) manifest = json.loads(str(json_manifest.read()))
manifest['lastUpdate'] = int(time.time()) manifest['lastUpdate'] = int(time.time())
except IOError: except IOError:
raise YunoHostError(1, _("Invalid App manifest")) raise MoulinetteError(1, _("Invalid App manifest"))
else: else:
app_dict = app_list(raw=True) app_dict = app_list(raw=True)
@ -999,24 +999,24 @@ def _fetch_app_from_git(app):
app_info['manifest']['lastUpdate'] = app_info['lastUpdate'] app_info['manifest']['lastUpdate'] = app_info['lastUpdate']
manifest = app_info['manifest'] manifest = app_info['manifest']
else: else:
raise YunoHostError(22, _("App doesn't exists")) raise MoulinetteError(22, _("App doesn't exists"))
if "github.com" in app_info['git']['url']: if "github.com" in app_info['git']['url']:
url = app_info['git']['url'].replace("git@github.com:", "https://github.com/") url = app_info['git']['url'].replace("git@github.com:", "https://github.com/")
if ".git" in url[-4:]: url = url[:-4] if ".git" in url[-4:]: url = url[:-4]
if "/" in url [-1:]: url = url[:-1] if "/" in url [-1:]: url = url[:-1]
url = url + "/archive/"+ str(app_info['git']['revision']) + ".zip" url = url + "/archive/"+ str(app_info['git']['revision']) + ".zip"
if os.system('wget "'+ url +'" -O "'+ app_tmp_folder +'.zip" > /dev/null 2>&1') == 0: if os.system('wget "%s" -O "%s.zip" > /dev/null 2>&1' % (url, app_tmp_folder)) == 0:
return _extract_app_from_file(app_tmp_folder +'.zip', remove=True) return _extract_app_from_file(app_tmp_folder +'.zip', remove=True)
app_tmp_folder = install_tmp +'/'+ app app_tmp_folder = install_tmp +'/'+ app
if os.path.exists(app_tmp_folder): shutil.rmtree(app_tmp_folder) if os.path.exists(app_tmp_folder): shutil.rmtree(app_tmp_folder)
git_result = os.system('git clone '+ app_info['git']['url'] +' -b '+ app_info['git']['branch'] +' '+ app_tmp_folder) git_result = os.system('git clone %s -b %s %s' % (app_info['git']['url'], app_info['git']['branch'], app_tmp_folder))
git_result_2 = os.system('cd '+ app_tmp_folder +' && git reset --hard '+ str(app_info['git']['revision'])) git_result_2 = os.system('cd %s && git reset --hard %s' % (app_tmp_folder, str(app_info['git']['revision'])))
if not git_result == git_result_2 == 0: if not git_result == git_result_2 == 0:
raise YunoHostError(22, _("Sources fetching failed")) raise MoulinetteError(22, _("Sources fetching failed"))
print(_('OK')) print(_('OK'))