diff options
| -rw-r--r-- | _doc/resolver.1.gz | bin | 2226 -> 2373 bytes | |||
| -rw-r--r-- | _doc/resolver.adoc | 16 | ||||
| -rw-r--r-- | client.go | 26 | ||||
| -rw-r--r-- | cmd/resolver/main.go | 42 | ||||
| -rw-r--r-- | cmd/resolver/resolver.go | 41 | ||||
| -rw-r--r-- | httpd.go | 12 |
6 files changed, 130 insertions, 7 deletions
diff --git a/_doc/resolver.1.gz b/_doc/resolver.1.gz Binary files differindex db15a61..c24c77c 100644 --- a/_doc/resolver.1.gz +++ b/_doc/resolver.1.gz diff --git a/_doc/resolver.adoc b/_doc/resolver.adoc index 46fbc15..179b976 100644 --- a/_doc/resolver.adoc +++ b/_doc/resolver.adoc @@ -121,6 +121,14 @@ env:: Fetch the current server environment and print it as JSON format to stdout. -- +env update <path-to-file / "-">:: ++ +-- +Update the server environment from JSON formatted file. +If the argument is "-", the new environment is read from stdin. +If the environment is valid, the server will be restarted. +-- + == EXIT STATUS @@ -172,6 +180,14 @@ Fetch and print current server environment, $ resolver env +Update the server environment from JSON file in /tmp/env.json, + + $ resolver env update /tmp/env.json + +Update the server environment by reading JSON from standard input, + + $ cat /tmp/env.json | resolver env update - + == AUTHOR @@ -144,3 +144,29 @@ func (cl *Client) Env() (env *Environment, err error) { } return env, nil } + +// EnvUpdate update the server environment using new Environment. +func (cl *Client) EnvUpdate(envIn *Environment) (envOut *Environment, err error) { + var ( + logp = "EnvUpdate" + res = libhttp.EndpointResponse{ + Data: &envOut, + } + + resb []byte + ) + + _, resb, err = cl.PostJSON(apiEnvironment, nil, envIn) + if err != nil { + return nil, fmt.Errorf("%s: %w", logp, err) + } + + err = json.Unmarshal(resb, &res) + if err != nil { + return nil, fmt.Errorf("%s: %w", logp, err) + } + if res.Code != http.StatusOK { + return nil, fmt.Errorf("%s: %d %s", logp, res.Code, res.Message) + } + return envOut, nil +} diff --git a/cmd/resolver/main.go b/cmd/resolver/main.go index 90fcc68..f2d4baf 100644 --- a/cmd/resolver/main.go +++ b/cmd/resolver/main.go @@ -21,6 +21,7 @@ const ( subCmdSearch = "search" subCmdRemove = "remove" + subCmdUpdate = "update" ) func main() { @@ -29,6 +30,7 @@ func main() { subCmd string args []string + err error optHelp bool ) @@ -85,7 +87,29 @@ func main() { } case cmdEnv: - rsol.doCmdEnv() + args = args[1:] + if len(args) == 0 { + rsol.doCmdEnv() + return + } + + subCmd = strings.ToLower(args[0]) + switch subCmd { + case subCmdUpdate: + args = args[1:] + if len(args) == 0 { + log.Fatalf("resolver: %s %s: missing file argument", rsol.cmd, subCmd) + } + + err = rsol.doCmdEnvUpdate(args[0]) + if err != nil { + log.Fatalf("resolver: %s", err) + } + + default: + log.Printf("resolver: %s: unknown sub command: %s", rsol.cmd, subCmd) + os.Exit(2) + } case cmdQuery: args = args[1:] @@ -174,6 +198,12 @@ env Fetch the current server environment and print it as JSON format to stdout. +env update <path-to-file / "-"> + + Update the server environment from JSON formatted file. + If the argument is "-", the new environment is read from stdin. + If the environment is valid, the server will be restarted. + == Examples @@ -218,5 +248,13 @@ Remove all caches in the server, Fetch and print current server environment, - $ resolver env`) + $ resolver env + +Update the server environment from JSON file in /tmp/env.json, + + $ resolver env update /tmp/env.json + +Update the server environment by reading JSON from standard input, + + $ cat /tmp/env.json | resolver env update -`) } diff --git a/cmd/resolver/resolver.go b/cmd/resolver/resolver.go index b7129cc..697953d 100644 --- a/cmd/resolver/resolver.go +++ b/cmd/resolver/resolver.go @@ -6,8 +6,10 @@ package main import ( "encoding/json" "fmt" + "io" "log" "math/rand" + "os" "strings" "time" @@ -128,6 +130,45 @@ func (rsol *resolver) doCmdEnv() { fmt.Printf("%s\n", envJson) } +// doCmdEnvUpdate update the server environment by reading the JSON formatted +// environment from file or from stdin. +func (rsol *resolver) doCmdEnvUpdate(fileOrStdin string) (err error) { + var ( + resc = rsol.newRescachedClient() + + env *rescached.Environment + envJson []byte + ) + + if fileOrStdin == "-" { + envJson, err = io.ReadAll(os.Stdin) + } else { + envJson, err = os.ReadFile(fileOrStdin) + } + if err != nil { + return fmt.Errorf("%s %s: %w", cmdEnv, subCmdUpdate, err) + } + + err = json.Unmarshal(envJson, &env) + if err != nil { + return fmt.Errorf("%s %s: %w", cmdEnv, subCmdUpdate, err) + } + + env, err = resc.EnvUpdate(env) + if err != nil { + return fmt.Errorf("%s %s: %w", cmdEnv, subCmdUpdate, err) + } + + envJson, err = json.MarshalIndent(env, "", " ") + if err != nil { + return fmt.Errorf("%s %s: %w", cmdEnv, subCmdUpdate, err) + } + + fmt.Printf("%s\n", envJson) + + return nil +} + func (rsol *resolver) doCmdQuery(args []string) { var ( maxAttempts = defAttempts @@ -98,15 +98,15 @@ func (srv *Server) httpdRegisterEndpoints() (err error) { return err } - epAPIPostEnvironment := &libhttp.Endpoint{ + apiEnvironmentUpdate := &libhttp.Endpoint{ Method: libhttp.RequestMethodPost, Path: apiEnvironment, RequestType: libhttp.RequestTypeJSON, ResponseType: libhttp.ResponseTypeJSON, - Call: srv.httpdAPIPostEnvironment, + Call: srv.httpApiEnvironmentUpdate, } - err = srv.httpd.RegisterEndpoint(epAPIPostEnvironment) + err = srv.httpd.RegisterEndpoint(apiEnvironmentUpdate) if err != nil { return err } @@ -323,9 +323,9 @@ func (srv *Server) httpdAPIGetEnvironment(epr *libhttp.EndpointRequest) (resBody return json.Marshal(&res) } -func (srv *Server) httpdAPIPostEnvironment(epr *libhttp.EndpointRequest) (resBody []byte, err error) { +func (srv *Server) httpApiEnvironmentUpdate(epr *libhttp.EndpointRequest) (resBody []byte, err error) { var ( - logp = "httpdAPIPostEnvironment" + logp = "httpApiEnvironmentUpdate" res = libhttp.EndpointResponse{} newOpts = new(Environment) ) @@ -371,6 +371,8 @@ func (srv *Server) httpdAPIPostEnvironment(epr *libhttp.EndpointRequest) (resBod res.Code = http.StatusOK res.Message = "Restarting DNS server" + res.Data = newOpts + return json.Marshal(&res) } |
