-
Notifications
You must be signed in to change notification settings - Fork 45
/
stop.go
133 lines (127 loc) · 3.14 KB
/
stop.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
package main
import (
"fmt"
"net/http"
"os"
ptp "github.com/subutai-io/p2p/lib"
)
// CommandStop will terminate P2P instance
// Function will send a request to the /stop/ REST endpoint with
// specified hash that is needed to stop or interface name that's
// needed to be removed from saved interfaces list
func CommandStop(rpcPort int, hash, dev string) {
args := &DaemonArgs{}
if hash != "" {
args.Hash = hash
args.Dev = ""
} else if dev != "" {
args.Dev = dev
args.Hash = ""
} else {
fmt.Printf("Not enough parameters for stop command")
return
}
out, err := sendRequest(rpcPort, "stop", args)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
fmt.Println(out.Message)
os.Exit(out.Code)
}
func (d *Daemon) execRESTStop(w http.ResponseWriter, r *http.Request) {
if !ReadyToServe {
resp, _ := getResponse(105, "P2P Daemon is in initialization state")
w.Write(resp)
return
}
if !bootstrap.isActive {
resp, _ := getResponse(106, "Not connected to DHT nodes")
w.Write(resp)
return
}
if bootstrap.ip == "" {
resp, _ := getResponse(107, "Didn't received outbound IP yet")
w.Write(resp)
return
}
args := new(DaemonArgs)
err := getJSON(r.Body, args)
if handleMarshalError(err, w) != nil {
return
}
ptp.Log(ptp.Debug, "Executing stop command: %+v", args)
response := new(Response)
d.Stop(&DaemonArgs{
Hash: args.Hash,
Dev: args.Dev,
}, response)
resp, err := getResponse(response.ExitCode, response.Output)
if err != nil {
ptp.Log(ptp.Error, "Internal error: %s", err)
return
}
w.Write(resp)
}
// Stop is used to terminate a specific P2P instance
func (p *Daemon) Stop(args *DaemonArgs, resp *Response) error {
resp.ExitCode = 0
if args.Hash != "" {
inst := p.Instances.getInstance(args.Hash)
if inst == nil {
resp.ExitCode = 1
resp.Output = "Instance with hash " + args.Hash + " was not found"
return nil
} else {
ip := inst.PTP.Interface.GetIP().String()
resp.Output = "Shutting down " + args.Hash
inst.PTP.Close()
p.Instances.delete(args.Hash)
if p.Restore.isActive() {
err := p.Restore.removeEntry(args.Hash)
if err != nil {
ptp.Log(ptp.Error, err.Error())
} else {
err := p.Restore.save()
if err != nil {
ptp.Log(ptp.Error, "Failed to save dump: %s", err.Error())
}
}
}
k := 0
for k, i := range usedIPs {
if i != ip {
usedIPs[k] = i
k++
}
}
usedIPs = usedIPs[:k]
bootstrap.unregisterInstance(args.Hash)
return nil
}
} else if args.Dev != "" {
resp.Output = "Removing " + args.Dev
instances := p.Instances.get()
for i, inf := range InterfaceNames {
if inf == args.Dev {
for _, instance := range instances {
if instance.PTP.Interface.GetName() == args.Dev {
resp.ExitCode = 12
resp.Output += "Can't remove interface: In use"
return nil
}
}
InterfaceNames = append(InterfaceNames[:i], InterfaceNames[i+1:]...)
resp.ExitCode = 0
resp.Output += "Removed " + args.Dev
return nil
}
}
resp.ExitCode = 1
resp.Output += "Interface was not found"
return nil
}
resp.ExitCode = 2
resp.Output = "Not enough parameters for stop"
return nil
}