-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
287 lines (247 loc) · 12.6 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/bin/bash
if grep -q "VERSION_ID=\"10\"" /etc/os-release; then
echo "Этот скрипт не может быть выполнен на Debian 10."
exit 1
fi
# Здесь идет код скрипта, который должен быть выполнен на всех системах, кроме Debian 10
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
RED='\033[0;31m'
NC='\033[0m'
# Проверяем, выполняется ли скрипт от имени пользователя root
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}Запустите скрипт с правами root${NC}"
exit
fi
# Проверяем, установлен ли Docker
if [ -x "$(command -v docker)" ]; then
echo -e "${GREEN}Docker уже установлен${NC}"
else
# Проверяем, какое распределение используется, и устанавливаем необходимые зависимости
if [ -f /etc/debian_version ]; then
apt-get update
apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release
elif [ -f /etc/redhat-release ]; then
dnf install -y dnf-plugins-core
dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
dnf install -y curl
else
echo -e "${RED}Неподдерживаемое распределение${NC}"
exit
fi
# Устанавливаем Docker
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh
# Запускаем и включаем службу Docker
systemctl start docker
systemctl enable docker
echo -e "${GREEN}Docker успешно установлен${NC}"
fi
# Проверка наличия docker-compose
if command -v docker-compose &> /dev/null
then
printf "${GREEN}Docker Compose уже установлен\n${NC}"
else
# Установка docker-compose
curl -L --fail https://raw.githubusercontent.com/linuxserver/docker-docker-compose/master/run.sh -o /usr/local/bin/docker-compose &&
chmod +x /usr/local/bin/docker-compose
# Проверка успешности установки
if [ $? -eq 0 ]; then
printf "${GREEN}Установка Docker Compose завершена успешно\n${NC}"
else
printf "${GREEN}Ошибка при установке Docker Compose\n${NC}"
printf "${YELLOW}Хотите продолжить выполнение скрипта? (y/n): ${NC}"
read choice
case "$choice" in
y|Y )
printf "${GREEN}Продолжение выполнения скрипта\n${NC}"
;;
n|N )
printf "${RED}Завершение выполнения скрипта\n${NC}"
exit 1
;;
* )
printf "${RED}Неверный выбор. Завершение выполнения скрипта\n${NC}"
exit 1
;;
esac
fi
fi
# Проверка актуальности версии docker-compose
#LATEST_VERSION=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep "tag_name" | cut -d\" -f4)
#INSTALLED_VERSION=$(docker-compose version --short 2>/dev/null)
#if [ "$LATEST_VERSION" = "$INSTALLED_VERSION" ]; then
# printf "${GREEN}Установленная версия Docker Compose (%s) является актуальной\n" "$INSTALLED_VERSION${NC}"
##else
# printf "${YELLOW}Установленная версия Docker Compose (%s) не является актуальной. Последняя версия: %s\n" "$INSTALLED_VERSION" "$LATEST_VERSION${NC}"
#
#fi
# Устанавливаем редактор Nano
if ! command -v nano &> /dev/null
then
read -p "Хотите установить текстовый редактор Nano? (y/n) " INSTALL_NANO
if [ "$INSTALL_NANO" == "y" ]; then
apt-get update
apt-get install -y nano
fi
else
echo "Текстовый редактор Nano уже установлен."
fi
# Проверяем есть ли контейнер с именем wireguard
printf "${BLUE} Сейчас проверим свободен ли порт 51821 и не установлен ли другой wireguard.\n${NC}"
if [[ $(docker ps -q --filter "name=wireguard") ]]; then
printf "!!!!>>> Другой Wireguard контейнер уже запущен, и вероятно занимает порт 51820. Пожалуйста удалите его и запустите скрипт заново\n "
printf "${RED} !!!!>>> Завершаю скрипт! \n${NC}"
exit 1
else
printf "Wireguard контейнер не запущен в докер. Можно продолжать\n"
# Проверка, запущен ли контейнер, использующий порт 51821
if lsof -Pi :51820 -sTCP:LISTEN -t >/dev/null ; then
printf "${RED}!!!!>>> Порт 51820 уже используется контейнером.!\n ${NC}"
if docker ps --format '{{.Names}} {{.Ports}}' | grep -q "wireguard.*:51820->" ; then
printf "WireGuard контейнер использует порт 51820. Хотите продолжить установку? (y/n): "
read -r choice
case "$choice" in
y|Y ) printf "Продолжаем установку...\n" ;;
n|N ) printf "${RED} ******* Завершаю скрипт!\n ${NC}" ; exit 1;;
* ) printf "${RED}Некорректный ввод. Установка остановлена.${NC}" ; exit 1;;
esac
else
printf "${RED} ******* Завершаю скрипт!\n ${NC}"
exit 1
fi
else
printf "Порт 51820 свободен.\n"
printf "Хотите продолжить установку? (y/n): "
read -r choice
case "$choice" in
y|Y ) printf "Продолжаем установку...\n" ;;
n|N ) printf "Установка остановлена.${NC}" ; exit 1;;
* ) printf "${RED}Некорректный ввод. Установка остановлена.${NC}" ; exit 1;;
esac
fi
fi
printf "${GREEN} Этап проверки докера закончен, можно продолжить установку\n${NC}"
##### ЗДЕСЬ БУДЕТ КОД ДЛЯ КОРРЕКТИРОВКИ COMPOSE
# Получаем внешний IP-адрес
MYHOST_IP=$(curl -s https://checkip.amazonaws.com/)
# Записываем IP-адрес в файл docker-compose.yml с меткой MYHOSTIP
sed -i -E "s/- SERVERURL=.*/- SERVERURL=$MYHOST_IP/g" docker-compose.yml
echo "Выберите способ настройки PEERS:"
echo "1. Установить количество пиров"
echo "2. Задать имена пиров через запятую"
read -p "Введите номер способа: " choice
if [ $choice -eq 1 ]
then
read -p "Введите количество пиров: " peers
sed -i "s/- PEERS=1/- PEERS=$peers/g" docker-compose.yml
echo "Количество пиров изменено на $peers"
elif [ $choice -eq 2 ]
then
read -p "Введите имена пиров через запятую: " peers
# Проверяем, используются ли имена
if [[ "$peers" == *[!a-zA-Z0-9,]* ]]
then
echo "Ошибка: имена пиров могут содержать только латинские буквы и цифры"
exit 1
fi
# Проверяем, существует ли уже переменная среды PEERS
if grep -q "PEERS=" docker-compose.yml
then
# Если переменная уже существует
# Спрашиваем пользователя, заменить ли текущие имена на новые
echo "Переменная PEERS уже существует"
echo "1. Заменить текущие имена на новые"
echo "2. Добавить новые имена к текущим"
read -p "Введите номер способа: " add_choice
if [ $add_choice -eq 1 ]
then
sed -i "s/- PEERS=.*/- PEERS=$peers/g" docker-compose.yml
elif [ $add_choice -eq 2 ]
then
current_peers=$(grep PEERS docker-compose.yml | cut -d '=' -f 2 | tr -d '"')
new_peers=$(echo "$current_peers,$peers")
sed -i "s/- PEERS=.*/- PEERS=$new_peers/g" docker-compose.yml
else
echo "Ошибка: неверный выбор"
exit 1
fi
else
# Если переменная не существует, добавляем ее с новыми именами
sed -i "s/- PEERS=1/- PEERS=\"$peers\"/g" docker-compose.yml
fi
echo "Имена пиров изменены на $peers"
else
echo "Ошибка: неверный выбор"
exit 1
fi
# Устанавливаем apache2-utils, если она не установлена
if ! [ -x "$(command -v htpasswd)" ]; then
echo -e "${RED}Установка apache2-utils...${NC}" >&2
apt-get update
apt-get install apache2-utils -y
fi
# Если логин не введен, устанавливаем логин по умолчанию "admin"
while true; do
printf "${YELLOW} Теперь необходимо задать параметры для AdGuard HOME \n${NC}"
echo -e "${YELLOW}Введите логин (только латинские буквы и цифры), если пропустить шаг будет задан логин admin:${NC}"
read username
if [ -z "$username" ]; then
username="admin"
break
fi
if ! [[ "$username" =~ [^a-zA-Z0-9] ]]; then
break
else
echo -e "${RED}Логин должен содержать только латинские буквы и цифры.${NC}"
fi
done
# Запрашиваем у пользователя пароль
while true; do
echo -e "${YELLOW}Введите пароль (если нажать Enter, пароль будет задан по умолчанию admin):${NC}"
read password
if [ -z "$password" ]; then
password="admin"
break
fi
if ! [[ "$password" =~ [^a-zA-Z0-9] ]]; then
break
else
echo -e "${RED}Пароль должен содержать латинские буквы верхнего и нижнего регистра, цифры.${NC}"
fi
done
# Генерируем хеш пароля с помощью htpasswd из пакета apache2-utils
hashed_password=$(htpasswd -nbB $username "$password" | cut -d ":" -f 2)
# Экранируем символы / и & в hashed_password
hashed_password=$(echo "$hashed_password" | sed -e 's/[\/&]/\\&/g')
# Проверяем наличие файла AdGuardHome.yaml и его доступность для записи
if [ ! -w "conf/AdGuardHome.yaml" ]; then
echo -e "${RED}Файл conf/AdGuardHome.yaml не существует или не доступен для записи.${NC}" >&2
exit 1
fi
# Записываем связку логина и зашифрованного пароля в файл conf/AdGuardHome.yaml
if
# sed -i "s/\(name: $username\).*\(password: \).*/\1\n\2$hashed_password/" conf/AdGuardHome.yaml
sed -i -E "s/- name: .*/- name: $username/g" conf/AdGuardHome.yaml
sed -i -E "s/password: .*/password: $hashed_password/g" conf/AdGuardHome.yaml
then
# Выводим сообщение об успешной записи связки логина и пароля в файл
echo -e "${GREEN}Связка логина и пароля успешно записана в файл conf/AdGuardHome.yaml${NC}"
else
echo -e "${RED}Не удалось записать связку логина и пароля в файл conf/AdGuardHome.yaml.${NC}" >&2
exit 1
fi
# Выводим связку логина и пароля в консоль
echo "Ниже представлены логин и пароль для входа в AdGuardHome"
echo -e "${GREEN}Логин: $username${NC}"
echo -e "${GREEN}Пароль: $password${NC}"
# Запускаем docker-compose
docker-compose up -d
echo ""
echo -e "Адрес входа в веб-интерфейс AdGuardHome после установки (только когда подключитесь к сети WireGuard!!!): ${BLUE}http://agh.local${NC}"
echo "Ниже представлены логин и пароль для входа в AdGuardHome"
echo -e "Логин:${BLUE} $username${NC}"
echo -e "Пароль:${BLUE} $password${NC}"
echo ""
echo -e "${GREEN}Заходите на мой форум: https://openode.ru${NC}"