diff --git a/README.md b/README.md index 53160c9..9623955 100644 --- a/README.md +++ b/README.md @@ -1,36 +1,31 @@ -# rocketchat_ynh - -Rocketchat for yunohost +# RocketChat for YunoHost [![Integration level](https://dash.yunohost.org/integration/rocketchat.svg)](https://dash.yunohost.org/appci/app/rocketchat) [![Install Rocketchat with YunoHost](https://install-app.yunohost.org/install-with-yunohost.png)](https://install-app.yunohost.org/?app=rocketchat) -## Install with -You can either : - -* Install from the Yunohost Admin web interface, by providing this URL: [https://github.com/YunoHost-Apps/rocketchat_ynh](https://github.com/YunoHost-Apps/rocketchat_ynh) -* Install from the command-line: `yunohost app install https://github.com/YunoHost-Apps/rocketchat_ynh` - -**Please note that currently the installation does not work on raspberry pi or arm architecture in general.** - -## Backup information - -Backup stores: - -- rocketchat app dir -- rocketchat mongodb (via mongodump) -- nginx configuration -- systemd service configuration #### Supported architectures -* x86-64b - [![](https://ci-apps.yunohost.org/ci/logs/rocketchat%20%28Community%29.svg)](https://ci-apps.yunohost.org/ci/apps/rocketchat/) +* x86-64 - [![](https://ci-apps.yunohost.org/ci/logs/rocketchat%20%28Community%29.svg)](https://ci-apps.yunohost.org/ci/apps/rocketchat/) * ARMv8-A - [![](https://ci-apps-arm.yunohost.org/ci/logs/rocketchat%20%28Community%29.svg)](https://ci-apps-arm.yunohost.org/ci/apps/rocketchat/) -* Jessie x86-64b - [![](https://ci-stretch.nohost.me/ci/logs/rocketchat%20%28Community%29.svg)](https://ci-stretch.nohost.me/ci/apps/rocketchat/) ## Links - * Report a bug: [Issues](/../../issues) + * Report a bug: https://github.com/YunoHost-Apps/rocketchat_ynh/issues * Rocketchat website: https://rocket.chat/ + * Upstream app repository: https://github.com/RocketChat/Rocket.Chat * YunoHost website: https://yunohost.org/ + +--- + +## Developer info + +Please send your pull request to the [testing branch](https://github.com/YunoHost-Apps/rocketchat_ynh/tree/testing). + +To try the testing branch, please proceed like that. +``` +sudo yunohost app install https://github.com/YunoHost-Apps/rocketchat_ynh/tree/testing --debug +or +sudo yunohost app upgrade rocketchat -u https://github.com/YunoHost-Apps/rocketchat_ynh/tree/testing --debug +``` diff --git a/conf/app.src b/conf/app.src index 5e807eb..062b930 100644 --- a/conf/app.src +++ b/conf/app.src @@ -1,7 +1,7 @@ -SOURCE_URL=https://releases.rocket.chat/0.73.2/download -SOURCE_SUM=3dc3eb11f383f7b72b0f23fedb305b6a566fa536a1e5087a4398255deeb864d8 +SOURCE_URL=https://github.com/RocketChat/Rocket.Chat/archive/3.4.3.tar.gz +SOURCE_SUM=b586b1a3ad0ca22ed563fa9339058b481548544c4a9719fb955044496b65a418 SOURCE_SUM_PRG=sha256sum SOURCE_FORMAT=tar.gz SOURCE_IN_SUBDIR=true -SOURCE_FILENAME=rocket.chat-0.73.2.tgz +SOURCE_FILENAME=Rocket.Chat-3.4.3.tar.gz SOURCE_EXTRACT=true \ No newline at end of file diff --git a/conf/nginx.conf b/conf/nginx.conf index e29a459..dd08124 100644 --- a/conf/nginx.conf +++ b/conf/nginx.conf @@ -1,20 +1,19 @@ -location ^~ #LOCATION# { +#sub_path_only rewrite ^__PATH__$ __PATH__/ permanent; +location ^~ __PATH__/ { - # Force HTTPS - if ($scheme = http) { - rewrite ^ https://$server_name$request_uri? permanent; - } + proxy_pass http://127.0.0.1:__PORT__/; + proxy_redirect off; + 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 $server_name; - proxy_pass http://localhost:#PORT##PATH#; proxy_http_version 1.1; - proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; - proxy_set_header Host $http_host; + proxy_set_header Upgrade $http_upgrade; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forward-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forward-Proto http; - proxy_set_header X-Nginx-Proxy true; - - proxy_redirect off; + # Include SSOWAT user panel. + include conf.d/yunohost_panel.conf.inc; + more_clear_input_headers 'Accept-Encoding'; } diff --git a/conf/rocketchat.service b/conf/rocketchat.service deleted file mode 100644 index 7afc22b..0000000 --- a/conf/rocketchat.service +++ /dev/null @@ -1,18 +0,0 @@ -[Unit] -Description=rocketchat -After=network.target - -[Service] -Type=simple -WorkingDirectory=#DESTDIR# -User=#USER# -ExecStart=#NODE# main.js -Restart=always -SyslogIdentifier=rocketchat -Environment="BIND_IP=127.0.0.1" -Environment="ROOT_URL=https://#ROOTURL##LOCATION#" -Environment="PORT=#PORT#" -Environment="MONGO_URL=mongodb://localhost:27017/#DBNAME#" - -[Install] -WantedBy=multi-user.target diff --git a/conf/rocketchat_ldap.js b/conf/rocketchat_ldap.js index ccb0f77..62293ff 100644 --- a/conf/rocketchat_ldap.js +++ b/conf/rocketchat_ldap.js @@ -1,4 +1,4 @@ -use #DBNAME# +use __DB_NAME__ db.rocketchat_settings.update( diff --git a/conf/systemd.service b/conf/systemd.service new file mode 100644 index 0000000..b68fc08 --- /dev/null +++ b/conf/systemd.service @@ -0,0 +1,19 @@ +[Unit] +Description=The Rocket.Chat server +After=network.target remote-fs.target nss-lookup.target nginx.target mongod.target + +[Service] +User=__APP__ +WorkingDirectory=__FINALPATH__ +Environment="PATH=__ENV_PATH__" +Environment="NODE_ENV=production" +ExecStart=__YNH_NODE__ main.js + +StandardOutput=syslog +StandardError=syslog +SyslogIdentifier=rocketchat + +Environment=MONGO_URL=mongodb://localhost:27017/rocketchat?replicaSet=rs01 MONGO_OPLOG_URL=mongodb://localhost:27017/local?replicaSet=rs01 ROOT_URL=http://__DOMAIN__:__PORT__/ PORT=__PORT__ + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/manifest.json b/manifest.json index 0ce0e4f..a3c1644 100644 --- a/manifest.json +++ b/manifest.json @@ -1,13 +1,14 @@ { - "packaging_format": 1, "name": "RocketChat", "id": "rocketchat", + "packaging_format": 1, "description": { - "en": "the ultimate chat platform" + "en": "the ultimate chat platform", + "fr": "the ultimate chat platform" }, "url": "https://rocket.chat", "license": "GPL-3.0", - "version": "0.73.2", + "version": "3.4.2~ynh1", "maintainer": { "name": "selamanse", "email": "selamanse@scheinfrei.info", @@ -18,7 +19,7 @@ "nginx" ], "requirements": { - "yunohost": ">> 2.5.6" + "yunohost": ">> 3.8.1" }, "arguments": { "install" : [ @@ -26,7 +27,8 @@ "name": "domain", "type": "domain", "ask": { - "en": "Choose a domain for Rocketchat" + "en": "Choose a domain for RocketChat", + "fr": "Choose a domain for RocketChat" }, "example": "domain.org" }, @@ -34,7 +36,8 @@ "name": "path", "type": "path", "ask": { - "en": "Choose a path for RocketChat" + "en": "Choose a path for RocketChat", + "fr": "Choose a path for RocketChat" }, "example": "/rocketchat", "default": "/rocketchat" @@ -43,15 +46,17 @@ "name": "is_public", "type": "boolean", "ask": { - "en": "Is it a public application?" + "en": "Is it a public application?", + "fr": "Is it a public application ?" }, - "default": "Yes" + "default": true }, { "name": "lang", "type": "string", "ask": { - "en": "Select default language" + "en": "Select default language", + "fr": "Select default language" }, "choices": ["English"], "default": "English" diff --git a/scripts/_common.sh b/scripts/_common.sh index eddb1d4..1799b2e 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -1,82 +1,588 @@ -# common.sh +#!/bin/bash -ROCKETCHAT_VERSION=0.73.2 -ROCKETCHAT_SHASUM=3dc3eb11f383f7b72b0f23fedb305b6a566fa536a1e5087a4398255deeb864d8 -ROCKETCHAT_DOWNLOAD_URI=https://releases.rocket.chat/${ROCKETCHAT_VERSION}/download -NODE_VERSION=8.11.4 -DEBIAN_ISSUE=$(grep 9 /etc/debian_version >/dev/null && echo stretch || echo jessie) +#================================================= +# COMMON VARIABLES +#================================================= -checkcmd() { - curl -m 1 -s localhost:$port$path/api/v1/info | \ - python -c "import sys, json; print json.load(sys.stdin)['success']" 2>/dev/null | \ - grep "True" >/dev/null 2>&1 +# dependencies used by the app +#pkg_dependencies="apt-transport-https build-essential gzip curl graphicsmagick" dirmngr + +nodejs_version=12 + +# dependencies used by the app +pkg_dependencies="mongodb mongodb-server mongo-tools apt-transport-https build-essential gzip curl graphicsmagick " +pkg_dependencies_buster="mongodb-org mongodb-org-server mongodb-org-tools" + +mongodb_stretch="mongodb" +mongodb_buster="mongod" + +#================================================= +# PERSONAL HELPERS +#================================================= + +#================================================= +# EXPERIMENTAL HELPERS +#================================================= + +# Evaluate a mongo command +# +# example: ynh_mongo_eval_as --command='db.getMongo().getDBNames().indexOf("wekan")' +# example: ynh_mongo_eval_as --command="db.getMongo().getDBNames().indexOf(\"wekan\")" +# +# usage: ynh_mongo_eval_as [--user=user] [--password=password] [--authenticationdatabase=authenticationdatabase] [--database=database] [--host=host] [--port=port] --command="command" +# | arg: -u, --user= - the user name to connect as +# | arg: -p, --password= - the user password +# | arg: -d, --authenticationdatabase= - the authenticationdatabase to connect to +# | arg: -d, --database= - the database to connect to +# | arg: -h, --host= - the host to connect to +# | arg: -P, --port= - the port to connect to +# | arg: -c, --command= - the command to evaluate +# +# +ynh_mongo_eval_as() { + # Declare an array to define the options of this helper. + local legacy_args=upadhPc + local -A args_array=( [u]=user= [p]=password= [a]=authenticationdatabase= [d]=database= [h]=host= [P]=port= [c]=command= ) + local user + local password + local authenticationdatabase + local database + local host + local port + local command + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + user="${user:-}" + password="${password:-}" + authenticationdatabase="${authenticationdatabase:-"admin"}" + database="${database:-"admin"}" + host="${host:-}" + port="${port:-}" + + # If user is provided + if [ -n "$user" ] + then + user="--username=$user" + password="--password=$password" + authenticationdatabase="--authenticationDatabase=$authenticationdatabase" + else + password="" + authenticationdatabase="" + fi + + # Configure dabase connection + database="use $database" + + # If host is provided + if [ -n "$host" ] + then + host="--host=$host" + fi + + # If host is provided + if [ -n "$port" ] + then + port="--port=$port" + fi + + mongo --quiet $user $password $authenticationdatabase $host $port < ./dump.bson +# +# usage: ynh_mongo_dump_db --database=database +# | arg: -d, --database= - the database name to dump +# | ret: the mongodump output +# +# +ynh_mongo_dump_db() { + # Declare an array to define the options of this helper. + local legacy_args=d + local -A args_array=( [d]=database= ) + local database + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + mongodump --quiet --db="$database" --archive +} + +# Create a user +# +# [internal] +# +# usage: ynh_mongo_create_user --db_user=db_user --db_pwd=db_pwd --db_name=db_name +# | arg: -u, --db_user= - the user name to create +# | arg: -p, --db_pwd= - the password to identify user by +# | arg: -n, --db_name= - Name of the database to grant privilegies +# +# +ynh_mongo_create_user() { + # Declare an array to define the options of this helper. + local legacy_args=unp + local -A args_array=( [u]=db_user= [n]=db_name= [p]=db_pwd= ) + local db_user + local db_name + local db_pwd + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + # Create the user and set the user as admin of the db + ynh_mongo_eval_as --database="$db_name" --command='db.createUser( { user: "'${db_user}'", pwd: "'${db_pwd}'", roles: [ { role: "readWrite", db: "'${db_name}'" } ] } );' + + # Add clustermonitoring rights + ynh_mongo_eval_as --database="$db_name" --command='db.grantRolesToUser("'${db_user}'",[{ role: "clusterMonitor", db: "admin" }]);' +} + +# Check if a mongo database exists +# +# usage: ynh_mongo_database_exists --database=database +# | arg: -d, --database= - the database for which to check existence +# | exit: Return 1 if the database doesn't exist, 0 otherwise +# +# +ynh_mongo_database_exists() { + # Declare an array to define the options of this helper. + local legacy_args=d + local -A args_array=([d]=database=) + local database + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + if [ $(ynh_mongo_eval_as --command='db.getMongo().getDBNames().indexOf("'${database}'")') -lt 0 ] + then + return 0 + else + return 1 + fi +} + +# Restore a database +# +# example: ynh_mongo_restore_db --database=wekan < ./dump.bson +# +# usage: ynh_mongo_restore_db --database=database +# | arg: -d, --database= - the database name to restore +# +# +ynh_mongo_restore_db() { + # Declare an array to define the options of this helper. + local legacy_args=d + local -A args_array=( [d]=database= ) + local database + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + mongorestore --quiet --db="$database" --archive +} + +# Drop a user +# +# [internal] +# +# usage: ynh_mongo_drop_user --db_user=user +# | arg: -u, --db_user= -the user to drop +# +# +ynh_mongo_drop_user() { + # Declare an array to define the options of this helper. + local legacy_args=u + local -A args_array=( [u]=db_user= ) + local db_user + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + ynh_mongo_eval_as --command='db.dropUser("'${db_user}'", {w: "majority", wtimeout: 5000})' +} + +# Create a database, an user and its password. Then store the password in the app's config +# +# usage: ynh_mongo_setup_db --db_user=user --db_name=name [--db_pwd=pwd] +# | arg: -u, --db_user= - Owner of the database +# | arg: -n, --db_name= - Name of the database +# | arg: -p, --db_pwd= - Password of the database. If not provided, a password will be generated +# +# After executing this helper, the password of the created database will be available in $db_pwd +# It will also be stored as "mongopwd" into the app settings. +# +# +ynh_mongo_setup_db() { + # Declare an array to define the options of this helper. + local legacy_args=unp + local -A args_array=( [u]=db_user= [n]=db_name= [p]=db_pwd= ) + local db_user + local db_name + local db_pwd + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + local new_db_pwd=$(ynh_string_random) # Generate a random password + # If $db_pwd is not provided, use new_db_pwd instead for db_pwd + db_pwd="${db_pwd:-$new_db_pwd}" + + # Create the user and grant access to the database + ynh_mongo_create_user --db_user="$db_user" --db_pwd="$db_pwd" --db_name="$db_name" + + ynh_app_setting_set --app=$app --key=db_pwd --value=$db_pwd +} + +# Remove a database if it exists, and the associated user +# +# usage: ynh_mongo_remove_db --db_user=user --db_name=name +# | arg: -u, --db_user= - Owner of the database +# | arg: -n, --db_name= - Name of the database +# +# +ynh_mongo_remove_db() { + # Declare an array to define the options of this helper. + local legacy_args=un + local -A args_array=( [u]=db_user= [n]=db_name= ) + local db_user + local db_name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + if ynh_mongo_database_exists --database=$db_name + then # Check if the database exists + ynh_mongo_drop_db --database="$db_name" # Remove the database + else + ynh_print_warn --message="Database $db_name not found" + fi + + # Remove mongo user if it exists + ynh_mongo_drop_user --db_user=$db_user +} + + + + + + +read_json () { + sudo python3 -c "import sys, json;print(json.load(open('$1'))['$2'])" +} + +read_manifest () { + if [ -f '../manifest.json' ] ; then + read_json '../manifest.json' "$1" + else + read_json '../settings/manifest.json' "$1" + fi +} + +abort_if_up_to_date () { + version=$(read_json "/etc/yunohost/apps/$YNH_APP_INSTANCE_NAME/manifest.json" 'version' 2> /dev/null || echo '20160501-7') + last_version=$(read_manifest 'version') + if [ "${version}" = "${last_version}" ]; then + ynh_script_progression --message="Up-to-date, nothing to do" + ynh_die "" 0 + fi +} + +ynh_version_gt () +{ + dpkg --compare-versions "$1" gt "$2" +} + + + + + +# Check the architecture +# +# example: architecture=$(ynh_detect_arch) +# +# usage: ynh_detect_arch +# +# Requires YunoHost version 2.2.4 or higher. + +ynh_detect_arch(){ + local architecture + if [ -n "$(uname -m | grep arm64)" ] || [ -n "$(uname -m | grep aarch64)" ]; then + architecture="arm64" + elif [ -n "$(uname -m | grep 64)" ]; then + architecture="x86-64" + elif [ -n "$(uname -m | grep 86)" ]; then + architecture="i386" + elif [ -n "$(uname -m | grep arm)" ]; then + architecture="arm" else - ynh_die "$app could not be started" + architecture="unknown" fi + echo $architecture } -installnode(){ - if [ $DEBIAN_ISSUE == "stretch" ]; then - sudo curl -sL https://deb.nodesource.com/setup_8.x -o nodesource_setup.sh - sudo bash nodesource_setup.sh - sudo apt-get install -y nodejs - else - sudo apt-get install -y npm - # Using npm install inherits and n, and the node version required by Rocket.Chat: - sudo npm install -g inherits n - sudo n $NODE_VERSION - fi - echo "node version is now: " - node --version + +# Create a dedicated config file +# +# usage: ynh_add_config [--origin="origin file"] [--destination="destination file"] [--vars="vars to replace"] +# | arg: -o, --origin= - Template config file to use (optionnal, ../conf/.env by default) +# | arg: -d, --destination= - Destination of the config file (optionnal, $final_path/.env) +# | arg: -v, --vars= - List of variables to replace separated by a space. For example: 'var_1 var_2 ...' +# +# This will use the template $origin or ../conf/.env by default +# to generate a config file $destination or $final_path/.env by default, +# by replacing the following keywords with global variables +# that should be defined before calling this helper : +# __PATH__ by $path_url +# __DOMAIN__ by $domain +# __PORT__ by $port +# __NAME__ by $app +# __NAMETOCHANGE__ by $app +# __USER__ by $app +# __APP__ by $app +# __FINALPATH__ by $final_path +# __PHPVERSION__ by $YNH_PHP_VERSION +# __YNH_NPM__ by $ynh_npm +# __YNH_NODE__ by $ynh_node +# __YNH_NODE_LOAD_PATH__ by $ynh_node_load_PATH +# __NODEJS_PATH__ by $nodejs_path +# __NODEJS_VERSION__ by $nodejs_version +# __DB_NAME__ by $db_name +# __DB_USER__ by $db_user +# __DB_PWD__ by $db_pwd +# +# And dynamic variables (from the last example) : +# __VAR_1__ by $var_1 +# __VAR_2__ by $var_2 +# +# The helper will verify the checksum and backup the destination file +# if it's different before applying the new origin file. +# And it will calculate and store a destination file checksum +# into the app settings when configuraation is done. +# +ynh_add_config () { + # Declare an array to define the options of this helper. + local legacy_args=odv + local -A args_array=( [o]=origin= [d]=destination= [v]=vars= ) + local origin + local destination + local vars + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + local origin="${origin:-"../conf/.env"}" + local destination="${destination:-"$final_path/.env"}" + local vars="${vars:-}" + + ynh_backup_if_checksum_is_different --file="$destination" + cp "$origin" "$destination" + + ynh_replace_vars --file="$destination" --vars="$vars" + + ynh_store_file_checksum --file="$destination" } -installdeps(){ +# Remove the dedicated config +# +# usage: ynh_remove_config [--file=file] +# | arg: -f, --file= - Config file to remove (optionnal, $final_path/.env) +# +# +ynh_remove_config () { + # Declare an array to define the options of this helper. + local legacy_args=f + local -A args_array=( [f]=file= ) + local file + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + local service="${file:-}" - if [ $(dpkg --print-architecture) == "armhf" ]; then - #Install mongodb for debian armhf - sudo apt-get update - sudo apt-get install -y mongodb-server + # The default behaviour is to use .env file. + if [ -n "$file" ]; then + file="final_path/.env" + fi - # start mongodb service - sudo systemctl enable mongodb.service - sudo systemctl start mongodb.service - - # add mongodb to services - sudo yunohost service add mongodb -l /var/log/mongodb/mongodb.log - else - #Install mongodb for debian x86/x64 - sudo apt-get install dirmngr && sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4 - echo "deb http://repo.mongodb.org/apt/debian ${DEBIAN_ISSUE}/mongodb-org/4.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list - sudo apt-get update - sudo apt-get install -y mongodb-org - - # start mongodb service - sudo systemctl enable mongod.service - sudo systemctl start mongod.service - - # add mongodb to services - sudo yunohost service add mongod -l /var/log/mongodb/mongod.log - fi - - #Install other dependencies - sudo apt-get install -y build-essential gzip curl graphicsmagick - - installnode + if [ -e "$file" ] + then + ynh_secure_remove --file="$file" + fi } + +# Replace variables in a file +# +# usage: ynh_replace_vars --file="file" [--vars="vars to replace"] +# | arg: -o, --file= - Template config file to use +# | arg: -v, --vars= - List of variables to replace separated by a space. For example: 'var_1 var_2 ...' +# +# This will replace in the the following keywords with global variables +# that should be defined before calling this helper : +# __PATH__ by $path_url +# __DOMAIN__ by $domain +# __PORT__ by $port +# __NAME__ by $app +# __NAMETOCHANGE__ by $app +# __USER__ by $app +# __APP__ by $app +# __FINALPATH__ by $final_path +# __PHPVERSION__ by $YNH_PHP_VERSION +# __YNH_NPM__ by $ynh_npm +# __YNH_NODE__ by $ynh_node +# __YNH_NODE_LOAD_PATH__ by $ynh_node_load_PATH +# __NODEJS_PATH__ by $nodejs_path +# __NODEJS_VERSION__ by $nodejs_version +# __DB_NAME__ by $db_name +# __DB_USER__ by $db_user +# __DB_PWD__ by $db_pwd +# +# And dynamic variables (from the last example) : +# __VAR_1__ by $var_1 +# __VAR_2__ by $var_2 +# +# +ynh_replace_vars () { + # Declare an array to define the options of this helper. + local legacy_args=fv + local -A args_array=( [f]=file= [v]=vars= ) + local file + local vars + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + local file="${file:-}" + local vars="${vars:-}" + + + #Replace usual YunoHost variables + if test -n "${path_url:-}" + then + # path_url_slash_less is path_url, or a blank value if path_url is only '/' + local path_url_slash_less=${path_url%/} + ynh_replace_string --match_string="__PATH__/" --replace_string="$path_url_slash_less/" --target_file="$file" + ynh_replace_string --match_string="__PATH__" --replace_string="$path_url" --target_file="$file" + fi + if test -n "${domain:-}"; then + ynh_replace_string --match_string="__DOMAIN__" --replace_string="$domain" --target_file="$file" + fi + if test -n "${port:-}"; then + ynh_replace_string --match_string="__PORT__" --replace_string="$port" --target_file="$file" + fi + if test -n "${app:-}"; then + ynh_replace_string --match_string="__NAME__" --replace_string="$app" --target_file="$file" + ynh_replace_string --match_string="__NAMETOCHANGE__" --replace_string="$app" --target_file="$file" + ynh_replace_string --match_string="__USER__" --replace_string="$app" --target_file="$file" + ynh_replace_string --match_string="__APP__" --replace_string="$app" --target_file="$file" + fi + if test -n "${final_path:-}"; then + ynh_replace_string --match_string="__FINALPATH__" --replace_string="$final_path" --target_file="$file" + fi + if test -n "${YNH_PHP_VERSION:-}"; then + ynh_replace_string --match_string="__PHPVERSION__" --replace_string="$YNH_PHP_VERSION" --target_file="$file" + fi + if test -n "${ynh_npm:-}"; then + ynh_replace_string --match_string="__YNH_NPM__" --replace_string="$ynh_npm" --target_file="$file" + fi + if test -n "${ynh_node:-}"; then + ynh_replace_string --match_string="__YNH_NODE__" --replace_string="$ynh_node" --target_file="$file" + fi + if test -n "${ynh_node_load_PATH:-}"; then + ynh_replace_string --match_string="__YNH_NODE_LOAD_PATH__" --replace_string="$ynh_node_load_PATH" --target_file="$file" + fi + if test -n "${nodejs_path:-}"; then + ynh_replace_string --match_string="__NODEJS_PATH__" --replace_string="$nodejs_path" --target_file="$file" + fi + if test -n "${nodejs_version:-}"; then + ynh_replace_string --match_string="__NODEJS_VERSION__" --replace_string="$nodejs_version" --target_file="$file" + fi + if test -n "${db_name:-}"; then + ynh_replace_string --match_string="__DB_NAME__" --replace_string="$db_name" --target_file="$file" + fi + if test -n "${db_user:-}"; then + ynh_replace_string --match_string="__DB_USER__" --replace_string="$db_user" --target_file="$file" + fi + if test -n "${db_pwd:-}"; then + ynh_replace_string --match_string="__DB_PWD__" --replace_string="$db_pwd" --target_file="$file" + fi + + # Replace all other variables given as arguments + for var_to_replace in $vars + do + # ${var_to_replace^^} make the content of the variable on upper-cases + # ${!var_to_replace} get the content of the variable named $var_to_replace + ynh_replace_string --match_string="__${var_to_replace^^}__" --replace_string="${!var_to_replace}" --target_file="$file" + done +} + + + + + + + + +#================================================= +# FUTURE OFFICIAL HELPERS +#================================================= + + + +# installdeps(){ + +# if [ $(dpkg --print-architecture) == "armhf" ]; then +# #Install mongodb for debian armhf +# sudo apt-get update +# sudo apt-get install -y mongodb-server + +# # start mongodb service +# sudo systemctl enable mongodb.service +# sudo systemctl start mongodb.service + +# # add mongodb to services +# sudo yunohost service add mongodb -l /var/log/mongodb/mongodb.log +# else +# #Install mongodb for debian x86/x64 +# sudo apt-get install dirmngr && sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4 +# echo "deb http://repo.mongodb.org/apt/debian ${DEBIAN_ISSUE}/mongodb-org/4.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list +# sudo apt-get update +# sudo apt-get install -y mongodb-org + +# # start mongodb service +# sudo systemctl enable mongod.service +# sudo systemctl start mongod.service + +# # add mongodb to services +# sudo yunohost service add mongod -l /var/log/mongodb/mongod.log +# fi + +# #Install other dependencies +# sudo apt-get install -y build-essential gzip curl graphicsmagick + +# } diff --git a/scripts/backup b/scripts/backup index f5a45d0..9389cc9 100644 --- a/scripts/backup +++ b/scripts/backup @@ -1,11 +1,11 @@ #!/bin/bash -set -eu #================================================= # IMPORT GENERIC HELPERS #================================================= source /usr/share/yunohost/helpers + ynh_abort_if_errors #================================================= diff --git a/scripts/install b/scripts/install index 28e2b9e..0871f02 100644 --- a/scripts/install +++ b/scripts/install @@ -1,5 +1,4 @@ #!/bin/bash -set -eu #================================================= # GENERIC START @@ -14,6 +13,9 @@ source ./_common.sh # MANAGE SCRIPT FAILURE #================================================= +ynh_clean_setup () { + true +} # Exit if an error occurs during the execution of the script ynh_abort_if_errors @@ -22,129 +24,189 @@ ynh_abort_if_errors #================================================= domain=$YNH_APP_ARG_DOMAIN -path=$YNH_APP_ARG_PATH +path_url=$YNH_APP_ARG_PATH is_public=$YNH_APP_ARG_IS_PUBLIC language=$YNH_APP_ARG_LANG app=$YNH_APP_INSTANCE_NAME -dbname=$app -serviceuser=rocketchat #================================================= -# CHECK IF THE APP CAN BE INSTALLED +# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS #================================================= +ynh_script_progression --message="Validating installation parameters..." --weight=1 -# Check for supported architecture -arch="$(uname -m)" -if [[ "$arch" != "x86_64" ]]; then - script_die "This app requires an x86_64 machine, but this one is '${arch}'." +final_path=/var/www/$app +test ! -e "$final_path" || ynh_die --message="This path already contains a folder" + +# Register (book) web path +ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url + +#================================================= +# STORE SETTINGS FROM MANIFEST +#================================================= +ynh_script_progression --message="Storing installation settings..." --weight=2 + +ynh_app_setting_set --app=$app --key=domain --value=$domain +ynh_app_setting_set --app=$app --key=path --value=$path_url +ynh_app_setting_set --app=$app --key=is_public --value=$is_public +ynh_app_setting_set --app=$app --key=language --value=$language + +#================================================= +# STANDARD MODIFICATIONS +#================================================= +# FIND AND OPEN A PORT +#================================================= +ynh_script_progression --message="Configuring firewall..." --weight=2 + +# Find an available port +port=$(ynh_find_port --port=3000) +ynh_app_setting_set --app=$app --key=port --value=$port + +#================================================= +# INSTALL DEPENDENCIES +#================================================= +ynh_script_progression --message="Installing dependencies..." --weight=20 + +ynh_install_app_dependencies $pkg_dependencies + +# Install Nodejs +ynh_exec_warn_less ynh_install_nodejs --nodejs_version=$nodejs_version + +# Install Yarn +#ynh_install_extra_app_dependencies --repo="deb https://dl.yarnpkg.com/debian/ stable main" --package="yarn" --key="https://dl.yarnpkg.com/debian/pubkey.gpg" + +if [ "$(lsb_release --codename --short)" = "buster" ]; then + ynh_install_extra_app_dependencies --repo="deb http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 main" --package="$pkg_dependencies_buster" --key="https://www.mongodb.org/static/pgp/server-4.2.asc" + mongodb_servicename=$mongodb_buster +else + ynh_install_app_dependencies $pkg_dependencies + mongodb_servicename=$mongodb_stretch fi -workdir=$(pwd) +#================================================= +# CREATE A MONGODB DATABASE +#================================================= +ynh_print_info --message="Creating a MongoDB database..." -# find suitable port (default 3000) -port=$(ynh_find_port 3000) +# Start mongodb +systemctl enable $mongodb_servicename +systemctl start $mongodb_servicename -# if path do not begin with / add a / at the begining -if [ "${path:0:1}" != "/" ]; then - path="/$path" -fi -# if path do not end with / add a / at the end -if [ "${path:${#path}-1}" != "/" ] && [ ${#path} -gt 1 ]; then - path="$path/" -fi - - -# Check domain/path availability -ynh_webpath_available $domain $path -if [[ ! $? -eq 0 ]]; then - ynh_die "domain not available" -fi - -# Register/book a web path for an app -ynh_webpath_register $app $domain $path - -final_path="/var/lib/$app" -[[ -d $final_path ]] && ynh_die \ -"The destination directory '$final_path' already exists.\ - You should safely delete it before installing this app." - -# Save specific settings -ynh_app_setting_set $app node_version $NODE_VERSION -ynh_app_setting_set $app rocketchat_version $ROCKETCHAT_VERSION -ynh_app_setting_set $app serviceuser $serviceuser -ynh_app_setting_set $app port $port -ynh_app_setting_set $app final_path $final_path -ynh_app_setting_set $app path $path -ynh_app_setting_set $app is_public $is_public - -#Install dependencies -installdeps - -# Create destination -mkdir -p $final_path - -# Create user -id -u $serviceuser || useradd -d "$final_path" -M $serviceuser - -# Copy and set systemd configuration -ynh_replace_string "#ROOTURL#" "$domain" ../conf/rocketchat.service -ynh_replace_string "#LOCATION#" "${path:-/}" ../conf/rocketchat.service -ynh_replace_string "#PORT#" "$port" ../conf/rocketchat.service -ynh_replace_string "#USER#" "$serviceuser" ../conf/rocketchat.service -ynh_replace_string "#DESTDIR#" "$final_path" ../conf/rocketchat.service -ynh_replace_string "#DBNAME#" "$dbname" ../conf/rocketchat.service -# absolute node path needed -NODE_BIN=$(which node) -ynh_replace_string "#NODE#" "$NODE_BIN" ../conf/rocketchat.service -cp ../conf/rocketchat.service /etc/systemd/system/$app.service -systemctl daemon-reload - -# Copy and set nginx configuration -nginx_conf="/etc/nginx/conf.d/${domain}.d/${app}.conf" -ynh_replace_string "#APP#" "$app" ../conf/nginx.conf -ynh_replace_string "#PATH#" "$path" ../conf/nginx.conf -ynh_replace_string "#PORT#" "$port" ../conf/nginx.conf -ynh_replace_string "#LOCATION#" "${path:-/}" ../conf/nginx.conf - -#set db name into ldap config -ynh_replace_string "#DBNAME#" "$dbname" ../conf/rocketchat_ldap.js - -cp ../conf/nginx.conf "$nginx_conf" +# Registering db name +db_name=$(ynh_sanitize_dbid --db_name=$app) +ynh_app_setting_set --app=$app --key=db_name --value=$db_name #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE #================================================= +ynh_script_progression --message="Setting up source files..." --weight=2 -ynh_setup_source "$final_path" +ynh_app_setting_set --app=$app --key=final_path --value=$final_path +# Download, check integrity, uncompress and patch the source from app.src +ynh_setup_source --dest_dir=$final_path -chown -R $serviceuser: $final_path +#================================================= +# NGINX CONFIGURATION +#================================================= +ynh_script_progression --message="Configuring Nginx web server..." --weight=2 -cd $final_path/programs/server/ +# Create a dedicated nginx config +ynh_add_nginx_config -sudo -u $serviceuser npm install --production +#================================================= +# CREATE DEDICATED USER +#================================================= +ynh_script_progression --message="Configuring system user..." --weight=3 -cd $workdir +# Create a system user +ynh_system_user_create --username=$app --home_dir=$final_path -systemctl reload nginx -if [ "$is_public" = "Yes" ]; +#================================================= +# SPECIFIC SETUP +#============================================== +# INSTALL HEDGEDOC +#============================================== +ynh_script_progression --message="Building $app... (this will take some time and resources!)" --weight=240 + +pushd "$final_path" || ynh_die +curl https://install.meteor.com/ | sh + sudo -u $app meteor ynh_npm install + sudo -u $app meteor ynh_npm run postinstall + sudo -u $app meteor add rocketchat:lib + sudo -u $app meteor build --server-only --directory $final_path + cd $final_path/bundle/programs/server + ynh_npm install + +popd || ynh_die + +#================================================= +# SETUP SYSTEMD +#================================================= +ynh_script_progression --message="Configuring a systemd service..." --weight=1 + +ynh_replace_string --match_string="__ENV_PATH__" --replace_string="$PATH" --target_file="../conf/systemd.service" +ynh_replace_string --match_string="__YNH_NPM__" --replace_string="$ynh_npm" --target_file="../conf/systemd.service" +ynh_replace_string --match_string="__PORT__" --replace_string="$port" --target_file="../conf/systemd.service" +ynh_replace_string --match_string="__DOMAIN__" --replace_string="$domain" --target_file="../conf/systemd.service" + +ynh_add_systemd_config + +# #================================================= +# # MODIFY A CONFIG FILE +# #================================================= + +ynh_replace_string --match_string=" engine:/" --replace_string="engine: wiredTiger/" --target_file="/etc/mongod.conf" +ynh_replace_string --match_string="replication:/replication:\n" --replace_string="replSetName: rs01/" --target_file="/etc/mongod.conf" + +#================================================= +# GENERIC FINALIZATION +#================================================= +# SECURE FILES AND DIRECTORIES +#================================================= +ynh_script_progression --message="Securing files and directories..." + +# Set permissions to app files +chown -R $app:$app $final_path + +#================================================= +# INTEGRATE SERVICE IN YUNOHOST +#================================================= +ynh_script_progression --message="Integrating service in YunoHost..." + +yunohost service add $app --description "Chat" --log="/var/log/$app/$app.log" +yunohost service add mongodb --description "MongoDB daemon" --log "/var/log/mongodb/mongodb.log" + + +mongo --eval "printjson(rs.initiate())" + +#================================================= +# START SYSTEMD SERVICE +#================================================= +ynh_script_progression --message="Starting a systemd service..." --weight=2 + +# Start a systemd service +ynh_systemd_action --service_name=$app --action="start" --log_path="systemd" #--line_match="HTTP Server listening" + +#================================================= +# SETUP SSOWAT +#================================================= +ynh_script_progression --message="Configuring SSOwat..." --weight=1 + +# Make app public if necessary or protect it +if [ $is_public -eq 1 ] then - ynh_app_setting_set "$app" skipped_uris "/" + ynh_permission_update --permission "main" --add "visitors" fi -systemctl start $app.service -systemctl enable $app.service +#================================================= +# RELOAD NGINX +#================================================= +ynh_script_progression --message="Reloading NGINX web server..." --weight=2 -# add rocketchat to services -yunohost service add $app +ynh_systemd_action --service_name=nginx --action=reload -# wait for rocketchat to populate db and start -waitforservice +#================================================= +# END OF SCRIPT +#================================================= -mongo < ../conf/rocketchat_ldap.js - -systemctl restart $app.service - -waitforservice - -yunohost app ssowatconf +ynh_script_progression --message="Installation of $app completed" --last diff --git a/scripts/remove b/scripts/remove index 9e77aa1..42fc704 100644 --- a/scripts/remove +++ b/scripts/remove @@ -1,5 +1,4 @@ #!/bin/bash -set -u #================================================= # GENERIC START @@ -7,25 +6,97 @@ set -u # IMPORT GENERIC HELPERS #================================================= +source _common.sh source /usr/share/yunohost/helpers -source ./_common.sh + +#================================================= +# LOAD SETTINGS +#================================================= +ynh_script_progression --message="Loading installation settings..." --time --weight=1 app=$YNH_APP_INSTANCE_NAME -domain=$(ynh_app_setting_get $app domain) -final_path=$(ynh_app_setting_get $app final_path) -systemctl stop $app.service +domain=$(ynh_app_setting_get --app=$app --key=domain) +port=$(ynh_app_setting_get --app=$app --key=port) +db_name=$(ynh_app_setting_get --app=$app --key=db_name) +db_user=$db_name +final_path=$(ynh_app_setting_get --app=$app --key=final_path) -# remove Rocketchat from services -yunohost service remove $app +#================================================= +# STANDARD REMOVE +#================================================= +# REMOVE SERVICE INTEGRATION IN YUNOHOST +#================================================= + +# Remove the service from the list of services known by Yunohost (added from `yunohost service add`) +if ynh_exec_warn_less yunohost service status $app >/dev/null +then + ynh_script_progression --message="Removing $app service..." --time --weight=1 + yunohost service remove $app +fi + +#================================================= +# STOP AND REMOVE SERVICE +#================================================= +ynh_script_progression --message="Stopping and removing the systemd service..." --time --weight=1 + +# Remove the dedicated systemd config +ynh_remove_systemd_config + +#================================================= +# REMOVE THE MYSQL DATABASE +#================================================= +ynh_script_progression --message="Removing the Mongodb database..." --time --weight=1 -ynh_secure_remove $final_path -ynh_secure_remove /etc/nginx/conf.d/$domain.d/$app.conf -ynh_secure_remove /var/log/$app -ynh_secure_remove /etc/systemd/system/$app.service # remove database!!! -mongo rocketchat --eval "db.dropDatabase()" +mongo $app --eval "db.dropDatabase()" -systemctl daemon-reload -systemctl reload nginx +#================================================= +# REMOVE DEPENDENCIES +#================================================= +ynh_script_progression --message="Removing dependencies..." --time --weight=1 + +# Remove metapackage and its dependencies +ynh_remove_app_dependencies + +#================================================= +# REMOVE APP MAIN DIR +#================================================= +ynh_script_progression --message="Removing app main directory..." --time --weight=1 + +# Remove the app directory securely +ynh_secure_remove --file="$final_path" + +#================================================= +# REMOVE NGINX CONFIGURATION +#================================================= +ynh_script_progression --message="Removing nginx web server configuration..." --time --weight=1 + +# Remove the dedicated nginx config +ynh_remove_nginx_config + +#================================================= +# SPECIFIC REMOVE +#================================================= +# REMOVE VARIOUS FILES +#================================================= + +# Remove the log files +ynh_secure_remove --file="/var/log/$app/" + +#================================================= +# GENERIC FINALIZATION +#================================================= +# REMOVE DEDICATED USER +#================================================= +ynh_script_progression --message="Removing the dedicated system user..." --time --weight=1 + +# Delete a system user +ynh_system_user_delete --username=$app + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Removal of $app completed" --time --last