From 0478e79ea86dbf3b42d56eeefd58b193dc2627be Mon Sep 17 00:00:00 2001 From: Jimmy Monin Date: Mon, 11 Sep 2017 19:40:04 +0200 Subject: [PATCH] Refactor, use latest YunoHost 2.7.2 standards --- README.md | 75 ++++++++--- check_process | 7 +- conf/app.src | 4 + conf/netdata-deps.control | 13 -- conf/{nginx_sub_dir.conf => nginx.conf} | 8 +- conf/nginx_root.conf | 14 --- manifest.json | 2 +- scripts/_common.sh | 56 ++++----- scripts/backup | 44 +++++-- scripts/install | 158 ++++++++++++++---------- scripts/remove | 78 +++++++----- scripts/restore | 113 ++++++++++------- scripts/upgrade | 146 +++++++++++++--------- 13 files changed, 419 insertions(+), 299 deletions(-) create mode 100644 conf/app.src delete mode 100644 conf/netdata-deps.control rename conf/{nginx_sub_dir.conf => nginx.conf} (82%) delete mode 100644 conf/nginx_root.conf diff --git a/README.md b/README.md index 76847a1..5bcf92c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ NetData for YunoHost --------------------- +[![Install Piwigo with YunoHost](https://install-app.yunohost.org/install-with-yunohost.png)](https://install-app.yunohost.org/?app=piwigo) +[![Integration level](https://dash.yunohost.org/integration/piwigo.svg)](https://ci-apps.yunohost.org/jenkins/job/piwigo%20%28Community%29/lastBuild/consoleFull) + [NetData](http://my-netdata.io/) is a system for **distributed real-time performance and health monitoring**. It provides **unparalleled insights, in real-time**, of everything happening on the @@ -37,47 +40,52 @@ It has been tested on x86_64 and ARM. - **Stunning interactive bootstrap dashboards**
mouse and touch friendly, in 2 themes: dark, light - + - **Amazingly fast**
responds to all queries in less than 0.5 ms per metric, even on low-end hardware - + - **Highly efficient**
collects thousands of metrics per server per second, with just 1% CPU utilization of a single core, a few MB of RAM and no disk I/O at all - + - **Sophisticated alarming**
+ hundreds of alarms, **out of the box**!
supports dynamic thresholds, hysteresis, alarm templates, multiple role-based notification methods (such as email, slack.com, - pushover.net, pushbullet.com telegram.org, twilio.com, messagebird.com) - + pushover.net, pushbullet.com, telegram.org, twilio.com, messagebird.com) + - **Extensible**
you can monitor anything you can get a metric for, using its Plugin API (anything can be a netdata plugin, BASH, python, perl, node.js, java, Go, ruby, etc) - + - **Embeddable**
it can run anywhere a Linux kernel runs (even IoT) and its charts can be embedded on your web pages too - + - **Customizable**
custom dashboards can be built using simple HTML (no javascript necessary) - + - **Zero configuration**
auto-detects everything, it can collect up to 5000 metrics per server out of the box - + - **Zero dependencies**
it is even its own web server, for its static web files and its web API - + - **Zero maintenance**
you just run it, it does the rest - + - **scales to infinity**
requiring minimal central resources - - - **back-ends supported**
- can archive its metrics on `graphite` or `opentsdb`, in the same or lower detail + + - **several operating modes**
+ autonomous host monitoring, headless data collector, forwarding proxy, store and forward proxy, central multi-host monitoring, in all possible configurations. + Each node may have different metrics retention policy and run with or without health monitoring. + + - **time-series back-ends supported**
+ can archive its metrics on `graphite`, `opentsdb`, `prometheus`, json document DBs, in the same or lower detail (lower: to prevent it from congesting these servers due to the amount of data collected) ![netdata](https://cloud.githubusercontent.com/assets/2662304/14092712/93b039ea-f551-11e5-822c-beadbf2b2a2e.gif) @@ -86,7 +94,7 @@ It has been tested on x86_64 and ARM. ## What does it monitor? -netdata monitors several thousands of metrics per device. +netdata collects several thousands of metrics per device. All these metrics are collected and visualized in real-time. > _Almost all metrics are auto-detected, without any configuration._ @@ -94,13 +102,13 @@ All these metrics are collected and visualized in real-time. This is a list of what it currently monitors: - **CPU**
- usage, interrupts, softirqs, frequency, total and per core + usage, interrupts, softirqs, frequency, total and per core, CPU states - **Memory**
RAM, swap and kernel memory usage, KSM (Kernel Samepage Merging), NUMA - **Disks**
- per disk: I/O, operations, backlog, utilization, space + per disk: I/O, operations, backlog, utilization, space, software RAID (md) ![sda](https://cloud.githubusercontent.com/assets/2662304/14093195/c882bbf4-f554-11e5-8863-1788d643d2c0.gif) @@ -176,6 +184,9 @@ This is a list of what it currently monitors: - **Tomcat**
accesses, threads, free memory, volume +- **web server log files**
+ extracting in real-time, web server performance metrics and applying several health checks + - **mySQL databases**
multiple servers, each showing: bandwidth, queries/s, handlers, locks, issues, tmp operations, connections, binlog metrics, threads, innodb metrics, and more @@ -188,12 +199,21 @@ This is a list of what it currently monitors: - **Redis databases**
multiple servers, each showing: operations, hit rate, memory, keys, clients, slaves +- **mongodb**
+ operations, clients, transactions, cursors, connections, asserts, locks, etc + - **memcached databases**
multiple servers, each showing: bandwidth, connections, items +- **elasticsearch**
+ search and index performance, latency, timings, cluster statistics, threads statistics, etc + - **ISC Bind name servers**
multiple servers, each showing: clients, requests, queries, updates, failures and several per view metrics +- **NSD name servers**
+ queries, zones, protocols, query types, transfers, etc. + - **Postfix email servers**
message queue (entries, size) @@ -202,14 +222,26 @@ This is a list of what it currently monitors: - **Dovecot** POP3/IMAP servers
+- **ISC dhcpd**
+ pools utilization, leases, etc. + - **IPFS**
bandwidth, peers - **Squid proxy servers**
multiple servers, each showing: clients bandwidth and requests, servers bandwidth and requests +- **HAproxy**
+ bandwidth, sessions, backends, etc + +- **varnish**
+ threads, sessions, hits, objects, backends, etc + +- **OpenVPN**
+ status per tunnel + - **Hardware sensors**
- temperature, voltage, fans, power, humidity + `lm_sensors` and `IPMI`: temperature, voltage, fans, power, humidity - **NUT and APC UPSes**
load, charge, battery voltage, temperature, utility metrics, output metrics @@ -220,11 +252,16 @@ This is a list of what it currently monitors: - **hddtemp**
disk temperatures +- **smartd**
+ disk S.M.A.R.T. values + - **SNMP devices**
can be monitored too (although you will need to configure these) -And you can extend it, by writing plugins that collect data from any source, using any computer language. +- **statsd**
+ [netdata is a fully featured statsd server](https://github.com/firehol/netdata/wiki/statsd) +And you can extend it, by writing plugins that collect data from any source, using any computer language. ## Links diff --git a/check_process b/check_process index f2a187f..9abc6c2 100644 --- a/check_process +++ b/check_process @@ -17,10 +17,6 @@ wrong_user=0 wrong_path=1 incorrect_path=1 - corrupt_source=0 - fail_download_source=0 - port_already_use=0 - final_path_already_use=0 ;;; Levels Level 1=auto Level 2=auto @@ -34,3 +30,6 @@ Level 8=0 Level 9=0 Level 10=0 +;;; Options +Email= +Notification=none \ No newline at end of file diff --git a/conf/app.src b/conf/app.src new file mode 100644 index 0000000..d4df1b7 --- /dev/null +++ b/conf/app.src @@ -0,0 +1,4 @@ +SOURCE_URL=https://github.com/firehol/netdata/releases/download/v1.7.0/netdata-1.7.0.tar.gz +SOURCE_SUM=d088d5c6c22ac9241b87480df76de65a414ba9955be3399bd593929ca3c7a417 +SOURCE_FORMAT=tar.gz + diff --git a/conf/netdata-deps.control b/conf/netdata-deps.control deleted file mode 100644 index f9f2847..0000000 --- a/conf/netdata-deps.control +++ /dev/null @@ -1,13 +0,0 @@ -Section: misc -Priority: optional -Homepage: https://https://my-netdata.io/ -Standards-Version: 3.9.2 - -Package: netdata-deps -Version: 1.6.0-1 -Depends: zlib1g-dev, uuid-dev, libmnl-dev, gcc, make, git, autoconf, autoconf-archive, autogen, automake, pkg-config, curl, jq, nodejs, python-mysqldb, libipmimonitoring-dev -Architecture: all -Description: meta package for NetData dependencies - netdata is a system for distributed real-time performance and health monitoring. It provides unparalleled insights, in real-time, of everything happening on the system it runs (including applications such as web and database servers), using modern interactive web dashboards. - . - This meta-package is only responsible of installing its dependencies. diff --git a/conf/nginx_sub_dir.conf b/conf/nginx.conf similarity index 82% rename from conf/nginx_sub_dir.conf rename to conf/nginx.conf index 55d4e6b..fde6590 100644 --- a/conf/nginx_sub_dir.conf +++ b/conf/nginx.conf @@ -1,8 +1,8 @@ -location YNH_WWW_PATH { - return 301 YNH_WWW_PATH/; -} +#for-subdir location __PATH__ { +#for-subdir return 301 __PATH__/; +#for-subdir } -location ~ YNH_WWW_PATH/(?.*) { +location ~ __PATH__/(?.*) { proxy_redirect off; proxy_set_header Host $host; diff --git a/conf/nginx_root.conf b/conf/nginx_root.conf deleted file mode 100644 index 052b98e..0000000 --- a/conf/nginx_root.conf +++ /dev/null @@ -1,14 +0,0 @@ -location / { - proxy_set_header X-Forwarded-Host $host; - proxy_set_header X-Forwarded-Server $host; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header Accept-Encoding ""; - proxy_pass http://127.0.0.1:19999; - proxy_http_version 1.1; - proxy_pass_request_headers on; - proxy_set_header Connection "keep-alive"; - proxy_store off; - - # Include SSOWAT user panel. - include conf.d/yunohost_panel.conf.inc; -} \ No newline at end of file diff --git a/manifest.json b/manifest.json index 97cb9dd..e961b8d 100644 --- a/manifest.json +++ b/manifest.json @@ -15,7 +15,7 @@ "url": "" }, "requirements": { - "yunohost": ">> 2.4.0" + "yunohost": ">= 2.7.2" }, "multi_instance": false, "services": [ diff --git a/scripts/_common.sh b/scripts/_common.sh index dbf17c6..758ec9b 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -3,42 +3,32 @@ # Common variables # -# Application version -VERSION="1.7.0" +pkg_dependencies="zlib1g-dev uuid-dev libmnl-dev gcc make git autoconf autoconf-archive autogen automake pkg-config curl jq nodejs python-mysqldb libipmimonitoring-dev" -# Remote URL to fetch application source archive -APPLICATION_SOURCE_URL="https://github.com/firehol/netdata/releases/download/v${VERSION}/netdata-${VERSION}.tar.gz" +# Configure NetData +configure_netdata() { + # Create netdata user to monitor MySQL (if needed) + is_mysql_user_existing=$(ynh_mysql_execute_as_root "select user from mysql.user where user = 'netdata';") + if [ -z "$is_mysql_user_existing" ] ; then + ynh_mysql_execute_as_root "create user 'netdata'@'localhost'; + grant usage on *.* to 'netdata'@'localhost' with grant option; + flush privileges;" + fi -# Package name for NetData dependencies -DEPS_PKG_NAME="netdata-deps" + # Give dovecot privileges to netdata user to monitor Dovecot + # Need dovecot 2.2.16+ + setfacl -m u:netdata:rw /var/run/dovecot/stats -# -# Common helpers -# + # Add netdata to the adm group to access web logs + usermod -a -G adm netdata -# Download and extract application sources to the given directory -# usage: extract_application_to DESTDIR -extract_application() { - TMPDIR=$(mktemp -d) - chmod 755 $TMPDIR - archive="${TMPDIR}/application.tar.gz" - wget -q -O "$archive" "$APPLICATION_SOURCE_URL" \ - || ynh_die "Unable to download application archive" - tar xf "$archive" -C "$TMPDIR" --strip-components 1 \ - || ynh_die "Unable to extract application archive" - rm "$archive" - echo "$TMPDIR" + # Declare service for YunoHost monitoring + yunohost service add netdata --log "/opt/netdata/var/log/netdata/error.log" "/opt/netdata/var/log/netdata/access.log" "/opt/netdata/var/log/netdata/debug.log" + + # Restart NetData + systemctl restart netdata + + # Store the uninstaller for the removal script + mv ./netdata-uninstaller.sh /opt/netdata/etc/netdata } -# Fix path if needed -# usage: fix_patch PATH_TO_FIX -fix_path() { - local path=$1 - if [ "${path:0:1}" != "/" ] && [ ${#path} -gt 0 ]; then - path="/$path" - fi - if [ "${path:${#path}-1}" == "/" ] && [ ${#path} -gt 1 ]; then - path="${path:0:${#path}-1}" - fi - echo "$path" -} \ No newline at end of file diff --git a/scripts/backup b/scripts/backup index ff94d41..61def6a 100644 --- a/scripts/backup +++ b/scripts/backup @@ -1,17 +1,45 @@ #!/bin/bash -# Exit on command errors and treat unset variables as an error +#================================================= +# GENERIC START +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= + +# Exit on command errors and treat access to unset variables as an error set -eu +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +if [ ! -e _common.sh ]; then + # Get the _common.sh file if it's not in the current directory + cp ../settings/scripts/_common.sh ./_common.sh + chmod a+rx _common.sh +fi +source _common.sh +source /usr/share/yunohost/helpers + +#================================================= +# LOAD SETTINGS +#================================================= + app=$YNH_APP_INSTANCE_NAME -# Source YunoHost helpers -source /usr/share/yunohost/helpers +domain=$(ynh_app_setting_get "$app" domain) + +#================================================= +# STANDARD BACKUP STEPS +#================================================= +# BACKUP THE APP CONFIGURATION +#================================================= # Backup configuration files -# Note: the last argument is where to save this path, see the restore script. -ynh_backup "/opt/netdata/etc/netdata" "conf" +ynh_backup "/opt/netdata/etc/netdata" -# Copy NGINX configuration -domain=$(ynh_app_setting_get "$app" domain) -ynh_backup "/etc/nginx/conf.d/${domain}.d/${app}.conf" "nginx.conf" +#================================================= +# BACKUP THE NGINX CONFIGURATION +#================================================= + +ynh_backup "/etc/nginx/conf.d/$domain.d/$app.conf" diff --git a/scripts/install b/scripts/install index c5bbf88..14f213c 100644 --- a/scripts/install +++ b/scripts/install @@ -1,89 +1,121 @@ #!/bin/bash -# Exit on command errors and treat unset variables as an error -set -eu +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= -shopt -s extglob # sets extended pattern matching options in the bash shell +source _common.sh +source /usr/share/yunohost/helpers + +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= + +# Exit if an error occurs during the execution of the script +ynh_abort_if_errors + +#================================================= +# RETRIEVE ARGUMENTS FROM THE MANIFEST +#================================================= + +domain=$YNH_APP_ARG_DOMAIN +path_url=$YNH_APP_ARG_PATH +is_public=$YNH_APP_ARG_IS_PUBLIC app=$YNH_APP_INSTANCE_NAME -# Source local helpers -source ./_common.sh +#================================================= +# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS +#================================================= -SRCPATH=$(pwd) +# Normalize the url path syntax +path_url=$(ynh_normalize_url_path $path_url) -# Retrieve arguments -domain=$YNH_APP_ARG_DOMAIN -path=$YNH_APP_ARG_PATH -is_public=$YNH_APP_ARG_IS_PUBLIC +# Check web path availability +ynh_webpath_available $domain $path_url +# Register (book) web path +ynh_webpath_register $app $domain $path_url -# Source YunoHost helpers -source /usr/share/yunohost/helpers +#================================================= +# STORE SETTINGS FROM MANIFEST +#================================================= -# Save app settings -ynh_app_setting_set "$app" is_public "$is_public" +ynh_app_setting_set $app domain $domain +ynh_app_setting_set $app path $path_url +ynh_app_setting_set $app is_public $is_public -# Fix path if needed -path=$(fix_path $path) +#================================================= +# STANDARD MODIFICATIONS +#================================================= +# INSTALL DEPENDENCIES +#================================================= -# Check domain/path availability -sudo yunohost app checkurl "${domain}${path}" -a "$app" \ - || ynh_die "Path not available: ${domain}${path}" +ynh_install_app_dependencies $pkg_dependencies -# Download and extract application -NETDATA_TMPDIR=$(extract_application) +#================================================= +# DOWNLOAD, CHECK AND UNPACK SOURCE +#================================================= -# Install dependencies -ynh_package_install_from_equivs ../conf/${DEPS_PKG_NAME}.control \ -|| ynh_die "Unable to install dependencies" +# Download, check integrity, uncompress and patch the source from app.src +NETDATA_TMPDIR=$(mktemp -d) +ynh_setup_source "$NETDATA_TMPDIR" + +#================================================= +# NGINX CONFIGURATION +#================================================= + +nginx_conf="../conf/nginx.conf" +if [ "$path_url" = "/" ] +then + # Handle root path, avoid double slash. + # Temporary fix, in waiting for an upgrade of the helper. (#361) + path_url_slash_less=${path_url%/} + ynh_replace_string "__PATH__/" "$path_url_slash_less/" $nginx_conf +else + # Move prefix comment #for-subdir at end of lines + ynh_replace_string "#for-subdir\(.*\)" "\1 #for-subdir" $nginx_conf +fi + +# Create a dedicated nginx config +ynh_add_nginx_config + +#================================================= +# SPECIFIC SETUP +#================================================= + +#================================================= +# INSTALL AND CONFIGURE NETDATA +#================================================= # Launch netdata installation in /opt directory -cd $NETDATA_TMPDIR -sudo ./netdata-installer.sh --install /opt --dont-wait +pushd $NETDATA_TMPDIR +./netdata-installer.sh --install /opt --dont-wait -# Create netdata user to monitor MySQL -ynh_mysql_execute_as_root "create user 'netdata'@'localhost'; -grant usage on *.* to 'netdata'@'localhost' with grant option; -flush privileges;" +configure_netdata -# Give dovecot privileges to netdata user to monitor Dovecot -# Need dovecot 2.2.16+ -sudo setfacl -m u:netdata:rw /var/run/dovecot/stats +# Store the app.src file +popd +cp ../conf/app.src /opt/netdata/etc/netdata -# Add netdata to the adm group to access web logs -sudo usermod -a -G adm netdata +#================================================= +# GENERIC FINALIZATION +#================================================= -# Extend weblog path to process logs for every domain -sed -i "s@path: '/var/log/nginx/access.log'@path: '/var/log/nginx/*-access.log'@g" /opt/netdata/etc/netdata/python.d/web_log.conf - -# Declare service for YunoHost monitoring -sudo yunohost service add netdata --log "/opt/netdata/var/log/netdata/error.log" "/opt/netdata/var/log/netdata/access.log" "/opt/netdata/var/log/netdata/debug.log" - -# Restart NetData -sudo systemctl restart netdata - -# Store the uninstaller for the removal script -sudo mv ./netdata-uninstaller.sh /opt/netdata/etc/netdata - -# Store useful files for backup/restore scripts -sudo cp $SRCPATH/_common.sh /opt/netdata/etc/netdata -sudo cp $SRCPATH/../conf/netdata-deps.control /opt/netdata/etc/netdata - -# Modify Nginx configuration file and copy it to Nginx conf directory -if [[ "$path" == "/" ]] ; then - nginx_conf=$SRCPATH/../conf/nginx_root.conf -else - nginx_conf=$SRCPATH/../conf/nginx_sub_dir.conf -fi - -sed -i "s@YNH_WWW_PATH@$path@g" $nginx_conf -sudo cp $nginx_conf /etc/nginx/conf.d/$domain.d/$app.conf +#================================================= +# SETUP SSOWAT +#================================================= # If app is public, add url to SSOWat conf as skipped_uris -if [[ $is_public -eq 1 ]]; then +if [ $is_public -eq 1 ]; then # unprotected_uris allows SSO credentials to be passed anyway. ynh_app_setting_set "$app" unprotected_uris "/" fi -# Reload services -sudo systemctl reload nginx + +#================================================= +# RELOAD NGINX +#================================================= + +systemctl reload nginx diff --git a/scripts/remove b/scripts/remove index 57b3bf1..d77fd22 100644 --- a/scripts/remove +++ b/scripts/remove @@ -1,53 +1,65 @@ #!/bin/bash -# Treat unset variables as an error -set -u +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= -# See comments in install script -app=$YNH_APP_INSTANCE_NAME - -# Source YunoHost helpers +if [ ! -e _common.sh ]; then + # Get file fonction if not been to the current directory + cp ../settings/scripts/_common.sh ./_common.sh + chmod a+rx _common.sh +fi +# Source app helpers +source _common.sh source /usr/share/yunohost/helpers -# Source local helpers -source ./_common.sh +#================================================= +# LOAD SETTINGS +#================================================= + +app=$YNH_APP_INSTANCE_NAME -# Retrieve app settings domain=$(ynh_app_setting_get "$app" domain) +#================================================= +# STANDARD REMOVE +#================================================= +# REMOVE DEPENDENCIES +#================================================= + +# Remove metapackage and its dependencies +ynh_remove_app_dependencies + +#================================================= +# REMOVE APP +#================================================= + # Prepare to execute uninstaller(generated by NetData install script) UNINSTALL_SCRIPT="netdata-uninstaller.sh" # Move outside the directory (which will be removed) -sudo mv /opt/netdata/etc/netdata/${UNINSTALL_SCRIPT} /tmp +mv /opt/netdata/etc/netdata/${UNINSTALL_SCRIPT} /tmp cd /tmp # Remove the need for interaction -sudo sed -i "s/rm -i/rm -f/g" ${UNINSTALL_SCRIPT} -sudo sed -i "s/rm -I/rm -f/g" ${UNINSTALL_SCRIPT} +ynh_replace_string "rm -i" "rm -f" ${UNINSTALL_SCRIPT} +ynh_replace_string "rm -I" "rm -f" ${UNINSTALL_SCRIPT} # Execute the uninstall script -sudo ./${UNINSTALL_SCRIPT} --force +./${UNINSTALL_SCRIPT} --force -# Remove app dependencies -ynh_package_autoremove "$DEPS_PKG_NAME" || true +#================================================= +# REMOVE NGINX CONFIGURATION +#================================================= -# Remove access rights for Dovecot monitoring -sudo setfacl -x u:netdata /var/run/dovecot/stats - -# Remove user and group -sudo userdel netdata - -# Remove service from YunoHost monitoring -sudo yunohost service remove netdata - -# Remove MySQL user -echo "drop user 'netdata'@'localhost';" | mysql -uroot -p$(sudo cat /etc/yunohost/mysql) - -# Remove nginx configuration file -sudo rm -f /etc/nginx/conf.d/$domain.d/$app.conf -sudo rm -rf /home/yunohost.app/$app - -# Reload nginx service -sudo systemctl reload nginx +# Remove the dedicated nginx config +ynh_remove_nginx_config +#================================================= +# SPECIFIC REMOVE +#================================================= +# The following command is kept as a matter of transition with the previous way +# of managing dependencies +ynh_package_autoremove "netdata-deps" || true diff --git a/scripts/restore b/scripts/restore index 58d7cf2..3fe54fe 100644 --- a/scripts/restore +++ b/scripts/restore @@ -1,67 +1,88 @@ #!/bin/bash -# Exit on command errors and treat unset variables as an error +#================================================= +# GENERIC START +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= + +# Exit on command errors and treat access to unset variables as an error set -eu +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +if [ ! -e _common.sh ]; then + # Get the _common.sh file if it's not in the current directory + cp ../settings/scripts/_common.sh ./_common.sh + chmod a+rx _common.sh +fi +source _common.sh +source /usr/share/yunohost/helpers + +#================================================= +# LOAD SETTINGS +#================================================= + app=$YNH_APP_INSTANCE_NAME -# Source YunoHost helpers -source /usr/share/yunohost/helpers +domain=$(ynh_app_setting_get $app domain) +path_url=$(ynh_app_setting_get $app path) -# Retrieve old app settings -domain=$(ynh_app_setting_get "$app" domain) -path=$(ynh_app_setting_get "$app" path) +#================================================= +# CHECK IF THE APP CAN BE RESTORED +#================================================= -# Source local helpers -source ./conf/_common.sh -SRCPATH=$(pwd) +ynh_webpath_available $domain $path_url \ + || ynh_die "Path not available: ${domain}${path_url}" -# Fix path if needed -path=$(fix_path $path) +#================================================= +# STANDARD RESTORATION STEPS +#================================================= +# RESTORE THE NGINX CONFIGURATION +#================================================= -# Check domain/path availability -sudo yunohost app checkurl "${domain}${path}" -a "$app" \ - || ynh_die "Path not available: ${domain}${path}" - -# Download and extract application -NETDATA_TMPDIR=$(extract_application) +ynh_restore_file "/etc/nginx/conf.d/$domain.d/$app.conf" -# Install dependencies -ynh_package_install_from_equivs ./conf/${DEPS_PKG_NAME}.control \ -|| ynh_die "Unable to install dependencies" +#================================================= +# INSTALL DEPENDENCIES +#================================================= + +ynh_install_app_dependencies $pkg_dependencies + +#================================================= +# DOWNLOAD, CHECK AND UNPACK SOURCE +#================================================= + +# Retrieve app.src from archive for ynh_setup_source helper +mkdir ../conf +cp opt/netdata/etc/netdata/app.src ../conf + +# Download, check integrity, uncompress and patch the source from app.src +NETDATA_TMPDIR=$(mktemp -d) +ynh_setup_source "$NETDATA_TMPDIR" + +#================================================= +# INSTALL AND RESTORE THE APP CONFIGURATION +#================================================= # Launch netdata installation in /opt directory cd $NETDATA_TMPDIR -sudo ./netdata-installer.sh --install /opt --dont-wait +./netdata-installer.sh --install /opt --dont-wait -# Create netdata user to monitor MySQL (if needed) - echo "select User from mysql.user where User = 'netdata';"| mysql -uroot -p$(sudo cat /etc/yunohost/mysql) || echo "create user 'netdata'@'localhost'; -grant usage on *.* to 'netdata'@'localhost' with grant option; -flush privileges;" | mysql -uroot -p$(sudo cat /etc/yunohost/mysql) - -# Give dovecot privileges to netdata user to monitor Dovecot -# Need dovecot 2.2.16+ -sudo setfacl -m u:netdata:rw /var/run/dovecot/stats - -# Add netdata to the adm group to access web logs -sudo usermod -a -G adm netdata - -# Restart NetData -sudo systemctl restart netdata - -# Store the uninstaller for the removal script -sudo mv ./netdata-uninstaller.sh /opt/netdata +configure_netdata # Restore configuration files -conf_path="/opt/netdata/etc/netdata" -sudo cp -a $SRCPATH/conf/* "$conf_path" +ynh_restore_file "/opt/netdata/etc/netdata" # Restore permissions to app files -# you may need to make some file and/or directory writeable by www-data (nginx user) -sudo chown -R root: "$conf_path" +chown -R root: "/opt/netdata/etc/netdata" -# Restore NGINX configuration -sudo cp -a $SRCPATH/nginx.conf "/etc/nginx/conf.d/${domain}.d/${app}.conf" +#================================================= +# GENERIC FINALISATION +#================================================= +# RELOAD NGINX AND PHP-FPM +#================================================= -# Restart webserver -sudo systemctl reload nginx +systemctl reload nginx diff --git a/scripts/upgrade b/scripts/upgrade index 948ae90..0d281b7 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -1,38 +1,86 @@ #!/bin/bash -# Exit on command errors and treat unset variables as an error -set -eu +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= -shopt -s extglob # sets extended pattern matching options in the bash shell +source _common.sh +source /usr/share/yunohost/helpers + +#================================================= +# LOAD SETTINGS +#================================================= app=$YNH_APP_INSTANCE_NAME -# Source local helpers -source ./_common.sh -SRCPATH=$(pwd) +domain=$(ynh_app_setting_get $app domain) +path_url=$(ynh_app_setting_get $app path) +is_public=$(ynh_app_setting_get $app is_public) -# Source YunoHost helpers -source /usr/share/yunohost/helpers -# Retrieve app settings -domain=$(ynh_app_setting_get "$app" domain) -path=$(ynh_app_setting_get "$app" path) -is_public=$(ynh_app_setting_get "$app" is_public) +#================================================= +# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP +#================================================= -# Fix path if needed -path=$(fix_path $path) +ynh_backup_before_upgrade # Backup the current version of the app +ynh_clean_setup () { + ynh_restore_upgradebackup # restore it if the upgrade fails +} +ynh_abort_if_errors # Active trap pour arrêter le script si une erreur est détectée. -# Upgrade dependencies -ynh_package_install_from_equivs ../conf/${DEPS_PKG_NAME}.control \ -|| ynh_die "Unable to upgrade dependencies" +#================================================= +# CHECK THE PATH +#================================================= -# Download and extract application -NETDATA_TMPDIR=$(extract_application) +# Normalize the URL path syntax +path_url=$(ynh_normalize_url_path $path_url) + +#================================================= +# STANDARD UPGRADE STEPS +#================================================= +#================================================= +# NGINX CONFIGURATION +#================================================= + +nginx_conf="../conf/nginx.conf" +if [ "$path_url" = "/" ] +then + # Handle root path, avoid double slash. + # Temporary fix, in waiting for an upgrade of the helper. (#361) + path_url_slash_less=${path_url%/} + ynh_replace_string "__PATH__/" "$path_url_slash_less/" $nginx_conf +else + # Move prefix comment #for-subdir at end of lines + ynh_replace_string "#for-subdir\(.*\)" "\1 #for-subdir" $nginx_conf +fi + +# Create a dedicated nginx config +ynh_add_nginx_config + +#================================================= +# UPGRADE DEPENDENCIES +#================================================= + +ynh_install_app_dependencies $pkg_dependencies + +#================================================= +# DOWNLOAD, CHECK AND UNPACK SOURCE +#================================================= + +# Download, check integrity, uncompress and patch the source from app.src +NETDATA_TMPDIR=$(mktemp -d) +ynh_setup_source "$NETDATA_TMPDIR" + +#================================================= +# SPECIFIC UPGRADE +#================================================= # signal netdata to start saving its database # this is handy if your database is big pids=$(pidof netdata) -[ ! -z "${pids}" ] && sudo kill -USR1 ${pids} +[ ! -z "${pids}" ] && kill -USR1 ${pids} # create a temporary file for the log tmp=$(mktemp /tmp/netdata-updater-log-XXXXXX.log) @@ -40,53 +88,29 @@ tmp=$(mktemp /tmp/netdata-updater-log-XXXXXX.log) exec 3>${tmp} # Launch netdata installation in /opt directory -cd $NETDATA_TMPDIR -sudo ./netdata-installer.sh --install /opt --dont-wait >&3 2>&3 || ynh_die "FAILED TO COMPILE/INSTALL NETDATA" +pushd $NETDATA_TMPDIR +./netdata-installer.sh --install /opt --dont-wait >&3 2>&3 || ynh_die "FAILED TO COMPILE/INSTALL NETDATA" -# Create netdata user to monitor MySQL (if needed) -is_mysql_user_existing=$(ynh_mysql_execute_as_root "select user from mysql.user where user = 'netdata';") -if [ -z "$is_mysql_user_existing" ] ; then - ynh_mysql_execute_as_root "create user 'netdata'@'localhost'; - grant usage on *.* to 'netdata'@'localhost' with grant option; - flush privileges;" -fi +configure_netdata -# Give dovecot privileges to netdata user to monitor Dovecot -# Need dovecot 2.2.16+ -sudo setfacl -m u:netdata:rw /var/run/dovecot/stats - -# Add netdata to the adm group to access web logs -sudo usermod -a -G adm netdata - -# Extend weblog path to process logs for every domain -sed -i "s@path: '/var/log/nginx/access.log'@path: '/var/log/nginx/*-access.log'@g" /opt/netdata/etc/netdata/python.d/web_log.conf - -# Declare service for YunoHost monitoring -sudo yunohost service add netdata --log "/opt/netdata/var/log/netdata/error.log" "/opt/netdata/var/log/netdata/access.log" "/opt/netdata/var/log/netdata/debug.log" - -# Restart NetData -sudo systemctl restart netdata - -# Store the uninstaller for the removal script -sudo mv ./netdata-uninstaller.sh /opt/netdata/etc/netdata -# Store the local helper for backup/restore scripts -sudo cp $SRCPATH/_common.sh /opt/netdata/etc/netdata - -# Modify Nginx configuration file and copy it to Nginx conf directory -if [[ "$path" == "/" ]] ; then - nginx_conf=$SRCPATH/../conf/nginx_root.conf -else - nginx_conf=$SRCPATH/../conf/nginx_sub_dir.conf -fi - -sed -i "s@YNH_WWW_PATH@${path}@g" $nginx_conf -sudo cp $nginx_conf /etc/nginx/conf.d/$domain.d/$app.conf +# Store the app.src file +popd +cp ../conf/app.src /opt/netdata/etc/netdata +#================================================= +# GENERIC FINALIZATION +#================================================= +# SETUP SSOWAT +#================================================= # If app is public, add url to SSOWat conf as skipped_uris if [[ $is_public -eq 1 ]]; then # See install script ynh_app_setting_set "$app" unprotected_uris "/" fi -# Reload nginx service -sudo systemctl reload nginx \ No newline at end of file +#================================================= +# RELOAD NGINX +#================================================= + +systemctl reload nginx +