github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/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() + `
    28  
    29  Client Config Options:
    30  
    31    -servers
    32      List the known server addresses of the client node. Client
    33      nodes do not participate in the gossip pool, and instead
    34      register with these servers periodically over the network.
    35  
    36    -update-servers
    37      Updates the client's server list using the provided
    38      arguments. Multiple server addresses may be passed using
    39      multiple arguments. IMPORTANT: When updating the servers
    40      list, you must specify ALL of the server nodes you wish
    41      to configure. The set is updated atomically.
    42  
    43      Example:
    44        $ nomad node config -update-servers foo:4647 bar:4647
    45  `
    46  	return strings.TrimSpace(helpText)
    47  }
    48  
    49  func (c *NodeConfigCommand) Synopsis() string {
    50  	return "View or modify client configuration details"
    51  }
    52  
    53  func (c *NodeConfigCommand) Name() string { return "node config" }
    54  
    55  func (c *NodeConfigCommand) Run(args []string) int {
    56  	var listServers, updateServers bool
    57  
    58  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    59  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    60  	flags.BoolVar(&listServers, "servers", false, "")
    61  	flags.BoolVar(&updateServers, "update-servers", false, "")
    62  
    63  	if err := flags.Parse(args); err != nil {
    64  		return 1
    65  	}
    66  	args = flags.Args()
    67  
    68  	// Check the flags for misuse
    69  	if !listServers && !updateServers {
    70  		c.Ui.Error("The '-servers' or '-update-servers' flag(s) must be set")
    71  		c.Ui.Error(commandErrorText(c))
    72  		return 1
    73  	}
    74  
    75  	// Get the HTTP client
    76  	client, err := c.Meta.Client()
    77  	if err != nil {
    78  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    79  		return 1
    80  	}
    81  
    82  	if updateServers {
    83  		// Get the server addresses
    84  		if len(args) == 0 {
    85  			c.Ui.Error("If the '-update-servers' flag is set, at least one server argument must be provided")
    86  			c.Ui.Error(commandErrorText(c))
    87  			return 1
    88  		}
    89  
    90  		// Set the servers list
    91  		if err := client.Agent().SetServers(args); err != nil {
    92  			c.Ui.Error(fmt.Sprintf("Error updating server list: %s", err))
    93  			return 1
    94  		}
    95  		c.Ui.Output(fmt.Sprint("Updated server list"))
    96  		return 0
    97  	}
    98  
    99  	if listServers {
   100  		// Query the current server list
   101  		servers, err := client.Agent().Servers()
   102  		if err != nil {
   103  			c.Ui.Error(fmt.Sprintf("Error querying server list: %s", err))
   104  			return 1
   105  		}
   106  
   107  		// Print the results
   108  		for _, server := range servers {
   109  			c.Ui.Output(server)
   110  		}
   111  		return 0
   112  	}
   113  
   114  	// Should not make it this far
   115  	return 1
   116  }
   117  
   118  func (c *NodeConfigCommand) AutocompleteFlags() complete.Flags {
   119  	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
   120  		complete.Flags{
   121  			"-servers":        complete.PredictNothing,
   122  			"-update-servers": complete.PredictAnything,
   123  		})
   124  }
   125  
   126  func (c *NodeConfigCommand) AutocompleteArgs() complete.Predictor {
   127  	return complete.PredictNothing
   128  }