1package command 2 3import ( 4 "flag" 5 "fmt" 6 "strings" 7 8 "github.com/hashicorp/serf/cmd/serf/command/agent" 9 "github.com/mitchellh/cli" 10) 11 12// TagsCommand is an interface to dynamically add or otherwise modify a 13// running serf agent's tags. 14type TagsCommand struct { 15 Ui cli.Ui 16} 17 18var _ cli.Command = &TagsCommand{} 19 20func (c *TagsCommand) Help() string { 21 helpText := ` 22Usage: serf tags [options] ... 23 24 Modifies tags on a running Serf agent. 25 26Options: 27 28 -rpc-addr=127.0.0.1:7373 RPC Address of the Serf agent. 29 -rpc-auth="" RPC auth token of the Serf agent. 30 -set key=value Creates or modifies the value of a tag 31 -delete key Removes a tag, if present 32` 33 return strings.TrimSpace(helpText) 34} 35 36func (c *TagsCommand) Run(args []string) int { 37 var tagPairs []string 38 var delTags []string 39 cmdFlags := flag.NewFlagSet("tags", flag.ContinueOnError) 40 cmdFlags.Usage = func() { c.Ui.Output(c.Help()) } 41 cmdFlags.Var((*agent.AppendSliceValue)(&tagPairs), "set", 42 "tag pairs, specified as key=value") 43 cmdFlags.Var((*agent.AppendSliceValue)(&delTags), "delete", 44 "tag keys to unset") 45 rpcAddr := RPCAddrFlag(cmdFlags) 46 rpcAuth := RPCAuthFlag(cmdFlags) 47 if err := cmdFlags.Parse(args); err != nil { 48 return 1 49 } 50 51 if len(tagPairs) == 0 && len(delTags) == 0 { 52 c.Ui.Output(c.Help()) 53 return 1 54 } 55 56 client, err := RPCClient(*rpcAddr, *rpcAuth) 57 if err != nil { 58 c.Ui.Error(fmt.Sprintf("Error connecting to Serf agent: %s", err)) 59 return 1 60 } 61 defer client.Close() 62 63 tags, err := agent.UnmarshalTags(tagPairs) 64 if err != nil { 65 c.Ui.Error(fmt.Sprintf("Error: %s", err)) 66 return 1 67 } 68 69 if err := client.UpdateTags(tags, delTags); err != nil { 70 c.Ui.Error(fmt.Sprintf("Error setting tags: %s", err)) 71 return 1 72 } 73 74 c.Ui.Output("Successfully updated agent tags") 75 return 0 76} 77 78func (c *TagsCommand) Synopsis() string { 79 return "Modify tags of a running Serf agent" 80} 81