Skip to content

Commit 45e612d

Browse files
committed
Add zerotier
1 parent ec0bba0 commit 45e612d

File tree

7 files changed

+109
-12
lines changed

7 files changed

+109
-12
lines changed

Dockerfile

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
1-
FROM nginx:mainline-alpine-perl
1+
ARG BASE_IMAGE=nginx:mainline-alpine
2+
ARG MAKE_THREADS=8
3+
FROM ${BASE_IMAGE} AS zerotier_builder
4+
5+
6+
RUN apk add --update alpine-sdk linux-headers openssl-dev curl gcc libgcc musl-dev openssl openssl-dev
7+
8+
RUN curl -sSL sh.rustup.rs >/usr/local/bin/rustup-dl && chmod +x /usr/local/bin/rustup-dl && /usr/local/bin/rustup-dl -y --default-toolchain stable
9+
10+
RUN git clone --quiet https://github.com/zerotier/ZeroTierOne.git /src \
11+
&& cd /src \
12+
&& make -j ${MAKE_THREADS} -f make-linux.mk
13+
14+
FROM ${BASE_IMAGE}-perl
15+
16+
COPY --from=zerotier_builder /src/zerotier-one /usr/sbin/
17+
18+
RUN apk add --no-cache --purge --clean-protected libc6-compat libstdc++ \
19+
&& mkdir -p /var/lib/zerotier-one \
20+
&& ln -s /usr/sbin/zerotier-one /usr/sbin/zerotier-idtool \
21+
&& ln -s /usr/sbin/zerotier-one /usr/sbin/zerotier-cli
22+
223
RUN apk update \
324
&& apk upgrade \
4-
&& apk add git git-gitweb git-daemon openssh fcgiwrap perl-cgi spawn-fcgi rsync highlight
25+
&& apk add git git-gitweb git-daemon openssh fcgiwrap perl-cgi spawn-fcgi rsync highlight \
26+
&& rm -rf /var/cache/apk/*
27+
528
RUN sed -i 's/#UseDNS no/UseDNS no/' /etc/ssh/sshd_config \
629
&& sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config \
730
&& sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/' /etc/ssh/sshd_config

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ The Backup is done via git bundle files. If a bundle exists at the backup locati
1111
we copy it to the server and clone the repo. If not, we create a new repo.
1212
After 30 seconds (the next check loop) we create a backup of the new repo.
1313

14-
To preserve repos during container restarts create a volume mapping to the `/git` folder
14+
To preserve repos and/or the zerotier config during container restarts create a volume mapping to the `/git/repos` or `/var/lib/zerotier-one` folder.
15+
16+
Zerotier additionally needs the ability to create tun/tap devices which can be enabled with `--cap-add=NET_ADMIN --cap-add=SYS_ADMIN --device=/dev/net/tun`
1517
```
1618
cp env.example env
1719
vi env
1820
19-
docker run --rm -it -v $(pwd)/env:/.env -p 30080:80 -p 30022:22 kaitsh/gitweb
21+
docker run --rm -it -v $(pwd)/env:/.env:ro -p 30080:80 -p 30022:22 kaitsh/gitweb
2022
```
2123

2224
Clone a repo with ssh or http
@@ -27,6 +29,8 @@ git clone ssh://git@localhost:30022/git/repo1.git
2729
git clone http://localhost:30080/repo1.git
2830
```
2931

32+
_NOTE:_ http is currently disabled. Please only use ssh.
33+
3034
Run the init script
3135
```
3236
cd repo1

docker-compose.yaml

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
version: '3.0'
22
services:
33
web:
4-
image: local/gitweb
4+
privileged: true
5+
image: kaitsh/gitweb
56
ports:
67
- "30080:80"
78
- "30022:22"
89
volumes:
910
- ./env:/.env
1011
- repos:/git
12+
- zerotier:/var/lib/zerotier-one
1113
extra_hosts:
1214
- "host.docker.internal:host-gateway"
1315

1416
volumes:
1517
repos: {}
18+
zerotier: {}

env.example

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
PRIVATE_KEY='openssh private key for backup server
22
(ensure the backup server has the pub key in the authorized_keys file)'
33

4+
ZEROTIER_ID=""
5+
46
AUTHORIZED_KEYS='authorized keys for git ssh connection'
57

68
# Needs escaped dollar signs... TODO: find solution
@@ -9,6 +11,5 @@ BASIC_AUTH='htpassd config for nginx'
911
# Backup location/origin of the repos
1012
# New repos must also be specified here. The service will backup them every 30 seconds
1113
REPOS=""
12-
REPOS="${REPOS} [email protected]:repo1.git"
13-
REPOS="${REPOS} [email protected]:repo2.git"
14-
14+
#REPOS="${REPOS} [email protected]:repo1.git"
15+
#REPOS="${REPOS} [email protected]:repo2.git"

init.template

+6-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ read -p "Git email: [${git_email}] " input
7878
git_email=${input:-$git_email}
7979
git config user.email "${git_email}"
8080

81+
git_signing=`git config commit.gpgsign`
82+
read -p "Git signing: [${git_signing}] " input
83+
git_signing=${input:-$git_signing}
84+
git config commit.gpgsign "${git_signing}"
85+
8186
###########
8287
# Cleanup #
8388
###########
@@ -87,5 +92,5 @@ if [ "$answer" != "${answer#[Yy]}" ] ;then
8792
rm -f $0
8893
git rm --cached $0
8994
git commit --allow-empty -m "Initial commit" --no-verify
90-
git push origin +master
95+
git push origin +main
9196
fi

service.sh

+61-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ USER=git
33
ENV=/.env
44
NGINX_CONF=/etc/nginx/conf.d/default.conf
55
GITWEB_CONF=/etc/gitweb.conf
6-
SERVER_DIR=/git
6+
SSHD_CONF=/etc/ssh/sshd_config
7+
SERVER_ROOT=/jail
8+
SERVER_DIR=${SERVER_ROOT}/git
79
SYNC_SCRIPT=/sync.sh
810

911
# TODO(dgl): to load config on each loop
@@ -32,9 +34,29 @@ EOF
3234
chmod 600 /home/$USER/.ssh/id_rsa
3335
chmod 600 /home/$USER/.ssh/authorized_keys
3436
chmod 600 /home/$USER/.htpasswd
35-
3637
EOC
3738

39+
# ---- Setup chroot for git ----
40+
mkdir -p $SERVER_ROOT/dev
41+
mkdir -p $SERVER_ROOT/lib
42+
mkdir -p $SERVER_ROOT/bin
43+
mkdir -p $SERVER_ROOT/proc
44+
mkdir -p $SERVER_ROOT/usr/bin
45+
mkdir -p $SERVER_ROOT/usr/lib
46+
mknod -m 666 $SERVER_ROOT/dev/null c 1 3
47+
mknod -m 444 $SERVER_ROOT/dev/random c 1 8
48+
mknod -m 444 $SERVER_ROOT/dev/urandom c 1 9
49+
cp /bin/busybox $SERVER_ROOT/bin
50+
cp /usr/bin/git $SERVER_ROOT/usr/bin
51+
cp /usr/bin/git-receive-pack $SERVER_ROOT/usr/bin
52+
cp /usr/bin/git-upload-pack $SERVER_ROOT/usr/bin
53+
cp /usr/bin/git-receive-pack $SERVER_ROOT/usr/bin
54+
cp /lib/ld-musl* $SERVER_ROOT/lib
55+
cp /usr/lib/libpcre2* $SERVER_ROOT/usr/lib
56+
cp /lib/libz* $SERVER_ROOT/lib
57+
ln $SERVER_ROOT/bin/busybox $SERVER_ROOT/bin/ash
58+
mount -t proc proc $SERVER_ROOT/proc
59+
3860
# TODO(dgl): fix remote: warning: unable to access '/root/.config/git/attributes': Permission denied
3961
# on git clone via http
4062

@@ -113,6 +135,28 @@ sed -i "s/user nginx;/user $USER www-data;/" /etc/nginx/nginx.conf
113135
chown -R $USER:www-data /usr/share/gitweb
114136

115137
# Start sshd
138+
mv $SSHD_CONF $SSHD_CONF.original
139+
cat << EOF > $SSHD_CONF
140+
# $OpenBSD: sshd_config,v 1.104 2021/07/02 05:11:21 dtucker Exp $
141+
142+
# This is the sshd server system-wide configuration file. See
143+
# sshd_config(5) for more information.
144+
145+
# This sshd was compiled with PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
146+
147+
PubkeyAuthentication yes
148+
149+
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
150+
# but this is overridden so installations will only check .ssh/authorized_keys
151+
AuthorizedKeysFile .ssh/authorized_keys
152+
PasswordAuthentication no
153+
AllowTcpForwarding no
154+
GatewayPorts no
155+
X11Forwarding no
156+
UseDNS no
157+
ChrootDirectory $SERVER_ROOT
158+
EOF
159+
116160
ssh-keygen -A
117161
/usr/sbin/sshd -f /etc/ssh/sshd_config
118162
status=$?
@@ -159,6 +203,21 @@ while sleep 30; do
159203
exit 1
160204
fi
161205

206+
if [ -n "${ZEROTIER_ID}" ]; then
207+
# Check if zerotier is running, otherwise connect
208+
if zerotier-cli info; then
209+
if zerotier-cli listnetworks | grep $ZEROTIER_ID; then
210+
echo "Still connected to $ZEROTIER_ID all good"
211+
else
212+
zerotier-cli join $ZEROTIER_ID
213+
fi
214+
else
215+
zerotier-one -d
216+
sleep 10
217+
zerotier-cli join $ZEROTIER_ID
218+
fi
219+
fi
220+
162221
# Running the script for syncing or backup the repos
163222
$SYNC_SCRIPT
164223
done

sync.sh

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
ENV="/.env"
22
USER=git
3-
SERVER_DIR=/git
3+
SERVER_ROOT=/jail
4+
SERVER_DIR=${SERVER_ROOT}/git
45

56
. /.env
67

@@ -35,6 +36,7 @@ for REPO in ${REPOS}; do
3536
&& cp /init.template ${BASE}_temp/init.sh \
3637
&& chmod +x ${BASE}_temp/init.sh \
3738
&& cd ${BASE}_temp \
39+
&& echo ${BASE} Repository > .git/description \
3840
&& git config user.name GitWeb \
3941
&& git config user.email gitweb@localhost \
4042
&& git add --all \

0 commit comments

Comments
 (0)