-
Notifications
You must be signed in to change notification settings - Fork 130
/
joinBySnapshot.go
200 lines (182 loc) · 6.93 KB
/
joinBySnapshot.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
package operations
import (
"context"
"fmt"
"net/url"
"os"
"strings"
"github.com/davecgh/go-spew/spew"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/hyperledger/fabric-test/tools/operator/connectionprofile"
"github.com/hyperledger/fabric-test/tools/operator/logger"
"github.com/hyperledger/fabric-test/tools/operator/networkclient"
"github.com/hyperledger/fabric-test/tools/operator/paths"
"github.com/hyperledger/fabric-test/tools/operator/testclient/inputStructs"
)
//JoinBySnapshotUIObject --
type JoinBySnapshotUIObject struct {
TLS string `json:"TLS,omitempty"`
ChannelOpt ChannelOptions `json:"ChannelOpt,omitempty"`
ConnProfilePath string `json:"ConnProfilePath,omitempty"`
TargetPeers []string `json:"targetPeers,omitempty"`
SnapshotPath string
SnapshotPeer string
}
//JoinBySnapshot -- To join channel using snapshot
func (j JoinBySnapshotUIObject) JoinBySnapshot(config inputStructs.Config, tls string) error {
var joinBySnapshotObjects []JoinBySnapshotUIObject
var joinBySnapshotConfigObjects []interface{}
var configObjects []inputStructs.JoinChannelBySnapshot
configObjects = config.JoinChannelBySnapshot
// print action (in bold) and input
fmt.Printf("\033[1m\nAction:joinBySnapshot\nInput:\033[0m\n%s\n", spew.Sdump(configObjects))
for index := 0; index < len(configObjects); index++ {
ccObjects := j.createJoinBySnapshotObjects(configObjects[index], config.Organizations, tls)
joinBySnapshotObjects = append(joinBySnapshotObjects, ccObjects...)
joinBySnapshotConfigObjects = append(joinBySnapshotConfigObjects, &config.JoinChannelBySnapshot[index])
}
err := j.joinBySnapshot(joinBySnapshotObjects)
if err != nil {
return err
}
var connProfileObject connectionprofile.ConnProfile
err = connProfileObject.UpdateConnectionProfiles(joinBySnapshotConfigObjects, config.Organizations, "joinBySnapshot")
if err != nil {
return err
}
return nil
}
//createSnapshotObjects -- To create snapshot objects for snapshot
func (j JoinBySnapshotUIObject) createJoinBySnapshotObjects(ccObject inputStructs.JoinChannelBySnapshot, organizations []inputStructs.Organization, tls string) []JoinBySnapshotUIObject {
var joinBySnapshotObjects []JoinBySnapshotUIObject
orgNames := strings.Split(ccObject.Organizations, ",")
targetPeers := strings.Split(ccObject.TargetPeers, ",")
snapshotConfig := strings.Split(ccObject.SnapshotPath, ":")
j = JoinBySnapshotUIObject{
TLS: tls,
ChannelOpt: ChannelOptions{
OrgName: orgNames,
Name: ccObject.ChannelName,
},
TargetPeers: targetPeers,
SnapshotPeer: snapshotConfig[0],
SnapshotPath: snapshotConfig[1],
ConnProfilePath: paths.GetConnProfilePath(orgNames, organizations),
}
joinBySnapshotObjects = append(joinBySnapshotObjects, j)
return joinBySnapshotObjects
}
//joinBysnapshotCLI -- to join the channel using snapshot
func (j JoinBySnapshotUIObject) joinBySnapshotCLI(joinBySnapshotObject JoinBySnapshotUIObject) error {
for _, orgName := range joinBySnapshotObject.ChannelOpt.OrgName {
currentDir, err := paths.GetCurrentDir()
if err != nil {
return err
}
var connProfilePath string
if strings.Contains(joinBySnapshotObject.ConnProfilePath, ".yaml") || strings.Contains(joinBySnapshotObject.ConnProfilePath, ".json") {
connProfilePath = joinBySnapshotObject.ConnProfilePath
} else {
connProfilePath = fmt.Sprintf("%s/connection_profile_%s.yaml", joinBySnapshotObject.ConnProfilePath, orgName)
}
for _, peerName := range joinBySnapshotObject.TargetPeers {
peerOrgName := strings.Split(peerName, "-")
if peerOrgName[1] == orgName {
connProfConfig, err := ConnProfileInformationForOrg(connProfilePath, orgName)
if err != nil {
return err
}
peerURL, err := url.Parse(connProfConfig.Peers[peerName].URL)
if err != nil {
logger.ERROR("Failed to get peer url from connection profile")
return err
}
peerAddress := peerURL.Host
err = SetEnvForCLI(orgName, peerName, connProfilePath, joinBySnapshotObject.TLS, currentDir)
if err != nil {
return err
}
args := []string{
"channel",
"joinbysnapshot",
"--snapshotpath",
}
os.Setenv("CORE_PEER_ADDRESS", peerAddress)
if os.Getenv("KUBECONFIG") != "" || strings.Contains(peerAddress, "127.0.0.1") {
err = j.copySnapshotDirectoryDocker(peerName, joinBySnapshotObject)
if err != nil {
return err
}
args = append(args, fmt.Sprintf("/var/hyperledger/production/snapshots/completed/%s", joinBySnapshotObject.SnapshotPath))
} else {
err = j.copySnapshotDirectoryK8s(peerName, joinBySnapshotObject)
if err != nil {
return err
}
args = append(args, fmt.Sprintf("/shared/data/snapshots/completed/%s", joinBySnapshotObject.SnapshotPath))
}
_, err = networkclient.ExecuteCommand("peer", args, true)
if err != nil {
return err
}
}
}
}
return nil
}
func (j JoinBySnapshotUIObject) copySnapshotDirectoryDocker(peer string, joinBySnapshotObject JoinBySnapshotUIObject) error {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return err
}
content, _, err := cli.CopyFromContainer(ctx, joinBySnapshotObject.SnapshotPeer, fmt.Sprintf("/var/hyperledger/production/snapshots/completed/%s/%s", joinBySnapshotObject.ChannelOpt.Name, joinBySnapshotObject.SnapshotPath))
if err != nil {
return err
}
options := types.CopyToContainerOptions{AllowOverwriteDirWithFile: true, CopyUIDGID: false}
err = cli.CopyToContainer(ctx, peer, "/var/hyperledger/production/snapshots/completed/", content, options)
if err != nil {
return err
}
defer content.Close()
return nil
}
//TODO: to replace kubectl call with api call
func (j JoinBySnapshotUIObject) copySnapshotDirectoryK8s(peer string, joinBySnapshotObject JoinBySnapshotUIObject) error {
copyFromArgs := []string{
"-n", "fabric-system-test",
"cp",
fmt.Sprintf("%s-0:/shared/data/snapshots/completed/%s/%s", joinBySnapshotObject.SnapshotPeer, joinBySnapshotObject.ChannelOpt.Name, joinBySnapshotObject.SnapshotPath),
fmt.Sprintf("/tmp/%s", joinBySnapshotObject.SnapshotPath),
"-c", "peer",
}
_, err := networkclient.ExecuteCommand("kubectl", copyFromArgs, true)
if err != nil {
return err
}
copyToArgs := []string{
"-n", "fabric-system-test",
"cp",
fmt.Sprintf("/tmp/%s", joinBySnapshotObject.SnapshotPath),
fmt.Sprintf("%s-0:/shared/data/snapshots/completed/%s", peer, joinBySnapshotObject.SnapshotPath),
"-c", "peer",
}
_, err = networkclient.ExecuteCommand("kubectl", copyToArgs, true)
if err != nil {
return err
}
return nil
}
//joinBySnapshot -- To join peer to channel using snapshot
func (j JoinBySnapshotUIObject) joinBySnapshot(joinBySnapshotObjects []JoinBySnapshotUIObject) error {
var err error
for i := 0; i < len(joinBySnapshotObjects); i++ {
err = j.joinBySnapshotCLI(joinBySnapshotObjects[i])
if err != nil {
return err
}
}
return err
}