-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
382 lines (322 loc) · 9.77 KB
/
main.go
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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"
"github.com/Songmu/prompter"
"github.com/dixonwille/wmenu"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/client"
"github.com/kr/pretty"
"golang.org/x/net/context"
"gopkg.in/yaml.v2"
)
//Config defines model for storing account details in database
type Config struct {
Name string `json:"name"`
Version string `json:"version"`
License string `json:"license"`
Client Client `json:"client"`
Channel map[string]Channel `json:"channels"`
}
type Crypto struct {
Name string
Domain string
EnableNodeOUs bool
Specs []Spec
}
type Spec struct {
Hostname string
CommonName string
}
type Client struct {
TlsEnable bool `json:"tlsEnable"`
AdminUser string `json:"adminUser"`
}
type Channel struct {
Peers map[string]Peer `json:"peers"`
}
type Peer struct {
}
func pullCli(ver string) {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}
out, err := cli.ImagePull(ctx, "hyperledger/fabric-tools:"+ver, types.ImagePullOptions{})
if err != nil {
panic(err)
}
defer out.Close()
io.Copy(os.Stdout, out)
}
func listCli(peer string) (string, string) {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}
var peerContainer types.Container
var network string
c := types.ContainerListOptions{}
if list, err := cli.ContainerList(ctx, c); err != nil {
panic(err)
} else {
for _, container := range list {
if strings.Split(container.Names[0], "/")[1] == peer {
fmt.Printf("Found : %s\n", peer)
fmt.Print(container.HostConfig.NetworkMode)
network = container.HostConfig.NetworkMode
peerContainer = container
break
}
}
}
cc := types.ExecConfig{AttachStdout: true, AttachStderr: true, Cmd: []string{"peer", "channel", "list"}, Env: []string{"FABRIC_LOGGING_SPEC=critical"}}
execID, _ := cli.ContainerExecCreate(ctx, peerContainer.ID, cc)
config := types.ExecStartCheck{}
res, err := cli.ContainerExecAttach(ctx, execID.ID, config)
if err != nil {
panic(err)
}
err = cli.ContainerExecStart(ctx, execID.ID, types.ExecStartCheck{})
if err != nil {
panic(err)
}
var dfltChannel string
actChannelFunc := func(opts []wmenu.Opt) error {
for _, opt := range opts {
dfltChannel = opt.Value.(string)
fmt.Printf("%s is selected\n", dfltChannel)
}
return nil
}
menu := wmenu.NewMenu("Choose an default channel to connect network")
menu.Action(actChannelFunc)
content, _, _ := res.Reader.ReadLine() // Skip the first line
for content, _, err = res.Reader.ReadLine(); err == nil; content, _, err = res.Reader.ReadLine() {
ch := string(content)
// fmt.Println(ch)
menu.Option(ch, ch, false, nil)
}
err = menu.Run()
if err != nil {
log.Fatal(err)
}
return network, dfltChannel
}
func discoverPeers(peer string, net string, ch string, domain string, mspid string) {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}
skname := "6b3cf4905408799eecf955fbb31325d24356c1cd042069a72887ab24d2455863_sk"
cmd := fmt.Sprintf(`discover --configFile conf.yaml \
--peerTLSCA=tls/ca.crt \
--userKey=msp/keystore/%s \
--userCert=msp/signcerts/User1@%s-cert.pem \
--MSP %s \
saveConfig; \
discover --configFile conf.yaml \
peers \
--channel %s \
--server %s:7051`, skname, domain, mspid, ch, peer)
fmt.Println(cmd)
resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: "hyperledger/fabric-tools:1.4.2",
Cmd: []string{"sh", "-c", cmd},
Tty: true,
AttachStdout: true,
AttachStderr: true,
WorkingDir: "/etc/hyperledger/fabric",
}, &container.HostConfig{
// AutoRemove: true,
NetworkMode: container.NetworkMode(net),
Mounts: []mount.Mount{
{
Type: mount.TypeBind,
Source: "/home/atsushi/hyperledger/bc-explorer-setup/crypto-config/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp",
Target: "/etc/hyperledger/fabric/msp",
},
{
Type: mount.TypeBind,
Source: "/home/atsushi/hyperledger/bc-explorer-setup/crypto-config/peerOrganizations/org1.example.com/users/User1@org1.example.com/tls",
Target: "/etc/hyperledger/fabric/tls",
},
},
}, nil, "")
if err != nil {
panic(err)
}
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
panic(err)
}
statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning)
select {
case err := <-errCh:
if err != nil {
panic(err)
}
case <-statusCh:
}
out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStderr: true, ShowStdout: true})
if err != nil {
panic(err)
}
data, _ := ioutil.ReadAll(out)
// fmt.Println(string(data))
discResp := []DiscoverResp{}
json.Unmarshal(data, &discResp)
// fmt.Println(discResp)
for _, node := range discResp {
fmt.Println(node.Endpoint)
}
}
type DiscoverResp struct {
MSPID string
LedgerHeight string
Endpoint string
Identity string
Chaincodes []string
}
func main() {
config := Config{}
config.Name = "first-network-generated"
config.Version = "1.0.0"
config.License = "Apache-2.0"
config.Client.AdminUser = "admin"
if prompter.YN("TLS enabled ?", true) {
config.Client.TlsEnable = true
} else {
config.Client.TlsEnable = false
}
cid := prompter.Prompt("Enter channel ID", "mychannel")
config.Channel = make(map[string]Channel)
config.Channel[cid] = Channel{Peers: make(map[string]Peer)}
channel := config.Channel[cid]
channel.Peers["peer0.org1.example.com"] = Peer{}
fabricLoc := prompter.Choose("Where is your Fabric network located?", []string{"local", "remote"}, "local")
explorerBoot := prompter.Choose("How to bring up explorer?", []string{"source", "docker"}, "source")
fmt.Println(fabricLoc, ":", explorerBoot)
org := make(map[interface{}]interface{})
actOrgFunc := func(opts []wmenu.Opt) error {
for _, opt := range opts {
org = opt.Value.(map[interface{}]interface{})
fmt.Printf("%s has an id of %d. %s\n", opt.Text, opt.ID, org["MSPDir"].(string))
}
return nil
}
menu := wmenu.NewMenu("Choose an organization used to connect network")
menu.Action(actOrgFunc)
// menu.AllowMultiple()
// menu.SetSeparator(",")
configdata, _ := ioutil.ReadFile("./configtx.yaml")
m := make(map[interface{}]interface{})
yaml.Unmarshal(configdata, &m)
configurationsarray := m["Organizations"].([]interface{})
for _, e := range configurationsarray {
ee := e.(map[interface{}]interface{})
menu.Option(ee["Name"].(string), ee, false, nil)
// pretty.Printf("--- configurations:\n%# v\n\n", ee)
}
err := menu.Run()
if err != nil {
log.Fatal(err)
}
mspDirPath := strings.Split(org["MSPDir"].(string), "/")
domain := mspDirPath[len(mspDirPath)-2]
fmt.Println(domain)
orgCrypto := make(map[interface{}]interface{})
cryptoConfig, _ := ioutil.ReadFile("./crypto-config.yaml")
n := make(map[interface{}]interface{})
yaml.Unmarshal(cryptoConfig, &n)
peerOrgArray := n["PeerOrgs"].([]interface{})
for _, e := range peerOrgArray {
ee := e.(map[interface{}]interface{})
pretty.Printf("--- Crypto Config:\n%# v\n\n", ee)
if ee["Domain"].(string) == domain {
orgCrypto = ee
break
}
}
fmt.Println(orgCrypto["Name"].(string))
if v, ok := orgCrypto["Specs"]; ok {
array := v.([]interface{})
for _, e := range array {
spec := e.(map[interface{}]interface{})
fmt.Println("HOSTNAME:" + spec["Hostname"].(string))
if cmnName, ok := spec["CommonName"]; ok {
fmt.Println("CN:" + cmnName.(string))
} else {
fmt.Println("CN:" + spec["Hostname"].(string) + "." + orgCrypto["Domain"].(string))
}
}
}
var dfltPeer string
actPeerFunc := func(opts []wmenu.Opt) error {
for _, opt := range opts {
dfltPeer = opt.Value.(string)
fmt.Printf("%s is selected\n", dfltPeer)
}
return nil
}
menu = wmenu.NewMenu("Choose a peer to use as default peer")
menu.Action(actPeerFunc)
// menu.AllowMultiple()
// menu.SetSeparator(",")
if v, ok := orgCrypto["Template"]; ok {
template := v.(map[interface{}]interface{})
startIdx := 0
if start, ok := template["Start"]; ok {
startIdx = start.(int)
}
for i := startIdx; i < startIdx+template["Count"].(int); i++ {
// fmt.Printf("HOSTNAME:peer%d\n", i)
// fmt.Printf("CN:peer%d.%s\n", i, orgCrypto["Domain"].(string))
cn := fmt.Sprintf("peer%d.%s", i, orgCrypto["Domain"].(string))
menu.Option(cn, cn, false, nil)
}
}
err = menu.Run()
if err != nil {
log.Fatal(err)
}
// pullCli("1.4.2")
networkName, dfltChannel := listCli(dfltPeer)
discoverPeers(dfltPeer, networkName, dfltChannel, domain, org["ID"].(string))
bytes, err := json.Marshal(config)
if err != nil {
return
}
fmt.Println(string(bytes))
err = ioutil.WriteFile(config.Name+".json", []byte(string(bytes)), 0644)
if err != nil {
return
}
// //Parse json request body and use it to set fields on config
// //Note that config is passed as a pointer variable so that it's fields can be modified
// err := json.NewDecoder(r.Body).Decode(&config)
// if err != nil{
// panic(err)
// }
// //Set CreatedAt field on user to current local time
// user.CreatedAt = time.Now().Local()
// //Marshal or convert user object back to json and write to response
// userJson, err := json.Marshal(user)
// if err != nil{
// panic(err)
// }
// //Set Content-Type header so that clients will know how to read response
// w.Header().Set("Content-Type","application/json")
// w.WriteHeader(http.StatusOK)
// //Write json response back to response
// w.Write(userJson)
}