1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/grafana_ynh.git synced 2024-09-03 20:36:29 +02:00
grafana_ynh/scripts/upgrade

192 lines
7.2 KiB
Text
Raw Normal View History

2017-02-18 16:45:43 +01:00
#!/bin/bash
2020-05-17 09:28:22 +02:00
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
2017-02-18 16:45:43 +01:00
2020-05-17 09:28:22 +02:00
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading installation settings..." --weight=1
2017-02-18 16:45:43 +01:00
app=$YNH_APP_INSTANCE_NAME
2020-05-17 09:28:22 +02:00
domain=$(ynh_app_setting_get --app=$app --key=domain)
path_url=$(ynh_app_setting_get --app=$app --key=path)
admin=$(ynh_app_setting_get --app=$app --key=admin)
db_name=$(ynh_app_setting_get --app=$app --key=db_name)
db_user=$db_name
db_pwd=$(ynh_app_setting_get --app=$app --key=mysqlpwd)
port=$(ynh_app_setting_get --app=$app --key=port)
2017-02-18 16:45:43 +01:00
2020-05-17 09:28:22 +02:00
#=================================================
# CHECK VERSION
#=================================================
2021-08-22 16:17:29 +02:00
ynh_script_progression --message="Checking version..."
2017-02-18 16:45:43 +01:00
2020-05-17 09:28:22 +02:00
upgrade_type=$(ynh_check_app_version_changed)
2021-08-22 16:17:29 +02:00
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
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"
2021-08-22 16:17:29 +02:00
# Backup the current version of the app
ynh_backup_before_upgrade
ynh_clean_setup () {
2022-01-29 03:49:58 +01:00
ynh_clean_check_starting
2021-08-22 16:17:29 +02:00
# Restore it if the upgrade fails
ynh_restore_upgradebackup
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# STANDARD UPGRADE STEPS
2020-05-17 09:28:22 +02:00
#=================================================
# 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)
ynh_app_setting_set --app=$app --key=db_name --value=$db_name
fi
2017-02-18 16:45:43 +01:00
2021-08-03 08:02:26 +02:00
# Cleaning legacy permissions
if ynh_legacy_permissions_exists; then
ynh_legacy_permissions_delete_all
ynh_app_setting_delete --app=$app --key=is_public
fi
2022-01-29 03:49:58 +01:00
# Migration: remove old repository if defined
2022-01-27 00:30:36 +01:00
if [ -f "/etc/apt/sources.list.d/grafana_stable.list" ] ; then
ynh_secure_remove --file="/etc/apt/sources.list.d/grafana_stable.list"
fi
2020-05-17 09:28:22 +02:00
#=================================================
# NGINX CONFIGURATION
#=================================================
2021-08-03 08:02:26 +02:00
ynh_script_progression --message="Upgrading NGINX web server configuration..." --weight=1
2021-08-22 16:17:29 +02:00
# Create a dedicated NGINX config
2020-05-17 09:28:22 +02:00
ynh_add_nginx_config
2017-08-03 07:38:57 +02:00
2020-05-17 09:28:22 +02:00
#=================================================
# UPGRADE DEPENDENCIES
#=================================================
ynh_script_progression --message="Upgrading dependencies..." --weight=12
2020-05-17 09:28:22 +02:00
ynh_install_app_dependencies $pkg_dependencies
2022-01-29 03:49:58 +01:00
ynh_install_extra_app_dependencies --repo="deb https://packages.grafana.com/oss/deb stable main" --package="grafana" --key="https://packages.grafana.com/gpg.key"
2017-02-18 16:45:43 +01:00
2020-05-17 09:28:22 +02:00
#=================================================
# SPECIFIC UPGRADE
#=================================================
2021-08-22 16:17:29 +02:00
# CONFIGURING GRAFANA AND INFLUXDB
#=================================================
2020-05-17 09:28:22 +02:00
ynh_script_progression --message="Configuring Grafana and InfluxDB..." --weight=3
2021-08-22 16:17:29 +02:00
2020-05-17 09:28:22 +02:00
# 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 <<EOF
[opentsdb:yunohost]
enabled = yes
destination = localhost:4242
# data source = average
# prefix = netdata
# hostname = my_hostname
update every = 60
# buffer on failures = 10
# timeout ms = 20000
# send names instead of ids = yes
# send charts matching = *
# send hosts matching = localhost *
EOF
2020-05-17 09:28:22 +02:00
fi
# Remove obsolete NetData backend if in use
netdata_conf="/opt/netdata/etc/netdata/netdata.conf"
if [ -f "$netdata_conf" ] ; then
# If there is already a [backend] section
if [ -n "$(cat $netdata_conf | grep '\[backend\]')" ] ; then
# These regexps replaces patterns inside ini [sections] ([backend] section, here)
sed -i '/^\[backend\]$/,/^\[/ {
s/enabled = yes/enabled = no/
}' $netdata_conf
else
# Otherwise create the section
echo "[backend]
enabled = yes
type = opentsdb
destination = localhost:4242" | tee -a $netdata_conf
fi
fi
# Restart NetData
ynh_systemd_action --service_name=netdata --action="restart"
2020-05-17 09:28:22 +02:00
fi
# Update default dashboard for NetData (source: https://grafana.com/grafana/dashboards/2701)
# Remove new lines
tr -d '\n' < ../conf/netdata_dashboard.json > dashboard.json
2022-01-29 03:49:58 +01:00
# Fill the template with the defined data source
2020-05-17 09:28:22 +02:00
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"
2022-01-29 03:49:58 +01:00
# Import dashboard into MySQL
2020-05-17 09:28:22 +02:00
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
#=================================================
2020-12-11 22:33:52 +01:00
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
2021-08-22 16:17:29 +02:00
ynh_script_progression --message="Integrating service in YunoHost..."
2020-12-11 22:33:52 +01:00
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"
2020-05-17 09:28:22 +02:00
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Restarting a systemd service..." --weight=2
2022-02-06 19:08:44 +01:00
systemctl daemon-reload
2020-05-17 09:28:22 +02:00
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
#=================================================
2021-08-03 08:02:26 +02:00
ynh_script_progression --message="Reloading NGINX web server..." --weight=1
2020-05-17 09:28:22 +02:00
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Upgrade of $app completed" --last