Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add molecule to build_npm role & reordered tasks & add linting #252

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python 3.11.4
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
ansible>=6.7
molecule-plugins[docker]
ansible-lint
ansible-lint==6.22.1
passlib
wheel
yamllint
pre-commit
psycopg2_binary
pyyaml
pyyaml
4 changes: 4 additions & 0 deletions roles/build_npm/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
---
# default node
node_release_url: "https://pulmirror.princeton.edu/mirror/node/dist/"
node_version: "20"
desired_node_version: "16"
# defaults file for build_npm
npm_install_mode: "production" # alternate: ci
npm_install_path: "{{ deploy }}"
46 changes: 8 additions & 38 deletions roles/build_npm/meta/main.yml
Original file line number Diff line number Diff line change
@@ -1,47 +1,17 @@
---
galaxy_info:
role_name: build_npm
company: Princeton University Library
description: <Role Description>
author: cdh
description: install javascript dependencies for an app
company: Center for Digital Humanities @ Princeton

# If the issue tracker for your role is not on github, uncomment the
# next line and provide a value
# issue_tracker_url: http://example.com/issue/tracker
license: Apache2

# Choose a valid license ID from https://spdx.org - some suggested licenses:
# - BSD-3-Clause (default)
# - MIT
# - GPL-2.0-or-later
# - GPL-3.0-only
# - Apache-2.0
# - CC-BY-4.0
license: Apache-2.0
min_ansible_version: 2.2

min_ansible_version: 2.10

# If this a Container Enabled role, provide the minimum Ansible Container version.
# min_ansible_container_version:

#
# Provide a list of supported platforms, and for each platform a list of versions.
# If you don't wish to enumerate all versions for a particular platform, use 'all'.
# To view available platforms and versions (or releases), visit:
# https://galaxy.ansible.com/api/v1/platforms/
#
platforms:
- name: Ubuntu
versions:
- 22.04

galaxy_tags:
[]
# List tags for your role here, one per line. A tag is a keyword that describes
# and categorizes the role. Users find roles by searching for tags. Be sure to
# remove the '[]' above, if you add tags to this list.
#
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
# Maximum 20 tags per role.
- jammy
dependencies: []

dependencies:
[]
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
# if you add dependencies to this list.
16 changes: 16 additions & 0 deletions roles/build_npm/molecule/default/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
- name: Converge
hosts: all
# vars:
# - running_on_server: false
# - node_version: "20"
become: true
pre_tasks:
- name: Update cache
ansible.builtin.apt:
update_cache: true
cache_valid_time: 600
tasks:
- name: "Include build_npm"
ansible.builtin.include_role:
name: build_npm
22 changes: 22 additions & 0 deletions roles/build_npm/molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
scenario:
name: default
driver:
name: docker
lint: |
set -e
yamllint .
ansible-lint
platforms:
- name: instance
image: "ghcr.io/pulibrary/pul_containers:jammy_multi"
command: "sleep infinity"
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
privileged: true
pre_build_image: true
provisioner:
name: ansible
log: true
verifier:
name: ansible
20 changes: 20 additions & 0 deletions roles/build_npm/molecule/default/verify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
- name: Verify
hosts: all
gather_facts: false
tasks:
- name: Check ruby package status
ansible.builtin.package:
name: "{{ item }}"
state: present
check_mode: true
register: pkg_status
loop:
- ruby-switch
- ruby2.6-dev
- ruby2.6

- name: Test for ruby packages
ansible.builtin.assert:
that:
- not pkg_status.changed
91 changes: 61 additions & 30 deletions roles/build_npm/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,71 @@
# present in the project root directory (deploy).
###
---
- name: npm configuration tasks
block:
- name: "Install nodejs from --channel={{ node_version }}/stable"
become: true
tags:
- setup
- never
community.general.snap:
name: node
classic: true
channel: "{{ node_version }}/stable"
state: present
register: snap_results
- name: Uninstall any apt installed node
ansible.builtin.apt:
name: ["nodejs"]
state: absent
changed_when: false

- name: Determine architecture
ansible.builtin.set_fact:
architecture: "{{ 'arm64' if ansible_architecture == 'aarch64' else 'x64' }}"

- name: Download node version
ansible.builtin.unarchive:
src: "{{ node_release_url }}/{{ desired_node_version }}/node-{{ desired_node_version }}-linux-{{ architecture }}.tar.gz"
dest: /usr/local
creates: /usr/local/node-{{ desired_node_version }}-linux-{{ architecture }}
remote_src: true
tags:
- setup
- never

- name: Create symbolic links for node and npm
ansible.builtin.file:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
owner: root
group: root
state: link
loop:
- {src: "/usr/local/node-{{ desired_node_version }}-linux-{{ architecture }}/bin/node", dest: "/usr/local/bin/node"}
- {src: "/usr/local/node-{{ desired_node_version }}-linux-{{ architecture }}/lib/node_modules/npm/bin/npm-cli.js", dest: "/usr/local/bin/npm"}
changed_when: false
tags:
- setup
- never

- name: Install corepack
ansible.builtin.shell: npm install -g corepack
changed_when: false
tags:
- setup
- never

- name: Create symbolic link for corepack
ansible.builtin.file:
src: "/usr/local/node-{{ desired_node_version }}-linux-{{ architecture }}/lib/node_modules/corepack/dist/corepack.js"
dest: "/usr/local/bin/corepack"
owner: root
group: root
state: link
tags:
- setup
- never

- name: Enable Corepack
ansible.builtin.shell: corepack enable
changed_when: false
tags:
- setup
- never

# NOTE: upgrading nodejs with ansible snap fails, even though
# the documentation claims it should refresh when then channel changes.
# Manually run a refresh command to ensure version changes take effect, e.g.:
# sudo snap refresh node --channel=18
# NOTE2: could add a node -v check and only refresh on mismatch

- name: Refresh nodejs to ensure version changes take effect
become: true
ansible.builtin.command: "snap refresh node --channel={{ node_version }}/stable"

- name: "install javascript dependencies with npm ({{ npm_install_mode }} install)"
become: true
become_user: "{{ deploy_user }}"
community.general.npm:
path: "{{ npm_install_path }}"
# NOTE: for shxco, omitting dev dependencies means the webpack build fails;
# using production install solved a problem for cdhweb
# shxco prod/dev dependencies should be adjusted so this does not fail
production: "{{ npm_install_mode == 'production' }}"
ci: "{{ npm_install_mode == 'ci' }}"

rescue:
- include_tasks: roles/create_deployment/tasks/fail.yml
# ansible.builtin.rescue:
# - ansible.builtin.include_tasks: roles/create_deployment/tasks/fail.yml