From cc61abbeccd16df74f95352160360282dd0b271b Mon Sep 17 00:00:00 2001 From: Jimmy Monin Date: Sun, 12 Mar 2017 12:40:26 +0100 Subject: [PATCH] Refactoring and cleaning, levels 4 & 5 validation --- check_process | 6 ++- conf/influxdb-grafana-deps.control | 12 ----- manifest.json | 1 + scripts/_common.sh | 82 ++++++++++++++++++++++++++++++ scripts/install | 44 ++-------------- scripts/remove | 8 ++- scripts/restore | 34 ++----------- scripts/upgrade | 2 +- 8 files changed, 101 insertions(+), 88 deletions(-) delete mode 100644 conf/influxdb-grafana-deps.control diff --git a/check_process b/check_process index a6ef101..05e8dc1 100644 --- a/check_process +++ b/check_process @@ -26,8 +26,10 @@ Level 1=auto Level 2=auto Level 3=auto - Level 4=0 - Level 5=0 +# https://github.com/YunoHost-Apps/grafana_ynh/issues/4 + Level 4=1 +# https://github.com/YunoHost-Apps/grafana_ynh/issues/5 + Level 5=1 Level 6=auto Level 7=auto Level 8=0 diff --git a/conf/influxdb-grafana-deps.control b/conf/influxdb-grafana-deps.control deleted file mode 100644 index da708a6..0000000 --- a/conf/influxdb-grafana-deps.control +++ /dev/null @@ -1,12 +0,0 @@ -Section: misc -Priority: optional -Homepage: http://grafana.org/ -Standards-Version: 3.9.2 - -Package: influxdb-grafana-deps -Version: 4.1.1-1 -Depends: influxdb, grafana -Architecture: all -Description: meta package for InfluxDB/Grafana dependencies - . - This meta-package is only responsible of installing dependencies. diff --git a/manifest.json b/manifest.json index c03762d..b402678 100644 --- a/manifest.json +++ b/manifest.json @@ -6,6 +6,7 @@ "en": "Beautiful metric & analytic dashboards for monitoring", "fr": "Tableaux de bords de supervision" }, + "version": "1.0.0", "license": "Apache-2.0", "url": "http://grafana.org/", "maintainer": { diff --git a/scripts/_common.sh b/scripts/_common.sh index fe51eea..a7f8ad0 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -20,4 +20,86 @@ fix_path() { path="${path:0:${#path}-1}" fi echo "$path" +} +# Install needed repositories and install dependencies +# usage: install_dependencies +install_dependencies() { + # Install needed dependency for HTTPS apt access + # (that dependency could be handled upstream in YunoHost) + 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 + # 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 + fi + + # Install packages + # We install them as dependencies as they may already be installed and used for other purposes + ynh_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" + } +} + + +# ======== Future YunoHost helpers ======== +# Install dependencies with a equivs control file +# +# usage: ynh_app_dependencies dep [dep [...]] +# | arg: dep - the package name to install in dependence +ynh_app_dependencies () { + dependencies=$@ + manifest_path="../manifest.json" + if [ ! -e "$manifest_path" ]; then + manifest_path="../settings/manifest.json" # Into the restore script, the manifest is not at the same place + fi + version=$(sudo python3 -c "import sys, json;print(json.load(open(\"$manifest_path\"))['version'])") # Retrieve the version number in the manifest file. + dep_app=${app//_/-} # Replace all '_' by '-' + cat > ./${dep_app}-ynh-deps.control << EOF # Make a control file for equivs-build +Section: misc +Priority: optional +Package: ${dep_app}-ynh-deps +Version: ${version} +Depends: ${dependencies// /, } +Architecture: all +Description: Fake package for ${app} (YunoHost app) dependencies + This meta-package is only responsible of installing its dependencies. +EOF + ynh_package_install_from_equivs ./${dep_app}-ynh-deps.control \ + || ynh_die "Unable to install dependencies" # Install the fake package and its dependencies +} + +# Remove fake package and its dependencies +# +# Dependencies will removed only if no other package need them. +# +# usage: ynh_remove_app_dependencies +ynh_remove_app_dependencies () { + dep_app=${app//_/-} # Replace all '_' by '-' + ynh_package_autoremove ${dep_app}-ynh-deps # Remove the fake package and its dependencies if they not still used. } \ No newline at end of file diff --git a/scripts/install b/scripts/install index 49a1e01..aaa569d 100644 --- a/scripts/install +++ b/scripts/install @@ -30,46 +30,9 @@ sudo yunohost app checkurl "${domain}${path}" -a "$app" \ ynh_app_setting_set "$app" admin "$admin" ynh_app_setting_set "$app" is_public "$is_public" -# Install needed dependency for HTTPS apt access -# (that dependency could be handled upstream in YunoHost) -ynh_package_install apt-transport-https +# Install dependencies +install_dependencies -# Test repository 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 - # 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 -fi - -# Install packages -# We install them as dependencies as they may already be installed and used for other purposes -ynh_package_install_from_equivs ../conf/influxdb-grafana-deps.control \ -|| { - # Remove apt repositories if they were added - [[ -n "$influxdb_repository_present" ]] && rm $INFLUXDB_REPOSITORY - [[ -n "$grafana_repository_present" ]] && rm $GRAFANA_REPOSITORY - ynh_die "Unable to install Debian packages" -} # If NetData is installed, configure it to feed InfluxDB netdata_conf="/opt/netdata/etc/netdata/netdata.conf" if [[ -f "$netdata_conf" ]] ; then @@ -166,7 +129,6 @@ sudo systemctl enable grafana-server.service # Store useful files for backup/restore scripts sudo cp _common.sh /etc/grafana -sudo cp ../conf/influxdb-grafana-deps.control /etc/grafana # Modify Nginx configuration file and copy it to Nginx conf directory if [[ "$path" == "/" ]] ; then @@ -185,4 +147,4 @@ if [[ $is_public -eq 1 ]]; then fi # Reload services -sudo service nginx reload +sudo systemctl reload nginx diff --git a/scripts/remove b/scripts/remove index 2d2f35d..d7e69a4 100644 --- a/scripts/remove +++ b/scripts/remove @@ -6,6 +6,9 @@ set -u # See comments in install script app=$YNH_APP_INSTANCE_NAME +# Source local helpers +source ./_common.sh + # Source YunoHost helpers source /usr/share/yunohost/helpers @@ -17,7 +20,10 @@ sudo systemctl stop grafana-server 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 @@ -52,6 +58,6 @@ ynh_mysql_drop_user "$dbuser" || true # Reload nginx service -sudo service nginx reload +sudo systemctl reload nginx diff --git a/scripts/restore b/scripts/restore index da6739b..c9389b4 100644 --- a/scripts/restore +++ b/scripts/restore @@ -14,7 +14,6 @@ path=$(ynh_app_setting_get "$app" path) # Fix permissions sudo chmod a+rx ./conf_grafana/_common.sh -sudo chmod a+r ./conf_grafana/influxdb-grafana-deps.control # Source local helpers source ./conf_grafana/_common.sh @@ -27,35 +26,8 @@ path=$(fix_path $path) sudo yunohost app checkurl "${domain}${path}" -a "$app" \ || ynh_die "Path not available: ${domain}${path}" -# Install needed dependency for HTTPS apt access -# (that dependency could be handled upstream in YunoHost) -ynh_package_install apt-transport-https - -# 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 - # 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 -fi - -# Install packages -# We install them as dependencies as they may already be installed and used for other purposes -ynh_package_install_from_equivs ./conf_grafana/influxdb-grafana-deps.control \ -|| ynh_die "Unable to install Debian packages" +# Install dependencies +install_dependencies # Create and restore Grafana database dbname=$app @@ -88,4 +60,4 @@ sudo systemctl restart netdata || true sudo cp -a $SRCPATH/nginx.conf "/etc/nginx/conf.d/${domain}.d/${app}.conf" # Restart webserver -sudo service nginx reload +sudo systemctl reload nginx diff --git a/scripts/upgrade b/scripts/upgrade index b4fd681..e59be3f 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -42,4 +42,4 @@ if [[ $is_public -eq 1 ]]; then fi # Reload nginx service -sudo service nginx reload \ No newline at end of file +sudo systemctl reload nginx \ No newline at end of file