-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
183 lines (165 loc) · 4.08 KB
/
main.go
File metadata and controls
183 lines (165 loc) · 4.08 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
)
func getNodeName() (string, error) {
name, err := os.Hostname()
if err != nil {
return "", err
}
shortName := strings.Split(name, ".")[0]
return shortName, nil
}
type ansibleStatus struct {
Status string `json:"status"`
}
type provisionStatus struct {
Status string `json:"status"`
}
func getAnsibleStatus(factPath string) (string, error) {
var statusData ansibleStatus
jsonFile, err := os.Open(factPath)
if err != nil {
return "", err
}
defer jsonFile.Close()
jsonBytes, err := io.ReadAll(jsonFile)
if err != nil {
return "", err
}
json.Unmarshal(jsonBytes, &statusData)
return statusData.Status, nil
}
func getProvisionStatus(factPath string) (string, error) {
var statusData provisionStatus
jsonFile, err := os.Open(factPath)
if err != nil {
return "", err
}
defer jsonFile.Close()
jsonBytes, err := io.ReadAll(jsonFile)
if err != nil {
return "", err
}
json.Unmarshal(jsonBytes, &statusData)
return statusData.Status, nil
}
type NodeStatusResponse struct {
Name string `json:"name,omitempty"`
AnsibleStatus string `json:"ansible_status,omitempty"`
ProvisionStatus string `json:"provision_status,omitempty"`
}
type NodeStatusRequest struct {
name string
ansibleStatusPath string
provisionStatusPath string
}
type NodeStatusRequestOption func(*NodeStatusRequest)
func WithName(name string) NodeStatusRequestOption {
return func(ns *NodeStatusRequest) {
ns.name = name
}
}
func WithAnsibleFactPath(path string) NodeStatusRequestOption {
return func(ns *NodeStatusRequest) {
ns.ansibleStatusPath = path
}
}
func WithProvisionFactPath(path string) NodeStatusRequestOption {
return func(ns *NodeStatusRequest) {
ns.provisionStatusPath = path
}
}
func NewNodeStatusRequest(nodeName string, opts ...NodeStatusRequestOption) *NodeStatusRequest {
const (
defaultAnsibleFactPath = "/etc/ansible/facts.d/ansible_status.fact"
defaultProvisionFactPath = "/etc/ansible/facts.d/provision_status.fact"
)
nsr := &NodeStatusRequest{
name: nodeName,
ansibleStatusPath: defaultAnsibleFactPath,
provisionStatusPath: defaultProvisionFactPath,
}
for _, opt := range opts {
opt(nsr)
}
return nsr
}
func NewNodeStatus(nsr *NodeStatusRequest) (*NodeStatusResponse, error) {
ansibleStatus, err := getAnsibleStatus(nsr.ansibleStatusPath)
if err != nil {
return nil, err
}
provisionStatus, err := getProvisionStatus(nsr.provisionStatusPath)
if err != nil {
return nil, err
}
return &NodeStatusResponse{
Name: nsr.name,
AnsibleStatus: ansibleStatus,
ProvisionStatus: provisionStatus,
}, nil
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
nodeName, err := getNodeName()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
nsr := NewNodeStatusRequest(nodeName)
status, err := NewNodeStatus(nsr)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
jsonStatus, err := json.Marshal(status)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write(jsonStatus)
}
func f5Handler(w http.ResponseWriter, r *http.Request) {
nodeName, err := getNodeName()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
nsr := NewNodeStatusRequest(nodeName)
status, err := NewNodeStatus(nsr)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
if status.ProvisionStatus == "success" {
w.Write([]byte("up"))
return
}
w.Write([]byte("down"))
}
func main() {
host, found := os.LookupEnv("NODE_STATUS_SERVER_HOST")
if !found {
host = "0.0.0.0"
}
port, found := os.LookupEnv("NODE_STATUS_SERVER_PORT")
if !found {
port = "30622"
}
connStr := fmt.Sprintf("%s:%s", host, port)
http.HandleFunc("/", rootHandler)
http.HandleFunc("/f5", f5Handler)
fmt.Printf("Server is running at http://%s\n", connStr)
log.Fatal(http.ListenAndServe(connStr, nil))
}