import base64 import hashlib import json from pathlib import Path import posixpath import shlex import tarfile import tempfile import time import uuid import zipfile import paramiko from PySide6.QtCore import QThread, Signal from . import remote from .store import data_dir def validate_config(config): remote.validate(config) def package_front(source, output): source = Path(source) with tempfile.TemporaryDirectory() as scratch: root = source if source.is_file() and source.suffix.lower() == '.zip': root = Path(scratch) with zipfile.ZipFile(source) as z: for entry in z.infolist(): path = Path(entry.filename.replace('\\', '/')) if path.is_absolute() or '..' in path.parts or ':' in entry.filename or (entry.external_attr >> 16) & 0o170000 == 0o120000: raise ValueError('ZIP 包包含不安全条目') z.extractall(root) if not (root / 'index.html').is_file() and (root / 'dist/index.html').is_file(): root = root / 'dist' if not root.is_dir() or not (root / 'index.html').is_file(): raise ValueError('请选择包含 index.html 的 dist 文件夹或 ZIP 压缩包') with tarfile.open(output, 'w:gz') as archive: for p in root.rglob('*'): if p.is_symlink(): raise ValueError('前端目录不能包含软链接') if p.is_file(): info = archive.gettarinfo(str(p), p.relative_to(root).as_posix()) info.mode = 0o644 with p.open('rb') as f: archive.addfile(info, f) class RememberHost(paramiko.MissingHostKeyPolicy): def missing_host_key(self, client, hostname, key): client.get_host_keys().add(hostname, key.get_name(), key) client.save_host_keys(str(data_dir() / 'known_hosts')) class Worker(QThread): message = Signal(str) progress = Signal(int) completed = Signal(bool, str, str) def __init__(self, config, action='deploy', mode='all', backup='', parent=None): super().__init__(parent) self.config, self.action, self.mode = dict(config), action, mode self.restore_backup = backup self.backup = '' self.logs = [] self.client = None self.remote_result = '' def log(self, message): password = self.config.get('password') text = str(message).replace(password, '••••••') if password else str(message) line = time.strftime('%H:%M:%S') + ' ' + text self.logs.append(line) self.message.emit(line) def run_remote(self, script, payload): encoded = base64.b64encode(json.dumps(payload).encode()).decode() command = 'python3 -u ' + shlex.quote(script) + ' ' + shlex.quote(encoded) channel = self.client.get_transport().open_session(timeout=15) channel.set_combine_stderr(True) channel.exec_command(command) channel.shutdown_write() buffer = b'' # A silent, large backup can take a while. Socket liveness is still checked. last = time.monotonic() try: while True: if channel.recv_ready(): chunk = channel.recv(65536) buffer += chunk last = time.monotonic() while b'\n' in buffer: line, buffer = buffer.split(b'\n', 1) self.consume(line) elif channel.exit_status_ready(): break else: if not self.client.get_transport().is_active(): raise ConnectionError('SSH 连接断开;远端操作结果未知,请重新连接检查服务和备份') if time.monotonic() - last > 7200: raise TimeoutError('远程任务长时间无响应;结果未知,请检查服务器') time.sleep(.05) if buffer: self.consume(buffer) code = channel.recv_exit_status() if code != 0: raise RuntimeError('远端任务失败,详见上方日志') finally: channel.close() def consume(self, raw): line = raw.decode('utf-8', errors='replace') try: item = json.loads(line) except ValueError: self.log(line) return if not isinstance(item, dict) or 'message' not in item: self.log(line) return self.log(item['message']) if item.get('progress') is not None: self.progress.emit(item['progress']) if item.get('backup'): self.backup = item['backup'] if item.get('result'): self.remote_result = item['result'] def run(self): ok = False sftp = None stage = None script = None dispatched = False try: c = self.config if not c['host'].strip() or not c['username'].strip(): raise ValueError('请填写服务器地址和登录账号') if self.action != 'test': remote.validate(c) self.log('正在连接 {}:{} …'.format(c['host'], c['port'])) self.progress.emit(3) self.client = paramiko.SSHClient() known = data_dir() / 'known_hosts' if known.exists(): self.client.load_host_keys(str(known)) self.client.set_missing_host_key_policy(RememberHost()) self.client.connect(c['host'], port=int(c['port']), username=c['username'], password=c['password'], timeout=15, auth_timeout=20, banner_timeout=20, look_for_keys=False, allow_agent=False) self.client.get_transport().set_keepalive(15) self.log('SSH 连接成功') stdin, stdout, stderr = self.client.exec_command('python3 --version', timeout=15) version = stdout.read().decode().strip() error = stderr.read().decode().strip() if stdout.channel.recv_exit_status() != 0: raise RuntimeError('服务器需要 Python 3.8 或更新版本:' + error) try: version_parts = tuple(int(p) for p in version.split()[-1].split('.')[:2]) except (ValueError, IndexError): raise RuntimeError('无法识别远端 Python 版本:' + version) if version_parts < (3, 6): raise RuntimeError('服务器需要 Python 3.6 或更新版本,当前为 ' + version) self.log(version) if self.action == 'test': self.progress.emit(100) ok = True return sftp = self.client.open_sftp() with tempfile.TemporaryDirectory(prefix='autodeploy-') as local: files = {} size = 0 if self.action == 'deploy': if self.mode != 'frontend': war = Path(c['local_war']) if not war.is_file() or war.suffix.lower() != '.war' or not zipfile.is_zipfile(war): raise ValueError('请选择有效的 WAR 文件') with zipfile.ZipFile(war) as z: if z.testzip(): raise ValueError('WAR 压缩包已损坏') size += sum(entry.file_size for entry in z.infolist()) files['ROOT.war'] = war size += war.stat().st_size if self.mode != 'backend': self.log('正在打包前端文件…') archive = Path(local) / 'dist.tar.gz' package_front(c['local_front'], archive) files['dist.tar.gz'] = archive with tarfile.open(archive) as tf: size += sum(m.size for m in tf.getmembers()) elif self.action == 'cert_deploy': cert = Path(c.get('local_cert', '')) key = Path(c.get('local_key', '')) if not cert.is_file() or not key.is_file(): raise ValueError('请选择有效的证书文件和私钥文件') if cert.stat().st_size > 10 * 1024 * 1024 or key.stat().st_size > 10 * 1024 * 1024: raise ValueError('证书或私钥文件不能超过 10 MB') files['certificate.pem'] = cert files['private.key'] = key size = cert.stat().st_size + key.stat().st_size elif self.action not in ('rollback', 'nginx_restart'): raise ValueError('未知操作') # Bootstrap script goes to SFTP home, before configurable directories exist. script = posixpath.join(sftp.normalize('.'), '.autodeploy-runner-' + uuid.uuid4().hex + '.py') with sftp.file(script, 'w') as f: f.write(Path(remote.__file__).with_name('remote.py').read_bytes()) sftp.chmod(script, 0o600) safe_config = {k: v for k, v in c.items() if k not in ('password', 'local_war', 'local_front', 'local_cert', 'local_key')} payload = dict(config=safe_config, mode=self.mode, size=size, operation=self.action) self.run_remote(script, dict(payload, action='preflight')) if self.action == 'nginx_restart': dispatched = True self.run_remote(script, dict(payload, action=self.action)) ok = True return stage = posixpath.join(c['temp'], 'autodeploy-' + uuid.uuid4().hex) sftp.mkdir(stage, 0o700) total = sum(p.stat().st_size for p in files.values()) or 1 transferred = 0 hashes = {} for name, path in files.items(): self.log('正在上传 ' + name) h = hashlib.sha256() with path.open('rb') as f: for block in iter(lambda: f.read(1024 * 1024), b''): h.update(block) hashes[name] = h.hexdigest() sftp.put(str(path), posixpath.join(stage, name), callback=lambda current, amount: self.progress.emit(15 + int(20 * (transferred + current) / total))) transferred += path.stat().st_size dispatched = True self.run_remote(script, dict(payload, action=self.action, stage=stage, hashes=hashes, backup=self.restore_backup)) ok = True except Exception as exc: self.log(str(exc)) finally: if sftp: if stage and not dispatched: for name in ('ROOT.war', 'dist.tar.gz', 'certificate.pem', 'private.key'): try: sftp.remove(posixpath.join(stage, name)) except Exception: pass try: sftp.rmdir(stage) except Exception: pass # Only remove known uploaded files; never delete a remote running transaction. if script: try: sftp.remove(script) except Exception: pass try: sftp.close() except Exception: pass if self.client: self.client.close() self.completed.emit(ok, '\n'.join(self.logs), self.backup)