forked from subutai-io/p2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow.go
231 lines (216 loc) · 5.08 KB
/
show.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
ptp "github.com/subutai-io/p2p/lib"
)
// ShowOutput is a JSON object for output from `show` command
type ShowOutput struct {
ID string `json:"id"`
IP string `json:"ip"`
Endpoint string `json:"endpoint"`
HardwareAddress string `json:"hw"`
Error string `json:"error"`
Text string `json:"text"`
Code int `json:"code"`
InterfaceName string `json:"interface"`
Hash string `json:"hash"`
}
// Show outputs information about P2P instances and interfaces
func CommandShow(queryPort int, hash, ip string, interfaces, all bool) {
req := &request{}
if hash != "" {
req.Hash = hash
} else {
req.Hash = ""
}
req.IP = ip
req.Interfaces = interfaces
req.All = all
out, err := sendRequestRaw(queryPort, "show", req)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
show := []ShowOutput{}
err = json.Unmarshal(out, &show)
if err != nil {
fmt.Printf("Failed to unmarshal JSON. Error %s\n", err)
os.Exit(99)
}
if req.Hash != "" {
if req.IP != "" {
for _, m := range show {
if m.Code != 0 {
fmt.Println(m.Error)
} else {
fmt.Println(m.Text)
}
os.Exit(m.Code)
}
fmt.Println("No data available")
os.Exit(102)
} else {
fmt.Println("< Peer ID >\t< IP >\t< Endpoint >\t< HW >")
for _, m := range show {
if m.Code != 0 {
fmt.Println(m.Error)
os.Exit(m.Code)
}
fmt.Printf("%s\t%s\t%s\t%s\n", m.ID, m.IP, m.Endpoint, m.HardwareAddress)
}
os.Exit(0)
}
}
if req.Interfaces {
for _, m := range show {
if m.Code != 0 {
fmt.Println(m.Error)
os.Exit(m.Code)
}
fmt.Println(m.InterfaceName)
}
os.Exit(0)
}
for _, m := range show {
if m.Code != 0 {
fmt.Println(m.Error)
os.Exit(m.Code)
}
fmt.Printf("%s\t%s\t%s\n", m.HardwareAddress, m.IP, m.Hash)
}
os.Exit(0)
}
func (d *Daemon) execRESTShow(w http.ResponseWriter, r *http.Request) {
args := new(DaemonArgs)
err := getJSON(r.Body, args)
if handleMarshalError(err, w) != nil {
return
}
output, err := d.Show(&request{
Hash: args.Hash,
IP: args.IP,
Interfaces: args.Interfaces,
All: args.All,
})
if err != nil {
ptp.Log(ptp.Error, "Internal error: %s", err)
return
}
w.Write(output)
}
// Show is used to output information about instances
func (d *Daemon) Show(args *request) ([]byte, error) {
if !ReadyToServe {
out := []ShowOutput{ShowOutput{Error: "P2P Daemon is in initialization mode. Can't handle request", Code: 105}}
return d.showOutput(out)
}
if args.Hash != "" {
inst := d.Instances.GetInstance(args.Hash)
if inst != nil {
if args.IP != "" {
out, err := d.showIP(args.IP, inst)
return out, err
}
out, err := d.showHash(inst)
return out, err
}
return d.showOutput([]ShowOutput{
ShowOutput{
Error: "Specified environment was not found",
Code: 15,
},
})
} else if args.Interfaces {
if !args.All {
return d.showInterfaces()
}
return d.showAllInterfaces()
} else {
return d.showInstances()
}
return nil, nil
}
func (d *Daemon) showOutput(data []ShowOutput) ([]byte, error) {
return json.Marshal(data)
}
func (d *Daemon) showIP(ip string, instance *P2PInstance) ([]byte, error) {
peers := instance.PTP.Peers.Get()
for _, peer := range peers {
if peer.PeerLocalIP.String() == ip {
if peer.State == ptp.PeerStateConnected {
out := []ShowOutput{
ShowOutput{
Text: "Integrated with " + ip,
Code: 0,
},
}
return d.showOutput(out)
}
}
}
out := []ShowOutput{
ShowOutput{
Error: "Not yet integrated with " + ip,
Code: 12,
},
}
return d.showOutput(out)
}
func (d *Daemon) showHash(instance *P2PInstance) ([]byte, error) {
peers := instance.PTP.Peers.Get()
out := []ShowOutput{}
for _, peer := range peers {
s := ShowOutput{
ID: peer.ID,
IP: peer.PeerLocalIP.String(),
Endpoint: peer.Endpoint.String(),
HardwareAddress: peer.PeerHW.String(),
}
out = append(out, s)
}
return d.showOutput(out)
}
func (d *Daemon) showInterfaces() ([]byte, error) {
instances := d.Instances.Get()
out := []ShowOutput{}
for _, inst := range instances {
if inst.PTP != nil {
s := ShowOutput{InterfaceName: inst.PTP.Interface.GetName()}
out = append(out, s)
}
}
return d.showOutput(out)
}
func (d *Daemon) showAllInterfaces() ([]byte, error) {
out := []ShowOutput{}
for _, inf := range InterfaceNames {
s := ShowOutput{InterfaceName: inf}
out = append(out, s)
}
return d.showOutput(out)
}
func (d *Daemon) showInstances() ([]byte, error) {
instances := d.Instances.Get()
out := []ShowOutput{}
for key, inst := range instances {
if inst.PTP != nil {
s := ShowOutput{
HardwareAddress: inst.PTP.Interface.GetHardwareAddress().String(),
IP: inst.PTP.Interface.GetIP().String(),
Hash: key,
}
out = append(out, s)
} else {
s := ShowOutput{
HardwareAddress: "Unknown",
IP: "Unknown",
Hash: key,
}
out = append(out, s)
}
}
return d.showOutput(out)
}