forked from minhlh/Ansible-Fabric-Starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
add-new-org.yml
337 lines (263 loc) · 15.6 KB
/
add-new-org.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
---
- hosts: nodes
tasks:
- name: Perfom check for 'newcomers' flag
fail:
msg: "newcomers flag is not defined. Are you sure, you are using right configuration? ..Aborting."
tags:
- safetycheck
when: "newcomers is not defined"
- name: Clean all previous dockers, if any
raw: "docker rm -f $(docker ps -a | grep {{ global_domain }} | awk '{print $1};')"
ignore_errors: true
when: "'newcomer' in node_roles"
- name: Clean all docker volumes
raw: "docker volume rm $(docker volume ls -qf dangling=true)"
ignore_errors: true
when: "'newcomer' in node_roles"
- name: Ensure all old folders not exist
become: yes
file:
dest: "{{ item }}"
state: absent
loop: "{{ fabric_artifacts_folders }}"
when: "'newcomer' in node_roles"
- name: Ensure all folders exist
file:
dest: "{{ item }}"
state: directory
loop: "{{ fabric_artifacts_folders }}"
when: "'newcomer' in node_roles"
- name: Findout UID
raw: "id -u {{ ansible_user }}"
register: ansible_uid
- name: Findout GID
raw: "id -g {{ ansible_user }}"
register: ansible_gid
- set_fact:
ansible_user_uid: "{{ ansible_uid.stdout | int }}"
ansible_user_gid: "{{ ansible_gid.stdout | int }}"
new_orgs: "{% set new_orgs2=[] %}{% for fabric_host in groups['nodes'] %}{% if 'newcomer' in hostvars[fabric_host].node_roles %}{{ new_orgs2.append(hostvars[fabric_host].org)}}{% endif %}{% endfor %}{{ new_orgs2 | list }}"
- name: Transfer common configuration files
template:
src: "{{ item.from }}"
dest: "{{ item.to }}"
backup: yes
loop: "{{ fabric_templates }}"
- name: Transfer peer configuration files
template:
backup: yes
src: "{{ item.from }}"
dest: "{{ item.to }}"
loop: "{{ peer_templates }}"
when: "'peer' in node_roles"
# - name: Transfer orderer configuration files
# template:
# backup: yes
# src: "{{ item.from }}"
# dest: "{{ item.to }}"
# loop: "{{ orderer_templates }}"
# when: "'orderer' in node_roles"
- name: Configure new org
block:
- name: Transfer chaincode
template:
src: "{{ item.from }}"
dest: "{{ item.to }}"
mode: a+x
loop: "{{ chaincode_templates }}"
- name: Start cli container without crypto volumes
raw: "docker-compose --file {{ docker_artifacts }}/docker-compose-{{ org }}.yaml up -d 'cliNoCryptoVolume.{{ org }}.{{ global_domain }}'"
- name: Generate crypto material with cryptogen
raw: "docker exec 'cliNoCryptoVolume.{{ org }}.{{ global_domain }}' bash -c 'sleep 2 && cryptogen generate --config=cryptogen-{{ org }}.yaml'"
- name: Generating orgConfig.json
raw: 'docker exec "cliNoCryptoVolume.{{ org }}.{{ global_domain }}" bash -c "FABRIC_CFG_PATH=./ configtxgen -printOrg {{ org }}MSP > {{ org }}Config.json"'
- name: Changing artifacts ownership
raw: "docker exec 'cliNoCryptoVolume.{{ org }}.{{ global_domain }}' bash -c 'chown -R {{ ansible_user_uid }}:{{ ansible_user_gid }} .'"
- name: Rename generated private keys to server.key
shell: find {{ fabric_artifacts }}/crypto-config -iname "*_sk*" -exec bash -c 'cp $0 $(echo $0 | sed -e "s/[^/]*_sk/server.key/")' {} \;
- name: Create all folders for rsync..
file:
path: "./artifacts/{{ item.to }}"
state: directory
loop: "{{ files_to_rsync_orderer }}"
delegate_to: localhost
- name: Synchronize artifacts
synchronize: src="{{ fabric_artifacts }}/{{ item.from }}" dest="./artifacts/{{ item.to }}" mode=pull recursive=yes
loop: "{{ files_to_rsync_orderer }}"
when: "'peer' in node_roles and 'newcomer' in node_roles"
- name: Sync generated configs to orderer
block:
- name: Synchronize artifacts back to orderer
become: true
synchronize: src="artifacts" dest="{{ fabric_starter_workdir }}" recursive=yes
- name: Transfer orderer configuration files
template:
backup: yes
src: "{{ item.from }}"
dest: "{{ item.to }}"
loop: "{{ orderer_templates }}"
when: "'orderer' in node_roles"
- name: Configure needed channel updates for new orgs
block:
- name: Start cli global_domain container
raw: "docker-compose --file {{ docker_artifacts }}/docker-compose-{{ org }}.yaml up -d 'cli.{{ global_domain }}'"
when: "'root_orderer' in node_roles"
- name: Modifying current channels, if needed
include_tasks: playbooks/modify-channel.yml
loop_control:
loop_var: ch_loop
vars:
mod_channel: "{{ ch_loop.0.name }}"
mod_channel_participants:
- "{{ ch_loop.1 }}"
loop: "{{ global_channels|product(new_orgs)|list }}"
when: "global_channels is defined and ch_loop.1 in ch_loop.0.particapants and org in ch_loop.0.particapants[0]"
- name: Modifying testchainid system channel
import_tasks: playbooks/modify-channel.yml
vars:
mod_channel: testchainid
mod_channel_participants: "{{ new_orgs }}"
when: "'root_peer' in node_roles"
- name: Generating other channels config transactions, if any
raw: 'docker exec -e FABRIC_CFG_PATH=/etc/hyperledger/artifacts "cli.{{ global_domain }}" configtxgen -profile "{{ item.name }}" -outputCreateChannelTx "./channel/{{ item.name }}.tx" -channelID "{{ item.name }}"'
loop: "{{ new_channels }}"
when: "new_channels is defined and 'root_orderer' in node_roles"
when: "'peer' in node_roles"
- hosts: nodes
serial: 1
tasks:
- block:
- name: Start cli container
raw: "docker-compose --file {{ docker_artifacts }}/docker-compose-{{ org }}.yaml up -d 'cli.{{ org }}.{{ global_domain }}'"
- name: Changing ownership of channel block files
raw: "docker exec 'cli.{{ org }}.{{ global_domain }}' bash -c 'chown -R {{ ansible_user_uid }}:{{ ansible_user_gid }} /etc/hyperledger/artifacts'"
- name: Create all folders for rsync..
file:
path: "{{ fabric_artifacts }}/{{ item.to }}"
state: directory
loop: "{{ files_to_rsync_nodes_phase1 }}"
- name: Synchronize genrated block files (artifacts)
become: true
synchronize: src="./artifacts/{{ item.from }}" dest="{{ fabric_artifacts }}/{{ item.to }}" recursive=yes
loop: "{{ files_to_rsync_nodes_phase1 }}"
# - name: Sign testchainid channel update by all orgs
# raw: 'docker exec cli.{{ org }}.{{ global_domain }} bash -c "export CORE_PEER_ADDRESS=peer0.{{ org }}.{{ global_domain }}:7051 && peer channel signconfigtx -f /etc/hyperledger/artifacts/channel/config_testchainid_new_envelope.pb -o orderer{{ orderer_id | default() }}.{{ global_domain }}:{{ orderer_port }} --tls --cafile /etc/hyperledger/artifacts/crypto-config/ordererOrganizations/{{ global_domain }}/orderers/orderer{{ orderer_id | default() }}.{{ global_domain }}/tls/ca.crt"'
# when: "'peer' in node_roles"
- name: Sign other channel update by appropriate orgs
raw: 'docker exec cli.{{ org }}.{{ global_domain }} bash -c "export CORE_PEER_ADDRESS=peer0.{{ org }}.{{ global_domain }}:7051 && peer channel signconfigtx -f /etc/hyperledger/artifacts/channel/config_{{ item.0.name }}_new_envelope.pb -o orderer0.{{ global_domain }}:{{ orderer_port }} --tls --cafile /etc/hyperledger/artifacts/crypto-config/ordererOrganizations/{{ global_domain }}/tlsca/tlsca.{{ global_domain }}-cert.pem"'
loop: "{{ global_channels | product(new_orgs) | list }}"
when: "global_channels is defined and org in item.0.particapants and item.1 in item.0.particapants"
- name: Changing ownership of channel block files
raw: 'docker exec "cli.{{ org }}.{{ global_domain }}" bash -c "chown -R {{ ansible_user_uid }}:{{ ansible_user_gid }} /etc/hyperledger/artifacts"'
- name: Synchronize genrated block files (artifacts)
become: true
synchronize: src="{{ fabric_artifacts }}/{{ item.from }}" dest="./artifacts/{{ item.to }}" mode=pull recursive=yes
loop: "{{ files_to_rsync_nodes_phase1 }}"
when: "'peer' in node_roles"
- hosts: nodes
tasks:
- name: Start cli container
raw: "docker-compose --file {{ docker_artifacts }}/docker-compose-{{ org }}.yaml up -d 'cli.{{ org }}.{{ global_domain }}'"
- name: Changing ownership of channel block files
raw: 'docker exec "cli.{{ org }}.{{ global_domain }}" bash -c "chown -R {{ ansible_user_uid }}:{{ ansible_user_gid }} /etc/hyperledger/artifacts"'
when: "'peer' in node_roles"
- name: Synchronize genrated block files (artifacts)
become: true
synchronize: src="./artifacts/{{ item.from }}" dest="{{ fabric_artifacts }}/{{ item.to }}" recursive=yes
loop: "{{ files_to_rsync_nodes_phase1 }}"
when: "'peer' in node_roles"
- name: Updating testchainid channel with new config..
raw: 'docker-compose -f {{ docker_artifacts }}/docker-compose-{{ org }}.yaml run --rm cli.{{ global_domain }} bash -c "sleep 3 && peer channel update -f /etc/hyperledger/artifacts/channel/config_testchainid_new_envelope.pb -o orderer{{ orderer_id | default() }}.{{ global_domain }}:{{ orderer_port }} -c testchainid --tls --cafile /etc/hyperledger/artifacts/crypto-config/ordererOrganizations/{{ global_domain }}/orderers/orderer{{ orderer_id | default() }}.{{ global_domain }}/tls/ca.crt"'
when: "'root_orderer' in node_roles"
- name: Updating other channels with new config, if any..
raw: 'docker exec cli.{{ org }}.{{ global_domain }} bash -c "export CORE_PEER_ADDRESS=peer0.{{ org }}.{{ global_domain }}:7051 && peer channel update -f /etc/hyperledger/artifacts/channel/config_{{ item.0.name }}_new_envelope.pb -o orderer{{ orderer_id | default() }}.{{ global_domain }}:{{ orderer_port }} -c {{ item.0.name }} --tls --cafile /etc/hyperledger/artifacts/crypto-config/ordererOrganizations/{{ global_domain }}/orderers/orderer{{ orderer_id | default() }}.{{ global_domain }}/tls/ca.crt"'
loop: "{{ global_channels | product(new_orgs) | list }}"
when: "'peer' in node_roles and global_channels is defined and org in item.0.particapants[0] and item.1 in item.0.particapants"
# - name: Restarting peer docker containers
# block:
#
# - name: Restart peer conatainers
# raw: "{{ item }}"
# loop:
# - "docker-compose -f {{ docker_artifacts }}/docker-compose-{{ global_domain }}.yaml down || true"
# - "sleep 5 && docker-compose -f {{ docker_artifacts }}/docker-compose-{{ global_domain }}.yaml up -d 2>&1"
#
# when: "'peer' in node_roles and 'newcomer' not in node_roles"
- name: Bootstrap new orgs
block:
- name: Changing artifacts ownership
raw: "docker exec 'cliNoCryptoVolume.{{ org }}.{{ global_domain }}' bash -c 'chown -R {{ ansible_user_uid }}:{{ ansible_user_gid }} .'"
- name: Create all folders for rsync..
file:
path: "{{ fabric_artifacts }}/{{ item.to }}"
state: directory
loop: "{{ files_to_rsync_nodes_phase1 }}"
when: "orderer_count is not defined or orderer_count <= 1"
- name: Synchronize genrated block files (artifacts)
become: true
synchronize: src="./artifacts/{{ item.from }}" dest="{{ fabric_artifacts }}/{{ item.to }}" recursive=yes
loop: "{{ files_to_rsync_nodes_phase1 }}"
- name: Synchronize www-client folder
synchronize: src="www-client" dest="{{ fabric_starter_workdir }}" recursive=yes
# - name: Synchronize middleware folder
# synchronize: src="middleware" dest="{{ fabric_starter_workdir }}" recursive=yes
- name: Start docker containers
raw: "docker-compose -f {{ docker_artifacts }}/docker-compose-{{ org }}.yaml up -d 2>&1"
- name: Generate init-script..
lineinfile:
path: "{{ fabric_starter_workdir }}/start-node.sh"
line: "docker-compose -f {{ docker_artifacts }}/docker-compose-{{ org }}.yaml up -d 2>&1"
create: yes
- name: Generate down-script..
lineinfile:
path: "{{ fabric_starter_workdir }}/stop-node.sh"
insertbefore: BOF
line: "docker-compose -f {{ docker_artifacts }}/docker-compose-{{ org }}.yaml down"
create: yes
- name: Install chaincode
include_tasks: playbooks/install-chaincode.yaml
loop: "{{ global_channels }} + {{ new_channels }}"
when: "'peer' in node_roles and 'newcomer' in node_roles"
- name: Creating addtional channels, if any
block:
- name: Creating other channels
raw: 'docker exec "cli.{{ org }}.{{ global_domain }}" bash -c "peer channel create -o orderer0.{{ global_domain }}:{{ orderer_port }} -c {{ item.name }} -f /etc/hyperledger/artifacts/channel/{{ item.name }}.tx --tls --cafile /etc/hyperledger/artifacts/crypto-config/ordererOrganizations/{{ global_domain }}/tlsca/tlsca.{{ global_domain }}-cert.pem"'
loop: "{{ new_channels }}"
when: "org in item.particapants[0]"
- name: Changing ownership of channel block files
raw: 'docker exec "cli.{{ org }}.{{ global_domain }}" bash -c "chown -R {{ ansible_user_uid }}:{{ ansible_user_gid }} ."'
- name: Synchronize genrated block files (artifacts)
synchronize: src="{{ fabric_artifacts }}/*.block" dest="./artifacts/" mode=pull recursive=yes
loop: "{{ new_channels }}"
when: "org in item.particapants[0]"
- name: Synchronize genrated block files (artifacts)
become: true
synchronize: src="artifacts" dest="{{ fabric_starter_workdir }}" recursive=yes
- name: Joining other channels
raw: 'docker exec "cli.{{ org }}.{{ global_domain }}" bash -c "export CORE_PEER_ADDRESS=peer0.{{ org }}.{{ global_domain }}:7051 && peer channel join -b {{ item.name }}.block"'
loop: "{{ new_channels }}"
when: "org in item.particapants"
- name: Instantiating chaincode for other channels, if any
raw: "docker exec cli.{{ org }}.{{ global_domain }} bash -c 'export CORE_PEER_ADDRESS=peer0.{{ org }}.{{ global_domain }}:7051 && peer chaincode instantiate -n {{ item.chaincode.name }} -v {{ item.chaincode.version }} -c {{ item.chaincode.init | to_json }} -o orderer0.{{ global_domain }}:{{ orderer_port }} -C {{ item.name }} --tls --cafile /etc/hyperledger/artifacts/crypto-config/ordererOrganizations/{{ global_domain }}/tlsca/tlsca.{{ global_domain }}-cert.pem'"
loop: "{{ new_channels }}"
when: "org in item.particapants[0]"
- name: Changing ownership of channel block files
raw: 'docker exec "cli.{{ org }}.{{ global_domain }}" bash -c "chown -R {{ ansible_user_uid }}:{{ ansible_user_gid }} ."'
when: "new_channels is defined and 'peer' in node_roles"
- name: Joining channels by new nodes
block:
- name: Synchronize genrated block files (artifacts)
become: true
synchronize: src="artifacts" dest="{{ fabric_starter_workdir }}" recursive=yes
- name: Fetch other channels block
raw: 'docker exec "cli.{{ org }}.{{ global_domain }}" bash -c "export CORE_PEER_ADDRESS=peer0.{{ org }}.{{ global_domain }}:7051 && peer channel fetch 0 {{ item.name }}.block -o orderer0.{{ global_domain }}:7050 -c {{ item.name }} --tls --cafile /etc/hyperledger/artifacts/crypto-config/ordererOrganizations/{{ global_domain }}/tlsca/tlsca.{{ global_domain }}-cert.pem"'
loop: "{{ global_channels }}"
when: "org in item.particapants"
- name: Joining other channels
raw: 'docker exec "cli.{{ org }}.{{ global_domain }}" bash -c "export CORE_PEER_ADDRESS=peer0.{{ org }}.{{ global_domain }}:7051 && peer channel join -b {{ item.name }}.block"'
loop: "{{ global_channels }}"
when: "org in item.particapants"
- name: Changing ownership of channel block files
raw: 'docker exec "cli.{{ org }}.{{ global_domain }}" bash -c "chown -R {{ ansible_user_uid }}:{{ ansible_user_gid }} ."'
when: "'peer' in node_roles and 'newcomer' in node_roles"