diff --git a/gitea b/gitea index 2d21411..51f0a3a 100755 --- a/gitea +++ b/gitea @@ -12,6 +12,18 @@ NETWORK_NAME="incusbr0" DB_USER="gitea" DB_PASS="gitea_password" # Default password, can be overridden with -p option +# Add this near the top of your script +handle_error() { + echo "ERROR: $1" >&2 + exit 1 +} + +# Near the top of your script after setting default variables +if [ -f .env ]; then + echo "Loading configuration from .env file..." + source .env +fi + # Function to create the Incus profile create_profile() { local cpu=$1 @@ -19,18 +31,21 @@ create_profile() { echo "Creating Incus profile with root disk size of $ROOT_DISK_SIZE..." incus profile create $PROFILE_NAME || true - if [ ! -z "$ram" ]; then - echo "Setting RAM limit to ${ram}GB" - incus profile set $PROFILE_NAME limits.memory=${ram}GB - else - echo "No RAM limit specified. Using default." + echo "Recommended RAM allocation for Gitea: at least 2GB" + echo "Recommended CPU allocation: at least 2 cores" + + # Then set reasonable defaults if not specified + if [ -z "$ram" ]; then + ram=2 + echo "Setting default RAM to ${ram}GB" fi - if [ ! -z "$cpu" ]; then - echo "Setting CPU limit to $cpu" - incus profile set $PROFILE_NAME limits.cpu=$cpu - else - echo "No CPU limit specified. Using default." + + if [ -z "$cpu" ]; then + cpu=2 + echo "Setting default CPU to $cpu cores" fi + incus profile set $PROFILE_NAME limits.memory=${ram}GB + incus profile set $PROFILE_NAME limits.cpu=$cpu incus profile device add $PROFILE_NAME root disk path=/ pool=default size=$ROOT_DISK_SIZE echo "Ensuring network exists..." @@ -67,6 +82,8 @@ install_gitea() { incus config device add $CONTAINER_NAME gitea-ssh proxy listen=tcp:0.0.0.0:$SSH_PORT connect=tcp:127.0.0.1:22 incus config device add $CONTAINER_NAME gitea-http proxy listen=tcp:0.0.0.0:$HTTP_PORT connect=tcp:127.0.0.1:80 incus config device add $CONTAINER_NAME gitea-https proxy listen=tcp:0.0.0.0:$HTTPS_PORT connect=tcp:127.0.0.1:443 + incus config device add $CONTAINER_NAME gitea-data disk source=/path/to/host/storage path=/var/lib/gitea + incus config device add $CONTAINER_NAME postgres-data disk source=/path/to/host/postgres path=/var/lib/postgresql/data echo "Waiting for network to be ready..." sleep 10 @@ -150,6 +167,19 @@ EOL" docker-compose up -d " + echo "Waiting for Gitea to start..." + for i in {1..30}; do + if incus exec $CONTAINER_NAME -- curl -s http://localhost:3000 > /dev/null; then + echo "Gitea is running!" + break + fi + echo -n "." + sleep 2 + if [ $i -eq 30 ]; then + echo "Gitea didn't start properly. Check logs with: incus exec $CONTAINER_NAME -- docker-compose logs" + fi + done + echo "Configuring SSL with Certbot..." incus exec $CONTAINER_NAME -- certbot --nginx -d $DOMAIN_NAME --non-interactive --agree-tos --email admin@$DOMAIN_NAME @@ -181,6 +211,30 @@ EOL" echo "Important: Make sure your domain ($DOMAIN_NAME) is pointed to this server's IP address: $CONTAINER_IP" } +# Add a backup function +backup_gitea() { + echo "Creating backup of Gitea data..." + BACKUP_DATE=$(date +%Y%m%d-%H%M%S) + BACKUP_DIR="./backups/$BACKUP_DATE" + + mkdir -p $BACKUP_DIR + + # Export container data + incus exec $CONTAINER_NAME -- bash -c "cd /root && docker-compose stop" + incus file pull $CONTAINER_NAME/var/lib/gitea $BACKUP_DIR/gitea-data + incus file pull $CONTAINER_NAME/var/lib/postgresql/data $BACKUP_DIR/postgres-data + incus exec $CONTAINER_NAME -- bash -c "cd /root && docker-compose start" + + echo "Backup completed: $BACKUP_DIR" +} + +# Add an update function +update_gitea() { + echo "Updating Gitea..." + incus exec $CONTAINER_NAME -- bash -c "cd /root && docker-compose pull && docker-compose up -d" + echo "Gitea has been updated to the latest version." +} + # Function to display usage usage() { echo "Usage" @@ -238,8 +292,19 @@ case "$1" in ;; esac done + if [ -z "$DB_PASS" ]; then + DB_PASS=$(openssl rand -base64 16) + echo "Generated random database password: $DB_PASS" + echo "Please save this password in a secure location." + fi install_gitea ;; + backup) + backup_gitea + ;; + update) + update_gitea + ;; *) usage exit 1