forked from minhlh/Ansible-Fabric-Starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart-network.yml
173 lines (138 loc) · 7.11 KB
/
start-network.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
---
- hosts: nodes
tasks:
- name: Perfom check for 'newcomers' flag
fail:
msg: "newcomers flag is defined. Are you sure, you want to destroy existing network? ..Aborting."
tags:
- safetycheck
when: "newcomers is defined"
- name: Clean all previous dockers, if any
raw: "docker rm -f $(docker ps -a | grep {{ global_domain }} | awk '{print $1};')"
ignore_errors: true
- name: Clean all docker volumes
raw: "docker volume rm $(docker volume ls -qf dangling=true)"
ignore_errors: true
- name: Findout UID
raw: "id -u {{ ansible_user }}"
register: ansible_uid
- name: Findout GID
raw: "id -g {{ ansible_user }}"
register: ansible_gid
- name: Clean all help-scripts, if any
file:
dest: "{{ item }}"
state: absent
loop:
- "{{ fabric_starter_workdir }}/start-node.sh"
- "{{ fabric_starter_workdir }}/stop-node.sh"
- set_fact:
ansible_user_uid: "{{ ansible_uid.stdout | int }}"
ansible_user_gid: "{{ ansible_gid.stdout | int }}"
- name: Starting orderer
block:
- name: Start orderer
raw: "docker-compose -f {{ docker_artifacts }}/docker-compose-{{ global_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-{{ global_domain }}.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-{{ global_domain }}.yaml down"
create: yes
- name: Let orderer start
raw: "docker logs orderer{{ orderer_id | default() }}.{{ global_domain }}"
register: result
until: result.stdout.find("Beginning to serve requests") != -1
retries: 60
delay: 1
when: "'orderer' in node_roles"
- name: Starting peers
block:
- 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)
synchronize: src="./artifacts/{{ item.from }}" dest="{{ fabric_artifacts }}/{{ item.to }}" recursive=yes
loop: "{{ files_to_rsync_nodes_phase1 }}"
when: "orderer_count is not defined or orderer_count <= 1"
- 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 }}"
when: "'peer' in node_roles"
- name: Creating channels
block:
- name: Creating channels
raw: "docker exec cli.{{ org }}.{{ global_domain }} bash -c 'peer channel create \
-o localhost:7050 \
--ordererTLSHostnameOverride orderer{{ orderer_id | default() }}.{{ global_domain }} \
-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: "{{ global_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 }}/{{ item.name }}.block" dest="./artifacts/" mode=pull recursive=yes
loop: "{{ global_channels }}"
when: "org in item.particapants[0]"
- name: Synchronize generated block files (artifacts) back
# become: true
synchronize: src="artifacts/{{ item.name }}.block" dest="{{ fabric_artifacts }}/" recursive=yes
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=localhost:7051 && peer channel join -b {{ item.name }}.block"'
loop: "{{ global_channels }}"
when: "org in item.particapants"
- name: Approve for my org chaincode
raw: "docker exec cli.{{ org }}.{{ global_domain }} bash -c \
'export CORE_PEER_ADDRESS=localhost:7051 && \
peer lifecycle chaincode approveformyorg \
-o localhost:7050 \
--ordererTLSHostnameOverride orderer{{ orderer_id | default() }}.{{ global_domain }} --tls \
--cafile /etc/hyperledger/artifacts/crypto-config/ordererOrganizations/{{ global_domain }}/tlsca/tlsca.{{ global_domain }}-cert.pem \
-C {{ item.name }} -n {{ item.chaincode.name }} \
-v {{ item.chaincode.version }} \
--package-id {{ item.chaincode.name }}_{{ item.chaincode.version }} \
--sequence 1 NA NA NA'"
loop: "{{ global_channels }}"
when: "org in item.particapants and item.chaincode.policy == ''"
- name: Check chaincode
raw: "docker exec cli.{{ org }}.{{ global_domain }} bash -c \
'export CORE_PEER_ADDRESS=localhost:7051 && \
cd /opt/chaincode && ./checkCC.sh'"
loop: "{{ global_channels }}"
when: "org in item.particapants[0] and item.chaincode.policy == ''"
- 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: Updating anchor peers
raw: 'docker exec "cli.{{ org }}.{{ global_domain }}" bash -c "peer channel update -o localhost:7050 --ordererTLSHostnameOverride orderer{{ orderer_id | default() }}.{{ global_domain }} -c {{ item.name }} -f /etc/hyperledger/artifacts/channel/{{ org }}MSPanchors-{{ item.name }}.tx --tls --cafile /etc/hyperledger/artifacts/crypto-config/ordererOrganizations/{{ global_domain }}/tlsca/tlsca.{{ global_domain }}-cert.pem"'
loop: "{{ global_channels }}"
when: "org in item.particapants"
when: "global_channels is defined and 'peer' in node_roles"