diff --git a/PDMI/__init__.py b/PDMI/__init__.py index 8a39895..998d5f7 100644 --- a/PDMI/__init__.py +++ b/PDMI/__init__.py @@ -1,3 +1,4 @@ +#!/bin/python import mimetypes from flask import Flask, request, session diff --git a/PDMI/blueprints/api/download/__init__.py b/PDMI/blueprints/api/download/__init__.py index b641f56..0746eca 100644 --- a/PDMI/blueprints/api/download/__init__.py +++ b/PDMI/blueprints/api/download/__init__.py @@ -16,26 +16,26 @@ class ApiIndex(Resource): class ApiModuleList(Resource): def get(self): - modules = Module.query.filter().all() + modules = Modules.query.filter().all() return {module.name: [version.version for version in module.versions] for module in modules} class ApiModuleVersions(Resource): def get(self, module): - module = Module.query.filter_by(name=module).first_or_404() + module = Modules.query.filter_by(name=module).first_or_404() return {"versions": [version.version for version in module.versions]} class ApiModuleGet(Resource): def get(self, module, version): - module = Module.query.filter_by(name=module).first_or_404() + module = Modules.query.filter_by(name=module).first_or_404() versions = [version.version for version in module.versions] if version not in versions: abort(404) print(os.getcwd()) - if not os.path.exists(os.path.join("modules", module.name, version + ".zip")): - shutil.make_archive(os.path.join("modules", module.name, version), 'zip', - os.path.join("modules", module.name, version)) + if not os.path.exists(os.path.join("PDMI", "modules", module.name, version + ".zip")): + shutil.make_archive(os.path.join("PDMI", "modules", module.name, version), 'zip', + os.path.join("PDMI", "modules", module.name, version)) return send_from_directory( os.path.join("modules", module.name, ), version + ".zip" diff --git a/PDMI/config.py b/PDMI/config.py index d61dcd8..d6ab7e6 100644 --- a/PDMI/config.py +++ b/PDMI/config.py @@ -10,7 +10,9 @@ DEBUG = True # TODO # ====================================================================================================================== # =================================================== sqlalchemy ======================================================= # ====================================================================================================================== -SQLALCHEMY_DATABASE_URI = os.environ["DATABASE_URI"] +SQLALCHEMY_DATABASE_URI = "mysql://"+os.environ["DATABASE_USER"]+":"+os.environ["DATABASE_PASSWORD"]+\ + "@"+os.environ["DATABASE_HOST"]+":"+os.environ["DATABASE_PORT"]+"/"\ + +os.environ["DATABASE_NAME"] SQLALCHEMY_TRACK_MODIFICATIONS = False # ====================================================================================================================== diff --git a/PDMI/modules/panic/0.0.1.zip b/PDMI/modules/panic/0.0.1.zip new file mode 100644 index 0000000..91b0a2b Binary files /dev/null and b/PDMI/modules/panic/0.0.1.zip differ diff --git a/PDMI/modules/panic/0.0.1/infos.json b/PDMI/modules/panic/0.0.1/infos.json deleted file mode 100644 index fc2742e..0000000 --- a/PDMI/modules/panic/0.0.1/infos.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "name": "panic", - "version": "0.0.1", - "requirement": { - "base": { - "min": "0.0.1", - "max": "0.0.1" - } - } -} \ No newline at end of file diff --git a/PDMI/modules/panic/0.0.1/version.json b/PDMI/modules/panic/0.0.1/version.json new file mode 100644 index 0000000..acfacc8 --- /dev/null +++ b/PDMI/modules/panic/0.0.1/version.json @@ -0,0 +1,13 @@ +{ + "version": "0.1.0", + "dependencies": { + "base": { + "min": "0.1.0", + "max": "0.1.0" + } + }, + "bot_version": { + "min": "0.1.0", + "max": "0.1.0" + } +} \ No newline at end of file diff --git a/PDMI/modules/panic/0.0.2.zip b/PDMI/modules/panic/0.0.2.zip deleted file mode 100644 index add9d2b..0000000 Binary files a/PDMI/modules/panic/0.0.2.zip and /dev/null differ diff --git a/Pipfile b/Pipfile index 3e08554..fb325be 100644 --- a/Pipfile +++ b/Pipfile @@ -8,18 +8,17 @@ name = "pypi" [packages] flask-admin = {git = "https://github.com/Fomys/flask-admin"} -Flask = "*" -Flask-BabelEx = "*" -Flask-Security = "*" -Flask-Migrate = "*" -Flask-Script = "*" -Flask-Uploads = "*" -SQLAlchemy = "*" -Flask-SQLAlchemy = "*" -Flask-RESTful = "*" -PyMySQL = "*" -mysqlclient = "*" +flask = "*" +flask-babelex = "*" +flask-security = "*" +flask-migrate = "*" +flask-script = "*" +flask-uploads = "*" +sqlalchemy = "*" +flask-sqlalchemy = "*" +flask-restful = "*" gunicorn = "*" +aiohttp = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index 6149b9f..cf1a0cf 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "0c2d1d15089362ca266cf49ebcdd8ec88aec4ea44b0f26d917c7aa38c64d88c3" + "sha256": "ea0d8bb7db8b76ca580aaa467b0d8986afa79a1c1fb3424f8a810489a603c602" }, "host-environment-markers": { "implementation_name": "cpython", @@ -11,7 +11,7 @@ "platform_python_implementation": "CPython", "platform_release": "4.19.27-gentoo-r1", "platform_system": "Linux", - "platform_version": "#1 SMP Tue Apr 23 17:00:37 -00 2019", + "platform_version": "#1 SMP Wed May 1 11:50:20 CEST 2019", "python_full_version": "3.7.3", "python_version": "3.7", "sys_platform": "linux" @@ -29,6 +29,10 @@ ] }, "default": { + "aiohttp": { + "hashes": [], + "version": "==3.5.4" + }, "alembic": { "hashes": [], "version": "==1.0.10" @@ -37,6 +41,14 @@ "hashes": [], "version": "==6.0.0" }, + "async-timeout": { + "hashes": [], + "version": "==3.0.1" + }, + "attrs": { + "hashes": [], + "version": "==19.1.0" + }, "babel": { "hashes": [], "version": "==2.6.0" @@ -45,6 +57,10 @@ "hashes": [], "version": "==1.4" }, + "chardet": { + "hashes": [], + "version": "==3.0.4" + }, "click": { "hashes": [], "version": "==7.0" @@ -104,6 +120,15 @@ "hashes": [], "version": "==19.9.0" }, + "idna": { + "hashes": [], + "version": "==2.8" + }, + "idna-ssl": { + "hashes": [], + "markers": "python_version < '3.7'", + "version": "==1.1.0" + }, "itsdangerous": { "hashes": [], "version": "==1.1.0" @@ -120,18 +145,14 @@ "hashes": [], "version": "==1.1.1" }, - "mysqlclient": { + "multidict": { "hashes": [], - "version": "==1.4.2.post1" + "version": "==4.5.2" }, "passlib": { "hashes": [], "version": "==1.7.1" }, - "pymysql": { - "hashes": [], - "version": "==0.9.3" - }, "python-dateutil": { "hashes": [], "version": "==2.8.0" @@ -156,6 +177,11 @@ "hashes": [], "version": "==1.3.3" }, + "typing-extensions": { + "hashes": [], + "markers": "python_version < '3.7'", + "version": "==3.7.2" + }, "werkzeug": { "hashes": [], "version": "==0.15.2" @@ -163,6 +189,10 @@ "wtforms": { "hashes": [], "version": "==2.2.1" + }, + "yarl": { + "hashes": [], + "version": "==1.3.0" } }, "develop": {} diff --git a/conf.sh b/conf.sh index 3819666..b260553 100644 --- a/conf.sh +++ b/conf.sh @@ -1,21 +1,143 @@ #!/bin/sh +SCRIPT_NAME=$0 + +################# +# Configuration # +################# + +# List of all env variable +declare -A DATABASE_VAR_DESCRIPTION +DATABASE_VAR_DESCRIPTION["DATABASE_HOST"]="Set database host" +DATABASE_VAR_DESCRIPTION["DATABASE_PORT"]="Set database port" +DATABASE_VAR_DESCRIPTION["DATABASE_USER"]="Set database user" +DATABASE_VAR_DESCRIPTION["DATABASE_PASSWORD"]="Set database password, never share your .env file, your password is stored in clear" +DATABASE_VAR_DESCRIPTION["DATABASE_NAME"]="Set database name" +DATABASE_VARS="${!DATABASE_VAR_DESCRIPTION[@]}" + +declare -A CONFIG_CATEGORIES_DESCRIPTION +CONFIG_CATEGORIES_DESCRIPTION["DATABASE"]="Set database related config vars" +CONFIG_CATEGORIES=(DATABASE) + +CONFIG_VARS="$DATABASE_VARS" + +################ +# USEFULL VARS # +################ + +# Usefull vars for using dialog, kdialog and xdialog : ${DIALOG_OK=0} : ${DIALOG_CANCEL=1} : ${DIALOG_HELP=2} : ${DIALOG_EXTRA=3} : ${DIALOG_ITEM_HELP=4} : ${DIALOG_ESC=255} +# Colors +: ${BLACK='\033[0:30m'} +: ${RED='\033[0;31m'} +: ${GREEN='\033[0;32m'} +: ${ORANGE='\033[0;33m'} +: ${BLUE='\033[0;34m'} +: ${PURPLE='\033[0;35m'} +: ${CYAN='\033[0;36m'} +: ${LIGHT_GRAY='\033[0;37m'} +: ${DARK_GRAY='\033[1;30m'} +: ${LIGHT_RED='\033[1;31m'} +: ${LIGHT_GREEN='\033[1;32m'} +: ${YELLOW='\033[1;33m'} +: ${LIGHT_BLUE='\033[1;34m'} +: ${LIGHT_PURPLE='\033[1;35m'} +: ${LIGHT_CYAN='\033[1;36m'} +: ${WHITE='\033[1;37m'} +: ${NC='\033[0m'} + +########################## +# Rewrite beautiful echo # +########################## + +einfo () { +# einfo "comment" + echo -e " ${GREEN}*${NC} ${@:1}${NC}" +} +ewarn () { +# ewarn "comment" + echo -e " ${YELLOW}*${NC} ${@:1}${NC}" +} +eerror () { +# eerror "comment" + echo -e " ${RED}*${LIGHT_RED} ${@:1}${NC}" +} +die () { +# die exit_code "comment" + echo + eerror $2 + exit $1 +} +eend () { +# eend exit_status "comment" + einfo $2 + exit $1 +} +show_help () { +# show_help + echo "Usage : PDMI-configure [OPTIONS...]" + echo + echo " -h, --help Print this help menu" + echo " --xdialog Use xdialog instead of dialog" +} + +#################### +# Useful functions # +#################### + + + +################### +# Parse arguments # +################### + +SHORT=h: +LONG=xdialog,help,easter-egg: + +OPTS=$(getopt --options ${SHORT} --long ${LONG} --name "$0" -- "$@") + +# Problem when parsing args +if [[ $? != 0 ]] ; then echo "Failed to parse options...exiting." >&2 ; exit 1 ; fi + +eval set -- "${OPTS}" DIALOG=dialog -CONFIG_VARS="DATABASE_URI" +while true ; do + case "$1" in + --xdialog ) + which Xdialog && : || die 1 "Xdialog not found" + DIALOG=Xdialog + shift + ;; + --easter-egg) + die 0 "Suwako, je t'ai vu décortiquer le fichier" + ;; + -h | --help ) + show_help + end + shift + ;; + -- ) + shift + break + ;; + *) + echo "Internal error!" + exit 1 + ;; + esac +done set_env_var () { +load_config exec 3>&1 - source .env - load_config - local value=$($DIALOG \ + local value=$(${DIALOG} \ --backtitle "Configuration" \ --title $1 \ --inputbox "Veuillez insérez la nouvelle valeur de la variable ${1}:" \ @@ -24,31 +146,106 @@ set_env_var () { 2>&1 1>&3) local exit_status=$? exec 3>&- - close $exit_status - export $1=$value + export ${1}=$value save_config } -close () { - case $1 in - $DIALOG_CANCEL) - exit;; - $DIALOG_ESC) - exit;; +set_env_vars_cat () { + clear + CATEGORY=$1 + CATEGORY_VARS_NAME=${1}_VARS + CAT=${!CATEGORY_VARS_NAME} + CATEGORY_LIST=($CAT) + local OPTIONS="" + local COUNT=${#CATEGORY_LIST[@]} + for I in `seq 1 ${COUNT}`; + do + OPTIONS="$OPTIONS "${I}" "${CATEGORY_LIST[$(($I-1))]}"" + done + echo $OPTIONS + exec 3>&1 + selection=$(${DIALOG} \ + --backtitle "PDMI - Configuration" \ + --title "Set $CATEGORY vars" \ + --help-button \ + --menu "Choose an option" \ + -1 -1 0 \ + ${OPTIONS} \ + 2>&1 1>&3) + exit_status=$? + exec 3>&- + case ${exit_status} in + ${DIALOG_CANCEL}) + return + ;; + ${DIALOG_HELP}) + ;; + ${DIALOG_ESC}) + return + ;; esac + set_env_var ${CATEGORY_LIST[$(($selection-1))]} +} + +set_env_vars () { + local OPTIONS="" + local COUNT=`echo "$CONFIG_CATEGORIES" | wc -w` + for I in `seq 1 ${COUNT}`; + do + OPTIONS="$OPTIONS "${I}" "${CONFIG_CATEGORIES[$(($I-1))]}"" + done + exec 3>&1 + selection=$(${DIALOG} \ + --backtitle "PDMI - Configuration" \ + --title "Set configuration vars" \ + --help-button \ + --menu "Choose an option" \ + -1 -1 0 \ + ${OPTIONS} \ + 2>&1 1>&3) + exit_status=$? + exec 3>&- + case ${exit_status} in + ${DIALOG_CANCEL}) + return + ;; + ${DIALOG_HELP}) + local HELP="" + for NAME in ${!CONFIG_CATEGORIES_DESCRIPTION[@]}; do + HELP="$HELP\n${NAME} - ${CONFIG_CATEGORIES_DESCRIPTION[${NAME}]}" + done + echo ${HELP} + ${DIALOG} \ + --backtitle "PDMI - Set configuration vars" \ + --title "Set configuration vars - Help" \ + --msgbox "$HELP" \ + -1 -1 + ;; + ${DIALOG_ESC}) + return + ;; + esac + set_env_vars_cat $CONFIG_CATEGORIES } load_config () { - source .env + export $(cat .env | xargs) } save_config () { - export -p | grep -E "(${CONFIG_VARS})=" > .env + rm .env + for VAR in ${CONFIG_VARS} + do + if [[ -z ${!VAR} ]]; then + export ${VAR}=${VAR} + fi + echo ${VAR}=${!VAR} >> .env + done } bdd_config () { exec 3>&1 - local selection=$($DIALOG \ + local selection=$(${DIALOG} \ --backtitle "Base de donnée" \ --menu "Que souhaitez vous faire?" \ 0 0 0 \ @@ -58,9 +255,9 @@ bdd_config () { 2>&1 1>&3) local exit_status=$? exec 3>&- - close $exit_status + close ${exit_status} source .env - case $selection in + case ${selection} in 1) pipenv run python manage.py db init;; 2) @@ -72,7 +269,7 @@ bdd_config () { add_super_admin () { exec 3>&1 - local utilisateur=$($DIALOG \ + local utilisateur=$(${DIALOG} \ --backtitle "Utilitaires" \ --title "Administrateurs" \ --inputbox "Entrez le mail de l'utilisateur que vous voulez passer en superadministrateur" \ @@ -81,32 +278,32 @@ add_super_admin () { 2>&1 1>&3) local exit_status=$? exec 3>&- - close $exit_status + close ${exit_status} source .env pipenv run python manage.py set_superadmin -m "${utilisateur}" exit } -configuration () { - exec 3>&1 - local selection=$($DIALOG \ - --backtitle "Configuration" \ - --menu "Quelle variable modifier?" \ - 0 0 0 \ - "1" "DATABASE_URI" \ - 2>&1 1>&3) - exit_status=$? - exec 3>&- - close $exit_status - case $selection in - 1) - set_env_var "DATABASE_URI";; - esac -} +#configuration () { +# exec 3>&1 +# local selection=$($DIALOG \ +# --backtitle "Configuration" \ +# --menu "Quelle variable modifier?" \ +# 0 0 0 \ +# "1" "DATABASE_URI" \ +# 2>&1 1>&3) +# exit_status=$? +# exec 3>&- +# close $exit_status +# case $selection in +# 1) +# set_env_var "DATABASE_URI";; +# esac +#} utilitaires () { exec 3>&1 - selection=$($DIALOG \ + selection=$(${DIALOG} \ --backtitle "Utilitaires" \ --menu "Que voulez vous faire?" \ 0 0 0 \ @@ -115,8 +312,8 @@ utilitaires () { 2>&1 1>&3) exit_status=$? exec 3>&- - close $exit_status - case $selection in + close ${exit_status} + case ${selection} in 1) add_super_admin;; 2) @@ -124,32 +321,118 @@ utilitaires () { esac } -while true; do +check_config_file () { + clear + export $(cat .env | xargs) + rm .env + echo ${CONFIG_VARS} + for VAR in ${CONFIG_VARS} + do + echo ${VAR}=${!VAR} + if [[ -z ${!VAR} ]]; then + export ${VAR}=${VAR} + fi + echo ${VAR}=${!VAR} >> .env + done +} + +initialisation () { +: +} + +start () { + pipenv run python wsgy.py +} + +configuration () { exec 3>&1 - selection=$($DIALOG \ - --backtitle "Configuration" \ - --menu "Que souhaitez vous faire?" \ - 0 0 0 \ - "1" "Gestion de la base de donnée" \ - "2" "Configuration" \ - "3" "Utilitaires" \ - "4" "Démarre le serveur" \ - "5" "Quitter" \ + selection=$(${DIALOG} \ + --backtitle "PDMI - Configuration" \ + --title "Configuration" \ + --help-button \ + --menu "Choose an option" \ + -1 -1 0 \ + "1" "Check config file" \ + "2" "Set configuration" \ + "-" "" \ + "3" "Exit" \ 2>&1 1>&3) exit_status=$? exec 3>&- - close $exit_status - case $selection in + case ${exit_status} in + ${DIALOG_OK}) + ;; + ${DIALOG_CANCEL}) + return + ;; + ${DIALOG_HELP}) + ${DIALOG} \ + --backtitle "PDMI - Configuration" \ + --title "Configuration - Help" \ + --msgbox "Check config file - Check config file integrity +Set configuration - Set configuration" \ + -1 -1 + ;; + ${DIALOG_ESC}) + return + ;; + esac + case ${selection} in 1) - bdd_config;; + check_config_file + ;; + 2) + set_env_vars + ;; + 3) + break + ;; + esac +} + +# Main loop +while true; do + exec 3>&1 + selection=$(${DIALOG} \ + --backtitle "PDMI - Configuration" \ + --title "Configuration" \ + --help-button \ + --menu "Choose an option" \ + -1 -1 0 \ + "1" "Initialisation" \ + "2" "Configuration" \ + "3" "Start" \ + "-" "-" \ + "4" "Quit" \ + 2>&1 1>&3) + exit_status=$? + exec 3>&- + case ${exit_status} in + ${DIALOG_OK}) + ;; + ${DIALOG_CANCEL}) + eend 0 "End." + ;; + ${DIALOG_HELP}) + ${DIALOG} \ + --backtitle "PDMI - Configuration" \ + --title "Configuration - Help" \ + --msgbox "Initialisation - \n\nConfiguration - Configure usefull parameters, such as port, database uri, ..." \ + -1 -1 + ;; + ${DIALOG_ESC}) + eend 0 "End." + ;; + esac + close ${exit_status} + case ${selection} in + 1) + initialisation;; 2) configuration;; 3) - utilitaires;; + start;; 4) - source .env - pipenv run gunicorn --bind 0.0.0.0:5000 wsgy;; - 5) exit;; esac done