mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
Refactoring
This commit is contained in:
parent
7ed0094a14
commit
8d4517d1c6
1 changed files with 114 additions and 66 deletions
180
yunohost_app.py
180
yunohost_app.py
|
@ -8,6 +8,10 @@ from urllib import urlopen, urlretrieve
|
|||
from yunohost import YunoHostError, YunoHostLDAP, win_msg, random_password
|
||||
from yunohost_domain import domain_list, domain_add
|
||||
|
||||
repo_path = '/var/cache/yunohost/repo'
|
||||
install_tmp = '/tmp/yunohost/install'
|
||||
app_tmp_folder = install_tmp + '/from_file'
|
||||
|
||||
def app_fetchlist(url=None, name=None):
|
||||
"""
|
||||
Fetch application list
|
||||
|
@ -20,8 +24,6 @@ def app_fetchlist(url=None, name=None):
|
|||
True | YunoHostError
|
||||
|
||||
"""
|
||||
repo_path = '/var/cache/yunohost/repo/'
|
||||
|
||||
# Create app path if not exists
|
||||
try: os.listdir(repo_path)
|
||||
except OSError: os.makedirs(repo_path)
|
||||
|
@ -32,12 +34,16 @@ def app_fetchlist(url=None, name=None):
|
|||
else:
|
||||
if not name: raise YunoHostError(22, _("You must indicate a name for your custom list"))
|
||||
|
||||
if os.system('wget "'+ url +'" -O "'+ repo_path + name +'.json"') != 0:
|
||||
if os.system('wget "'+ url +'" -O "'+ repo_path +'/'+ name +'.json"') != 0:
|
||||
raise YunoHostError(1, _("List server connection failed"))
|
||||
|
||||
win_msg(_("List successfully fetched"))
|
||||
|
||||
|
||||
def app_listlist():
|
||||
pass
|
||||
|
||||
|
||||
def app_list(offset=None, limit=None, filter=None, raw=False):
|
||||
"""
|
||||
List available applications
|
||||
|
@ -52,7 +58,6 @@ def app_list(offset=None, limit=None, filter=None, raw=False):
|
|||
Dict of apps
|
||||
|
||||
"""
|
||||
|
||||
# TODO: List installed applications
|
||||
|
||||
if offset: offset = int(offset)
|
||||
|
@ -60,7 +65,6 @@ def app_list(offset=None, limit=None, filter=None, raw=False):
|
|||
if limit: limit = int(limit)
|
||||
else: limit = 1000
|
||||
|
||||
repo_path = '/var/cache/yunohost/repo/'
|
||||
applists = os.listdir(repo_path)
|
||||
app_dict = {}
|
||||
list_dict = {}
|
||||
|
@ -69,7 +73,7 @@ def app_list(offset=None, limit=None, filter=None, raw=False):
|
|||
|
||||
for applist in applists:
|
||||
if '.json' in applist:
|
||||
with open(repo_path + applist) as json_list:
|
||||
with open(repo_path +'/'+ applist) as json_list:
|
||||
app_dict.update(json.loads(str(json_list.read())))
|
||||
|
||||
if len(app_dict) > (0 + offset) and limit > 0:
|
||||
|
@ -108,55 +112,17 @@ def app_install(app, domain, path='/', label=None, public=False, protected=True)
|
|||
Win | Fail
|
||||
|
||||
"""
|
||||
|
||||
# TODO: Check if the app is already installed
|
||||
|
||||
# Fetch | Extract sources
|
||||
|
||||
with YunoHostLDAP() as yldap:
|
||||
install_tmp = '/tmp/yunohost/install'
|
||||
try: os.listdir(install_tmp)
|
||||
except OSError: os.makedirs(install_tmp)
|
||||
|
||||
|
||||
# Install from file
|
||||
# Check if install from file or git
|
||||
if "." in app:
|
||||
install_from_file = True
|
||||
app_tmp_folder = install_tmp + '/from_file'
|
||||
if os.path.exists(app_tmp_folder): shutil.rmtree(app_tmp_folder)
|
||||
os.makedirs(app_tmp_folder)
|
||||
if ".zip" in app:
|
||||
extract_result = os.system('cd '+ os.getcwd() +' && unzip '+ app +' -d '+ app_tmp_folder)
|
||||
elif ".tar" in app:
|
||||
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"))
|
||||
|
||||
with open(app_tmp_folder + '/manifest.webapp') as json_manifest:
|
||||
manifest = json.loads(str(json_manifest.read()))
|
||||
|
||||
# Install from git
|
||||
manifest = extract_app_tarball(app)
|
||||
else:
|
||||
install_from_file = False
|
||||
app_tmp_folder = install_tmp +'/'+ app
|
||||
if os.path.exists(app_tmp_folder): shutil.rmtree(app_tmp_folder)
|
||||
|
||||
app_dict = app_list(raw=True)
|
||||
|
||||
if app in app_dict:
|
||||
app_info = app_dict[app]
|
||||
manifest = app_info['manifest']
|
||||
else:
|
||||
raise YunoHostError(22, _("App doesn't exists"))
|
||||
|
||||
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']))
|
||||
|
||||
if not git_result == git_result_2 == 0:
|
||||
raise YunoHostError(22, _("Sources fetching failed"))
|
||||
manifest = fetch_app_from_git(app)
|
||||
|
||||
# TODO: Check if exists another instance
|
||||
|
||||
|
@ -166,29 +132,14 @@ def app_install(app, domain, path='/', label=None, public=False, protected=True)
|
|||
except YunoHostError:
|
||||
domain_add([domain])
|
||||
|
||||
if ('debian' in manifest['dependencies']) and (len(manifest['dependencies']['debian']) > 0):
|
||||
#os.system('apt-get update')
|
||||
if os.system('apt-get install "'+ '" "'.join(manifest['dependencies']['debian']) +'"') != 0:
|
||||
raise YunoHostError(1, _("Dependency installation failed: ") + dependency)
|
||||
|
||||
# TODO: Install npm, pip, gem and pear dependencies
|
||||
if 'dependencies' in manifest: install_app_dependencies(manifest['dependencies'])
|
||||
|
||||
if 'webapp' in manifest['yunohost']:
|
||||
if 'db' in manifest['yunohost']['webapp']:
|
||||
db_user = manifest['yunohost']['uid'] # TODO: app.instance
|
||||
db_user_pwd = random_password()
|
||||
db_user = manifest['yunohost']['uid'] # TODO: app.instance
|
||||
db_pwd = random_password()
|
||||
|
||||
# Need MySQL DB ?
|
||||
if 'has_mysql_db' in manifest['yunohost']['webapp']['db'] and ((manifest['yunohost']['webapp']['db']['has_mysql_db'] == 'true') or (manifest['yunohost']['webapp']['db']['has_mysql_db'] == 'yes')):
|
||||
mysql_root_pwd = open('/etc/yunohost/mysql', 'rb').read().rstrip()
|
||||
mysql_command = 'mysql -u root -p'+ mysql_root_pwd +' -e "CREATE DATABASE '+ db_user +' ; GRANT ALL PRIVILEGES ON '+ db_user +'.* TO \''+ db_user +'\'@localhost IDENTIFIED BY \''+ db_user_pwd +'\';"'
|
||||
if os.system(mysql_command) != 0:
|
||||
raise YunoHostError(1, _("MySQL DB creation failed"))
|
||||
if 'mysql_init_script' in manifest['yunohost']['webapp']['db']:
|
||||
if os.system('mysql -u '+ db_user +' -p'+ db_user_pwd +' '+ db_user +' < '+ app_tmp_folder + manifest['yunohost']['webapp']['db']['mysql_init_script'] +' ;') != 0:
|
||||
raise YunoHostError(1, _("MySQL DB init failed"))
|
||||
|
||||
# TODO: PgSQL/MongoDB ?
|
||||
init_app_db(db_user, db_pwd, manifest['yunohost']['webapp']['db'])
|
||||
|
||||
# TODO: Copy files to the right place
|
||||
|
||||
|
@ -201,4 +152,101 @@ def app_install(app, domain, path='/', label=None, public=False, protected=True)
|
|||
# TODO: Remove scripts folder
|
||||
|
||||
|
||||
def extract_app_tarball(app):
|
||||
"""
|
||||
Unzip or untar application tarball in app_tmp_folder
|
||||
|
||||
Keyword arguments:
|
||||
app -- Path of the tarball
|
||||
|
||||
Returns:
|
||||
Dict manifest
|
||||
|
||||
"""
|
||||
if os.path.exists(app_tmp_folder): shutil.rmtree(app_tmp_folder)
|
||||
os.makedirs(app_tmp_folder)
|
||||
if ".zip" in app:
|
||||
extract_result = os.system('cd '+ os.getcwd() +' && unzip '+ app +' -d '+ app_tmp_folder)
|
||||
elif ".tar" in app:
|
||||
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"))
|
||||
|
||||
with open(app_tmp_folder + '/manifest.webapp') as json_manifest:
|
||||
manifest = json.loads(str(json_manifest.read()))
|
||||
|
||||
return manifest
|
||||
|
||||
|
||||
def fetch_app_from_git(app):
|
||||
"""
|
||||
Unzip or untar application tarball in app_tmp_folder
|
||||
|
||||
Keyword arguments:
|
||||
app -- Path of the tarball
|
||||
|
||||
Returns:
|
||||
Dict manifest
|
||||
|
||||
"""
|
||||
global app_tmp_folder
|
||||
|
||||
app_tmp_folder = install_tmp +'/'+ app
|
||||
if os.path.exists(app_tmp_folder): shutil.rmtree(app_tmp_folder)
|
||||
|
||||
app_dict = app_list(raw=True)
|
||||
|
||||
if app in app_dict:
|
||||
app_info = app_dict[app]
|
||||
else:
|
||||
raise YunoHostError(22, _("App doesn't exists"))
|
||||
|
||||
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']))
|
||||
|
||||
if not git_result == git_result_2 == 0:
|
||||
raise YunoHostError(22, _("Sources fetching failed"))
|
||||
|
||||
return app_info['manifest']
|
||||
|
||||
|
||||
def install_app_dependencies(dep_dict):
|
||||
"""
|
||||
Install debian, npm, gem, pip and pear dependencies of the app
|
||||
|
||||
Keyword arguments:
|
||||
dep_dict -- Dict of dependencies from the manifest
|
||||
|
||||
"""
|
||||
if ('debian' in dep_dict) and (len(dep_dict['debian']) > 0):
|
||||
#os.system('apt-get update')
|
||||
if os.system('apt-get install "'+ '" "'.join(dep_dict['debian']) +'"') != 0:
|
||||
raise YunoHostError(1, _("Dependency installation failed: ") + dependency)
|
||||
|
||||
# TODO: Install npm, pip, gem and pear dependencies
|
||||
|
||||
|
||||
def init_app_db(db_user, db_pwd, db_dict):
|
||||
"""
|
||||
Create database and initialize it with optionnal attached script
|
||||
|
||||
Keyword arguments:
|
||||
db_user -- Name of the DB user (also used as database name)
|
||||
db_pwd -- Password for the user
|
||||
db_dict -- Dict of DB parameters from the manifest
|
||||
|
||||
"""
|
||||
# Need MySQL DB ?
|
||||
if 'has_mysql_db' in db_dict and ((db_dict['has_mysql_db'] == 'true') or (db_dict['has_mysql_db'] == 'yes')):
|
||||
mysql_root_pwd = open('/etc/yunohost/mysql', 'rb').read().rstrip()
|
||||
mysql_command = 'mysql -u root -p'+ mysql_root_pwd +' -e "CREATE DATABASE '+ db_user +' ; GRANT ALL PRIVILEGES ON '+ db_user +'.* TO \''+ db_user +'\'@localhost IDENTIFIED BY \''+ db_pwd +'\';"'
|
||||
if os.system(mysql_command) != 0:
|
||||
raise YunoHostError(1, _("MySQL DB creation failed"))
|
||||
if 'mysql_init_script' in db_dict:
|
||||
if os.system('mysql -u '+ db_user +' -p'+ db_pwd +' '+ db_user +' < '+ app_tmp_folder + db_dict['mysql_init_script'] +' ;') != 0:
|
||||
raise YunoHostError(1, _("MySQL DB init failed"))
|
||||
|
||||
# TODO: PgSQL/MongoDB ?
|
||||
|
|
Loading…
Add table
Reference in a new issue