-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
142 lines (130 loc) · 3.59 KB
/
database.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
// Machine temporary database
//
// Copyright (C) 2021 Tateru Authors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"net/url"
"sync"
"text/template"
"time"
)
type Machine struct {
Name string `json:"name,omitempty"`
UUID string `json:"uuid"`
SerialNumber string `json:"serialNumber,omitempty"`
AssetTag string `json:"assetTag,omitempty"`
Type string `json:"type"`
ManagerName string `json:"-"`
ManagedBy string `json:"managedBy"`
}
type tateruDb struct {
machinesMutex sync.RWMutex
machines []Machine
indexTmpl *template.Template
}
func (db *tateruDb) HandleIndex(w http.ResponseWriter, r *http.Request) {
w.Header().Add("content-type", "text/html; charset=utf-8")
var d struct {
Machines []Machine
}
db.machinesMutex.RLock()
d.Machines = db.machines
b := &bytes.Buffer{}
if err := db.indexTmpl.Execute(b, d); err != nil {
http.Error(w, "Failed to render template", http.StatusInternalServerError)
log.Printf("Template rendering error: %v", err)
return
}
db.machinesMutex.RUnlock()
w.Write(b.Bytes())
return
}
func (db *tateruDb) Poll() {
log.Printf("Polling of managers started")
for {
machs := []Machine{}
for maddr, mcfg := range cfg.Managers {
u, err := url.Parse(maddr)
if err != nil {
panic(err)
}
u.Path += "/v1/machines"
resp, err := http.Get(u.String())
if err != nil {
log.Printf("Poll of %q failed: %v", mcfg.Name, err)
continue
}
if resp.StatusCode != 200 {
log.Printf("Poll of %q failed: status code %d", mcfg.Name, resp.StatusCode)
continue
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Poll read of %q failed: %v", mcfg.Name, err)
continue
}
var mr []struct {
UUID string
SerialNumber string
AssetTag string
Name string
}
if err := json.Unmarshal(body, &mr); err != nil {
log.Printf("Poll of %q failed to parse: %v", mcfg.Name, err)
continue
}
for _, m := range mr {
machs = append(machs, Machine{
Name: m.Name,
SerialNumber: m.SerialNumber,
AssetTag: m.AssetTag,
UUID: m.UUID,
ManagerName: mcfg.Name,
Type: mcfg.Type,
ManagedBy: maddr,
})
}
}
db.machinesMutex.Lock()
db.machines = machs
db.machinesMutex.Unlock()
time.Sleep(time.Second * 30)
}
}
func (db *tateruDb) HandleMachinesAPI(w http.ResponseWriter, r *http.Request) {
w.Header().Add("content-type", "application/json; charset=utf-8")
if r.Method != "GET" {
http.Error(w, "Unsupported method", http.StatusMethodNotAllowed)
return
}
// TODO: Add filter of alias query param
db.machinesMutex.RLock()
b, err := json.MarshalIndent(db.machines, "", " ")
if err != nil {
http.Error(w, "Failed to render JSON", http.StatusInternalServerError)
log.Printf("Failed to marshal machines JSON: %v", err)
return
}
db.machinesMutex.RUnlock()
w.Write(b)
return
}