From b3cecb64cd89fdd4e4306734e602a895f96613ee Mon Sep 17 00:00:00 2001 From: ljf Date: Wed, 2 Aug 2017 16:26:44 +0200 Subject: [PATCH] [enh] Use postgresql --- conf/local.py | 36 +++++++++----------- manifest.json | 2 +- scripts/common.sh | 15 +++++++- scripts/future.sh | 87 ++++++++++++++++++++++++++++++++++++++++++++++- scripts/install | 18 ++-------- scripts/remove | 4 +-- scripts/upgrade | 36 +++++++++++++++++--- 7 files changed, 154 insertions(+), 44 deletions(-) diff --git a/conf/local.py b/conf/local.py index 3d2417a..883ceb4 100644 --- a/conf/local.py +++ b/conf/local.py @@ -4,18 +4,18 @@ import os DEBUG = TEMPLATE_DEBUG = True -ALLOWED_HOSTS= ['YNH_APP_ARG_DOMAIN'] +ALLOWED_HOSTS = ['YNH_APP_ARG_DOMAIN'] -URL_PREFIX='YNH_APP_PREFIX' -STATIC_ROOT='YNH_APP_STATIC_ROOT' -NOTIFICATION_EMAILS=['YNH_APP_ARG_EMAIL'] -DEFAULT_FROM_EMAIL='notifier@YNH_APP_ARG_DOMAIN' -SITE_URL="https://YNH_APP_ARG_DOMAINYNH_APP_ARG_PATH" +URL_PREFIX = 'YNH_APP_PREFIX' +STATIC_ROOT = 'YNH_APP_STATIC_ROOT' +NOTIFICATION_EMAILS = ['YNH_APP_ARG_EMAIL'] +DEFAULT_FROM_EMAIL = 'notifier@YNH_APP_ARG_DOMAIN' +SITE_URL = "https://YNH_APP_ARG_DOMAINYNH_APP_ARG_PATH" SECRET_KEY = 'YNH_APP_SECRET_KEY' -ISP={ - 'NAME':'YNH_APP_ARG_ISP_NAME', - 'SITE':'YNH_APP_ARG_ISP_SITE', - 'EMAIL':'YNH_APP_ARG_EMAIL', +ISP = { + 'NAME' : 'YNH_APP_ARG_ISP_NAME', + 'SITE' : 'YNH_APP_ARG_ISP_SITE', + 'EMAIL' : 'YNH_APP_ARG_EMAIL', } SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') @@ -23,19 +23,15 @@ PROJECT_DIR = os.path.abspath(os.path.dirname(__file__)) DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': './coin.db', - #'NAME': 'YNH_DB_NAME', - #'USER': 'YNH_DB_USER', - #'PASSWORD': 'YNH_DB_PASSWORD', - #'HOST': '', # Empty for localhost through domain sockets - #'PORT': '', # Empty for default + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'NAME': 'YNH_DB_NAME', + 'USER': 'YNH_DB_USER', + 'PASSWORD': 'YNH_DB_PASSWORD', + 'HOST': '', # Empty for localhost through domain sockets + 'PORT': '', # Empty for default }, } - - - DEFAULT_FROM_EMAIL = "YNH_APP_ARG_EMAIL" FEEDS = (('isp', 'http://www.illyse.net/feed/', 3), diff --git a/manifest.json b/manifest.json index bee0b2a..c61c6d1 100644 --- a/manifest.json +++ b/manifest.json @@ -13,7 +13,7 @@ "requirements": { "yunohost": ">> 2.5.0" }, - "version": "20170731", + "version": "20170802", "multi_instance": "false", "services": [ "nginx" diff --git a/scripts/common.sh b/scripts/common.sh index 55e8e94..3f0e96b 100644 --- a/scripts/common.sh +++ b/scripts/common.sh @@ -2,6 +2,19 @@ function install_dependencies() { # Dependencies - ynh_install_app_dependencies gunicorn python-dev python-pip libldap2-dev libpq-dev libsasl2-dev libjpeg-dev libxml2-dev libxslt1-dev libffi-dev python-cairo libpango1.0-0 #libmysqlclient-dev + ynh_install_app_dependencies gunicorn python-dev python-pip libldap2-dev libpq-dev libsasl2-dev libjpeg-dev libxml2-dev libxslt1-dev libffi-dev python-cairo libpango1.0-0 postgresql postgresql-contrib #libmysqlclient-dev } +function init_db() +{ + sed -i '/local\s*all\s*all\s*peer/i \ +local all coin password' /etc/postgresql/9.4/main/pg_hba.conf + service postgresql reload + # Generate random password + db_name=$app + db_user=$app + db_pwd=$(ynh_string_random) + # Initialize database and store pssql password for upgrade + ynh_psql_create_db $db_name $db_user $db_pwd + ynh_app_setting_set "$app" psqlpassword "$db_pwd" +} diff --git a/scripts/future.sh b/scripts/future.sh index 3ead185..d827d1d 100644 --- a/scripts/future.sh +++ b/scripts/future.sh @@ -32,7 +32,7 @@ ynh_install_app_dependencies () { if [ ! -e "$manifest_path" ]; then manifest_path="../settings/manifest.json" # Into the restore script, the manifest is not at the same place fi - version=$(sudo python3 -c "import sys, json;print(json.load(open(\"$manifest_path\"))['version'])") # Retrieve the version number in the manifest file. + local version=$(sudo python3 -c "import sys, json;print(json.load(open(\"$manifest_path\"))['version'])") # Retrieve the version number in the manifest file. dep_app=${app//_/-} # Replace all '_' by '-' if ynh_package_is_installed "${dep_app}-ynh-deps"; then @@ -238,4 +238,89 @@ ynh_exit_if_up_to_date () { fi } +# Open a connection as a user +# +# example: ynh_psql_connect_as 'user' 'pass' <<< "UPDATE ...;" +# example: ynh_psql_connect_as 'user' 'pass' < /path/to/file.sql +# +# usage: ynh_psql_connect_as user pwd [db] +# | arg: user - the user name to connect as +# | arg: pwd - the user password +# | arg: db - the database to connect to +ynh_psql_connect_as() { + ynh_die "ynh_psql_connect_as is not yet implemented" +} +# # Execute a command as root user +# +# usage: ynh_psql_execute_as_root sql [db] +# | arg: sql - the SQL command to execute +# | arg: db - the database to connect to +ynh_psql_execute_as_root () { + sudo su -c "psql" - postgres <<< ${1} +} + +# Execute a command from a file as root user +# +# usage: ynh_psql_execute_file_as_root file [db] +# | arg: file - the file containing SQL commands +# | arg: db - the database to connect to +ynh_psql_execute_file_as_root() { + ynh_die "ynh_psql_execute_file_as_root is not yet implemented" +} + +# Create a database and grant optionnaly privilegies to a user +# +# usage: ynh_psql_create_db db [user [pwd]] +# | arg: db - the database name to create +# | arg: user - the user to grant privilegies +# | arg: pwd - the password to identify user by +ynh_psql_create_db() { + db=$1 + # grant all privilegies to user + if [[ $# -gt 1 ]]; then + ynh_psql_create_user ${2} "${3}" + sudo su -c "createdb -O ${2} $db" - postgres + else + sudo su -c "createdb $db" - postgres + fi + +} + +# Drop a database +# +# usage: ynh_psql_drop_db db +# | arg: db - the database name to drop +ynh_psql_drop_db() { + sudo su -c "dropdb ${1}" - postgres +} + +# Dump a database +# +# example: ynh_psql_dump_db 'roundcube' > ./dump.sql +# +# usage: ynh_psql_dump_db db +# | arg: db - the database name to dump +# | ret: the psqldump output +ynh_psql_dump_db() { + ynh_die "ynh_psql_dump_db is not yet implemented" +} + + +# Create a user +# +# usage: ynh_psql_create_user user pwd [host] +# | arg: user - the user name to create +# | arg: pwd - the password to identify user by +ynh_psql_create_user() { + ynh_psql_execute_as_root \ + "CREATE USER ${1} WITH PASSWORD '${2}';" +} + +# Drop a user +# +# usage: ynh_psql_drop_user user +# | arg: user - the user name to drop +ynh_psql_drop_user() { + sudo su -c "dropuser ${1}" - postgres +} diff --git a/scripts/install b/scripts/install index a758fd7..27f112c 100644 --- a/scripts/install +++ b/scripts/install @@ -41,18 +41,6 @@ function configure_app() ynh_app_setting_set "$app" secret "$secret" } -function init_db() -{ - # Generate random password - db_name=$app - db_user=$app - db_pwd=$(ynh_string_random) - - # Initialize database and store mysql password for upgrade - ynh_mysql_create_db $db_name $db_user $db_pwd - ynh_app_setting_set "$app" mysqlpassword "$db_pwd" -} - function install_from_sources() { $final_path/venv/bin/pip install "pip>=1.5.6" @@ -74,9 +62,9 @@ function install_from_sources() sed -i "s#YNH_APP_ARG_ISP_NAME#$isp_name#g" ../conf/local.py sed -i "s#YNH_APP_ARG_ISP_SITE#$isp_site#g" ../conf/local.py sed -i "s#YNH_APP_STATIC_ROOT#$final_path/static#g" ../conf/local.py -# sed -i "s#YNH_DB_NAME#$db_name#g" ../conf/local.py -# sed -i "s#YNH_DB_USER#$db_user#g" ../conf/local.py -# sed -i "s#YNH_DB_PASSWORD#$db_pwd#g" ../conf/local.py + sed -i "s#YNH_DB_NAME#$db_name#g" ../conf/local.py + sed -i "s#YNH_DB_USER#$db_user#g" ../conf/local.py + sed -i "s#YNH_DB_PASSWORD#$db_pwd#g" ../conf/local.py sudo cp ../conf/local.py $final_path/$app/settings_local.py # Set production diff --git a/scripts/remove b/scripts/remove index 812357d..98277c9 100644 --- a/scripts/remove +++ b/scripts/remove @@ -15,8 +15,8 @@ DOMAIN=$(ynh_app_setting_get "$APP" domain) # Add tests that stuff actually exists before removing them # Remove mysql table -ynh_mysql_drop_user $app -ynh_mysql_drop_db $app +ynh_psql_drop_user $app +ynh_psql_drop_db $app # Remove dependencies ynh_remove_app_dependencies diff --git a/scripts/upgrade b/scripts/upgrade index 9e3b6b4..212a5aa 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -24,9 +24,12 @@ ynh_normalize_url_path "$path" #if [ "${version}" = "20170408" ]; then #fi +# Install new dependencies +install_dependencies + # Copy files to the right place final_path=/opt/$app -ynh_setup_source $final_path +[ -L ${final_path}/coin ] || ynh_setup_source $final_path set +o nounset source $final_path/venv/bin/activate @@ -34,14 +37,39 @@ set -o nounset $final_path/venv/bin/pip install -r $final_path/requirements.txt # Set permissions -sudo useradd $app -d $final_path || echo "User already exists" -sudo chown -R $app:www-data $final_path +useradd $app -d $final_path || echo "User already exists" +chown -R $app:www-data $final_path pushd $final_path +if [ "${version}" = "20170731" ]; then + init_db + ynh_app_setting_delete $app mysqlpassword + $final_path/venv/bin/python manage.py dumpdata > /tmp/dump.json + + cat >> /opt/$app/coin/settings_local.py <