This commit is contained in:
Aliaksei Pilko 2025-05-09 19:45:24 +01:00
parent 7d45266c30
commit c21a9345ec
2 changed files with 10 additions and 34 deletions

View File

@ -25,23 +25,19 @@ function update_script() {
check_container_storage check_container_storage
check_container_resources check_container_resources
# Check if installation is present | -f for file, -d for folder
if [[ ! -d /opt/garmin-grafana/ ]]; then if [[ ! -d /opt/garmin-grafana/ ]]; then
msg_error "No ${APP} Installation Found!" msg_error "No ${APP} Installation Found!"
exit exit
fi fi
# Crawling the new version and checking whether an update is required
RELEASE=$(curl -fsSL https://api.github.com/repos/arpanghosh8453/garmin-grafana/releases/latest | grep "tag_name" | awk '{print substr($2, 2, length($2)-3) }') RELEASE=$(curl -fsSL https://api.github.com/repos/arpanghosh8453/garmin-grafana/releases/latest | grep "tag_name" | awk '{print substr($2, 2, length($2)-3) }')
if [[ ! -d /opt/garmin-grafana/ ]] || [[ "${RELEASE}" != "$(cat /opt/${APP}_version.txt)" ]] || [[ ! -f /opt/${APP}_version.txt ]]; then if [[ ! -d /opt/garmin-grafana/ ]] || [[ "${RELEASE}" != "$(cat /opt/${APP}_version.txt)" ]] || [[ ! -f /opt/${APP}_version.txt ]]; then
# Stopping Services
msg_info "Stopping $APP" msg_info "Stopping $APP"
systemctl stop garmin-grafana systemctl stop garmin-grafana
systemctl stop grafana-server systemctl stop grafana-server
systemctl stop influxdb systemctl stop influxdb
msg_ok "Stopped $APP" msg_ok "Stopped $APP"
# Get required environment variables from the .env file
if [[ ! -f /opt/garmin-grafana/.env ]]; then if [[ ! -f /opt/garmin-grafana/.env ]]; then
msg_error "No .env file found in /opt/garmin-grafana/.env" msg_error "No .env file found in /opt/garmin-grafana/.env"
exit exit
@ -52,21 +48,17 @@ function update_script() {
exit exit
fi fi
# Creating Backup
msg_info "Creating Backup" msg_info "Creating Backup"
tar -czf "/opt/${APP}_backup_$(date +%F).tar.gz" /opt/garmin-grafana/.garminconnect /opt/garmin-grafana/.env tar -czf "/opt/${APP}_backup_$(date +%F).tar.gz" /opt/garmin-grafana/.garminconnect /opt/garmin-grafana/.env
mv /opt/garmin-grafana/ /opt/garmin-grafana-backup/ mv /opt/garmin-grafana/ /opt/garmin-grafana-backup/
msg_ok "Backup Created" msg_ok "Backup Created"
# Execute Update
msg_info "Updating $APP to v${RELEASE}" msg_info "Updating $APP to v${RELEASE}"
curl -fsSL -o "${RELEASE}.zip" "https://github.com/arpanghosh8453/garmin-grafana/archive/refs/tags/${RELEASE}.zip" curl -fsSL -o "${RELEASE}.zip" "https://github.com/arpanghosh8453/garmin-grafana/archive/refs/tags/${RELEASE}.zip"
unzip -q "${RELEASE}.zip" unzip -q "${RELEASE}.zip"
mv "garmin-grafana-${RELEASE}/" "/opt/garmin-grafana" mv "garmin-grafana-${RELEASE}/" "/opt/garmin-grafana"
rm -f "${RELEASE}.zip" rm -f "${RELEASE}.zip"
# Install python dependencies with uv
$STD uv sync --locked --project /opt/garmin-grafana/ $STD uv sync --locked --project /opt/garmin-grafana/
# Setup grafana provisioning configs
# shellcheck disable=SC2016 # shellcheck disable=SC2016
sed -i 's/\${DS_GARMIN_STATS}/garmin_influxdb/g' /opt/garmin-grafana/Grafana_Dashboard/Garmin-Grafana-Dashboard.json sed -i 's/\${DS_GARMIN_STATS}/garmin_influxdb/g' /opt/garmin-grafana/Grafana_Dashboard/Garmin-Grafana-Dashboard.json
sed -i 's/influxdb:8086/localhost:8086/' /opt/garmin-grafana/Grafana_Datasource/influxdb.yaml sed -i 's/influxdb:8086/localhost:8086/' /opt/garmin-grafana/Grafana_Datasource/influxdb.yaml
@ -81,19 +73,16 @@ function update_script() {
cp -r /opt/garmin-grafana-backup/.garminconnect /opt/garmin-grafana/.garminconnect cp -r /opt/garmin-grafana-backup/.garminconnect /opt/garmin-grafana/.garminconnect
msg_ok "Updated $APP to v${RELEASE}" msg_ok "Updated $APP to v${RELEASE}"
# Starting Services
msg_info "Starting $APP" msg_info "Starting $APP"
systemctl start garmin-grafana systemctl start garmin-grafana
systemctl start grafana-server systemctl start grafana-server
systemctl start influxdb systemctl start influxdb
msg_ok "Started $APP" msg_ok "Started $APP"
# Cleaning up
msg_info "Cleaning Up" msg_info "Cleaning Up"
rm -rf /opt/garmin-grafana-backup rm -rf /opt/garmin-grafana-backup
msg_ok "Cleanup Completed" msg_ok "Cleanup Completed"
# Last Action
echo "${RELEASE}" >/opt/${APP}_version.txt echo "${RELEASE}" >/opt/${APP}_version.txt
msg_ok "Update Successful" msg_ok "Update Successful"
else else

View File

@ -16,18 +16,16 @@ update_os
# Installing Dependencies # Installing Dependencies
msg_info "Installing Dependencies" msg_info "Installing Dependencies"
# Grafana dependencies $STD apt-get install -y \
$STD apt-get install -y gnupg gnupg \
$STD apt-get install -y apt-transport-https apt-transport-https \
$STD apt-get install -y software-properties-common software-properties-common \
# Influx dependencies lsb-base \
$STD apt-get install -y lsb-base lsb-release \
$STD apt-get install -y lsb-release gnupg2 \
$STD apt-get install -y gnupg2 python3 \
# garmin-grafana dependencies python3-requests \
$STD apt-get install -y python3 python3-dotenv
$STD apt-get install -y python3-requests
$STD apt-get install -y python3-dotenv
setup_uv setup_uv
msg_ok "Installed Dependencies" msg_ok "Installed Dependencies"
@ -46,10 +44,8 @@ $STD dpkg -i chronograf_1.10.7_amd64.deb
msg_ok "Installed InfluxDB" msg_ok "Installed InfluxDB"
msg_info "Setting up InfluxDB" msg_info "Setting up InfluxDB"
# Patch the config file to use the tsi1 index
$STD sed -i 's/# index-version = "inmem"/index-version = "tsi1"/' /etc/influxdb/influxdb.conf $STD sed -i 's/# index-version = "inmem"/index-version = "tsi1"/' /etc/influxdb/influxdb.conf
# Create InfluxDB user and database
INFLUXDB_USER="garmin_grafana_user" INFLUXDB_USER="garmin_grafana_user"
INFLUXDB_PASSWORD=$(openssl rand -base64 18 | tr -dc 'a-zA-Z0-9' | cut -c1-13) INFLUXDB_PASSWORD=$(openssl rand -base64 18 | tr -dc 'a-zA-Z0-9' | cut -c1-13)
INFLUXDB_NAME="GarminStats" INFLUXDB_NAME="GarminStats"
@ -78,9 +74,7 @@ msg_ok "Installed Grafana"
msg_info "Setting up Grafana" msg_info "Setting up Grafana"
GRAFANA_USER="admin" GRAFANA_USER="admin"
GRAFANA_PASS=$(openssl rand -base64 18 | tr -dc 'a-zA-Z0-9' | cut -c1-13) GRAFANA_PASS=$(openssl rand -base64 18 | tr -dc 'a-zA-Z0-9' | cut -c1-13)
# Create Grafana user
$STD grafana-cli admin reset-admin-password "${GRAFANA_PASS}" $STD grafana-cli admin reset-admin-password "${GRAFANA_PASS}"
# # Install plugins
$STD grafana-cli plugins install marcusolsson-hourly-heatmap-panel $STD grafana-cli plugins install marcusolsson-hourly-heatmap-panel
$STD systemctl restart grafana-server $STD systemctl restart grafana-server
# Output credentials to file # Output credentials to file
@ -101,9 +95,7 @@ if [[ "${RELEASE}" == v* ]]; then
RELEASE="${RELEASE:1}" RELEASE="${RELEASE:1}"
fi fi
mv "garmin-grafana-${RELEASE}/" "/opt/garmin-grafana" mv "garmin-grafana-${RELEASE}/" "/opt/garmin-grafana"
# Create dir for garmin tokens
mkdir -p /opt/garmin-grafana/.garminconnect mkdir -p /opt/garmin-grafana/.garminconnect
# Install python dependencies with uv
$STD uv sync --locked --project /opt/garmin-grafana/ $STD uv sync --locked --project /opt/garmin-grafana/
# Setup grafana provisioning configs # Setup grafana provisioning configs
# shellcheck disable=SC2016 # shellcheck disable=SC2016
@ -127,7 +119,6 @@ else
GARMIN_CN="False" GARMIN_CN="False"
fi fi
# Setup environment variables
cat <<EOF >/opt/garmin-grafana/.env cat <<EOF >/opt/garmin-grafana/.env
INFLUXDB_HOST=localhost INFLUXDB_HOST=localhost
INFLUXDB_PORT=8086 INFLUXDB_PORT=8086
@ -142,7 +133,6 @@ EOF
# garmin-grafana usually prompts the user for email and password (and MFA) on first run, # garmin-grafana usually prompts the user for email and password (and MFA) on first run,
# then stores a refreshable token. We try to avoid storing user credentials in the env vars # then stores a refreshable token. We try to avoid storing user credentials in the env vars
if [ -z "$(ls -A /opt/garmin-grafana/.garminconnect)" ]; then if [ -z "$(ls -A /opt/garmin-grafana/.garminconnect)" ]; then
# Get the email and password from the user
read -r -p "Please enter your Garmin Connect Email: " GARMIN_EMAIL read -r -p "Please enter your Garmin Connect Email: " GARMIN_EMAIL
read -r -p "Please enter your Garmin Connect Password (this is used to generate a token and NOT stored): " GARMIN_PASSWORD read -r -p "Please enter your Garmin Connect Password (this is used to generate a token and NOT stored): " GARMIN_PASSWORD
read -r -p "Please enter your MFA Code (if applicable, leave blank if not): " GARMIN_MFA read -r -p "Please enter your MFA Code (if applicable, leave blank if not): " GARMIN_MFA
@ -153,7 +143,6 @@ ${GARMIN_EMAIL}
${GARMIN_PASSWORD} ${GARMIN_PASSWORD}
${GARMIN_MFA} ${GARMIN_MFA}
EOF EOF
# Clear the credentials from the terminal
unset GARMIN_EMAIL unset GARMIN_EMAIL
unset GARMIN_PASSWORD unset GARMIN_PASSWORD
unset GARMIN_MFA unset GARMIN_MFA
@ -164,7 +153,6 @@ EOF
fi fi
fi fi
# Restart Grafana to pick up the provisioned data sources and dashboards
$STD systemctl restart grafana-server $STD systemctl restart grafana-server
# Add a script to make the manual bulk data import easier # Add a script to make the manual bulk data import easier
@ -197,7 +185,6 @@ EOF
chmod +x ~/bulk-import.sh chmod +x ~/bulk-import.sh
msg_ok "Set up garmin-grafana" msg_ok "Set up garmin-grafana"
# Creating Service (if needed)
msg_info "Creating Service" msg_info "Creating Service"
cat <<EOF >/etc/systemd/system/garmin-grafana.service cat <<EOF >/etc/systemd/system/garmin-grafana.service
[Unit] [Unit]