-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add command to interogate the server version and other det…
…ails Signed-off-by: Laurentiu Niculae <[email protected]>
- Loading branch information
1 parent
8e7b2d2
commit f52aef9
Showing
10 changed files
with
336 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
//go:build search | ||
// +build search | ||
|
||
package client | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v2" | ||
|
||
zerr "zotregistry.io/zot/errors" | ||
"zotregistry.io/zot/pkg/api/constants" | ||
) | ||
|
||
func NewServerInfoCommand() *cobra.Command { | ||
serverInfoCmd := &cobra.Command{ | ||
Use: "status", | ||
Short: "Information about the server configuration and build information", | ||
Long: `Information about the server configuration and build information`, | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
searchConfig, err := GetSearchConfigFromFlags(cmd, NewSearchService()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return GetServerInfo(searchConfig) | ||
}, | ||
} | ||
|
||
serverInfoCmd.PersistentFlags().String(URLFlag, "", | ||
"Specify zot server URL if config-name is not mentioned") | ||
serverInfoCmd.PersistentFlags().StringP(ConfigFlag, "c", "", | ||
"Specify the registry configuration to use for connection") | ||
serverInfoCmd.PersistentFlags().StringP(UserFlag, "u", "", | ||
`User Credentials of zot server in "username:password" format`) | ||
serverInfoCmd.Flags().StringP(OutputFormatFlag, "f", "text", "Specify the output format [text|json|yaml]") | ||
|
||
return serverInfoCmd | ||
} | ||
|
||
const ( | ||
StatusOnline = "online" | ||
StatusOffline = "offline" | ||
StatusUnknown = "unknown" | ||
) | ||
|
||
func GetServerInfo(config SearchConfig) error { | ||
ctx := context.Background() | ||
username, password := getUsernameAndPassword(config.User) | ||
|
||
checkAPISupportEndpoint, err := combineServerAndEndpointURL(config.ServURL, constants.RoutePrefix) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = makeGETRequest(ctx, checkAPISupportEndpoint, username, password, config.VerifyTLS, config.Debug, | ||
nil, config.ResultWriter) | ||
if err != nil { | ||
serverInfo := ServerInfo{} | ||
|
||
switch { | ||
case errors.Is(err, zerr.ErrUnauthorizedAccess): | ||
serverInfo.Status = StatusUnknown | ||
serverInfo.ErrorMsg = "unauthorised access. given credentials are incorrect" | ||
case errors.Is(err, zerr.ErrBadHTTPStatusCode), errors.Is(err, zerr.ErrPageNotFound): | ||
serverInfo.Status = StatusOffline | ||
serverInfo.ErrorMsg = fmt.Sprintf("%s: request at %s failed", zerr.ErrAPINotSupported.Error(), | ||
checkAPISupportEndpoint) | ||
default: | ||
serverInfo.Status = StatusOffline | ||
serverInfo.ErrorMsg = err.Error() | ||
} | ||
|
||
return PrintServerInfo(serverInfo, config) | ||
} | ||
|
||
mgmtEndpoint, err := combineServerAndEndpointURL(config.ServURL, fmt.Sprintf("%s%s", | ||
constants.RoutePrefix, constants.ExtMgmt)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
serverInfo := ServerInfo{} | ||
|
||
_, err = makeGETRequest(ctx, mgmtEndpoint, username, password, config.VerifyTLS, config.Debug, | ||
&serverInfo, config.ResultWriter) | ||
|
||
switch { | ||
case err == nil: | ||
serverInfo.Status = StatusOnline | ||
case errors.Is(err, zerr.ErrPageNotFound): | ||
serverInfo.Status = StatusOnline | ||
serverInfo.ErrorMsg = "MGMT endpoint is not enabled" | ||
case errors.Is(err, zerr.ErrUnauthorizedAccess): | ||
serverInfo.Status = StatusOnline | ||
serverInfo.ErrorMsg = "credentials provided have been rejected" | ||
case errors.Is(err, zerr.ErrBadHTTPStatusCode): | ||
serverInfo.Status = StatusOnline | ||
serverInfo.ErrorMsg = err.Error() | ||
default: | ||
serverInfo.Status = StatusOffline | ||
serverInfo.ErrorMsg = err.Error() | ||
} | ||
|
||
return PrintServerInfo(serverInfo, config) | ||
} | ||
|
||
func PrintServerInfo(serverInfo ServerInfo, config SearchConfig) error { | ||
outputResult, err := serverInfo.ToStringFormat(config.OutputFormat) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Fprintln(config.ResultWriter, outputResult) | ||
|
||
return nil | ||
} | ||
|
||
type ServerInfo struct { | ||
Status string `json:"status,omitempty" mapstructure:"status"` | ||
ErrorMsg string `json:"error,omitempty" mapstructure:"error"` | ||
DistSpecVersion string `json:"distSpecVersion,omitempty" mapstructure:"distSpecVersion"` | ||
Commit string `json:"commit,omitempty" mapstructure:"commit"` | ||
BinaryType string `json:"binaryType,omitempty" mapstructure:"binaryType"` | ||
ReleaseTag string `json:"releaseTag,omitempty" mapstructure:"releaseTag"` | ||
} | ||
|
||
func (si *ServerInfo) ToStringFormat(format string) (string, error) { | ||
switch format { | ||
case "text", "": | ||
return si.ToText() | ||
case "json": | ||
return si.ToJSON() | ||
case "yaml", "yml": | ||
return si.ToYAML() | ||
default: | ||
return "", zerr.ErrFormatNotSupported | ||
} | ||
} | ||
|
||
func (si *ServerInfo) ToText() (string, error) { | ||
flagsList := strings.Split(strings.Trim(si.BinaryType, "-"), "-") | ||
flags := strings.Join(flagsList, ", ") | ||
|
||
var output string | ||
|
||
if si.ErrorMsg != "" { | ||
serverStatus := fmt.Sprintf("Server Status: %s\n"+ | ||
"Error: %s", si.Status, si.ErrorMsg) | ||
|
||
output = serverStatus | ||
} else { | ||
serverStatus := fmt.Sprintf("Server Status: %s", si.Status) | ||
serverInfo := fmt.Sprintf("Server Version: %s\n"+ | ||
"Dist Spec Version: %s\n"+ | ||
"Built with: %s", | ||
si.ReleaseTag, si.DistSpecVersion, flags, | ||
) | ||
|
||
output = serverStatus + "\n" + serverInfo + "\n" | ||
} | ||
|
||
return output, nil | ||
} | ||
|
||
func (si *ServerInfo) ToJSON() (string, error) { | ||
blob, err := json.MarshalIndent(*si, "", " ") | ||
|
||
return string(blob), err | ||
} | ||
|
||
func (si *ServerInfo) ToYAML() (string, error) { | ||
body, err := yaml.Marshal(*si) | ||
|
||
return string(body), err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
//go:build search | ||
// +build search | ||
|
||
package client //nolint:testpackage | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"regexp" | ||
"strings" | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
|
||
"zotregistry.io/zot/pkg/api" | ||
"zotregistry.io/zot/pkg/api/config" | ||
extconf "zotregistry.io/zot/pkg/extensions/config" | ||
test "zotregistry.io/zot/pkg/test/common" | ||
) | ||
|
||
func TestServerInfoCommand(t *testing.T) { | ||
Convey("ServerInfoCommand", t, func() { | ||
port := test.GetFreePort() | ||
baseURL := test.GetBaseURL(port) | ||
conf := config.New() | ||
conf.HTTP.Port = port | ||
conf.Storage.GC = false | ||
defaultVal := true | ||
conf.Extensions = &extconf.ExtensionConfig{ | ||
Search: &extconf.SearchConfig{BaseConfig: extconf.BaseConfig{Enable: &defaultVal}}, | ||
} | ||
|
||
ctlr := api.NewController(conf) | ||
ctlr.Config.Storage.RootDirectory = t.TempDir() | ||
cm := test.NewControllerManager(ctlr) | ||
|
||
cm.StartAndWait(conf.HTTP.Port) | ||
defer cm.StopServer() | ||
|
||
configPath := makeConfigFile(fmt.Sprintf(`{"configs":[{"_name":"status-test","url":"%s","showspinner":false}]}`, | ||
baseURL)) | ||
defer os.Remove(configPath) | ||
|
||
args := []string{"status", "--config", "status-test"} | ||
cmd := NewCliRootCmd() | ||
buff := bytes.NewBufferString("") | ||
cmd.SetOut(buff) | ||
cmd.SetErr(buff) | ||
cmd.SetArgs(args) | ||
err := cmd.Execute() | ||
So(err, ShouldBeNil) | ||
space := regexp.MustCompile(`\s+`) | ||
str := space.ReplaceAllString(buff.String(), " ") | ||
actual := strings.TrimSpace(str) | ||
So(actual, ShouldContainSubstring, config.ReleaseTag) | ||
So(actual, ShouldContainSubstring, config.BinaryType) | ||
|
||
// JSON | ||
args = []string{"status", "--config", "status-test", "--format", "json"} | ||
cmd = NewCliRootCmd() | ||
buff = bytes.NewBufferString("") | ||
cmd.SetOut(buff) | ||
cmd.SetErr(buff) | ||
cmd.SetArgs(args) | ||
err = cmd.Execute() | ||
So(err, ShouldBeNil) | ||
space = regexp.MustCompile(`\s+`) | ||
str = space.ReplaceAllString(buff.String(), " ") | ||
actual = strings.TrimSpace(str) | ||
So(actual, ShouldContainSubstring, config.ReleaseTag) | ||
So(actual, ShouldContainSubstring, config.BinaryType) | ||
|
||
// YAML | ||
args = []string{"status", "--config", "status-test", "--format", "yaml"} | ||
cmd = NewCliRootCmd() | ||
buff = bytes.NewBufferString("") | ||
cmd.SetOut(buff) | ||
cmd.SetErr(buff) | ||
cmd.SetArgs(args) | ||
err = cmd.Execute() | ||
So(err, ShouldBeNil) | ||
space = regexp.MustCompile(`\s+`) | ||
str = space.ReplaceAllString(buff.String(), " ") | ||
actual = strings.TrimSpace(str) | ||
So(actual, ShouldContainSubstring, config.ReleaseTag) | ||
So(actual, ShouldContainSubstring, config.BinaryType) | ||
|
||
// bad type | ||
args = []string{"status", "--config", "status-test", "--format", "badType"} | ||
cmd = NewCliRootCmd() | ||
buff = bytes.NewBufferString("") | ||
cmd.SetOut(buff) | ||
cmd.SetErr(buff) | ||
cmd.SetArgs(args) | ||
err = cmd.Execute() | ||
So(err, ShouldNotBeNil) | ||
}) | ||
} | ||
|
||
func TestServerInfoCommandErrors(t *testing.T) { | ||
Convey("ServerInfoCommand", t, func() { | ||
args := []string{"status"} | ||
cmd := NewCliRootCmd() | ||
buff := bytes.NewBufferString("") | ||
cmd.SetOut(buff) | ||
cmd.SetErr(buff) | ||
cmd.SetArgs(args) | ||
err := cmd.Execute() | ||
So(err, ShouldNotBeNil) | ||
|
||
// invalid URL | ||
err = GetServerInfo(SearchConfig{ | ||
ServURL: "a: ds", | ||
}) | ||
So(err, ShouldNotBeNil) | ||
|
||
// fail Get request | ||
err = GetServerInfo(SearchConfig{ | ||
ServURL: "http://127.0.0.1:8000", | ||
}) | ||
So(err, ShouldNotBeNil) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.