diff --git a/manifest.json b/manifest.json index 8f93fb8..8059b7f 100644 --- a/manifest.json +++ b/manifest.json @@ -16,7 +16,7 @@ "requirements": { "yunohost": ">> 3.0" }, - "version": "20180817~ynh3", + "version": "20180817~ynh4", "multi_instance": false, "services": [ "nginx" diff --git a/scripts/backup b/scripts/backup index eb5832a..1a6d84d 100644 --- a/scripts/backup +++ b/scripts/backup @@ -18,3 +18,6 @@ ynh_backup "/etc/nginx/conf.d/${domain}.d/${app}.conf" "nginx.conf" # Copy Gunicorn pool configuration ynh_backup "/etc/systemd/system/$app.service" "gunicorn.service" + +# Backup database +ynh_psql_dump_db "$app" > ${YNH_CWD}/dump.sql diff --git a/scripts/common.sh b/scripts/common.sh index 3ecf90c..87e10a8 100644 --- a/scripts/common.sh +++ b/scripts/common.sh @@ -7,9 +7,7 @@ function install_dependencies() function init_db() { - sed -i '/local\s*all\s*all\s*peer/i \ -local all coin password' /etc/postgresql/9.*/main/pg_hba.conf - service postgresql reload + ynh_psql_test_if_first_run # Generate random password db_name=$app db_user=$app diff --git a/scripts/future.sh b/scripts/future.sh index d827d1d..7652ee3 100644 --- a/scripts/future.sh +++ b/scripts/future.sh @@ -237,90 +237,3 @@ ynh_exit_if_up_to_date () { exit 0 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 c2aab89..5adc56a 100644 --- a/scripts/install +++ b/scripts/install @@ -98,13 +98,14 @@ function configure_gunicorn() sudo cp ../conf/gunicorn.service /etc/systemd/system/$app.service sudo systemctl daemon-reload sudo cp ../conf/gunicorn_config.py /opt/$app/ + chown $app:www-data /opt/$app/gunicorn_config.py } function configure_nginx_and_ssowat() { # Reload Nginx and regenerate SSOwat conf sudo yunohost app addaccess $app -u $admin - + # Modify Nginx configuration file and copy it to Nginx conf directory sed -i "s@PATHTOCHANGE@$path@g" ../conf/nginx.conf sed -i "s@ALIASTOCHANGE@$final_path/@g" ../conf/nginx.conf diff --git a/scripts/remove b/scripts/remove index 3a445c1..b69c04b 100644 --- a/scripts/remove +++ b/scripts/remove @@ -15,8 +15,7 @@ DOMAIN=$(ynh_app_setting_get "$APP" domain) # Add tests that stuff actually exists before removing them # Remove mysql table -ynh_psql_drop_db $app -ynh_psql_drop_user $app +ynh_psql_remove_db $app $app # Remove dependencies ynh_remove_app_dependencies diff --git a/scripts/restore b/scripts/restore index c3d4fc8..902cb6b 100644 --- a/scripts/restore +++ b/scripts/restore @@ -8,6 +8,7 @@ app=$YNH_APP_INSTANCE_NAME # Source YunoHost helpers . /usr/share/yunohost/helpers +source ../settings/scripts/common.sh # Retrieve old app settings domain=$(ynh_app_setting_get "$app" domain) @@ -17,6 +18,7 @@ email=$(ynh_app_setting_get "$app" email) isp_name=$(ynh_app_setting_get "$app" isp_name) isp_site=$(ynh_app_setting_get "$app" isp_site) secret=$(ynh_app_setting_get "$app" secret) +db_pwd=$(ynh_app_setting_get "$app" psqlpassword) # Check domain/path availability sudo yunohost app checkurl $domain$path -a $app \ @@ -39,14 +41,16 @@ if [ -f $gunicorn_path ]; then fi # Dependences -ynh_package_install python3-pip python3-virtualenv +install_dependencies # Restore sources & data sudo cp -a ./sources $final_path +# Create user +useradd $app -d $final_path || echo "User already exists" # Set permissions -sudo chown -R www-data:www-data $final_path +sudo chown -R $app:www-data $final_path # Restore conf files sudo cp -a ./nginx.conf $conf @@ -59,6 +63,13 @@ if ynh_user_exists $admin; then sudo yunohost app addaccess $app -u $admin fi +# Restore database +ynh_psql_test_if_first_run +db_name=$app +db_user=$app +ynh_psql_create_db $db_name $db_user $db_pwd +ynh_psql_execute_file_as_root "${YNH_CWD}/dump.sql" "$db_name" + # Log folder sudo mkdir -p /var/log/$app sudo chown -R $app /var/log/$app @@ -66,6 +77,7 @@ sudo chgrp -R www-data /var/log/$app # Reload Nginx sudo service nginx reload +systemctl daemon-reload sudo systemctl start $app sudo systemctl enable $app sudo yunohost service add $app -l /var/log/gunicorn/$app.log