Skip to content

Commit

Permalink
fix: managed node installation fixes contentful backup (#467)
Browse files Browse the repository at this point in the history
* fix: managed node installation fixes contentful backup

* chore: removed global yarn install

In the previous commit, we removed the global Nodejs install, replaced by a
global nvm install. Yarn should also be installed locally and not globally.

* chore: referenced node removal pr
  • Loading branch information
SilasPeters authored Nov 15, 2024
1 parent 08aa7c1 commit 779a5d5
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 59 deletions.
6 changes: 6 additions & 0 deletions ansible/group_vars/all/vars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ secret_deploy_key: "{{ vault_secret_deploy_key }}"
# The API key for our Mailgun account.
# Change? Refresh API key at https://app.mailgun.com/app/account/security
secret_mailgun_token: "{{ vault_secret_mailgun_token }}"

# The place where https://github.com/nvm-sh/nvm will be installed, to be globally used
nvm:
directory: "/usr/local/bin/.nvm"
script: "/usr/local/bin/.nvm/nvm.sh"
version: "v0.40.0" # Derived from the git tag
5 changes: 3 additions & 2 deletions ansible/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
tags: "docker"
- role: "databases"
tags: "databases"
- role: "nvm"
tags: "nvm"
- role: "backups"
tags: "backups"
- role: "nginx"
tags: "nginx"
- role: "node"
tags: "node"
- role: "certbot"
tags: "certbot"
- role: "redis"
Expand Down Expand Up @@ -90,3 +90,4 @@
#
# - execut: #475
# - php: #474
# - node: #467
2 changes: 2 additions & 0 deletions ansible/roles/backups/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
shell: "/usr/sbin/nologin"
home: "/home/backup"
system: true
groups: "nvm"
append: true

- name: "install awscli"
ansible.builtin.apt:
Expand Down
13 changes: 8 additions & 5 deletions ansible/roles/backups/templates/backup-to-s3.sh.j2
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,14 @@ case "${SOURCE}" in
FILE_NAME="${FILE_TITLE}.tar.gz"

sudo -u backup mkdir -p /tmp/contentful-export
sudo -u backup -H npx contentful-cli space export \
--management-token {{ secret_contentful_export.token }} \
--space-id {{ secret_contentful_export.space_id }} \
--download-assets \
--export-dir /tmp/contentful-export
sudo -Hu backup bash -c `
`'source {{ nvm.script }} && nvm install {{ backups_node_version }} &&'`
`' nvm exec {{ backups_node_version }} npx contentful-cli space export'`
`' --management-token {{ secret_contentful_export.token }}'`
`' --space-id {{ secret_contentful_export.space_id }}'`
`' --download-assets'`
`' --export-dir /tmp/contentful-export'

upload_backup_to_s3 < <(tar \
-c -f - -C /tmp contentful-export \
| gzip -9)
Expand Down
3 changes: 3 additions & 0 deletions ansible/roles/backups/vars/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---

backups_node_version: "22"
25 changes: 0 additions & 25 deletions ansible/roles/node/tasks/main.yml

This file was deleted.

29 changes: 29 additions & 0 deletions ansible/roles/nvm/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
- name: "Install nvm"
ansible.builtin.git:
repo: "https://github.com/nvm-sh/nvm.git"
version: "{{ nvm.version }}"
dest: "{{ nvm.directory }}"
recursive: false # cloning submodules fails, but they are purely for testing
diff: false

- name: "Test nvm install"
ansible.builtin.shell: "source {{ nvm.script }} && command -v nvm"
register: "nvm_command"
args:
executable: "/bin/bash"
changed_when: false

- name: "Assert that nvm is installed correctly"
ansible.builtin.assert:
that: "nvm_command.stdout == 'nvm'"

- name: "Create nvm group"
ansible.builtin.group:
name: "nvm"

- name: "Allow nvm group to manage nodejs installs"
ansible.builtin.file:
path: "{{ nvm.directory }}"
group: "nvm"
mode: "0774"
1 change: 0 additions & 1 deletion ansible/roles/packages/templates/50unattended-upgrades.j2
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Unattended-Upgrade::Allowed-Origins {
"${distro_id} stable";
"${distro_id} ${distro_codename}-updates";
"LP-PPA-certbot-certbot:${distro_codename}";
"yarn:stable";
{% endif %}
};

Expand Down
26 changes: 2 additions & 24 deletions ansible/roles/radio/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,11 @@
---
- name: "clone nvm"
listen: "radio repo updated"
become_user: "radio"
become: true
ansible.builtin.git:
repo: "https://github.com/nvm-sh/nvm.git"
dest: "/var/www/radio/.nvm"
recursive: false # cloning submodules fails, but they are purely for testing
diff: false

- name: "checkout latest nvm version" # based on nvm manual installation guide
listen: "radio repo updated"
become_user: "radio"
become: true
ansible.builtin.shell:
cmd: git checkout `git describe --abbrev=0 --tags --match "v[0-9]*" $(git rev-list --tags --max-count=1)`
chdir: "/var/www/radio/.nvm"
executable: "/bin/bash"
tags:
- skip_ansible_lint
# Reason: linter wants us to use the git module, but that can't do this
# complicated stuff

- name: "install node version"
listen: "radio repo updated"
become_user: "radio"
become: true
ansible.builtin.shell: |
source /var/www/radio/.nvm/nvm.sh
source {{ nvm.script }}
nvm install $(cat /var/www/radio/radio/.nvmrc)
args:
chdir: "/var/www/radio/radio"
Expand All @@ -39,7 +17,7 @@
become_user: "radio"
become: true
ansible.builtin.shell: |
source /var/www/radio/.nvm/nvm.sh
source {{ nvm.script }}
nvm use
npm rebuild
args:
Expand Down
6 changes: 4 additions & 2 deletions ansible/roles/radio/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
shell: "/usr/sbin/nologin"
state: "present"
system: true
groups: "nvm"
append: true

- name: "copy nginx configuration"
ansible.builtin.template:
Expand Down Expand Up @@ -53,7 +55,7 @@

- name: "run npm install"
ansible.builtin.shell: |
source /var/www/radio/.nvm/nvm.sh
source {{ nvm.script }}
nvm use
npm install
args:
Expand All @@ -64,7 +66,7 @@
block:
- name: "build website"
ansible.builtin.shell: |
source /var/www/radio/.nvm/nvm.sh
source {{ nvm.script }}
nvm use
npm run build
args:
Expand Down

0 comments on commit 779a5d5

Please sign in to comment.