Skip to content

Commit 6551142

Browse files
committed
feat(ansible): create deployment plays for manual ops
1 parent d252aa9 commit 6551142

File tree

2 files changed

+251
-0
lines changed

2 files changed

+251
-0
lines changed
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
- name: Deploy Oldeworld Cluster - API
3+
hosts: "{{ variable_host | default('null') }}"
4+
serial: "{{ variable_serial | default(1) }}"
5+
gather_facts: false
6+
vars:
7+
git_source_branch: "{{ variable_branch_name | default('null') }}"
8+
tasks:
9+
- name: Build the server
10+
shell:
11+
cmd: |
12+
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ]
13+
printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
14+
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
15+
pm2 stop all
16+
git status
17+
git clean -f
18+
git fetch --all --prune
19+
git checkout {{ git_source_branch }}
20+
git reset --hard origin/{{ git_source_branch }}
21+
git status
22+
npm i -g pnpm@9
23+
pnpm clean:packages
24+
pnpm clean:server
25+
pnpm install
26+
pnpm prebuild
27+
pnpm build:curriculum
28+
pnpm build:server
29+
pnpm reload:server
30+
pm2 ls
31+
pm2 save
32+
chdir: /home/freecodecamp/freeCodeCamp
33+
executable: /bin/bash
+218
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
---
2+
- name: Deploy Oldeworld Cluster - Client
3+
hosts: localhost
4+
gather_facts: false
5+
vars:
6+
azure_devops_org: 'freecodecamp-org'
7+
azure_devops_project: 'freecodecamp'
8+
branch_name: "{{ variable_branch_name | default('null') }}"
9+
api_version: '7.2-preview'
10+
azure_devops_token: "{{ lookup('env', 'AZURE_DEVOPS_TOKEN') }}"
11+
12+
tasks:
13+
- name: Exit if branch name is not set
14+
fail:
15+
msg: 'Branch name is not set'
16+
when: branch_name == 'null'
17+
- name: Set build definition ID for PRD
18+
set_fact:
19+
build_definition_id: 30
20+
deployment_environment: prd
21+
when: variable_branch_name == 'prod-current'
22+
23+
- name: Set build definition ID for STG
24+
set_fact:
25+
build_definition_id: 29
26+
deployment_environment: stg
27+
when: variable_branch_name == 'prod-staging'
28+
29+
- name: Get latest build ID
30+
uri:
31+
url:
32+
'https://dev.azure.com/{{ azure_devops_org }}/{{ azure_devops_project
33+
}}/_apis/build/latest/{{ build_definition_id }}?branchName={{
34+
branch_name }}&api-version={{ api_version }}'
35+
method: GET
36+
user: '{{ azure_devops_token }}'
37+
password: ''
38+
force_basic_auth: yes
39+
return_content: yes
40+
status_code: 200
41+
register: build_response
42+
43+
- name: Set build ID and build number
44+
set_fact:
45+
build_id: '{{ build_response.json.id }}'
46+
build_number: '{{ build_response.json.buildNumber }}'
47+
languages:
48+
- eng
49+
- chn
50+
- esp
51+
- cnt
52+
- ita
53+
- por
54+
- ukr
55+
- jpn
56+
- ger
57+
- swa
58+
59+
- name: Process artifact information
60+
set_fact:
61+
artifacts:
62+
'{{ artifacts | default([]) + [{ "language": item, "tarball":
63+
build_number + "-" + build_id + "-" + item + ".tar" }] }}'
64+
loop: '{{ languages }}'
65+
66+
- name: Create a host group and add pre-defined groups to it
67+
add_host:
68+
name:
69+
'{{ deployment_environment }}-vm-oldeworld-clt-{{ item[0].language
70+
}}-{{ item[1] }}'
71+
groups:
72+
- client_hosts
73+
artifact_tar: '{{ item[0].tarball }}'
74+
build_id: '{{ build_id }}'
75+
build_number: '{{ build_number }}'
76+
deployment_environment: '{{ deployment_environment }}'
77+
loop: '{{ artifacts | product(["0","1"]) | list }} '
78+
loop_control:
79+
label: '{{ item[0].language }}'
80+
81+
- name: Upload Client Artifacts
82+
hosts: client_hosts
83+
become: true
84+
become_user: freecodecamp
85+
become_method: sudo
86+
gather_facts: false
87+
vars:
88+
temp_dir: '/tmp/{{ build_number }}'
89+
access_key_id: "{{ lookup('env', 'LINODE_OS_ACCESS_KEY_ID') }}"
90+
secret_access_key: "{{ lookup('env', 'LINODE_OS_SECRET_ACCESS_KEY') }}"
91+
92+
tasks:
93+
- name: Debug host information
94+
debug:
95+
msg: 'Uploading {{ artifact_tar }} to {{ inventory_hostname }}'
96+
97+
- name: Install Rclone
98+
ansible.builtin.shell:
99+
rclone version || curl https://rclone.org/install.sh | sudo bash
100+
become: true
101+
become_method: sudo
102+
103+
- name: Create Rclone config for Linode Object Storage
104+
ansible.builtin.command:
105+
rclone config create crl s3 access_key_id={{ access_key_id }}
106+
secret_access_key={{ secret_access_key }}
107+
endpoint=us-east-1.linodeobjects.com provider=Linode
108+
no_log: true
109+
register: create_rclone_config
110+
changed_when: true
111+
112+
- name: Remove directory for artifacts if it exists
113+
ansible.builtin.file:
114+
path: '{{ temp_dir }}'
115+
state: absent
116+
changed_when: true
117+
118+
- name: Create directory for artifacts
119+
ansible.builtin.file:
120+
path: '{{ temp_dir }}'
121+
state: directory
122+
mode: '0755'
123+
owner: freecodecamp
124+
register: create_temp_dir
125+
changed_when: true
126+
127+
- name: Download artifact with rclone if checksum differs
128+
shell: >
129+
rclone copy crl:/{{ deployment_environment }}-releases/{{ artifact_tar
130+
}} {{temp_dir }} --checksum
131+
register: rclone_copy
132+
changed_when: rclone_copy.rc == 0
133+
134+
- name: Create unpacked directory
135+
ansible.builtin.file:
136+
path: '{{ temp_dir }}/unpacked'
137+
state: directory
138+
mode: '0755'
139+
owner: freecodecamp
140+
register: create_unpacked_dir
141+
changed_when: false
142+
143+
- name: Extract artifact
144+
ansible.builtin.unarchive:
145+
src: '{{ temp_dir }}/{{ artifact_tar }}'
146+
dest: '{{ temp_dir }}/unpacked'
147+
remote_src: yes
148+
mode: '0755'
149+
owner: freecodecamp
150+
register: extract_artifact
151+
changed_when: extract_artifact.changed
152+
153+
- name: Create the final destination directory
154+
ansible.builtin.file:
155+
path:
156+
'/home/freecodecamp/client/releases/{{ build_number }}-{{ build_id }}'
157+
state: directory
158+
mode: '0755'
159+
owner: freecodecamp
160+
register: create_final_dir
161+
changed_when: true
162+
163+
- name: Copy artifacts to the final destination
164+
ansible.builtin.shell: |
165+
shopt -s dotglob nullglob
166+
mv {{ temp_dir }}/unpacked/client/public/* '/home/freecodecamp/client/releases/{{ build_number }}-{{ build_id }}/'
167+
args:
168+
executable: /bin/bash
169+
register: copy_artifacts
170+
changed_when: copy_artifacts.rc == 0
171+
172+
- name: Check permissions
173+
shell:
174+
cmd: |
175+
ls -hal '/home/freecodecamp/client/releases/{{ build_number }}-{{ build_id }}' | grep 'index.html'
176+
register: check_permissions
177+
178+
- name: Print permissions
179+
debug:
180+
var: check_permissions.stdout
181+
when: check_permissions.rc == 0
182+
183+
- name: Cleanup temporary directory
184+
ansible.builtin.file:
185+
path: '{{ temp_dir }}'
186+
state: absent
187+
changed_when: true
188+
189+
- name: Restart Client
190+
hosts: client_hosts
191+
serial: 1
192+
become: true
193+
become_user: freecodecamp
194+
become_method: sudo
195+
gather_facts: false
196+
197+
tasks:
198+
- name: Restart Client
199+
shell:
200+
cmd: |
201+
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ]
202+
printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
203+
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
204+
export CLIENT_BINARIES="{{ build_number }}-{{build_id}}"
205+
npm install -g serve@13
206+
rm client-start-primary.sh
207+
echo "serve -c ../../serve.json releases/$CLIENT_BINARIES -p 50505" >> client-start-primary.sh
208+
chmod +x client-start-primary.sh
209+
pm2 delete client-primary
210+
pm2 start ./client-start-primary.sh --name client-primary
211+
rm client-start-secondary.sh
212+
echo "serve -c ../../serve.json releases/$CLIENT_BINARIES -p 52525" >> client-start-secondary.sh
213+
chmod +x client-start-secondary.sh
214+
pm2 delete client-secondary
215+
pm2 start ./client-start-secondary.sh --name client-secondary
216+
pm2 save
217+
chdir: /home/freecodecamp/client
218+
executable: /bin/bash

0 commit comments

Comments
 (0)