From 0e014fae05f5ac2a179d981bfa0bcde597ae2d72 Mon Sep 17 00:00:00 2001 From: Madhu Rajanna Date: Fri, 1 Jun 2018 13:06:24 +0530 Subject: [PATCH] added endpoints flag to glustercli to connect to glusterd2 Signed-off-by: Madhu Rajanna --- glustercli/cmd/common.go | 14 ++++------ glustercli/cmd/georep.go | 57 ++++++++++++++++++++++------------------ glustercli/cmd/root.go | 15 +++-------- 3 files changed, 39 insertions(+), 47 deletions(-) diff --git a/glustercli/cmd/common.go b/glustercli/cmd/common.go index 03542915..bc6471ab 100644 --- a/glustercli/cmd/common.go +++ b/glustercli/cmd/common.go @@ -13,8 +13,8 @@ var ( client *restclient.Client logWriter io.WriteCloser errFailedToConnectToGlusterd = `Failed to connect to glusterd. Please check if -- Glusterd is running(%s://%s:%d) and reachable from this node. -- Make sure hostname/IP and Port specified in the command are valid +- Glusterd is running(%s) and reachable from this node. +- Make sure Endpoints specified in the command is valid ` ) @@ -34,24 +34,20 @@ func isNoRouteToHostErr(err error) bool { return strings.Contains(err.Error(), "no route to host") } -func handleGlusterdConnectFailure(msg string, err error, https bool, host string, port int, errcode int) { +func handleGlusterdConnectFailure(msg, endpoints string, err error, errcode int) { if err == nil { return } if isConnectionRefusedErr(err) || isNoSuchHostErr(err) || isNoRouteToHostErr(err) { - scheme := "http" - if https { - scheme = "https" - } os.Stderr.WriteString(msg + "\n\n") - os.Stderr.WriteString(fmt.Sprintf(errFailedToConnectToGlusterd, scheme, flagHostname, flagPort)) + os.Stderr.WriteString(fmt.Sprintf(errFailedToConnectToGlusterd, endpoints)) os.Exit(errcode) } } func failure(msg string, err error, errcode int) { - handleGlusterdConnectFailure(msg, err, flagHTTPS, flagHostname, flagPort, errcode) + handleGlusterdConnectFailure(msg, flagEndpoints[0], err, errcode) // If different error os.Stderr.WriteString(msg + "\n") diff --git a/glustercli/cmd/georep.go b/glustercli/cmd/georep.go index feed3361..326d0993 100644 --- a/glustercli/cmd/georep.go +++ b/glustercli/cmd/georep.go @@ -3,6 +3,7 @@ package cmd import ( "errors" "fmt" + "net/url" "os" "strings" @@ -44,19 +45,20 @@ Rerun the Session Create command with --force once the issues related to Remote errGeorepStatusCommandFailed = "Geo-replication Status command failed.\n" ) +const ( + geoRepHTTPScheme = "http" + geoRepGlusterdPort = 24007 +) + var ( - flagGeorepCmdForce bool - flagGeorepShowAllConfig bool - flagGeorepRemoteGlusterdHTTPS bool - flagGeorepRemoteGlusterdHost string - flagGeorepRemoteGlusterdPort int + flagGeorepCmdForce bool + flagGeorepShowAllConfig bool + flagGeorepRemoteEndpoints string ) func init() { // Geo-rep Create - georepCreateCmd.Flags().BoolVarP(&flagGeorepRemoteGlusterdHTTPS, "remote-glusterd-https", "", false, "Remote Glusterd HTTPS") - georepCreateCmd.Flags().StringVarP(&flagGeorepRemoteGlusterdHost, "remote-glusterd-host", "", "", "Remote Glusterd Host") - georepCreateCmd.Flags().IntVarP(&flagGeorepRemoteGlusterdPort, "remote-glusterd-port", "", 24007, "Remote Glusterd Port") + georepCreateCmd.Flags().StringVar(&flagGeorepRemoteEndpoints, "remote-endpoints", "", "remote glusterd2 endpoints") georepCreateCmd.Flags().BoolVarP(&flagGeorepCmdForce, "force", "f", false, "Force") georepCmd.AddCommand(georepCreateCmd) @@ -188,11 +190,13 @@ var georepCreateCmd = &cobra.Command{ failure(errGeorepSessionCreationFailed, err, 1) } - rclient := getRemoteClient(remotehost) - + remoteEndpoint, rclient, err := getRemoteClient(remotehost) + if err != nil { + failure(errGeorepSessionCreationFailed, err, 1) + } remotevoldata, err := getVolumeDetails(remotevol, rclient) if err != nil { - handleGlusterdConnectFailure(errGeorepSessionCreationFailed, err, flagGeorepRemoteGlusterdHTTPS, flagGeorepRemoteGlusterdHost, flagGeorepRemoteGlusterdPort, 1) + handleGlusterdConnectFailure(errGeorepSessionCreationFailed, remoteEndpoint, err, 1) // If not Glusterd connect Failure failure(errGeorepSessionCreationFailed, err, 1) @@ -233,7 +237,7 @@ var georepCreateCmd = &cobra.Command{ "error": err.Error(), }).Error("failed to push SSH Keys to Remote Cluster") } - handleGlusterdConnectFailure(errGeorepSessionCreationFailed, err, flagGeorepRemoteGlusterdHTTPS, flagGeorepRemoteGlusterdHost, flagGeorepRemoteGlusterdPort, 1) + handleGlusterdConnectFailure(errGeorepSessionCreationFailed, remoteEndpoint, err, 1) // If not Glusterd connect issue failure(errGeorepSSHKeysPush, err, 1) @@ -344,21 +348,19 @@ var georepDeleteCmd = &cobra.Command{ }, } -func getRemoteClient(host string) *restclient.Client { +func getRemoteClient(host string) (string, *restclient.Client, error) { // TODO: Handle Remote Cluster Authentication and certificates and URL scheme - scheme := "http" - if flagGeorepRemoteGlusterdHTTPS { - scheme = "https" - } - if flagGeorepRemoteGlusterdHost != "" { - host = flagGeorepRemoteGlusterdHost - } + clienturl := flagGeorepRemoteEndpoints - // Set Global based on final decision - flagGeorepRemoteGlusterdHost = host - - return restclient.New(fmt.Sprintf("%s://%s:%d", scheme, host, flagGeorepRemoteGlusterdPort), - "", "", "", true) + if flagGeorepRemoteEndpoints != "" { + _, err := url.Parse(flagGeorepRemoteEndpoints) + if err != nil { + return "", nil, errors.New("failed to parse geo-replication remote endpoints") + } + } else { + clienturl = fmt.Sprintf("%s://%s:%d", geoRepHTTPScheme, host, geoRepGlusterdPort) + } + return clienturl, restclient.New(clienturl, "", "", "", true), nil } func getVolIDs(pargs []string) (*volumeDetails, *volumeDetails, error) { @@ -378,7 +380,10 @@ func getVolIDs(pargs []string) (*volumeDetails, *volumeDetails, error) { if err != nil { return nil, nil, err } - rclient := getRemoteClient(remotehost) + _, rclient, err := getRemoteClient(remotehost) + if err != nil { + return nil, nil, err + } remotedata, err = getVolumeDetails(remotevol, rclient) if err != nil { return nil, nil, err diff --git a/glustercli/cmd/root.go b/glustercli/cmd/root.go index 4808faa7..ef9b63da 100644 --- a/glustercli/cmd/root.go +++ b/glustercli/cmd/root.go @@ -17,21 +17,14 @@ var RootCmd = &cobra.Command{ if err != nil { fmt.Println("Error initializing log file ", err) } - scheme := "http" - if flagHTTPS { - scheme = "https" - } - hostname := fmt.Sprintf("%s://%s:%d", scheme, flagHostname, flagPort) - initRESTClient(hostname, flagUser, flagSecret, flagCacert, flagInsecure) + initRESTClient(flagEndpoints[0], flagUser, flagSecret, flagCacert, flagInsecure) }, } var ( flagXMLOutput bool flagJSONOutput bool - flagHostname string - flagHTTPS bool - flagPort int + flagEndpoints []string flagCacert string flagInsecure bool flagLogLevel string @@ -48,9 +41,7 @@ func init() { // Global flags, applicable for all sub commands RootCmd.PersistentFlags().BoolVarP(&flagXMLOutput, "xml", "", false, "XML Output") RootCmd.PersistentFlags().BoolVarP(&flagJSONOutput, "json", "", false, "JSON Output") - RootCmd.PersistentFlags().StringVarP(&flagHostname, "glusterd-host", "", "localhost", "Glusterd Host") - RootCmd.PersistentFlags().BoolVarP(&flagHTTPS, "glusterd-https", "", false, "Use HTTPS while connecting to Glusterd") - RootCmd.PersistentFlags().IntVarP(&flagPort, "glusterd-port", "", 24007, "Glusterd Port") + RootCmd.PersistentFlags().StringSliceVar(&flagEndpoints, "endpoints", []string{"http://127.0.0.1:24007"}, "glusterd2 endpoints") RootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output") //user and secret for token authentication