This repository has been archived by the owner on Aug 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ipfs.go
352 lines (290 loc) · 9.67 KB
/
ipfs.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
package service
import (
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"time"
"go.dedis.ch/onet/v3/log"
)
// setIPFSVariables set service variables useful for IPFS and config folders
func (s *Service) setIPFSVariables() {
// get config path
// e.g $GOPATH/src/github.com/dedis/student_19_cruxIPFS/simulation/build
pwd, err := os.Getwd()
checkErr(err)
//pwd := "/users/guissou"
configPath := filepath.Join(pwd, ConfigsFolder)
// set the port range allocated to s
s.MinPort = BaseHostPort + s.getNodeID()*MaxPortNumberPerHost
s.MaxPort = s.MinPort + MaxPortNumberPerHost
s.ConfigPath = filepath.Join(configPath, s.Name)
checkErr(CreateEmptyDir(s.ConfigPath))
// create own config home folder and ipfs config folder
s.MyIPFSPath = filepath.Join(s.ConfigPath, IPFSFolder)
s.MyIPFS = make([]IPFSInformation, 0)
}
// StartIPFS starts an IPFS instance for the given service
// return the multiaddress of the IPFS API
func (s *Service) StartIPFS(secret string) string {
path := s.MyIPFSPath + "-" + secret
checkErr(CreateEmptyDir(path))
cmd := "ipfs -c" + path + " init"
ou, err := exec.Command("bash", "-c", cmd).Output()
if err != nil {
log.Lvl1(string(ou))
log.Error(err)
}
s.PortMutex.Lock()
// edit the ip in the config file
ipfsAPI := s.EditIPFSConfig(path)
// start ipfs daemon
go func() {
cmd := "ipfs -c" + path + " daemon"
o, err := exec.Command("bash", "-c", cmd).Output()
fmt.Println("ipfs at ip", s.Name, "crashed")
fmt.Println(cmd)
fmt.Println(string(o))
fmt.Println(err)
}()
// wait until it has started
time.Sleep(IPFSStartupTime)
s.PortMutex.Unlock()
return ipfsAPI
}
// EditIPFSConfig edit the ipfs configuration file (mainly the ip)
// returns the API address of the IPFS instance
func (s *Service) EditIPFSConfig(path string) string {
addr := IPVersion + s.MyIP + TransportProtocol
// select available ports
ports, err := GetNextAvailablePorts(s.MinPort, s.MaxPort, IPFSPortNumber)
checkErr(err)
// [\"/ip4/0.0.0.0/tcp/5001\", \"/ip6/::/tcp/5001\"]
//swarmList := []string{addr + SwarmPort}
Swarm := MakeJSONArray([]string{addr +
strconv.Itoa((*ports)[0])})
// /ip4/127.0.0.1/tcp/5001
API := MakeJSONElem(addr + strconv.Itoa((*ports)[1]))
// /ip4/127.0.0.1/tcp/8080
Gateway := MakeJSONElem(addr + strconv.Itoa((*ports)[2]))
EditIPFSField(path, "Addresses.API", API)
EditIPFSField(path, "Addresses.Gateway", Gateway)
EditIPFSField(path, "Addresses.Swarm", Swarm)
// filling my IPFS info
s.MyIPFS = append(s.MyIPFS, IPFSInformation{
Name: s.Name,
IP: s.MyIP,
SwarmPort: (*ports)[0],
APIPort: (*ports)[1],
GatewayPort: (*ports)[2],
})
return addr + strconv.Itoa((*ports)[1])
}
// EditIPFSField with the native IPFS config command
func EditIPFSField(path, field, value string) {
cmd := "ipfs -c " + path + " config --json " + field + " " + value
o, err := exec.Command("bash", "-c", cmd).Output()
if err != nil {
fmt.Println(cmd)
fmt.Println(string(o))
fmt.Println(err)
}
}
// SetClusterLeaderConfig set the configs for the leader of a cluster
func (s *Service) SetClusterLeaderConfig(path, apiIPFSAddr string,
replmin, replmax int, ports ClusterInstance) (
string, string, error) {
// generate random secret
key := make([]byte, 32)
_, err := rand.Read(key)
if err != nil {
return "", "", errors.New("could not generate secret")
}
secret := hex.EncodeToString(key)
vars := GetClusterVariables(path, s.MyIP, s.Name, secret, apiIPFSAddr,
replmin, replmax, ports)
return vars, secret, nil
}
// GetClusterVariables get the cluster variables
func GetClusterVariables(path, ip, secret, peername, apiIPFSAddr string,
replmin, replmax int, ports ClusterInstance) string {
//apiIPFSAddr := IPVersion + ip +
// TransportProtocol + strconv.Itoa(ports.IPFSAPIPort) // 5001
restAPIAddr := IPVersion + ip +
TransportProtocol + strconv.Itoa(ports.RestAPIPort) // 9094
IPFSProxyAddr := IPVersion + ip +
TransportProtocol + strconv.Itoa(ports.IPFSProxyPort) // 9095
clusterAddr := IPVersion + ip +
TransportProtocol + strconv.Itoa(ports.ClusterPort) // 9096
cmd := ""
// edit peername
cmd += GetEnvVar("CLUSTER_PEERNAME", peername)
// edit the secret
cmd += GetEnvVar("CLUSTER_SECRET", secret)
cmd += GetEnvVar("CLUSTER_REPLICATIONFACTORMIN", "3")
cmd += GetEnvVar("CLUSTER_REPLICATIONFACTORMAX", "3")
// replace IPFS API port (5001)
cmd += GetEnvVar("CLUSTER_IPFSPROXY_NODEMULTIADDRESS", apiIPFSAddr) // 5001
cmd += GetEnvVar("CLUSTER_IPFSHTTP_NODEMULTIADDRESS", apiIPFSAddr) // 5001
// replace Cluster ports (9094, 9095, 9096)
cmd += GetEnvVar("CLUSTER_RESTAPI_HTTPLISTENMULTIADDRESS", restAPIAddr) // 9094
cmd += GetEnvVar("CLUSTER_IPFSPROXY_LISTENMULTIADDRESS", IPFSProxyAddr) // 9095
cmd += GetEnvVar("CLUSTER_LISTENMULTIADDRESS", clusterAddr) // 9096
// replace replication factor
cmd += GetEnvVar("CLUSTER_REPLICATIONFACTORMIN", strconv.Itoa(replmin))
cmd += GetEnvVar("CLUSTER_REPLICATIONFACTORMAX", strconv.Itoa(replmax))
return cmd
}
// GetEnvVar get the environnment variable for the given field and value
func GetEnvVar(field, value string) string {
// `CLUSTER_FIELD="value" `
return field + "=\"" + value + "\" "
}
// MakeJSONElem make a JSON single element
func MakeJSONElem(elem string) string {
// \"elem\"
return "\\\"" + elem + "\\\""
}
// MakeJSONArray make a json array from the given elements
func MakeJSONArray(elements []string) string {
// "[
str := "\"["
for _, e := range elements {
// \"elem\"
str += "\\\"" + e + "\\\""
}
// str + ]"
return str + "]\""
}
// SetupClusterLeader setup a cluster instance for the ARA leader
func (s *Service) SetupClusterLeader(path, secret, apiIPFSAddr string,
replmin, replmax int) (string, *ClusterInstance, error) {
//path = path //+ "-" + secret
if err := CreateEmptyDir(path); err != nil {
return "", nil, err
}
s.PortMutex.Lock()
ints, err := GetNextAvailablePorts(s.MinPort, s.MaxPort, ClusterPortNumber)
if err != nil {
return "", nil, err
}
// set the ports that the cluster will use
ports := ClusterInstance{
HostName: s.Name,
IP: IPVersion + s.MyIP + TransportProtocol,
IPFSAPIAddr: apiIPFSAddr,
RestAPIPort: (*ints)[0],
IPFSProxyPort: (*ints)[1],
ClusterPort: (*ints)[2],
}
// get the environment variables to set cluster configs
vars := GetClusterVariables(path, s.MyIP, secret, s.Name, apiIPFSAddr,
replmin, replmax, ports)
// init command to be run
cmd := vars + "ipfs-cluster-service -c " + path + " init"
if ClusterConsensusMode == "crdt" {
cmd += " --consensus crdt"
}
o, err := exec.Command("bash", "-c", cmd).Output()
if err != nil {
fmt.Println(cmd)
fmt.Println(string(o))
fmt.Println(err)
return "", nil, err
}
// start cluster daemon
cmd = "ipfs-cluster-service -c " + path + " daemon"
go func() {
exec.Command("bash", "-c", cmd).Run()
log.Lvl1(s.Name + " cluster leader crashed")
}()
// wait for the daemon to be launched
time.Sleep(ClusterStartupTime)
s.PortMutex.Unlock()
addr := IPVersion + s.MyIP + TransportProtocol + strconv.Itoa(ports.RestAPIPort)
log.Lvl2("Started ipfs-cluster leader at " + addr)
return secret, &ports, nil
}
// SetupClusterSlave setup a cluster slave instance
func (s *Service) SetupClusterSlave(path, bootstrap, secret, apiIPFSAddr string,
replmin, replmax int) (*ClusterInstance, error) {
err := CreateEmptyDir(path)
if err != nil {
return nil, err
}
s.PortMutex.Lock()
ints, err := GetNextAvailablePorts(s.MinPort, s.MaxPort, ClusterPortNumber)
if err != nil {
log.Lvl1(err)
return nil, err
}
// set the ports that the cluster will use
ports := ClusterInstance{
HostName: s.Name,
IP: IPVersion + s.MyIP + TransportProtocol,
IPFSAPIAddr: apiIPFSAddr,
RestAPIPort: (*ints)[0],
IPFSProxyPort: (*ints)[1],
ClusterPort: (*ints)[2],
}
// get the environment variables to set cluster configs
vars := GetClusterVariables(path, s.MyIP, secret, s.Name, apiIPFSAddr,
replmin, replmax, ports)
// init command to be run
cmd := vars + "ipfs-cluster-service -c " + path + " init"
if ClusterConsensusMode == "crdt" {
cmd += " --consensus crdt"
}
err = exec.Command("bash", "-c", cmd).Run()
if err != nil {
log.Lvl1(err)
return nil, err
}
// start cluster daemon
cmd = "ipfs-cluster-service -c " + path + " daemon --bootstrap " + bootstrap
go func() {
o, err := exec.Command("bash", "-c", cmd).Output()
log.Lvl1("slave " + s.Name + " crashed")
log.Lvl1(string(o))
log.Lvl1(err)
log.Lvl1(cmd)
}()
// wait for the daemon to be launched
time.Sleep(ClusterStartupTime)
s.PortMutex.Unlock()
addr := ports.IP + strconv.Itoa(ports.RestAPIPort)
//log.Lvl1("ipfs-cluster-service -c " + path + " daemon --bootstrap " + bootstrap)
log.Lvl2("Started ipfs-cluster slave at " + addr)
return &ports, nil
}
// StartIPFSAndCluster starts an IPFS instance along with the cluster instance
// empty secret means that this instance is the cluster leader
// if leader, returns secret and the started instance information containing
// bootstrap address for slaves
func (s *Service) StartIPFSAndCluster(leader, secret,
bootstrap string) *ClusterInstance {
clusterPath := filepath.Join(s.ConfigPath, leader+"-"+secret)
// if no secret -> this instance is the leader
if secret == "" {
secret = genSecret()
}
// start ipfs and get the multiaddr of the IPFS API
apiIPFSAddr := s.StartIPFS(secret)
var err error
var instance *ClusterInstance
if bootstrap == "" {
// leader
_, instance, err = s.SetupClusterLeader(clusterPath, secret, apiIPFSAddr,
DefaultReplMin, DefaultReplMax)
} else {
// slave
instance, err = s.SetupClusterSlave(clusterPath, bootstrap, secret, apiIPFSAddr,
DefaultReplMin, DefaultReplMax)
}
checkErr(err)
return instance
}