2012-11-17 16:29:06 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
from urllib import urlopen, urlretrieve
|
2013-02-10 21:04:15 +01:00
|
|
|
from yunohost import YunoHostError, YunoHostLDAP, win_msg
|
2013-02-12 13:52:11 +01:00
|
|
|
from yunohost_domain import domain_list, domain_add
|
2012-11-17 16:29:06 +01:00
|
|
|
|
2012-11-29 14:48:58 +01:00
|
|
|
def app_updatelist(url=None):
|
2012-11-17 16:29:06 +01:00
|
|
|
"""
|
|
|
|
Fetch application list
|
|
|
|
|
|
|
|
Keyword arguments:
|
2012-11-29 14:48:58 +01:00
|
|
|
url -- Custom list URL
|
2012-11-17 16:29:06 +01:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
True | YunoHostError
|
|
|
|
|
|
|
|
"""
|
|
|
|
app_path = '/var/cache/yunohost/apps/'
|
|
|
|
|
|
|
|
# Create app path if not exists
|
|
|
|
try: os.listdir(app_path)
|
|
|
|
except OSError: os.makedirs(app_path)
|
|
|
|
|
2013-02-10 21:04:15 +01:00
|
|
|
if not url: url = 'http://fapp.yunohost.org/app/list/raw'
|
2012-11-17 16:29:06 +01:00
|
|
|
|
2013-02-12 13:52:11 +01:00
|
|
|
# TODO: Add multiple list support
|
|
|
|
|
2012-11-17 16:29:06 +01:00
|
|
|
# Get list
|
2013-02-10 21:04:15 +01:00
|
|
|
try: fetch = urlopen(url)
|
|
|
|
except IOError: fetch = False
|
2012-11-17 16:29:06 +01:00
|
|
|
finally:
|
2013-02-10 21:04:15 +01:00
|
|
|
if fetch and (fetch.code == 200): urlretrieve(url, app_path + 'list.json')
|
2012-11-17 16:29:06 +01:00
|
|
|
else: raise YunoHostError(1, _("List server connection failed"))
|
|
|
|
|
2013-02-12 13:52:11 +01:00
|
|
|
# TODO: Use system wget in order to have a status bar
|
|
|
|
|
2013-02-10 21:04:15 +01:00
|
|
|
win_msg(_("List updated successfully"))
|
2012-11-17 16:29:06 +01:00
|
|
|
|
|
|
|
|
2013-02-10 22:34:14 +01:00
|
|
|
def app_list(offset=None, limit=None):
|
|
|
|
"""
|
|
|
|
List available applications
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
offset -- App to begin with
|
|
|
|
limit -- Number of apps to list
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Dict of apps
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
# TODO: List installed applications
|
|
|
|
# TODO: Implement fields to fetch
|
|
|
|
|
|
|
|
if offset: offset = int(offset)
|
|
|
|
else: offset = 0
|
|
|
|
if limit: limit = int(limit)
|
|
|
|
else: limit = 1000
|
2013-02-10 21:04:15 +01:00
|
|
|
with open('/var/cache/yunohost/apps/list.json') as json_list:
|
|
|
|
app_dict = json.loads(str(json_list.read()))
|
|
|
|
|
|
|
|
list_dict = {}
|
|
|
|
|
2013-02-10 22:34:14 +01:00
|
|
|
if len(app_dict) > (0 + offset) and limit > 0:
|
|
|
|
i = 0 + offset
|
|
|
|
sorted_app_dict = {}
|
|
|
|
for sorted_keys in sorted(app_dict.keys())[i:]:
|
|
|
|
if i <= limit:
|
|
|
|
sorted_app_dict[sorted_keys] = app_dict[sorted_keys]
|
|
|
|
i += 1
|
|
|
|
for app_id, app_info in sorted_app_dict.items():
|
|
|
|
list_dict[app_id] = {
|
|
|
|
'Name': app_info['manifest']['name'],
|
|
|
|
'Version': app_info['manifest']['version'],
|
|
|
|
'Description': app_info['manifest']['description']
|
|
|
|
}
|
2013-02-10 21:04:15 +01:00
|
|
|
|
|
|
|
return list_dict
|
2013-02-11 11:14:39 +01:00
|
|
|
|
|
|
|
def app_install(app, domain=None, path=None, label=None, public=False, protected=True):
|
|
|
|
"""
|
|
|
|
Install selected app
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
app -- AppID to install (or filename)
|
|
|
|
domain -- Web domain for the app
|
|
|
|
path -- Subpath of the domain
|
|
|
|
label -- User defined name for the app
|
|
|
|
public -- Allow app public access
|
|
|
|
protected -- App is protected by the SSO
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Win | Fail
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
# TODO: Check if the app is already installed
|
|
|
|
|
2013-02-12 13:52:11 +01:00
|
|
|
# Fetch | Extract sources
|
|
|
|
|
2013-02-11 13:45:58 +01:00
|
|
|
install_tmp = '/tmp/yunohost/install'
|
|
|
|
try: os.listdir(install_tmp)
|
|
|
|
except OSError: os.makedirs(install_tmp)
|
|
|
|
|
|
|
|
|
|
|
|
if "." in app:
|
|
|
|
install_from_file = True
|
|
|
|
app_tmp_folder = install_tmp + '/from_file'
|
|
|
|
os.makedirs(app_tmp_folder)
|
|
|
|
if ".zip" in app:
|
2013-02-11 13:54:36 +01:00
|
|
|
extract_result = os.system('cd '+ os.getcwd() +' && unzip '+ app +' -d '+ app_tmp_folder)
|
2013-02-11 13:45:58 +01:00
|
|
|
elif ".tar" in app:
|
2013-02-11 13:54:36 +01:00
|
|
|
extract_result = os.system('cd '+ os.getcwd() +' && tar -C '+ app_tmp_folder +' -xf '+ app)
|
|
|
|
else:
|
|
|
|
extract_result = 1
|
|
|
|
|
|
|
|
if extract_result != 0:
|
|
|
|
raise YunoHostError(22, _("Invalid install file"))
|
2013-02-11 13:45:58 +01:00
|
|
|
|
2013-02-12 13:52:11 +01:00
|
|
|
with open(app_tmp_folder + '/manifest.webapp') as json_manifest:
|
|
|
|
manifest = json.loads(str(json_manifest.read()))
|
|
|
|
|
2013-02-11 13:45:58 +01:00
|
|
|
else:
|
|
|
|
install_from_file = False
|
|
|
|
app_tmp_folder = install_tmp +'/'+ app
|
|
|
|
with open('/var/cache/yunohost/apps/list.json') as json_list:
|
|
|
|
app_dict = json.loads(str(json_list.read()))
|
|
|
|
|
|
|
|
if app in app_dict:
|
|
|
|
app_info = app_dict[app]
|
2013-02-12 13:52:11 +01:00
|
|
|
manifest = app_info['manifest']
|
2013-02-11 13:45:58 +01:00
|
|
|
else:
|
|
|
|
raise YunoHostError(22, _("App doesn't exists"))
|
|
|
|
|
2013-02-11 13:54:36 +01:00
|
|
|
git_result = os.system('git clone '+ app_info['git']['url'] +' -b '+ app_info['git']['branch'] +' '+ app_tmp_folder)
|
|
|
|
git_result_2 = os.system('cd '+ app_tmp_folder +' && git reset --hard '+ str(app_info['git']['revision']))
|
2013-02-11 13:45:58 +01:00
|
|
|
|
2013-02-11 13:54:36 +01:00
|
|
|
if not git_result == git_result_2 == 0:
|
2013-02-11 13:45:58 +01:00
|
|
|
raise YunoHostError(22, _("Sources fetching failed"))
|
|
|
|
|
2013-02-11 11:14:39 +01:00
|
|
|
# TODO: Check if exists another instance
|
2013-02-12 13:52:11 +01:00
|
|
|
|
2013-02-11 11:14:39 +01:00
|
|
|
# TODO: Create domain
|
|
|
|
|
2013-02-12 13:52:11 +01:00
|
|
|
try:
|
|
|
|
domain_list(filter="virtualdomain="+ domain)
|
|
|
|
except YunoHostError:
|
2013-02-15 18:53:52 +01:00
|
|
|
domain_add([domain])
|
2013-02-12 13:52:11 +01:00
|
|
|
|
|
|
|
|
2013-02-11 11:14:39 +01:00
|
|
|
# TODO: Install dependencies
|
|
|
|
|
|
|
|
# TODO: Exec install script
|
|
|
|
|
|
|
|
# TODO: Check if MYSQL DB is needed and create it, then init DB if needed
|
|
|
|
|
|
|
|
# TODO: Copy files to the right place
|
|
|
|
|
|
|
|
# TODO: Exec postinstall script
|
|
|
|
|
|
|
|
# TODO: Create appsettings
|
|
|
|
|
|
|
|
# TODO: Configure apache/lemon with NPZE's scripts
|
|
|
|
|
2013-02-11 13:45:58 +01:00
|
|
|
# TODO: Move scripts folder
|
2013-02-11 11:14:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
|