PortValhalla/scripts/Common/Software/docker/services/forgejo/main.fish

110 lines
3.8 KiB
Fish
Raw Normal View History

2024-11-07 22:58:06 +00:00
#!/bin/env fish
begin
set -l dir (status dirname)
2024-12-11 00:30:36 +00:00
set -l user git
2024-11-29 00:13:58 +00:00
set -l domain git
2024-11-07 22:58:06 +00:00
set -l server "$domain" ""
2024-12-11 00:30:36 +00:00
set -l service "forgejo"
2024-11-07 22:58:06 +00:00
source "$dir/../service.fish"
function getSSHPortKey -V service
echo "$(getServiceKey "$service").ports[1]"
end
function installSW -V dir -V domain -V server -V service
set -l root (getServiceRoot $argv)
2024-11-27 01:54:44 +00:00
set -l overrides (getServiceOverrides $argv)
set -l source "$dir/$(basename "$overrides")"
2024-11-07 22:58:06 +00:00
set -l pw (nix-shell -p keepassxc --run "keepassxc-cli generate --length 32")
set -l port (getRandomPort)
initializeServiceInstallation $argv
2024-11-13 15:22:57 +00:00
sudo cp "$dir/docker-compose.base.yml" "$root"
2024-11-07 22:58:06 +00:00
2024-11-19 23:30:08 +00:00
set port (yq (getSSHPortKey) "$source" | mutatePort "$port")
2024-11-07 22:58:06 +00:00
DOMAIN=(getServiceDomain $server) PW=$pw DB=Git USER=forgejo PW=$pw begin
2024-11-29 00:13:58 +00:00
set -l gitEnv "$(getServiceKey "$service").environment"
set -l actEnv "$(getServiceKey "runner").environment"
set -l dbEnv "$(getServiceKey "db").environment"
PORT=$port yq "$(getSSHPortKey) = env(PORT)" "$source" |
yq "$gitEnv.FORGEJO__server__DOMAIN = env(DOMAIN)" |
yq "$gitEnv.FORGEJO__database__NAME = env(DB)" |
yq "$gitEnv.FORGEJO__database__USER = env(USER)" |
yq "$gitEnv.FORGEJO__database__PASSWD = env(PW)" |
yq "$dbEnv.MARIADB_DATABASE = env(DB)" |
yq "$dbEnv.MARIADB_USER = env(USER)" |
yq "$dbEnv.MARIADB_PASSWORD = env(PW)" |
URL="https://$DOMAIN/" yq "$actEnv.GITEA_INSTANCE_URL = env(URL)" |
sudo tee "$overrides" >/dev/null
end
2024-11-07 22:58:06 +00:00
installDockerService $argv
end
function configureSW -V dir -V user -V service
set -l uid
set -l gid
set -l port
set -l file (mktemp)
set -l root (getServiceRoot $argv)
2024-12-11 02:30:36 +00:00
set -l home /srv/git
2024-12-11 03:17:48 +00:00
set -l sshDir "$home/.ssh"
set -l keyFile "$sshDir/id_rsa"
2024-11-07 22:58:06 +00:00
set -l dir "$root/data"
2024-11-29 00:13:58 +00:00
set -l bin /usr/local/bin/forgejo
2024-11-13 15:22:57 +00:00
set -l config "$root/docker-compose.base.yml"
2024-11-27 01:54:44 +00:00
set -l overrides (getServiceOverrides $argv)
2024-11-07 22:58:06 +00:00
set -l envKey "$(getServiceKey "$service").environment"
configureDockerService $argv
cp "$config" "$file"
and sudo useradd \
--system \
--shell /bin/bash \
--comment 'Git Version Control' \
$user
set uid (id -u $user)
set gid (id -g $user)
2024-12-11 02:30:36 +00:00
and sudo usermod -d "$home" "$user"
2024-11-07 22:58:06 +00:00
2024-11-29 00:13:58 +00:00
and yq "$envKey.USER_UID = $uid" "$file" |
yq "$envKey.USER_GID = $gid" |
2024-11-07 22:58:06 +00:00
sudo tee "$config" >/dev/null
2024-12-11 03:17:48 +00:00
rm "$file"
2024-12-11 00:17:54 +00:00
and sudo mkdir -p "$dir"
2024-12-11 02:30:36 +00:00
and sudo mkdir -p "$home"
2024-12-04 15:44:01 +00:00
and sudo chown -R $uid:$gid "$dir"
2024-12-11 02:30:36 +00:00
and sudo chown -R $uid:$gid "$home"
2024-12-11 03:17:48 +00:00
and sudo -u "#$uid" mkdir -p "$sshDir"
and sudo -u "#$uid" ssh-keygen -t rsa -b 4096 -C "Forgejo Host Key" -f "$keyFile" -N ""
and sudo -u "#$uid" cat "$keyFile.pub" | sudo -u "#$uid" tee -a "$sshDir/authorized_keys"
and sudo chmod 600 "$sshDir/authorized_keys"
2024-11-07 22:58:06 +00:00
2024-11-27 01:54:44 +00:00
set port (yq (getSSHPortKey) "$overrides" | extractPort)
2024-11-07 22:58:06 +00:00
begin
printf "%s\n" \
"#!/bin/sh" \
2024-12-11 03:18:13 +00:00
"ssh -p $port -o StrictHostKeyChecking=no git@127.0.0.1 \"SSH_ORIGINAL_COMMAND=\\\"\$SSH_ORIGINAL_COMMAND\\\" \$0 \$@\""
2024-11-07 22:58:06 +00:00
end | sudo tee "$bin" >/dev/null
2024-12-06 12:51:08 +00:00
sudo chmod +x "$bin"
2024-12-11 03:18:00 +00:00
sudo ln -s "$bin" "$(dirname "$bin")/gitea"
2024-11-07 22:58:06 +00:00
end
function getServiceServers -V server
printf "%s\0" $server
end
function getServiceLocations
argparse -i "name=" -- $argv
printf "%s\0" "$_flag_name" /
end
runInstaller --force $argv
end