github.com/hernad/nomad@v1.6.112/command/operator_gossip_keyring_install.go (about)

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