#!/bin/bash #================================================= # GENERIC START #================================================= # IMPORT GENERIC HELPERS #================================================= source _common.sh source /usr/share/yunohost/helpers #================================================= # MANAGE SCRIPT FAILURE #================================================= ynh_clean_setup () { ynh_clean_check_starting } # Exit if an error occurs during the execution of the script ynh_abort_if_errors #================================================= # RETRIEVE ARGUMENTS FROM THE MANIFEST #================================================= domain=$YNH_APP_ARG_DOMAIN path_url=$YNH_APP_ARG_PATH is_public=$YNH_APP_ARG_IS_PUBLIC admin=$YNH_APP_ARG_ADMIN app=$YNH_APP_INSTANCE_NAME #================================================= # CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS #================================================= ynh_script_progression --message="Validating installation parameters..." --weight=1 # Register (book) web path ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url #================================================= # 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 #================================================= # STANDARD MODIFICATIONS #================================================= # FIND AND OPEN A PORT #================================================= ynh_script_progression --message="Finding an available port..." --weight=1 # Find an available port port=$(ynh_find_port --port=3000) ynh_app_setting_set --app=$app --key=port --value=$port #================================================= # 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 #================================================= # NGINX CONFIGURATION #================================================= ynh_script_progression --message="Configuring NGINX web server..." --weight=1 # Create a dedicated NGINX config ynh_add_nginx_config #================================================= # SPECIFIC SETUP #================================================= # CONFIGURING GRAFANA AND INFLUXDB #================================================= ynh_script_progression --message="Configuring Grafana and InfluxDB..." --weight=30 # If NetData is installed, configure it to feed InfluxDB if [ -d "/opt/netdata/etc/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" /opt/netdata/etc/netdata 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 (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\");" # Enable the systemd service so that InfluxDB and Grafana start at boot systemctl enable influxdb.service --quiet systemctl enable grafana-server.service --quiet #================================================= # 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="Starting 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 #================================================= # SETUP SSOWAT #================================================= ynh_script_progression --message="Configuring permissions..." --weight=1 # Make app public if necessary if [ $is_public -eq 1 ] then # Everyone can access the app. # The "main" permission is automatically created before the install script. ynh_permission_update --permission="main" --add="visitors" fi #================================================= # RELOAD NGINX #================================================= ynh_script_progression --message="Reloading NGINX web server..." --weight=1 ynh_systemd_action --service_name=nginx --action=reload #================================================= # END OF SCRIPT #================================================= ynh_script_progression --message="Installation of $app completed" --last