From ee43936e2f88cbea5be8bc8aaf11318570872111 Mon Sep 17 00:00:00 2001 From: p-netm Date: Fri, 31 Jan 2020 21:04:26 +0300 Subject: [PATCH 01/40] Add role that configures node-express --- .drone.yml | 17 ++++ .gitignore | 4 + LICENCE | 201 ++++++++++++++++++++++++++++++++++++++++++++ Vagrantfile | 18 ++++ ansible.cfg | 6 ++ defaults/main.yml | 35 ++++++++ handlers/main.yml | 7 ++ meta/main.yml | 12 +++ tasks/cleanup.yml | 11 +++ tasks/configure.yml | 78 +++++++++++++++++ tasks/install.yml | 115 +++++++++++++++++++++++++ tasks/main.yml | 15 ++++ templates/env.j2 | 3 + tests/inventory | 2 + tests/test.yml | 32 +++++++ 15 files changed, 556 insertions(+) create mode 100644 .drone.yml create mode 100644 .gitignore create mode 100644 LICENCE create mode 100644 Vagrantfile create mode 100644 ansible.cfg create mode 100644 defaults/main.yml create mode 100644 handlers/main.yml create mode 100644 meta/main.yml create mode 100644 tasks/cleanup.yml create mode 100644 tasks/configure.yml create mode 100644 tasks/install.yml create mode 100644 tasks/main.yml create mode 100644 templates/env.j2 create mode 100644 tests/inventory create mode 100644 tests/test.yml diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..29b285d --- /dev/null +++ b/.drone.yml @@ -0,0 +1,17 @@ +pipeline: + build: + image: ubuntu:xenial + commands: + - apt update + - apt install -y git sudo python-pip python-minimal software-properties-common curl apt-transport-https + - pip install ansible + - apt-get -y clean && apt-get -y autoclean + - ansible-playbook -i "127.0.0.1," tests/test.yml --syntax-check + - ansible-playbook -i "127.0.0.1," tests/test.yml --connection=local --become + secrets: [ git_key ] + notify: + image: plugins/slack + secrets: [ slack_webhook ] + when: + status: [success, failure] + event: [push, tag] diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..66b6d2b --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.retry +*.log +yarn.lock +.vagrant/ \ No newline at end of file diff --git a/LICENCE b/LICENCE new file mode 100644 index 0000000..083b6d6 --- /dev/null +++ b/LICENCE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be consTrued + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2018 Ona Kenya Limited + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/Vagrantfile b/Vagrantfile new file mode 100644 index 0000000..97a6c42 --- /dev/null +++ b/Vagrantfile @@ -0,0 +1,18 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : + +Vagrant.configure("2") do |config| + config.vm.provision "ansible_local" do |ansible| + ansible.playbook = "tests/test.yml" + ansible.verbose = "v" + end + + config.vm.provider "virtualbox" do |v| + v.memory = 1024 + v.cpus = 1 + end + + config.vm.network "forwarded_port", guest: 3000, host: 3333 + config.vm.box = "ubuntu/xenial64" + config.ssh.forward_agent = true +end \ No newline at end of file diff --git a/ansible.cfg b/ansible.cfg new file mode 100644 index 0000000..691b50a --- /dev/null +++ b/ansible.cfg @@ -0,0 +1,6 @@ +[defaults] +allow_world_readable_tmpfiles=True +host_key_checking = False + +[ssh_connection] +ssh_args = -o ForwardAgent=yes -o ControlMaster=auto -o ControlPersist=60s \ No newline at end of file diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..22dc970 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,35 @@ +--- +# defaults file for ansible-express +# user +express_system_user: "express" +express_system_group: "www-data" +express_system_user_home: "/home/{{ express_system_user }}" + +# git +express_git_url: +express_git_version: "master" +express_git_key: +express_git_key_filename: "id_ed25519" +express_remove_git_key: True + +# system-wide dependencies +express_system_wide_dependencies: + - git + - nodejs + - yarn +express_node_version: 10.x + +# app +express_env_name: "prod" +express_app_name: "{{ express_system_user }}" +express_codebase_path: "{{ express_system_user_home }}/app" +express_versioned_path: "{{ express_codebase_path }}-versioned" +express_checkout_path: "{{ express_versioned_path }}/{{ ansible_date_time['epoch'] }}" +express_app_path: "{{ express_checkout_path }}/server" +express_log_path: "/var/log/{{ express_app_name }}" +express_service_name: "express-service" +express_pm2_path: "/usr/bin/pm2" + +# app settings +express_app_settings: + NODE_ENV: "production" \ No newline at end of file diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..71c2827 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,7 @@ +--- +- name: restart_express_service + systemd: + name: "{{ express_service_name }}" + state: reloaded + daemon_reload: true + enabled: true \ No newline at end of file diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..3ce7255 --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,12 @@ +--- +galaxy_info: + author: Ona Engineering + description: Install and configure Express + company: Ona Systems Inc + license: Apache-2.0 + + min_ansible_version: 2.9 + platforms: + - name: ubuntu + versions: + - all \ No newline at end of file diff --git a/tasks/cleanup.yml b/tasks/cleanup.yml new file mode 100644 index 0000000..7628825 --- /dev/null +++ b/tasks/cleanup.yml @@ -0,0 +1,11 @@ +--- +- name: Get previous app deploy listing + shell: ls -tr1 chdir={{ express_versioned_path }} + register: versioned_list + +- name: Remove extra files from app directory + file: + path: "{{ express_versioned_path }}/{{ versioned_list.stdout_lines[item|int] }}" + state: absent + with_sequence: start=0 end={{ versioned_list.stdout_lines|length - 10 }} + ignore_errors: yes \ No newline at end of file diff --git a/tasks/configure.yml b/tasks/configure.yml new file mode 100644 index 0000000..a45fe1b --- /dev/null +++ b/tasks/configure.yml @@ -0,0 +1,78 @@ +--- +- name: Install Reveal-web Javascript requirements + shell: yarn install + args: + chdir: "{{ express_checkout_path }}" + become: True + become_user: "{{ express_system_user }}" + +- name: Copy environment variables file + template: + src: env.j2 + dest: "{{ express_app_path }}/.env" + mode: 0644 + owner: "{{ express_system_user }}" + group: "{{ express_system_group }}" + +- name: Install Javascript requirements + shell: yarn install + args: + chdir: "{{ express_app_path }}" + become: True + become_user: "{{ express_system_user }}" + +- name: Compile Javascript + shell: yarn build + args: + chdir: "{{ express_app_path }}" + become: True + become_user: "{{ express_system_user }}" + +# creates a sym-link express_codebase_path; that points to the express checkout path +- name: Make the new codebase current + file: + src: "{{ express_checkout_path }}" + dest: "{{ express_codebase_path }}" + state: link + force: yes + owner: "{{ express_system_user }}" + group: "{{ express_system_group }}" + +- name: Delete express service pm2 process + become: true + become_user: "{{ express_system_user }}" + command: "{{ express_pm2_path }} delete {{express_service_name}}" + ignore_errors: yes + notify: + - restart_express_service + +- name: Start express pm2 app + become: true + become_user: "{{ express_system_user }}" + command: "{{express_pm2_path}} start dist" + args: + chdir: "{{ express_app_path }}" + notify: + - restart_express_service + +- name: save the current PM2 running list + become: true + become_user: "{{ express_system_user }}" + command: "{{ express_pm2_path }} save" + notify: + - restart_express_service + +- name: delete the old startup file + become: true + become_user: "root" + command: "{{ express_pm2_path }} unstartup -u {{ express_system_user }} --service-name {{ express_service_name }}" + ignore_errors: yes + notify: + - restart_express_service + +- name: Generate an express PM2 init service file + become: true + become_user: "root" + command: "{{express_pm2_path}} startup -u {{ express_system_user }} --hp {{ express_app_path }} --service-name {{ express_service_name }}" + notify: + - restart_express_service diff --git a/tasks/install.yml b/tasks/install.yml new file mode 100644 index 0000000..ca41a57 --- /dev/null +++ b/tasks/install.yml @@ -0,0 +1,115 @@ +--- +- name: Add system user + user: + name: "{{ express_system_user }}" + shell: /bin/bash + group: "{{ express_system_group }}" + append: yes + create_home: yes + +- name: Add yarn apt key + shell: curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - + become: True + become_user: root + args: + warn: no + +- name: Add yarn to sources + shell: echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list + become: True + become_user: root + +- name: Download node + shell: curl -sL https://deb.nodesource.com/setup_{{ express_node_version }} | sudo -E bash - + become: True + become_user: root + args: + warn: no + +- name: Update apt cache + apt: + update_cache: "yes" + +- name: Install system-wide dependencies + apt: + name: "{{ express_system_wide_dependencies }}" + state: latest + update_cache: yes + cache_valid_time: 600 + +- name: Install typescript + npm: + name: typescript@latest + global: yes + become: True + become_user: root + +- name: Install PM2 + npm: + name: pm2 + global: yes + become: True + become_user: root + + +- name: Ensure required directories are present + file: + state: directory + owner: "{{ express_system_user }}" + group: www-data + path: "{{ item }}" + when: + - item is defined + - item is not none + with_items: + - "{{ express_versioned_path }}" + - "{{ express_checkout_path }}" + - "{{ express_system_user_home }}/.ssh" + +- name: Git clone without key + git: + accept_hostkey: "yes" + repo: "{{ express_git_url }}" + dest: "{{ express_checkout_path }}" + version: "{{ express_git_version }}" + depth: 1 + become: True + become_user: "{{ express_system_user }}" + when: + - express_git_key is not defined or express_git_key is none + +- name: Copy git key + copy: + content: "{{ express_git_key }}" + dest: "{{ express_system_user_home }}/.ssh/{{ express_git_key_filename }}" + owner: "{{ express_system_user }}" + mode: 0600 + no_log: False + when: + - express_git_key is defined + - express_git_key is not none + +- name: Git clone with key + git: + accept_hostkey: "yes" + repo: "{{ express_git_url }}" + dest: "{{ express_checkout_path }}" + version: "{{ express_git_version }}" + depth: 1 + key_file: "{{ express_system_user_home }}/.ssh/{{ express_git_key_filename }}" + become: True + become_user: "{{ express_system_user }}" + when: + - express_git_key is defined + - express_git_key is not none + +- name: Remove Git Key + file: + state: absent + path: "{{ express_system_user_home }}/.ssh/{{ express_git_key_filename }}" + become: True + become_user: "{{ express_system_user }}" + when: + - express_git_key is defined + - express_git_key is not none + - express_remove_git_key == True diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..d5dc616 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,15 @@ +--- +- name: Install express app + include_tasks: install.yml + tags: + - install + +- name: Configure express app + include_tasks: configure.yml + tags: + - configure + +- name: Cleanup + include_tasks: cleanup.yml + tags: + - cleanup \ No newline at end of file diff --git a/templates/env.j2 b/templates/env.j2 new file mode 100644 index 0000000..64f9de2 --- /dev/null +++ b/templates/env.j2 @@ -0,0 +1,3 @@ +{% for k,v in express_app_settings.items() %} +{{k}}={{v}} +{% endfor %} \ No newline at end of file diff --git a/tests/inventory b/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/tests/test.yml b/tests/test.yml new file mode 100644 index 0000000..ef3be21 --- /dev/null +++ b/tests/test.yml @@ -0,0 +1,32 @@ +--- +- name: Pre-install stuff + hosts: all + tasks: + - name: Install python 2 if not there + raw: | + test -e /usr/bin/python || \ + (apt -y update && apt install -y python-minimal) + register: output + changed_when: output.stdout|trim() != "" + - name: Install sudo if not there + raw: | + test -e /usr/bin/sudo || apt install -y sudo + register: output + changed_when: output.stdout|trim() != "" + gather_facts: False + become: True + +- name: Test express role + hosts: all + vars: + express_system_user: "express_example_app" + express_node_version: 10.x + express_git_url: "https://github.com/onaio/reveal-frontend" + express_git_version: "tiny-backend-integrating-gatekeeper" + express_app_settings: + NODE_ENV: "production" + + gather_facts: True + become: True + roles: + - role: ../.. \ No newline at end of file From 8ea9eeaac91f9d50821812c5e22bb546bf9375c5 Mon Sep 17 00:00:00 2001 From: p-netm Date: Tue, 4 Feb 2020 17:46:36 +0300 Subject: [PATCH 02/40] Fix express_service_name var --- defaults/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/defaults/main.yml b/defaults/main.yml index 22dc970..a33ae4b 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -27,7 +27,7 @@ express_versioned_path: "{{ express_codebase_path }}-versioned" express_checkout_path: "{{ express_versioned_path }}/{{ ansible_date_time['epoch'] }}" express_app_path: "{{ express_checkout_path }}/server" express_log_path: "/var/log/{{ express_app_name }}" -express_service_name: "express-service" +express_service_name: "{{ express_system_user }}" express_pm2_path: "/usr/bin/pm2" # app settings From 9c370e6669671ad83e5996d9bdb84a9df603af7a Mon Sep 17 00:00:00 2001 From: p-netm Date: Tue, 4 Feb 2020 17:48:16 +0300 Subject: [PATCH 03/40] Refactor restart express service handler --- handlers/main.yml | 4 ++-- tasks/configure.yml | 21 +++++++++------------ 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/handlers/main.yml b/handlers/main.yml index 71c2827..7239d52 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,7 +1,7 @@ --- -- name: restart_express_service +- name: start_express_service systemd: name: "{{ express_service_name }}" - state: reloaded + state: started daemon_reload: true enabled: true \ No newline at end of file diff --git a/tasks/configure.yml b/tasks/configure.yml index a45fe1b..39493fd 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -38,41 +38,38 @@ owner: "{{ express_system_user }}" group: "{{ express_system_group }}" -- name: Delete express service pm2 process +- name: Delete express pm2 process become: true become_user: "{{ express_system_user }}" - command: "{{ express_pm2_path }} delete {{express_service_name}}" + command: "{{ express_pm2_path }} delete all" ignore_errors: yes notify: - - restart_express_service + - start_express_service - name: Start express pm2 app become: true become_user: "{{ express_system_user }}" - command: "{{express_pm2_path}} start dist" + command: "{{ express_pm2_path }} start dist/index.js --name {{ express_service_name }}" args: chdir: "{{ express_app_path }}" - notify: - - restart_express_service - name: save the current PM2 running list become: true become_user: "{{ express_system_user }}" command: "{{ express_pm2_path }} save" - notify: - - restart_express_service + - name: delete the old startup file become: true become_user: "root" - command: "{{ express_pm2_path }} unstartup -u {{ express_system_user }} --service-name {{ express_service_name }}" + command: "{{ express_pm2_path }} unstartup -u {{ express_system_user }} --service-name {{ express_service_name }} --hp {{ express_system_user_home }}" ignore_errors: yes notify: - - restart_express_service + - start_express_service - name: Generate an express PM2 init service file become: true become_user: "root" - command: "{{express_pm2_path}} startup -u {{ express_system_user }} --hp {{ express_app_path }} --service-name {{ express_service_name }}" + command: "{{ express_pm2_path }} startup -u {{ express_system_user }} --hp {{ express_system_user_home }} --service-name {{ express_service_name }}" notify: - - restart_express_service + - start_express_service From 447c8e8e6fa091700ba491933f36ff061db2a72f Mon Sep 17 00:00:00 2001 From: p-netm Date: Wed, 5 Feb 2020 14:32:59 +0300 Subject: [PATCH 04/40] Modify startup scripts to run as user not root --- tasks/configure.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tasks/configure.yml b/tasks/configure.yml index 39493fd..2f1ee80 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -61,7 +61,7 @@ - name: delete the old startup file become: true - become_user: "root" + become_user: "{{ express_system_user }}" command: "{{ express_pm2_path }} unstartup -u {{ express_system_user }} --service-name {{ express_service_name }} --hp {{ express_system_user_home }}" ignore_errors: yes notify: @@ -69,7 +69,7 @@ - name: Generate an express PM2 init service file become: true - become_user: "root" + become_user: "{{ express_system_user }}" command: "{{ express_pm2_path }} startup -u {{ express_system_user }} --hp {{ express_system_user_home }} --service-name {{ express_service_name }}" notify: - start_express_service From 418f268cdf5c81fe27d8e4a08d6d91b6f957b430 Mon Sep 17 00:00:00 2001 From: p-netm Date: Wed, 5 Feb 2020 17:32:21 +0300 Subject: [PATCH 05/40] Revert "Modify startup scripts to run as user not root" This reverts commit 447c8e8e6fa091700ba491933f36ff061db2a72f. --- tasks/configure.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tasks/configure.yml b/tasks/configure.yml index 2f1ee80..39493fd 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -61,7 +61,7 @@ - name: delete the old startup file become: true - become_user: "{{ express_system_user }}" + become_user: "root" command: "{{ express_pm2_path }} unstartup -u {{ express_system_user }} --service-name {{ express_service_name }} --hp {{ express_system_user_home }}" ignore_errors: yes notify: @@ -69,7 +69,7 @@ - name: Generate an express PM2 init service file become: true - become_user: "{{ express_system_user }}" + become_user: "root" command: "{{ express_pm2_path }} startup -u {{ express_system_user }} --hp {{ express_system_user_home }} --service-name {{ express_service_name }}" notify: - start_express_service From 7ea3df3281fe70d2d602069b9a91ed98c30c528a Mon Sep 17 00:00:00 2001 From: p-netm Date: Thu, 6 Feb 2020 16:21:26 +0300 Subject: [PATCH 06/40] Restart systemctl as root; handler --- handlers/main.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/handlers/main.yml b/handlers/main.yml index 7239d52..a9d4d19 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,7 +1,9 @@ --- - name: start_express_service + become: true + become_user: root systemd: name: "{{ express_service_name }}" - state: started + state: restarted daemon_reload: true enabled: true \ No newline at end of file From a1ecc34746a7a3a96bbd6aa6b89d15e70bddb21e Mon Sep 17 00:00:00 2001 From: p-netm Date: Wed, 12 Feb 2020 15:13:49 +0300 Subject: [PATCH 07/40] Add yarn idempotently --- tasks/install.yml | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/tasks/install.yml b/tasks/install.yml index ca41a57..3f9835b 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -7,16 +7,19 @@ append: yes create_home: yes -- name: Add yarn apt key - shell: curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - - become: True - become_user: root - args: - warn: no +- name: Add yarn apt key to keyring + apt_key: + url: https://dl.yarnpkg.com/debian/pubkey.gpg + state: present - name: Add yarn to sources - shell: echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list - become: True + lineinfile: + create: yes + line: "deb https://dl.yarnpkg.com/debian/ stable main" + path: /etc/apt/sources.list.d/yarn.list + regexp: "deb https://dl.yarnpkg.com/debian/ stable main" + state: present + become: true become_user: root - name: Download node From f67846270ba6dbd0aafae9e2b00e1b95bc757bcd Mon Sep 17 00:00:00 2001 From: p-netm Date: Wed, 12 Feb 2020 20:32:30 +0300 Subject: [PATCH 08/40] make tasks idempotent --- defaults/main.yml | 3 +++ tasks/cleanup.yml | 3 ++- tasks/configure.yml | 39 +++++++++++++++++++++++++++++++++------ tasks/install.yml | 23 ++++++++++++++++------- 4 files changed, 54 insertions(+), 14 deletions(-) diff --git a/defaults/main.yml b/defaults/main.yml index a33ae4b..4a4da16 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -29,6 +29,9 @@ express_app_path: "{{ express_checkout_path }}/server" express_log_path: "/var/log/{{ express_app_name }}" express_service_name: "{{ express_system_user }}" express_pm2_path: "/usr/bin/pm2" +express_package_json_paths: + - "{{ express_app_path }}" + - "{{ express_checkout_path }}" # app settings express_app_settings: diff --git a/tasks/cleanup.yml b/tasks/cleanup.yml index 7628825..9528c84 100644 --- a/tasks/cleanup.yml +++ b/tasks/cleanup.yml @@ -1,7 +1,8 @@ --- - name: Get previous app deploy listing - shell: ls -tr1 chdir={{ express_versioned_path }} + command: ls -tr1 chdir={{ express_versioned_path }} register: versioned_list + changed_when: false - name: Remove extra files from app directory file: diff --git a/tasks/configure.yml b/tasks/configure.yml index 39493fd..dbc90bf 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -13,20 +13,23 @@ mode: 0644 owner: "{{ express_system_user }}" group: "{{ express_system_group }}" + changed_when: false - name: Install Javascript requirements - shell: yarn install - args: - chdir: "{{ express_app_path }}" + yarn: + state: present + path: "{{ express_app_path }}" + changed_when: false become: True become_user: "{{ express_system_user }}" - name: Compile Javascript - shell: yarn build - args: + command: + cmd: yarn build chdir: "{{ express_app_path }}" become: True become_user: "{{ express_system_user }}" + changed_when: false # creates a sym-link express_codebase_path; that points to the express checkout path - name: Make the new codebase current @@ -37,14 +40,34 @@ force: yes owner: "{{ express_system_user }}" group: "{{ express_system_group }}" + changed_when: false + +- name: Restart express pm2 process + become: true + become_user: "{{ express_system_user }}" + command: "{{ express_pm2_path }} restart {{ express_service_name }}" + args: + chdir: "{{ express_app_path }}" + register: pm2_process + ignore_errors: yes + +- name: set express_service_exists fact + set_fact: + express_service_not_exists: "{{pm2_process.rc != 0}}" + +- name: set express_service_exists fact + set_fact: + express_service_exists: "{{not express_service_not_exists}}" - name: Delete express pm2 process become: true become_user: "{{ express_system_user }}" command: "{{ express_pm2_path }} delete all" - ignore_errors: yes notify: - start_express_service + when: express_service_not_exists + ignore_errors: yes + - name: Start express pm2 app become: true @@ -52,11 +75,13 @@ command: "{{ express_pm2_path }} start dist/index.js --name {{ express_service_name }}" args: chdir: "{{ express_app_path }}" + when: express_service_not_exists - name: save the current PM2 running list become: true become_user: "{{ express_system_user }}" command: "{{ express_pm2_path }} save" + when: express_service_not_exists - name: delete the old startup file @@ -66,6 +91,7 @@ ignore_errors: yes notify: - start_express_service + when: express_service_not_exists - name: Generate an express PM2 init service file become: true @@ -73,3 +99,4 @@ command: "{{ express_pm2_path }} startup -u {{ express_system_user }} --hp {{ express_system_user_home }} --service-name {{ express_service_name }}" notify: - start_express_service + when: express_service_not_exists diff --git a/tasks/install.yml b/tasks/install.yml index 3f9835b..91c650c 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -7,6 +7,20 @@ append: yes create_home: yes +- name: Detect if Node is installed + command: node --version + register: node_version + changed_when: false + ignore_errors: True + +- name: Download node + shell: curl -sL https://deb.nodesource.com/setup_{{ express_node_version }} | sudo -E bash - + become: True + become_user: root + args: + warn: no + when: node_version.rc != 0 + - name: Add yarn apt key to keyring apt_key: url: https://dl.yarnpkg.com/debian/pubkey.gpg @@ -22,12 +36,6 @@ become: true become_user: root -- name: Download node - shell: curl -sL https://deb.nodesource.com/setup_{{ express_node_version }} | sudo -E bash - - become: True - become_user: root - args: - warn: no - name: Update apt cache apt: @@ -47,7 +55,7 @@ become: True become_user: root -- name: Install PM2 +- name: Install PM2 npm: name: pm2 global: yes @@ -68,6 +76,7 @@ - "{{ express_versioned_path }}" - "{{ express_checkout_path }}" - "{{ express_system_user_home }}/.ssh" + changed_when: false - name: Git clone without key git: From 627a1d056d733a4da2f17f79aa63a482858b424a Mon Sep 17 00:00:00 2001 From: p-netm Date: Fri, 14 Feb 2020 17:38:37 +0300 Subject: [PATCH 09/40] Fix linting issues --- tasks/install.yml | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/tasks/install.yml b/tasks/install.yml index 91c650c..a1e43a3 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -14,11 +14,12 @@ ignore_errors: True - name: Download node - shell: curl -sL https://deb.nodesource.com/setup_{{ express_node_version }} | sudo -E bash - + shell: set -o pipefail && curl -sL https://deb.nodesource.com/setup_{{ express_node_version }} | sudo -E bash - become: True become_user: root args: warn: no + executable: /bin/bash when: node_version.rc != 0 - name: Add yarn apt key to keyring @@ -48,14 +49,7 @@ update_cache: yes cache_valid_time: 600 -- name: Install typescript - npm: - name: typescript@latest - global: yes - become: True - become_user: root - -- name: Install PM2 +- name: Install PM2 npm: name: pm2 global: yes @@ -124,4 +118,4 @@ when: - express_git_key is defined - express_git_key is not none - - express_remove_git_key == True + - express_remove_git_key From cd309b2df57c26482723b940cb22e8db57f445bc Mon Sep 17 00:00:00 2001 From: p-netm Date: Mon, 17 Feb 2020 18:32:10 +0300 Subject: [PATCH 10/40] Refactor due to lint errors --- .gitignore | 3 +- Vagrantfile | 18 ---------- tasks/configure.yml | 85 +++++++++++++++++++++------------------------ tasks/install.yml | 2 +- tests/inventory | 2 -- tests/test.yml | 32 ----------------- 6 files changed, 42 insertions(+), 100 deletions(-) delete mode 100644 Vagrantfile delete mode 100644 tests/inventory delete mode 100644 tests/test.yml diff --git a/.gitignore b/.gitignore index 66b6d2b..576c082 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.retry *.log yarn.lock -.vagrant/ \ No newline at end of file +.vagrant/ +.vscode/ \ No newline at end of file diff --git a/Vagrantfile b/Vagrantfile deleted file mode 100644 index 97a6c42..0000000 --- a/Vagrantfile +++ /dev/null @@ -1,18 +0,0 @@ -# -*- mode: ruby -*- -# vi: set ft=ruby : - -Vagrant.configure("2") do |config| - config.vm.provision "ansible_local" do |ansible| - ansible.playbook = "tests/test.yml" - ansible.verbose = "v" - end - - config.vm.provider "virtualbox" do |v| - v.memory = 1024 - v.cpus = 1 - end - - config.vm.network "forwarded_port", guest: 3000, host: 3333 - config.vm.box = "ubuntu/xenial64" - config.ssh.forward_agent = true -end \ No newline at end of file diff --git a/tasks/configure.yml b/tasks/configure.yml index dbc90bf..2107dbd 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -1,11 +1,4 @@ --- -- name: Install Reveal-web Javascript requirements - shell: yarn install - args: - chdir: "{{ express_checkout_path }}" - become: True - become_user: "{{ express_system_user }}" - - name: Copy environment variables file template: src: env.j2 @@ -16,12 +9,13 @@ changed_when: false - name: Install Javascript requirements + become: True + become_user: "{{ express_system_user }}" yarn: state: present - path: "{{ express_app_path }}" + path: "{{ item }}" + with_items: "{{ express_package_json_paths }}" changed_when: false - become: True - become_user: "{{ express_system_user }}" - name: Compile Javascript command: @@ -50,53 +44,52 @@ chdir: "{{ express_app_path }}" register: pm2_process ignore_errors: yes + changed_when: false - name: set express_service_exists fact set_fact: - express_service_not_exists: "{{pm2_process.rc != 0}}" + express_service_not_exists: "{{ pm2_process.rc != 0 }}" - name: set express_service_exists fact set_fact: - express_service_exists: "{{not express_service_not_exists}}" + express_service_exists: "{{ not express_service_not_exists }}" -- name: Delete express pm2 process - become: true - become_user: "{{ express_system_user }}" - command: "{{ express_pm2_path }} delete all" - notify: - - start_express_service - when: express_service_not_exists - ignore_errors: yes +- name: creating pm2 process + block: + - name: Delete express pm2 process + become: true + become_user: "{{ express_system_user }}" + command: "{{ express_pm2_path }} delete all" + notify: + - start_express_service + ignore_errors: yes -- name: Start express pm2 app - become: true - become_user: "{{ express_system_user }}" - command: "{{ express_pm2_path }} start dist/index.js --name {{ express_service_name }}" - args: - chdir: "{{ express_app_path }}" - when: express_service_not_exists + - name: Start express pm2 app + become: true + become_user: "{{ express_system_user }}" + command: "{{ express_pm2_path }} start dist/index.js --name {{ express_service_name }}" + args: + chdir: "{{ express_app_path }}" -- name: save the current PM2 running list - become: true - become_user: "{{ express_system_user }}" - command: "{{ express_pm2_path }} save" - when: express_service_not_exists + - name: save the current PM2 running list + become: true + become_user: "{{ express_system_user }}" + command: "{{ express_pm2_path }} save" -- name: delete the old startup file - become: true - become_user: "root" - command: "{{ express_pm2_path }} unstartup -u {{ express_system_user }} --service-name {{ express_service_name }} --hp {{ express_system_user_home }}" - ignore_errors: yes - notify: - - start_express_service - when: express_service_not_exists + - name: delete the old startup file + become: true + become_user: "root" + command: "{{ express_pm2_path }} unstartup -u {{ express_system_user }} --service-name {{ express_service_name }} --hp {{ express_system_user_home }}" + ignore_errors: yes + notify: + - start_express_service -- name: Generate an express PM2 init service file - become: true - become_user: "root" - command: "{{ express_pm2_path }} startup -u {{ express_system_user }} --hp {{ express_system_user_home }} --service-name {{ express_service_name }}" - notify: - - start_express_service + - name: Generate an express PM2 init service file + become: true + become_user: "root" + command: "{{ express_pm2_path }} startup -u {{ express_system_user }} --hp {{ express_system_user_home }} --service-name {{ express_service_name }}" + notify: + - start_express_service when: express_service_not_exists diff --git a/tasks/install.yml b/tasks/install.yml index a1e43a3..3bdb63b 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -45,7 +45,7 @@ - name: Install system-wide dependencies apt: name: "{{ express_system_wide_dependencies }}" - state: latest + state: present update_cache: yes cache_valid_time: 600 diff --git a/tests/inventory b/tests/inventory deleted file mode 100644 index 878877b..0000000 --- a/tests/inventory +++ /dev/null @@ -1,2 +0,0 @@ -localhost - diff --git a/tests/test.yml b/tests/test.yml deleted file mode 100644 index ef3be21..0000000 --- a/tests/test.yml +++ /dev/null @@ -1,32 +0,0 @@ ---- -- name: Pre-install stuff - hosts: all - tasks: - - name: Install python 2 if not there - raw: | - test -e /usr/bin/python || \ - (apt -y update && apt install -y python-minimal) - register: output - changed_when: output.stdout|trim() != "" - - name: Install sudo if not there - raw: | - test -e /usr/bin/sudo || apt install -y sudo - register: output - changed_when: output.stdout|trim() != "" - gather_facts: False - become: True - -- name: Test express role - hosts: all - vars: - express_system_user: "express_example_app" - express_node_version: 10.x - express_git_url: "https://github.com/onaio/reveal-frontend" - express_git_version: "tiny-backend-integrating-gatekeeper" - express_app_settings: - NODE_ENV: "production" - - gather_facts: True - become: True - roles: - - role: ../.. \ No newline at end of file From ffb428c7705e3acb05f961c3e0eab2d9336c68f1 Mon Sep 17 00:00:00 2001 From: p-netm Date: Mon, 17 Feb 2020 18:32:50 +0300 Subject: [PATCH 11/40] Initial molecule tests --- molecule/default/Dockerfile.j2 | 22 +++++++ molecule/default/INSTALL.rst | 22 +++++++ molecule/default/molecule.yml | 25 ++++++++ molecule/default/playbook.yml | 13 ++++ molecule/default/prepare.yml | 10 +++ .../test_default.cpython-36-pytest-5.3.5.pyc | Bin 0 -> 6133 bytes .../__pycache__/test_default.cpython-36.pyc | Bin 0 -> 712 bytes molecule/default/tests/test_default.py | 60 ++++++++++++++++++ 8 files changed, 152 insertions(+) create mode 100644 molecule/default/Dockerfile.j2 create mode 100644 molecule/default/INSTALL.rst create mode 100644 molecule/default/molecule.yml create mode 100644 molecule/default/playbook.yml create mode 100644 molecule/default/prepare.yml create mode 100644 molecule/default/tests/__pycache__/test_default.cpython-36-pytest-5.3.5.pyc create mode 100644 molecule/default/tests/__pycache__/test_default.cpython-36.pyc create mode 100644 molecule/default/tests/test_default.py diff --git a/molecule/default/Dockerfile.j2 b/molecule/default/Dockerfile.j2 new file mode 100644 index 0000000..0de39e6 --- /dev/null +++ b/molecule/default/Dockerfile.j2 @@ -0,0 +1,22 @@ +# Molecule managed + +{% if item.registry is defined %} +FROM {{ item.registry.url }}/{{ item.image }} +{% else %} +FROM {{ item.image }} +{% endif %} + +{% if item.env is defined %} +{% for var, value in item.env.items() %} +{% if value %} +ENV {{ var }} {{ value }} +{% endif %} +{% endfor %} +{% endif %} + +RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y python sudo bash ca-certificates iproute2 && apt-get clean; \ + elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python sudo python-devel python*-dnf bash iproute && dnf clean all; \ + elif [ $(command -v yum) ]; then yum makecache fast && yum install -y python sudo yum-plugin-ovl bash iproute && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \ + elif [ $(command -v zypper) ]; then zypper refresh && zypper install -y python sudo bash python-xml iproute2 && zypper clean -a; \ + elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates; \ + elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates iproute2 && xbps-remove -O; fi diff --git a/molecule/default/INSTALL.rst b/molecule/default/INSTALL.rst new file mode 100644 index 0000000..6a44bde --- /dev/null +++ b/molecule/default/INSTALL.rst @@ -0,0 +1,22 @@ +******* +Docker driver installation guide +******* + +Requirements +============ + +* Docker Engine + +Install +======= + +Please refer to the `Virtual environment`_ documentation for installation best +practices. If not using a virtual environment, please consider passing the +widely recommended `'--user' flag`_ when invoking ``pip``. + +.. _Virtual environment: https://virtualenv.pypa.io/en/latest/ +.. _'--user' flag: https://packaging.python.org/tutorials/installing-packages/#installing-to-the-user-site + +.. code-block:: bash + + $ pip install 'molecule[docker]' diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml new file mode 100644 index 0000000..9aa49d0 --- /dev/null +++ b/molecule/default/molecule.yml @@ -0,0 +1,25 @@ +--- +dependency: + name: galaxy +driver: + name: docker +lint: + name: yamllint +platforms: + - name: instance + image: "geerlingguy/docker-ubuntu1804-ansible:latest" + command: ${MOLECULE_DOCKER_COMMAND:-""} + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:ro + privileged: true + pre_build_image: true +provisioner: + name: ansible + lint: + name: ansible-lint + playbook: + prepare: prepare.yml +verifier: + name: testinfra + lint: + name: flake8 diff --git a/molecule/default/playbook.yml b/molecule/default/playbook.yml new file mode 100644 index 0000000..4d6ec96 --- /dev/null +++ b/molecule/default/playbook.yml @@ -0,0 +1,13 @@ +--- +- name: Converge + hosts: all + roles: + - role: ansible-express + express_system_user: "express" + express_system_group: "www-data" + express_system_user_home: "/home/{{ express_system_user }}" + express_node_version: 10.x + express_git_url: "https://github.com/onaio/reveal-frontend" + express_git_version: "testAnsible" + express_app_settings: + NODE_ENV: "production" diff --git a/molecule/default/prepare.yml b/molecule/default/prepare.yml new file mode 100644 index 0000000..f5cef00 --- /dev/null +++ b/molecule/default/prepare.yml @@ -0,0 +1,10 @@ +--- +- hosts: all + become: yes + become_user: root + tasks: + - name: Install curl + apt: + name: curl + update_cache: yes + state: present diff --git a/molecule/default/tests/__pycache__/test_default.cpython-36-pytest-5.3.5.pyc b/molecule/default/tests/__pycache__/test_default.cpython-36-pytest-5.3.5.pyc new file mode 100644 index 0000000000000000000000000000000000000000..dc9e66b8e9f436c139cfc0879f7c4fd7accdc550 GIT binary patch literal 6133 zcmbtYTW=f372aL$E-#`e%koXWm9?A3bZt_$W!Z7-)NU--P8Hg95+fywENIS3BE?&0 zmX4*6Kv4xjQJ_U0`cMQxpNax~>o4fR-@PzjJ0uE@?|qQ3;%xot>SXo%5aV zd}nUV&SviX{ryk-NyGTJ5&N^zzK5Us2MS>bvuRXJuB}F*VOJb(yA_w~WF^UUs*?88 zjZ9;vk@elitgsf1U9*uBiCwcYCv4$-VN{L?S0qu-`^m=9#)3$Rw8(5&yOx*{*)K4@ za!kyM97Y_EM$CyLVxC7F6$?D#gg7RSW5h{uLYzc>N}Ljls29a)aR&8iaaNo|eMXoc z8in)!!8aJ?%E=F&tgXKH@!G2Qxcq#z{LzzVzwjPDURwD1(jfe$@N@Bd4?pz^ zy?vvB@82~CX5Va>1FLVT=UAZ~U?+BsEmvI$?1t0F2)TlmORctr`_#5}P2qfFoiGN8 zAc>w-AA7ND{sw!AnaoCdU!z5a{&j2Dj=k+`+}CgW z3%_CNZQN($t?6$Pc&m%Oo9VlC6YXTQg5+=oHr9ZjgZG<_ndy~$WvWeH%RH~;=<932 zh|9y(d}YezL#=1wFzXQp>~^fYlPUQ@t+d%ufeMYi%T=X(8RXx()ZM;bP?^u8M*c2$ zEfrMJe^tlW%pf$^3s#uuZZA=BJ+x(~6YN=c@9teW;OQQw&kXW+^?WxAs*sR`1qt^s zXrh~Yx!4+R6%=*e+PiwdoEx&!>rPB6$+-%qOzSE`FUU@GT5wQ5s^8T1Ce3cRWePkCyy(`yP(`d#U5G&?V4C4df=;*8 zX;-Qt75Q#woZj*p(+kIR8 zjwl20x8d4Z>Sxz>$%U9m)^MNcLq10<84-!gZ4e39pbwE`*LFZcnZCoW?FK2_P50d? z*R}@T7L(Ro(Ehd$_73x~=Oq ziyp$?M9lazIy4j-h!|uLiWv%?hCl;w(wwBudY1sYXIW&1tCtu!#XlW5ONv!CJ2E@ftAA_(Rf`s#8CiTP`v-ONU}|B zw)wkceby3>&>B(zVS2M%KrM|&)74bPzMa*i&iczc#NJX(Mv%PCS!hDDpL zgSY%Ku1CH&v8|c?RD33;tN%|GkF4OY$Y91<(F^L#Rp^sly88!NisAt1Ow=7 zOMu-+5TG2vA^(^E;6{AMbp3t3Ru>rtvf@&I&8I)Q=aIp5I^wL&Cu|e8%W~eg^2*C_^-0N{F>! zvyh+&a*c``-!4TSVvnIse~L=}jM_2WRC1GVi)j)TrDIrevC5LaL<{F2 z5!|W0AKVX4wE*|iwVrI|H-n&ir&Qt;of6MGH*gZTNx5OGEyq0_1Ox zF^znmif_%|B3F4zgP);@l$m^oI+$2FeLIwE{G6UyqvAXjA5f8};!Ww>NzJP|^(T>z zT%ir`Cs8+2UsKNTA?}S}gQi1;-`8mzloVx1!_>DI4?T z1KiigaAmZHhkg#X31Y5RVUPr=zRgLQ14U*cje}g*iVUJWfkQWPzifGDE^?wGF=M^v zvrN-D*-vH}pA8q{CoL2AoK{L7HsPaoYV6hREkA3TbHRZB+}JF{4dU z*;LN$YQ^?3O~Iz394juBm$mUIu4&)Dh*o%d!fw$AlKpz!JGieXHj~fm!nEN8dw3uTPOeM5GfghVCrtXZi6_w= zd~l@C66{u0ZiO>AN5dHmLf4MZNu*8^NWyZkX9{Nql!RYNdlBPO-d^6>)Q>bTze^cB d%ZU_rFJ-1I{L^tpB9%at+e*rrx$PoYl literal 0 HcmV?d00001 diff --git a/molecule/default/tests/__pycache__/test_default.cpython-36.pyc b/molecule/default/tests/__pycache__/test_default.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..90bddda29b26c2022b734799893f1445f377f9f7 GIT binary patch literal 712 zcmZWn&5qMB5VoD9+lFnqAR#Usxb@Hm3B&;*M71i?O5Lus{7CBqQHUp9r%4>y&O+Jk zh4L~y3a{WRCtiUQj!Rc%aWr3M#`E*(lb`ha;mP^r!*h(#PqcSD>%X$5o>^ds;R4a^ zA5#J(_i{fE0OcJ<9-s}*516;Xw99-Jd_%Oy@EbJj{Ib1hPVb+b&ZbB2X4Ck1{(d@t zdwO;rzdoK#jh7TfYB#$>EVg!JO+B@6jdHtpgV*>P=UCm-^qWoERWwmB3;%|&FfK9)k(SpHu`h{P>l34Ef-v_qEZx)HU&hf5KC@dtwBaAg({I; zM3*3W#%)G}(!9!K5?x*GmyUM>V#Xz;TFBLUeY<8iF0PUi{=eQW`DaACFlLY?O`*s2 z%G`Acr%l{S61HM@Eg&3{5D&2ITYnfDA{2diLwDS0UZ` Date: Mon, 24 Feb 2020 11:30:51 +0300 Subject: [PATCH 12/40] Add requirements file --- requirements.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..33e47ea --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +molecule[docker] From e2e58e25cb59ffd30be8affca956179de3652466 Mon Sep 17 00:00:00 2001 From: p-netm Date: Tue, 25 Feb 2020 15:22:52 +0300 Subject: [PATCH 13/40] A new start with molecule --- .drone.yml | 17 --- .github/workflows/main.yml | 23 ++++ .gitignore | 121 +++++++++++++++++- .yamllint | 41 ++++++ ansible.cfg | 6 - defaults/main.yml | 4 +- handlers/main.yml | 2 +- meta/main.yml | 6 +- molecule/default/Dockerfile.j2 | 22 ---- molecule/default/converge.yml | 16 +++ molecule/default/molecule.yml | 13 +- molecule/default/playbook.yml | 13 -- molecule/default/prepare.yml | 14 +- .../test_default.cpython-36-pytest-5.3.5.pyc | Bin 6133 -> 0 bytes .../__pycache__/test_default.cpython-36.pyc | Bin 712 -> 0 bytes molecule/default/tests/conftest.py | 21 +++ molecule/default/tests/test_default.py | 58 ++++++--- requirements.txt | 6 +- tasks/cleanup.yml | 2 +- tasks/configure.yml | 76 ++++++----- tasks/install.yml | 37 +++--- tasks/main.yml | 2 +- tests/inventory | 2 + tests/test.yml | 5 + vars/main.yml | 2 + 25 files changed, 353 insertions(+), 156 deletions(-) delete mode 100644 .drone.yml create mode 100644 .github/workflows/main.yml create mode 100644 .yamllint delete mode 100644 ansible.cfg delete mode 100644 molecule/default/Dockerfile.j2 create mode 100644 molecule/default/converge.yml delete mode 100644 molecule/default/playbook.yml delete mode 100644 molecule/default/tests/__pycache__/test_default.cpython-36-pytest-5.3.5.pyc delete mode 100644 molecule/default/tests/__pycache__/test_default.cpython-36.pyc create mode 100644 molecule/default/tests/conftest.py create mode 100644 tests/inventory create mode 100644 tests/test.yml create mode 100644 vars/main.yml diff --git a/.drone.yml b/.drone.yml deleted file mode 100644 index 29b285d..0000000 --- a/.drone.yml +++ /dev/null @@ -1,17 +0,0 @@ -pipeline: - build: - image: ubuntu:xenial - commands: - - apt update - - apt install -y git sudo python-pip python-minimal software-properties-common curl apt-transport-https - - pip install ansible - - apt-get -y clean && apt-get -y autoclean - - ansible-playbook -i "127.0.0.1," tests/test.yml --syntax-check - - ansible-playbook -i "127.0.0.1," tests/test.yml --connection=local --become - secrets: [ git_key ] - notify: - image: plugins/slack - secrets: [ slack_webhook ] - when: - status: [success, failure] - event: [push, tag] diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..a914081 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,23 @@ +--- +name: CI + +on: + - push + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + image: + - ubuntu + steps: + - name: checkout + uses: actions/checkout@v2 + with: + path: "${{ github.repository }}" + - name: molecule + uses: robertdebock/molecule-action@2.0.0 + with: + image: "${{ matrix.image }}" + options: parallel diff --git a/.gitignore b/.gitignore index 576c082..73fa06b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,118 @@ -*.retry +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: *.log -yarn.lock -.vagrant/ -.vscode/ \ No newline at end of file +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject +.vscode diff --git a/.yamllint b/.yamllint new file mode 100644 index 0000000..e081dda --- /dev/null +++ b/.yamllint @@ -0,0 +1,41 @@ +--- +# Based on ansible-lint config +extends: default + +ignore: | + .github/ + +rules: + braces: + min-spaces-inside: 1 + max-spaces-inside: 1 + level: error + brackets: + max-spaces-inside: 0 + level: error + colons: + max-spaces-after: 1 + level: error + commas: + max-spaces-after: 1 + level: error + comments: + require-starting-space: true + comments-indentation: enable + document-start: + present: true + level: error + empty-lines: + max: 2 + level: error + hyphens: + max-spaces-after: 1 + level: error + indentation: + spaces: 2 + key-duplicates: enable + line-length: + max: 89 + new-line-at-end-of-file: enable + trailing-spaces: enable + truthy: enable diff --git a/ansible.cfg b/ansible.cfg deleted file mode 100644 index 691b50a..0000000 --- a/ansible.cfg +++ /dev/null @@ -1,6 +0,0 @@ -[defaults] -allow_world_readable_tmpfiles=True -host_key_checking = False - -[ssh_connection] -ssh_args = -o ForwardAgent=yes -o ControlMaster=auto -o ControlPersist=60s \ No newline at end of file diff --git a/defaults/main.yml b/defaults/main.yml index 4a4da16..784ec68 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -10,7 +10,7 @@ express_git_url: express_git_version: "master" express_git_key: express_git_key_filename: "id_ed25519" -express_remove_git_key: True +express_remove_git_key: true # system-wide dependencies express_system_wide_dependencies: @@ -35,4 +35,4 @@ express_package_json_paths: # app settings express_app_settings: - NODE_ENV: "production" \ No newline at end of file + NODE_ENV: "production" diff --git a/handlers/main.yml b/handlers/main.yml index a9d4d19..6cb482f 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -6,4 +6,4 @@ name: "{{ express_service_name }}" state: restarted daemon_reload: true - enabled: true \ No newline at end of file + enabled: true diff --git a/meta/main.yml b/meta/main.yml index 3ce7255..10c6484 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -7,6 +7,6 @@ galaxy_info: min_ansible_version: 2.9 platforms: - - name: ubuntu - versions: - - all \ No newline at end of file + - name: ubuntu + versions: + - all diff --git a/molecule/default/Dockerfile.j2 b/molecule/default/Dockerfile.j2 deleted file mode 100644 index 0de39e6..0000000 --- a/molecule/default/Dockerfile.j2 +++ /dev/null @@ -1,22 +0,0 @@ -# Molecule managed - -{% if item.registry is defined %} -FROM {{ item.registry.url }}/{{ item.image }} -{% else %} -FROM {{ item.image }} -{% endif %} - -{% if item.env is defined %} -{% for var, value in item.env.items() %} -{% if value %} -ENV {{ var }} {{ value }} -{% endif %} -{% endfor %} -{% endif %} - -RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y python sudo bash ca-certificates iproute2 && apt-get clean; \ - elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python sudo python-devel python*-dnf bash iproute && dnf clean all; \ - elif [ $(command -v yum) ]; then yum makecache fast && yum install -y python sudo yum-plugin-ovl bash iproute && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \ - elif [ $(command -v zypper) ]; then zypper refresh && zypper install -y python sudo bash python-xml iproute2 && zypper clean -a; \ - elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates; \ - elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates iproute2 && xbps-remove -O; fi diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml new file mode 100644 index 0000000..0d1f1a8 --- /dev/null +++ b/molecule/default/converge.yml @@ -0,0 +1,16 @@ +--- +- name: Converge + hosts: all + tasks: + - name: "Include ansible-express" + include_role: + name: "ansible-express" + vars: + express_system_user: "express" + express_system_group: "www-data" + express_system_user_home: "/home/{{ express_system_user }}" + express_node_version: 10.x + express_git_url: "https://github.com/onaio/reveal-frontend" + express_git_version: "tiny-backend" + express_app_settings: + NODE_ENV: "production" diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index 9aa49d0..6de3839 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -3,11 +3,9 @@ dependency: name: galaxy driver: name: docker -lint: - name: yamllint platforms: - name: instance - image: "geerlingguy/docker-ubuntu1804-ansible:latest" + image: "geerlingguy/docker-ubuntu1604-ansible:latest" command: ${MOLECULE_DOCKER_COMMAND:-""} volumes: - /sys/fs/cgroup:/sys/fs/cgroup:ro @@ -15,11 +13,12 @@ platforms: pre_build_image: true provisioner: name: ansible - lint: - name: ansible-lint playbook: prepare: prepare.yml verifier: name: testinfra - lint: - name: flake8 +lint: | + set -e + yamllint . + ansible-lint + flake8 diff --git a/molecule/default/playbook.yml b/molecule/default/playbook.yml deleted file mode 100644 index 4d6ec96..0000000 --- a/molecule/default/playbook.yml +++ /dev/null @@ -1,13 +0,0 @@ ---- -- name: Converge - hosts: all - roles: - - role: ansible-express - express_system_user: "express" - express_system_group: "www-data" - express_system_user_home: "/home/{{ express_system_user }}" - express_node_version: 10.x - express_git_url: "https://github.com/onaio/reveal-frontend" - express_git_version: "testAnsible" - express_app_settings: - NODE_ENV: "production" diff --git a/molecule/default/prepare.yml b/molecule/default/prepare.yml index f5cef00..2ddf58b 100644 --- a/molecule/default/prepare.yml +++ b/molecule/default/prepare.yml @@ -1,10 +1,12 @@ --- - hosts: all - become: yes + become: true become_user: root tasks: - - name: Install curl - apt: - name: curl - update_cache: yes - state: present + - name: Install prerequisites + apt: + name: " {{ item }} " + update_cache: true + state: present + loop: + - curl diff --git a/molecule/default/tests/__pycache__/test_default.cpython-36-pytest-5.3.5.pyc b/molecule/default/tests/__pycache__/test_default.cpython-36-pytest-5.3.5.pyc deleted file mode 100644 index dc9e66b8e9f436c139cfc0879f7c4fd7accdc550..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6133 zcmbtYTW=f372aL$E-#`e%koXWm9?A3bZt_$W!Z7-)NU--P8Hg95+fywENIS3BE?&0 zmX4*6Kv4xjQJ_U0`cMQxpNax~>o4fR-@PzjJ0uE@?|qQ3;%xot>SXo%5aV zd}nUV&SviX{ryk-NyGTJ5&N^zzK5Us2MS>bvuRXJuB}F*VOJb(yA_w~WF^UUs*?88 zjZ9;vk@elitgsf1U9*uBiCwcYCv4$-VN{L?S0qu-`^m=9#)3$Rw8(5&yOx*{*)K4@ za!kyM97Y_EM$CyLVxC7F6$?D#gg7RSW5h{uLYzc>N}Ljls29a)aR&8iaaNo|eMXoc z8in)!!8aJ?%E=F&tgXKH@!G2Qxcq#z{LzzVzwjPDURwD1(jfe$@N@Bd4?pz^ zy?vvB@82~CX5Va>1FLVT=UAZ~U?+BsEmvI$?1t0F2)TlmORctr`_#5}P2qfFoiGN8 zAc>w-AA7ND{sw!AnaoCdU!z5a{&j2Dj=k+`+}CgW z3%_CNZQN($t?6$Pc&m%Oo9VlC6YXTQg5+=oHr9ZjgZG<_ndy~$WvWeH%RH~;=<932 zh|9y(d}YezL#=1wFzXQp>~^fYlPUQ@t+d%ufeMYi%T=X(8RXx()ZM;bP?^u8M*c2$ zEfrMJe^tlW%pf$^3s#uuZZA=BJ+x(~6YN=c@9teW;OQQw&kXW+^?WxAs*sR`1qt^s zXrh~Yx!4+R6%=*e+PiwdoEx&!>rPB6$+-%qOzSE`FUU@GT5wQ5s^8T1Ce3cRWePkCyy(`yP(`d#U5G&?V4C4df=;*8 zX;-Qt75Q#woZj*p(+kIR8 zjwl20x8d4Z>Sxz>$%U9m)^MNcLq10<84-!gZ4e39pbwE`*LFZcnZCoW?FK2_P50d? z*R}@T7L(Ro(Ehd$_73x~=Oq ziyp$?M9lazIy4j-h!|uLiWv%?hCl;w(wwBudY1sYXIW&1tCtu!#XlW5ONv!CJ2E@ftAA_(Rf`s#8CiTP`v-ONU}|B zw)wkceby3>&>B(zVS2M%KrM|&)74bPzMa*i&iczc#NJX(Mv%PCS!hDDpL zgSY%Ku1CH&v8|c?RD33;tN%|GkF4OY$Y91<(F^L#Rp^sly88!NisAt1Ow=7 zOMu-+5TG2vA^(^E;6{AMbp3t3Ru>rtvf@&I&8I)Q=aIp5I^wL&Cu|e8%W~eg^2*C_^-0N{F>! zvyh+&a*c``-!4TSVvnIse~L=}jM_2WRC1GVi)j)TrDIrevC5LaL<{F2 z5!|W0AKVX4wE*|iwVrI|H-n&ir&Qt;of6MGH*gZTNx5OGEyq0_1Ox zF^znmif_%|B3F4zgP);@l$m^oI+$2FeLIwE{G6UyqvAXjA5f8};!Ww>NzJP|^(T>z zT%ir`Cs8+2UsKNTA?}S}gQi1;-`8mzloVx1!_>DI4?T z1KiigaAmZHhkg#X31Y5RVUPr=zRgLQ14U*cje}g*iVUJWfkQWPzifGDE^?wGF=M^v zvrN-D*-vH}pA8q{CoL2AoK{L7HsPaoYV6hREkA3TbHRZB+}JF{4dU z*;LN$YQ^?3O~Iz394juBm$mUIu4&)Dh*o%d!fw$AlKpz!JGieXHj~fm!nEN8dw3uTPOeM5GfghVCrtXZi6_w= zd~l@C66{u0ZiO>AN5dHmLf4MZNu*8^NWyZkX9{Nql!RYNdlBPO-d^6>)Q>bTze^cB d%ZU_rFJ-1I{L^tpB9%at+e*rrx$PoYl diff --git a/molecule/default/tests/__pycache__/test_default.cpython-36.pyc b/molecule/default/tests/__pycache__/test_default.cpython-36.pyc deleted file mode 100644 index 90bddda29b26c2022b734799893f1445f377f9f7..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 712 zcmZWn&5qMB5VoD9+lFnqAR#Usxb@Hm3B&;*M71i?O5Lus{7CBqQHUp9r%4>y&O+Jk zh4L~y3a{WRCtiUQj!Rc%aWr3M#`E*(lb`ha;mP^r!*h(#PqcSD>%X$5o>^ds;R4a^ zA5#J(_i{fE0OcJ<9-s}*516;Xw99-Jd_%Oy@EbJj{Ib1hPVb+b&ZbB2X4Ck1{(d@t zdwO;rzdoK#jh7TfYB#$>EVg!JO+B@6jdHtpgV*>P=UCm-^qWoERWwmB3;%|&FfK9)k(SpHu`h{P>l34Ef-v_qEZx)HU&hf5KC@dtwBaAg({I; zM3*3W#%)G}(!9!K5?x*GmyUM>V#Xz;TFBLUeY<8iF0PUi{=eQW`DaACFlLY?O`*s2 z%G`Acr%l{S61HM@Eg&3{5D&2ITYnfDA{2diLwDS0UZ` + {{ express_pm2_path }} start dist/index.js + --name {{ express_service_name }} + args: + chdir: "{{ express_app_path }}" - - name: save the current PM2 running list - become: true - become_user: "{{ express_system_user }}" - command: "{{ express_pm2_path }} save" + - name: save the current PM2 running list + become: true + become_user: "{{ express_system_user }}" + command: "{{ express_pm2_path }} save" - - name: delete the old startup file - become: true - become_user: "root" - command: "{{ express_pm2_path }} unstartup -u {{ express_system_user }} --service-name {{ express_service_name }} --hp {{ express_system_user_home }}" - ignore_errors: yes - notify: - - start_express_service + - name: delete the old startup file + become: true + become_user: "root" + command: > + {{ express_pm2_path }} unstartup + -u {{ express_system_user }} + --service-name {{ express_service_name }} + --hp {{ express_system_user_home }} + ignore_errors: true + notify: + - start_express_service - - name: Generate an express PM2 init service file - become: true - become_user: "root" - command: "{{ express_pm2_path }} startup -u {{ express_system_user }} --hp {{ express_system_user_home }} --service-name {{ express_service_name }}" - notify: - - start_express_service + - name: Generate an express PM2 init service file + become: true + become_user: "root" + command: "{{ express_pm2_path }} startup -u {{ express_system_user }} + --hp {{ express_system_user_home }} + --service-name {{ express_service_name }}" + notify: + - start_express_service when: express_service_not_exists diff --git a/tasks/install.yml b/tasks/install.yml index 3bdb63b..91a56a9 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -4,21 +4,24 @@ name: "{{ express_system_user }}" shell: /bin/bash group: "{{ express_system_group }}" - append: yes - create_home: yes + append: true + create_home: true - name: Detect if Node is installed command: node --version register: node_version changed_when: false - ignore_errors: True + ignore_errors: true - name: Download node - shell: set -o pipefail && curl -sL https://deb.nodesource.com/setup_{{ express_node_version }} | sudo -E bash - - become: True + shell: > + set -o pipefail && + curl -sL https://deb.nodesource.com/setup_{{ express_node_version }} + | sudo -E bash - + become: true become_user: root args: - warn: no + warn: false executable: /bin/bash when: node_version.rc != 0 @@ -29,7 +32,7 @@ - name: Add yarn to sources lineinfile: - create: yes + create: true line: "deb https://dl.yarnpkg.com/debian/ stable main" path: /etc/apt/sources.list.d/yarn.list regexp: "deb https://dl.yarnpkg.com/debian/ stable main" @@ -40,20 +43,20 @@ - name: Update apt cache apt: - update_cache: "yes" + update_cache: "true" - name: Install system-wide dependencies apt: name: "{{ express_system_wide_dependencies }}" state: present - update_cache: yes + update_cache: true cache_valid_time: 600 - name: Install PM2 npm: name: pm2 - global: yes - become: True + global: true + become: true become_user: root @@ -74,12 +77,12 @@ - name: Git clone without key git: - accept_hostkey: "yes" + accept_hostkey: "true" repo: "{{ express_git_url }}" dest: "{{ express_checkout_path }}" version: "{{ express_git_version }}" depth: 1 - become: True + become: true become_user: "{{ express_system_user }}" when: - express_git_key is not defined or express_git_key is none @@ -90,20 +93,20 @@ dest: "{{ express_system_user_home }}/.ssh/{{ express_git_key_filename }}" owner: "{{ express_system_user }}" mode: 0600 - no_log: False + no_log: false when: - express_git_key is defined - express_git_key is not none - name: Git clone with key git: - accept_hostkey: "yes" + accept_hostkey: "true" repo: "{{ express_git_url }}" dest: "{{ express_checkout_path }}" version: "{{ express_git_version }}" depth: 1 key_file: "{{ express_system_user_home }}/.ssh/{{ express_git_key_filename }}" - become: True + become: true become_user: "{{ express_system_user }}" when: - express_git_key is defined @@ -113,7 +116,7 @@ file: state: absent path: "{{ express_system_user_home }}/.ssh/{{ express_git_key_filename }}" - become: True + become: true become_user: "{{ express_system_user }}" when: - express_git_key is defined diff --git a/tasks/main.yml b/tasks/main.yml index d5dc616..7edad9e 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -12,4 +12,4 @@ - name: Cleanup include_tasks: cleanup.yml tags: - - cleanup \ No newline at end of file + - cleanup diff --git a/tests/inventory b/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/tests/test.yml b/tests/test.yml new file mode 100644 index 0000000..3e624b1 --- /dev/null +++ b/tests/test.yml @@ -0,0 +1,5 @@ +--- +- hosts: localhost + remote_user: root + roles: + - ansible-express diff --git a/vars/main.yml b/vars/main.yml new file mode 100644 index 0000000..7ee8f2d --- /dev/null +++ b/vars/main.yml @@ -0,0 +1,2 @@ +--- +# vars file for ansible-express From 49d38867c93d8f63bab52f277e191793ceaab619 Mon Sep 17 00:00:00 2001 From: p-netm Date: Tue, 25 Feb 2020 15:35:41 +0300 Subject: [PATCH 14/40] Update readme; now using molecule --- README.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index db86217..8133b25 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,6 @@ Installs and configures an express app built with typescript This role installs and configures: - node -- typescript - yarn - pm2 - and other node packages that the app is dependent on @@ -40,9 +39,15 @@ express_app_settings: Testing ------------ -This project comes with a Vagrantfile, this is a fast and easy way to test changes to the role, fire it up with `vagrant up`. +This project uses molecule for testing using a docker driver and testinfra as the verifier -See [vagrant docs](https://docs.vagrantup.com/v2/) for getting setup with vagrant +Start by creating a virtual environment and install the pypi packages in `requirements.txt` + +Then to run the full test sequence + +```sh +molecule test +``` License ------- @@ -52,4 +57,4 @@ APACHE-2 Author Information ------------------ -[Ona Engineering](https://ona.io) \ No newline at end of file +[Ona Engineering](https://ona.io) From 3c7d96cd65393515740fc108c24217e8450f4b19 Mon Sep 17 00:00:00 2001 From: p-netm Date: Tue, 25 Feb 2020 15:38:54 +0300 Subject: [PATCH 15/40] Add flake8 to required pip packages --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 94fa03a..4bb557a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,4 @@ molecule[docker]==3.0.2 ansible-lint==4.2.0 yamllint==1.20.0 testinfra==4.1.0 - +flake8==3.7.9 \ No newline at end of file From 351283ffa9cc59df60bb65f8cbcf37be9363d62c Mon Sep 17 00:00:00 2001 From: p-netm Date: Tue, 25 Feb 2020 15:45:09 +0300 Subject: [PATCH 16/40] Install dependencies step in github workflow --- .github/workflows/main.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index a914081..534b18d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -16,6 +16,13 @@ jobs: uses: actions/checkout@v2 with: path: "${{ github.repository }}" + + - name: Install dependencies + run: | + sudo apt install docker + python -m pip install --upgrade pip + pip install -r requirements.txt + - name: molecule uses: robertdebock/molecule-action@2.0.0 with: From 7338321c131f37be6bea875d7bca27c9374187c4 Mon Sep 17 00:00:00 2001 From: p-netm Date: Tue, 25 Feb 2020 15:47:47 +0300 Subject: [PATCH 17/40] Trying out different workflow file --- .github/workflows/main.yml | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 534b18d..37e96ae 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,30 +1,30 @@ --- -name: CI +name: Molecule Test -on: - - push +on: [push] jobs: build: + runs-on: ubuntu-latest strategy: + max-parallel: 4 matrix: - image: - - ubuntu + python-version: [3.6] + steps: - - name: checkout - uses: actions/checkout@v2 + - uses: actions/checkout@v1 with: - path: "${{ github.repository }}" - + path: molecule_demo + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v1 + with: + python-version: ${{ matrix.python-version }} - name: Install dependencies run: | sudo apt install docker python -m pip install --upgrade pip pip install -r requirements.txt - - - name: molecule - uses: robertdebock/molecule-action@2.0.0 - with: - image: "${{ matrix.image }}" - options: parallel + - name: Test with molecule + run: | + molecule test \ No newline at end of file From 636e57a6e9ddb14898180989ac991e1214f84ec0 Mon Sep 17 00:00:00 2001 From: p-netm Date: Thu, 27 Feb 2020 12:57:49 +0300 Subject: [PATCH 18/40] Refactor tasks when should run --- tasks/configure.yml | 41 ++++++++++++++++++++--------------------- tasks/install.yml | 1 + 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/tasks/configure.yml b/tasks/configure.yml index e6c6516..26a7d77 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -78,26 +78,25 @@ become: true become_user: "{{ express_system_user }}" command: "{{ express_pm2_path }} save" + when: express_service_not_exists +- name: delete the old startup file + become: true + become_user: "root" + command: > + {{ express_pm2_path }} unstartup + -u {{ express_system_user }} + --service-name {{ express_service_name }} + --hp {{ express_system_user_home }} + ignore_errors: true + changed_when: false - - name: delete the old startup file - become: true - become_user: "root" - command: > - {{ express_pm2_path }} unstartup - -u {{ express_system_user }} - --service-name {{ express_service_name }} - --hp {{ express_system_user_home }} - ignore_errors: true - notify: - - start_express_service - - - name: Generate an express PM2 init service file - become: true - become_user: "root" - command: "{{ express_pm2_path }} startup -u {{ express_system_user }} - --hp {{ express_system_user_home }} - --service-name {{ express_service_name }}" - notify: - - start_express_service - when: express_service_not_exists +- name: Generate an express PM2 init service file + become: true + become_user: "root" + command: "{{ express_pm2_path }} startup -u {{ express_system_user }} + --hp {{ express_system_user_home }} + --service-name {{ express_service_name }}" + notify: + - start_express_service + changed_when: false \ No newline at end of file diff --git a/tasks/install.yml b/tasks/install.yml index 91a56a9..0a8f374 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -86,6 +86,7 @@ become_user: "{{ express_system_user }}" when: - express_git_key is not defined or express_git_key is none + changed_when: false - name: Copy git key copy: From b25933fcc0c34057bb982983aa83f264f219d1f3 Mon Sep 17 00:00:00 2001 From: p-netm Date: Thu, 27 Feb 2020 13:01:08 +0300 Subject: [PATCH 19/40] Fix lint issue --- tasks/configure.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks/configure.yml b/tasks/configure.yml index 26a7d77..d65d688 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -99,4 +99,4 @@ --service-name {{ express_service_name }}" notify: - start_express_service - changed_when: false \ No newline at end of file + changed_when: false From 7b93f481c174bbf0b81fc1139c3fb355345afe09 Mon Sep 17 00:00:00 2001 From: p-netm Date: Thu, 27 Feb 2020 16:08:33 +0300 Subject: [PATCH 20/40] Remove idempotence test from some tasks --- handlers/main.yml | 2 +- molecule/default/tests/test_default.py | 4 ++-- tasks/configure.yml | 16 ++++++++++++---- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/handlers/main.yml b/handlers/main.yml index 6cb482f..3b935e2 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -4,6 +4,6 @@ become_user: root systemd: name: "{{ express_service_name }}" - state: restarted + state: started daemon_reload: true enabled: true diff --git a/molecule/default/tests/test_default.py b/molecule/default/tests/test_default.py index 317590e..148a474 100644 --- a/molecule/default/tests/test_default.py +++ b/molecule/default/tests/test_default.py @@ -64,8 +64,8 @@ def test_installs_js_requirements(host): def test_express_is_running(host): get_local_host = host.run("curl http://localhost:3000") assert ( - (""""message":"ENOENT: no such file or directory, - stat '/home/express/app-versioned/""") + (""""message":"ENOENT: no such file or directory,""" + + """ stat '/home/express/app-versioned/""") in get_local_host.stdout ) diff --git a/tasks/configure.yml b/tasks/configure.yml index d65d688..d3f1f9d 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -15,7 +15,8 @@ state: present path: "{{ item }}" with_items: "{{ express_package_json_paths }}" - changed_when: false + tags: + - molecule-idempotence-notest - name: Compile Javascript command: @@ -34,7 +35,8 @@ force: true owner: "{{ express_system_user }}" group: "{{ express_system_group }}" - changed_when: false + tags: + - molecule-idempotence-notest - name: Restart express pm2 process become: true @@ -79,6 +81,8 @@ become_user: "{{ express_system_user }}" command: "{{ express_pm2_path }} save" when: express_service_not_exists + notify: + - start_express_service - name: delete the old startup file become: true @@ -89,7 +93,10 @@ --service-name {{ express_service_name }} --hp {{ express_system_user_home }} ignore_errors: true - changed_when: false + notify: + - start_express_service + tags: + - molecule-idempotence-notest - name: Generate an express PM2 init service file become: true @@ -99,4 +106,5 @@ --service-name {{ express_service_name }}" notify: - start_express_service - changed_when: false + tags: + - molecule-idempotence-notest \ No newline at end of file From 6862f4c85cce9eef0ed464ff45b4ae6a75354ce9 Mon Sep 17 00:00:00 2001 From: p-netm Date: Thu, 27 Feb 2020 16:09:16 +0300 Subject: [PATCH 21/40] Add new line in configure.yml --- tasks/configure.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks/configure.yml b/tasks/configure.yml index d3f1f9d..75c1bb6 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -107,4 +107,4 @@ notify: - start_express_service tags: - - molecule-idempotence-notest \ No newline at end of file + - molecule-idempotence-notest From 8ebfbbb2e5df06b6ad80411d655c8473de1a3019 Mon Sep 17 00:00:00 2001 From: p-netm Date: Thu, 27 Feb 2020 16:12:21 +0300 Subject: [PATCH 22/40] Format command for task in configure --- tasks/configure.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tasks/configure.yml b/tasks/configure.yml index 75c1bb6..611600d 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -101,9 +101,11 @@ - name: Generate an express PM2 init service file become: true become_user: "root" - command: "{{ express_pm2_path }} startup -u {{ express_system_user }} + command: > + {{ express_pm2_path }} startup + -u {{ express_system_user }} --hp {{ express_system_user_home }} - --service-name {{ express_service_name }}" + --service-name {{ express_service_name }} notify: - start_express_service tags: From 56ae3ad07074f3b805bfa3683adaba136b873645 Mon Sep 17 00:00:00 2001 From: p-netm Date: Thu, 27 Feb 2020 16:15:44 +0300 Subject: [PATCH 23/40] Disable rule 301 for 2 tasks --- tasks/configure.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tasks/configure.yml b/tasks/configure.yml index 611600d..e6e358c 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -96,7 +96,7 @@ notify: - start_express_service tags: - - molecule-idempotence-notest + - molecule-idempotence-notest # noqa 301 - name: Generate an express PM2 init service file become: true @@ -109,4 +109,4 @@ notify: - start_express_service tags: - - molecule-idempotence-notest + - molecule-idempotence-notest # noqa 301 From d2d08a945bb2591e41596a4f89cb7c1c2ae33c22 Mon Sep 17 00:00:00 2001 From: p-netm Date: Thu, 27 Feb 2020 16:46:12 +0300 Subject: [PATCH 24/40] Refactor tasks that can notify handler --- tasks/configure.yml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tasks/configure.yml b/tasks/configure.yml index e6e358c..f222e29 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -62,11 +62,8 @@ become: true become_user: "{{ express_system_user }}" command: "{{ express_pm2_path }} delete all" - notify: - - start_express_service ignore_errors: true - - name: Start express pm2 app become: true become_user: "{{ express_system_user }}" @@ -81,8 +78,6 @@ become_user: "{{ express_system_user }}" command: "{{ express_pm2_path }} save" when: express_service_not_exists - notify: - - start_express_service - name: delete the old startup file become: true From 4c3fecf41e510c418d49e4b56010c23e6cf0ad92 Mon Sep 17 00:00:00 2001 From: p-netm Date: Thu, 27 Feb 2020 16:46:38 +0300 Subject: [PATCH 25/40] Refactor github actions workflow file --- .github/workflows/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 37e96ae..899f0c0 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,7 +15,7 @@ jobs: steps: - uses: actions/checkout@v1 with: - path: molecule_demo + path: "${{ github.repository }}" - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v1 with: @@ -27,4 +27,4 @@ jobs: pip install -r requirements.txt - name: Test with molecule run: | - molecule test \ No newline at end of file + molecule test From 25f912231b6a7f7954b1d073e98eb4e511648b90 Mon Sep 17 00:00:00 2001 From: p-netm Date: Thu, 27 Feb 2020 17:39:26 +0300 Subject: [PATCH 26/40] Refactor idempotence check for 1 task --- tasks/configure.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tasks/configure.yml b/tasks/configure.yml index f222e29..4299803 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -24,7 +24,8 @@ chdir: "{{ express_app_path }}" become: true become_user: "{{ express_system_user }}" - changed_when: false + tags: + - molecule-idempotence-notest # creates a sym-link express_codebase_path; that points to the express checkout path - name: Make the new codebase current From e9de4c8005488a2d8cac9078814c29bcd8788bb2 Mon Sep 17 00:00:00 2001 From: p-netm Date: Fri, 28 Feb 2020 11:44:37 +0300 Subject: [PATCH 27/40] Refactor when task should run --- tasks/configure.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tasks/configure.yml b/tasks/configure.yml index 4299803..6a2932c 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -6,7 +6,9 @@ mode: 0644 owner: "{{ express_system_user }}" group: "{{ express_system_group }}" - changed_when: false + tags: + - molecule-idempotence-notest + changed_when: false # noqa [301] - name: Install Javascript requirements become: true From 41e0d8a42647884736a397f50fbf6396593a240c Mon Sep 17 00:00:00 2001 From: p-netm Date: Fri, 28 Feb 2020 11:48:02 +0300 Subject: [PATCH 28/40] Disable ansible lint for some tasks --- tasks/configure.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tasks/configure.yml b/tasks/configure.yml index 6a2932c..5f045cc 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -8,7 +8,7 @@ group: "{{ express_system_group }}" tags: - molecule-idempotence-notest - changed_when: false # noqa [301] + changed_when: false # noqa 301 - name: Install Javascript requirements become: true @@ -27,7 +27,7 @@ become: true become_user: "{{ express_system_user }}" tags: - - molecule-idempotence-notest + - molecule-idempotence-notest # noqa 301 # creates a sym-link express_codebase_path; that points to the express checkout path - name: Make the new codebase current From e0c793bc3f2ffc3aaa3f594055a89516edd40d9e Mon Sep 17 00:00:00 2001 From: Lincoln Simba Date: Tue, 3 Mar 2020 11:18:46 +0300 Subject: [PATCH 29/40] Update molecule.yml --- molecule/default/molecule.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index 6de3839..fd62ef8 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -5,7 +5,7 @@ driver: name: docker platforms: - name: instance - image: "geerlingguy/docker-ubuntu1604-ansible:latest" + image: "geerlingguy/docker-ubuntu1804-ansible:latest" command: ${MOLECULE_DOCKER_COMMAND:-""} volumes: - /sys/fs/cgroup:/sys/fs/cgroup:ro From 172c4533ac279a086c0e8083c3b01660ce7752cc Mon Sep 17 00:00:00 2001 From: p-netm Date: Tue, 3 Mar 2020 11:23:18 +0300 Subject: [PATCH 30/40] Install gpg-agent in prepare step --- molecule/default/prepare.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/molecule/default/prepare.yml b/molecule/default/prepare.yml index 2ddf58b..8513417 100644 --- a/molecule/default/prepare.yml +++ b/molecule/default/prepare.yml @@ -10,3 +10,4 @@ state: present loop: - curl + - gpg-agent From 82536fed6d5d8a501f2e57c9558230330f05fc86 Mon Sep 17 00:00:00 2001 From: Lincoln Simba Date: Tue, 3 Mar 2020 18:02:24 +0300 Subject: [PATCH 31/40] Improve image name --- molecule/default/molecule.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index fd62ef8..74c5a38 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -5,7 +5,7 @@ driver: name: docker platforms: - name: instance - image: "geerlingguy/docker-ubuntu1804-ansible:latest" + image: "geerlingguy/docker-${MOLECULE_DISTRO:-ubuntu1804}-ansible:latest" command: ${MOLECULE_DOCKER_COMMAND:-""} volumes: - /sys/fs/cgroup:/sys/fs/cgroup:ro From 38462605c09b204afc8d30df3d9452af933f9688 Mon Sep 17 00:00:00 2001 From: Lincoln Simba Date: Tue, 3 Mar 2020 18:26:43 +0300 Subject: [PATCH 32/40] Update main.yml --- defaults/main.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/defaults/main.yml b/defaults/main.yml index 784ec68..b1d2e7c 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -17,6 +17,7 @@ express_system_wide_dependencies: - git - nodejs - yarn + - nodejs-legacy express_node_version: 10.x # app From c2689a9486fda1f6e5d2c15a644c9c7db0c5b665 Mon Sep 17 00:00:00 2001 From: p-netm Date: Fri, 6 Mar 2020 18:43:09 +0300 Subject: [PATCH 33/40] cleanup god Deamon process, its making role fail --- tasks/configure.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tasks/configure.yml b/tasks/configure.yml index 5f045cc..99d0303 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -108,3 +108,10 @@ - start_express_service tags: - molecule-idempotence-notest # noqa 301 + # HACK: this should not be happening + +- name: Cleanup pm2 processes + shell: ps aux | grep PM2 | grep -v grep | awk '{print $2}' | xargs kill -9 + tags: + - molecule-idempotence-notest + From e13fb3654f174adccd3aaf872b139b2b8ad6de08 Mon Sep 17 00:00:00 2001 From: p-netm Date: Fri, 6 Mar 2020 18:50:15 +0300 Subject: [PATCH 34/40] Fix lint --- tasks/configure.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tasks/configure.yml b/tasks/configure.yml index 99d0303..1a6f1bd 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -108,10 +108,8 @@ - start_express_service tags: - molecule-idempotence-notest # noqa 301 - # HACK: this should not be happening -- name: Cleanup pm2 processes +- name: Cleanup pm2 processes # HACK: this should not be happening shell: ps aux | grep PM2 | grep -v grep | awk '{print $2}' | xargs kill -9 tags: - molecule-idempotence-notest - From 884b7a53feac14a76c901df3eaafceeab3ee34cf Mon Sep 17 00:00:00 2001 From: p-netm Date: Fri, 6 Mar 2020 18:56:44 +0300 Subject: [PATCH 35/40] Fix lint --- tasks/configure.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tasks/configure.yml b/tasks/configure.yml index 1a6f1bd..c6b9b3e 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -110,6 +110,8 @@ - molecule-idempotence-notest # noqa 301 - name: Cleanup pm2 processes # HACK: this should not be happening - shell: ps aux | grep PM2 | grep -v grep | awk '{print $2}' | xargs kill -9 + shell: > + set -o pipefail && + ps aux | grep PM2 | grep -v grep | awk '{print $2}' | xargs kill -9 tags: - - molecule-idempotence-notest + - molecule-idempotence-notest # noqa 301 From 82efb5f26843979b0b2edfb7e0580f6a96bb821f Mon Sep 17 00:00:00 2001 From: p-netm Date: Fri, 6 Mar 2020 19:06:10 +0300 Subject: [PATCH 36/40] Ignore set pipefail lint --- tasks/configure.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tasks/configure.yml b/tasks/configure.yml index c6b9b3e..91e73bf 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -110,8 +110,7 @@ - molecule-idempotence-notest # noqa 301 - name: Cleanup pm2 processes # HACK: this should not be happening - shell: > - set -o pipefail && - ps aux | grep PM2 | grep -v grep | awk '{print $2}' | xargs kill -9 + shell: + ps aux | grep PM2 | grep -v grep | awk '{print $2}' | xargs kill -9 # noqa 306 tags: - molecule-idempotence-notest # noqa 301 From 0ed7607f59536edca72de8794e5ae71d44d912d4 Mon Sep 17 00:00:00 2001 From: p-netm Date: Tue, 17 Mar 2020 10:19:39 +0300 Subject: [PATCH 37/40] Change git versionto master in test converge.yml --- molecule/default/converge.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index 0d1f1a8..499d886 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -11,6 +11,6 @@ express_system_user_home: "/home/{{ express_system_user }}" express_node_version: 10.x express_git_url: "https://github.com/onaio/reveal-frontend" - express_git_version: "tiny-backend" + express_git_version: "master" express_app_settings: NODE_ENV: "production" From a3f80c6c045873408e0a6f1c0010510641ed6094 Mon Sep 17 00:00:00 2001 From: p-netm Date: Tue, 17 Mar 2020 11:25:54 +0300 Subject: [PATCH 38/40] Refactor remove extra files task --- defaults/main.yml | 1 + tasks/cleanup.yml | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/defaults/main.yml b/defaults/main.yml index b1d2e7c..e1d0942 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -30,6 +30,7 @@ express_app_path: "{{ express_checkout_path }}/server" express_log_path: "/var/log/{{ express_app_name }}" express_service_name: "{{ express_system_user }}" express_pm2_path: "/usr/bin/pm2" +express_max_versioned_folders: 10 express_package_json_paths: - "{{ express_app_path }}" - "{{ express_checkout_path }}" diff --git a/tasks/cleanup.yml b/tasks/cleanup.yml index 416edd7..27adf0e 100644 --- a/tasks/cleanup.yml +++ b/tasks/cleanup.yml @@ -8,5 +8,5 @@ file: path: "{{ express_versioned_path }}/{{ versioned_list.stdout_lines[item|int] }}" state: absent - with_sequence: start=0 end={{ versioned_list.stdout_lines|length - 10 }} - ignore_errors: true + with_sequence: start=0 end="{{ versioned_list.stdout_lines|length - express_max_versioned_folders - 1 }}" + when: "{{ versioned_list.stdout_lines|length|int > express_max_versioned_folders }}" \ No newline at end of file From bcc09d8394564474840f184220fa708bf2a1db63 Mon Sep 17 00:00:00 2001 From: p-netm Date: Tue, 17 Mar 2020 12:11:20 +0300 Subject: [PATCH 39/40] Change git versionto tag in test converge.yml --- molecule/default/converge.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index 499d886..32977fc 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -11,6 +11,6 @@ express_system_user_home: "/home/{{ express_system_user }}" express_node_version: 10.x express_git_url: "https://github.com/onaio/reveal-frontend" - express_git_version: "master" + express_git_version: "v0.4.0-rc11" express_app_settings: NODE_ENV: "production" From fb93341d7a9231a068641799e441d3dd250ab1c5 Mon Sep 17 00:00:00 2001 From: p-netm Date: Tue, 17 Mar 2020 12:33:38 +0300 Subject: [PATCH 40/40] Refactor remove extra files froma app task --- tasks/cleanup.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tasks/cleanup.yml b/tasks/cleanup.yml index 27adf0e..373fb06 100644 --- a/tasks/cleanup.yml +++ b/tasks/cleanup.yml @@ -5,8 +5,12 @@ changed_when: false - name: Remove extra files from app directory + when: "versioned_list.stdout_lines|length > express_max_versioned_folders" file: path: "{{ express_versioned_path }}/{{ versioned_list.stdout_lines[item|int] }}" state: absent - with_sequence: start=0 end="{{ versioned_list.stdout_lines|length - express_max_versioned_folders - 1 }}" - when: "{{ versioned_list.stdout_lines|length|int > express_max_versioned_folders }}" \ No newline at end of file + # end will be 0 if number of versioned folders is less than the max defined + with_sequence: > + start=0 + end="{{ versioned_list.stdout_lines|length - express_max_versioned_folders - 1 + if versioned_list.stdout_lines|length > express_max_versioned_folders else 0 }}"