1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/coin_ynh.git synced 2024-09-03 18:16:26 +02:00

[enh] Use postgresql

This commit is contained in:
ljf 2017-08-02 16:26:44 +02:00
parent c9da4dd899
commit b3cecb64cd
7 changed files with 154 additions and 44 deletions

View file

@ -4,18 +4,18 @@ import os
DEBUG = TEMPLATE_DEBUG = True DEBUG = TEMPLATE_DEBUG = True
ALLOWED_HOSTS= ['YNH_APP_ARG_DOMAIN'] ALLOWED_HOSTS = ['YNH_APP_ARG_DOMAIN']
URL_PREFIX='YNH_APP_PREFIX' URL_PREFIX = 'YNH_APP_PREFIX'
STATIC_ROOT='YNH_APP_STATIC_ROOT' STATIC_ROOT = 'YNH_APP_STATIC_ROOT'
NOTIFICATION_EMAILS=['YNH_APP_ARG_EMAIL'] NOTIFICATION_EMAILS = ['YNH_APP_ARG_EMAIL']
DEFAULT_FROM_EMAIL='notifier@YNH_APP_ARG_DOMAIN' DEFAULT_FROM_EMAIL = 'notifier@YNH_APP_ARG_DOMAIN'
SITE_URL="https://YNH_APP_ARG_DOMAINYNH_APP_ARG_PATH" SITE_URL = "https://YNH_APP_ARG_DOMAINYNH_APP_ARG_PATH"
SECRET_KEY = 'YNH_APP_SECRET_KEY' SECRET_KEY = 'YNH_APP_SECRET_KEY'
ISP={ ISP = {
'NAME':'YNH_APP_ARG_ISP_NAME', 'NAME' : 'YNH_APP_ARG_ISP_NAME',
'SITE':'YNH_APP_ARG_ISP_SITE', 'SITE' : 'YNH_APP_ARG_ISP_SITE',
'EMAIL':'YNH_APP_ARG_EMAIL', 'EMAIL' : 'YNH_APP_ARG_EMAIL',
} }
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
@ -23,19 +23,15 @@ PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
DATABASES = { DATABASES = {
'default': { 'default': {
'ENGINE': 'django.db.backends.sqlite3', 'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': './coin.db', 'NAME': 'YNH_DB_NAME',
#'NAME': 'YNH_DB_NAME', 'USER': 'YNH_DB_USER',
#'USER': 'YNH_DB_USER', 'PASSWORD': 'YNH_DB_PASSWORD',
#'PASSWORD': 'YNH_DB_PASSWORD', 'HOST': '', # Empty for localhost through domain sockets
#'HOST': '', # Empty for localhost through domain sockets 'PORT': '', # Empty for default
#'PORT': '', # Empty for default
}, },
} }
DEFAULT_FROM_EMAIL = "YNH_APP_ARG_EMAIL" DEFAULT_FROM_EMAIL = "YNH_APP_ARG_EMAIL"
FEEDS = (('isp', 'http://www.illyse.net/feed/', 3), FEEDS = (('isp', 'http://www.illyse.net/feed/', 3),

View file

@ -13,7 +13,7 @@
"requirements": { "requirements": {
"yunohost": ">> 2.5.0" "yunohost": ">> 2.5.0"
}, },
"version": "20170731", "version": "20170802",
"multi_instance": "false", "multi_instance": "false",
"services": [ "services": [
"nginx" "nginx"

View file

@ -2,6 +2,19 @@
function install_dependencies() function install_dependencies()
{ {
# 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"
}

View file

@ -32,7 +32,7 @@ ynh_install_app_dependencies () {
if [ ! -e "$manifest_path" ]; then if [ ! -e "$manifest_path" ]; then
manifest_path="../settings/manifest.json" # Into the restore script, the manifest is not at the same place manifest_path="../settings/manifest.json" # Into the restore script, the manifest is not at the same place
fi 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 '-' dep_app=${app//_/-} # Replace all '_' by '-'
if ynh_package_is_installed "${dep_app}-ynh-deps"; then if ynh_package_is_installed "${dep_app}-ynh-deps"; then
@ -238,4 +238,89 @@ ynh_exit_if_up_to_date () {
fi 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
}

View file

@ -41,18 +41,6 @@ function configure_app()
ynh_app_setting_set "$app" secret "$secret" 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() function install_from_sources()
{ {
$final_path/venv/bin/pip install "pip>=1.5.6" $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_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_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_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_NAME#$db_name#g" ../conf/local.py
# sed -i "s#YNH_DB_USER#$db_user#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_PASSWORD#$db_pwd#g" ../conf/local.py
sudo cp ../conf/local.py $final_path/$app/settings_local.py sudo cp ../conf/local.py $final_path/$app/settings_local.py
# Set production # Set production

View file

@ -15,8 +15,8 @@ DOMAIN=$(ynh_app_setting_get "$APP" domain)
# Add tests that stuff actually exists before removing them # Add tests that stuff actually exists before removing them
# Remove mysql table # Remove mysql table
ynh_mysql_drop_user $app ynh_psql_drop_user $app
ynh_mysql_drop_db $app ynh_psql_drop_db $app
# Remove dependencies # Remove dependencies
ynh_remove_app_dependencies ynh_remove_app_dependencies

View file

@ -24,9 +24,12 @@ ynh_normalize_url_path "$path"
#if [ "${version}" = "20170408" ]; then #if [ "${version}" = "20170408" ]; then
#fi #fi
# Install new dependencies
install_dependencies
# Copy files to the right place # Copy files to the right place
final_path=/opt/$app final_path=/opt/$app
ynh_setup_source $final_path [ -L ${final_path}/coin ] || ynh_setup_source $final_path
set +o nounset set +o nounset
source $final_path/venv/bin/activate source $final_path/venv/bin/activate
@ -34,14 +37,39 @@ set -o nounset
$final_path/venv/bin/pip install -r $final_path/requirements.txt $final_path/venv/bin/pip install -r $final_path/requirements.txt
# Set permissions # Set permissions
sudo useradd $app -d $final_path || echo "User already exists" useradd $app -d $final_path || echo "User already exists"
sudo chown -R $app:www-data $final_path chown -R $app:www-data $final_path
pushd $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 <<EOF
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '$db_name',
'USER': '$db_user',
'PASSWORD': '$db_pwd',
'HOST': '', # Empty for localhost through domain sockets
'PORT': '', # Empty for default
},
}
EOF
fi
$final_path/venv/bin/python manage.py migrate --noinput $final_path/venv/bin/python manage.py migrate --noinput
if [ "${version}" = "20170731" ]; then
$final_path/venv/bin/python manage.py loaddata /tmp/dump.json
fi
$final_path/venv/bin/python manage.py collectstatic --noinput $final_path/venv/bin/python manage.py collectstatic --noinput
popd popd
# Set permissions to directory # Set permissions to directory
sudo chown $app:www-data -R $final_path chown $app:www-data -R $final_path
[ -L ${final_path}/coin ] || service coin restart