2017-02-18 16:45:43 +01:00
#!/bin/bash
2017-12-03 19:24:03 +01:00
#=================================================
2020-05-17 09:28:22 +02:00
# GENERIC START
2017-12-03 19:24:03 +01:00
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
2017-02-18 16:45:43 +01:00
2017-12-03 19:24:03 +01:00
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
2020-05-17 09:28:22 +02:00
ynh_clean_setup () {
### Remove this function if there's nothing to clean before calling the remove script.
true
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
2017-12-03 19:24:03 +01:00
#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================
2017-02-18 16:45:43 +01:00
domain=$YNH_APP_ARG_DOMAIN
2020-05-17 09:28:22 +02:00
path_url=$YNH_APP_ARG_PATH
2017-03-05 14:41:56 +01:00
admin=$YNH_APP_ARG_ADMIN
2017-02-18 16:45:43 +01:00
is_public=$YNH_APP_ARG_IS_PUBLIC
2020-05-17 09:28:22 +02:00
app=$YNH_APP_INSTANCE_NAME
#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
2017-02-18 16:45:43 +01:00
2020-05-17 09:28:22 +02:00
ynh_script_progression --message="Validating installation parameters..." --weight=1
2017-02-18 16:45:43 +01:00
2020-05-17 09:28:22 +02:00
# Register (book) web path
ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url
2017-02-18 16:45:43 +01:00
2020-05-17 09:28:22 +02:00
#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================
ynh_script_progression --message="Storing installation settings..." --weight=2
ynh_app_setting_set --app=$app --key=domain --value=$domain
ynh_app_setting_set --app=$app --key=path --value=$path_url
ynh_app_setting_set --app=$app --key=admin --value=$admin
ynh_app_setting_set --app=$app --key=is_public --value=$is_public
#=================================================
# STANDARD MODIFICATIONS
#=================================================
# FIND AND OPEN A PORT
#=================================================
ynh_script_progression --message="Configuring port..." --weight=1
2017-02-18 16:45:43 +01:00
2020-05-17 09:28:22 +02:00
# Find an available port
port=$(ynh_find_port --port=3000)
ynh_app_setting_set --app=$app --key=port --value=$port
2017-02-18 16:45:43 +01:00
2020-05-17 09:28:22 +02:00
#=================================================
# INSTALL DEPENDENCIES
#=================================================
ynh_script_progression --message="Installing dependencies..." --weight=44
ynh_install_app_dependencies $pkg_dependencies
ynh_install_extra_app_dependencies --repo="deb https://packages.grafana.com/oss/deb stable main" --package="grafana" --key="https://packages.grafana.com/gpg.key"
#=================================================
# CREATE A MYSQL DATABASE
#=================================================
ynh_script_progression --message="Creating a MySQL database..." --weight=2
db_name=$(ynh_sanitize_dbid --db_name=$app)
db_user=$db_name
ynh_app_setting_set --app=$app --key=db_name --value=$db_name
ynh_mysql_setup_db --db_user=$db_user --db_name=$db_name
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
# None in this package (all APT-based)
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Configuring nginx web server..." --weight=1
2017-08-03 07:38:57 +02:00
2020-05-17 09:28:22 +02:00
### `ynh_add_nginx_config` will use the file conf/nginx.conf
# Create a dedicated nginx config
ynh_add_nginx_config
#=================================================
# SPECIFIC SETUP
#=================================================
ynh_script_progression --message="Configuring Grafana and InfluxDB..." --weight=30
2017-02-18 16:45:43 +01:00
# If NetData is installed, configure it to feed InfluxDB
netdata_conf="/opt/netdata/etc/netdata/netdata.conf"
2020-05-17 09:28:22 +02:00
if [ -f "$netdata_conf" ] ; then
2017-02-18 16:45:43 +01:00
# If there is already a [backend] section
2020-05-17 09:28:22 +02:00
if [ -n "$(cat $netdata_conf | grep '\[backend\]')" ] ; then
2017-02-18 16:45:43 +01:00
# These regexps replaces patterns inside ini [sections] ([backend] section, here)
2020-05-17 09:28:22 +02:00
# (source: https://stackoverflow.com/a/16987794)
sed -i '/^\[backend\]$/,/^\[/ {
2017-02-18 16:45:43 +01:00
s/# enabled = no/enabled = yes/
s/# type = graphite/type = opentsdb/
s/# destination = localhost/destination = localhost:4242/
2018-05-26 09:28:18 +02:00
s/# update every = 10/update every = 60/
2017-02-18 16:45:43 +01:00
}' $netdata_conf
else
# Otherwise create the section
echo "[backend]
enabled = yes
type = opentsdb
2020-05-17 09:28:22 +02:00
destination = localhost:4242" | tee -a $netdata_conf
2017-02-18 16:45:43 +01:00
fi
2018-05-26 09:28:18 +02:00
2017-02-18 16:45:43 +01:00
# Restart NetData
2020-05-17 09:28:22 +02:00
ynh_systemd_action --service_name=netdata --action="restart"
2017-02-18 16:45:43 +01:00
fi
# Configure InfluxDB
if [ -f /etc/influxdb/influxdb.conf ] ; then
2020-05-17 09:28:22 +02:00
sed -i '/^\[\[opentsdb\]\]$/,/^\[/ s/enabled = false/enabled = true/' /etc/influxdb/influxdb.conf
2017-02-18 16:45:43 +01:00
else
2020-05-17 09:28:22 +02:00
[ -d /etc/influxdb ] || mkdir /etc/influxdb
cp ../conf/influxdb.conf /etc/influxdb
2017-02-18 16:45:43 +01:00
fi
# Start InfluxDB server
2020-05-17 09:28:22 +02:00
ynh_systemd_action --service_name=influxdb --action="restart"
2017-02-18 16:45:43 +01:00
# Configure Grafana
2020-05-17 09:28:22 +02:00
cp ../conf/ldap.toml /etc/grafana
2017-02-18 16:45:43 +01:00
grafana_conf="/etc/grafana/grafana.ini"
2017-05-14 18:26:27 +02:00
# Set final port
2020-05-17 09:28:22 +02:00
sed -i "/^\[server\]$/,/^\[/ s@;http_port = .*@http_port = $port@" $grafana_conf
2019-01-13 18:51:59 +01:00
# Set domain
2020-05-17 09:28:22 +02:00
sed -i "/^\[server\]$/,/^\[/ s@;domain = .*@domain = $domain@" $grafana_conf
2017-02-18 16:45:43 +01:00
# Set final URL
2020-05-17 09:28:22 +02:00
sed -i "/^\[server\]$/,/^\[/ s@;root_url = .*@root_url = https://$domain$path_url@" $grafana_conf
# Specify if using sub_path
if [ "$path_url" != "/" ]; then
sed -i "/^\[server\]$/,/^\[/ s@;serve_from_sub_path = .*@serve_from_sub_path = true@" $grafana_conf
else
sed -i "/^\[server\]$/,/^\[/ s@;serve_from_sub_path = .*@serve_from_sub_path = false@" $grafana_conf
fi
2017-02-18 16:45:43 +01:00
# Disable check for updates
2020-05-17 09:28:22 +02:00
sed -i '/^\[analytics\]$/,/^\[/ s/;check_for_updates = .*/check_for_updates = false/' $grafana_conf
# Disable analytics reporting
sed -i '/^\[analytics\]$/,/^\[/ s/;reporting_enabled = .*/reporting_enabled = false/' $grafana_conf
2017-03-05 15:25:18 +01:00
# Disable organization creation
2020-05-17 09:28:22 +02:00
sed -i '/^\[users\]$/,/^\[/ s/;allow_org_create = .*/allow_org_create = false/' $grafana_conf
2017-02-18 16:45:43 +01:00
# Enable HTTP and LDAP authentication
2020-05-17 09:28:22 +02:00
sed -i '/^\[auth.basic\]$/,/^\[/ s/;enabled = .*/enabled = false/' $grafana_conf
sed -i '/^\[auth.proxy\]$/,/^\[/ s/;enabled = .*/enabled = true/' $grafana_conf
sed -i '/^\[auth.ldap\]$/,/^\[/ {
2017-02-18 16:45:43 +01:00
s/;enabled = .*/enabled = true/
s/;allow_sign_up = .*/allow_sign_up = true/
}' $grafana_conf
# Set log level to debug
2020-05-17 09:28:22 +02:00
sed -i '/^\[log\]$/,/^\[/ s/;level = .*/level = debug/' $grafana_conf
2017-02-18 16:45:43 +01:00
# Change URL and database credentials
2020-05-17 09:28:22 +02:00
sed -i "/^\[database\]$/,/^\[/ {
2017-02-18 16:45:43 +01:00
s/;type = .*/type = mysql/
2020-05-17 09:28:22 +02:00
s/;name = .*/name = $db_name/
s/;user = .*/user = $db_user/
s/;\?password =.*/password = $db_pwd/
2017-02-18 16:45:43 +01:00
}" $grafana_conf
2020-05-17 09:28:22 +02:00
# Calculate and store the config file checksum into the app settings
ynh_store_file_checksum --file="$grafana_conf"
2019-01-13 18:51:59 +01:00
# Start Grafana and wait for it to be fully started
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
2017-02-18 16:45:43 +01:00
2017-03-05 14:41:56 +01:00
# Change admin name to the specified one
mail=$(ynh_user_get_info "$admin" 'mail')
2020-05-17 09:28:22 +02:00
ynh_mysql_connect_as --user=$db_user --password=$db_pwd --database=$db_name <<< "UPDATE user SET login=\"$admin\", email=\"$mail\" WHERE login=\"admin\";"
# Create InfluxDB data source
ynh_mysql_connect_as --user=$db_user --password=$db_pwd --database=$db_name <<< "INSERT INTO data_source (id, org_id, version, type, name, access, url, password, user, \`database\`, basic_auth, basic_auth_user, basic_auth_password, is_default, json_data, created, updated, with_credentials, secure_json_data) VALUES (99999, 1, 0, 'influxdb', 'InfluxDB', 'proxy', 'http://localhost:8086', '', '', 'opentsdb', 0, '', '', 0, '{\"tsdbResolution\":1,\"tsdbVersion\":1}', '2017-02-12 14:35:33', '2017-02-12 14:36:27', 0, '{}');"
# Import default dashboard for NetData (source: https://grafana.com/grafana/dashboards/2701)
# Remove new lines
tr -d '\n' < ../conf/netdata_dashboard.json > 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 (source: https://stackoverflow.com/a/4383994)
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 <<< "INSERT INTO dashboard (id, version, slug, title, data, org_id, created, updated, uid) VALUES
(99999, 0, 'NetData', 'NetData', \"$escaped_dashboard\", 1, '2020-05-16 14:36:50', '2020-05-16 14:36:50', 'yunohost');"
# Add dashboard version
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\");"
2017-03-05 14:41:56 +01:00
2020-05-17 09:28:22 +02:00
# Enable the systemd service so that InfluxDB and Grafana start at boot
systemctl enable influxdb.service
systemctl enable grafana-server.service
2017-03-05 14:41:56 +01:00
2020-05-17 09:28:22 +02:00
#=================================================
# GENERIC FINALIZATION
#=================================================
2017-02-18 16:45:43 +01:00
2020-05-17 09:28:22 +02:00
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
2017-02-18 16:45:43 +01:00
2020-05-17 09:28:22 +02: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"
2017-02-18 16:45:43 +01:00
2020-05-17 09:28:22 +02:00
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..." --weight=2
2017-02-18 16:45:43 +01:00
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
#=================================================
# SETUP SSOWAT
#=================================================
ynh_script_progression --message="Configuring SSOwat..." --weight=1
2017-02-18 16:45:43 +01:00
2020-05-17 09:28:22 +02:00
# Make app public if necessary
if [ $is_public -eq 1 ]
then
# unprotected_uris allows SSO credentials to be passed anyway.
ynh_app_setting_set --app=$app --key=unprotected_uris --value="/"
2017-02-18 16:45:43 +01:00
fi
2020-05-17 09:28:22 +02:00
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading nginx web server..." --weight=1
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
2019-01-13 18:51:59 +01:00
2020-05-17 09:28:22 +02:00
ynh_script_progression --message="Installation of $app completed" --last