Skip to content

Commit

Permalink
[CE-386][CE-387]Refactor code to reduce duplicate code
Browse files Browse the repository at this point in the history
Fabric peer creates chaincode container using docker
in docker method. This create challenges while
docker endpoint in k8s cluster is unknown. In this
patch set, a k8s daemon set is introduced to start
up a DINK pod on each k8s node to make sure that
the VM endpoint is available. This patch set also
remove a lot of duplicate code.

Change-Id: I7bb1c4d7a1746c236dc21751617ed7a3c431921d
Signed-off-by: Tong Li <litong01@us.ibm.com>
  • Loading branch information
Tong Li committed Jun 19, 2018
1 parent d6475e5 commit 402ad34
Show file tree
Hide file tree
Showing 17 changed files with 318 additions and 375 deletions.
120 changes: 120 additions & 0 deletions src/agent/ansible/roles/common/config_apply.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
---
- name: Setup and initialize variables
set_fact:
fabricworkdir: "/opt/gopath/{{ env }}/fabric"
fabricpath: "/opt/gopath/{{ env }}/src/github.com/hyperledger/fabric"
gopath: "/opt/gopath/{{ env }}"
peers: "{{ [] }}"
orderers: "{{ [] }}"
cas: "{{ [] }}"
kafkas: "{{ [] }}"
zookeepers: "{{ [] }}"
znodes: ""
zkparam: ""
index: 1
zoo_ids: "{{ {} }}"
kafka_ids: "{{ {} }}"
zookeeperls: "{{ [] }}"
kafkals: "{{ [] }}"
peerls: "{{ [] }}"
ordererls: "{{ [] }}"
cals: "{{ [] }}"
allpeers: "{{ [] }}"
allcas: "{{ [] }}"
allorderers: "{{ [] }}"
clipeer: "{}"
cliorderer: "{}"

- name: Make sure that working directory exists and clean
file:
path: "{{ fabricworkdir }}/run"
state: "{{ item }}"
mode: 0775
with_items:
- "absent"
- "directory"

- name: Create component list
set_fact:
cals: "{{ cals | default([]) + item.value.cas | default([]) }}"
ordererls: "{{ ordererls | default([]) + item.value.orderers | default([]) }}"
peerls: "{{ peerls | default([]) + item.value.peers | default([]) }}"
zookeeperls: "{{ zookeeperls | default([]) + item.value.zookeepers | default([]) }}"
kafkals: "{{ kafkals | default([]) + item.value.kafkas | default([]) }}"
with_dict: "{{ fabric.network }}"

- name: Sort component list
set_fact:
zookeeperls: "{{ zookeeperls | sort }}"
kafkals: "{{ kafkals | sort }}"

- name: Generate zookeeper node list
set_fact:
znodes: "{{ znodes }} server.{{ index }}={{ item }}:2888:3888"
zkparam: "{{ zkparam + item }}:2181 "
zoo_ids: "{{ zoo_ids | combine({item: index}) }}"
index: "{{ index | int + 1 }}"
with_items: "{{ zookeeperls | default([]) }}"

- name: Reset the index
set_fact:
index: 1

- name: Generate kafka broker ids
set_fact:
kafka_ids: "{{ kafka_ids | combine({item: index}) }}"
index: "{{ index | int + 1 }}"
with_items: "{{ kafkals | default([]) }}"

- name: Get all ca object list
set_fact:
allcas: |
{{ allcas | default([]) + [{ 'org':item.split('.')[-1],
'name':item | replace('.', '-') }] }}
with_items: "{{ cals }}"

- name: Get all peer object list
set_fact:
allpeers: |
{{ allpeers | default([]) + [{'org':item.split('@')[1].split('.')[-1],
'name':item.split('@')[1] | replace('.', '-'),
'role':item.split('@')[0]}] }}
with_items: "{{ peerls }}"

- name: Get all orderer object list
set_fact:
allorderers: |
{{ allorderers | default([]) + [{ 'org':item.split('.')[-1],
'name':item | replace('.', '-') }] }}
with_items: "{{ ordererls }}"

- name: Find all anchor peers
set_fact:
anchor_peers: "{{ allpeers | selectattr('role', 'equalto', 'anchor') | list }}"
when: allpeers is defined and (allpeers | length > 0)

- name: Create org anchor peer hash
set_fact:
org_anchor: "{{ org_anchor | default({}) | combine( { item.org: item } ) }}"
with_items: "{{ anchor_peers }}"
when: anchor_peers is defined and (anchor_peers | length > 0)

- name: Select a peer to run channel and chaincode command
set_fact:
clipeer: "{{ allpeers | random }}"
when: allpeers | default([]) | length > 0

- name: Select an orderer to run channel and chaincode command
set_fact:
cliorderer: "{{ allorderers | random }}"
when: allorderers | default([]) | length > 0

- name: Get ca, peer and orderer orgs
set_fact:
caorgs: "{{ allcas | default([]) | map(attribute='org') | list | unique | sort }}"
peerorgs: "{{ allpeers | default([]) | map(attribute='org') | list | unique | sort }}"
ordererorgs: "{{ allorderers | default([]) | map(attribute='org') | list | unique | sort }}"

- name: Create all orgs list
set_fact:
allorgs: "{{ (caorgs + peerorgs + ordererorgs) | sort | unique }}"
5 changes: 5 additions & 0 deletions src/agent/ansible/roles/common/config_destroy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- name: Make sure that working directory is clean
file:
path: "/opt/gopath/{{ env }}/fabric/run"
state: "absent"
85 changes: 8 additions & 77 deletions src/agent/ansible/roles/deploy_compose/certsetup/tasks/apply.yml
Original file line number Diff line number Diff line change
@@ -1,77 +1,6 @@
---
- name: Setup few variables
set_fact:
fabricworkdir: "/opt/gopath/{{ env }}/fabric"
fabricpath: "/opt/gopath/{{ env }}/src/github.com/hyperledger/fabric"
gopath: "/opt/gopath/{{ env }}"

- name: Create component list
set_fact:
cals: "{{ cals | default([]) + item.value.cas | default([]) }}"
peerls: "{{ peerls | default([]) + item.value.peers | default([]) }}"
ordererls: "{{ ordererls | default([]) + item.value.orderers | default([]) }}"
kafkals: "{{ kafkals | default([]) + item.value.kafkas | default([]) }}"
with_dict: "{{ fabric.network }}"

- name: Get ca object list
set_fact:
cas: |
{{ cas | default([]) + [{ 'org':item.split('.')[-1],
'name':item | replace('.', '-') }] }}
with_items: "{{ cals }}"
when: cals | length > 0

- name: Get ca object list
set_fact:
cas: "{{ cas | default([]) }}"

- name: Get peer object list
set_fact:
peers: |
{{ peers | default([]) + [{'org':item.split('@')[1].split('.')[-1],
'name':item.split('@')[1] | replace('.','-'),
'role':item.split('@')[0]}] }}
with_items: "{{ peerls }}"

- name: Find all anchor peers
set_fact:
anchor_peers: "{{ peers | selectattr('role', 'equalto', 'anchor') | list }}"

- name: Create org anchor peer hash
set_fact:
org_anchor: "{{ org_anchor | default({}) | combine( { item.org: item } ) }}"
with_items: "{{ anchor_peers }}"

- name: Sort the peer list
set_fact:
peers: "{{ peers | sort(attribute='org') }}"

- name: Get orderer object list
set_fact:
orderers: |
{{ orderers | default([]) + [{'org':item.split('.')[-1],
'name':item | replace('.', '-') }] }}
with_items: "{{ ordererls }}"

- name: Sort the orderer list
set_fact:
orderers: "{{ orderers | sort(attribute='org') }}"
allorgs: "{{ [] }}"

- name: Sort the ca and kafka list
set_fact:
caorgs: "{{ cas | map(attribute='org') | list | unique | sort }}"
peerorgs: "{{ peers | map(attribute='org') | list | unique | sort }}"
ordererorgs: "{{ orderers | map(attribute='org') | list | unique | sort }}"
kafkas: "{{ kafkals | sort }}"

- name: Create all orgs list
set_fact:
allorgs: "{{ caorgs + peerorgs + ordererorgs | sort }}"

- name: Filter the all orgs list
set_fact:
allorgs: "{{ allorgs | unique | sort }}"
- name: Reuse common code
include_tasks: "{{ playbook_dir }}/../common/config_apply.yml"

- name: Ensure certificate directory is clean
file:
Expand All @@ -93,7 +22,7 @@

- name: Combine all orgs into one location
shell: >-
mv {{ item }}/* . && rm -rf {{ item }}
if [ -d '{{ item }}' ]; then mv {{ item }}/* . && rm -rf {{ item }}; fi
args:
chdir: "{{ fabricworkdir }}/keyfiles"
with_items:
Expand All @@ -115,7 +44,7 @@

- name: Rename admin private key
shell: >-
ls | cat -n | while read n f; do mv "$f" "admin_private.key"; done
ls *_sk | cat -n | while read n f; do mv "$f" "admin_private.key"; done
args:
chdir: "{{ fabricworkdir }}/keyfiles/{{ item }}/users/Admin@{{ item }}/msp/keystore"
with_items: "{{ allorgs }}"
Expand Down Expand Up @@ -149,7 +78,7 @@
template:
src: "{{ playbook_dir }}/../deploy_compose/certsetup/templates/core.j2"
dest: "{{ fabricworkdir }}/keyfiles/{{ item.org }}/peers/{{ item.name }}.{{ item.org }}/core.yaml"
with_items: "{{ peers }}"
with_items: "{{ allpeers }}"

- name: Create config tx file
template:
Expand All @@ -165,6 +94,7 @@
-outputCreateChannelTx keyfiles/firstchannel.tx -channelID firstchannel
args:
chdir: "{{ fabricworkdir }}"
when: allorderers | length > 0

- name: Define the anchor peer for organizations
shell: >-
Expand All @@ -184,7 +114,8 @@
{{ fabricworkdir }}/keyfiles/{{ item.org }}/peers/{{ item.name }}.{{ item.org }}
args:
chdir: "{{ fabricworkdir }}"
with_items: "{{ peers }}"
when: allorderers | length > 0
with_items: "{{ allpeers }}"

- name: Create a zip file of all certificates
archive:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Organizations:
Name: {{ org }}
ID: {{ org }}
MSPDir: {{ fabricworkdir }}/keyfiles/{{ org }}/msp
{% if org_anchor[org] is defined %}
{% if project_version | version_compare('1.2.0','>=') or 'stable' in project_version or project_version == 'latest' %}
Policies:
Readers:
Expand All @@ -25,9 +24,11 @@ Organizations:
Type: Signature
Rule: "OR('{{ org }}.admin')"
{% endif %}
{% if org_anchor is defined and org_anchor[org] is defined %}
AnchorPeers:
- Host: {{ org_anchor[org].name }}
Port: 7051
{% endif %}
{% else %}
{% if project_version | version_compare('1.2.0','>=') or 'stable' in project_version or project_version == 'latest' %}
Policies:
Expand All @@ -41,7 +42,6 @@ Organizations:
Type: Signature
Rule: "OR('{{ org }}.admin')"
{% endif %}
{% endif %}

{% endfor %}
{% endif %}
Expand All @@ -61,22 +61,22 @@ Capabilities:
V1_1: true
{% endif %}
{% endif %}

{% if allorderers is defined and ( allorderers|length > 0) %}
Orderer: &OrdererDefaults
OrdererType: kafka
Addresses:
{% for orderer in orderers %}
{% for orderer in allorderers %}
- {{ orderer.name }}:7050
{% endfor %}
BatchTimeout: 2s
BatchSize:
MaxMessageCount: 10
AbsoluteMaxBytes: 98 MB
PreferredMaxBytes: 512 KB
{% if kafkas | length > 0 %}
{% if kafkals | length > 0 %}
Kafka:
Brokers:
{% for item in kafkas %}
{% for item in kafkals %}
- {{ item }}:9092
{% endfor %}
{% endif %}
Expand All @@ -103,7 +103,7 @@ Orderer: &OrdererDefaults
Capabilities:
<<: *OrdererCapabilities
{% endif %}

{% endif %}
Application: &ApplicationDefaults
Organizations:
{% for org in peerorgs %}
Expand Down Expand Up @@ -153,8 +153,10 @@ Profiles:
Capabilities:
<<: *ChannelCapabilities
{% endif %}
{% if allorderers is defined and ( allorderers|length > 0) %}
Orderer:
<<: *OrdererDefaults
{% endif %}
Consortiums:
FabricConsortium:
Organizations:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
"channels": {
"firstchannel": {
"orderers": [
{% for orderer in orderers %}
{% for orderer in allorderers %}
"{{ orderer.name }}"{{ '' if loop.last else ',' }}
{% endfor %}
],
"peers": {
{% for peer in peers %}
{% for peer in allpeers %}
"{{ peer.name }}": {
"endorsingPeer": true, "chaincodeQuery": true, "eventSource": true
}{{ '' if loop.last else ',' }}
Expand All @@ -32,20 +32,20 @@
"{{ org }}": {
"mspid": "{{ org }}",
"peers": [
{% for peer in peers|selectattr('org', 'equalto', org)|list %}
{% for peer in allpeers|selectattr('org', 'equalto', org)|list %}
"{{ peer.name }}"{{ '' if loop.last else ',' }}
{% endfor %}
],
"certificateAuthorities": [
{% for ca in cas|selectattr('org', 'equalto', org)|list %}
{% for ca in allcas|selectattr('org', 'equalto', org)|list %}
"{{ ca.name }}"{{ '' if loop.last else ',' }}
{% endfor %}
]
}{{ '' if loop.last else ',' }}
{% endfor %}
},
"orderers": {
{% for orderer in orderers %}
{% for orderer in allorderers %}
"{{ orderer.name }}": {
"url": "{{ fabric.tls|ternary('grpcs','grpc') }}://{{ orderer.name }}:7050",
"grpcOptions": {
Expand All @@ -58,7 +58,7 @@
{% endfor %}
},
"peers": {
{% for peer in peers %}
{% for peer in allpeers|selectattr('org', 'equalto', item)|list %}
"{{ peer.name }}": {
"url": "{{ fabric.tls|ternary('grpcs','grpc') }}://{{ peer.name }}:7051",
"eventUrl": "{{ fabric.tls|ternary('grpcs','grpc') }}://{{ peer.name }}:7053",
Expand All @@ -72,7 +72,7 @@
{% endfor %}
},
"certificateAuthorities": {
{% for ca in cas %}
{% for ca in allcas|selectattr('org', 'equalto', item)|list %}
"{{ ca.name }}": {
"url": "{{ fabric.tls|ternary('https','http') }}://{{ca.name}}:7054",
"caName": "{{ ca.name }}",
Expand Down
Loading

0 comments on commit 402ad34

Please sign in to comment.