diff --git a/handlers/main.yml b/handlers/main.yml deleted file mode 100755 index 67dca56..0000000 --- a/handlers/main.yml +++ /dev/null @@ -1,8 +0,0 @@ ---- -# handlers file for node -- name: Execute the nvm installation script - command: "{{ nvm_dir }}/install_nvm_{{ nvm_node_version }}.script" - -- name: Install new global packages - shell: ". /root/.bashrc && {{ nvm_dir }}/versions/node/v{{ nvm_node_version }}/bin/npm install --global {{ item }}" - loop: "{{ nvm_install_globally }}" diff --git a/meta/main.yml b/meta/main.yml index d8ac482..606f49c 100755 --- a/meta/main.yml +++ b/meta/main.yml @@ -13,6 +13,7 @@ galaxy_info: - trusty - xenial - bionic + - focal - name: Debian versions: - stretch diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index 00226d7..43f5085 100755 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -6,15 +6,17 @@ driver: lint: name: yamllint platforms: - - name: ansible-role-node-test-instance-18-08 + - name: ansible-nvm-node-test-20-04 + image: ubuntu/focal/amd64 + - name: ansible-nvm-node-test-18-08 image: ubuntu/bionic/amd64 - - name: ansible-role-node-test-instance-16-04 + - name: ansible-nvm-node-test-16-04 image: ubuntu/xenial/amd64 - - name: ansible-role-node-test-instance-14-04 + - name: ansible-nvm-node-test-14-04 image: ubuntu/trusty/amd64 - - name: ansible-role-node-test-instance-buster + - name: ansible-nvm-node-test-buster image: debian/buster/amd64 - - name: ansible-role-node-test-instance-stretch + - name: ansible-nvm-node-test-stretch image: debian/stretch/amd64 provisioner: name: ansible diff --git a/molecule/default/playbook.yml b/molecule/default/playbook.yml index de75b18..fc4662a 100644 --- a/molecule/default/playbook.yml +++ b/molecule/default/playbook.yml @@ -16,7 +16,19 @@ yum: name=sudo state=present when: ansible_os_family == "RedHat" + - name: Crontab for which-ing gulp + cron: + name: "which gulp" + minute: "*" + job: "which gulp > /tmp/which_gulp" + + - name: Crontab for which-ing lighthouse + cron: + name: "which lighthouse" + minute: "*" + job: "which lighthouse > /tmp/which_lighthouse" + roles: - role: ansible-nvm-node - nvm_node_version: "8.11.3" - nvm_install_globally: ['gulp'] + nvm_node_version: "10.16.3" + nvm_install_globally: ['gulp', 'lighthouse'] diff --git a/molecule/default/tests/test_default.py b/molecule/default/tests/test_default.py index a3da033..41c5ee9 100755 --- a/molecule/default/tests/test_default.py +++ b/molecule/default/tests/test_default.py @@ -1,8 +1,16 @@ import os +import time import testinfra.utils.ansible_runner +# let the which-checking crontab run +# anyone know better way of checking what cron thinks ? Please, do let me know! +time.sleep(61) + +# man handle the damn 20.04's sudo bug... +FOCAL_BUG_STR = 'sudo: setrlimit(RLIMIT_CORE): Operation not permitted\n' + testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') @@ -10,33 +18,49 @@ def test_node_version_ok(host): cmd = host.run("node -v") assert cmd.stderr == '' - assert cmd.stdout.rstrip() == 'v8.11.3' + assert cmd.stdout.rstrip() == 'v10.16.3' -def test_global_gulp_version_ok(host): +def test_global_libraries(host): cmd = host.run("gulp -v") assert cmd.stderr == '' assert 'CLI version' in cmd.stdout - cmd = host.run("sudo -u www-data which gulp") + cmd = host.run("lighthouse --version") assert cmd.stderr == '' - assert cmd.stdout.rstrip() == '/usr/bin/gulp' + assert cmd.stdout != '' + + with host.sudo("nvm_tester"): + cmd = host.run("which lighthouse") + assert cmd.stderr == '' or cmd.stderr == FOCAL_BUG_STR + assert cmd.stdout.rstrip() == '/usr/bin/lighthouse' + + cmd = host.run("which gulp") + assert cmd.stderr == '' or cmd.stderr == FOCAL_BUG_STR + assert cmd.stdout.rstrip() == '/usr/bin/gulp' + + +def test_cron_environment_for_global_libs(host): + + # make sure cron sees global libraries in correct locations + assert host.run("cat /tmp/which_gulp").stdout.rstrip() == '/usr/bin/gulp' + assert host.run("cat /tmp/which_lighthouse").stdout.rstrip() == '/usr/bin/lighthouse' + + +def test_user_node_version_ok(host): + with host.sudo("nvm_tester"): + cmd = host.run("node -v") + assert cmd.stderr == '' or cmd.stderr == FOCAL_BUG_STR + assert cmd.stdout.rstrip() == 'v10.16.3' def test_npm_ok(host): cmd = host.run("npm -v") - assert cmd.stdout.rstrip() == '5.6.0' + assert cmd.stdout.rstrip() == '6.9.0' def test_user_npm_ok(host): with host.sudo("nvm_tester"): host.check_output("whoami") cmd = host.run("npm -v") - assert cmd.stdout.rstrip() == '5.6.0' - - -def test_user_node_version_ok(host): - with host.sudo("nvm_tester"): - cmd = host.run("node -v") - assert cmd.stderr == '' - assert cmd.stdout.rstrip() == 'v8.11.3' + assert cmd.stdout.rstrip() == '6.9.0' diff --git a/tasks/main.yml b/tasks/main.yml index edf4078..5980e05 100755 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -9,15 +9,19 @@ - name: Create the target VNM dir file: path="{{ nvm_dir }}" state=directory owner="{{ nvm_user_name }}" mode="o+rwx" - # variablize the name of the installation script so it fire handlers properly when + # variablize the name of the installation script so it fire installation script properly when # updating from version to version - name: Copy over nvm installation script template: src="install_nvm.script.j2" dest="{{ nvm_dir }}/install_nvm_{{ nvm_node_version }}.script" mode="u+x,g+x" owner="{{ nvm_user_name }}" - register: "nvm_installation_script_changed" + register: "nvm_installation_script_template" -- include: handlers/main.yml - when: nvm_installation_script_changed is defined and nvm_installation_script_changed.changed +- name: Execute the nvm installation script + command: "{{ nvm_dir }}/install_nvm_{{ nvm_node_version }}.script" + when: nvm_installation_script_template is defined and nvm_installation_script_template.changed + # the tag is needed so that linter wouldn't complain about this not being handler, + # and handlers are so unstable I eventually decided not to use them in this role + tags: skip_ansible_lint - name: Add the NVM's variables to all the profiles in the system (Debian) blockinfile: | @@ -44,8 +48,11 @@ file: src="{{ nvm_dir }}/versions/node/v{{ nvm_node_version }}/bin/npm" dest="/usr/bin/npm" state=link mode="u+rwx,g+rx,o+rx" -- name: We love handlers - meta: flush_handlers +- name: Install global packages + shell: + cmd: ". /root/.bashrc && {{ nvm_dir }}/versions/node/v{{ nvm_node_version }}/bin/npm install --global {{ item }}" + creates: "{{ nvm_dir }}/versions/node/v{{ nvm_node_version }}/bin/{{ item }}" + loop: "{{ nvm_install_globally }}" - name: Symlink global packages into PATH for specific environments (like cron's) to be able to access them. file: src="{{ nvm_dir }}/versions/node/v{{ nvm_node_version }}/bin/{{ item }}"