-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
176 lines (132 loc) · 4.83 KB
/
install.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
echo "INSTALL caddy + consul in DOCKER COMPOSE (HA)"
echo "`cat <<YOLLOPUKKI
██████╗ █████╗ ██████╗ ██████╗ ██╗ ██╗
██╔════╝██╔══██╗██╔══██╗██╔══██╗╚██╗ ██╔╝
██║ ███████║██║ ██║██║ ██║ ╚████╔╝
██║ ██╔══██║██║ ██║██║ ██║ ╚██╔╝
╚██████╗██║ ██║██████╔╝██████╔╝ ██║
╚═════╝╚═╝ ╚═╝╚═════╝ ╚═════╝ ╚═╝
██████╗ ██████╗ ███╗ ██╗███████╗██╗ ██╗██╗
██╔════╝██╔═══██╗████╗ ██║██╔════╝██║ ██║██║
██║ ██║ ██║██╔██╗ ██║███████╗██║ ██║██║
██║ ██║ ██║██║╚██╗██║╚════██║██║ ██║██║
╚██████╗╚██████╔╝██║ ╚████║███████║╚██████╔╝███████╗
╚═════╝ ╚═════╝ ╚═╝ ╚═══╝╚══════╝ ╚═════╝ ╚══════╝
YOLLOPUKKI`"
echo "!!! for HA need 3/5/7/9 etc servers"
read -p "Enter number of servers: " server_count
if ! [[ "$server_count" =~ ^[0-9]+$ ]]; then
echo "Error: enter a number."
exit 1
fi
declare -a server_ips
echo "The first ip must be the address of the current server"
for (( i=1; i<=$server_count; i++ )); do
read -p "Enter external IP server #$i: " server_ip
server_ips+=("$server_ip")
done
command="agent -server -data-dir=//data -bind ${server_ips[0]} -client 0.0.0.0"
mkdir -p /app/caddyconsul -p
cd /app/caddyconsul || exit
mkdir caddy_config consul-data sites files
touch Caddyfile consul-config.json
##### ADD docker-compose.yml
cat << EOF > docker-compose.yml
version: "3.7"
services:
caddy:
network_mode: host
hostname: caddy
container_name: caddy
restart: always
build:
context: .
dockerfile: Dockerfile
volumes:
- ./caddy_config:/config
- ./sites:/sites
- ./files:/files
- ./Caddyfile:/etc/caddy/Caddyfile
consul-$HOSTNAME:
image: hashicorp/consul
hostname: consul-$HOSTNAME
container_name: consul-$HOSTNAME
restart: always
command: $command
network_mode: host
volumes:
- "./consul-data:/consul/data"
- "./consul-config.json:/consul/config/consul-config.json"
EOF
##### ADD Caddyfile
cat << EOF > Caddyfile
{
email [email protected]
storage consul {
address "${server_ips[0]}:8500"
token "consul-access-token"
timeout 10
prefix "caddytls"
value_prefix "myprefix"
aes_key "consultls-1234567890-caddytls-32"
tls_enabled "false"
tls_insecure "true"
}
}
import /sites/*
{
}
EOF
cat << EOF > sites/test
test.${server_ips[0]}.sslip.io {
respond 200
}
EOF
##### ADD Dockerfile
cat << \EOF > Dockerfile
FROM caddy:2.6.4-builder AS builder
RUN xcaddy build \
--with github.com/pteich/caddy-tlsconsul \
--with github.com/hundertzehn/caddy-ratelimit
FROM caddy:2.6.4
COPY --from=builder /usr/bin/caddy /usr/bin/caddy
EXPOSE 80
EXPOSE 443
EXPOSE 2019
CMD ["caddy", "run", "--config", "/etc/caddy/Caddyfile", "--adapter", "caddyfile"]
EOF
##### ADD consul-config.json
cat << EOF > consul-config.json
{
"bootstrap_expect": $server_count,
"client_addr": "0.0.0.0",
"datacenter": "Mydc",
"data_dir": "/var/consul",
"domain": "consul",
"enable_script_checks": true,
"dns_config": {
"enable_truncate": true,
"only_passing": true
},
"encrypt": "CONSULKEY",
"leave_on_terminate": true,
"log_level": "INFO",
"rejoin_after_leave": true,
"server": true,
EOF
echo ' "start_join": [' >> consul-config.json
for server_ip in "${server_ips[@]}"; do
echo " \"$server_ip\"," >> consul-config.json
done
sed -i '$ s/,$/\n \],/' consul-config.json
echo ' "ui": true' >> consul-config.json
echo '}' >> consul-config.json
docker compose up -d --build
sleep 10
echo "if multiple server - go to next server and run script there"
echo "open browser and check https://test.${server_ips[0]}.sslip.io - for caddy test(after install on all servers)"
echo "open browser and check http://${server_ips[0]}:8500/ui/ - for consul test(after install on all servers)"
#### clean
# cd /app/caddyconsul && docker compose stop
# cd / && rm -rf /app/caddyconsul
# docker system prune -a