/
host.go
287 lines (248 loc) · 6.71 KB
/
host.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright 2014 The Serviced Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package api
import (
"time"
"github.com/control-center/serviced/auth"
"github.com/control-center/serviced/dao"
"github.com/control-center/serviced/domain/host"
"github.com/control-center/serviced/metrics"
"github.com/control-center/serviced/rpc/agent"
"github.com/control-center/serviced/rpc/master"
"github.com/control-center/serviced/utils"
)
// HostConfig is the deserialized object from the command-line
type HostConfig struct {
Address *utils.URL
Nat *utils.URL
PoolID string
Memory string
IPs []string
}
type HostUpdateConfig struct {
HostID string
Memory string
}
type AuthHost struct {
host.Host
Authenticated bool
}
func getAuthInfo(client master.ClientInterface, hosts []host.Host) ([]AuthHost, error) {
hostIDs := []string{}
for _, h := range hosts {
hostIDs = append(hostIDs, h.ID)
}
authHosts, err := client.HostsAuthenticated(hostIDs)
if err != nil {
return nil, err
}
hostsWithAuth := []AuthHost{}
for _, h := range hosts {
hostsWithAuth = append(hostsWithAuth, AuthHost{h, authHosts[h.ID]})
}
return hostsWithAuth, nil
}
// Get host information by its id
func (a *api) GetHostWithAuthInfo(id string) (*AuthHost, error) {
client, err := a.connectMaster()
if err != nil {
return nil, err
}
h, err := client.GetHost(id)
if err != nil {
return nil, err
}
hostsWithAuth, err := getAuthInfo(client, []host.Host{*h})
if err != nil {
return nil, err
}
return &hostsWithAuth[0], nil
}
func (a *api) GetHostsWithAuthInfo() ([]AuthHost, error) {
client, err := a.connectMaster()
if err != nil {
return nil, err
}
hosts, err := client.GetHosts()
if err != nil {
return nil, err
}
return getAuthInfo(client, hosts)
}
// Returns a list of all hosts
func (a *api) GetHosts() ([]host.Host, error) {
client, err := a.connectMaster()
if err != nil {
return nil, err
}
return client.GetHosts()
}
// Get host information by its id
func (a *api) GetHost(id string) (*host.Host, error) {
client, err := a.connectMaster()
if err != nil {
return nil, err
}
return client.GetHost(id)
}
func (a *api) GetHostMemory(id string) (*metrics.MemoryUsageStats, error) {
client, err := a.connectDAO()
if err != nil {
return nil, err
}
req := dao.MetricRequest{
StartTime: time.Now().Add(-24 * time.Hour),
HostID: id,
}
var result metrics.MemoryUsageStats
if err := client.GetHostMemoryStats(req, &result); err != nil {
return nil, err
}
return &result, nil
}
// Adds a new host
func (a *api) AddHost(config HostConfig) (*host.Host, []byte, error) {
// if a nat is configured then we connect rpc to the nat, otherwise
// connect to the host address.
var rpcAddress string
if len(config.Nat.Host) > 0 {
rpcAddress = config.Nat.String()
} else {
rpcAddress = config.Address.String()
}
agentClient, err := a.connectAgent(rpcAddress)
if err != nil {
return nil, nil, err
}
req := agent.BuildHostRequest{
IP: config.Address.Host,
Port: config.Address.Port,
PoolID: config.PoolID,
Memory: config.Memory,
}
h, err := agentClient.BuildHost(req)
if err != nil {
return nil, nil, err
}
if len(config.Nat.Host) > 0 {
h.NatIP = config.Nat.Host
}
masterClient, err := a.connectMaster()
if err != nil {
return nil, nil, err
}
var privateKey []byte
if privateKey, err = masterClient.AddHost(*h); err != nil {
return nil, nil, err
}
if host_, err := a.GetHost(h.ID); err != nil {
return nil, nil, err
} else {
return host_, privateKey, nil
}
}
// Adds a new host and uses a common key to register it. Returns the host and the master's public key.
func (a *api) AddHostPrivate(config HostConfig) (*host.Host, []byte, error) {
// if a nat is configured then we connect rpc to the nat, otherwise
// connect to the host address.
var rpcAddress string
if len(config.Nat.Host) > 0 {
rpcAddress = config.Nat.String()
} else {
rpcAddress = config.Address.String()
}
agentClient, err := a.connectAgent(rpcAddress)
if err != nil {
log.Errorf("Couldn't connect to the agent: %+v\n", err)
return nil, nil, err
}
req := agent.BuildHostRequest{
IP: config.Address.Host,
Port: config.Address.Port,
PoolID: config.PoolID,
Memory: config.Memory,
}
h, err := agentClient.BuildHost(req)
if err != nil {
return nil, nil, err
}
masterClient, err := a.connectMaster()
if err != nil {
log.Errorf("Couldn't connect to master: %+v\n", err)
return nil, nil, err
}
var masterPublicKey []byte
if masterPublicKey, err = masterClient.AddHostPrivate(*h); err != nil {
log.Errorf("AddHostPrivate failed: %+v\n", err)
return nil, nil, err
}
return h, masterPublicKey, nil
}
// Removes an existing host by its id
func (a *api) RemoveHost(id string) error {
client, err := a.connectMaster()
if err != nil {
return err
}
return client.RemoveHost(id)
}
// Sets the memory allocation for an existing host
func (a *api) SetHostMemory(config HostUpdateConfig) error {
client, err := a.connectMaster()
if err != nil {
return err
}
h, err := client.GetHost(config.HostID)
if err != nil {
return err
}
if _, err := host.GetRAMLimit(config.Memory, h.Memory); err != nil {
return err
}
h.RAMLimit = config.Memory
return client.UpdateHost(*h)
}
func (a *api) AuthenticateHost(hostID string) (string, int64, error) {
client, err := a.connectMaster()
if err != nil {
return "", 0, err
}
return client.AuthenticateHost(hostID)
}
// Retrieve host's public key
func (a *api) GetHostPublicKey(id string) ([]byte, error) {
client, err := a.connectMaster()
if err != nil {
return nil, err
}
return client.GetHostPublicKey(id)
}
// Reset a host's key
func (a *api) ResetHostKey(id string) ([]byte, error) {
client, err := a.connectMaster()
if err != nil {
return nil, err
}
return client.ResetHostKey(id)
}
// Write delegate keys to disk
func (a *api) RegisterHost(keydata []byte) error {
return auth.RegisterLocalHost(keydata)
}
func (a *api) RegisterRemoteHost(h *host.Host, nat utils.URL, keyData []byte, prompt bool) error {
return auth.RegisterRemoteHost(h.ID, nat, h.IPAddr, keyData, prompt)
}
// Output a delegate key file to a given location on disk
func (a *api) WriteDelegateKey(filename string, data []byte) error {
return auth.WriteKeyToFile(filename, data)
}