-
Notifications
You must be signed in to change notification settings - Fork 2
/
bootstrap_nodered.yml
133 lines (121 loc) · 5.24 KB
/
bootstrap_nodered.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# SPDX-License-Identifier: AGPL-3.0-only
#
# Komponist - Generate Your Favourite Compose Stack With the Least Effort
#
# Copyright (C) 2023 Shantanoo "Shan" Desai <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# bootstrap_nodered.yml: Ansible Playbook that installs External Nodes
# into running node-red (either offline or online mode)
---
- name: Bootstrap Node-RED instance with Database Nodes
hosts: localhost
vars_files:
- vars/config.yml
- vars/creds.yml
module_defaults:
ansible.builtin.uri:
method: POST
headers:
Content-Type: application/json
tasks:
- name: Gather Container Facts for Checks
community.docker.docker_container_info:
name: "{{ komponist.project_name }}_nodered"
register: nodered_container_info
tags: [always]
- name: Assert that Node-RED Container is Running
ansible.builtin.assert:
that: nodered_container_info.container.State.Status == 'running'
fail_msg: "FAIL: trying to bootstrap Node-RED container but it is not running."
success_msg: "PASS: Node-RED container running."
tags: [always]
- name: Get Node-RED user with Read-Write Permissions
ansible.builtin.set_fact:
nodered_creds: "{{ item }}"
when: "item.permissions is defined and item['permissions'] == '*'"
loop: "{{ credentials.nodered.users }}"
no_log: true
tags: [always]
- name: Obtain Node-RED Authentication Token
ansible.builtin.uri:
url: http://localhost/nodered/auth/token
body:
client_id: node-red-admin
grant_type: password
scope: "{{ nodered_creds.permissions }}"
username: "{{ nodered_creds.username }}"
password: "{{ nodered_creds.password }}"
body_format: json
status_code: 200
register: auth_token
when: nodered_creds is defined
tags: [always]
- name: (Online) Read `nodes.json` File for Bootstrapping Node-RED
ansible.builtin.set_fact:
external_nodes: "{{ lookup('ansible.builtin.file', '{{ komponist.deploy_dir}}/nodered/nodes.json') }}"
tags: [online]
- name: (Online) Bootstrap Node-RED with External Nodes using nodes.json
ansible.builtin.uri:
url: http://localhost/nodered/nodes
headers:
Authorization: "{{ auth_token.json.token_type }} {{ auth_token.json.access_token }}"
body:
module: "{{ item.module }}"
version: "{{ item.version }}"
body_format: json
status_code:
- 200
- 400
loop: "{{ external_nodes }}"
tags: [online]
- name: (Offline) Obtain information of External Nodes tarballs
ansible.builtin.find:
paths: "{{ komponist.deploy_dir }}/nodered/"
patterns:
- '*.tgz'
register: node_tarballs
tags: [never, offline]
- name: Check for existing tarballs
ansible.builtin.assert:
that: "{{ node_tarballs.files | length != 0 }}"
fail_msg: "Cannot find any node tarballs with .tgz suffix in {{ komponist.deploy_dir }}/nodered"
success_msg: "Found tarballs for external nodes to upload in {{ komponist.deploy_dir }}/nodered"
tags: [never, offline]
# NOTE: Uploading via uri Module with multipart/form-data body and HTTP POST
# throws unknown errors from node-RED side. Chances are uri module does base64 encoding
# of the tarball which the node-RED backend is unable to decode causing errors.
# Only solution at the moment is to use curl with the command module.
- name: (Offline) Bootstap Node-RED with External Nodes via tarballs
ansible.builtin.command: # noqa: command-instead-of-module
cmd: >
curl -s -XPOST http://localhost/nodered/nodes -H "Content-Type: multipart/form-data" \
-H "Authorization: {{ auth_token.json.token_type }} {{ auth_token.json.access_token }}" \
-F "tarball=@{{ item.path }};type=application/gzip;filename={{ item.path | basename }}"
with_items: "{{ node_tarballs.files }}"
register: curl_post_output
failed_when: curl_post_output.stdout == 'Unauthorized'
changed_when: curl_post_output.stdout != '{"code":"module_already_loaded","message":"Module already loaded"}'
tags: [never, offline]
- name: Revoke Authentication Token
ansible.builtin.uri:
url: http://localhost/nodered/auth/revoke
headers:
Authorization: "{{ auth_token.json.token_type }} {{ auth_token.json.access_token }}"
body:
token: "{{ auth_token.json.access_token }}"
body_format: json
status_code: 200
tags: [always]