-
Notifications
You must be signed in to change notification settings - Fork 84
/
一键所有VPS部署发卡网站.py
250 lines (184 loc) · 9.15 KB
/
一键所有VPS部署发卡网站.py
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
import paramiko
servers = [
{"name": "美国", "hostname": "1.1.1.1", "port": 22, "username": "root", "password": "123456", "domain": "yuming.com"},
{"name": "不丹", "hostname": "1.1.1.1", "port": 22, "username": "root", "password": "123456", "domain": "yuming.com"},
{"name": "毛里求斯", "hostname": "1.1.1.1", "port": 22, "username": "root", "password": "123456", "domain": "yuming.com"},
# 添加更多服务器
]
# 定义更新操作
def update_server(name, hostname, port, username, password, domain):
try:
# 连接服务器
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, port=port, username=username, password=password)
print(f" {name} 更新")
stdin, stdout, stderr = client.exec_command("apt update -y && apt install -y curl wget sudo socat")
print(f"正在更新:")
while not stdout.channel.exit_status_ready():
if stdout.channel.recv_ready():
print(stdout.channel.recv(1024).decode(), end="")
# 检查执行状态
if stderr.channel.recv_exit_status() == 0:
print(f"更新成功")
else:
print(f"更新失败")
print()
print(f"{name} 安装 Docker")
stdin, stdout, stderr = client.exec_command("curl -fsSL https://get.docker.com | sh")
print(f"正在安装 Docker:")
while not stdout.channel.exit_status_ready():
if stdout.channel.recv_ready():
print(stdout.channel.recv(1024).decode(), end="")
# 检查执行状态
if stderr.channel.recv_exit_status() == 0:
print(f"安装 Docker 成功")
else:
print(f"安装 Docker 失败")
print()
print(f"{name} 安装 Docker Compose")
stdin, stdout, stderr = client.exec_command('curl -L "https://github.com/docker/compose/releases/download/v2.18.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose')
print(f"正在安装 Docker Compose:")
while not stdout.channel.exit_status_ready():
if stdout.channel.recv_ready():
print(stdout.channel.recv(1024).decode(), end="")
# 检查执行状态
if stderr.channel.recv_exit_status() == 0:
print(f"安装 Docker Compose 成功")
else:
print(f"安装 Docker Compose 失败")
print()
print(f"{name} 创建web目录")
stdin, stdout, stderr = client.exec_command("cd /home && mkdir -p web/html web/mysql web/certs web/redis && touch web/nginx.conf web/docker-compose.yml")
while not stdout.channel.exit_status_ready():
if stdout.channel.recv_ready():
print(stdout.channel.recv(1024).decode(), end="")
# 检查执行状态
if stderr.channel.recv_exit_status() == 0:
print(f"创建目录成功")
else:
print(f"创建目录失败")
print()
print(f"{name} 申请证书")
stdin, stdout, stderr = client.exec_command("curl https://get.acme.sh | sh && ~/.acme.sh/acme.sh --register-account -m xxxx@gmail.com && ~/.acme.sh/acme.sh --issue -d {} --standalone".format(domain))
print(f"正在申请中:")
while not stdout.channel.exit_status_ready():
if stdout.channel.recv_ready():
print(stdout.channel.recv(1024).decode(), end="")
# 检查执行状态
if stderr.channel.recv_exit_status() == 0:
print(f"申请成功")
else:
print(f"申请失败")
print()
print(f"{name} 下载证书")
stdin, stdout, stderr = client.exec_command("~/.acme.sh/acme.sh --installcert -d {} --key-file /home/web/certs/key.pem --fullchain-file /home/web/certs/cert.pem".format(domain))
while not stdout.channel.exit_status_ready():
if stdout.channel.recv_ready():
print(stdout.channel.recv(1024).decode(), end="")
# 检查执行状态
if stderr.channel.recv_exit_status() == 0:
print(f"下载证书成功")
else:
print(f"下载证书失败")
print()
print(f"{name} 配置nginx")
stdin, stdout, stderr = client.exec_command('wget -O /home/web/nginx.conf https://raw.githubusercontent.com/kejilion/nginx/main/nginx7.conf && sed -i "s/yuming.com/' + domain + '/g" /home/web/nginx.conf')
while not stdout.channel.exit_status_ready():
if stdout.channel.recv_ready():
print(stdout.channel.recv(1024).decode(), end="")
# 检查执行状态
if stderr.channel.recv_exit_status() == 0:
print(f"配置成功")
else:
print(f"配置失败")
print()
print(f"{name} 配置docker-compose.yml")
stdin, stdout, stderr = client.exec_command('wget -O /home/web/docker-compose.yml https://raw.githubusercontent.com/kejilion/docker/main/LNMP-docker-compose-2.yml')
while not stdout.channel.exit_status_ready():
if stdout.channel.recv_ready():
print(stdout.channel.recv(1024).decode(), end="")
# 检查执行状态
if stderr.channel.recv_exit_status() == 0:
print(f"配置成功")
else:
print(f"配置失败")
print()
print(f"{name} 下载网站源码-发卡网站")
stdin, stdout, stderr = client.exec_command('cd /home/web/html && wget https://github.com/assimon/dujiaoka/releases/download/2.0.6/2.0.6-antibody.tar.gz && apt install -y tar && tar -zxvf 2.0.6-antibody.tar.gz && rm 2.0.6-antibody.tar.gz')
while not stdout.channel.exit_status_ready():
if stdout.channel.recv_ready():
print(stdout.channel.recv(1024).decode(), end="")
# 检查执行状态
if stderr.channel.recv_exit_status() == 0:
print(f"配置成功")
else:
print(f"配置失败")
print()
print(f"{name} 启动环境")
stdin, stdout, stderr = client.exec_command('cd /home/web && docker-compose up -d')
print(f"启动中:")
while not stdout.channel.exit_status_ready():
if stdout.channel.recv_ready():
print(stdout.channel.recv(1024).decode(), end="")
# 检查执行状态
if stderr.channel.recv_exit_status() == 0:
print(f"启动成功")
else:
print(f"启动失败")
print()
print(f"{name} 赋予文件权限")
stdin, stdout, stderr = client.exec_command('docker exec nginx chmod -R 777 /var/www/html && docker exec php chmod -R 777 /var/www/html')
while not stdout.channel.exit_status_ready():
if stdout.channel.recv_ready():
print(stdout.channel.recv(1024).decode(), end="")
# 检查执行状态
if stderr.channel.recv_exit_status() == 0:
print(f"赋予成功")
else:
print(f"赋予失败")
print()
print(f"{name} 安装PHP依赖")
stdin, stdout, stderr = client.exec_command('docker exec php apt update && docker exec php apt install -y libmariadb-dev-compat libmariadb-dev libzip-dev')
print(f"安装中:")
while not stdout.channel.exit_status_ready():
if stdout.channel.recv_ready():
print(stdout.channel.recv(1024).decode(), end="")
# 检查执行状态
if stderr.channel.recv_exit_status() == 0:
print(f"安装成功")
else:
print(f"安装失败")
print()
print(f"{name} 安装PHP扩展")
stdin, stdout, stderr = client.exec_command('docker exec php docker-php-ext-install pdo_mysql zip bcmath && docker exec php pecl install redis && docker exec php sh -c \'echo "extension=redis.so" > /usr/local/etc/php/conf.d/docker-php-ext-redis.ini\' && docker restart php')
print(f"安装中:")
while not stdout.channel.exit_status_ready():
if stdout.channel.recv_ready():
print(stdout.channel.recv(1024).decode(), end="")
# 检查执行状态
if stderr.channel.recv_exit_status() == 0:
print(f"安装成功")
else:
print(f"安装失败")
print()
print(f"搭建完成\nhttps://{domain}")
print()
print()
print()
print()
# 关闭 SSH 连接
client.close()
except Exception as e:
print(f"连接 {name} 失败")
# 遍历服务器列表,逐一更新
for server in servers:
name = server["name"]
hostname = server["hostname"]
port = server["port"]
username = server["username"]
password = server["password"]
domain = server["domain"]
update_server(name, hostname, port, username, password, domain)
# 等待用户按下任意键后关闭窗口
input("按任意键关闭窗口...")