Files
Scripts/gitea-ct-setup.sh
2026-04-17 23:29:35 +03:00

206 lines
7.6 KiB
Bash

#!/bin/bash
# ═══════════════════════════════════════════════════════════════════
# gitea-ct-setup.sh
# Run this ON THE PROXMOX HOST (vanga) as root
# Creates CT116, installs Gitea, bind-mounts /srv/depo/scripts
# After: point git.edemilov.ru → 10.0.99.116:3000 in NPM
# ═══════════════════════════════════════════════════════════════════
set -e
# ── Config ──────────────────────────────────────────────────────────
CTID=116
CTNAME="gitea"
CT_IP="10.0.99.116/24"
CT_GW="10.0.99.1"
CT_RAM=512
CT_DISK=8 # GB on local-lvm
CT_CORES=2
CT_PASSWORD="changeme123" # ← change this before running!
GITEA_VERSION="1.22.6"
STORAGE="local-lvm"
TEMPLATE_STORAGE="local"
# ── Colors ──────────────────────────────────────────────────────────
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'
CYAN='\033[0;36m'; BOLD='\033[1m'; NC='\033[0m'
info() { echo -e "${CYAN}[•]${NC} $1"; }
success() { echo -e "${GREEN}[✓]${NC} $1"; }
warn() { echo -e "${YELLOW}[!]${NC} $1"; }
die() { echo -e "${RED}[✗]${NC} $1"; exit 1; }
echo -e "${BOLD}"
echo " ╔══════════════════════════════════════╗"
echo " ║ Gitea CT116 Setup — vanga ║"
echo " ╚══════════════════════════════════════╝"
echo -e "${NC}"
# ── Preflight ───────────────────────────────────────────────────────
[[ $EUID -ne 0 ]] && die "Run as root on Proxmox host"
pct status $CTID &>/dev/null && die "CT$CTID already exists. Abort."
# ── Download Debian template if needed ──────────────────────────────
info "Checking for Debian 12 template..."
TEMPLATE=$(pveam list $TEMPLATE_STORAGE 2>/dev/null | grep "debian-12" | tail -1 | awk '{print $1}')
if [[ -z "$TEMPLATE" ]]; then
info "Downloading Debian 12 template..."
pveam update
TEMPLATE_NAME=$(pveam available --section system | grep "debian-12" | tail -1 | awk '{print $2}')
pveam download $TEMPLATE_STORAGE $TEMPLATE_NAME
TEMPLATE="$TEMPLATE_STORAGE:vztmpl/$TEMPLATE_NAME"
fi
success "Template ready: $TEMPLATE"
# ── Create CT ───────────────────────────────────────────────────────
info "Creating CT$CTID ($CTNAME)..."
pct create $CTID $TEMPLATE \
--hostname $CTNAME \
--password $CT_PASSWORD \
--cores $CT_CORES \
--memory $CT_RAM \
--swap 512 \
--rootfs $STORAGE:$CT_DISK \
--net0 name=eth0,bridge=vmbr0,ip=$CT_IP,gw=$CT_GW \
--nameserver 10.0.99.101 \
--searchdomain local \
--unprivileged 1 \
--features nesting=1 \
--onboot 1
success "CT$CTID created"
# ── Bind mount /srv/depo/scripts into CT ────────────────────────────
info "Adding bind mount /srv/depo/scripts → /opt/gitea-repos/scripts..."
# Create the ZFS dataset for gitea data if not exists
zfs list zdata4tbnvme1/apps/gitea &>/dev/null || \
zfs create -o mountpoint=/srv/apps/gitea zdata4tbnvme1/apps/gitea
mkdir -p /srv/apps/gitea/data
mkdir -p /srv/depo/scripts
# Bind mounts (need privileged=0 workaround — use mp)
pct set $CTID \
--mp0 /srv/depo/scripts,mp=/opt/scripts,ro=0 \
--mp1 /srv/apps/gitea/data,mp=/opt/gitea-data,ro=0
success "Bind mounts configured"
# ── Start CT ────────────────────────────────────────────────────────
info "Starting CT$CTID..."
pct start $CTID
sleep 5
success "CT$CTID running"
# ── Install Gitea inside CT ─────────────────────────────────────────
info "Installing Gitea $GITEA_VERSION inside CT..."
pct exec $CTID -- bash -c "
set -e
# Update & deps
apt-get update -qq
apt-get install -y -qq git curl wget sqlite3 ca-certificates
# Create git user
adduser --system --shell /bin/bash --gecos 'Gitea' --group \
--disabled-password --home /home/git git
# Download Gitea binary
cd /tmp
wget -q https://dl.gitea.com/gitea/${GITEA_VERSION}/gitea-${GITEA_VERSION}-linux-amd64 \
-O /usr/local/bin/gitea
chmod +x /usr/local/bin/gitea
# Gitea directory structure
mkdir -p /opt/gitea-data/{repositories,custom,log,data}
mkdir -p /etc/gitea
chown -R git:git /opt/gitea-data
chown root:git /etc/gitea
chmod 770 /etc/gitea
# app.ini config
cat > /etc/gitea/app.ini << 'APPINI'
APP_NAME = edemilov scripts & notes
RUN_USER = git
RUN_MODE = prod
[server]
HTTP_PORT = 3000
ROOT_URL = https://git.edemilov.ru/
DOMAIN = git.edemilov.ru
DISABLE_SSH = false
SSH_PORT = 2222
[database]
DB_TYPE = sqlite3
PATH = /opt/gitea-data/data/gitea.db
[repository]
ROOT = /opt/gitea-data/repositories
[log]
ROOT_PATH = /opt/gitea-data/log
MODE = file
LEVEL = warn
[security]
INSTALL_LOCK = false
SECRET_KEY = $(openssl rand -hex 32)
INTERNAL_TOKEN = $(openssl rand -hex 32)
[service]
DISABLE_REGISTRATION = true
REQUIRE_SIGNIN_VIEW = false
[ui]
DEFAULT_THEME = arc-green
APPINI
chown git:git /etc/gitea/app.ini
# systemd service
cat > /etc/systemd/system/gitea.service << 'SERVICE'
[Unit]
Description=Gitea
After=network.target
[Service]
User=git
Group=git
WorkingDirectory=/opt/gitea-data
ExecStart=/usr/local/bin/gitea web --config /etc/gitea/app.ini
Restart=always
RestartSec=5
Environment=USER=git HOME=/home/git GITEA_WORK_DIR=/opt/gitea-data
[Install]
WantedBy=multi-user.target
SERVICE
systemctl daemon-reload
systemctl enable gitea
systemctl start gitea
echo 'Gitea service started'
"
success "Gitea installed and running inside CT$CTID"
# ── Final summary ───────────────────────────────────────────────────
echo ""
echo -e "${BOLD}${GREEN}═══════════════════════════════════════════════════${NC}"
echo -e "${BOLD} ✓ Gitea CT116 ready!${NC}"
echo -e "${GREEN}═══════════════════════════════════════════════════${NC}"
echo ""
echo -e " ${CYAN}Internal URL:${NC} http://10.0.99.116:3000"
echo -e " ${CYAN}Public URL:${NC} https://git.edemilov.ru (after NPM setup)"
echo ""
echo -e "${YELLOW} NPM Steps:${NC}"
echo " 1. New Proxy Host"
echo " 2. Domain: git.edemilov.ru"
echo " 3. Forward: 10.0.99.116 port 3000"
echo " 4. Enable SSL (Let's Encrypt)"
echo ""
echo -e "${YELLOW} First run — finish setup at:${NC}"
echo " http://10.0.99.116:3000"
echo " (creates admin account, everything is pre-configured)"
echo ""
echo -e "${YELLOW} Script curl usage (after pushing scripts to Gitea):${NC}"
echo " curl -s https://git.edemilov.ru/edemilov/scripts/raw/branch/main/apps.sh | bash"
echo ""