crazy-max/diun

[Script] Diun easy install & update -> linux amd64 only.

Ramalama2 opened this issue · 0 comments

Description

Hi, i've made a small script, that installs and updates diun.

The reason is here, i have multiple LXC Docker Containers on Multiple Proxmox hosts and well, i didn't wanted to make it by hand.

What the script does:
You can define your own user/group (in the script) -> Helpfull if your docker runs as root by default
You can simply execute the script and the script will install or update diun. -> Sinply execute it multiple times.
Stops the diun Service, if it runs,
Downloads the newest linux amd64 binary into temp folder,
Copyes it over to /usr/local/bin,
Creates the user/group only if it doesn't exist already,
Creates the folders only if it doesn't exist,
Creates the diun yaml only if there isn't one already,
Creates the Systemd Service and overwrites it, cause the service is anyway always the same.

Its very basic, but does the job.

The only thing thats left, is modify the /etc/diun/diun.yml to your needs.

The script, name it as you like (open me)
#!/bin/bash
temp_folder=/tmp/diun
repo="crazy-max/diun"
diun_user=diun
diun_group=diun

if systemctl --quiet is-active diun.service
then
    sudo systemctl stop diun.service
    echo "diun.service has been stopped"
fi

mkdir -p $temp_folder
cd $temp_folder
latest_release=$(curl --silent "https://api.github.com/repos/$repo/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
url="https://github.com/$repo/releases/download/$latest_release/diun_${latest_release:1}_linux_amd64.tar.gz"
wget $url
tar -xzvf $temp_folder/diun_${latest_release:1}_linux_amd64.tar.gz -C $temp_folder/
rm -f $temp_folder/diun_${latest_release:1}_linux_amd64.tar.gz

if [ "$diun_group" = "root" ]; then
    echo "Group: root, don't create a new group!"
else
    echo "Group: $diun_group, creating if not exists!"
    if ! getent group "$diun_group" >/dev/null; then
        groupadd "$diun_group"
        echo "Group: '$diun_group' created successfully."
    else
        echo "Group: '$diun_group' already exists."
    fi
fi

if [ "$diun_user" = "root" ]; then
    echo "User: root, don't create a new user!"
else
    echo "User: $diun_user, creating if not exists!"
    if ! id -u $diun_user >/dev/null 2>&1; then
       useradd -s /bin/false -d /bin/null -g $diun_group $diun_user
       echo "User $diun_user added successfully."
    else
       echo "User $diun_user already exists."
    fi
fi

if [ ! -d "/var/lib/diun" ]; then
    mkdir -p /var/lib/diun
fi
if [ ! -d "/etc/diun" ]; then
    mkdir -p /etc/diun
fi
chown $diun_user:$diun_group /var/lib/diun/
chmod -R 750 /var/lib/diun/
chown $diun_user:$diun_group /etc/diun
chmod 770 /etc/diun

if [ ! -f "/etc/diun/diun.yml" ]; then
    touch /etc/diun/diun.yml
fi
chown $diun_user:$diun_group /etc/diun/diun.yml
chmod 644 /etc/diun/diun.yml

cp $temp_folder/diun /usr/local/bin/diun
chmod +x /usr/local/bin/diun

read -r -d '' SERVICE <<- EOM
[Unit]
Description=Diun
Documentation=https://crazymax.dev/diun/
After=syslog.target
After=network.target

[Service]
RestartSec=2s
Type=simple
User=$diun_user
Group=$diun_group
ExecStart=/usr/local/bin/diun serve --config /etc/diun/diun.yml --log-level warn
Restart=always
Environment=TZ=Europe/Berlin
Environment=DIUN_DB_PATH=/var/lib/diun/diun.db

[Install]
WantedBy=multi-user.target
EOM

# Write the service file
echo "$SERVICE" > /etc/systemd/system/diun.service

# Reload the systemd daemon to recognize the new service
systemctl daemon-reload
rm -rf $temp_folder

echo "Diun Successfully installed or updated"

Hope that helps someone.
Do with that script what you like, i don't care.

Hopefully something like this script "but enhanced" could make it to the install docs of diun.
With enhanced, i mean for all platforms and all distros.

Thx & Cheers