This commit is contained in:
rogier committed 2025-03-20 17:08:03 +01:00
1 parent e15b8c6d47
commit cfa24f0f74
3 files changed
+346 -2

No files matched your search

+86 -2
View File
@@ -1,2 +1,86 @@
# gitea
# Gitea Auto-Update Script
Dit script automatiseert het update proces van Gitea op een Linux systeem. Het is specifiek ontworpen om via cron te draaien en voert automatisch updates uit wanneer er een nieuwe versie beschikbaar is.
## Kenmerken
- Automatische detectie van nieuwe Gitea versies
- Volledig geautomatiseerd update proces
- Uitgebreide logging van alle acties
- Automatische rollback bij mislukte updates
- Lock-systeem om dubbele uitvoering te voorkomen
- Backup van de huidige versie voor het updaten
## Installatie
1. Download het script:
```bash
sudo wget -O /usr/local/sbin/update-gitea.sh https://raw.githubusercontent.com/[your-repo]/update-gitea.sh
```
2. Maak het script uitvoerbaar:
```bash
sudo chmod +x /usr/local/sbin/update-gitea.sh
```
3. Maak het logbestand aan:
```bash
sudo touch /var/log/gitea-update.log
```
## Configuratie
### Cron Setup
Om het script automatisch te laten draaien, voeg je een regel toe aan de root crontab:
1. Open de root crontab:
```bash
sudo crontab -e
```
2. Voeg de volgende regel toe om het script elke dag om 3:00 uur 's nachts te draaien:
```bash
0 3 * * * /usr/local/sbin/update-gitea.sh
```
### Script Configuratie
De volgende variabelen kunnen worden aangepast in het script:
```bash
APP="Gitea" # Applicatie naam
LOG_FILE="/var/log/gitea-update.log" # Locatie van het logbestand
LOCK_FILE="/tmp/gitea-update.lock" # Locatie van het lock bestand
```
## Gebruik
### Handmatig Uitvoeren
Het script kan ook handmatig worden uitgevoerd:
```bash
sudo /usr/local/sbin/update-gitea.sh
```
### Logging Bekijken
Bekijk de live log output:
```bash
tail -f /var/log/gitea-update.log
```
## Foutafhandeling
Het script bevat verschillende veiligheidsmaatregelen:
- Controleert of het script als root wordt uitgevoerd
- Gebruikt een lock file om dubbele uitvoering te voorkomen
- Maakt een backup van de huidige versie
- Rolt automatisch terug bij fouten
- Logt alle acties voor latere analyse
## Log Voorbeelden
Succesvolle update:
+155
View File
@@ -0,0 +1,155 @@
#!/bin/bash
# Kleuren voor berichten
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'
# App naam
APP="Gitea"
# Helper functies voor berichten
msg_info() {
echo -e "${YELLOW}[INFO]${NC} $1"
}
msg_ok() {
echo -e "${GREEN}[OK]${NC} $1"
}
msg_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
header_info() {
clear
msg_info "Script voor $APP Installatie/Update"
echo ""
}
# Controleer systeemvereisten
check_system_requirements() {
# Controleer of script als root draait
if [[ $EUID -ne 0 ]]; then
msg_error "Dit script moet als root worden uitgevoerd"
exit 1
}
# Controleer beschikbare schijfruimte (minimum 1GB)
AVAILABLE_SPACE=$(df -BG / | awk 'NR==2 {print $4}' | sed 's/G//')
if [[ $AVAILABLE_SPACE -lt 1 ]]; then
msg_error "Onvoldoende schijfruimte. Minimaal 1GB nodig"
exit 1
}
# Controleer RAM (minimum 1GB)
TOTAL_RAM=$(free -m | awk '/^Mem:/{print $2}')
if [[ $TOTAL_RAM -lt 1024 ]]; then
msg_error "Onvoldoende RAM. Minimaal 1GB nodig"
exit 1
}
}
# Installatie functie
install_gitea() {
header_info
check_system_requirements
# Controleer of Gitea al geïnstalleerd is
if [[ -f /usr/local/bin/gitea ]]; then
msg_error "$APP is al geïnstalleerd!"
exit 1
}
msg_info "Start $APP installatie..."
# Maak Gitea gebruiker aan
useradd -m -s /bin/bash git || true
# Download laatste versie
RELEASE=$(wget -q https://github.com/go-gitea/gitea/releases/latest -O - | grep "title>Release" | cut -d " " -f 4 | sed 's/^v//')
msg_info "Installeren van $APP versie ${RELEASE}"
wget -q https://github.com/go-gitea/gitea/releases/download/v$RELEASE/gitea-$RELEASE-linux-amd64
mv gitea* /usr/local/bin/gitea
chmod +x /usr/local/bin/gitea
# Maak benodigde mappen aan
mkdir -p /var/lib/gitea/{custom,data,log}
chown -R git:git /var/lib/gitea/
chmod -R 750 /var/lib/gitea/
mkdir /etc/gitea
chown root:git /etc/gitea
chmod 770 /etc/gitea
# Maak systemd service aan
cat > /etc/systemd/system/gitea.service <<EOF
[Unit]
Description=Gitea (Git with a cup of tea)
After=network.target
Wants=network.target
[Service]
Type=simple
User=git
Group=git
WorkingDirectory=/var/lib/gitea/
ExecStart=/usr/local/bin/gitea web -c /etc/gitea/app.ini
Restart=always
Environment=USER=git HOME=/home/git GITEA_WORK_DIR=/var/lib/gitea
[Install]
WantedBy=multi-user.target
EOF
# Start en enable service
systemctl daemon-reload
systemctl enable --now gitea
msg_ok "$APP Installatie voltooid!"
msg_info "Je kunt nu Gitea configureren via http://SERVER_IP:3000"
}
# Update functie
update_gitea() {
header_info
check_system_requirements
if [[ ! -f /usr/local/bin/gitea ]]; then
msg_error "Geen ${APP} installatie gevonden!"
exit 1
fi
RELEASE=$(wget -q https://github.com/go-gitea/gitea/releases/latest -O - | grep "title>Release" | cut -d " " -f 4 | sed 's/^v//')
msg_info "Updaten van $APP naar versie ${RELEASE}"
wget -q https://github.com/go-gitea/gitea/releases/download/v$RELEASE/gitea-$RELEASE-linux-amd64
systemctl stop gitea
rm -rf /usr/local/bin/gitea
mv gitea* /usr/local/bin/gitea
chmod +x /usr/local/bin/gitea
systemctl start gitea
msg_ok "$APP succesvol geüpdatet!"
}
# Menu voor gebruikersinteractie
show_menu() {
header_info
echo "1) Installeer $APP"
echo "2) Update $APP"
echo "3) Exit"
echo ""
read -p "Kies een optie [1-3]: " choice
case $choice in
1) install_gitea ;;
2) update_gitea ;;
3) exit 0 ;;
*) msg_error "Ongeldige keuze" && sleep 2 && show_menu ;;
esac
}
# Start het script
show_menu
+105
View File
@@ -0,0 +1,105 @@
#!/bin/bash
# Configuratie
APP="Gitea"
LOG_FILE="/var/log/gitea-update.log"
LOCK_FILE="/tmp/gitea-update.lock"
# Kleuren voor logging
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'
# Helper functies voor logging
log_info() {
echo "$(date '+%Y-%m-%d %H:%M:%S') [INFO] $1" | tee -a "$LOG_FILE"
}
log_error() {
echo "$(date '+%Y-%m-%d %H:%M:%S') [ERROR] $1" | tee -a "$LOG_FILE"
}
log_success() {
echo "$(date '+%Y-%m-%d %H:%M:%S') [SUCCESS] $1" | tee -a "$LOG_FILE"
}
# Controleer of het script als root draait
if [[ $EUID -ne 0 ]]; then
log_error "Dit script moet als root worden uitgevoerd"
exit 1
fi
# Controleer en creëer lock file
if [ -f "$LOCK_FILE" ]; then
log_error "Update proces is al actief. Lock file bestaat: $LOCK_FILE"
exit 1
fi
touch "$LOCK_FILE"
# Cleanup functie
cleanup() {
rm -f "$LOCK_FILE"
log_info "Cleanup uitgevoerd"
}
# Registreer cleanup voor als het script stopt
trap cleanup EXIT
# Start update proces
log_info "Start $APP update controle"
# Controleer of Gitea is geïnstalleerd
if [[ ! -f /usr/local/bin/gitea ]]; then
log_error "Geen ${APP} installatie gevonden!"
exit 1
fi
# Haal huidige en laatste versie op
CURRENT_VERSION=$(/usr/local/bin/gitea --version | cut -d' ' -f3)
LATEST_VERSION=$(wget -q https://github.com/go-gitea/gitea/releases/latest -O - | grep "title>Release" | cut -d " " -f 4 | sed 's/^v//')
if [[ "$CURRENT_VERSION" == "$LATEST_VERSION" ]]; then
log_info "Gitea is al up-to-date (versie $CURRENT_VERSION)"
exit 0
fi
log_info "Update beschikbaar: $CURRENT_VERSION -> $LATEST_VERSION"
# Download nieuwe versie
log_info "Downloaden van nieuwe versie..."
if ! wget -q https://github.com/go-gitea/gitea/releases/download/v$LATEST_VERSION/gitea-$LATEST_VERSION-linux-amd64; then
log_error "Download mislukt"
exit 1
fi
# Backup huidige versie
log_info "Maken van backup..."
cp /usr/local/bin/gitea /usr/local/bin/gitea.backup
# Stop service
log_info "Stoppen van Gitea service..."
systemctl stop gitea
# Installeer nieuwe versie
log_info "Installeren van nieuwe versie..."
mv gitea-$LATEST_VERSION-linux-amd64 /usr/local/bin/gitea
chmod +x /usr/local/bin/gitea
chown root:root /usr/local/bin/gitea
# Start service
log_info "Starten van Gitea service..."
systemctl start gitea
# Controleer of service draait
if systemctl is-active --quiet gitea; then
log_success "Update succesvol afgerond naar versie $LATEST_VERSION"
rm -f /usr/local/bin/gitea.backup
else
log_error "Service start mislukt, terugdraaien naar vorige versie..."
mv /usr/local/bin/gitea.backup /usr/local/bin/gitea
systemctl start gitea
exit 1
fi
exit 0