github.com/djenriquez/nomad-1@v0.8.1/command/acl_bootstrap.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/hashicorp/nomad/api" 8 "github.com/posener/complete" 9 ) 10 11 type ACLBootstrapCommand struct { 12 Meta 13 } 14 15 func (c *ACLBootstrapCommand) Help() string { 16 helpText := ` 17 Usage: nomad acl bootstrap [options] 18 19 Bootstrap is used to bootstrap the ACL system and get an initial token. 20 21 General Options: 22 23 ` + generalOptionsUsage() + ` 24 25 ` 26 return strings.TrimSpace(helpText) 27 } 28 29 func (c *ACLBootstrapCommand) AutocompleteFlags() complete.Flags { 30 return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient), 31 complete.Flags{}) 32 } 33 34 func (c *ACLBootstrapCommand) AutocompleteArgs() complete.Predictor { 35 return complete.PredictNothing 36 } 37 38 func (c *ACLBootstrapCommand) Synopsis() string { 39 return "Bootstrap the ACL system for initial token" 40 } 41 42 func (c *ACLBootstrapCommand) Run(args []string) int { 43 flags := c.Meta.FlagSet("acl bootstrap", FlagSetClient) 44 flags.Usage = func() { c.Ui.Output(c.Help()) } 45 if err := flags.Parse(args); err != nil { 46 return 1 47 } 48 49 // Check that we got no arguments 50 args = flags.Args() 51 if l := len(args); l != 0 { 52 c.Ui.Error(c.Help()) 53 return 1 54 } 55 56 // Get the HTTP client 57 client, err := c.Meta.Client() 58 if err != nil { 59 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 60 return 1 61 } 62 63 // Get the bootstrap token 64 token, _, err := client.ACLTokens().Bootstrap(nil) 65 if err != nil { 66 c.Ui.Error(fmt.Sprintf("Error bootstrapping: %s", err)) 67 return 1 68 } 69 70 // Format the output 71 c.Ui.Output(formatKVACLToken(token)) 72 return 0 73 } 74 75 // formatKVPolicy returns a K/V formatted policy 76 func formatKVPolicy(policy *api.ACLPolicy) string { 77 output := []string{ 78 fmt.Sprintf("Name|%s", policy.Name), 79 fmt.Sprintf("Description|%s", policy.Description), 80 fmt.Sprintf("Rules|%s", policy.Rules), 81 fmt.Sprintf("CreateIndex|%v", policy.CreateIndex), 82 fmt.Sprintf("ModifyIndex|%v", policy.ModifyIndex), 83 } 84 return formatKV(output) 85 } 86 87 // formatKVACLToken returns a K/V formatted ACL token 88 func formatKVACLToken(token *api.ACLToken) string { 89 // Add the fixed preamble 90 output := []string{ 91 fmt.Sprintf("Accessor ID|%s", token.AccessorID), 92 fmt.Sprintf("Secret ID|%s", token.SecretID), 93 fmt.Sprintf("Name|%s", token.Name), 94 fmt.Sprintf("Type|%s", token.Type), 95 fmt.Sprintf("Global|%v", token.Global), 96 } 97 98 // Special case the policy output 99 if token.Type == "management" { 100 output = append(output, "Policies|n/a") 101 } else { 102 output = append(output, fmt.Sprintf("Policies|%v", token.Policies)) 103 } 104 105 // Add the generic output 106 output = append(output, 107 fmt.Sprintf("Create Time|%v", token.CreateTime), 108 fmt.Sprintf("Create Index|%d", token.CreateIndex), 109 fmt.Sprintf("Modify Index|%d", token.ModifyIndex), 110 ) 111 return formatKV(output) 112 }