-
Notifications
You must be signed in to change notification settings - Fork 339
/
getdata.go
248 lines (225 loc) · 8.15 KB
/
getdata.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
package t3cutil
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
import (
"encoding/json"
"errors"
"io"
"net/url"
"os"
"time"
"github.com/apache/trafficcontrol/cache-config/t3cutil/toreq"
"github.com/apache/trafficcontrol/cache-config/t3cutil/toreq/torequtil"
"github.com/apache/trafficcontrol/lib/go-atscfg"
"github.com/apache/trafficcontrol/lib/go-log"
"github.com/apache/trafficcontrol/lib/go-tc"
)
type TCCfg struct {
CacheHostName string
GetData string
TOClient *toreq.TOClient
TOInsecure bool
TOTimeoutMS time.Duration
TOPass string
TOUser string
TOURL *url.URL
UserAgent string
// TODisableProxy is whether to not use a configured Traffic Ops Proxy.
// This is only used by WriteConfig, which is the only command that makes enough requests to matter.
TODisableProxy bool
// RevalOnly is whether to only fetch config data necessary to revalidate, versus all data necessary to generate config. This is only used by WriteConfig
RevalOnly bool
// OldCfg is the previously fetched ConfigData, for 'config' requests. May be nil.
OldCfg *ConfigData
}
func GetDataFuncs() map[string]func(TCCfg, io.Writer) error {
return map[string]func(TCCfg, io.Writer) error{
`update-status`: WriteServerUpdateStatus,
`packages`: WritePackages,
`chkconfig`: WriteChkconfig,
`system-info`: WriteSystemInfo,
`statuses`: WriteStatuses,
`config`: WriteConfig,
}
}
func GetServerUpdateStatus(cfg TCCfg) (*tc.ServerUpdateStatus, error) {
status, _, err := cfg.TOClient.GetServerUpdateStatus(tc.CacheName(cfg.CacheHostName), nil)
if err != nil {
return nil, errors.New("getting server '" + cfg.CacheHostName + "' update status: " + err.Error())
}
return &status, nil
}
func WriteData(cfg TCCfg) error {
log.Infoln("Getting data '" + cfg.GetData + "'")
dataF, ok := GetDataFuncs()[cfg.GetData]
if !ok {
return errors.New("unknown data request '" + cfg.GetData + "'")
}
return dataF(cfg, os.Stdout)
}
const SystemInfoParamConfigFile = `global`
// WriteSystemInfo writes the "system info" to output.
//
// This is the same info as the Traffic Ops API's /system/info endpoint, which
// is actually just all Parameters with the config_file 'global'.
// Note this is different than the more common "global parameters", which
// usually refers to all Parameters on the Profile named 'GLOBAL'.
//
// This is identical to the /system/info endpoint, except it does not include a
// '{response: {parameters:' wrapper.
func WriteSystemInfo(cfg TCCfg, output io.Writer) error {
paramArr, _, err := cfg.TOClient.GetConfigFileParameters(SystemInfoParamConfigFile, nil)
if err != nil {
return errors.New("getting system info parameters: " + err.Error())
}
params := map[string]string{}
for _, param := range paramArr {
params[param.Name] = param.Value
}
if err := json.NewEncoder(output).Encode(params); err != nil {
return errors.New("encoding system info parameters: " + err.Error())
}
return nil
}
// WriteStatuses writes the Traffic Ops statuses to output.
// Note this is identical to /statuses except it omits the '{response:'
// wrapper.
func WriteStatuses(cfg TCCfg, output io.Writer) error {
statuses, _, err := cfg.TOClient.GetStatuses(nil)
if err != nil {
return errors.New("getting statuses: " + err.Error())
}
if err := json.NewEncoder(output).Encode(statuses); err != nil {
return errors.New("encoding statuses: " + err.Error())
}
return nil
}
// WriteServerUpdateStatus writes the Traffic Ops server update status to
// output.
// Note this is identical to the Traffic Ops API's
// /servers/{{host name}}/update_status endpoint except it omits the '[]'
// wrapper.
func WriteServerUpdateStatus(cfg TCCfg, output io.Writer) error {
status, err := GetServerUpdateStatus(cfg)
if err != nil {
return err
}
if err := json.NewEncoder(output).Encode(status); err != nil {
return errors.New("encoding server update status: " + err.Error())
}
return nil
}
// WriteORTServerPackages writes the packages for serverName to output.
// Note this is identical to /ort/serverName/packages.
func WritePackages(cfg TCCfg, output io.Writer) error {
packages, err := GetPackages(cfg)
if err != nil {
return errors.New("getting ORT server packages: " + err.Error())
}
if err := json.NewEncoder(output).Encode(packages); err != nil {
return errors.New("writing packages: " + err.Error())
}
return nil
}
func GetPackages(cfg TCCfg) ([]Package, error) {
server, _, err := cfg.TOClient.GetServerByHostName(string(cfg.CacheHostName), nil)
if err != nil {
return nil, errors.New("getting server: " + err.Error())
} else if server.Profile == nil {
return nil, errors.New("getting server: nil profile")
}
params, _, err := cfg.TOClient.GetServerProfileParameters(*server.Profile, nil)
if err != nil {
return nil, errors.New("getting server profile '" + *server.Profile + "' parameters: " + err.Error())
}
packages := []Package{}
for _, param := range params {
if param.ConfigFile != atscfg.PackagesParamConfigFile {
continue
}
packages = append(packages, Package{Name: param.Name, Version: param.Value})
}
return packages, nil
}
type Package struct {
Name string `json:"name"`
Version string `json:"version"`
}
// WriteChkconfig writes the chkconfig for cfg.CacheHostName to output.
// Note this is identical to /ort/serverName/chkconfig.
func WriteChkconfig(cfg TCCfg, output io.Writer) error {
chkconfig, err := GetChkconfig(cfg)
if err != nil {
return errors.New("getting chkconfig: " + err.Error())
}
if err := json.NewEncoder(output).Encode(chkconfig); err != nil {
return errors.New("writing chkconfig: " + err.Error())
}
return nil
}
func GetChkconfig(cfg TCCfg) ([]ChkConfigEntry, error) {
server, _, err := cfg.TOClient.GetServerByHostName(string(cfg.CacheHostName), nil)
if err != nil {
return nil, errors.New("getting server: " + err.Error())
} else if server.Profile == nil {
return nil, errors.New("getting server: nil profile")
}
params, _, err := cfg.TOClient.GetServerProfileParameters(*server.Profile, nil)
if err != nil {
return nil, errors.New("getting server profile '" + *server.Profile + "' parameters: " + err.Error())
}
chkconfig := []ChkConfigEntry{}
for _, param := range params {
if param.ConfigFile != atscfg.ChkconfigParamConfigFile {
continue
}
chkconfig = append(chkconfig, ChkConfigEntry{Name: param.Name, Val: param.Value})
}
return chkconfig, nil
}
type ChkConfigEntry struct {
Name string `json:"name"`
Val string `json:"value"`
}
// SetUpdateStatus sets the queue and reval status of serverName in Traffic Ops.
func SetUpdateStatus(cfg TCCfg, serverName tc.CacheName, queue bool, revalPending bool) error {
// TODO need to move to toreq, add fallback
reqInf, err := cfg.TOClient.SetServerUpdateStatus(serverName, &queue, &revalPending)
if err != nil {
return errors.New("setting update statuses (Traffic Ops '" + torequtil.MaybeIPStr(reqInf.RemoteAddr) + "'): " + err.Error())
}
return nil
}
func jsonBoolStr(b bool) string {
if b {
return `true`
}
return `false`
}
// WriteConfig writes the Traffic Ops data necessary to generate config to output.
func WriteConfig(cfg TCCfg, output io.Writer) error {
cfgData, err := GetConfigData(cfg.TOClient, cfg.TODisableProxy, cfg.CacheHostName, cfg.RevalOnly, cfg.OldCfg)
if err != nil {
return errors.New("getting config data: " + err.Error())
}
if err := json.NewEncoder(output).Encode(cfgData); err != nil {
return errors.New("encoding config data: " + err.Error())
}
return nil
}