-
Notifications
You must be signed in to change notification settings - Fork 45
/
status.go
107 lines (96 loc) · 2.45 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
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) {
out, err := sendRequestRaw(restPort, "status", &request{})
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
response := new(statusResponse)
err = json.Unmarshal(out, response)
if err != nil {
fmt.Printf("Failed to unmarshal status response: %s", err)
os.Exit(125)
}
if response.Code != 0 {
os.Exit(response.Code)
}
for _, instance := range response.Instances {
fmt.Printf("%s|%s\n", instance.ID, instance.IP)
for _, peer := range instance.Peers {
fmt.Printf("%s|%s|State:%s|", peer.ID, peer.IP, peer.State)
if peer.LastError != "" {
fmt.Printf("LastError:%s", peer.LastError)
}
fmt.Printf("\n")
}
}
os.Exit(0)
}
func (d *Daemon) execRESTStatus(w http.ResponseWriter, r *http.Request) {
args := new(DaemonArgs)
err := getJSON(r.Body, args)
if handleMarshalError(err, w) != nil {
return
}
response, err := d.Status()
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() (*statusResponse, error) {
response := &statusResponse{}
if !ReadyToServe {
response.Code = 105
return response, nil
}
response.Instances = []*statusInstance{}
instances := d.Instances.Get()
for _, inst := range instances {
instance := &statusInstance{
ID: inst.ID,
IP: inst.PTP.Interface.GetIP().String(),
}
peers := inst.PTP.Peers.Get()
for _, peer := range peers {
instance.Peers = append(instance.Peers, &statusPeer{
ID: peer.ID,
IP: peer.PeerLocalIP.String(),
State: StringifyState(peer.State),
LastError: peer.LastError,
})
}
response.Instances = append(response.Instances, instance)
}
return response, nil
}