From 5a2de2c4d1564ccd7ef02925ef2db0eebe0b7c49 Mon Sep 17 00:00:00 2001 From: JimboJoe Date: Sun, 13 Jan 2019 18:51:59 +0100 Subject: [PATCH] Use Debian influxdb package and adapt to new Grafana APT repositories (including official ARMv7 package) (#17) --- check_process | 9 +++-- manifest.json | 6 ++-- scripts/_common.sh | 85 ++++++++++++++++++++++++++++++++++++---------- scripts/backup | 2 +- scripts/install | 16 ++++----- scripts/remove | 9 ++--- scripts/restore | 4 ++- scripts/upgrade | 3 ++ 8 files changed, 92 insertions(+), 42 deletions(-) diff --git a/check_process b/check_process index f81cb62..0355078 100644 --- a/check_process +++ b/check_process @@ -16,13 +16,9 @@ upgrade=1 backup_restore=1 multi_instance=0 - wrong_user=0 - wrong_path=1 incorrect_path=1 - corrupt_source=0 - fail_download_source=0 port_already_use=1 - final_path_already_use=0 + change_url=0 ;;; Levels Level 1=auto Level 2=auto @@ -36,3 +32,6 @@ Level 8=0 Level 9=0 Level 10=0 +;;; Options +Email= +Notification=none diff --git a/manifest.json b/manifest.json index 03ad20d..df8658d 100644 --- a/manifest.json +++ b/manifest.json @@ -6,7 +6,7 @@ "en": "Beautiful metric & analytic dashboards for monitoring", "fr": "Tableaux de bords de supervision" }, - "version": "1.1.0~ynh1", + "version": "1.2.0~ynh1", "license": "Apache-2.0", "url": "http://grafana.org/", "maintainer": { @@ -15,12 +15,12 @@ "url": "" }, "requirements": { - "yunohost": ">= 2.7.2" + "yunohost": ">= 3.0.0" }, "multi_instance": false, "services": [ "nginx", - "php5-fpm" + "php7.0-fpm" ], "arguments": { "install" : [ diff --git a/scripts/_common.sh b/scripts/_common.sh index e58bfc8..953a2d1 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -2,7 +2,6 @@ # # Common variables # -INFLUXDB_REPOSITORY="/etc/apt/sources.list.d/influxdb.list" GRAFANA_REPOSITORY="/etc/apt/sources.list.d/grafana_stable.list" # @@ -29,30 +28,22 @@ install_dependencies() { ynh_package_install apt-transport-https # Test repositories existence, in case of failed installation - influxdb_repository_present="" grafana_repository_present="" - [[ -f $INFLUXDB_REPOSITORY ]] && influxdb_repository_present="true" [[ -f $GRAFANA_REPOSITORY ]] && grafana_repository_present="true" - # Install needed apt repository for InfluxDB - curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add - - source /etc/os-release - test $VERSION_ID = "8" && echo "deb https://repos.influxdata.com/debian jessie stable" | sudo tee $INFLUXDB_REPOSITORY - # Install needed apt repository for Grafana machine=$(uname -m) # Add the repos depending on processor architecture - if [[ "$machine" =~ "x86" ]]; then - # x86 processor --> we use the official repository - curl -s https://packagecloud.io/install/repositories/grafana/stable/script.deb.sh | sudo bash - elif [[ "$machine" =~ "armv6" ]] ; then + + if [[ "$machine" =~ "armv6" ]] ; then # For ARM, use fg2it repository # https://github.com/fg2it/grafana-on-raspberry curl https://bintray.com/user/downloadSubjectPublicKey?username=bintray | sudo apt-key add - - echo "deb http://dl.bintray.com/fg2it/deb-rpi-1b jessie main" | sudo tee $GRAFANA_REPOSITORY - elif [[ "$machine" =~ "armv7" ]] ; then - curl https://bintray.com/user/downloadSubjectPublicKey?username=bintray | sudo apt-key add - - echo "deb http://dl.bintray.com/fg2it/deb jessie main" | sudo tee $GRAFANA_REPOSITORY + echo "deb http://dl.bintray.com/fg2it/deb-rpi-1b stretch main" | sudo tee $GRAFANA_REPOSITORY + else + # x86 processor --> we use the official repository + curl https://packages.grafana.com/gpg.key | sudo apt-key add - + echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee $GRAFANA_REPOSITORY fi # Install packages @@ -60,7 +51,6 @@ install_dependencies() { ynh_install_app_dependencies influxdb, grafana \ || { # Remove apt repositories if they were added - [[ -n "$influxdb_repository_present" ]] && sudo rm $INFLUXDB_REPOSITORY [[ -n "$grafana_repository_present" ]] && sudo rm $GRAFANA_REPOSITORY ynh_die "Unable to install Debian packages" } @@ -78,3 +68,64 @@ ynh_delete_file_checksum () { local checksum_setting_name=checksum_${1//[\/ ]/_} # Replace all '/' and ' ' by '_' ynh_app_setting_delete $app $checksum_setting_name } + +# Start or restart a service and follow its booting +# +# usage: ynh_check_starting "Line to match" [Log file] [Timeout] [Service name] +# +# | arg: Line to match - The line to find in the log to attest the service have finished to boot. +# | arg: Log file - The log file to watch; specify "systemd" to read systemd journal for specified service +# /var/log/$app/$app.log will be used if no other log is defined. +# | arg: Timeout - The maximum time to wait before ending the watching. Defaut 300 seconds. +# | arg: Service name +ynh_check_starting () { + local line_to_match="$1" + local service_name="${4:-$app}" + local app_log="${2:-/var/log/$service_name/$service_name.log}" + local timeout=${3:-300} + + echo "Starting of $service_name" >&2 + systemctl stop $service_name + local templog="$(mktemp)" + # Following the starting of the app in its log + if [ "$app_log" == "systemd" ] ; then + # Read the systemd journal + journalctl -u $service_name -f --since=-45 > "$templog" & + else + # Read the specified log file + tail -F -n0 "$app_log" > "$templog" & + fi + # Get the PID of the last command + local pid_tail=$! + systemctl start $service_name + + local i=0 + for i in `seq 1 $timeout` + do + # Read the log until the sentence is found, which means the app finished starting. Or run until the timeout. + if grep --quiet "$line_to_match" "$templog" + then + echo "The service $service_name has correctly started." >&2 + break + fi + echo -n "." >&2 + sleep 1 + done + if [ $i -eq $timeout ] + then + echo "The service $service_name didn't fully start before the timeout." >&2 + fi + + echo "" + ynh_clean_check_starting +} +# Clean temporary process and file used by ynh_check_starting +# (usually used in ynh_clean_setup scripts) +# +# usage: ynh_clean_check_starting + +ynh_clean_check_starting () { + # Stop the execution of tail. + kill -s 15 $pid_tail 2>&1 + ynh_secure_remove "$templog" 2>&1 +} diff --git a/scripts/backup b/scripts/backup index 99b3b5a..256cfc2 100644 --- a/scripts/backup +++ b/scripts/backup @@ -35,7 +35,7 @@ ynh_backup "/var/lib/grafana/plugins" "conf_grafana_plugins" # Backup InfluxDB data # Source: http://stackoverflow.com/questions/39501416/how-to-restore-data-base-using-influxd TMPDIR=$(mktemp -d) -influx -database opentsdb -execute "show series" && sudo influxd backup -database opentsdb $TMPDIR +influxd backup -database opentsdb $TMPDIR ynh_backup "$TMPDIR" "influxdb_data" # Dump the Grafana database diff --git a/scripts/install b/scripts/install index cfdbb30..e78014a 100644 --- a/scripts/install +++ b/scripts/install @@ -100,8 +100,10 @@ sudo cp ../conf/ldap.toml /etc/grafana grafana_conf="/etc/grafana/grafana.ini" # Set final port sudo sed -i "/^\[server\]$/,/^\[/ s@;http_port = .*@http_port = $port@" $grafana_conf +# Set domain +sudo sed -i "/^\[server\]$/,/^\[/ s@;domain = .*@domain = $domain@" $grafana_conf # Set final URL -sudo sed -i "/^\[server\]$/,/^\[/ s@;root_url = .*@root_url = https://$domain$path@" $grafana_conf +sudo sed -i "/^\[server\]$/,/^\[/ s@;root_url = .*@root_url = %(protocol)s://%(domain)s$path@" $grafana_conf # Disable check for updates sudo sed -i '/^\[analytics\]$/,/^\[/ s/;check_for_updates = .*/check_for_updates = false/' $grafana_conf # Disable organization creation @@ -124,13 +126,8 @@ sudo sed -i "/^\[database\]$/,/^\[/ { s/;\?password =.*/password = $dbpass/ }" $grafana_conf -# Restart grafana server -sudo systemctl restart grafana-server - -# Wait for Grafana to start and initialize database -while [ -z "$(sudo netstat -lnput|grep grafana)" ] ; do - sleep 5s -done +# Start Grafana and wait for it to be fully started +ynh_check_starting "HTTP Server Listen" "/var/log/grafana/grafana.log" "240" "grafana-server" # Change admin name to the specified one mail=$(ynh_user_get_info "$admin" 'mail') @@ -168,5 +165,8 @@ if [[ $is_public -eq 1 ]]; then ynh_app_setting_set "$app" unprotected_uris "/" fi +# Start Grafana and wait for it to be fully started +ynh_check_starting "HTTP Server Listen" "/var/log/grafana/grafana.log" "240" "grafana-server" + # Reload services sudo systemctl reload nginx diff --git a/scripts/remove b/scripts/remove index aae9a2a..5831461 100644 --- a/scripts/remove +++ b/scripts/remove @@ -31,18 +31,15 @@ sudo systemctl stop influxdb # Remove app dependencies ynh_remove_app_dependencies || true -# We keep this second removal for backward compatibility -ynh_package_autoremove "influxdb-grafana-deps" || true # If packages deinstalled (weren't installed priorly to package installation) # purge remaining files if [[ -n "$(dpkg-query --status grafana | grep -E "Status|deinstall")" ]] ; then sudo dpkg --purge grafana - sudo rm /etc/apt/sources.list.d/grafana_stable.list + sudo rm $GRAFANA_REPOSITORY fi if [[ -n "$(dpkg-query --status influxdb | grep -E "Status|deinstall")" ]] ; then sudo dpkg --purge influxdb - sudo rm /etc/apt/sources.list.d/influxdb.list fi # Remove services from YunoHost monitoring @@ -56,7 +53,7 @@ if [[ -f "$netdata_conf" ]] ; then s/enabled = yes/# enabled = no/ s/type = opentsdb/# type = graphite/ s/destination = localhost:4242/# destination = localhost/ - s/update every = 60/# update every = 10/ + s/update every = 60/# update every = 10/ }' $netdata_conf fi @@ -73,5 +70,3 @@ ynh_mysql_drop_user "$dbuser" || true # Reload nginx service sudo systemctl reload nginx - - diff --git a/scripts/restore b/scripts/restore index e200df1..94f8f51 100644 --- a/scripts/restore +++ b/scripts/restore @@ -60,7 +60,9 @@ ynh_mysql_connect_as "$dbuser" "$dbpass" "$dbname" < ./dump.sql # That happens when passing automated tests (NetData not present) if [ "$(ls -A $SRCPATH/influxdb_data)" ] ; then sudo influxd restore -metadir /var/lib/influxdb/meta $SRCPATH/influxdb_data - sudo influxd restore -database opentsdb -datadir /var/lib/influxdb/data $SRCPATH/influxdb_data + if [ "$(ls -A $SRCPATH/influxdb_data/opentsdb*)" ] ; then + sudo influxd restore -database opentsdb -datadir /var/lib/influxdb/data $SRCPATH/influxdb_data + fi fi # Restore configuration files sudo cp -a $SRCPATH/conf_influxdb/* /etc/influxdb diff --git a/scripts/upgrade b/scripts/upgrade index 4854988..edce867 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -51,6 +51,9 @@ sudo systemctl restart grafana-server sudo yunohost service add influxdb sudo yunohost service add grafana-server --log "/var/log/grafana/grafana.log" +# Upgrade dependencies if needed +install_dependencies + # Modify Nginx configuration file and copy it to Nginx conf directory if [[ "$path" == "/" ]] ; then nginx_conf=$SRCPATH/../conf/nginx_root.conf