diff --git a/scripts/_common.sh b/scripts/_common.sh new file mode 100644 index 0000000..54ff881 --- /dev/null +++ b/scripts/_common.sh @@ -0,0 +1,105 @@ +# +# Common variables +# + +APPNAME="odoo" + + +# Package name for dependencies +DEPS_PKG_NAME="${APPNAME}-deps" + +# Remote URL to fetch tarball +SOURCE_URL="http://download.gna.org/wkhtmltopdf/0.12/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz" + +# Remote URL to fetch tarball checksum +SOURCE_SHA256="049b2cdec9a8254f0ef8ac273afaf54f7e25459a273e27189591edc7d7cf29db" + +# App package root directory should be the parent folder +PKGDIR=$(cd ../; pwd) + +# +# Common helpers +# + +# Create a user +# +# usage: ynh_mysql_create_user user pwd [host] +# | arg: user - the user name to create +# | arg: pwd - the password to identify user by +ynh_psql_create_user() { + sudo su -c "psql" postgres <<< \ + "CREATE USER ${1} WITH PASSWORD '${2}';" +} + +# Create a database and grant optionnaly privilegies to a user +# +# usage: ynh_mysql_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_mysql_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_mysql_drop_user user +# | arg: user - the user name to drop +ynh_psql_drop_user() { + sudo su -c "dropuser ${1}" postgres +} + + +# Download and extract sources to the given directory +# usage: extract_sources DESTDIR [AS_USER] +extract_sources() { + local DESTDIR=$1 + local AS_USER=${2:-admin} + + # retrieve and extract Roundcube tarball + tarball="/tmp/${APPNAME}.tar.gz" + rm -f "$tarball" + wget -q -O "$tarball" "$SOURCE_URL" \ + || ynh_die "Unable to download tarball" + echo "$SOURCE_SHA256 $tarball" | sha256sum -c >/dev/null \ + || ynh_die "Invalid checksum of downloaded tarball" + exec_as "$AS_USER" tar xzf "$tarball" -C "$DESTDIR" --strip-components 1 \ + || ynh_die "Unable to extract tarball" + rm -f "$tarball" + + # apply patches + (cd "$DESTDIR" \ + && for p in ${PKGDIR}/patches/*.patch; do \ + exec_as "$AS_USER" patch -p1 < $p; done) \ + || ynh_die "Unable to apply patches" +} + +# 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 +} diff --git a/scripts/install b/scripts/install index 3a7c996..f584296 100644 --- a/scripts/install +++ b/scripts/install @@ -11,6 +11,9 @@ admin_password=$YNH_APP_ARG_ADMIN_PASSWORD database_password=$YNH_APP_ARG_DATABASE_PASSWORD lang=$YNH_APP_ARG_LANG +# Load common variables +source ./_common.sh + # Source YunoHost helpers . /usr/share/yunohost/helpers @@ -52,12 +55,19 @@ else # Initialize installation ynh_package_install postgresql - sudo su -c "psql" postgres <<< \ - "CREATE USER $app WITH PASSWORD '$database_password' CREATEDB;" + # This db may be unused + dbuser=$app + dbname=$app + ynh_psql_create_db "$dbname" "$dbuser" "$database_password" ynh_package_install odoo # Install wkhtmltopdf to print PDF reports - ynh_package_install wkhtmltopdf + ynh_package_install xfonts-75dpi xfonts-base wkhtmltopdf + + # The debian package has a bug so we deploy a more recent version + extract_sources "/usr/" "$app" + + # Set admin password # TODO Support @ in password @@ -79,22 +89,6 @@ else sudo yunohost service start odoo fi -ynh_psql_create_user() { - sudo su -c "psql" postgres <<< \ - "CREATE USER ${1} WITH PASSWORD '${2}';" -} -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 - -} - # Configure Nginx and reload sed -i "s@#YNH_APP_INSTANCE_NAME#@${YNH_APP_INSTANCE_NAME}@g" ../conf/nginx.conf sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/$app.conf diff --git a/scripts/remove b/scripts/remove index 3fbc58a..686b6dd 100644 --- a/scripts/remove +++ b/scripts/remove @@ -2,6 +2,10 @@ set -eu app=$YNH_APP_INSTANCE_NAME + +# Load common variables +source ./_common.sh + # Source YunoHost helpers . /usr/share/yunohost/helpers