gitea/deploy-gitea

114 lines
5.9 KiB
Bash
Executable File

#!/bin/bash
# Gitea Container Deployment - Developed by acidvegas (https://git.acid.vegas)
set -xev
# TODO: Automate this process on a timer to do daily backups & remote backups
backup_gitea() {
GITEA_DB="giteadb"
GITEA_DB_USER="gitea"
NOW=$(date +%Y%m%d)
incus exec gitea-container -- systemctl stop gitea
incus exec gitea-container -- sudo -u git sh -c 'gitea dump -c /etc/gitea/app.ini --file - > /home/git/gitea-${NOW}.zip'
incus exec gitea-container -- sudo -u git sh -c 'pg_dump -U $GITEA_DB_USER $GITEA_DB > /home/git/gitea-${NOW}.sql'
incus file pull gitea-container/home/git/gitea-${NOW}.zip gitea-${NOW}.zip
incus file pull gitea-container/home/git/gitea-${NOW}.sql gitea-${NOW}.sql
incus exec gitea-container -- rm /home/git/gitea-${NOW}.zip /home/git/gitea-${NOW}.sql
incus exec gitea-container -- systemctl start gitea
}
setup_gitea() {
[ ! -f gitea.zip ] && echo "Missing gitea.zip" && exit 1
[ ! -f gitea.sql ] && echo "Missing gitea.sql" && exit 1
GITEA_DB="giteadb"
GITEA_DB_USER="gitea"
PORT_GITEA_SSH=30022
CONTAINER_IP=$(incus list | grep gitea-container | awk '{print $6}')
VERSION=$(curl -s https://api.github.com/repos/go-gitea/gitea/releases/latest | jq -r .tag_name | cut -c2-)
incus config set gitea-container boot.autostart true
incus config device add gitea-container gitea-ssh-port proxy listen=tcp:0.0.0.0:$PORT_GITEA_SSH connect=tcp:$CONTAINER_IP:${PORT_GITEA_SSH}
incus exec prosody-container -- userdel -r agent
incus exec gitea-container -- apt-get install git postgresql postgresql-client unzip wget -y
incus exec gitea-container -- adduser --system --shell /bin/bash --group --disabled-password --home /home/git git
incus exec gitea-container -- wget -O /usr/local/bin/gitea https://github.com/go-gitea/gitea/releases/download/v${VERSION}/gitea-${VERSION}-linux-amd64
incus exec gitea-container -- chmod +x /usr/local/bin/gitea
incus exec gitea-container -- mkdir -p /etc/gitea /var/lib/gitea/custom/assets /var/lib/gitea/data /var/lib/gitea/log
incus exec gitea-container -- chown root:git /etc/gitea
incus exec gitea-container -- chmod 750 /etc/gitea
incus exec gitea-container -- chmod 640 /etc/gitea/app.ini
incus exec gitea-container -- chown -R git:git /var/lib/gitea
incus exec gitea-container -- chmod -R 750 /var/lib/gitea
incus exec gitea-container -- wget -O /usr/share/bash-completion/completions/gitea https://raw.githubusercontent.com/go-gitea/gitea/main/contrib/autocompletion/bash_autocomplete
incus exec gitea-container -- wget -O /etc/systemd/system/gitea.service https://raw.githubusercontent.com/go-gitea/gitea/release/v1.22/contrib/systemd/gitea.service
incus file push gitea.sql gitea-container/root/gitea.sql
incus exec gitea-container -- systemctl enable postgresql
incus exec gitea-container -- systemctl start postgresql
PASSWORD=$(grep PASSWD /etc/gitea/app.ini | awk '{print $3}')
su -c "psql -c \"CREATE ROLE $GITEA_DB_USER WITH LOGIN PASSWORD '${PASSWORD}';\" -c \"CREATE DATABASE $GITEA_DB WITH OWNER $GITEA_DB_USER TEMPLATE template0 ENCODING UTF8 LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';\"" - postgres
printf "\nlocal $GITEA_DB $GITEA_DB_USER scram-sha-256" >> /etc/postgresql/*/main/pg_hba.conf # Verify this will write to all lol...
psql -h localhost -U $GIT_DB_USER $GIT_DB < /root/gitea.sql
incus exec gitea-container -- rm /root/gitea.sql
incus exec gitea-container -- systemctl restart postgresql
incus file push gitea.zip gitea-container/root/gitea.zip
incus exec gitea-container -- mkdir -p /root/restore
incus exec gitea-container -- unzip /root/gitea.zip -d /root/restore
incus exec gitea-container -- rm /root/gitea.zip
mv /root/restore/app.ini /etc/gitea/app.ini
mv /root/restore/custom/* /var/lib/gitea/custom/
mv /root/restore/data/* /var/lib/gitea/data/
mv /root/restore/repos/* /var/lib/gitea/data/gitea-repositories/
chown -R git:git /etc/gitea/app.ini /var/lib/gitea
incus exec gitea-container -- systemctl enable gitea
incus exec gitea-container -- systemctl start gitea
}
# TODO: Unfinished
setup_runner() {
GITEA_URL="https://git.supernets.org"
CONTAINER_IP=$(incus list | grep gitea-runner-container | awk '{print $6}')
VERSION=$(curl -s https://gitea.com/api/v1/repos/gitea/act_runner/releases/latest | grep -o '"tag_name":"[^"]*' | cut -d'"' -f4)
incus config set gitea-container boot.autostart true
incus exec gitea-container -- wget -O /usr/local/bin/gitea-runner https://gitea.com/gitea/act_runner/releases/download/v${VERSION}/act_runner-${VERSION}-linux-amd64
incus exec gitea-container -- chmod +x /usr/local/bin/gitea-runner
incus exec gitea-container -- mkdir /etc/gitea-runner
incus exec gitea-container -- /usr/local/bin/gitea-runner generate-config > /etc/gitea-runner/config.yaml
incus exec gitea-container -- /usr/local/bin/gitea-runner register --no-interactive --instance $GITEA_URL --token <registration_token> --name <runner_name> --labels <runner_labels>
{
echo "[Unit]"
echo "Description=Gitea Actions runner"
echo "Documentation=https://gitea.com/gitea/act_runner"
echo "After=docker.service"
echo "[Service]"
echo "ExecStart=/usr/local/bin/act_runner daemon --config /etc/act_runner/config.yaml"
echo "ExecReload=/bin/kill -s HUP \$MAINPID"
echo "WorkingDirectory=/var/lib/act_runner"
echo "TimeoutSec=0"
echo "RestartSec=10"
echo "Restart=always"
echo "User=act_runner"
echo "[Install]"
echo "WantedBy=multi-user.target"
} > gitea-runner.service.tmp
incus file push gitea-runner.service.tmp gitea-container/etc/systemd/system/gitea-runner.service
rm gitea-runner.service.tmp
incus exec gitea-container -- systemctl enable gitea-runner
incus exec gitea-container -- systemctl start gitea-runner
}