File size: 2,183 Bytes
57e6cdf
 
 
 
 
 
 
 
7032382
 
 
 
57e6cdf
 
 
7032382
57e6cdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
406bc5a
57e6cdf
 
 
 
 
 
 
 
 
 
 
406bc5a
7032382
0d3f8ee
 
 
57e6cdf
 
 
 
 
 
0d3f8ee
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
- name: My first play
  hosts: hostgroup
  remote_user: root
  vars:
    app_name: cursor2api
    app_port: 7860
    deploy_path: /root/workspace/cursor2api

# root     1349786 1344741  0 10:32 pts/0    00:00:00 npm run start --host 0.0.0.0 --port 8888
# root     1349797 1349786  0 10:32 pts/0    00:00:00 sh -c node src/app.js --host 0.0.0.0 --port 8888
# root     1349798 1349797  0 10:32 pts/0    00:00:00 node src/app.js --host 0.0.0.0 --port 8888
# 查找进程id 排除掉"sh -c"以及"grep"的
  tasks:
    - name: 停止特定应用服务
      shell: |
        pid=$(ps -ef | grep "node src/app.js" | grep -v "sh -c" | grep -v grep | grep "{{ app_port }}" | 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 }}
        echo '#!/bin/bash
        cd {{ deploy_path }}
        test -f $HOME/.nvm/nvm.sh && source $HOME/.nvm/nvm.sh
        npm install
        npm run start -- --host 0.0.0.0 --port {{ app_port }}
        ' > {{ deploy_path }}/start_app.sh
        chmod +x {{ deploy_path }}/start_app.sh
        nohup {{ deploy_path }}/start_app.sh > {{ deploy_path }}/nohup.log 2>&1 &
      args:
        executable: /bin/bash

    - name: 等待服务启动
      wait_for:
        port: "{{ app_port }}"
        timeout: 30