Skip to content

Commit 6dc90fc

Browse files
committed
feat: add rollback scripts
1 parent be50419 commit 6dc90fc

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
- name: List available releases
3+
hosts: "{{ variable_host | default('null') }}"
4+
serial: '{{ variable_serial | default(1) }}'
5+
gather_facts: false
6+
tasks:
7+
- name: List client releases
8+
ansible.builtin.include_tasks:
9+
file: tasks/list-client-releases.yml
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
- name: Rollback
3+
hosts: "{{ variable_host | default('null') }}"
4+
serial: '{{ variable_serial | default(1) }}'
5+
gather_facts: false
6+
vars:
7+
dry_run: '{{ variable_dry_run | default(false) }}'
8+
client_binaries_id: '{{ variable_client_binaries_id }}'
9+
tasks:
10+
- name: Check if target release directory exists
11+
ansible.builtin.stat:
12+
path: "/home/freecodecamp/client/releases/{{ client_binaries_id }}"
13+
register: release_directory
14+
15+
- name: Fail if release directory does not exist
16+
ansible.builtin.fail:
17+
msg: "Release directory {{ client_binaries_id }} does not exist in /home/freecodecamp/client/releases/"
18+
when: not release_directory.stat.exists
19+
20+
- name: Update release ID and restart client services (rolling)
21+
ansible.builtin.include_tasks:
22+
file: tasks/rollback-client-service.yml
23+
loop:
24+
- primary
25+
- secondary
26+
loop_control:
27+
loop_var: service_type
28+
29+
- name: Save PM2 process list
30+
ansible.builtin.shell: |
31+
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
32+
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
33+
pm2 save
34+
args:
35+
executable: /bin/bash
36+
chdir: /home/freecodecamp/client
37+
when: not dry_run | bool
38+
39+
- name: Display PM2 process status
40+
ansible.builtin.shell: |
41+
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
42+
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
43+
pm2 ls
44+
args:
45+
executable: /bin/bash
46+
chdir: /home/freecodecamp/client
47+
register: pm2_status
48+
49+
- name: Show PM2 status
50+
ansible.builtin.debug:
51+
msg: "{{ pm2_status.stdout_lines }}"
52+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
- name: Test Node Environment - Print Node/NPM/PM2 Versions
3+
hosts: "{{ variable_host | default('null') }}"
4+
serial: '{{ variable_serial | default(1) }}'
5+
gather_facts: false
6+
become: true
7+
become_user: freecodecamp
8+
tasks:
9+
- name: Print Node, NPM, and PM2 versions
10+
ansible.builtin.shell: |
11+
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
12+
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
13+
echo "Node version:"
14+
node -v
15+
echo ""
16+
echo "NPM version:"
17+
npm -v
18+
echo ""
19+
echo "PM2 version:"
20+
pm2 -v
21+
echo ""
22+
echo "PM2 location:"
23+
which pm2
24+
args:
25+
executable: /bin/bash
26+
chdir: /home/freecodecamp
27+
register: version_output
28+
29+
- name: Display version information
30+
ansible.builtin.debug:
31+
msg: "{{ version_output.stdout_lines }}"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
- name: Get all release directories
3+
ansible.builtin.find:
4+
paths: /home/freecodecamp/client/releases
5+
recurse: false
6+
file_type: directory
7+
register: all_releases
8+
9+
- name: Sort releases by modification time
10+
ansible.builtin.set_fact:
11+
sorted_releases:
12+
"{{ all_releases.files | sort(attribute='mtime', reverse=true) }}"
13+
14+
- name: Display available releases (newest first)
15+
ansible.builtin.debug:
16+
msg: "{{ my_idx + 1 }}. {{ item.path | basename }}"
17+
loop: "{{ sorted_releases }}"
18+
loop_control:
19+
index_var: my_idx
20+
label: "{{ item.path | basename }}"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
- name: "Update release ID in client-start-{{ service_type }}.sh"
3+
ansible.builtin.replace:
4+
path: "/home/freecodecamp/client/client-start-{{ service_type }}.sh"
5+
regexp: 'prd-release-\d{8}-\d+'
6+
replace: "{{ client_binaries_id }}"
7+
backup: yes
8+
when: not dry_run | bool
9+
10+
- name: "Restart PM2 client-{{ service_type }} service"
11+
ansible.builtin.shell: |
12+
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
13+
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
14+
pm2 restart client-{{ service_type }}
15+
args:
16+
executable: /bin/bash
17+
chdir: /home/freecodecamp/client
18+
when: not dry_run | bool
19+
20+
- name: "Wait for client-{{ service_type }} to stabilize"
21+
ansible.builtin.pause:
22+
seconds: 10
23+
when: not dry_run | bool

0 commit comments

Comments
 (0)