LOKI_VERSION=$(curl -s "https://api.github.com/repos/grafana/loki/releases/latest" | grep -Po '"tag_name": "v\K[0-9.]+')
mkdir -p /opt/loki/{bin,systemd}
cat > /etc/profile.d/loki.sh <<EOF
#!/bin/bash
export PATH="${PATH}:/opt/loki/bin"
EOF
wget -qO /opt/loki/loki.gz "https://github.com/grafana/loki/releases/download/v${LOKI_VERSION}/loki-linux-amd64.zip"
gunzip /opt/loki/loki.gz
mv loki /opt/loki/loki /opt/loki/bin/loki
rm /opt/loki/loki.gz
source /etc/profile.d/loki.sh
wget -qO /opt/loki/conf/loki.conf "https://raw.githubusercontent.com/grafana/loki/v${LOKI_VERSION}/cmd/loki/loki-local-config.yaml"
cat > /opt/loki/systemd/loki.service<<EOF
[Unit]
Description=Loki log aggregation system
After=network.target
[Service]
ExecStart=/opt/loki/bin/loki -config.file=/opt/loki/conf/loki.conf
Restart=always
[Install]
WantedBy=multi-user.target
EOF
ln -sf /opt/loki/systemd/loki.service /etc/systemd/system/
systemctl daemon-reload
systemctl start loki