Skip to content
This repository has been archived by the owner on Feb 18, 2021. It is now read-only.

Commit

Permalink
Added service-config get/set/delete options to admin cli (#12)
Browse files Browse the repository at this point in the history
* Added service-config get/set options to admin cli

* Added input validation

* Added IsValidServiceName() to common
  • Loading branch information
venkat1109 committed Jan 20, 2017
1 parent de0b9e6 commit c052ce2
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 58 deletions.
14 changes: 13 additions & 1 deletion clients/metadata/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import (
"golang.org/x/net/context"

ccli "github.com/uber/cherami-client-go/client/cherami"
"github.com/uber/cherami-server/common"
m "github.com/uber/cherami-thrift/.generated/go/metadata"
"github.com/uber/cherami-thrift/.generated/go/shared"
"github.com/uber/cherami-server/common"
"github.com/uber/tchannel-go"
)

Expand Down Expand Up @@ -223,6 +223,18 @@ func (c *clientImpl) UpdateServiceConfig(request *m.UpdateServiceConfigRequest)
return c.client.UpdateServiceConfig(ctx, request)
}

func (c *clientImpl) ReadServiceConfig(request *m.ReadServiceConfigRequest) (*m.ReadServiceConfigResult_, error) {
ctx, cancel := c.createContext()
defer cancel()
return c.client.ReadServiceConfig(ctx, request)
}

func (c *clientImpl) DeleteServiceConfig(request *m.DeleteServiceConfigRequest) error {
ctx, cancel := c.createContext()
defer cancel()
return c.client.DeleteServiceConfig(ctx, request)
}

func getDefaultOptions() *ccli.ClientOptions {
return &ccli.ClientOptions{Timeout: time.Minute}
}
Expand Down
2 changes: 2 additions & 0 deletions clients/metadata/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ type (
ListHosts(request *m.ListHostsRequest) (*m.ListHostsResult_, error)
ListConsumerGroups(request *m.ListConsumerGroupRequest) (*m.ListConsumerGroupResult_, error)
ListAllConsumerGroups(request *m.ListConsumerGroupRequest) (*m.ListConsumerGroupResult_, error)
ReadServiceConfig(request *m.ReadServiceConfigRequest) (*m.ReadServiceConfigResult_, error)
UpdateServiceConfig(request *m.UpdateServiceConfigRequest) error
DeleteServiceConfig(request *m.DeleteServiceConfigRequest) error
ListEntityOps(request *m.ListEntityOpsRequest) (*m.ListEntityOpsResult_, error)
}
)
57 changes: 40 additions & 17 deletions cmd/tools/admin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ package main
import (
"os"

"github.com/codegangsta/cli"
"github.com/uber/cherami-server/common"
"github.com/uber/cherami-server/tools/admin"
"github.com/codegangsta/cli"
)

func main() {
Expand All @@ -43,7 +43,7 @@ func main() {
})
app.Name = "cherami"
app.Usage = "A command-line tool for cherami developer, including debugging tool"
app.Version = "1.0"
app.Version = "1.1"
app.Flags = []cli.Flag{
cli.BoolTFlag{
Name: "hyperbahn",
Expand Down Expand Up @@ -350,21 +350,6 @@ func main() {
admin.UpdateConsumerGroup(c)
},
},
{
Name: "storehost",
Aliases: []string{"s", "sh"},
Usage: "update storehost <host-name> <sku>",
Flags: []cli.Flag{
cli.StringFlag{
Name: "status, s",
Value: "enabled",
Usage: "status: enabled | disabled",
},
},
Action: func(c *cli.Context) {
admin.UpdateStoreHost(c)
},
},
},
},
{
Expand Down Expand Up @@ -605,6 +590,44 @@ func main() {
},
},
},
{
Name: "serviceconfig",
Aliases: []string{"cfg"},
Usage: "serviceconfig (get|set|delete)",
Subcommands: []cli.Command{
{
Name: "get",
Aliases: []string{"g"},
Usage: "serviceconfig get <service-name> [options]",
Flags: []cli.Flag{
cli.StringFlag{
Name: "key, k",
Value: "",
Usage: "The config key whose value is to be fetched",
},
},
Action: func(c *cli.Context) {
admin.GetServiceConfig(c)
},
},
{
Name: "set",
Aliases: []string{"s"},
Usage: "serviceconfig set <service-name.version.sku.hostname.config-key> <config-value>",
Action: func(c *cli.Context) {
admin.SetServiceConfig(c)
},
},
{
Name: "delete",
Aliases: []string{"d"},
Usage: "serviceconfig delete <service-name.version.sku.hostname.config-key>",
Action: func(c *cli.Context) {
admin.DeleteServiceConfig(c)
},
},
},
},
}

app.Run(os.Args)
Expand Down
12 changes: 12 additions & 0 deletions common/defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,15 @@ func MaxInt64(a, b int64) int64 {
}
return b
}

// IsValidServiceName returns true if the
// given input is a valid service name,
// false otherwise
func IsValidServiceName(input string) bool {
for _, svc := range services {
if svc == input {
return true
}
}
return false
}
1 change: 1 addition & 0 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

136 changes: 126 additions & 10 deletions tools/admin/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ import (
"strings"
"time"

"github.com/uber/cherami-thrift/.generated/go/cherami"
"github.com/uber/cherami-thrift/.generated/go/metadata"
"github.com/uber/cherami-thrift/.generated/go/shared"
"github.com/codegangsta/cli"
mcli "github.com/uber/cherami-server/clients/metadata"
"github.com/uber/cherami-server/common"
toolscommon "github.com/uber/cherami-server/tools/common"
"github.com/codegangsta/cli"
"github.com/uber/cherami-thrift/.generated/go/cherami"
"github.com/uber/cherami-thrift/.generated/go/metadata"
"github.com/uber/cherami-thrift/.generated/go/shared"
)

const (
Expand Down Expand Up @@ -127,12 +127,6 @@ func Consume(c *cli.Context) {
toolscommon.Consume(c, cClient)
}

// UpdateStoreHost updates the properties of the given storehost
func UpdateStoreHost(c *cli.Context) {
mClient := toolscommon.GetMClient(c, adminToolService)
toolscommon.UpdateStoreHost(c, mClient)
}

// UnloadConsumerGroup unloads the CG on the given outputhost
func UnloadConsumerGroup(c *cli.Context) {
mClient := toolscommon.GetMClient(c, adminToolService)
Expand Down Expand Up @@ -998,3 +992,125 @@ func HostAddr2uuid(c *cli.Context) {

fmt.Fprintf(os.Stdout, "%v\n", uuid)
}

const nServiceConfigTreeLevels = 5

var errInvalidServiceName = errors.New("ServiceName is invalid")
var errInvalidConfigKey = errors.New("configKey must be of the form serviceName.version.sku.hostname.key")

func splitServiceConfigKey(arg string) ([]string, error) {
tokens := strings.Split(arg, ".")
if len(tokens) != nServiceConfigTreeLevels {
return nil, errInvalidConfigKey
}
for i := 0; i < len(tokens); i++ {
if len(tokens[i]) == 0 {
return nil, errInvalidConfigKey
}
}
if !common.IsValidServiceName(tokens[0]) {
return nil, errInvalidServiceName
}
// make sure configKey is not a wildcard
if tokens[4] == "*" {
return nil, errInvalidConfigKey
}
return tokens, nil
}

// GetServiceConfig prints the config items matching
// the given input criteria.
func GetServiceConfig(c *cli.Context) {
if len(c.Args()) < 1 {
toolscommon.ExitIfError(errors.New("not enough arguments"))
}

configKey := ""
serviceName := c.Args().First()

if !common.IsValidServiceName(serviceName) {
toolscommon.ExitIfError(errInvalidServiceName)
}

isKeySet := c.IsSet("key")
if isKeySet {
configKey = c.String("key")
}

mClient := toolscommon.GetMClient(c, adminToolService)
request := &metadata.ReadServiceConfigRequest{
ServiceName: common.StringPtr(serviceName),
}

result, err := mClient.ReadServiceConfig(request)
toolscommon.ExitIfError(err)

for _, cItem := range result.GetConfigItems() {

if isKeySet && cItem.GetConfigKey() != configKey {
continue
}

keyParams := []string{
serviceName,
cItem.GetServiceVersion(),
cItem.GetSku(),
cItem.GetHostname(),
cItem.GetConfigKey(),
}

fmt.Fprintf(os.Stdout, "%v=%v\n", strings.Join(keyParams, "."), cItem.GetConfigValue())
}
}

// SetServiceConfig persists the given key-value
// config mapping onto the config store.
func SetServiceConfig(c *cli.Context) {
if len(c.Args()) < 2 {
toolscommon.ExitIfError(errors.New("not enough arguments"))
}

tokens, err := splitServiceConfigKey(c.Args()[0])
toolscommon.ExitIfError(err)

configValue := c.Args()[1]

mClient := toolscommon.GetMClient(c, adminToolService)

cItem := &metadata.ServiceConfigItem{
ServiceName: common.StringPtr(strings.ToLower(tokens[0])),
ServiceVersion: common.StringPtr(strings.ToLower(tokens[1])),
Sku: common.StringPtr(strings.ToLower(tokens[2])),
Hostname: common.StringPtr(strings.ToLower(tokens[3])),
ConfigKey: common.StringPtr(strings.ToLower(tokens[4])),
ConfigValue: common.StringPtr(strings.ToLower(configValue)),
}

req := &metadata.UpdateServiceConfigRequest{ConfigItem: cItem}
err = mClient.UpdateServiceConfig(req)
toolscommon.ExitIfError(err)
}

// DeleteServiceConfig deletes the service config items
// matching the given criteria
func DeleteServiceConfig(c *cli.Context) {
if len(c.Args()) < 1 {
toolscommon.ExitIfError(errors.New("not enough arguments"))
}

tokens, err := splitServiceConfigKey(c.Args().First())
toolscommon.ExitIfError(err)

mClient := toolscommon.GetMClient(c, adminToolService)

req := &metadata.DeleteServiceConfigRequest{
ServiceName: common.StringPtr(strings.ToLower(tokens[0])),
ServiceVersion: common.StringPtr(strings.ToLower(tokens[1])),
Sku: common.StringPtr(strings.ToLower(tokens[2])),
Hostname: common.StringPtr(strings.ToLower(tokens[3])),
ConfigKey: common.StringPtr(strings.ToLower(tokens[4])),
}

err = mClient.DeleteServiceConfig(req)
toolscommon.ExitIfError(err)
}
37 changes: 7 additions & 30 deletions tools/common/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ import (
"syscall"
"time"

"github.com/uber/cherami-thrift/.generated/go/cherami"
"github.com/apache/thrift/lib/go/thrift"
"github.com/codegangsta/cli"
ccli "github.com/uber/cherami-client-go/client/cherami"
"github.com/uber/cherami-thrift/.generated/go/admin"
"github.com/uber/cherami-thrift/.generated/go/metadata"
"github.com/uber/cherami-thrift/.generated/go/shared"
"github.com/uber/cherami-thrift/.generated/go/store"
mcli "github.com/uber/cherami-server/clients/metadata"
"github.com/uber/cherami-server/clients/outputhost"
"github.com/uber/cherami-server/clients/storehost"
"github.com/uber/cherami-server/common"
"github.com/apache/thrift/lib/go/thrift"
"github.com/codegangsta/cli"
"github.com/uber/cherami-thrift/.generated/go/admin"
"github.com/uber/cherami-thrift/.generated/go/cherami"
"github.com/uber/cherami-thrift/.generated/go/metadata"
"github.com/uber/cherami-thrift/.generated/go/shared"
"github.com/uber/cherami-thrift/.generated/go/store"
)

// GlobalOptions are options shared by most command line
Expand Down Expand Up @@ -376,29 +376,6 @@ func UpdateConsumerGroup(c *cli.Context, cClient ccli.Client) {
fmt.Printf("%v\n", Jsonify(desc))
}

// UpdateStoreHost updates the storehost based on cli.Context
func UpdateStoreHost(c *cli.Context, mClient mcli.Client) {
if len(c.Args()) < 2 {
ExitIfError(errors.New(strNotEnoughArgs))
}

host := c.Args()[0]
sku := c.Args()[1]
status := c.String("status")

cItem := metadata.ServiceConfigItem{
ServiceName: common.StringPtr(common.StoreServiceName),
Hostname: common.StringPtr(strings.ToLower(host)),
Sku: common.StringPtr(sku),
ConfigKey: common.StringPtr("adminStatus"), // TODO: Move this to common util; right now this needs some tag changes as well on controllerhost so leaving it for now
ConfigValue: common.StringPtr(strings.ToLower(status)),
}
ur := &metadata.UpdateServiceConfigRequest{ConfigItem: &cItem}
err := mClient.UpdateServiceConfig(ur)

ExitIfError(err)
}

// UnloadConsumerGroup unloads the CG based on cli.Context
func UnloadConsumerGroup(c *cli.Context, mClient mcli.Client) {
if len(c.Args()) < 1 {
Expand Down

0 comments on commit c052ce2

Please sign in to comment.