From 2aebf70f67fbb4b831872792b382bb7dca3397a3 Mon Sep 17 00:00:00 2001 From: Jared Watts Date: Wed, 7 Dec 2016 16:50:01 -0800 Subject: [PATCH] cli: rook tool object connection information output to environment variables --- README.md | 15 +++------ cmd/rook/object/connection.go | 53 +++++++++++++++++++++++------- cmd/rook/object/connection_test.go | 25 +++++++++----- scripts/clean-rookd.sh | 3 +- 4 files changed, 66 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index c00a51c54..1fe9c827f 100644 --- a/README.md +++ b/README.md @@ -127,37 +127,32 @@ On a modern Linux host run the following: 2. Get the connection information for accessing object storage ```bash - ./rook object connection - NAME VALUE - S3_HOST rook-rgw:53390 - S3_ENDPOINT 127.0.0.1:53390 - S3_ACCESS_KEY 4W5GJBA9K4CL7OZU9FRZ - S3_SECRET_KEY SRRRrYNxcoMtxuPcKzVlNciYscvB3YjpnIJMfO5q + eval $(./rook object connection --format env-var) ``` 3. Use an S3 compatible client to create a bucket in the object store ```bash - s3cmd mb --access_key=4W5GJBA9K4CL7OZU9FRZ --secret_key=SRRRrYNxcoMtxuPcKzVlNciYscvB3YjpnIJMfO5q --no-ssl --host=127.0.0.1:53390 --host-bucket= s3://rookbucket + s3cmd mb --no-ssl --host=127.0.0.1:53390 --host-bucket= s3://rookbucket ``` 4. List all buckets in the object store ```bash - s3cmd ls --access_key=4W5GJBA9K4CL7OZU9FRZ --secret_key=SRRRrYNxcoMtxuPcKzVlNciYscvB3YjpnIJMfO5q --no-ssl --host=127.0.0.1:53390 --host-bucket= s3:// + s3cmd ls --no-ssl --host=127.0.0.1:53390 --host-bucket= ``` 5. Upload a file to the newly created bucket ```bash echo "Hello Rook!" > /tmp/rookObj - s3cmd put /tmp/rookObj --access_key=4W5GJBA9K4CL7OZU9FRZ --secret_key=SRRRrYNxcoMtxuPcKzVlNciYscvB3YjpnIJMfO5q --no-ssl --host=127.0.0.1:53390 --host-bucket= s3://rookbucket + s3cmd put /tmp/rookObj --no-ssl --host=127.0.0.1:53390 --host-bucket= s3://rookbucket ``` 6. Download and verify the file from the bucket ```bash - s3cmd get s3://rookbucket/rookObj /tmp/rookObj-download --access_key=4W5GJBA9K4CL7OZU9FRZ --secret_key=SRRRrYNxcoMtxuPcKzVlNciYscvB3YjpnIJMfO5q --no-ssl --host=127.0.0.1:53390 --host-bucket= + s3cmd get s3://rookbucket/rookObj /tmp/rookObj-download --no-ssl --host=127.0.0.1:53390 --host-bucket= cat /tmp/rookObj-download ``` diff --git a/cmd/rook/object/connection.go b/cmd/rook/object/connection.go index 2ec85848b..dc62191bf 100644 --- a/cmd/rook/object/connection.go +++ b/cmd/rook/object/connection.go @@ -27,6 +27,21 @@ import ( "github.com/spf13/cobra" ) +const ( + FormatPretty = "pretty" + FormatEnvVar = "env-var" + AWSHost = "AWS_HOST" + AWSEndpoint = "AWS_ENDPOINT" + AWSAccessKey = "AWS_ACCESS_KEY_ID" + AWSSecretKey = "AWS_SECRET_ACCESS_KEY" + PrettyOutputFmt = "%s\t%s\t\n" + ExportOutputFmt = "export %s=%s\n" +) + +var ( + connOutputFormat string +) + var connectionCmd = &cobra.Command{ Use: "connection", Short: "Gets connection information that will allow a client to access object storage in the cluster", @@ -34,6 +49,9 @@ var connectionCmd = &cobra.Command{ } func init() { + connectionCmd.Flags().StringVar(&connOutputFormat, "format", FormatPretty, + fmt.Sprintf("Format of connection output, (valid values: %s,%s)", FormatPretty, FormatEnvVar)) + connectionCmd.RunE = getConnectionInfoEntry } @@ -45,7 +63,7 @@ func getConnectionInfoEntry(cmd *cobra.Command, args []string) error { } c := client.NewRookNetworkRestClient(client.GetRestURL(rook.APIServerEndpoint), http.DefaultClient) - out, err := getConnectionInfo(c) + out, err := getConnectionInfo(connOutputFormat, c) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) @@ -55,7 +73,11 @@ func getConnectionInfoEntry(cmd *cobra.Command, args []string) error { return nil } -func getConnectionInfo(c client.RookRestClient) (string, error) { +func getConnectionInfo(format string, c client.RookRestClient) (string, error) { + if format != FormatPretty && format != FormatEnvVar { + return "", fmt.Errorf("invalid output format: %s", format) + } + connInfo, err := c.GetObjectStoreConnectionInfo() if err != nil { @@ -67,17 +89,26 @@ func getConnectionInfo(c client.RookRestClient) (string, error) { } var buffer bytes.Buffer - w := rook.NewTableWriter(&buffer) - // write header columns - fmt.Fprintln(w, "NAME\tVALUE") + if format == FormatPretty { + w := rook.NewTableWriter(&buffer) - // write object store connection info - fmt.Fprintf(w, "%s\t%s\t\n", "S3_HOST", connInfo.Host) - fmt.Fprintf(w, "%s\t%s\t\n", "S3_ENDPOINT", connInfo.IPEndpoint) - fmt.Fprintf(w, "%s\t%s\t\n", "S3_ACCESS_KEY", connInfo.AccessKey) - fmt.Fprintf(w, "%s\t%s\t\n", "S3_SECRET_KEY", connInfo.SecretKey) + // write header columns + fmt.Fprintln(w, "NAME\tVALUE") + + // write object store connection info + fmt.Fprintf(w, PrettyOutputFmt, AWSHost, connInfo.Host) + fmt.Fprintf(w, PrettyOutputFmt, AWSEndpoint, connInfo.IPEndpoint) + fmt.Fprintf(w, PrettyOutputFmt, AWSAccessKey, connInfo.AccessKey) + fmt.Fprintf(w, PrettyOutputFmt, AWSSecretKey, connInfo.SecretKey) + + w.Flush() + } else if format == FormatEnvVar { + buffer.WriteString(fmt.Sprintf(ExportOutputFmt, AWSHost, connInfo.Host)) + buffer.WriteString(fmt.Sprintf(ExportOutputFmt, AWSEndpoint, connInfo.IPEndpoint)) + buffer.WriteString(fmt.Sprintf(ExportOutputFmt, AWSAccessKey, connInfo.AccessKey)) + buffer.WriteString(fmt.Sprintf(ExportOutputFmt, AWSSecretKey, connInfo.SecretKey)) + } - w.Flush() return buffer.String(), nil } diff --git a/cmd/rook/object/connection_test.go b/cmd/rook/object/connection_test.go index d9af63812..582361847 100644 --- a/cmd/rook/object/connection_test.go +++ b/cmd/rook/object/connection_test.go @@ -39,13 +39,22 @@ func TestGetConnectionInfo(t *testing.T) { }, } - expectedOut := "NAME VALUE\n" + - "S3_HOST rook-rgw:12345 \n" + - "S3_ENDPOINT 1.2.3.4:12345 \n" + - "S3_ACCESS_KEY UST0JAP8CE61FDE0Q4BE \n" + - "S3_SECRET_KEY tVCuH20xTokjEpVJc7mKjL8PLTfGh4NZ3le3zg9X \n" + // verify pretty format output + expectedOut := "NAME VALUE\n" + + "AWS_HOST rook-rgw:12345 \n" + + "AWS_ENDPOINT 1.2.3.4:12345 \n" + + "AWS_ACCESS_KEY_ID UST0JAP8CE61FDE0Q4BE \n" + + "AWS_SECRET_ACCESS_KEY tVCuH20xTokjEpVJc7mKjL8PLTfGh4NZ3le3zg9X \n" + out, err := getConnectionInfo(FormatPretty, c) + assert.Nil(t, err) + assert.Equal(t, expectedOut, out) - out, err := getConnectionInfo(c) + // verify pretty format output + expectedOut = "export AWS_HOST=rook-rgw:12345\n" + + "export AWS_ENDPOINT=1.2.3.4:12345\n" + + "export AWS_ACCESS_KEY_ID=UST0JAP8CE61FDE0Q4BE\n" + + "export AWS_SECRET_ACCESS_KEY=tVCuH20xTokjEpVJc7mKjL8PLTfGh4NZ3le3zg9X\n" + out, err = getConnectionInfo(FormatEnvVar, c) assert.Nil(t, err) assert.Equal(t, expectedOut, out) } @@ -59,7 +68,7 @@ func TestGetConnectionInfoNotFound(t *testing.T) { }, } - out, err := getConnectionInfo(c) + out, err := getConnectionInfo(FormatPretty, c) assert.Nil(t, err) assert.Equal(t, "object store connection info is not ready, if \"object create\" has already been run, please be patient\n", out) } @@ -71,7 +80,7 @@ func TestGetConnectionInfoError(t *testing.T) { }, } - out, err := getConnectionInfo(c) + out, err := getConnectionInfo(FormatPretty, c) assert.NotNil(t, err) assert.Equal(t, "", out) } diff --git a/scripts/clean-rookd.sh b/scripts/clean-rookd.sh index 25a620695..804b59280 100755 --- a/scripts/clean-rookd.sh +++ b/scripts/clean-rookd.sh @@ -15,6 +15,7 @@ # limitations under the License. sudo rm -fr /var/lib/rook +sudo mkdir /var/lib/rook;sudo chown -R $USER:$USER /var/lib/rook sudo rm -fr /var/log/rook # clean the etcd config @@ -30,6 +31,6 @@ ps aux | grep rookd | grep -E -v 'grep|clean-rookd' | awk '{print $2}' | xargs k # clear the data partitions for DEV in sdb sdc sdd; do - sudo sgdisk --zap-all /dev/$DEV + sudo sgdisk --zap-all /dev/$DEV >/dev/null 2>&1 done