github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/command/node_config.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/posener/complete"
     8  )
     9  
    10  type NodeConfigCommand struct {
    11  	Meta
    12  }
    13  
    14  func (c *NodeConfigCommand) Help() string {
    15  	helpText := `
    16  Usage: nomad node config [options]
    17  
    18    View or modify a client node's configuration details. This command only works
    19    on client nodes, and can be used to update the running client configurations
    20    it supports.
    21  
    22    The arguments behave differently depending on the flags given. See each
    23    flag's description for its specific requirements.
    24  
    25  General Options:
    26  
    27    ` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
    28  
    29  Client Config Options:
    30  
    31    -servers
    32      List the known server addresses of the client node. Client nodes do not
    33      participate in the gossip pool, and instead register with these servers
    34      periodically over the network.
    35  
    36      If ACLs are enabled, this option requires a token with the 'agent:read'
    37      capability.
    38  
    39    -update-servers
    40      Updates the client's server list using the provided arguments. Multiple
    41      server addresses may be passed using multiple arguments. IMPORTANT: When
    42      updating the servers list, you must specify ALL of the server nodes you
    43      wish to configure. The set is updated atomically.
    44  
    45      If ACLs are enabled, this option requires a token with the 'agent:write'
    46      capability.
    47  
    48      Example:
    49        $ nomad node config -update-servers foo:4647 bar:4647
    50  `
    51  	return strings.TrimSpace(helpText)
    52  }
    53  
    54  func (c *NodeConfigCommand) Synopsis() string {
    55  	return "View or modify client configuration details"
    56  }
    57  
    58  func (c *NodeConfigCommand) Name() string { return "node config" }
    59  
    60  func (c *NodeConfigCommand) Run(args []string) int {
    61  	var listServers, updateServers bool
    62  
    63  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    64  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    65  	flags.BoolVar(&listServers, "servers", false, "")
    66  	flags.BoolVar(&updateServers, "update-servers", false, "")
    67  
    68  	if err := flags.Parse(args); err != nil {
    69  		return 1
    70  	}
    71  	args = flags.Args()
    72  
    73  	// Check the flags for misuse
    74  	if !listServers && !updateServers {
    75  		c.Ui.Error("The '-servers' or '-update-servers' flag(s) must be set")
    76  		c.Ui.Error(commandErrorText(c))
    77  		return 1
    78  	}
    79  
    80  	// Get the HTTP client
    81  	client, err := c.Meta.Client()
    82  	if err != nil {
    83  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    84  		return 1
    85  	}
    86  
    87  	if updateServers {
    88  		// Get the server addresses
    89  		if len(args) == 0 {
    90  			c.Ui.Error("If the '-update-servers' flag is set, at least one server argument must be provided")
    91  			c.Ui.Error(commandErrorText(c))
    92  			return 1
    93  		}
    94  
    95  		// Set the servers list
    96  		if err := client.Agent().SetServers(args); err != nil {
    97  			c.Ui.Error(fmt.Sprintf("Error updating server list: %s", err))
    98  			return 1
    99  		}
   100  		c.Ui.Output("Updated server list")
   101  		return 0
   102  	}
   103  
   104  	if listServers {
   105  		// Query the current server list
   106  		servers, err := client.Agent().Servers()
   107  		if err != nil {
   108  			c.Ui.Error(fmt.Sprintf("Error querying server list: %s", err))
   109  			return 1
   110  		}
   111  
   112  		// Print the results
   113  		for _, server := range servers {
   114  			c.Ui.Output(server)
   115  		}
   116  		return 0
   117  	}
   118  
   119  	// Should not make it this far
   120  	return 1
   121  }
   122  
   123  func (c *NodeConfigCommand) AutocompleteFlags() complete.Flags {
   124  	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
   125  		complete.Flags{
   126  			"-servers":        complete.PredictNothing,
   127  			"-update-servers": complete.PredictAnything,
   128  		})
   129  }
   130  
   131  func (c *NodeConfigCommand) AutocompleteArgs() complete.Predictor {
   132  	return complete.PredictNothing
   133  }