-
Notifications
You must be signed in to change notification settings - Fork 130
/
main.go
232 lines (217 loc) · 6.47 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
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"github.com/hyperledger/fabric-test/tools/operator/launcher"
"github.com/hyperledger/fabric-test/tools/operator/launcher/nl"
"github.com/hyperledger/fabric-test/tools/operator/logger"
"github.com/hyperledger/fabric-test/tools/operator/networkclient"
"github.com/hyperledger/fabric-test/tools/operator/networkspec"
"github.com/hyperledger/fabric-test/tools/operator/paths"
"github.com/hyperledger/fabric-test/tools/operator/testclient"
"github.com/pkg/errors"
)
var inputFilePath = flag.String("i", "", "Input file path (required)")
var kubeConfigPath = flag.String("k", "", "Kube config file path (optional)")
var action = flag.String("a", "up", "Set action (Available options up, down, create, join, install, instantiate, upgrade, invoke, query, createChannelTxn, migrate, health)")
func validateArguments(networkSpecPath *string, kubeConfigPath *string) error {
if *networkSpecPath == "" {
return errors.New("Input file not provided")
} else if *kubeConfigPath == "" {
logger.INFO("Kube config file not provided, proceeding with local environment")
}
return nil
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func doAction(action, env, kubeConfigPath, inputFilePath string) error {
var err error
var inputPath string
var config networkspec.Config
actions := []string{"up", "down", "createChannelTxn", "migrate", "health", "upgradeNetwork", "networkInSync", "updateCapability", "updatePolicy", "upgradeDB", "addPeer"}
if contains(actions, action) {
contents, _ := ioutil.ReadFile(inputFilePath)
contents = append([]byte("#@data/values \n"), contents...)
inputPath = paths.JoinPath(paths.TemplatesDir(), "input.yaml")
ioutil.WriteFile(inputPath, contents, 0644)
var network nl.Network
config, err = network.GetConfigData(inputPath)
if err != nil {
return err
}
}
switch action {
case "up":
err = launcher.Launcher("up", env, kubeConfigPath, inputPath)
if err != nil {
logger.ERROR("Failed to launch network")
return err
}
case "down":
err = launcher.Launcher("down", env, kubeConfigPath, inputPath)
if err != nil {
logger.ERROR("Failed to delete network")
return err
}
case "upgradeNetwork":
err = launcher.Launcher("upgradeNetwork", env, kubeConfigPath, inputPath)
if err != nil {
logger.ERROR("Failed to upgrade network")
return err
}
case "addPeer":
err = launcher.Launcher("addPeer", env, kubeConfigPath, inputPath)
if err != nil {
logger.ERROR("Failed to add peers to network")
return err
}
case "upgradeDB":
err = launcher.Launcher("upgradeDB", env, kubeConfigPath, inputPath)
if err != nil {
logger.ERROR("Failed to upgrade network database")
return err
}
case "updateCapability":
err = launcher.Launcher("updateCapability", env, kubeConfigPath, inputPath)
if err != nil {
return err
}
case "updatePolicy":
err = launcher.Launcher("updatePolicy", env, kubeConfigPath, inputPath)
if err != nil {
return err
}
case "create":
err = testclient.Testclient("create", inputFilePath)
if err != nil {
logger.ERROR("Failed to create channel in network")
return err
}
case "anchorpeer":
err = testclient.Testclient("anchorpeer", inputFilePath)
if err != nil {
logger.ERROR("Failed to add anchor peers to channel in network")
return err
}
case "join":
err = testclient.Testclient("join", inputFilePath)
if err != nil {
logger.ERROR("Failed to join peers to channel in network")
return err
}
case "joinBySnapshot":
err = testclient.Testclient("joinBySnapshot", inputFilePath)
if err != nil {
logger.ERROR("Failed to join peers to channel using snapshot in network")
return err
}
case "snapshot":
err = testclient.Testclient("snapshot", inputFilePath)
if err != nil {
logger.ERROR("Failed to create a snapshot on peer to channel in network")
return err
}
case "install":
err = testclient.Testclient("install", inputFilePath)
if err != nil {
logger.ERROR("Failed to install chaincode")
return err
}
case "instantiate":
err = testclient.Testclient("instantiate", inputFilePath)
if err != nil {
logger.ERROR("Failed to instantiate chaincode")
return err
}
case "upgrade":
err = testclient.Testclient("upgrade", inputFilePath)
if err != nil {
logger.ERROR("Failed to upgrade chaincode")
return err
}
case "invoke":
err = testclient.Testclient("invoke", inputFilePath)
if err != nil {
logger.ERROR("Failed to send invokes")
return err
}
case "query":
err = testclient.Testclient("query", inputFilePath)
if err != nil {
logger.ERROR("Failed to send queries")
return err
}
case "createChannelTxn":
configTxnPath := paths.ConfigFilesDir(false)
err = networkclient.GenerateChannelTransaction(config, configTxnPath)
if err != nil {
logger.ERROR("Failed to create channel transaction")
return err
}
case "migrate":
err = networkclient.MigrateToRaft(config, kubeConfigPath)
if err != nil {
logger.ERROR("Failed to migrate consensus to raft from ", config.Orderer.OrdererType)
return err
}
case "networkInSync":
err = networkclient.CheckNetworkInSync(config, kubeConfigPath)
if err != nil {
return err
}
case "command":
err = testclient.Testclient("command", inputFilePath)
if err != nil {
logger.ERROR("Failed to execute command function")
return err
}
case "health":
err = launcher.Launcher("health", env, kubeConfigPath, inputPath)
if err != nil {
logger.ERROR("Failed to check health of fabric components")
return err
}
default:
logger.ERROR("Incorrect action ", action, " provided. Use up or down or create or join or anchorpeer or install or instantiate or upgrade or invoke or query or createChannelTxn or migrate or health or upgradeNetwork for action ")
return err
}
return nil
}
func writeLogToAFile() {
f, err := os.OpenFile("text.log",
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
defer f.Close()
}
func main() {
flag.Parse()
validateArguments(inputFilePath, kubeConfigPath)
env := "docker"
if *kubeConfigPath != "" {
env = "k8s"
}
f, err := os.OpenFile("/tmp/orders.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer f.Close()
wrt := io.MultiWriter(f)
log.SetOutput(wrt)
err = doAction(*action, env, *kubeConfigPath, *inputFilePath)
if err != nil {
logger.ERROR(fmt.Sprintln("Operator failed with error ", err))
os.Exit(1)
}
}