Skip to content

Commit

Permalink
feat: improve app management
Browse files Browse the repository at this point in the history
To keep things simple, this role currently deletes all apps on every run
and starts the configured ones. This ensures, that no obsolete apps are running.

New variables:

* `pm2_cmds` run pm2 commands before managing apps
* `pm2_cmds_default_env` default env to use for commands
* `pm2_apps_default_env` default env to use for apps
* `pm2_apps_default_cmd` default cmd to use for apps

See README and tests/ for more details.

closes #16
  • Loading branch information
franklinkim committed Dec 23, 2016
1 parent e903eba commit 0c51521
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
22 changes: 22 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
---
# pm2_cmd:
# - run: sendSignal # pm2 command name
# args: SIGUSR2 my-app # optional arguements to pass
# path: /var/www/myapp # optional chdir path
# ignore_errors: yes # optional don't fail on pm2 errors
# env: # optional environment settings
# NODE_ENV: production
# pm2_apps:
# - run: pm2.json # you can also run a .js file like app.js
# cmd: start # optional command to run on the app
# args: --name console_error # optional arguements to pass i.e. to app.js
# path: /var/www/myapp # optional chdir path
# env: # optional environment settings
# NODE_ENV: production
#


# list of commands to run
# note: these will be executed before managing apps
pm2_cmds:
# note: delete all apps initially on every run so only configured apps exist
- run: delete all
# default env to run on cmds
pm2_cmds_default_env: {}
# list of paths to JSON app declarations
pm2_apps: []
# default env to run on apps
pm2_apps_default_env: {}
# default command to run on apps
pm2_apps_default_cmd: start
# delete all initially on every run
pm2_apps_delete_all: yes
# service name for startup system
pm2_service_name: pm2-init.sh
# start on boot
Expand Down
21 changes: 14 additions & 7 deletions tasks/manage.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
---

- name: Deleting all apps
shell: pm2 delete all
register: command_result
failed_when: "'[ERROR]' in command_result.stderr"
- name: Executing commands
environment: "{{ item.env | default(pm2_cmds_default_env) }}"
shell: "pm2 {{ item.run }} {{ item.args | default() }}"
register: pm2_cmd_result
args:
chdir: "{{ item.path | default(omit) }}"
failed_when: "item.ignore_errors|default(false) == false and ('[ERROR]' in pm2_cmd_result.stderr)"
# ignore_errors: "item.ignore_errors|default(false)" this doesn't work!?
with_items: "{{ pm2_cmds }}"

- name: Starting apps
environment: "{{ item.env | default({}) }}"
shell: "pm2 start {{ item.run }} {{ item.args | default() }}"
- name: Managing apps
environment: "{{ item.env | default(pm2_apps_default_env) }}"
shell: "pm2 {{ item.cmd | default(pm2_apps_default_cmd) }} {{ item.run }} {{ item.args | default() }}"
register: pm2_app_result
args:
chdir: "{{ item.path | default(omit) }}"
failed_when: "'[ERROR]' in pm2_app_result.stderr"
with_items: "{{ pm2_apps }}"
17 changes: 15 additions & 2 deletions tests/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,30 @@
mode: "0777"
- name: Installing sources
command: /tmp/setup_nodejs
args:
creates: /usr/local/bin/node
- name: Installing packages
action: "{{ ansible_pkg_mgr }} pkg=nodejs state=present"
action: "{{ ansible_pkg_mgr }}"
args:
pkg: nodejs
state: present
roles:
- weareinteractive.pm2
vars:
pm2_cmds:
- run: delete
args: console_error
#ignore_errors: yes
pm2_apps:
- run: apps.json
path: "/etc/ansible/roles/weareinteractive.pm2/tests"
cmd: startOrGracefulReload
- run: console_error.js
args: --name console_error
path: "/etc/ansible/roles/weareinteractive.pm2/tests/apps"
cmd: start
env:
NODE_ENV: dev
pm2_startup: ubuntu
pm2_service_name: pm2
pm2_apps_default_env:
NODE_ENV: production

0 comments on commit 0c51521

Please sign in to comment.