forked from subutai-io/p2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstop.go
110 lines (104 loc) · 2.57 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
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.Println(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)
}
args := new(DaemonArgs)
err := getJSON(r.Body, args)
if handleMarshalError(err, w) != nil {
return
}
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.StopInstance()
p.Instances.Delete(args.Hash)
p.Instances.SaveInstances(p.SaveFile)
k := 0
for k, i := range usedIPs {
if i != ip {
usedIPs[k] = i
k++
}
}
usedIPs = usedIPs[:k]
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
}