Spaces:
Sleeping
Sleeping
- name: My first play | |
hosts: hostgroup | |
remote_user: root | |
vars: | |
app_name: scraper-proxy | |
app_port: 7860 | |
deploy_path: /root/workspace/scraper-proxy | |
tasks: | |
- name: 停止特定应用服务 | |
shell: | | |
pid=$(ps -ef | grep "uvicorn main:app" | grep "{{ app_port }}" | grep -v grep | awk '{print $2}') | |
if [ -n "$pid" ]; then | |
echo "Stopping {{ app_name }} process with PID: $pid" | |
kill -15 $pid | |
sleep 3 | |
if ps -p $pid > /dev/null 2>&1; then | |
echo "Force killing process with PID: $pid" | |
kill -9 $pid | |
fi | |
else | |
echo "No running {{ app_name }} process found" | |
fi | |
args: | |
executable: /bin/bash | |
ignore_errors: yes | |
- name: 确认服务已停止 | |
wait_for: | |
port: "{{ app_port }}" | |
state: stopped | |
timeout: 30 | |
- name: 复制到远程服务器 | |
copy: | |
src: archive/release.tar.gz | |
dest: /root/deploy/{{ app_name }}.tar.gz | |
- name: 解压文件 | |
shell: | | |
rm -rf {{ deploy_path }} | |
mkdir -p {{ deploy_path }} | |
tar -zxvf /root/deploy/{{ app_name }}.tar.gz -C {{ deploy_path }} | |
args: | |
executable: /bin/bash | |
- name: 构建并运行 | |
shell: | | |
cd {{ deploy_path }} | |
pip install --no-cache-dir -r requirements.txt | |
echo '#!/bin/bash | |
cd {{ deploy_path }} | |
exec uvicorn main:app --host 0.0.0.0 --port {{ app_port }} | |
' > {{ deploy_path }}/start.sh | |
chmod +x {{ deploy_path }}/start.sh | |
nohup {{ deploy_path }}/start.sh > {{ deploy_path }}/nohup.log 2>&1 & | |
args: | |
executable: /bin/bash | |
- name: 等待服务启动 | |
wait_for: | |
port: "{{ app_port }}" | |
timeout: 30 | |