文件
yjyf/scripts/deploy_server.py

123 行
4.0 KiB
Python

import os
import tarfile
import paramiko
import time
server_dir = r'D:\Projects\phpstudy_pro\WWW\yjyf\server'
tar_path = r'D:\Projects\phpstudy_pro\WWW\yjyf\server_deploy.tar.gz'
print('Step 1: Packaging local server files...')
exclude_prefixes = [
os.path.join(server_dir, 'runtime'),
os.path.join(server_dir, '.git'),
os.path.join(server_dir, '.idea'),
os.path.join(server_dir, '.vscode')
]
exclude_files = [
'.env',
'composer.phar'
]
with tarfile.open(tar_path, 'w:gz') as tar:
for root, dirs, files in os.walk(server_dir):
# 排除指定目录
if any(root.startswith(p) for p in exclude_prefixes):
continue
for file in files:
if file in exclude_files or file.endswith('.log') or file.endswith('.tmp'):
continue
full_path = os.path.join(root, file)
rel_path = os.path.relpath(full_path, server_dir)
tar.add(full_path, arcname=rel_path)
print(f'Package created: {tar_path}, size: {os.path.getsize(tar_path)/1024/1024:.2f} MB')
print('Step 2: Connecting to remote server...')
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('8.137.181.63', port=22, username='root', password='liyanxia@6688', timeout=20)
def run(cmd):
stdin, stdout, stderr = ssh.exec_command(cmd)
out = stdout.read().decode('utf-8', errors='ignore')
err = stderr.read().decode('utf-8', errors='ignore')
return out, err
print('Step 3: Backup current remote yjyf site...')
run('mkdir -p /www/backup')
out_b, err_b = run('tar -zcf /www/backup/yjyf_site_backup_before_deploy.tar.gz -C /www/wwwroot yjyf.gzyakj.com.cn')
out_b_check, _ = run('ls -lh /www/backup/yjyf_site_backup_before_deploy.tar.gz')
print('Remote site backup:', out_b_check.strip())
print('Step 4: Upload package to remote /tmp...')
sftp = ssh.open_sftp()
sftp.put(tar_path, '/tmp/server_deploy.tar.gz')
sftp.close()
out_up, _ = run('ls -lh /tmp/server_deploy.tar.gz')
print('Uploaded:', out_up.strip())
print('Step 5: Extract package to /www/wwwroot/yjyf.gzyakj.com.cn...')
out_ex, err_ex = run('tar -zxvf /tmp/server_deploy.tar.gz -C /www/wwwroot/yjyf.gzyakj.com.cn')
print('Extracted files count:', len(out_ex.splitlines()))
print('Step 6: Ensure .env for remote environment...')
remote_env_content = """[app]
debug = false
trace = false
[database]
hostname = 127.0.0.1
database = yjyf
username = yjyf
password = yjyf
hostport = 3306
prefix = fa_
[wechat]
appid = wx40ca4b02d4d1cb58
secret = f661146ce627e2c57770b7d80164dd65
mch_id = 1119105317
key = ffyqcyyszhdhi398672363efnwvikxlv
key_v3 = ffyqcyyszhdhi398672363efnwvikxlv
public_key_id = PUB_KEY_ID_0111191053172026102200381630000600
notify_domain = https://yjyf.gzyakj.com.cn
transfer_scene_id = 1005
transfer_single_limit = 200.00
transfer_daily_user_limit = 200.00
[tianditu]
key = 55aee666be14cd0100874d6faf3c963c
[sms]
; mock=1 时使用固定验证码(联调用);接入真实短信后置 0
mock = 1
mock_code = 123456
"""
sftp = ssh.open_sftp()
with sftp.file('/www/wwwroot/yjyf.gzyakj.com.cn/.env', 'w') as f:
f.write(remote_env_content)
sftp.close()
print('.env written.')
print('Step 7: Ensure admin.php and s.php exist...')
run('cp -f /www/wwwroot/yjyf.gzyakj.com.cn/public/s.php /www/wwwroot/yjyf.gzyakj.com.cn/public/admin.php')
print('Step 8: Set permissions and clean runtime cache...')
run('mkdir -p /www/wwwroot/yjyf.gzyakj.com.cn/runtime/cache /www/wwwroot/yjyf.gzyakj.com.cn/runtime/temp /www/wwwroot/yjyf.gzyakj.com.cn/runtime/log')
run('rm -rf /www/wwwroot/yjyf.gzyakj.com.cn/runtime/cache/* /www/wwwroot/yjyf.gzyakj.com.cn/runtime/temp/*')
run('chown -R www:www /www/wwwroot/yjyf.gzyakj.com.cn')
run('chmod -R 755 /www/wwwroot/yjyf.gzyakj.com.cn')
print('Step 9: Reload PHP-FPM...')
out_p, _ = run('/etc/init.d/php-fpm-85 reload')
print('PHP-FPM reload result:', out_p.strip())
print('Step 10: Clean up temporary package...')
run('rm -f /tmp/server_deploy.tar.gz')
if os.path.exists(tar_path):
os.remove(tar_path)
ssh.close()
print('Deploy server completed!')