#!/bin/bash #================================================= # GENERIC START #================================================= # IMPORT GENERIC HELPERS #================================================= source _common.sh source /usr/share/yunohost/helpers #================================================= # LOAD SETTINGS #================================================= #REMOVEME? ynh_script_progression --message="Loading installation settings..." --weight=1 #REMOVEME? app=$YNH_APP_INSTANCE_NAME #REMOVEME? domain=$(ynh_app_setting_get --app=$app --key=domain) #REMOVEME? path=$(ynh_app_setting_get --app=$app --key=path) #REMOVEME? admin=$(ynh_app_setting_get --app=$app --key=admin) #REMOVEME? db_name=$(ynh_app_setting_get --app=$app --key=db_name) #REMOVEME? db_user=$db_name #REMOVEME? db_pwd=$(ynh_app_setting_get --app=$app --key=mysqlpwd) #REMOVEME? port=$(ynh_app_setting_get --app=$app --key=port) #================================================= # CHECK VERSION #================================================= ynh_script_progression --message="Checking version..." upgrade_type=$(ynh_check_app_version_changed) #================================================= # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP #================================================= #REMOVEME? ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --weight=4 # Workaround for missing "/var/lib/grafana/plugins" mkdir -p "/var/lib/grafana/plugins" chown -R $app:$app "/var/lib/grafana/plugins" # Backup the current version of the app #REMOVEME? ynh_backup_before_upgrade #REMOVEME? ynh_clean_setup () { ynh_clean_check_starting # Restore it if the upgrade fails #REMOVEME? ynh_restore_upgradebackup } # Exit if an error occurs during the execution of the script #REMOVEME? ynh_abort_if_errors #================================================= # STANDARD UPGRADE STEPS #================================================= # ENSURE DOWNWARD COMPATIBILITY #================================================= ynh_script_progression --message="Ensuring downward compatibility..." --weight=1 # If db_name doesn't exist, create it if [ -z "$db_name" ]; then db_name=$(ynh_sanitize_dbid --db_name=$app) #REMOVEME? ynh_app_setting_set --app=$app --key=db_name --value=$db_name fi # Cleaning legacy permissions #REMOVEME? if ynh_legacy_permissions_exists; then #REMOVEME? ynh_legacy_permissions_delete_all ynh_app_setting_delete --app=$app --key=is_public fi # Migration: remove old repository if defined if [ -f "/etc/apt/sources.list.d/grafana_stable.list" ] ; then ynh_secure_remove --file="/etc/apt/sources.list.d/grafana_stable.list" fi #================================================= # NGINX CONFIGURATION #================================================= ynh_script_progression --message="Upgrading NGINX web server configuration..." --weight=1 # Create a dedicated NGINX config ynh_add_nginx_config #================================================= # UPGRADE DEPENDENCIES #================================================= #REMOVEME? ynh_script_progression --message="Upgrading dependencies..." --weight=12 #REMOVEME? ynh_install_app_dependencies $pkg_dependencies #REMOVEME? ynh_install_extra_app_dependencies --repo="deb https://packages.grafana.com/oss/deb stable main" --package="grafana" --key="https://packages.grafana.com/gpg.key" #================================================= # SPECIFIC UPGRADE #================================================= # CONFIGURING GRAFANA AND INFLUXDB #================================================= ynh_script_progression --message="Configuring Grafana and InfluxDB..." --weight=3 # If NetData is installed, configure it to feed InfluxDB if [ -d "/opt/netdata" ] ; then netdata_conf="/opt/netdata/etc/netdata/exporting.conf" if [ ! -f $netdata_conf ] ; then cp /opt/netdata/usr/lib/netdata/conf.d/exporting.conf $netdata_conf fi sed -i '/^\[exporting:global\]$/,/^\[/ { s/enabled = no/enabled = yes/ s/# update every = 10/update every = 60/ }' $netdata_conf if [ -z "$(grep "yunohost" $netdata_conf)" ] ; then cat >> $netdata_conf < dashboard.json # Fill the template with the defined data source sed -i 's/${DS_CENTCOM-INFLUXDB}/InfluxDB/g' dashboard.json # Escape the dashboard definition for MySQL query dashboard=$(cat dashboard.json) printf -v escaped_dashboard "%q" "$dashboard" # Import dashboard into MySQL ynh_mysql_connect_as --user=$db_user --password=$db_pwd --database=$db_name <<< "UPDATE dashboard set data=\"$escaped_dashboard\" WHERE id=99999;" # Insert dashboard version if non existent (for downward compatibility) ynh_mysql_connect_as --user=$db_user --password=$db_pwd --database=$db_name <<< "INSERT INTO dashboard_version (id, dashboard_id, parent_version, restored_from, version, created, created_by, message, data) VALUES (99999, 99999, 0, 0, 1, '2020-05-16 14:36:50', 1, 'YunoHost installer', \"$escaped_dashboard\");" > /dev/null 2>&1 || true #================================================= # GENERIC FINALIZATION #================================================= # INTEGRATE SERVICE IN YUNOHOST #================================================= ynh_script_progression --message="Integrating service in YunoHost..." yunohost service add influxdb --description="open source time series database" --log="/var/log/grafana/grafana.log" yunohost service add grafana-server --description="open source analytics and monitoring solution" --log="/var/log/grafana/grafana.log" #================================================= # START SYSTEMD SERVICE #================================================= ynh_script_progression --message="Restarting a systemd service..." --weight=2 systemctl daemon-reload ynh_systemd_action --service_name=grafana-server --action="restart" --log_path="/var/log/grafana/grafana.log" --line_match="HTTP Server Listen" --timeout=600 #================================================= # RELOAD NGINX #================================================= #REMOVEME? ynh_script_progression --message="Reloading NGINX web server..." --weight=1 #REMOVEME? ynh_systemd_action --service_name=nginx --action=reload #================================================= # END OF SCRIPT #================================================= ynh_script_progression --message="Upgrade of $app completed" --last