This repository was archived by the owner on Sep 17, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathhost.go
More file actions
164 lines (158 loc) · 3.26 KB
/
Copy pathhost.go
File metadata and controls
164 lines (158 loc) · 3.26 KB
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
package client
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"regexp"
"strings"
"github.com/ModuleAB/ModuleAB/agent/common"
"github.com/ModuleAB/ModuleAB/agent/logger"
"github.com/ModuleAB/ModuleAB/server/models"
)
func RegisterHost() (*models.Hosts, error) {
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
ip, err := getIP()
logger.AppLog.Debug("Got IP:", ip)
if err != nil {
return nil, err
}
body, err := GetHost(hostname)
if err != nil {
return nil, err
}
if body != nil {
return body, nil
}
body = &models.Hosts{
Name: hostname,
IpAddr: ip,
}
err = AddHost(body)
if err != nil {
return nil, err
}
return nil, nil
}
func GetHost(hostname string) (*models.Hosts, error) {
req, err := MakeRequest(
"GET",
fmt.Sprintf("/api/v1/hosts/%s", hostname),
nil,
)
if err != nil {
return nil, err
}
resp, err := StdHttp.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusNotFound:
return nil, nil
case http.StatusOK:
h := make([]*models.Hosts, 0)
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
logger.AppLog.Debug(string(b))
err = json.Unmarshal(b, &h)
if err != nil {
return nil, err
}
if len(h) == 0 {
return nil, fmt.Errorf("Failed to get hosts.")
}
return h[0], err
case http.StatusForbidden:
e := make(map[string]string)
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, &e)
if err != nil {
return nil, err
}
return nil, fmt.Errorf(e["error"])
case http.StatusInternalServerError:
e := make(map[string]string)
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, &e)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("%s %s", e["message"], e["error"])
default:
return nil, fmt.Errorf("Unknown error: %d", resp.StatusCode)
}
return nil, fmt.Errorf("Unknown error.")
}
func AddHost(h *models.Hosts) error {
b, err := json.Marshal(h)
if err != nil {
return err
}
buf := bytes.NewReader(b)
req, err := MakeRequest("POST", "/api/v1/hosts", buf)
if err != nil {
return err
}
resp, err := StdHttp.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusForbidden:
e := make(map[string]string)
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(b, &e)
if err != nil {
return err
}
return fmt.Errorf(e["error"])
case http.StatusBadRequest:
fallthrough
case http.StatusInternalServerError:
e := make(map[string]string)
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(b, &e)
if err != nil {
return err
}
return fmt.Errorf("%s %s", e["message"], e["error"])
case http.StatusCreated:
return nil
default:
return fmt.Errorf("Unknown error: %d", resp.StatusCode)
}
return fmt.Errorf("Unknown error.")
}
func getIP() (string, error) {
target := regexp.MustCompile("^(http://|https://)?(.*)/*").
ReplaceAllString(common.Server, "$2")
conn, err := net.Dial("tcp4", target)
if err != nil {
return "", err
}
defer conn.Close()
return strings.Split(conn.LocalAddr().String(), ":")[0], nil
}