summaryrefslogtreecommitdiff
path: root/roles/apps/node-red/instance/tasks/main.yml
blob: 63d0353928d23192178c96a9b21e618c71a9a690 (plain) (blame)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
---
- name: prepare storage volume
  vars:
    storage_volume: "{{ node_red_instances[node_red_instance].storage }}"
  include_role:
    name: "storage/{{ node_red_instances[node_red_instance].storage.type }}/volume"

- set_fact:
    node_red_instance_basepath: "{{ storage_volume_mountpoint }}"

- name: create instance directories
  loop:
  - data
  - tls
  file:
    path: "{{ node_red_instance_basepath }}/{{ item }}"
    state: directory
    owner: 1000
    mode: 0700

- name: generate/install/fetch TLS certificates for mqtt
  when: "'mqtt_tls' in node_red_instances[node_red_instance]"
  vars:
    x509_certificate_name: "node-red-{{ node_red_instance }}_mqtt"
    x509_certificate_hostnames: []
    x509_certificate_config: "{{ node_red_instances[node_red_instance].mqtt_tls.certificate_config }}"
    x509_certificate_renewal:
      install:
      - dest: "{{ node_red_instance_basepath }}/tls/mqtt-crt.pem"
        src:
        - fullchain
        owner: root
        group: 1000
        mode: "0644"
      - dest: "{{ node_red_instance_basepath }}/tls/mqtt-key.pem"
        src:
        - key
        owner: root
        group: 1000
        mode: "0640"
      - dest: "{{ node_red_instance_basepath }}/tls/mqtt-ca-crt.pem"
        src:
        - ca_cert
        owner: root
        group: 1000
        mode: "0644"
  include_role:
    name: "x509/{{ node_red_instances[node_red_instance].mqtt_tls.certificate_provider }}/cert"

- name: generate/install TLS certificates for publishment
  vars:
    x509_certificate_name: "node-red-{{ node_red_instance }}_publish"
    x509_certificate_hostnames: []
    x509_certificate_config:
      ca: "{{ node_red_instances[node_red_instance].publish.zone.certificate_ca_config }}"
      cert:
        common_name: "node-red-{{ node_red_instance }}.{{ inventory_hostname }}"
        extended_key_usage:
        - serverAuth
        extended_key_usage_critical: yes
        create_subject_key_identifier: yes
        not_after: +100w
    x509_certificate_renewal:
      install:
      - dest: "{{ node_red_instance_basepath }}/tls/publish-crt.pem"
        src:
        - fullchain
        owner: root
        group: 1000
        mode: "0644"
      - dest: "{{ node_red_instance_basepath }}/tls/publish-key.pem"
        src:
        - key
        owner: root
        group: 1000
        mode: "0640"
      - dest: "{{ node_red_instance_basepath }}/tls/publish-ca-crt.pem"
        src:
        - ca_cert
        owner: root
        group: 1000
        mode: "0644"
  include_role:
    name: "x509/{{ node_red_instances[node_red_instance].publish.zone.certificate_provider }}/cert"

- name: build custom image
  when: "'custom_image' in node_red_instances[node_red_instance]"
  block:
  - name: create build directory for custom image
    file:
      path: "{{ node_red_instance_basepath }}/build"
      state: directory

  - name: generate Dockerfile for custom image
    copy:
      content: |
        FROM {{ node_red_instances[node_red_instance].custom_image.from | default('nodered/node-red:' + node_red_instances[node_red_instance].version + '-debian') }}

        {{ node_red_instances[node_red_instance].custom_image.dockerfile }}
      dest: "{{ node_red_instance_basepath }}/build/Dockerfile"
    register: node_red_custom_image_docker

  - name: build custom image
    docker_image:
      name: "nodered/node-red/{{ node_red_instance }}:{{ node_red_instances[node_red_instance].version }}-debian"
      state: present
      force_source: "{{ node_red_custom_image_docker is changed }}"
      source: build
      build:
        path: "{{ node_red_instance_basepath }}/build"
        network: host
        pull: yes

- name: test if settings.js already exists
  stat:
    path: "{{ node_red_instance_basepath }}/data/settings.js"
  register: node_red_settings_js

- name: install pod manifest
  vars:
    kubernetes_standalone_pod:
      name: "node-red-{{ node_red_instance }}"
      spec: "{{ lookup('template', 'pod-spec.yml.j2') }}"
  include_role:
    name: kubernetes/standalone/pod

- name: configure nginx vhost for publishment
  vars:
    nginx_vhost__yaml: |
      name: "node-red-{{ node_red_instance }}.{{ inventory_hostname }}"
      template: generic
      {% if 'tls' in node_red_instances[node_red_instance].publish %}
      tls:
        {{ node_red_instances[node_red_instance].publish.tls | to_nice_yaml(indent=2) | indent(2) }}
      {% endif %}
      hostnames:
      {% for hostname in node_red_instances[node_red_instance].publish.hostnames %}
      - {{ hostname }}
      {% endfor %}
      locations:
        '/':
      {% if node_red_instances[node_red_instance].publish.zone.publisher == inventory_hostname %}
          proxy_pass: "https://127.0.0.1:{{ node_red_instances[node_red_instance].port }}"
      {% else %}
          proxy_pass: "https://{{ ansible_default_ipv4.address }}:{{ node_red_instances[node_red_instance].port }}"
      {% endif %}
          proxy_ssl:
            certificate: "/etc/ssl/apps-publish-{{ node_red_instances[node_red_instance].publish.zone.name }}/apps-publish-{{ node_red_instances[node_red_instance].publish.zone.name }}-crt.pem"
            certificate_key: "/etc/ssl/apps-publish-{{ node_red_instances[node_red_instance].publish.zone.name }}/apps-publish-{{ node_red_instances[node_red_instance].publish.zone.name }}-key.pem"
            trusted_certificate: "/etc/ssl/apps-publish-{{ node_red_instances[node_red_instance].publish.zone.name }}/apps-publish-{{ node_red_instances[node_red_instance].publish.zone.name }}-ca-crt.pem"
            verify: "on"
            name: "node-red-{{ node_red_instance }}.{{ inventory_hostname }}"
            protocols: "TLSv1.3"
      {% if 'location_extra_directives' in node_red_instances[node_red_instance].publish %}
          extra_directives: |
            {{ node_red_instances[node_red_instance].publish.location_extra_directives | indent(6) }}
      {% endif %}
      {% if 'vhost_extra_directives' in node_red_instances[node_red_instance].publish %}
      extra_directives: |
        {{ node_red_instances[node_red_instance].publish.vhost_extra_directives | indent(2) }}
      {% endif %}
    nginx_vhost: "{{ nginx_vhost__yaml | from_yaml }}"
  include_role:
    name: nginx/vhost
    apply:
      delegate_to: "{{ node_red_instances[node_red_instance].publish.zone.publisher }}"


- name: print info for new instance
  when: not node_red_settings_js.stat.exists
  pause:
    seconds: 5
    prompt: |
      ************* {{ node_red_instance }} is a new instance
      **
      **  Wait for default settings.js to be populated and then add the following options:

          credentialSecret: "{{ node_red_instances[node_red_instance].credential_secret }}",

          https: {
            key: require("fs").readFileSync('/tls/publish-key.pem'),
            cert: require("fs").readFileSync('/tls/publish-crt.pem'),
            ca: require("fs").readFileSync('/tls/publish-ca-crt.pem'),
            requestCert: true,
            minVersion: 'TLSv1.3'
          },
          {% if 'extra_settings' in node_red_instances[node_red_instance] %}

          {{ node_red_instances[node_red_instance].extra_settings | indent(4) }}
          {% endif %}

      **
      ****************************************