22
33set -e
44
5+ PUID=${PUID:- 1000}
6+ PGID=${PGID:- 1000}
7+
58SSH_DIR=" /home/borgwarehouse/.ssh"
69AUTHORIZED_KEYS_FILE=" $SSH_DIR /authorized_keys"
710REPOS_DIR=" /home/borgwarehouse/repos"
11+ CONFIG_DIR=" /home/borgwarehouse/app/config"
12+
13+ print_green () { echo -e " \e[92m$1 \e[0m" ; }
14+ print_red () { echo -e " \e[91m$1 \e[0m" ; }
15+
16+ # 1. Remap borgwarehouse to PUID:PGID
17+
18+ remap_user () {
19+ if [ " $PUID " -eq 0 ] || [ " $PGID " -eq 0 ]; then
20+ print_red " [ERROR] PUID and PGID cannot be 0. Running the app as root is not allowed."
21+ exit 1
22+ fi
823
9- print_green () {
10- echo -e " \e[92m$1 \e[0m" ;
24+ print_green " Mapping borgwarehouse to UID=$PUID GID=$PGID "
25+ groupmod -o -g " $PGID " borgwarehouse
26+ usermod -o -u " $PUID " borgwarehouse
27+
28+ # Chown the application directory only (never the user's volume mounts)
29+ chown -R borgwarehouse:borgwarehouse /home/borgwarehouse/app
30+ chown borgwarehouse:borgwarehouse /home/borgwarehouse /home/borgwarehouse/moduli
1131}
12- print_red () {
13- echo -e " \e[91m$1 \e[0m" ;
32+
33+ # 2. Check volume is mounted and writable
34+
35+ check_volume () {
36+ local dir=$1
37+ local name=$2
38+
39+ if [ ! -d " $dir " ]; then
40+ print_red " [ERROR] Volume '$name ' is not mounted. Expected path: $dir "
41+ print_red " Check the volumes section in your docker-compose.yml."
42+ exit 1
43+ fi
44+
45+ if ! gosu borgwarehouse test -w " $dir " 2> /dev/null; then
46+ print_red " [ERROR] Volume '$name ' ($dir ) is not writable by UID=$PUID GID=$PGID ."
47+ print_red " Fix on the host: chown -R $PUID :$PGID <your-host-path-for-$name >"
48+ exit 1
49+ fi
1450}
1551
52+ # 3. Generate SSH host keys if needed
53+
1654init_ssh_server () {
1755 if [ -z " $( ls -A /etc/ssh) " ]; then
1856 print_green " /etc/ssh is empty, generating SSH host keys..."
@@ -25,31 +63,19 @@ init_ssh_server() {
2563 fi
2664}
2765
28- check_ssh_directory () {
29- if [ ! -d " $SSH_DIR " ]; then
30- print_red " The .ssh directory does not exist, you need to mount it as docker volume."
31- exit 1
32- else
33- chmod 700 " $SSH_DIR "
34- fi
35- }
66+ # 4. Setup authorized_keys
3667
37- create_authorized_keys_file () {
68+ setup_authorized_keys () {
3869 if [ ! -f " $AUTHORIZED_KEYS_FILE " ]; then
39- print_green " The authorized_keys file does not exist, creating ..."
70+ print_green " Creating authorized_keys file..."
4071 touch " $AUTHORIZED_KEYS_FILE "
4172 fi
42- chmod 600 " $AUTHORIZED_KEYS_FILE "
73+ chown borgwarehouse:borgwarehouse " $SSH_DIR " " $AUTHORIZED_KEYS_FILE "
74+ chmod 700 " $SSH_DIR "
75+ chmod 600 " $AUTHORIZED_KEYS_FILE "
4376}
4477
45- check_repos_directory () {
46- if [ ! -d " $REPOS_DIR " ]; then
47- print_red " The repos directory does not exist, you need to mount it as docker volume."
48- exit 2
49- else
50- chmod 700 " $REPOS_DIR "
51- fi
52- }
78+ # 5. Read SSH fingerprints
5379
5480get_SSH_fingerprints () {
5581 print_green " Getting SSH fingerprints..."
@@ -61,6 +87,8 @@ get_SSH_fingerprints() {
6187 export SSH_SERVER_FINGERPRINT_ECDSA=" $ECDSA_FINGERPRINT "
6288}
6389
90+ # 6. Check secrets
91+
6492check_env () {
6593 if [ -z " $CRONJOB_KEY " ]; then
6694 CRONJOB_KEY=$( openssl rand -base64 32)
@@ -75,11 +103,16 @@ check_env() {
75103 fi
76104}
77105
106+ # Run
107+
108+ remap_user
78109check_env
110+ mkdir -p /run/sshd
79111init_ssh_server
80- check_ssh_directory
81- create_authorized_keys_file
82- check_repos_directory
112+ check_volume " $SSH_DIR " " .ssh"
113+ check_volume " $REPOS_DIR " " repos"
114+ check_volume " $CONFIG_DIR " " config"
115+ setup_authorized_keys
83116get_SSH_fingerprints
84117
85118print_green " Successful initialization. BorgWarehouse is ready !"
0 commit comments