-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote_setup_script.py
More file actions
303 lines (286 loc) · 14.1 KB
/
Copy pathremote_setup_script.py
File metadata and controls
303 lines (286 loc) · 14.1 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env python3
"""One-time SSH provisioning; application files reach the server through Git."""
import argparse
import getpass
import hashlib
import json
import os
from pathlib import Path
import shlex
import subprocess
import sys
import time
import tempfile
import paramiko
def connect(host, user, key=None, password=None):
config = paramiko.SSHConfig()
path = Path.home()/'.ssh/config'
if path.exists():
with path.open() as stream:
config.parse(stream)
settings = config.lookup(host)
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.RejectPolicy())
client.connect(hostname=settings.get('hostname',host),port=int(settings.get('port',22)),
username=user or settings.get('user','root'),password=password,
key_filename=key or settings.get('identityfile'),allow_agent=True,
look_for_keys=True,timeout=15,banner_timeout=15,auth_timeout=20)
client.get_transport().set_keepalive(30)
return client
def run(client, argv, cwd=None):
command=shlex.join([str(x) for x in argv])
if cwd:
command=f'cd {shlex.quote(cwd)} && '+command
channel=client.get_transport().open_session(timeout=20)
channel.exec_command(command)
output=[]
deadline=time.monotonic()+1800
try:
while True:
while channel.recv_ready():
chunk=channel.recv(65536).decode(errors='replace')
output.append(chunk)
print(chunk,end='',flush=True)
while channel.recv_stderr_ready():
chunk=channel.recv_stderr(65536).decode(errors='replace')
print(chunk,end='',file=sys.stderr,flush=True)
if channel.exit_status_ready() and not channel.recv_ready() and not channel.recv_stderr_ready():
break
if time.monotonic()>deadline:
raise TimeoutError('Remote command exceeded 30 minutes')
time.sleep(.02)
code=channel.recv_exit_status()
if code:
raise subprocess.CalledProcessError(code,argv,output=''.join(output))
return ''.join(output)
finally:
channel.close()
def atomic_local(path, content):
fd, name = tempfile.mkstemp(prefix='.' + path.name, dir=path.parent)
try:
with os.fdopen(fd, 'w') as stream:
os.fchmod(stream.fileno(), 0o600)
stream.write(content)
stream.flush()
os.fsync(stream.fileno())
os.replace(name, path)
finally:
if os.path.exists(name):
os.unlink(name)
def download_pool(sftp, directory, local):
"""Preserve credentials and diagnostics even when creation/verification fails."""
local.mkdir(mode=0o700, parents=True, exist_ok=True)
local.chmod(0o700)
try:
with sftp.open(directory + '/deployment.json') as source:
deployment = json.loads(source.read())
except FileNotFoundError:
return None
atomic_local(local / 'deployment.json', json.dumps(deployment, indent=2) + '\n')
try:
with sftp.open(directory + '/expansion.json') as source:
atomic_local(local / 'expansion.json', source.read().decode())
except FileNotFoundError:
pass
combined = []
for project in deployment.get('projects', []):
if Path(project).name != project or project in ('.', '..'):
raise ValueError('Invalid project in deployment record')
destination = local / project
destination.mkdir(mode=0o700, exist_ok=True)
destination.chmod(0o700)
for name in ('extracted_proxy', 'proxy_configs', 'pool.json'):
try:
with sftp.open(f'{directory}/generated_proxy_configs/{project}/{name}') as source:
content = source.read().decode()
except FileNotFoundError:
continue
atomic_local(destination / name, content)
if name == 'extracted_proxy':
combined.extend(content.splitlines())
atomic_local(local / 'extracted_proxy', '\n'.join(combined) + ('\n' if combined else ''))
return deployment
def migrate(client, sftp, args, network):
local = args.output_dir / args.host
common = ['--legacy-directory', args.legacy_directory, '--project-prefix', args.project_prefix,
'--listen-port', str(args.listen_port), *network]
if args.phase == 'prepare':
run(client, ['python3', 'migrate_server.py', 'inspect', *common], args.directory)
if not args.skip_install:
run(client, ['bash', 'install_all.sh'], args.directory)
try:
run(client, ['python3', 'migrate_server.py', args.phase, *common], args.directory)
finally:
deployment = download_pool(sftp, args.directory, local)
if deployment is None:
raise RuntimeError('Migration record is missing')
if args.phase != 'prepare':
return 2 if deployment['status'] == 'rollback_pending' else 0
if deployment['status'] == 'complete':
return 0
if args.skip_check:
print(f'Pool prepared but NOT VERIFIED: {local}')
return 2
failures, checked, reports = 0, 0, hashlib.sha256()
for project in deployment['projects']:
result = subprocess.run([sys.executable, str(Path(__file__).with_name('4_proxy_checker.py')),
'--project-name', project, '--base-dir', str(local), '--concurrency', str(args.check_concurrency)])
failures += result.returncode != 0
path = local / project / 'proxy_check_results.txt'
if path.exists():
raw = path.read_bytes()
reports.update(raw)
rows = [json.loads(line) for line in raw.splitlines() if line.strip()]
checked += sum(row.get('ok') is True for row in rows)
try:
run(client, ['python3', 'migrate_server.py', 'verify', '--verification',
'failed' if failures else 'passed', '--verified-count', str(checked),
'--report-sha256', reports.hexdigest()], args.directory)
finally:
download_pool(sftp, args.directory, local)
print(f'Prepared and verified {checked} proxies. Migrate consumers before --phase finalize.')
return 0
def expand(client, sftp, args):
local = args.output_dir / args.host
try:
if args.phase == 'rollback':
run(client, ['python3', 'expand_server.py', 'rollback'], args.directory)
return 0
raw = run(client, ['python3', 'expand_server.py', 'prepare', '--target-count',
str(args.target_count)], args.directory)
if json.loads(raw)['status'] == 'complete':
return 0
run(client, ['python3', 'expand_server.py', 'apply'], args.directory)
finally:
download_pool(sftp, args.directory, local)
if args.skip_check:
print(f'Expanded pool is NOT VERIFIED: {local}')
return 2
record = json.loads((local / 'expansion.json').read_text())
project = record['project']
result = subprocess.run([sys.executable, str(Path(__file__).with_name('4_proxy_checker.py')),
'--project-name', project, '--base-dir', str(local), '--concurrency', str(args.check_concurrency)])
report = (local / project / 'proxy_check_results.txt').read_bytes()
rows = [json.loads(line) for line in report.splitlines() if line.strip()]
checked = sum(r.get('ok') is True for r in rows)
try:
run(client, ['python3', 'expand_server.py', 'verify', '--verification',
'passed' if result.returncode == 0 else 'failed', '--verified-count', str(checked),
'--report-sha256', hashlib.sha256(report).hexdigest()], args.directory)
finally:
download_pool(sftp, args.directory, local)
print(f'Expanded and verified {checked} identities; credentials saved privately to {local}')
return 0
def main():
parser=argparse.ArgumentParser(description=__doc__)
parser.add_argument('--host')
parser.add_argument('--user',default='root')
parser.add_argument('--key')
parser.add_argument('--password',action='store_true',help='Prompt securely for an SSH password')
parser.add_argument('--target-count',type=int)
parser.add_argument('--mode',choices=('create','migrate','expand'),default='create')
parser.add_argument('--phase',choices=('prepare','finalize','rollback'),default='prepare')
parser.add_argument('--legacy-directory',default='/home/3proxy_configs_pub')
parser.add_argument('--listen-port',type=int,default=20000)
parser.add_argument('--check-concurrency',type=int,default=10)
parser.add_argument('--project-prefix')
parser.add_argument('--ipv6-subnet')
parser.add_argument('--interface')
parser.add_argument('--memory-max-mib', type=int)
parser.add_argument('--reserve-memory-mib', type=int)
parser.add_argument('--cpu-quota-percent', type=int)
parser.add_argument('--external-ipv4')
parser.add_argument('--repo-url',default='https://github.com/petrovichest/3proxy_configs_pub.git')
parser.add_argument('--directory')
parser.add_argument('--skip-install',action='store_true')
parser.add_argument('--skip-check',action='store_true',help='Export an unverified pool; returns exit code 2')
parser.add_argument('--output-dir',type=Path,default=Path('downloaded_configs'))
args=parser.parse_args()
args.host=args.host or input('SSH host: ').strip()
args.directory = args.directory or ('/home/3proxy_shared' if args.mode != 'create' else '/home/3proxy_configs_pub')
args.project_prefix = args.project_prefix or ('shared' if args.mode == 'migrate' else 'capacity')
if args.mode == 'create':
args.target_count=args.target_count if args.target_count is not None else int(input('Total proxy count: '))
if args.target_count<=0:
parser.error('Positive target count required')
elif args.mode == 'migrate' and args.target_count is not None:
parser.error('Migration preserves the existing count; omit --target-count')
if args.mode == 'expand':
if args.phase == 'finalize':
parser.error('Expansion verifies automatically; use prepare or rollback')
if args.phase == 'prepare' and (args.target_count is None or args.target_count <= 0):
parser.error('Expansion requires a positive --target-count')
if any((args.interface, args.external_ipv4, args.ipv6_subnet, args.project_prefix != 'capacity')):
parser.error('Expansion reads the project and network from the existing pool')
if args.check_concurrency <= 0 or not 1 <= args.listen_port <= 65535:
parser.error('Invalid check concurrency or listen port')
resource_options = []
for name in ('memory_max_mib', 'reserve_memory_mib', 'cpu_quota_percent'):
value = getattr(args, name)
if value is not None:
if value <= 0 or args.mode != 'create':
parser.error('Positive resource budgets are supported only for fresh creation')
resource_options += ['--' + name.replace('_', '-'), str(value)]
if args.mode == 'migrate' and args.directory.rstrip('/') == args.legacy_directory.rstrip('/'):
parser.error('Migration requires a separate destination directory')
if Path(args.host).name != args.host or args.host in ('.', '..'):
parser.error('Invalid host')
os.umask(0o077)
client=connect(args.host,args.user,args.key,getpass.getpass('SSH password: ') if args.password else None)
try:
if not args.skip_install and args.mode != 'expand':
run(client,['bash','-c','if ! command -v git >/dev/null 2>&1; then '
'export DEBIAN_FRONTEND=noninteractive; '
'apt-get update -q && apt-get install -y git ca-certificates; fi'])
sftp=client.open_sftp()
try:
sftp.stat(args.directory+'/.git')
except FileNotFoundError:
run(client,['git','clone',args.repo_url,args.directory])
else:
run(client,['git','pull','--ff-only'],args.directory)
network = []
for option, value in (('--interface', args.interface), ('--external-ipv4', args.external_ipv4),
('--ipv6-subnet', args.ipv6_subnet)):
if value:
network += [option, value]
if args.mode == 'migrate':
return migrate(client, sftp, args, network)
if args.mode == 'expand':
return expand(client, sftp, args)
# Discovery and the existing-pool guard run before installing packages.
run(client, ['python3', 'server_preflight.py', *network], args.directory)
if not args.skip_install:
run(client,['bash','install_all.sh'],args.directory)
local=args.output_dir/args.host
try:
run(client, ['venv/bin/python', 'provision_server.py', 'create', '--count', str(args.target_count),
'--project-prefix', args.project_prefix, *network, *resource_options], args.directory)
finally:
deployment = download_pool(sftp, args.directory, local)
if deployment is None:
raise RuntimeError('Remote deployment record is missing')
if args.skip_check:
print(f'Pool exported but NOT VERIFIED: {local}. Installation is not complete.')
return 2
failures = 0
for project in deployment['projects']:
result = subprocess.run([sys.executable, str(Path(__file__).with_name('4_proxy_checker.py')),
'--project-name', project, '--base-dir', str(local),
'--concurrency', str(args.check_concurrency)])
failures += result.returncode != 0
try:
run(client, ['venv/bin/python', 'provision_server.py', 'finalize',
'--verification', 'failed' if failures else 'passed'], args.directory)
finally:
download_pool(sftp, args.directory, local)
if failures:
raise RuntimeError(f'External verification failed; credentials and reports retained in {local}')
print(f'Verified {args.target_count} proxies; credentials saved privately to {local}')
sftp.close()
return 0
finally:
client.close()
if __name__=='__main__':
raise SystemExit(main())