Skip to content

Commit

Permalink
feat(ansible): create roles for azagent, nodejs,
Browse files Browse the repository at this point in the history
  • Loading branch information
raisedadead committed Jul 16, 2023
1 parent 77cefc9 commit bb8951e
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 5 deletions.
11 changes: 6 additions & 5 deletions ansible/inventory/linode.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ groups:
publish_leaders: "'publish_leader' in (tags|list)"
publish_workers: "'publish_worker' in (tags|list)"

# # OldeWorld Cluster
# oldeworld_all: "'oldeworld' in (tags|list)"
# oldeworld_pxy: "'oldeworld_pxy' in (tags|list)"

# Oldeworld Cluster -- STG
stg_oldeworld_all: "'stg' in (tags|list) and 'oldeworld' in (tags|list)"
stg_oldeworld_pxy: "'stg' in (tags|list) and 'oldeworld_pxy' in (tags|list)"
stg_oldeworld_api: "'stg_oldeworld_api' in (tags|list)"
stg_oldeworld_clt: "'stg_oldeworld_clt' in (tags|list)"
stg_oldeworld_pxy: "'stg_oldeworld_pxy' in (tags|list)"

#
# NOTE: More groups are automatically created by the plugin
#

compose:
ansible_ssh_host: ipv4[0]
61 changes: 61 additions & 0 deletions ansible/play-oldeworld--client-0-initialize.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
- name: Initialize Olde World Cluster - Clients
hosts: '{{ variable_host | default("null") }}'
become: true
become_user: freecodecamp
gather_facts: false
vars:
client_groups:
['chn', 'cnt', 'eng', 'esp', 'ger', 'ita', 'jpn', 'por', 'ukr']
client_hosts: ['1', '2']
client_env: '{{ variable_client_env | default("stg") }}'
agent_version: '{{ variable_agent_version | default("3.220.5") }}'
tasks:
# - name: Create deployment groups from list of items
# add_host:
# groups: '{{ client_env }}_oldeworld_clt{{ item[0] }}'
# name: '{{ client_env }}-vm-oldeworld-clt{{ item[0] }}-{{ item[1] }}'
# with_nested:
# - '{{ client_groups }}'
# - '{{ client_hosts }}'

# - name: Print deployment groups
# debug:
# msg: '{{ groups[client_env + "_oldeworld_clt" + item] }}'
# with_items: '{{ client_groups }}'
- name: Install or Update Node.js
include_role:
name: nodejs

- name: Uninstall and remove azagent
include_role:
name: azagent
tasks_from: delete-azagent.yml
vars:
agent_deploymentgroup_name: '{{ client_env }}-dp-clt{{ item[0] }}'
agent_machine_name:
'{{ client_env }}-vm-oldeworld-clt-{{ item[0] }}-{{ item[1] }}'
agent_project_name: 'freeCodeCamp'
agent_pat_token: '{{ variable_pat_token }}'
loop: '{{ client_groups | product(client_hosts) | list }}'
when:
inventory_hostname == client_env + "-vm-oldeworld-clt" + item[0] + "-" +
item[1]

- name: Install azagent
include_role:
name: azagent
tasks_from: install-azagent.yml
vars:
agent_deploymentgroup_name: '{{ client_env }}-dp-clt{{ item[0] }}'
agent_machine_name:
'{{ client_env }}-vm-oldeworld-clt-{{ item[0] }}-{{ item[1] }}'
agent_project_name: 'freeCodeCamp'
agent_pat_token: '{{ variable_pat_token }}'
agent_download_url:
'https://vstsagentpackage.azureedge.net/agent/{{ agent_version
}}/vsts-agent-linux-x64-{{ agent_version }}.tar.gz'
loop: '{{ client_groups | product(client_hosts) | list }}'
when:
inventory_hostname == client_env + "-vm-oldeworld-clt" + item[0] + "-" +
item[1]
15 changes: 15 additions & 0 deletions ansible/play-oldeworld--client-1-updateazagent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
- name: Initialize Olde World Cluster - Clients
hosts: '{{ variable_host | default("null") }}'
become: true
become_user: freecodecamp
gather_facts: false
tasks:
- name: Install or Update Node.js
include_role:
name: nodejs

- name: Update azagent environment
include_role:
name: azagent
tasks_from: update-azagent-env.yml
5 changes: 5 additions & 0 deletions ansible/roles/azagent/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
azagent_dir: "/home/freecodecamp/azagent"
vstsagent_tar_file: "vstsagent.tar.gz"
azdevops_url: "https://dev.azure.com/freeCodeCamp-org/"
work: "_work"
64 changes: 64 additions & 0 deletions ansible/roles/azagent/tasks/delete-azagent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
- name: Check the installation directory
stat:
path: "{{ azagent_dir }}"
register: azagent_dir_stat

- name: Stop Azure DevOps agent service
block:
- name: Check azagent status
shell:
executable: /bin/bash
cmd: |
sudo ./svc.sh status
chdir: "{{ azagent_dir }}"
register: azagent_status_running
ignore_errors: True

- name: Stop azagent
shell:
executable: /bin/bash
cmd: |
sudo ./svc.sh stop
chdir: "{{ azagent_dir }}"
when: azagent_status_running.stdout.find('running') != -1 and azagent_status_running.stderr.find('command not found') == -1
ignore_errors: True

- name: Check azagent status
shell:
executable: /bin/bash
cmd: |
sudo ./svc.sh status
chdir: "{{ azagent_dir }}"
register: azagent_status_installed
changed_when: False
ignore_errors: True

- name: Uninstall azagent
shell:
executable: /bin/bash
cmd: |
sudo ./svc.sh uninstall
chdir: "{{ azagent_dir }}"
when: (
azagent_status_installed.stdout.find('not installed') != -1 or
azagent_status_installed.stdout.find('dead') != -1
) and azagent_status_installed.stderr.find('command not found') == -1
ignore_errors: True

- name: Remove the agent from the pool
shell:
executable: /bin/bash
cmd: |
./config.sh remove --unattended --auth pat --token '{{ agent_pat_token }}'
chdir: "{{ azagent_dir }}"
register: azagent_remove
when: azagent_dir_stat.stat.exists

- name: Delete azagent directory
become: true
become_user: root
become_method: sudo
file:
path: "{{ azagent_dir }}"
state: absent
44 changes: 44 additions & 0 deletions ansible/roles/azagent/tasks/install-azagent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
- name: Create directory for Azure DevOps agent
file:
path: "{{ azagent_dir }}"
state: directory

- name: Download Azure DevOps agent
get_url:
url: "{{ agent_download_url }}"
dest: "{{ azagent_dir }}/{{ vstsagent_tar_file }}"
mode: '0755'

- name: Extract Azure DevOps agent tar file
unarchive:
src: "{{ azagent_dir }}/{{ vstsagent_tar_file }}"
dest: "{{ azagent_dir }}"
remote_src: yes

- name: Configure Azure DevOps agent
shell:
executable: /bin/bash
chdir: "{{ azagent_dir }}"
cmd: |
./config.sh \
--unattended \
--acceptteeeula \
--deploymentgroup \
--replace \
--runasservice \
--deploymentgroupname '{{ agent_deploymentgroup_name }}' \
--agent '{{ agent_machine_name }}' \
--url '{{ azdevops_url }}' \
--work '{{ work }}' \
--projectname '{{ agent_project_name }}' \
--auth pat \
--token '{{ agent_pat_token }}'
- name: Install & Start Azure DevOps Pipeline Agent
shell:
cmd: |
sudo ./svc.sh install
sudo ./svc.sh start
executable: /bin/bash
chdir: "{{ azagent_dir }}"
19 changes: 19 additions & 0 deletions ansible/roles/azagent/tasks/update-agent-env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
- name: Stop Azure DevOps Pipeline Agent
shell:
cmd: |
sudo ./svc.sh stop
executable: /bin/bash
chdir: '{{ azagent_dir }}'

- name: Update Env & Start Azure DevOps Pipeline Agent
shell:
cmd: |
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ]
printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
nvm version && echo PATH=$PATH && echo NVM_BIN=$NVM_BIN
./env.sh
sudo ./svc.sh start
executable: /bin/bash
chdir: '{{ azagent_dir }}'
22 changes: 22 additions & 0 deletions ansible/roles/nodejs/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
- name: Update nvm to the latest version
shell: |
curl --silent "https://api.github.com/repos/nvm-sh/nvm/releases/latest" |
grep '"tag_name":' |
sed -E 's/.*"([^"]+)".*/\1/' |
xargs -I % curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/%/install.sh |
bash
args:
executable: /bin/bash

- name: Update Node.js to the latest version
shell: |
source ~/.nvm/nvm.sh
if [[ $(nvm version) == 'none' ]] ; then
nvm install --lts --latest-npm --default
else
nvm install --lts --latest-npm --default --reinstall-packages-from=node
fi
node --version | xargs npx -y cowsay 'hello from node'
args:
executable: /bin/bash

0 comments on commit bb8951e

Please sign in to comment.