Last active 1752015588

rcal_install.sh Raw
1# radicale.install:
2# https://radicale.org/v3.html
3# docker-radicale: Docker image for Radicale calendar and contact server - https://github.com/tomsquest/docker-radicale
4# - How to use sed to remove all double quotes within a file - Stack Overflow - https://stackoverflow.com/questions/7635807/how-to-use-sed-to-remove-all-double-quotes-within-a-file
5alias rcal.install="radicale.install"
6alias ical.install="radicale.install"
7radicale.install(){
8
9# Config vars #
10app_domain="${1:-"cal.$org_domain"}"
11app_user="${2:-"op"}" # Company domain name
12app_name="rcal"
13app_path="/data/${app_name}"
14#app_version="0.2.0"
15app_port="5232"
16app_config="$app_path/config/config"
17app_password=""
18vault="/data/.vault/"
19data_path="$app_path/${app_name}_data"
20PUID="$(id -u)"
21PGID="$(id -g)"
22
23
24# Setup paths #
25mkdir -p "$app_path/config"
26
27echo cd "$app_path"
28cd "$app_path"
29if [ ! $? == "0" ];then
30 err "Failed to cd \"$app_path\""
31 return 1
32fi
33
34# Vault app password
35if [[ ! -e "$vault/$app_name" ]];then
36 # if empty then create password
37 app_password="$(openssl rand -base64 32 | tr -cd "[:alnum:]")"
38 echo "$app_password" > "$vault/$app_name"
39fi
40app_password="$(cat "$vault/$app_name")"
41
42
43# Install App 3 #
44echo git clone https://github.com/tomsquest/docker-radicale
45git clone https://github.com/tomsquest/docker-radicale
46cd ./docker-radicale
47if [ ! $? == "0" ];then
48 err "Failed to cd \"$app_path\""
49 return 1
50fi
51
52# Reset and Update docker-radicale
53echo git reset --hard origin
54git reset --hard origin
55echo git pull
56git pull
57
58# if file does not exists
59if [[ ! -e "$app_path/config/config-example" ]];then
60 # not exists
61 echo cp --no-clobber "$app_path/docker-radicale/config" "$app_path/config/config-example"
62 cp --no-clobber "$app_path/docker-radicale/config" "$app_path/config/config-example"
63fi
64
65
66# Setup User Password
67if [[ ! -e "$app_path/config/users" ]];then
68 # Create User
69 touch "$app_path/config/users"
70 echo htpasswd "$app_path/config/users" op
71 htpasswd "$app_path/config/users" op
72fi
73
74# Setup Server Cert
75if [[ ! -e "$app_path/config/server_cert.pem" ]];then
76 # Create Server Cert
77 echo openssl req -x509 -newkey rsa:4096 -keyout "$app_path/config/server_key.pem" -out "$app_path/config/server_cert.pem" -nodes -days 9999
78 openssl req -x509 -newkey rsa:4096 -keyout "$app_path/config/server_key.pem" -out "$app_path/config/server_cert.pem" -nodes -days 9999
79
80 echo openssl req -x509 -newkey rsa:4096 -keyout "$app_path/config/client_key.pem" -out "$app_path/config/client_cert.pem" -nodes -days 9999
81 openssl req -x509 -newkey rsa:4096 -keyout "$app_path/config/client_key.pem" -out "$app_path/config/client_cert.pem" -nodes -days 9999
82fi
83
84echo chmod 644 "$app_path/config/"*.pem
85chmod 644 "$app_path/config/"*.pem
86
87
88#if [[ ! -e "$app_config" ]];then
89
90rm "$app_config"
91touch "$app_config"
92dasel put -r toml -w toml -t string -f "$app_config" "server.hosts" -v "0.0.0.0:5232"
93dasel put -r toml -w toml -t bool -f "$app_config" "server.ssl" -v "false"
94dasel put -r toml -w toml -t string -f "$app_config" "server.certificate" -v "/config/server_cert.pem"
95dasel put -r toml -w toml -t string -f "$app_config" "server.key" -v "/config/server_key.pem"
96dasel put -r toml -w toml -t string -f "$app_config" "auth.type" -v "htpasswd"
97dasel put -r toml -w toml -t string -f "$app_config" "auth.htpasswd_filename" -v "/config/users"
98dasel put -r toml -w toml -t string -f "$app_config" "auth.htpasswd_encryption" -v "md5"
99dasel put -r toml -w toml -t string -f "$app_config" "storage.filesystem_folder" -v "/data/collections"
100dasel put -r toml -w toml -t string -f "$app_config" "web.type" -v "internal"
101dasel put -r toml -w toml -t string -f "$app_config" "logging.level" -v "warning"
102dasel put -r toml -w toml -t bool -f "$app_config" "logging.mask_passwords" -v "true"
103dasel put -r toml -w toml -t string -f "$app_config" "headers.Access-Control-Allow-Origin" -v "*"
104
105
106# Radicale config is not TOML compatable
107# Quick Fix: Remove double quotes
108sed -i 's/\"//g' "$app_config"
109
110#echo cat "$app_config"
111#cat "$app_config"
112
113#fi
114
115
116# Create folder $data_path
117echo sudo mkdir -p "$data_path"
118sudo mkdir -p "$data_path"
119
120# Set permissions $data_path
121#echo sudo chown -R 1000:1000 "$data_path"
122#sudo chown -R 1000:1000 "$data_path"
123
124# Contain App. Write docker-compose config. 4
125sudo tee "./docker-compose.yml" > /dev/null <<EOF
126services:
127 $app_name:
128 image: tomsquest/docker-radicale
129 container_name: $app_name-$(hostname)
130 user: "$PUID:$PGID"
131 environment:
132 - PUID=$PUID
133 - PGID=$PGID
134 - USER_UID=$PUID
135 - USER_GID=$PGID
136 ports:
137 - $app_port:5232
138 init: true
139 read_only: true
140 security_opt:
141 - no-new-privileges:true
142 cap_drop:
143 - ALL
144 cap_add:
145 - SETUID
146 - SETGID
147 - CHOWN
148 - KILL
149 healthcheck:
150 test: curl -f https://$org_domain || exit 1
151 interval: 30s
152 retries: 3
153 restart: always
154 volumes:
155 - "$data_path:/data:rw"
156 - "$app_path/config:/config:rw"
157EOF
158
159echo web $app_path
160
161echo cat ./docker-compose.yml
162cat ./docker-compose.yml
163
164# Run App. LIVE!
165echo Running $app_name docker container in the background.
166echo sudo docker-compose up -d
167sudo docker-compose up -d
168
169echo https://$(host.ip):$app_port
170
171
172}
173# ---