/
channel.go
148 lines (132 loc) · 3.24 KB
/
channel.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
package entity
import (
"encoding/json"
"fmt"
"github.com/fabric-lab/hyperledger-fabric-manager/server/pkg/store"
"github.com/fabric-lab/hyperledger-fabric-manager/server/pkg/util"
profileConfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig"
yaml "gopkg.in/yaml.v2"
"io/ioutil"
"path/filepath"
"strings"
)
type Channel struct {
Name string
Consortium string
Desc string
OrdererName string
OrdererEndpoint string
State string
}
func (c *Channel) Create() error {
Path(channelDir, c.Name)
o, err := getOrderByName(c.OrdererName)
if err != nil {
return err
}
c.OrdererEndpoint = fmt.Sprintf("%s:%d", o.ListenAddress, o.ListenPort)
return nil
}
func (c *Channel) Exec(cmdInfo map[string]string) string {
cmdInfo["ChannelId"] = c.Name
cmdInfo["OrdererEndpoint"] = c.OrdererEndpoint
err := c.config(cmdInfo)
if err != nil {
return err.Error()
}
str := ExecChannel(cmdInfo)
//check output if exist "Error:",not accuracy,to do next
index := strings.Index(str, "Error:")
if index == -1 {
c.State = "enable"
} else {
c.State = "disable"
}
b, err := json.Marshal(c)
if err != nil {
return err.Error()
}
err = store.Bt.AddJson(channels, c.Name, b)
if err != nil {
return err.Error()
}
return c.State
}
func (c *Channel) config(cmdInfo map[string]string) error {
path := filepath.Join(channelDir, c.Name)
profileBytes, err := c.configChannelProfile(path, c.Consortium)
if err != nil {
return err
}
err = SimpleWrite(path, configtxyml, profileBytes)
if err != nil {
return err
}
err = c.configCore(path)
if err != nil {
return err
}
return nil
}
func (c *Channel) configChannelProfile(path string, consortium string) ([]byte, error) {
orgs, err := configConsortiumOrgs(path, consortium)
if err != nil {
return nil, err
}
application := &profileConfig.Application{
Organizations: orgs,
}
profile := &profileConfig.Profile{
Application: application,
Consortium: consortium,
}
Profiles := make(map[string]*profileConfig.Profile)
Profiles["SampleSingleMSPChannel"] = profile
topLevel := &profileConfig.TopLevel{Profiles: Profiles}
b, err := yaml.Marshal(topLevel)
if err != nil {
return nil, err
}
s := string(b)
s = strings.Replace(s, "MaxMessageSize", "MaxMessageCount", 1)
return []byte(s), nil
}
func (c *Channel) configCore(path string) error {
consortium, err := getConsortiumByName(c.Consortium)
if err != nil {
return err
}
mspName := consortium.MspNames[0]
oname := strings.SplitN(mspName, ".", 2)[1]
localMSPID := fmt.Sprintf("%s.%s", "admin", oname)
template := filepath.Join(templateDir, configyml)
corepath := filepath.Join(path, coreYml)
b, err := ioutil.ReadFile(template)
if err != nil {
return err
}
str := string(b)
str = strings.Replace(str, "$LOCAL_MSP_ID", oname, -1)
err = ioutil.WriteFile(corepath, []byte(str), 0644)
if err != nil {
return err
}
msp, _ := getMspByName(localMSPID)
err = util.Copy(msp.Path, path)
if err != nil {
return err
}
return nil
}
func getChannelByName(cName string) (*Channel, error) {
var c *Channel
i, err := store.Bt.ViewByKey(channels, cName)
if err != nil {
return c, err
}
i = MapToEntity(i, channels)
if c, ok := i.(*Channel); ok {
return c, nil
}
return c, nil
}