forked from Ravinou/borgwarehouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-bw-init.sh
executable file
·91 lines (77 loc) · 2.49 KB
/
docker-bw-init.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash
set -e
SSH_DIR="/home/borgwarehouse/.ssh"
AUTHORIZED_KEYS_FILE="$SSH_DIR/authorized_keys"
REPOS_DIR="/home/borgwarehouse/repos"
print_green() {
echo -e "\e[92m$1\e[0m";
}
print_red() {
echo -e "\e[91m$1\e[0m";
}
init_ssh_server() {
if [ -z "$(ls -A /etc/ssh)" ]; then
print_green "/etc/ssh is empty, generating SSH host keys..."
ssh-keygen -A
cp /home/borgwarehouse/sshd_config /home/borgwarehouse/moduli /etc/ssh/
fi
}
check_ssh_directory() {
if [ ! -d "$SSH_DIR" ]; then
print_red "The .ssh directory does not exist, you need to mount it as docker volume."
exit 1
else
chmod 700 "$SSH_DIR"
fi
}
create_authorized_keys_file() {
if [ ! -f "$AUTHORIZED_KEYS_FILE" ]; then
print_green "The authorized_keys file does not exist, creating..."
touch "$AUTHORIZED_KEYS_FILE"
fi
chmod 600 "$AUTHORIZED_KEYS_FILE"
}
check_repos_directory() {
if [ ! -d "$REPOS_DIR" ]; then
print_red "The repos directory does not exist, you need to mount it as docker volume."
exit 2
else
chmod 700 "$REPOS_DIR"
fi
}
add_cron_job() {
print_green "Adding cron job..."
local CRON_JOB="* * * * * curl --request POST --url 'http://$HOSTNAME:3000/api/cronjob/checkStatus' --header 'Authorization: Bearer $CRONJOB_KEY'; curl --request POST --url 'http://$HOSTNAME:3000/api/cronjob/getStorageUsed' --header 'Authorization: Bearer $CRONJOB_KEY'"
echo "$CRON_JOB" | crontab -u borgwarehouse -
}
get_SSH_fingerprints() {
print_green "Getting SSH fingerprints..."
RSA_FINGERPRINT=$(ssh-keygen -lf /etc/ssh/ssh_host_rsa_key | awk '{print $2}')
ED25519_FINGERPRINT=$(ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key | awk '{print $2}')
ECDSA_FINGERPRINT=$(ssh-keygen -lf /etc/ssh/ssh_host_ecdsa_key | awk '{print $2}')
export SSH_SERVER_FINGERPRINT_RSA="$RSA_FINGERPRINT"
export SSH_SERVER_FINGERPRINT_ED25519="$ED25519_FINGERPRINT"
export SSH_SERVER_FINGERPRINT_ECDSA="$ECDSA_FINGERPRINT"
}
check_env() {
if [ -z "$CRONJOB_KEY" ]; then
CRONJOB_KEY=$(openssl rand -base64 32)
print_green "CRONJOB_KEY not found or empty. Generating a random key..."
export CRONJOB_KEY
fi
if [ -z "$NEXTAUTH_SECRET" ]; then
NEXTAUTH_SECRET=$(openssl rand -base64 32)
print_green "NEXTAUTH_SECRET not found or empty. Generating a random key..."
export NEXTAUTH_SECRET
fi
}
check_env
init_ssh_server
check_ssh_directory
create_authorized_keys_file
check_repos_directory
add_cron_job
get_SSH_fingerprints
sudo service ssh restart
sudo service cron restart
exec "$@"