MYSQL and dependencies in app_install()

This commit is contained in:
Kload 2013-02-25 19:54:23 +01:00
parent 0dea51f5cb
commit 7ed0094a14
2 changed files with 74 additions and 49 deletions

View file

@ -12,9 +12,14 @@ import ldap.modlist as modlist
import json import json
import re import re
import getpass import getpass
import random
import string
if not __debug__: if not __debug__:
import traceback import traceback
def random_password(length=8):
char_set = string.ascii_uppercase + string.digits + string.ascii_lowercase
return ''.join(random.sample(char_set,length))
def colorize(astr, color): def colorize(astr, color):
""" """

View file

@ -3,8 +3,9 @@
import os import os
import sys import sys
import json import json
import shutil
from urllib import urlopen, urlretrieve from urllib import urlopen, urlretrieve
from yunohost import YunoHostError, YunoHostLDAP, win_msg from yunohost import YunoHostError, YunoHostLDAP, win_msg, random_password
from yunohost_domain import domain_list, domain_add from yunohost_domain import domain_list, domain_add
def app_fetchlist(url=None, name=None): def app_fetchlist(url=None, name=None):
@ -53,7 +54,6 @@ def app_list(offset=None, limit=None, filter=None, raw=False):
""" """
# TODO: List installed applications # TODO: List installed applications
# TODO: Implement fields to fetch
if offset: offset = int(offset) if offset: offset = int(offset)
else: offset = 0 else: offset = 0
@ -113,72 +113,92 @@ def app_install(app, domain, path='/', label=None, public=False, protected=True)
# Fetch | Extract sources # Fetch | Extract sources
install_tmp = '/tmp/yunohost/install' with YunoHostLDAP() as yldap:
try: os.listdir(install_tmp) install_tmp = '/tmp/yunohost/install'
except OSError: os.makedirs(install_tmp) try: os.listdir(install_tmp)
except OSError: os.makedirs(install_tmp)
if "." in app: # Install from file
install_from_file = True if "." in app:
app_tmp_folder = install_tmp + '/from_file' install_from_file = True
os.makedirs(app_tmp_folder) app_tmp_folder = install_tmp + '/from_file'
if ".zip" in app: if os.path.exists(app_tmp_folder): shutil.rmtree(app_tmp_folder)
extract_result = os.system('cd '+ os.getcwd() +' && unzip '+ app +' -d '+ app_tmp_folder) os.makedirs(app_tmp_folder)
elif ".tar" in app: if ".zip" in app:
extract_result = os.system('cd '+ os.getcwd() +' && tar -C '+ app_tmp_folder +' -xf '+ 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
else: else:
extract_result = 1 install_from_file = False
app_tmp_folder = install_tmp +'/'+ app
if os.path.exists(app_tmp_folder): shutil.rmtree(app_tmp_folder)
if extract_result != 0: app_dict = app_list(raw=True)
raise YunoHostError(22, _("Invalid install file"))
with open(app_tmp_folder + '/manifest.webapp') as json_manifest: if app in app_dict:
manifest = json.loads(str(json_manifest.read())) app_info = app_dict[app]
manifest = app_info['manifest']
else:
raise YunoHostError(22, _("App doesn't exists"))
else: git_result = os.system('git clone '+ app_info['git']['url'] +' -b '+ app_info['git']['branch'] +' '+ app_tmp_folder)
install_from_file = False git_result_2 = os.system('cd '+ app_tmp_folder +' && git reset --hard '+ str(app_info['git']['revision']))
app_tmp_folder = install_tmp +'/'+ app
app_dict = app_list(raw=True) if not git_result == git_result_2 == 0:
raise YunoHostError(22, _("Sources fetching failed"))
if app in app_dict: # TODO: Check if exists another instance
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) # Handle domain if ain't already created
git_result_2 = os.system('cd '+ app_tmp_folder +' && git reset --hard '+ str(app_info['git']['revision'])) try:
domain_list(filter="virtualdomain="+ domain)
except YunoHostError:
domain_add([domain])
if not git_result == git_result_2 == 0: if ('debian' in manifest['dependencies']) and (len(manifest['dependencies']['debian']) > 0):
raise YunoHostError(22, _("Sources fetching failed")) #os.system('apt-get update')
if os.system('apt-get install "'+ '" "'.join(manifest['dependencies']['debian']) +'"') != 0:
raise YunoHostError(1, _("Dependency installation failed: ") + dependency)
# TODO: Check if exists another instance # TODO: Install npm, pip, gem and pear dependencies
try: if 'webapp' in manifest['yunohost']:
domain_list(filter="virtualdomain="+ domain) if 'db' in manifest['yunohost']['webapp']:
except YunoHostError: db_user = manifest['yunohost']['uid'] # TODO: app.instance
domain_add([domain]) db_user_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: Install dependencies # TODO: PgSQL/MongoDB ?
for dependency in manifest['dependencies']['debian']: # TODO: Copy files to the right place
print dependency
# TODO: Exec install script # TODO: Exec postinstall script
# TODO: Check if MYSQL DB is needed and create it, then init DB if needed # TODO: Create appsettings and chmod it
# TODO: Copy files to the right place # TODO: Configure apache/lemon with NPZE's scripts
# TODO: Exec postinstall script # TODO: Remove scripts folder
# TODO: Create appsettings
# TODO: Configure apache/lemon with NPZE's scripts
# TODO: Remove scripts folder