Repository navigation
Expand file tree
/
Copy pathgeneratescripts.go
More file actions
537 lines (469 loc) · 15.9 KB
/
Copy pathgeneratescripts.go
File metadata and controls
537 lines (469 loc) · 15.9 KB
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"text/template"
)
const _SetPeerTemplate = `
#!/bin/bash
export ORDERER_CA=/opt/ws/crypto-config/ordererOrganizations/{{.orderers.domain}}/msp/tlscacerts/tlsca.{{.orderers.domain}}-cert.pem
#For fabric 2.2.x extra environment variables
{{range .orgs}}
export {{.name | ToUpper }}_PEER0_CA=/opt/ws/crypto-config/peerOrganizations/{{.domain}}/peers/peer0.{{.domain}}/tls/ca.crt
{{end}}
{{$primechannel := (index .channels 0).channelName }}
if [ $# -lt 2 ];then
echo "Usage : . setpeer.sh {{range .orgs}}{{.name}}|{{end}} <peerid>"
fi
export peerId=$2
{{range .orgs}}
if [[ $1 = "{{.name}}" ]];then
echo "Setting to organization {{.name}} peer "$peerId
export CORE_PEER_ADDRESS=$peerId.{{.domain}}:7051
export CORE_PEER_LOCALMSPID={{.mspID}}
export CORE_PEER_TLS_CERT_FILE=/opt/ws/crypto-config/peerOrganizations/{{.domain}}/peers/$peerId.{{.domain}}/tls/server.crt
export CORE_PEER_TLS_KEY_FILE=/opt/ws/crypto-config/peerOrganizations/{{.domain}}/peers/$peerId.{{.domain}}/tls/server.key
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/ws/crypto-config/peerOrganizations/{{.domain}}/peers/$peerId.{{.domain}}/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=/opt/ws/crypto-config/peerOrganizations/{{.domain}}/users/Admin@{{.domain}}/msp
fi
{{end}}
`
const _GenerateArtifactsTemplate = `
#!/bin/bash -e
export PWD={{ "pwd" | ToCMDString}}
export FABRIC_CFG_PATH=$PWD
export ARCH=$(uname -s)
export CRYPTOGEN=$PWD/bin/cryptogen
export CONFIGTXGEN=$PWD/bin/configtxgen
function generateArtifacts() {
echo " *********** Generating artifacts ************ "
echo " *********** Deleting old certificates ******* "
rm -rf ./crypto-config
echo " ************ Generating certificates ********* "
$CRYPTOGEN generate --config=$FABRIC_CFG_PATH/crypto-config.yaml
echo " ************ Generating tx files ************ "
$CONFIGTXGEN -profile OrdererGenesis -channelID system-channel -outputBlock ./genesis.block -channelID genesischannel
{{range .channels}}{{$chName := .channelName }}{{$channelId:= $chName | ToLower }}
$CONFIGTXGEN -profile {{print $chName }} -outputCreateChannelTx ./{{print $channelId ".tx" }} -channelID {{ print $channelId }}
{{range $org:= .orgs}}
echo {{print "\"Generating anchor peers tx files for " $org "\""}}
$CONFIGTXGEN -profile {{print $chName }} -outputAnchorPeersUpdate ./{{print $channelId $org "MSPAnchor.tx" }} -channelID {{ print $channelId }} -asOrg {{print $org "MSP" }}
{{end}}
{{end}}
}
generateArtifacts
cd $PWD
`
const _GenerateArtifactsTemplateWithCA = `
#!/bin/bash -e
export PWD={{ "pwd" | ToCMDString}}
export FABRIC_CFG_PATH=$PWD
export ARCH=$(uname -s)
export CRYPTOGEN=$PWD/bin/cryptogen
export CONFIGTXGEN=$PWD/bin/configtxgen
function generateArtifacts() {
echo " *********** Generating artifacts ************ "
echo " *********** Deleting old certificates ******* "
rm -rf ./crypto-config
echo " ************ Generating certificates ********* "
$CRYPTOGEN generate --config=$FABRIC_CFG_PATH/crypto-config.yaml
echo " ************ Generating tx files ************ "
$CONFIGTXGEN -profile OrdererGenesis -channelID system-channel -outputBlock ./genesis.block
{{range .channels}}{{$chName := .channelName }}{{$channelId:= $chName | ToLower }}
$CONFIGTXGEN -profile {{print $chName }} -outputCreateChannelTx ./{{print $channelId ".tx" }} -channelID {{ print $channelId }}
{{range $org:= .orgs}}
echo {{print "\"Generating anchor peers tx files for " $org "\""}}
$CONFIGTXGEN -profile {{print $chName }} -outputAnchorPeersUpdate ./{{print $channelId $org "MSPAnchor.tx" }} -channelID {{ print $channelId }} -asOrg {{print $org "MSP" }}
{{end}}
{{end}}
}
function generateDockerComposeFile(){
OPTS="-i"
if [ "$ARCH" = "Darwin" ]; then
OPTS="-it"
fi
cp docker-compose-template.yaml docker-compose.yaml
{{ range .orgs}}
{{$orgName :=.name | ToUpper }}
cd crypto-config/peerOrganizations/{{.domain}}/ca
PRIV_KEY=$(ls *_sk)
cd ../../../../
sed $OPTS "s/{{$orgName}}_PRIVATE_KEY/${PRIV_KEY}/g" docker-compose.yaml
{{end}}
}
generateArtifacts
cd $PWD
generateDockerComposeFile
cd $PWD
{{ range .orgs}}
{{$caorgName :=.name | ToLower }}
mkdir ca-{{$caorgName}}
touch ca-{{$caorgName}}/fabric-ca-server.db
{{end}}
`
const _BuildChannelScript = `
#!/bin/bash -e
{{ $orderer:= .ordererURL}}
{{ $timeOut:= .timeout}}
{{ $root := . }}
{{range .channels}}
{{ $channelId := print .channelName | ToLower }}
echo "Building channel for {{print $channelId}}"
{{$firstOrg := (index .orgs 0) }}
. setpeer.sh {{$firstOrg}} peer0
export CHANNEL_NAME="{{print $channelId }}"
peer channel create -o {{ print $orderer }} -c $CHANNEL_NAME -f ./{{print $channelId ".tx"}} --tls true --cafile $ORDERER_CA -t {{$timeOut}}
{{ range $index,$orgName :=.orgs}}
{{$orgConfig := index $root $orgName }}
{{ range $i,$peerId:=$orgConfig.peerNames }}
. setpeer.sh {{$orgName}} {{$peerId}}
export CHANNEL_NAME="{{print $channelId }}"
peer channel join -b $CHANNEL_NAME.block
{{end}}
{{end}}
{{ range $index,$orgName :=.orgs}}
{{$orgConfig := index $root $orgName }}
{{ range $i,$peerId:=$orgConfig.peerNames }}
. setpeer.sh {{$orgName}} {{$peerId}}
export CHANNEL_NAME="{{print $channelId }}"
peer channel update -o {{ print $orderer }} -c $CHANNEL_NAME -f ./{{print $channelId $orgName "MSPAnchor.tx" }} --tls --cafile $ORDERER_CA
{{end}}
{{end}}
{{end}}
`
const _CleanUp = `
#!/bin/bash
echo "Clearing the old artifacts"
rm *.yaml
rm -rf crypto-config
rm *.block
rm *.tx
rm generateartifacts.sh
rm setenv.sh
rm setpeer.sh
rm buildandjoinchannel.sh
rm *_install.sh
rm *_update.sh
rm add_affi*.sh
rm -rf *.log
rm -rf *log.txt
rm -rf *.tar.gz
`
const _SetEnv_1_4_0 = `
#!/bin/bash
export IMAGE_TAG="1.4.0"
export COUCH_TAG="0.4.15"
export ZK_TAG="0.4.14"
export KAFKA_TAG="0.4.14"
export TOOLS_TAG="1.4.0"
export TAG_CCENV="1.4.0"
export TAG_BASEOS="0.4.14"
export CA_TAG="1.4.0"
`
const _SetEnv_1_4_2 = `
#!/bin/bash
export IMAGE_TAG="1.4.2"
export TOOLS_TAG="1.4.2"
export TAG_CCENV="1.4.2"
export COUCH_TAG="0.4.15"
export TAG_BASEOS="0.4.15"
export CA_TAG="1.4.2"
export KAFKA_TAG="0.4.15"
export ZK_TAG="0.4.15"
`
const _SetEnv_2_2 = `
#!/bin/bash
export IMAGE_TAG="2.2.0"
export TOOLS_TAG="2.2.0"
export TAG_CCENV="2.2.0"
export COUCH_TAG="3.1"
export TAG_BASEOS="2.2.0"
export CA_TAG="1.4.7"
`
const _GitIgnore = `
worldstate/
*.tar.gz
*.txt
*.block
*.tx
bin/
blocks/
crypto-config/*
config/
bin/
chaincode/*
`
const _DOTENV = `
COMPOSE_PROJECT_NAME=bc
`
const _DOWNLOAD_SCRIPTS = `
#!/bin/bash
export VERSION={{.downloadVersion}}
export ARCH=$(echo "$(uname -s|tr '[:upper:]' '[:lower:]'|sed 's/mingw64_nt.*/windows/')-$(uname -m | sed 's/x86_64/amd64/g')" | awk '{print tolower($0)}')
echo "===> Downloading platform binaries"
export URL="https://github.com/hyperledger/fabric/releases/download/v${VERSION}/hyperledger-fabric-${ARCH}-${VERSION}.tar.gz"
echo $URL
curl -L $URL| tar xz
`
const _VERSION_COMP_MAP = `
{
"1.4.0":{ "fabricCore":"1.4.0","thirdParty":"0.4.14","couch":"0.4.15","zk":"0.4.14","kafka":"0.4.14"}
}
`
const _REMOVE_IMAGES = `
#!/bin/bash
docker rmi -f {{ "docker images bc* -aq" | ToCMDString}}
`
const _CREATE_AFFILIATION = `
#!/bin/bash
fabric-ca-client enroll -u https://admin:adminpw@ca.{{.domain}}:7054 --tls.certfiles /etc/hyperledger/fabric-ca-server-config/ca.{{.domain}}-cert.pem
fabric-ca-client affiliation add {{.orgName | ToLower }} -u https://admin:adminpw@ca.{{.domain}}:7054 --tls.certfiles /etc/hyperledger/fabric-ca-server-config/ca.{{.domain}}-cert.pem
`
const _CLEAR_VOL_SCRIPT = `
#!/bin/bash
sudo rm -rf ./worldstate
sudo rm -rf ./blocks
sudo rm -rf ./ca-*
`
func ToCMDString(input string) string {
return "`" + input + "`"
}
func GenerateOtherScripts(config []byte, path string) bool {
tmpl, err := template.New("dotenv").Parse(_DOTENV)
if err != nil {
fmt.Printf("Error in reading template %v\n", err)
return false
}
dataMapContainer := make(map[string]interface{})
json.Unmarshal(config, &dataMapContainer)
var outputBytes bytes.Buffer
err = tmpl.Execute(&outputBytes, dataMapContainer)
if err != nil {
fmt.Printf("Error in generating the .env file %v\n", err)
return false
}
ioutil.WriteFile(path+".env", outputBytes.Bytes(), 0666)
fabVersion := "1.4.0"
version, _ := dataMapContainer["fabricVersion"].(string)
if ifExists(dataMapContainer, "fabricVersion") {
fabVersion = version
}
tmplName := _SetEnv_1_4_0
switch fabVersion {
case "1.4.0":
tmplName = _SetEnv_1_4_0
case "1.4.2":
tmplName = _SetEnv_1_4_2
case "2.2.0":
tmplName = _SetEnv_2_2
}
tmpl, err = template.New("setenv").Parse(tmplName)
if err != nil {
fmt.Printf("Error in reading template %v\n", err)
return false
}
var outputBytes2 bytes.Buffer
err = tmpl.Execute(&outputBytes2, nil)
if err != nil {
fmt.Printf("Error in generating the setenv.sh file %v\n", err)
return false
}
ioutil.WriteFile(path+"setenv.sh", outputBytes2.Bytes(), 0777)
tmpl, err = template.New("cleanup").Parse(_CleanUp)
if err != nil {
fmt.Printf("Error in reading template %v\n", err)
return false
}
var outputBytes3 bytes.Buffer
err = tmpl.Execute(&outputBytes3, dataMapContainer)
if err != nil {
fmt.Printf("Error in generating the cleanup.sh file %v\n", err)
return false
}
ioutil.WriteFile(path+"cleanup.sh", outputBytes3.Bytes(), 0777)
tmpl, err = template.New("download").Parse(_DOWNLOAD_SCRIPTS)
if err != nil {
fmt.Printf("Error in reading template %v\n", err)
return false
}
dataMapContainer["downloadVersion"] = version
var outputBytes4 bytes.Buffer
err = tmpl.Execute(&outputBytes4, dataMapContainer)
if err != nil {
fmt.Printf("Error in generating the downloadbin.sh file %v\n", err)
return false
}
ioutil.WriteFile(path+"downloadbin.sh", outputBytes4.Bytes(), 0777)
funcMap := template.FuncMap{
"ToCMDString": ToCMDString,
"ToLower": strings.ToLower,
"ToUpper": strings.ToUpper,
}
tmpl, err = template.New("rmImages").Funcs(funcMap).Parse(_REMOVE_IMAGES)
if err != nil {
fmt.Printf("Error in reading template %v\n", err)
return false
}
var outputBytes5 bytes.Buffer
err = tmpl.Execute(&outputBytes5, dataMapContainer)
if err != nil {
fmt.Printf("Error in generating the generateArtifacts file %v\n", err)
return false
}
ioutil.WriteFile(path+"removeImages.sh", outputBytes5.Bytes(), 0777)
ioutil.WriteFile(path+"clearVols.sh", []byte(_CLEAR_VOL_SCRIPT), 0777)
ioutil.WriteFile(path+".gitignore", []byte(_GitIgnore), 0666)
GenerateAffiliationScripts(config, path)
return true
}
func GenerateGenerateArtifactsScript(config []byte, filename string) bool {
funcMap := template.FuncMap{
"ToCMDString": ToCMDString,
"ToLower": strings.ToLower,
"ToUpper": strings.ToUpper,
}
templateToUse := _GenerateArtifactsTemplate
dataMapContainer := make(map[string]interface{})
json.Unmarshal(config, &dataMapContainer)
addCA := getBoolean(dataMapContainer["addCA"])
if addCA == true {
templateToUse = _GenerateArtifactsTemplateWithCA
}
tmpl, err := template.New("generateArtifacts").Funcs(funcMap).Parse(templateToUse)
if err != nil {
fmt.Printf("Error in reading template %v\n", err)
return false
}
var outputBytes bytes.Buffer
err = tmpl.Execute(&outputBytes, dataMapContainer)
if err != nil {
fmt.Printf("Error in generating the generateArtifacts file %v\n", err)
return false
}
ioutil.WriteFile(filename, outputBytes.Bytes(), 0777)
return true
}
//GenerateSetPeer emits setpeer.sh file
func GenerateSetPeer(config []byte, filename string) bool {
funcMap := template.FuncMap{
"ToLower": strings.ToLower,
"ToUpper": strings.ToUpper,
}
tmpl, err := template.New("setPeer").Funcs(funcMap).Parse(_SetPeerTemplate)
if err != nil {
fmt.Printf("Error in reading template %v\n", err)
return false
}
dataMapContainer := make(map[string]interface{})
json.Unmarshal(config, &dataMapContainer)
var outputBytes bytes.Buffer
err = tmpl.Execute(&outputBytes, dataMapContainer)
if err != nil {
fmt.Printf("Error in generating the setpeer.sh file %v\n", err)
return false
}
ioutil.WriteFile(filename, outputBytes.Bytes(), 0777)
return true
}
//GenerateBuildAndJoinChannelScript generates build and join channel script
func GenerateBuildAndJoinChannelScript(config []byte, filename string) bool {
funcMap := template.FuncMap{
"ToCMDString": ToCMDString,
"ToLower": strings.ToLower,
}
tmpl, err := template.New("buildChannel").Funcs(funcMap).Parse(_BuildChannelScript)
if err != nil {
fmt.Printf("Error in reading template %v\n", err)
return false
}
dataMapContainer := make(map[string]interface{})
channelMap := make(map[string]interface{})
json.Unmarshal(config, &dataMapContainer)
//Check the version
fabricVersion, _ := dataMapContainer["fabricVersion"].(string)
if strings.HasPrefix(fabricVersion, "2.2") {
return GenerateBuildAndJoinChannelScriptV2x(config)
}
orgs, _ := dataMapContainer["orgs"].([]interface{})
for _, org := range orgs {
orgConfig := getMap(org)
peerCount := getNumber(orgConfig["peerCount"])
peerNames := make([]string, 0)
fmt.Printf(" Peer count %d\n", peerCount)
for index := 0; index < peerCount; index++ {
peerNames = append(peerNames, fmt.Sprintf("peer%d", index))
}
orgConfig["peerNames"] = peerNames
orgName := getString(orgConfig["name"])
channelMap[orgName] = orgConfig
}
channelMap["channels"] = dataMapContainer["channels"]
//Resolve the orderer name
ordererConfig := getMap(dataMapContainer["orderers"])
if ifExists(ordererConfig, "type") && ifExists(ordererConfig, "haCount") && (getString(ordererConfig["type"]) == "kafka" || getString(ordererConfig["type"]) == "raft") {
channelMap["ordererURL"] = fmt.Sprintf("%s0.%s:7050", getString(ordererConfig["ordererHostname"]), getString(ordererConfig["domain"]))
} else {
channelMap["ordererURL"] = fmt.Sprintf("%s.%s:7050", getString(ordererConfig["ordererHostname"]), getString(ordererConfig["domain"]))
}
timeOut := "1000"
if IsVersionAbove(dataMapContainer, "1.3.0") {
fmt.Println("Generation 1.3 compatible build&joinchannel")
timeOut = "1000s"
}
channelMap["timeout"] = timeOut
var outputBytes bytes.Buffer
err = tmpl.Execute(&outputBytes, channelMap)
if err != nil {
fmt.Printf("Error in generating the buildchannel.sh file %v\n", err)
return false
}
ioutil.WriteFile(filename, outputBytes.Bytes(), 0777)
return true
}
func GetVersions(version string) map[string]string {
versionMap := make(map[string]map[string]string)
json.Unmarshal([]byte(_VERSION_COMP_MAP), &versionMap)
if details, isOk := versionMap[version]; !isOk {
fmt.Println("Invalid version number provided defaulting to 1.4.0")
return versionMap["1.4.0"]
} else {
return details
}
}
func IsVersionAbove(config map[string]interface{}, version string) bool {
confVersion := getString(config["fabricVersion"])
return len(version) > 0 && strings.Compare(confVersion, version) >= 0
}
func GenerateAffiliationScripts(config []byte, path string) {
funcMap := template.FuncMap{
"ToLower": strings.ToLower,
}
networkConfig := make(map[string]interface{})
json.Unmarshal(config, &networkConfig)
addCA := getBoolean(networkConfig["addCA"])
if addCA {
orgConfigs, _ := networkConfig["orgs"].([]interface{})
tmpl, err := template.New("addAffiliation").Funcs(funcMap).Parse(_CREATE_AFFILIATION)
if err != nil {
fmt.Printf("Error in reading template %v\n", err)
return
}
for _, org := range orgConfigs {
orgConfig, _ := org.(map[string]interface{})
templateData := make(map[string]string)
templateData["domain"] = getString(orgConfig["domain"])
templateData["orgName"] = getString(orgConfig["name"])
var outputBytes bytes.Buffer
err = tmpl.Execute(&outputBytes, templateData)
if err != nil {
fmt.Printf("Error in generating the generateAffiliation file %v\n", err)
return
}
fileName := fmt.Sprintf("add_affiliation_%s.sh", strings.ToLower(templateData["orgName"]))
ioutil.WriteFile(path+fileName, outputBytes.Bytes(), 0777)
}
}
}