From 4e8c709cf4b2ab63996d6a3165e938c0df524b72 Mon Sep 17 00:00:00 2001
From: ljf <ljf+git@grimaud.me>
Date: Fri, 14 Dec 2018 03:36:58 +0100
Subject: [PATCH] [enh] Install from sources

---
 check_process               |   9 ++-
 conf/nginx.conf.j2          |  31 +++++++-
 conf/odoo.service.j2        |  14 ----
 conf/openerp-server.conf.j2 |  12 ---
 manifest.json               |  10 +--
 scripts/_common.sh          | 146 +++++-----------------------------
 scripts/_future.sh          | 154 +-----------------------------------
 scripts/backup              |  23 ++----
 scripts/install             |  76 +++++++++---------
 scripts/remove              |  42 ++--------
 scripts/restore             |  41 +++-------
 scripts/upgrade             |  27 +++----
 12 files changed, 140 insertions(+), 445 deletions(-)
 delete mode 100644 conf/odoo.service.j2
 delete mode 100644 conf/openerp-server.conf.j2

diff --git a/check_process b/check_process
index 76a26b5..9a165a4 100644
--- a/check_process
+++ b/check_process
@@ -2,7 +2,7 @@
 	auto_remove=1
 	; Manifest
 		domain="domain.tld"	(DOMAIN)
-        version="10.0"
+        version="12"
         oca=0
         admin_password="testpassw0rD"
 		lang="en_US"
@@ -17,13 +17,13 @@
 		upgrade=1
 		backup_restore=1
 		multi_instance=1
-		wrong_user=0
+		wrong_user=1
 		wrong_path=0
 		incorrect_path=0
 		corrupt_source=1
 		fail_download_source=1
 		port_already_use=1
-		final_path_already_use=0
+		final_path_already_use=1
 ;;; Levels
 	Level 1=auto
 	Level 2=auto
@@ -35,3 +35,6 @@
 	Level 8=0
 	Level 9=0
 	Level 10=0
+;;; Options
+Email=ljf+odoo_ynh@reflexlibre.net
+Notification=down
diff --git a/conf/nginx.conf.j2 b/conf/nginx.conf.j2
index 623dce2..9700fb2 100644
--- a/conf/nginx.conf.j2
+++ b/conf/nginx.conf.j2
@@ -14,6 +14,33 @@ location @{{ app }} {
     proxy_set_header	X-Real-IP $remote_addr;
     proxy_set_header	X-Forwarded-Proto $scheme;
     proxy_set_header	X-Forwarded-For $proxy_add_x_forwarded_for;
-    proxy_set_header	X-Forwarded-Host $server_name;
-    proxy_read_timeout	999999999;
+    proxy_set_header	X-Forwarded-Host $host;
+    proxy_read_timeout 720s;
+    proxy_connect_timeout 720s;
+    proxy_send_timeout 720s;
+}
+location /longpolling {
+    proxy_pass http://127.0.0.1:8072;
+    proxy_set_header	Host $host;
+    proxy_set_header	X-Real-IP $remote_addr;
+    proxy_set_header	X-Forwarded-Proto $scheme;
+    proxy_set_header	X-Forwarded-For $proxy_add_x_forwarded_for;
+    proxy_set_header	X-Forwarded-Host $host;
+    proxy_read_timeout 720s;
+    proxy_connect_timeout 720s;
+    proxy_send_timeout 720s;
+}
+location ~* /web/static/ {
+    proxy_cache_valid 200 90m;
+    proxy_buffering on;
+    expires 864000;
+    proxy_pass http://127.0.0.1:{{ port }};
+    proxy_set_header	Host $host;
+    proxy_set_header	X-Real-IP $remote_addr;
+    proxy_set_header	X-Forwarded-Proto $scheme;
+    proxy_set_header	X-Forwarded-For $proxy_add_x_forwarded_for;
+    proxy_set_header	X-Forwarded-Host $host;
+    proxy_read_timeout 720s;
+    proxy_connect_timeout 720s;
+    proxy_send_timeout 720s;
 }
diff --git a/conf/odoo.service.j2 b/conf/odoo.service.j2
deleted file mode 100644
index 6c34902..0000000
--- a/conf/odoo.service.j2
+++ /dev/null
@@ -1,14 +0,0 @@
-[Unit]
-Description=Odoo
-After=network.target
-
-[Service]
-Type=simple
-User=odoo
-Environment=PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
-ExecStart={{ bin_file }} --config {{ conf_file }} --logfile /var/log/odoo/odoo-server.log
-Restart=always
-RestartSec=3
-
-[Install]
-WantedBy=multi-user.target
diff --git a/conf/openerp-server.conf.j2 b/conf/openerp-server.conf.j2
deleted file mode 100644
index c1ed393..0000000
--- a/conf/openerp-server.conf.j2
+++ /dev/null
@@ -1,12 +0,0 @@
-[options]
-; This is the password that allows database operations:
-without_demo = all
-admin_passwd = {{ admin_password }}
-db_host = localhost
-db_port = 5432
-db_user = odoo
-db_password = {{ dbpass }}
-addons_path = {{ source_path }}addons
-proxy_mode = true
-dbfilter = ^{{ database }}$
-logfile = /var/log/odoo/odoo-server.log
diff --git a/manifest.json b/manifest.json
index a0ad2e5..3ead0e8 100644
--- a/manifest.json
+++ b/manifest.json
@@ -8,16 +8,16 @@
   },
   "maintainer": {
     "name": "ljf",
-    "email": "ljf+ynh-odoo@reflexlibre.net"
+    "email": "ljf+odoo_ynh@reflexlibre.net"
   },
   "previous_maintainers": {
     "name": "scith"
   },
-  "version": "11.0-1",
+  "version": "12.0-0",
   "url": "https://www.odoo.com",
   "license": "LGPL-3.0,AGPL-3.0",
   "requirements": {
-    "yunohost": ">= 2.6.0"
+    "yunohost": ">= 3.3.0"
   },
   "multi_instance": true,
   "services": [
@@ -38,8 +38,8 @@
         "en": "Which version of Odoo do you want to install?",
         "fr": "Quelle version d'Odoo souhaitez-vous installer ?"
       },
-      "choices": ["8.0", "9.0", "10.0", "11.0", "12.0"],
-      "default": "12.0"
+      "choices": ["8", "9", "10", "11", "12"],
+      "default": "12"
     }, {
       "name": "oca",
       "type": "boolean",
diff --git a/scripts/_common.sh b/scripts/_common.sh
index bde9e5a..2f36b61 100644
--- a/scripts/_common.sh
+++ b/scripts/_common.sh
@@ -2,153 +2,49 @@
 # Common variables
 #
 
-APPNAME="odoo"
-
-function define_paths() {
-    command -v bc >/dev/null 2>&1 || ynh_package_install bc
-    # In odoo 10 some file change
-    if [ $(echo "$odoo_version >= 10" | bc) -ne 0 ]; then
-        if [ $(echo "$odoo_version >= 11" | bc) -ne 0 ]; then
-            export source_path=/usr/lib/python3/dist-packages/odoo/
-        else
-            export source_path=/usr/lib/python2.7/dist-packages/odoo/
-        fi
-        export conf_file=/etc/odoo/odoo.conf
-        export bin_file=/usr/bin/odoo
-    else
-        export source_path=/usr/lib/python2.7/dist-packages/openerp/
-        export conf_file=/etc/odoo/openerp-server.conf
-        export bin_file=/usr/bin/openerp-server
-    fi
-}
-
-function check_odoo_version () {
-    command -v bc >/dev/null 2>&1 || ynh_package_install bc
-    if [ $(echo "$odoo_version >= 10" | bc) -ne 0 ]; then
-        if [ -f /usr/bin/openerp-server ]; then
-            ynh_die "Another version of odoo is installed"
-        fi
-    else
-        if [ -f /usr/bin/odoo ]; then
-            ynh_die "Another version of odoo is installed"
-        fi
-    fi
-}
-
-function define_is_master() {
-    if [ -f $bin_file ]; then
-        export is_master=false
-    else
-        export is_master=true
-    fi
-}
-
-function define_port () {
-    if [ "$is_master" = true ]; then
-        export port=$(ynh_find_port 8069)
-        yunohost app checkport $port
-        if [[ ! $? -eq 0 ]]; then
-        ynh_die "Port 8069 unavailable" 1
-        fi
-    else
-        # FIXME find master port
-        export port="8069"
-    fi
-}
-
-function define_dbpass () {
-    # TODO set -x
-    if [ "$is_master" = true ]; then
-        # Generate random password
-        if [ "${1:-}" = "restore" ]; then
-            export dbpass=$(ynh_app_setting_get $app psqlpwd)
-        else
-            export dbpass=$(ynh_string_random)
-        fi
-    else
-        export dbpass=$(grep db_password /etc/odoo/odoo.conf | cut -d \= -f 2 | sed -e 's/^[ \t]*//')
-    fi
-    ynh_app_setting_set "$app" psqlpwd "$dbpass"
-}
+export APPNAME="odoo"
+DEPENDENCIES="curl postgresql xfonts-75dpi xfonts-base wkhtmltopdf node-less python3-dev gcc libldap2-dev libssl-dev libsasl2-dev python3-pip python3-dev python3-venv python3-wheel libxslt-dev libzip-dev python3-setuptools python-virtualenv python-wheel python-setuptools libjpeg-dev zlib1g-dev"
 
 # Install dependencies
 function install_dependencies() {
     ynh_add_swap 1024
-    if [ ! -f /etc/apt/sources.list.d/odoo.list ]; then
-        # Install Odoo
-        # Prepare installation
-        # We nee to setup postgresql before to let the odoo package make some magic
-        # see red comment on https://nightly.odoo.com/
-        ynh_package_install curl bc postgresql
-
-        # Install Odoo
-        curl -sS https://nightly.odoo.com/odoo.key | sudo apt-key add -
-        sh -c "echo 'deb http://nightly.odoo.com/${odoo_version}/nightly/deb/ ./' > /etc/apt/sources.list.d/odoo.list"
-        # TODO if 8.0 install https://www.odoo.com/apps/modules/8.0/shell/
-    fi
-
-    if is_jessie ; then
-        sudo echo "deb http://http.debian.net/debian jessie-backports main" | sudo tee /etc/apt/sources.list.d/jessie-backport.list
-        apt-get update
-        ynh_install_app_dependencies curl postgresql odoo xfonts-75dpi xfonts-base wkhtmltopdf node-less python-xlrd python3-dev gcc libldap2-dev libssl-dev libsasl2-dev python3-pip
-        pip3 install pyldap
-    fi
-    if is_stretch ; then
-        sudo echo "deb http://http.debian.net/debian stretch-backports main" | sudo tee /etc/apt/sources.list.d/stretch-backport.list
-        if [ $(echo "$odoo_version <= 10" | bc) -ne 0 ]; then
-            cat > /tmp/python-pypdf_1.13_all.control << EOF	# Make a control file for equivs-build
-Section: python
-Package: python-pypdf
-Version: 1.13
-Description: fake package to provide python-pypdf
-EOF
-            ynh_package_install python3-pip
-            ynh_package_install_from_equivs /tmp/python-pypdf_1.13_all.control
-            rm /tmp/python-pypdf_1.13_all.control
-            pip install pyPdf
-        fi
-        apt update
-        ynh_install_app_dependencies curl postgresql odoo xfonts-75dpi xfonts-base wkhtmltopdf node-less python-xlrd python3-dev gcc libldap2-dev libssl-dev libsasl2-dev python3-pip python3-num2words python3-pyldap python3-phonenumbers
-    fi
+    ynh_install_app_dependencies $DEPENDENCIES
 
     if ! wkhtmltopdf --version | grep "wkhtmltopdf 0.12.4 (with patched qt)"; then
         # The debian package has a bug so we deploy a more recent version
         ynh_setup_source /usr/
     fi
-}
-
-
-# Create db
-function create_general_db() {
-    service postgresql reload
-    if ! su -c "psql -lqt | cut -d \| -f 1 " - postgres | grep $APPNAME; then
-        # Generate random password
-        ynh_psql_execute_as_root "ALTER USER $APPNAME WITH CREATEDB;"
-        ynh_psql_execute_as_root "ALTER USER $APPNAME WITH PASSWORD '$dbpass';"
-        su -c "createdb -O $APPNAME $APPNAME" -  postgres
+    pushd $final_path
+    if grep "python3" $final_path/$APPNAME/$APPNAME-bin ; then
+        python3 -m venv venv
+        venv/bin/pip3 install wheel
+        venv/bin/pip3 install -r $APPNAME/requirements.txt
+    else
+        virtualenv venv
+        venv/bin/pip install wheel
+        venv/bin/pip install -r $APPNAME/requirements.txt
     fi
+    popd
 }
 
+
 # Add services
 function add_services() {
     if ! grep "^postgresql:$" /etc/yunohost/services.yml; then
         yunohost service add postgresql
     fi
-    if ! grep "^odoo:$" /etc/yunohost/services.yml; then
-        ynh_configure odoo.service /etc/systemd/system/odoo.service
-        rm /etc/init.d/odoo
-        systemctl daemon-reload
+    ynh_configure app.service /etc/systemd/system/$app.service
+    systemctl daemon-reload
 
-        yunohost service add odoo --log /var/log/odoo/odoo-server.log
-        yunohost service stop odoo
-        yunohost service start odoo
-        yunohost service enable odoo
-    fi
+    yunohost service add $app --log /var/log/$app.log
+    yunohost service stop $app
+    yunohost service start $app
+    yunohost service enable $app
 }
 
 function ssowat_and_restart() {
     # Restart odoo service
-    service odoo restart
+    service $app restart
 
     # Configure SSOWat
     ynh_sso_access "/web/database/manager"
diff --git a/scripts/_future.sh b/scripts/_future.sh
index 2f6e8c3..ab19073 100644
--- a/scripts/_future.sh
+++ b/scripts/_future.sh
@@ -1,53 +1,7 @@
-
-log() {
-  echo "${1}"
-}
-
-info() {
-  log "[INFO] ${1}"
-}
-
-warn() {
-  log "[WARN] ${1}"
-}
-
-err() {
-  log "[ERR] ${1}"
-}
 ynh_check_var () {
     test -n "$1" || ynh_die "$2"
 }
 
-ynh_exit_properly () {
-    exit_code=$?
-    if [ "$exit_code" -eq 0 ]; then
-            exit 0
-    fi
-    trap '' EXIT
-    set +eu
-    echo -e "\e[91m \e[1m"
-    err "$app script has encountered an error."
-
-    if type -t CLEAN_SETUP > /dev/null; then
-        CLEAN_SETUP
-    fi
-
-    ynh_die
-}
-
-# Activate signal capture
-# Exit if a command fail, and if a variable is used unset.
-# Capturing exit signals on shell script
-#
-# example: CLEAN_SETUP () {
-#             # Clean residual file un remove by remove script
-#          }
-#          ynh_trap_on
-ynh_trap_on () {
-    set -eu
-    trap ynh_exit_properly EXIT # Capturing exit signals on shell script
-}
-
 ynh_export () {
     local ynh_arg=""
     for var in $@;
@@ -78,43 +32,13 @@ ynh_sso_access () {
 ynh_configure () {
     local TEMPLATE=$1
     local DEST=$2
-    type j2 2>/dev/null || sudo pip install j2cli
-    j2 "${YNH_CWD}/../conf/$TEMPLATE.j2" > "${YNH_CWD}/../conf/$TEMPLATE"
-    sudo cp "${YNH_CWD}/../conf/$TEMPLATE" "$DEST"
+    ynh_render_template "${YNH_CWD}/../conf/$TEMPLATE.j2" "$DEST"
 }
 
 ynh_configure_nginx () {
     ynh_configure nginx.conf /etc/nginx/conf.d/$domain.d/$app.conf
     sudo service nginx reload
 }
-# Find a free port and return it
-#
-# example: port=$(ynh_find_port 8080)
-#
-# usage: ynh_find_port begin_port
-# | arg: begin_port - port to start to search
-ynh_find_port () {
-    port=$1
-    test -n "$port" || ynh_die "The argument of ynh_find_port must be a valid port."
-    while netcat -z 127.0.0.1 $port       # Check if the port is free
-    do
-        port=$((port+1))    # Else, pass to next port
-    done
-    echo $port
-}
-
-ynh_rm_nginx_conf () {
-    if [ -e "/etc/nginx/conf.d/$domain.d/$app.conf" ]; then
-        sudo rm "/etc/nginx/conf.d/$domain.d/$app.conf"
-        sudo service nginx reload
-    fi
-}
-
-
-ynh_secure_rm () {
-    [[ "/var/www /opt /home/yunohost.app" =~ $1 ]] \
-        || (test -n "$1" && sudo rm -Rf $1 )
-}
 
 # Upgrade
 ynh_read_json () {
@@ -136,80 +60,8 @@ ynh_exit_if_up_to_date () {
 }
 
 
-# # 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}
-}
-
-# 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}';"
-}
-
-# 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
-}
-
-# 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
-}
-
-
-# Execute a command as another user
-# usage: exec_as USER COMMAND [ARG ...]
-exec_as() {
-  local USER=$1
-  shift 1
-
-  if [[ $USER = $(whoami) ]]; then
-    eval "$@"
-  else
-    # use sudo twice to be root and be allowed to use another user
-    sudo sudo -u "$USER" "$@"
-  fi
-}
-
-ynh_debian_release () {
-	lsb_release --codename --short
-}
-
 is_stretch () {
-	if [ "$(ynh_debian_release)" == "stretch" ]
+	if [ "$(ynh_get_debian_release)" == "stretch" ]
 	then
 		return 0
 	else
@@ -218,7 +70,7 @@ is_stretch () {
 }
 
 is_jessie () {
-	if [ "$(ynh_debian_release)" == "jessie" ]
+	if [ "$(ynh_get_debian_release)" == "jessie" ]
 	then
 		return 0
 	else
diff --git a/scripts/backup b/scripts/backup
index f8da399..c5c2d22 100644
--- a/scripts/backup
+++ b/scripts/backup
@@ -9,27 +9,18 @@ source ../settings/scripts/_common.sh
 
 app=$YNH_APP_INSTANCE_NAME
 domain=$(ynh_app_setting_get "$app" domain)
-odoo_version=$(ynh_app_setting_get "$app" odoo_version)
-
-define_paths
+final_path=$(ynh_app_setting_get "$app" final_path)
 
 # Backup sources & data
-ynh_backup "${source_path}addons" "$1${source_path}addons"
-mkdir -p /opt/odoo-oca-addons
-ynh_backup "/opt/odoo-oca-addons" "$1/opt/odoo-oca-addons"
+ynh_backup "${final_path}/$APPNAME"
+ynh_backup "${final_path}/custom-addons"
+ynh_backup "${final_path}/.local"
 
 # Backup configuration
-ynh_backup "$conf_file" "$1$conf_file"
+ynh_backup "$conf_file"
 
 # Copy NGINX configuration
-ynh_backup "/etc/nginx/conf.d/${domain}.d/${app}.conf" "etc/nginx/conf.d/${domain}.d/${app}.conf"
+ynh_backup "/etc/nginx/conf.d/${domain}.d/${app}.conf"
 
-# Save postgresql conf
-if is_jessie ; then
-    ynh_backup "/etc/postgresql/9.4/main/pg_hba.conf" "etc/postgresql/9.4/main/pg_hba.conf"
-else
-    ynh_backup "/etc/postgresql/9.6/main/pg_hba.conf" "etc/postgresql/9.6/main/pg_hba.conf"
-fi
 # Copy database
-database=${domain//./-}
-sudo su -c "pg_dump $database" postgres > ./dump.sql
+sudo su -c "pg_dump $app" postgres > ./dump.sql
diff --git a/scripts/install b/scripts/install
index bb5aeff..4a2f336 100644
--- a/scripts/install
+++ b/scripts/install
@@ -6,66 +6,65 @@ source /usr/share/yunohost/helpers
 source _future.sh
 source _common.sh
 
-ynh_trap_on
+ynh_abort_if_errors
 
 # Arguments from manifest
 export app=$YNH_APP_INSTANCE_NAME
-export domain=$YNH_APP_ARG_DOMAIN
+ynh_export domain oca admin_password lang tz 
 export odoo_version=$YNH_APP_ARG_VERSION
-export oca=$YNH_APP_ARG_OCA
-export admin_password=$YNH_APP_ARG_ADMIN_PASSWORD
-export lang=$YNH_APP_ARG_LANG
-export tz=$YNH_APP_ARG_TZ
 export is_public=0
+export final_path="/var/www/$app"
+export conf_file=/etc/$app/main.conf
+export bin_file="$final_path/venv/bin/python $final_path/$APPNAME/$APPNAME-bin"
+if [ "$odoo_version" = "9" ]; then
+    bin_file="$final_path/venv/bin/python $final_path/$APPNAME/$APPNAME.py"
+fi
+if [ "$odoo_version" = "8" ]; then
+    bin_file="$final_path/venv/bin/python $final_path/$APPNAME/$APPNAME.py"
+fi
+export db_name=$(ynh_sanitize_dbid $app)
+export port=$(ynh_find_port 8069)
+export port_chat=$(ynh_find_port 8072)
 
 #=================================================
 # CHECK IF THE APP CAN BE INSTALLED WITH THIS ARGS
 #=================================================
-ynh_check_var "$app" "app name not set"
-ynh_webpath_available "$domain" "/"
-check_odoo_version
-define_paths
-define_is_master
-define_port
-define_dbpass
+test ! -e "$final_path" || ynh_die "This path already contains a folder"
+ynh_webpath_available $domain "/"
+ynh_webpath_register $app $domain "/"
 
 
 #=================================================
 # SETUP THE APP BY MODIFYING THE SYSTEM
 #=================================================
 function setup_files() {
-    # Set admin password
-    export database=${domain//./-}
-    ynh_configure openerp-server.conf $conf_file
-    chown odoo:odoo $conf_file
+   
+    ynh_setup_source $final_path/$APPNAME $odoo_version
+    mkdir -p $final_path/custom-addons
+    chown -R $app:$app $final_path
+    ynh_configure server.conf $conf_file
+    chown $app:$app $conf_file
 
     # Autoinstall the LDAP auth module
-    if [ $(echo "$odoo_version >= 10" | bc) -ne 0 ]; then
-        ynh_replace_string "^{$" "{'auto_install': True," ${source_path}addons/auth_ldap/__manifest__.py
+    if ls $final_path/$APPNAME/$APPNAME-bin > /dev/null ; then
+        ynh_replace_string "^{$" "{'auto_install': True," ${final_path}/$APPNAME/addons/auth_ldap/__manifest__.py
     else
-        ynh_replace_string "'auto_install': False" "'auto_install': True" ${source_path}addons/auth_ldap/__openerp__.py
+        ynh_replace_string "'auto_install': False" "'auto_install': True" ${final_path}/$APPNAME/addons/auth_ldap/__openerp__.py
     fi
-
-    # Fix peer authentification issue
-    sed -i '/local\s*all\s*all\s*peer/i \
-    local all odoo password' /etc/postgresql/9.*/main/pg_hba.conf
+    
+    touch /var/log/$app.log
+    chown $app:$app /var/log/$app.log
 
     ynh_configure_nginx
 }
 
 function setup_database() {
 
-    # Setup database: not working
-    database=${domain//./-}
-
-    if [ $(echo "$odoo_version <= 8" | bc) -ne 0 ]; then
-        su -c "createdb -O $APPNAME $database" -  postgres
-    fi
     # Load translation
-    sudo -u odoo $bin_file -c $conf_file --stop-after-init -i auth_ldap -d $database
-    sudo -u odoo $bin_file -c $conf_file --stop-after-init -d $database --load-language $lang
+    sudo -u $app $bin_file -c $conf_file --stop-after-init -i auth_ldap -d $app
+    sudo -u $app $bin_file -c $conf_file --stop-after-init -d $app --load-language $lang
     # Configure language, timezone and ldap
-    sudo -u odoo $bin_file shell -c $conf_file -d $database <<< \
+    sudo -u $app $bin_file shell -c $conf_file -d $app <<< \
 "
 self.env['res.users'].search([['login', '=', 'admin']])[0].write({'password': '$admin_password'})
 self.env.cr.commit()
@@ -92,12 +91,13 @@ self.company_id.ldaps.create({
 self.env.cr.commit()
 "
 }
-
-ynh_webpath_register "$app" "$domain" "/"
-ynh_save_args domain odoo_version oca lang tz is_master port
-install_dependencies
+ynh_save_args domain odoo_version oca lang tz final_path conf_file db_name port port_chat
+ynh_psql_test_if_first_run
+ynh_psql_setup_db $db_name $db_name
+export db_pass=$(ynh_app_setting_get $app psqlpwd)
+ynh_system_user_create $app $final_path
 setup_files
-create_general_db
+install_dependencies
 add_services
 setup_database
 ssowat_and_restart
diff --git a/scripts/remove b/scripts/remove
index 973848d..47a4418 100644
--- a/scripts/remove
+++ b/scripts/remove
@@ -10,47 +10,19 @@ app=$YNH_APP_INSTANCE_NAME
 
 # Retrieve app settings
 domain=$(ynh_app_setting_get "$app" domain)
-odoo_version=$(ynh_app_setting_get "$app" odoo_version)
-if [ "$odoo_version" = "" ]; then
-    exit 0
-fi
+final_path=$(ynh_app_setting_get "$app" final_path)
 database=${domain//./-}
-is_master=$(ynh_app_setting_get "$app" is_master)
 
-define_paths
+yunohost service stop $app
+yunohost service remove $app
+ynh_psql_remove_db $app $app
 
-yunohost service stop odoo
-list=$(yunohost app list --installed | grep odoo)
-# If removing master instance, remove Odoo.
-if [ $(echo $list | wc -l) = 1 ]; then
-    rm -f /etc/apt/sources.list.d/odoo.list
-    apt-get update
-	yunohost service remove odoo
-    ynh_psql_drop_db $APPNAME
-    #ynh_psql_drop_user $APPNAME
-else
-    # Set another app as master
-    if [ "$is_master" = "true" ]; then
-        for instance in $list; do
-            if [ "$instance" != "$app" ]; then
-                ynh_app_setting_set "$instance" is_master "true"
-                break
-            fi
-        done
-        is_master="false"
-    fi
-fi
+ynh_secure_remove "$final_path"
 
 ynh_remove_app_dependencies
 
-# Remove database
-ynh_psql_drop_db $database
+ynh_system_user_delete $app
 
-if [ "$is_master" = "false" ]; then
-    yunohost service start odoo
-fi
-
-# Remove NGINX conf
-ynh_rm_nginx_conf
+ynh_remove_nginx_config
 
 ynh_del_swap
diff --git a/scripts/restore b/scripts/restore
index f6c0b5c..2ef7bae 100644
--- a/scripts/restore
+++ b/scripts/restore
@@ -6,59 +6,44 @@ source /usr/share/yunohost/helpers
 source ../settings/scripts/_future.sh
 source ../settings/scripts/_common.sh
 
-ynh_trap_on
+ynh_abort_if_errors
 
 export app=$YNH_APP_INSTANCE_NAME
 export domain=$(ynh_app_setting_get $app domain)
 export odoo_version=$(ynh_app_setting_get $app odoo_version)
 export oca=$(ynh_app_setting_get $app oca)
 export port=$(ynh_app_setting_get $app port)
+export port_chat=$(ynh_app_setting_get $app port_chat)
+export db_name=$(ynh_app_setting_get $app db_name)
 export is_public=0
 
 #=================================================
 # CHECK IF THE APP CAN BE RESTORED
 #=================================================
-ynh_check_var "$app" "app name not set"
 ynh_webpath_available "$domain" "/"
-check_odoo_version
-define_paths
-define_is_master
-define_port
-define_dbpass restore
 
 #=================================================
 # RESTORE THE APP BY MODIFYING THE SYSTEM
 #=================================================
 # Restore files
 function restore_files () {
-    if [ "$is_master" = true ]; then
-        ynh_restore
-        chown -R odoo:odoo /var/lib/odoo/
-        chown postgres:postgres /etc/postgresql/9.*/main/pg_hba.conf
-    else
-        # If odoo is already setup we just restore nginx and add some missing
-        # modules. Other files are already on system, and we don't want to
-        # erase it
-
-        # Configure Nginx and reload
-        ynh_restore_file "/etc/nginx/conf.d/${domain}.d/${app}.conf"
-
-        # TODO Restore plugin/addons
-    fi
+    ynh_restore
+    chown -R $app:$app $final_path
+    chown $app:$app $conf_file
+    touch /var/log/$app.log
+    chown $app:$app /var/log/$app.log
 }
 
 # Restore database
 function restore_database () {
-    # Restore database
-    database=${domain//./-}
-    su -c "createdb -O $APPNAME $database" -  postgres
-    su -c "psql $database" postgres < ./dump.sql
+    su -c "psql $app" postgres < ./dump.sql
 }
 
-ynh_save_args is_master port
-install_dependencies
+ynh_psql_test_if_first_run
+ynh_psql_setup_db $db_name $db_name $db_pass
+ynh_system_user_create $app $final_path
 restore_files
-create_general_db
+install_dependencies
 add_services
 restore_database
 ssowat_and_restart
diff --git a/scripts/upgrade b/scripts/upgrade
index b0948a1..bb5bcee 100644
--- a/scripts/upgrade
+++ b/scripts/upgrade
@@ -46,11 +46,6 @@ ynh_clean_setup () {
 # Exit if an error occurs during the execution of the script
 ynh_abort_if_errors
 
-#=================================================
-# CHECK THE PATH
-#=================================================
-
-path_url=$(ynh_normalize_url_path $path_url)	# Vérifie et corrige la syntaxe du path.
 
 #=================================================
 # STEP Migrations
@@ -59,13 +54,6 @@ path_url=$(ynh_normalize_url_path $path_url)	# Vérifie et corrige la syntaxe du
 if ynh_version_le "9.0-0" ; then
     # set new args
     odoo_version=${odoo_version:-$(ynh_app_setting_get "$app" version)}
-    if [ "$is_master" = "" ]; then
-        if [ "$(ynh_app_setting_get "$app" instance)" = "master" ];then
-            is_master="true"
-        else
-            is_master="false"
-        fi
-    fi
     port=${port:-8069}
     oca=${oca:-false}
     ynh_save_args odoo_version oca is_master port
@@ -80,8 +68,15 @@ if ynh_version_le "11.0-1" ; then
     ynh_save_args major_upgrade
 fi
 
+ynh_system_user_create $app
+ynh_setup_source "$final_path/$APPNAME" $odoo_version
+mkdir -p $final_path/custom-addons
+chown -R $app:$app $final_path
+ynh_configure server.conf $conf_file
+chown $app:$app $conf_file
+    
+touch /var/log/$app.log
+chown $app:$app /var/log/$app.log
 
-# Upgrade official package
-apt-get install odoo -y -qq
-
-
+install_dependencies
+service $app restart