118 lines
3.5 KiB
Bash
118 lines
3.5 KiB
Bash
#!/bin/bash
|
|
#
|
|
# Gitea Service
|
|
|
|
PATH=$HOME/.docker/cli-plugins:/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
|
|
|
config_gitea() {
|
|
echo -ne "\n* Configuring /federated/apps/gitea container.."
|
|
spin &
|
|
SPINPID=$!
|
|
|
|
if [ ! -d "/federated/apps/gitea" ]; then
|
|
mkdir -p /federated/apps/gitea/data/data
|
|
fi
|
|
|
|
DOMAIN_ARRAY=(${DOMAIN//./ })
|
|
DOMAIN_FIRST=${DOMAIN_ARRAY[0]}
|
|
DOMAIN_LAST=${DOMAIN_ARRAY[1]}
|
|
|
|
cat > /federated/apps/gitea/docker-compose.yml <<EOF
|
|
version: "3.7"
|
|
|
|
services:
|
|
gitea:
|
|
image: gitea/gitea:\${IMAGE_VERSION}
|
|
container_name: gitea
|
|
hostname: gitea.$DOMAIN
|
|
domainname: $DOMAIN
|
|
restart: always
|
|
networks:
|
|
federated:
|
|
ipv4_address: 172.99.0.30
|
|
extra_hosts:
|
|
- "www.$DOMAIN:164.92.112.207"
|
|
ports:
|
|
- 22:22
|
|
env_file:
|
|
- ./.env
|
|
volumes:
|
|
- ./data/data:/data
|
|
- /etc/timezone:/etc/timezone:ro
|
|
- /etc/localtime:/etc/localtime:ro
|
|
|
|
networks:
|
|
federated:
|
|
external: true
|
|
EOF
|
|
|
|
cat > /federated/apps/gitea/.env <<EOF
|
|
IMAGE_VERSION="latest"
|
|
VIRTUAL_PROTO=http
|
|
VIRTUAL_PORT=3000
|
|
VIRTUAL_HOST=gitea.$DOMAIN
|
|
USER_UID=1000
|
|
USER_GID=1000
|
|
GITEA__database__DB_TYPE=postgres
|
|
GITEA__database__HOST=postgresql.$DOMAIN:5432
|
|
GITEA__database__NAME=gitea
|
|
GITEA__database__USER=gitea
|
|
GITEA__database__PASSWD=$GITEA_SECRET
|
|
GITEA__security__INSTALL_LOCK=true
|
|
GITEA__server__ROOT_URL=https://gitea.$DOMAIN
|
|
EOF
|
|
chmod 600 /federated/apps/gitea/.env
|
|
|
|
WEBHOOK_SECRET=$(create_password);
|
|
|
|
cat > /federated/apps/gitea/data/creategitea.sh <<EOF
|
|
#!/bin/bash
|
|
|
|
# Get the Gitea API token
|
|
GITEA_TOKEN_2=\`curl -H "Content-Type: application/json" -d '{"name":"gitea2"}' -u gitea:$ADMINPASS http://gitea.$DOMAIN:3000/api/v1/users/gitea/tokens 2>/dev/null | awk -F: '{ print \$4 }' | awk -F\" '{ print \$2 }'\`
|
|
|
|
# Create the repository hugowebsite
|
|
curl -k -X POST http://gitea.$DOMAIN:3000/api/v1/user/repos -H "content-type: application/json" -H "Authorization: token \$GITEA_TOKEN_2" --data '{"name":"hugowebsite","auto_init":true,"default_branch":"master"}'
|
|
|
|
# Create the webhook inside the hugowebsite repository
|
|
curl -X 'POST' \
|
|
'http://gitea.$DOMAIN:3000/api/v1/repos/gitea/hugowebsite/hooks' \
|
|
-H 'accept: application/json' \
|
|
-H 'Content-Type: application/json' \
|
|
-H "Authorization: token \$GITEA_TOKEN_2" \
|
|
-d '{
|
|
"active": true,
|
|
"config": {
|
|
"content_type": "json",
|
|
"url": "https://www.$DOMAIN/webhook",
|
|
"secret": "$WEBHOOK_SECRET"
|
|
},
|
|
"events": [
|
|
"push"
|
|
],
|
|
"type": "gitea"
|
|
}'
|
|
EOF
|
|
chmod +x /federated/apps/gitea/data/creategitea.sh
|
|
|
|
kill -9 $SPINPID &> /dev/null
|
|
echo -ne "done."
|
|
}
|
|
start_gitea() {
|
|
# Start service with command to make sure it's up before proceeding
|
|
start_service "gitea" "nc -z 172.99.0.30 3000 &> /dev/null"
|
|
|
|
mv /federated/apps/gitea/data/creategitea.sh /federated/apps/gitea/data/data/creategitea.sh
|
|
docker exec --user 1000 gitea gitea admin user create --admin --username gitea --password $ADMINPASS --email admin@$DOMAIN
|
|
[ $? -ne 0 ] && fail "Couldn't run gitea user create inside /federated/apps/gitea container"
|
|
|
|
docker exec -it gitea /data/creategitea.sh
|
|
[ $? -ne 0 ] && fail "Couldn't run creategitea.sh inside /federated/apps/gitea container"
|
|
|
|
GITEA_TOKEN_1=`docker exec -it gitea curl -H "Content-Type: application/json" -d '{"name":"gitea1"}' -u gitea:$ADMINPASS http://gitea.$DOMAIN:3000/api/v1/users/gitea/tokens 2>/dev/null | awk -F: '{ print $4 }' | awk -F\" '{ print $2 }'`
|
|
[ $? -ne 0 ] && fail "Couldn't run gitea curl to get token inside /federated/apps/gitea container"
|
|
|
|
kill -9 $SPINPID &> /dev/null
|
|
echo -ne "done."
|
|
}
|