forked from subutai-io/p2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
160 lines (149 loc) · 3.65 KB
/
status.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
ptp "github.com/subutai-io/p2p/lib"
)
type statusResponse struct {
Instances []*statusInstance `json:"instances"`
Code int `json:"code"`
}
type statusInstance struct {
ID string `json:"id"`
IP string `json:"ip"`
Peers []*statusPeer `json:"peers"`
}
type statusPeer struct {
ID string `json:"id"`
IP string `json:"ip"`
State string `json:"state"`
LastError string `json:"lastError"`
}
// CommandStatus outputs connectivity status of each peer
func CommandStatus(restPort int, hash string) {
out, err := sendRequestRaw(restPort, "status", &request{Hash: hash})
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
response := new(statusResponse)
err = json.Unmarshal(out, response)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to unmarshal status response: %s", err)
os.Exit(125)
}
if response.Code != 0 {
fmt.Fprintln(os.Stderr, "Failed to execute `status` command")
os.Exit(response.Code)
}
if len(hash) == 0 {
for _, instance := range response.Instances {
if len(hash) == 0 {
fmt.Printf("%s|%s\n", instance.ID, instance.IP)
}
for _, peer := range instance.Peers {
if len(hash) == 0 {
fmt.Printf("%s|", peer.ID)
}
fmt.Printf("%s|State:%s|", peer.IP, peer.State)
if peer.LastError != "" {
fmt.Printf("LastError:%s", peer.LastError)
}
fmt.Printf("\n")
}
}
} else {
fmt.Printf("[\n")
for _, instance := range response.Instances {
i := 0
for _, peer := range instance.Peers {
i++
fmt.Printf("\t{\n")
fmt.Printf("\t\t\"ip\": \"%s\",\n", peer.IP)
fmt.Printf("\t\t\"state\": \"%s\"", peer.State)
if peer.LastError != "" {
fmt.Printf(",\n")
fmt.Printf("\t\t\"last_error\": \"%s\"\n", peer.IP)
} else {
fmt.Printf("\n")
}
fmt.Printf("\t}")
if i != len(instance.Peers) {
fmt.Printf(",")
}
fmt.Printf("\n")
}
}
fmt.Printf("]\n")
}
os.Exit(0)
}
func (d *Daemon) execRESTStatus(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
}
response, err := d.Status(args.Hash)
if err != nil {
ptp.Log(ptp.Error, "Internal error: %s", err)
return
}
output, err := json.Marshal(response)
if err != nil {
ptp.Log(ptp.Error, "Failed to marshal status response: %s", err)
return
}
w.Write(output)
}
// Status displays information about instances, peers and their statuses
func (d *Daemon) Status(hash string) (*statusResponse, error) {
response := &statusResponse{}
if !ReadyToServe {
response.Code = 105
return response, nil
}
response.Instances = []*statusInstance{}
instances := d.Instances.get()
for _, inst := range instances {
id := inst.ID
if hash != "" {
if hash != inst.ID {
continue
}
id = ""
}
instance := &statusInstance{
ID: id,
IP: inst.PTP.Interface.GetIP().String(),
}
peers := inst.PTP.Swarm.Get()
for _, peer := range peers {
instance.Peers = append(instance.Peers, &statusPeer{
ID: peer.ID,
IP: peer.PeerLocalIP.String(),
State: ptp.StringifyState(peer.State),
LastError: peer.LastError,
})
}
response.Instances = append(response.Instances, instance)
}
return response, nil
}