github.com/hooklift/nomad@v0.5.7-0.20170407200202-db11e7dd7b55/command/client_config.go (about)

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