forked from minhlh/Ansible-Fabric-Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-new-org.yml
400 lines (313 loc) · 20.3 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
---
#- hosts: localhost
# tasks:
# - name: Clean local artifacts folder
# file:
# path: artifacts
# state: "{{ item }}"
# loop:
# - "absent"
# - "directory"
- 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 {{ 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: Trnasfer chaincode
synchronize:
src: "templates/chaincode"
dest: "{{ fabric_starter_workdir }}"
recursive: yes
when: "'newcomer' in node_roles"
- name: Generate crypto material with cryptogen
raw: "docker-compose exec 'cliNoCryptoVolume.{{ org }}.{{ domain }}' bash -c 'sleep 2 && cryptogen generate --config=cryptogen-{{ org }}.yaml'"
- name: Changing artifacts ownership
raw: "docker-compose exec 'cliNoCryptoVolume.{{ org }}.{{ domain }}' bash -c 'chown -R {{ ansible_user_uid }}:{{ ansible_user_gid }} .'"
- name: Adding generated CA private keys filenames
raw: 'sed -i -e "s/CA_PRIVATE_KEY/$(basename `ls -t {{ fabric_artifacts }}/crypto-config/peerOrganizations/{{ org }}.{{ domain }}/ca/*_sk`)/g" {{ docker_artifacts }}/docker-compose-{{ org }}.yaml'
- name: Generating orgConfig.json
raw: 'docker-compose --file {{ docker_artifacts }}/docker-compose-{{ org }}.yaml run --rm "cliNoCryptoVolume.{{ org }}.{{ domain }}" bash -c "FABRIC_CFG_PATH=./ configtxgen -printOrg {{ org }}MSP > {{ org }}Config.json"'
- 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: Updating crypto material with cryptogen
raw: 'docker-compose --file {{ docker_artifacts }}/docker-compose-{{ domain }}.yaml run --rm cli.{{ domain }} bash -c "cryptogen extend --config=cryptogen-{{ domain }}.yaml"'
when: "'root_orderer' in node_roles"
- name: Modifying common channel
import_tasks: playbooks/modify-channel.yml
vars:
mod_channel: "{{ common_channel_name }}"
mod_channel_participants: "{{ new_orgs }}"
when: "'root_peer' in node_roles"
- name: Modifying other 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 | list }}"
loop: "{{ additional_channels|product(new_orgs)|list }}"
when: "additional_channels is defined and ch_loop.1 in ch_loop.0.particapants and org in ch_loop.0.particapants[0]"
- name: Modifying testchainid 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-compose --file {{ docker_artifacts }}/docker-compose-{{ domain }}.yaml run --rm "cli.{{ 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: Changing ownership of channel block files
raw: 'docker-compose exec "cli.{{ 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 common channel update by all orgs
raw: 'docker-compose exec cli.{{ org }}.{{ domain }} bash -c "export CORE_PEER_ADDRESS=peer0.{{ org }}.{{ domain }}:7051 && peer channel signconfigtx -f /etc/hyperledger/artifacts/channel/config_{{ common_channel_name }}_new_envelope.pb -o orderer{{ orderer_id | default() }}.{{ domain }}:{{ orderer_port }} --tls --cafile /etc/hyperledger/artifacts/crypto-config/ordererOrganizations/{{ domain }}/orderers/orderer{{ orderer_id | default() }}.{{ domain }}/tls/ca.crt"'
when: "'root_peer' not in node_roles"
- name: Sign other channel update by appropriate orgs
raw: 'docker-compose exec cli.{{ org }}.{{ domain }} bash -c "export CORE_PEER_ADDRESS=peer0.{{ org }}.{{ domain }}:7051 && peer channel signconfigtx -f /etc/hyperledger/artifacts/channel/config_{{ item.0.name }}_new_envelope.pb -o orderer{{ orderer_id | default() }}.{{ domain }}:{{ orderer_port }} --tls --cafile /etc/hyperledger/artifacts/crypto-config/ordererOrganizations/{{ domain }}/orderers/orderer{{ orderer_id | default() }}.{{ domain }}/tls/ca.crt"'
loop: "{{ additional_channels | product(new_orgs) | list }}"
when: "additional_channels is defined and org in item.0.particapants and org not in item.0.particapants[0] and item.1 in item.0.particapants"
- name: Changing ownership of channel block files
raw: 'docker-compose exec "cli.{{ 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 and 'newcomer' not in node_roles"
- hosts: nodes
tasks:
- name: Changing ownership of channel block files
raw: 'docker-compose exec "cli.{{ 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 start cli.{{ domain }} && sleep 3 && docker exec cli.{{ domain }} bash -c "peer channel update -f /etc/hyperledger/artifacts/channel/config_testchainid_new_envelope.pb -o orderer{{ orderer_id | default() }}.{{ domain }}:{{ orderer_port }} -c testchainid --tls --cafile /etc/hyperledger/artifacts/crypto-config/ordererOrganizations/{{ domain }}/orderers/orderer{{ orderer_id | default() }}.{{ domain }}/tls/ca.crt"'
when: "'root_orderer' in node_roles"
- name: Updating common channel with new config..
raw: 'docker exec cli.{{ org }}.{{ domain }} bash -c "export CORE_PEER_ADDRESS=peer0.{{ org }}.{{ domain }}:7051 && peer channel update -f /etc/hyperledger/artifacts/channel/config_{{ common_channel_name }}_new_envelope.pb -o orderer{{ orderer_id | default() }}.{{ domain }}:{{ orderer_port }} -c {{ common_channel_name }} --tls --cafile /etc/hyperledger/artifacts/crypto-config/ordererOrganizations/{{ domain }}/orderers/orderer{{ orderer_id | default() }}.{{ domain }}/tls/ca.crt"'
when: "'root_peer' in node_roles"
- name: Updating other channel with new config, if any..
raw: 'docker exec cli.{{ org }}.{{ domain }} bash -c "export CORE_PEER_ADDRESS=peer0.{{ org }}.{{ domain }}:7051 && peer channel update -f /etc/hyperledger/artifacts/channel/config_{{ item.0.name }}_new_envelope.pb -o orderer{{ orderer_id | default() }}.{{ domain }}:{{ orderer_port }} -c {{ item.0.name }} --tls --cafile /etc/hyperledger/artifacts/crypto-config/ordererOrganizations/{{ domain }}/orderers/orderer{{ orderer_id | default() }}.{{ domain }}/tls/ca.crt"'
loop: "{{ additional_channels | product(new_orgs) | list }}"
when: "'peer' in node_roles and additional_channels is defined and org in item.0.particapants[0] and item.1 in item.0.particapants"
- name: Restarting orderer docker containers
block:
- name: Start orderer
raw: "docker-compose -f {{ docker_artifacts }}/docker-compose-{{ domain }}.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-{{ domain }}.yaml up -d 2>&1"
create: yes
when: "'newcomer' in node_roles"
- name: Generate down-script..
lineinfile:
path: "{{ fabric_starter_workdir }}/stop-node.sh"
insertbefore: BOF
line: "docker-compose -f {{ docker_artifacts }}/docker-compose-{{ domain }}.yaml down"
create: yes
when: "'newcomer' in node_roles"
when: "'orderer' in node_roles"
- name: Restarting peer docker containers
block:
- name: Restart peer conatainers
raw: "docker-compose -f {{ docker_artifacts }}/docker-compose-{{ domain }}.yaml up -d 2>&1"
when: "'peer' in node_roles and 'newcomer' not in node_roles"
- name: Bootstrap new orgs
block:
# - name: Generating anchor peer updates for common channels
# raw: 'docker-compose exec -e FABRIC_CFG_PATH=/etc/hyperledger/artifacts "cli.{{ org }}.{{ domain }}" configtxgen -profile "{{ common_channel_name }}" -outputAnchorPeersUpdate ./channel/{{ org }}MSPanchors-{{ common_channel_name }}.tx -channelID "{{ common_channel_name }}" -asOrg {{ org }}MSP'
#
# - name: Generating anchor peer updates for other channels, if defined
# raw: 'docker-compose exec -e FABRIC_CFG_PATH=/etc/hyperledger/artifacts "cli.{{ org }}.{{ domain }}" configtxgen -profile "{{ item.name }}" -outputAnchorPeersUpdate ./channel/{{ org }}MSPanchors-{{ item.name }}.tx -channelID "{{ item.name }}" -asOrg {{ org }}MSP'
# loop: "{{ additional_channels }}"
# when: "additional_channels is defined and org in item.particapants"
- name: Changing artifacts ownership
raw: "docker-compose exec 'cli.{{ org }}.{{ 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
raw: "{{ item }}"
loop:
- "docker-compose exec 'cli.{{ org }}.{{ domain }}' bash -c 'export CORE_PEER_ADDRESS=peer0.{{ org }}.{{ domain }}:7051 && peer chaincode install -n {{ chaincode_common_name }} -v {{ chaincode_version }} -p {{ chaincode_common_name }} -l {{ chaincode_lang }}'"
# - "docker-compose exec 'cli.{{ org }}.{{ domain }}' bash -c 'export CORE_PEER_ADDRESS=peer1.{{ org }}.{{ domain }}:7051 && peer chaincode install -n {{ chaincode_common_name }} -v {{ chaincode_version }} -p {{ chaincode_common_name }} -l {{ chaincode_lang }}'"
- name: Install chaincode for additional cahnnels, if any
raw: "{{ item }}"
loop:
- "docker-compose exec 'cli.{{ org }}.{{ domain }}' bash -c 'export CORE_PEER_ADDRESS=peer0.{{ org }}.{{ domain }}:7051 && peer chaincode install -n {{ chaincode_bilateral_name }} -v {{ chaincode_version }} -p {{ chaincode_bilateral_name }} -l {{ chaincode_lang }}'"
#- "docker-compose exec 'cli.{{ org }}.{{ domain }}' bash -c 'export CORE_PEER_ADDRESS=peer1.{{ org }}.{{ domain }}:7051 && peer chaincode install -n {{ chaincode_bilateral_name }} -v {{ chaincode_version }} -p {{ chaincode_bilateral_name }} -l {{ chaincode_lang }}'"
when: "additional_channels is defined"
# - name: Updating channels anchor peers
# raw: 'docker-compose exec "cli.{{ org }}.{{ domain }}" bash -c "peer channel update -o orderer{{ orderer_id | default() }}.{{ domain }}:{{ orderer_port }} -c common -f /etc/hyperledger/artifacts/channel/{{ org }}MSPanchors-{{ common_channel_name }}.tx --tls --cafile /etc/hyperledger/artifacts/crypto-config/ordererOrganizations/{{ domain }}/orderers/orderer{{ orderer_id | default() }}.{{ domain }}/tls/ca.crt"'
when: "'peer' in node_roles and 'newcomer' in node_roles"
- name: Creating addtional channels, if any
block:
- name: Creating other channels
raw: 'docker-compose exec "cli.{{ org }}.{{ domain }}" bash -c "peer channel create -o orderer{{ orderer_id | default() }}.{{ domain }}:{{ orderer_port }} -c {{ item.name }} -f /etc/hyperledger/artifacts/channel/{{ item.name }}.tx --tls --cafile /etc/hyperledger/artifacts/crypto-config/ordererOrganizations/{{ domain }}/orderers/orderer{{ orderer_id | default() }}.{{ domain }}/tls/ca.crt"'
loop: "{{ new_channels }}"
when: "org in item.particapants[0]"
# - name: Updating anchor peers
# raw: 'docker-compose exec "cli.{{ org }}.{{ domain }}" bash -c "peer channel update -o orderer{{ orderer_id | default() }}.{{ domain }}:{{ orderer_port }} -c {{ item.name }} -f /etc/hyperledger/artifacts/channel/{{ org }}MSPanchors-{{ item.name }}.tx --tls --cafile /etc/hyperledger/artifacts/crypto-config/ordererOrganizations/{{ domain }}/orderers/orderer{{ orderer_id | default() }}.{{ domain }}/tls/ca.crt"'
# loop: "{{ additional_channels }}"
# when: "org in item.particapants"
- name: Changing ownership of channel block files
raw: 'docker-compose exec "cli.{{ 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-compose exec "cli.{{ org }}.{{ domain }}" bash -c "export CORE_PEER_ADDRESS=peer0.{{ org }}.{{ domain }}:7051 && peer channel join -b {{ item.name }}.block"'
loop: "{{ new_channels }}"
when: "org in item.particapants"
# - raw: 'docker-compose exec "cli.{{ org }}.{{ domain }}" bash -c "export CORE_PEER_ADDRESS=peer1.{{ org }}.{{ 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-compose exec cli.{{ org }}.{{ domain }} bash -c 'export CORE_PEER_ADDRESS=peer0.{{ org }}.{{ domain }}:7051 && peer chaincode instantiate -n {{ chaincode_bilateral_name }} -v {{ chaincode_version }} -c {{ chanicode_bilateral_init | to_json }} -o orderer{{ orderer_id | default() }}.{{ domain }}:{{ orderer_port }} -C {{ item.name }} --tls --cafile /etc/hyperledger/artifacts/crypto-config/ordererOrganizations/{{ domain }}/orderers/orderer{{ orderer_id | default() }}.{{ domain }}/tls/ca.crt'"
loop: "{{ new_channels }}"
when: "org in item.particapants[0]"
- name: Changing ownership of channel block files
raw: 'docker-compose exec "cli.{{ domain }}" bash -c "chown -R {{ ansible_user_uid }}:{{ ansible_user_gid }} ."'
when: "new_channels is defined and 'peer' in node_roles"
- name: Joining common channels by other nodes
block:
- name: Synchronize genrated block files (artifacts)
become: true
synchronize: src="artifacts" dest="{{ fabric_starter_workdir }}" recursive=yes
- name: Joining common channel
raw: 'docker-compose exec "cli.{{ org }}.{{ domain }}" bash -c "export CORE_PEER_ADDRESS=peer0.{{ org }}.{{ domain }}:7051 && peer channel join -b common.block"'
#- raw: 'docker-compose exec "cli.{{ org }}.{{ domain }}" bash -c "export CORE_PEER_ADDRESS=peer1.{{ org }}.{{ domain }}:7051 && peer channel join -b common.block"'
- name: Joining other channels
raw: 'docker-compose exec "cli.{{ org }}.{{ domain }}" bash -c "export CORE_PEER_ADDRESS=peer0.{{ org }}.{{ domain }}:7051 && peer channel join -b {{ item.name }}.block"'
loop: "{{ additional_channels }}"
when: "org in item.particapants"
# - raw: 'docker-compose exec "cli.{{ org }}.{{ domain }}" bash -c "export CORE_PEER_ADDRESS=peer1.{{ org }}.{{ domain }}:7051 && peer channel join -b {{ item.name }}.block"'
# loop: "{{ additional_channels }}"
# when: "org in item.particapants"
- name: Changing ownership of channel block files
raw: 'docker-compose exec "cli.{{ domain }}" bash -c "chown -R {{ ansible_user_uid }}:{{ ansible_user_gid }} ."'
when: "'peer' in node_roles and 'newcomer' in node_roles"