Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Ciena Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package commands |
| 17 | |
| 18 | import ( |
| 19 | "context" |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 20 | "errors" |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 21 | "fmt" |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 22 | flags "github.com/jessevdk/go-flags" |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 23 | "github.com/opencord/voltctl/pkg/format" |
| 24 | "github.com/opencord/voltctl/pkg/model" |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 25 | "github.com/opencord/voltha-lib-go/v3/pkg/config" |
| 26 | "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore" |
| 27 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 28 | "io/ioutil" |
| 29 | "os" |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 30 | "strings" |
| 31 | ) |
| 32 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 33 | const ( |
| 34 | defaultComponentName = "global" |
| 35 | defaultPackageName = "default" |
| 36 | ) |
| 37 | |
| 38 | // LogLevelOutput represents the output structure for the loglevel |
| 39 | type LogLevelOutput struct { |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 40 | ComponentName string |
| 41 | Status string |
| 42 | Error string |
| 43 | } |
| 44 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 45 | // SetLogLevelOpts represents the supported CLI arguments for the loglevel set command |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 46 | type SetLogLevelOpts struct { |
| 47 | OutputOptions |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 48 | Args struct { |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 49 | Level string |
| 50 | Component []string |
| 51 | } `positional-args:"yes" required:"yes"` |
| 52 | } |
| 53 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 54 | // ListLogLevelOpts represents the supported CLI arguments for the loglevel list command |
| 55 | type ListLogLevelsOpts struct { |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 56 | ListOutputOptions |
| 57 | Args struct { |
| 58 | Component []string |
| 59 | } `positional-args:"yes" required:"yes"` |
| 60 | } |
| 61 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 62 | // ClearLogLevelOpts represents the supported CLI arguments for the loglevel clear command |
| 63 | type ClearLogLevelsOpts struct { |
| 64 | OutputOptions |
| 65 | Args struct { |
| 66 | Component []string |
| 67 | } `positional-args:"yes" required:"yes"` |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 68 | } |
| 69 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 70 | // LogLevelOpts represents the loglevel commands |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 71 | type LogLevelOpts struct { |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 72 | SetLogLevel SetLogLevelOpts `command:"set"` |
| 73 | ListLogLevels ListLogLevelsOpts `command:"list"` |
| 74 | ClearLogLevels ClearLogLevelsOpts `command:"clear"` |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | var logLevelOpts = LogLevelOpts{} |
| 78 | |
| 79 | const ( |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 80 | DEFAULT_LOGLEVELS_FORMAT = "table{{ .ComponentName }}\t{{.PackageName}}\t{{.Level}}" |
| 81 | DEFAULT_LOGLEVEL_RESULT_FORMAT = "table{{ .ComponentName }}\t{{.Status}}\t{{.Error}}" |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 82 | ) |
| 83 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 84 | // RegisterLogLevelCommands is used to register set,list and clear loglevel of components |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 85 | func RegisterLogLevelCommands(parent *flags.Parser) { |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 86 | _, err := parent.AddCommand("loglevel", "loglevel commands", "list, set, clear log levels of components", &logLevelOpts) |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 87 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 88 | Error.Fatalf("Unable to register log level commands with voltctl command parser: %s", err.Error()) |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 92 | // processComponentListArgs stores the component name and package names given in command arguments to LogLevel |
| 93 | // It checks the given argument has # key or not, if # is present then split the argument for # then stores first part as component name |
| 94 | // and second part as package name |
| 95 | func processComponentListArgs(Components []string) ([]model.LogLevel, error) { |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 96 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 97 | var logLevelConfig []model.LogLevel |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 98 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 99 | if len(Components) == 0 { |
| 100 | Components = append(Components, defaultComponentName) |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 101 | } |
| 102 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 103 | for _, component := range Components { |
| 104 | logConfig := model.LogLevel{} |
| 105 | val := strings.SplitN(component, "#", 2) |
| 106 | if len(val) > 1 { |
| 107 | if val[0] == defaultComponentName { |
| 108 | return nil, errors.New("global level doesn't support packageName") |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 109 | } |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 110 | logConfig.ComponentName = val[0] |
| 111 | logConfig.PackageName = strings.ReplaceAll(val[1], "/", "#") |
| 112 | } else { |
| 113 | logConfig.ComponentName = component |
| 114 | logConfig.PackageName = defaultPackageName |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 115 | } |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 116 | logLevelConfig = append(logLevelConfig, logConfig) |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 117 | } |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 118 | return logLevelConfig, nil |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 119 | } |
| 120 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 121 | // This method set loglevel for components. |
| 122 | // For example, using below command loglevel can be set for specific component with default packageName |
| 123 | // voltctl loglevel set level <componentName> |
| 124 | // For example, using below command loglevel can be set for specific component with specific packageName |
| 125 | // voltctl loglevel set level <componentName#packageName> |
| 126 | // For example, using below command loglevel can be set for more than one component for default package and other component for specific packageName |
| 127 | // voltctl loglevel set level <componentName1#packageName> <componentName2> |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 128 | func (options *SetLogLevelOpts) Execute(args []string) error { |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 129 | var ( |
| 130 | logLevelConfig []model.LogLevel |
| 131 | err error |
| 132 | ) |
| 133 | ProcessGlobalOptions() |
| 134 | |
| 135 | /* |
| 136 | * TODO: VOL-2738 |
| 137 | * EVIL HACK ALERT |
| 138 | * =============== |
| 139 | * It would be nice if we could squelch all but fatal log messages from |
| 140 | * the underlying libraries because as a CLI client we don't want a |
| 141 | * bunch of logs and stack traces output and instead want to deal with |
| 142 | * simple error propagation. To work around this, voltha-lib-go logging |
| 143 | * is set to fatal and we redirect etcd client logging to a temp file. |
| 144 | * |
| 145 | * Replacing os.Stderr is used here as opposed to Dup2 because we want |
| 146 | * low level panic to be displayed if they occurr. A temp file is used |
| 147 | * as opposed to /dev/null because it can't be assumed that /dev/null |
| 148 | * exists on all platforms and thus a temp file seems more portable. |
| 149 | */ |
| 150 | log.SetAllLogLevel(log.FatalLevel) |
| 151 | saveStderr := os.Stderr |
| 152 | if tmpStderr, err := ioutil.TempFile("", ""); err == nil { |
| 153 | os.Stderr = tmpStderr |
| 154 | defer func() { |
| 155 | os.Stderr = saveStderr |
| 156 | // Ignore errors on clean up because we can't do |
| 157 | // anything anyway. |
| 158 | _ = tmpStderr.Close() |
| 159 | _ = os.Remove(tmpStderr.Name()) |
| 160 | }() |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 161 | } |
| 162 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 163 | if options.Args.Level != "" { |
| 164 | if _, err := log.StringToLogLevel(options.Args.Level); err != nil { |
| 165 | return fmt.Errorf("Unknown log level %s. Allowed values are INFO, DEBUG, ERROR, WARN, FATAL", options.Args.Level) |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 169 | logLevelConfig, err = processComponentListArgs(options.Args.Component) |
| 170 | if err != nil { |
| 171 | return fmt.Errorf(err.Error()) |
| 172 | } |
| 173 | |
| 174 | client, err := kvstore.NewEtcdClient(GlobalConfig.KvStore, int(GlobalConfig.KvStoreConfig.Timeout.Seconds())) |
| 175 | if err != nil { |
| 176 | return fmt.Errorf("Unable to create kvstore client %s", err) |
| 177 | } |
| 178 | defer client.Close() |
| 179 | |
| 180 | // Already error checked during option processing |
| 181 | host, port, _ := splitEndpoint(GlobalConfig.KvStore, defaultKvHost, defaultKvPort) |
| 182 | cm := config.NewConfigManager(client, supportedKvStoreType, host, port, int(GlobalConfig.KvStoreConfig.Timeout.Seconds())) |
| 183 | |
| 184 | var output []LogLevelOutput |
| 185 | |
| 186 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.KvStoreConfig.Timeout) |
| 187 | defer cancel() |
| 188 | |
| 189 | for _, lConfig := range logLevelConfig { |
| 190 | |
| 191 | logConfig := cm.InitComponentConfig(lConfig.ComponentName, config.ConfigTypeLogLevel) |
| 192 | err := logConfig.Save(ctx, lConfig.PackageName, strings.ToUpper(options.Args.Level)) |
| 193 | if err != nil { |
| 194 | output = append(output, LogLevelOutput{ComponentName: lConfig.ComponentName, Status: "Failure", Error: err.Error()}) |
| 195 | } else { |
| 196 | output = append(output, LogLevelOutput{ComponentName: lConfig.ComponentName, Status: "Success"}) |
| 197 | } |
| 198 | |
| 199 | } |
| 200 | |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 201 | outputFormat := CharReplacer.Replace(options.Format) |
| 202 | if outputFormat == "" { |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 203 | outputFormat = GetCommandOptionWithDefault("loglevel-set", "format", DEFAULT_LOGLEVEL_RESULT_FORMAT) |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 204 | } |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 205 | result := CommandResult{ |
| 206 | Format: format.Format(outputFormat), |
| 207 | OutputAs: toOutputType(options.OutputAs), |
| 208 | NameLimit: options.NameLimit, |
| 209 | Data: output, |
| 210 | } |
| 211 | |
| 212 | GenerateOutput(&result) |
| 213 | return nil |
| 214 | } |
| 215 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 216 | // This method list loglevel for components. |
| 217 | // For example, using below command loglevel can be list for specific component |
| 218 | // voltctl loglevel list <componentName> |
| 219 | // For example, using below command loglevel can be list for all the components with all the packageName |
| 220 | // voltctl loglevel list |
| 221 | func (options *ListLogLevelsOpts) Execute(args []string) error { |
| 222 | |
| 223 | var ( |
| 224 | data []model.LogLevel |
| 225 | componentList []string |
| 226 | logLevelConfig map[string]string |
| 227 | err error |
| 228 | ) |
| 229 | ProcessGlobalOptions() |
| 230 | |
| 231 | /* |
| 232 | * TODO: VOL-2738 |
| 233 | * EVIL HACK ALERT |
| 234 | * =============== |
| 235 | * It would be nice if we could squelch all but fatal log messages from |
| 236 | * the underlying libraries because as a CLI client we don't want a |
| 237 | * bunch of logs and stack traces output and instead want to deal with |
| 238 | * simple error propagation. To work around this, voltha-lib-go logging |
| 239 | * is set to fatal and we redirect etcd client logging to a temp file. |
| 240 | * |
| 241 | * Replacing os.Stderr is used here as opposed to Dup2 because we want |
| 242 | * low level panic to be displayed if they occurr. A temp file is used |
| 243 | * as opposed to /dev/null because it can't be assumed that /dev/null |
| 244 | * exists on all platforms and thus a temp file seems more portable. |
| 245 | */ |
| 246 | log.SetAllLogLevel(log.FatalLevel) |
| 247 | saveStderr := os.Stderr |
| 248 | if tmpStderr, err := ioutil.TempFile("", ""); err == nil { |
| 249 | os.Stderr = tmpStderr |
| 250 | defer func() { |
| 251 | os.Stderr = saveStderr |
| 252 | // Ignore errors on clean up because we can't do |
| 253 | // anything anyway. |
| 254 | _ = tmpStderr.Close() |
| 255 | _ = os.Remove(tmpStderr.Name()) |
| 256 | }() |
| 257 | } |
| 258 | |
| 259 | client, err := kvstore.NewEtcdClient(GlobalConfig.KvStore, int(GlobalConfig.KvStoreConfig.Timeout.Seconds())) |
| 260 | if err != nil { |
| 261 | return fmt.Errorf("Unable to create kvstore client %s", err) |
| 262 | } |
| 263 | defer client.Close() |
| 264 | |
| 265 | // Already error checked during option processing |
| 266 | host, port, _ := splitEndpoint(GlobalConfig.KvStore, defaultKvHost, defaultKvPort) |
| 267 | cm := config.NewConfigManager(client, supportedKvStoreType, host, port, int(GlobalConfig.KvStoreConfig.Timeout.Seconds())) |
| 268 | |
| 269 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.KvStoreConfig.Timeout) |
| 270 | defer cancel() |
| 271 | |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 272 | if len(options.Args.Component) == 0 { |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 273 | componentList, err = cm.RetrieveComponentList(ctx, config.ConfigTypeLogLevel) |
| 274 | if err != nil { |
| 275 | return fmt.Errorf("Unable to retrieve list of voltha components : %s ", err) |
| 276 | } |
| 277 | } else { |
| 278 | componentList = options.Args.Component |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 279 | } |
| 280 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 281 | for _, componentName := range componentList { |
| 282 | logConfig := cm.InitComponentConfig(componentName, config.ConfigTypeLogLevel) |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 283 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 284 | logLevelConfig, err = logConfig.RetrieveAll(ctx) |
| 285 | if err != nil { |
| 286 | return fmt.Errorf("Unable to retrieve loglevel configuration for component %s : %s", componentName, err) |
| 287 | } |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 288 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 289 | for packageName, level := range logLevelConfig { |
| 290 | logLevel := model.LogLevel{} |
| 291 | if packageName == "" { |
| 292 | continue |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 293 | } |
| 294 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 295 | pName := strings.ReplaceAll(packageName, "#", "/") |
| 296 | logLevel.PopulateFrom(componentName, pName, level) |
| 297 | data = append(data, logLevel) |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | outputFormat := CharReplacer.Replace(options.Format) |
| 302 | if outputFormat == "" { |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 303 | outputFormat = GetCommandOptionWithDefault("loglevel-list", "format", DEFAULT_LOGLEVELS_FORMAT) |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 304 | } |
| 305 | orderBy := options.OrderBy |
| 306 | if orderBy == "" { |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 307 | orderBy = GetCommandOptionWithDefault("loglevel-list", "order", "") |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | result := CommandResult{ |
| 311 | Format: format.Format(outputFormat), |
| 312 | Filter: options.Filter, |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 313 | OrderBy: orderBy, |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 314 | OutputAs: toOutputType(options.OutputAs), |
| 315 | NameLimit: options.NameLimit, |
| 316 | Data: data, |
| 317 | } |
| 318 | GenerateOutput(&result) |
| 319 | return nil |
| 320 | } |
| 321 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 322 | // This method clear loglevel for components. |
| 323 | // For example, using below command loglevel can be clear for specific component with default packageName |
| 324 | // voltctl loglevel clear <componentName> |
| 325 | // For example, using below command loglevel can be clear for specific component with specific packageName |
| 326 | // voltctl loglevel clear <componentName#packageName> |
| 327 | func (options *ClearLogLevelsOpts) Execute(args []string) error { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 328 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 329 | var ( |
| 330 | logLevelConfig []model.LogLevel |
| 331 | err error |
| 332 | ) |
| 333 | ProcessGlobalOptions() |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 334 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 335 | /* |
| 336 | * TODO: VOL-2738 |
| 337 | * EVIL HACK ALERT |
| 338 | * =============== |
| 339 | * It would be nice if we could squelch all but fatal log messages from |
| 340 | * the underlying libraries because as a CLI client we don't want a |
| 341 | * bunch of logs and stack traces output and instead want to deal with |
| 342 | * simple error propagation. To work around this, voltha-lib-go logging |
| 343 | * is set to fatal and we redirect etcd client logging to a temp file. |
| 344 | * |
| 345 | * Replacing os.Stderr is used here as opposed to Dup2 because we want |
| 346 | * low level panic to be displayed if they occurr. A temp file is used |
| 347 | * as opposed to /dev/null because it can't be assumed that /dev/null |
| 348 | * exists on all platforms and thus a temp file seems more portable. |
| 349 | */ |
| 350 | log.SetAllLogLevel(log.FatalLevel) |
| 351 | saveStderr := os.Stderr |
| 352 | if tmpStderr, err := ioutil.TempFile("", ""); err == nil { |
| 353 | os.Stderr = tmpStderr |
| 354 | defer func() { |
| 355 | os.Stderr = saveStderr |
| 356 | // Ignore errors on clean up because we can't do |
| 357 | // anything anyway. |
| 358 | _ = tmpStderr.Close() |
| 359 | _ = os.Remove(tmpStderr.Name()) |
| 360 | }() |
| 361 | } |
| 362 | |
| 363 | logLevelConfig, err = processComponentListArgs(options.Args.Component) |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 364 | if err != nil { |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 365 | return fmt.Errorf("%s", err) |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 366 | } |
| 367 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 368 | client, err := kvstore.NewEtcdClient(GlobalConfig.KvStore, int(GlobalConfig.KvStoreConfig.Timeout.Seconds())) |
| 369 | if err != nil { |
| 370 | return fmt.Errorf("Unable to create kvstore client %s", err) |
| 371 | } |
| 372 | defer client.Close() |
| 373 | |
| 374 | // Already error checked during option processing |
| 375 | host, port, _ := splitEndpoint(GlobalConfig.KvStore, defaultKvHost, defaultKvPort) |
| 376 | cm := config.NewConfigManager(client, supportedKvStoreType, host, port, int(GlobalConfig.KvStoreConfig.Timeout.Seconds())) |
| 377 | |
| 378 | var output []LogLevelOutput |
| 379 | |
| 380 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.KvStoreConfig.Timeout) |
| 381 | defer cancel() |
| 382 | |
| 383 | for _, lConfig := range logLevelConfig { |
| 384 | |
| 385 | logConfig := cm.InitComponentConfig(lConfig.ComponentName, config.ConfigTypeLogLevel) |
| 386 | |
| 387 | err := logConfig.Delete(ctx, lConfig.PackageName) |
| 388 | if err != nil { |
| 389 | output = append(output, LogLevelOutput{ComponentName: lConfig.ComponentName, Status: "Failure", Error: err.Error()}) |
| 390 | } else { |
| 391 | output = append(output, LogLevelOutput{ComponentName: lConfig.ComponentName, Status: "Success"}) |
| 392 | } |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 393 | } |
| 394 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 395 | outputFormat := CharReplacer.Replace(options.Format) |
| 396 | if outputFormat == "" { |
| 397 | outputFormat = GetCommandOptionWithDefault("loglevel-clear", "format", DEFAULT_LOGLEVEL_RESULT_FORMAT) |
| 398 | } |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 399 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 400 | result := CommandResult{ |
| 401 | Format: format.Format(outputFormat), |
| 402 | OutputAs: toOutputType(options.OutputAs), |
| 403 | NameLimit: options.NameLimit, |
| 404 | Data: output, |
| 405 | } |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 406 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame^] | 407 | GenerateOutput(&result) |
| 408 | return nil |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 409 | } |