github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/operator_gossip_keyring_use.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/mitchellh/cli"
     8  	"github.com/posener/complete"
     9  )
    10  
    11  // OperatorGossipKeyringUseCommand is a Command implementation that
    12  // handles setting the gossip encryption key from a keyring
    13  type OperatorGossipKeyringUseCommand struct {
    14  	Meta
    15  }
    16  
    17  func (c *OperatorGossipKeyringUseCommand) Help() string {
    18  	helpText := `
    19  Usage: nomad operator gossip keyring use [options] <key>
    20  
    21    Change the encryption key used for gossip. The key must already be installed
    22    before this operator can succeed.
    23  
    24    This command can only be run against server nodes. It returns 0 if all nodes
    25    reply and there are no errors. If any node fails to reply or reports failure,
    26    the exit code will be 1.
    27  
    28    If ACLs are enabled, this command requires a token with the 'agent:write'
    29    capability.
    30  
    31  General Options:
    32  
    33    ` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace)
    34  
    35  	return strings.TrimSpace(helpText)
    36  }
    37  
    38  func (c *OperatorGossipKeyringUseCommand) Synopsis() string {
    39  	return "Change the gossip encryption key"
    40  }
    41  
    42  func (c *OperatorGossipKeyringUseCommand) AutocompleteFlags() complete.Flags {
    43  	return c.Meta.AutocompleteFlags(FlagSetClient)
    44  }
    45  
    46  func (c *OperatorGossipKeyringUseCommand) AutocompleteArgs() complete.Predictor {
    47  	return complete.PredictAnything
    48  }
    49  
    50  func (c *OperatorGossipKeyringUseCommand) Name() string { return "operator gossip keyring use" }
    51  
    52  func (c *OperatorGossipKeyringUseCommand) Run(args []string) int {
    53  	flags := c.Meta.FlagSet("operator-gossip-keyring-use", FlagSetClient)
    54  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    55  	if err := flags.Parse(args); err != nil {
    56  		return 1
    57  	}
    58  
    59  	c.Ui = &cli.PrefixedUi{
    60  		OutputPrefix: "",
    61  		InfoPrefix:   "==> ",
    62  		ErrorPrefix:  "",
    63  		Ui:           c.Ui,
    64  	}
    65  
    66  	args = flags.Args()
    67  	if len(args) != 1 {
    68  		c.Ui.Error("This command requires one argument: <key>")
    69  		c.Ui.Error(commandErrorText(c))
    70  		return 1
    71  	}
    72  	useKey := args[0]
    73  
    74  	client, err := c.Meta.Client()
    75  	if err != nil {
    76  		c.Ui.Error(fmt.Sprintf("Error creating nomad cli client: %s", err))
    77  		return 1
    78  	}
    79  
    80  	c.Ui.Output("Changing primary gossip encryption key...")
    81  	_, err = client.Agent().UseKey(useKey)
    82  	if err != nil {
    83  		c.Ui.Error(fmt.Sprintf("error: %s", err))
    84  		return 1
    85  	}
    86  	return 0
    87  }