github.com/hernad/nomad@v1.6.112/command/quota_init.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package command 5 6 import ( 7 "fmt" 8 "os" 9 "strings" 10 11 "github.com/posener/complete" 12 ) 13 14 const ( 15 // DefaultHclQuotaInitName is the default name we use when initializing the 16 // example quota file in HCL format 17 DefaultHclQuotaInitName = "spec.hcl" 18 19 // DefaultHclQuotaInitName is the default name we use when initializing the 20 // example quota file in JSON format 21 DefaultJsonQuotaInitName = "spec.json" 22 ) 23 24 // QuotaInitCommand generates a new quota spec that you can customize to your 25 // liking, like vagrant init 26 type QuotaInitCommand struct { 27 Meta 28 } 29 30 func (c *QuotaInitCommand) Help() string { 31 helpText := ` 32 Usage: nomad quota init <filename> 33 34 Creates an example quota specification file that can be used as a starting 35 point to customize further. If no filename is given, the default of "spec.hcl" 36 or "spec.json" will be used. 37 38 Init Options: 39 40 -json 41 Create an example JSON quota specification. 42 ` 43 return strings.TrimSpace(helpText) 44 } 45 46 func (c *QuotaInitCommand) Synopsis() string { 47 return "Create an example quota specification file" 48 } 49 50 func (c *QuotaInitCommand) AutocompleteFlags() complete.Flags { 51 return complete.Flags{ 52 "-json": complete.PredictNothing, 53 } 54 } 55 56 func (c *QuotaInitCommand) AutocompleteArgs() complete.Predictor { 57 return complete.PredictNothing 58 } 59 60 func (c *QuotaInitCommand) Name() string { return "quota init" } 61 62 func (c *QuotaInitCommand) Run(args []string) int { 63 var jsonOutput bool 64 flags := c.Meta.FlagSet(c.Name(), FlagSetClient) 65 flags.Usage = func() { c.Ui.Output(c.Help()) } 66 flags.BoolVar(&jsonOutput, "json", false, "") 67 68 if err := flags.Parse(args); err != nil { 69 return 1 70 } 71 72 // Check that we get no arguments 73 args = flags.Args() 74 if l := len(args); l > 1 { 75 c.Ui.Error("This command takes no arguments or one: <filename>") 76 c.Ui.Error(commandErrorText(c)) 77 return 1 78 } 79 80 fileName := DefaultHclQuotaInitName 81 fileContent := defaultHclQuotaSpec 82 if jsonOutput { 83 fileName = DefaultJsonQuotaInitName 84 fileContent = defaultJsonQuotaSpec 85 } 86 if len(args) == 1 { 87 fileName = args[0] 88 } 89 90 // Check if the file already exists 91 _, err := os.Stat(fileName) 92 if err != nil && !os.IsNotExist(err) { 93 c.Ui.Error(fmt.Sprintf("Failed to stat %q: %v", fileName, err)) 94 return 1 95 } 96 if !os.IsNotExist(err) { 97 c.Ui.Error(fmt.Sprintf("Quota specification %q already exists", fileName)) 98 return 1 99 } 100 101 // Write out the example 102 err = os.WriteFile(fileName, []byte(fileContent), 0660) 103 if err != nil { 104 c.Ui.Error(fmt.Sprintf("Failed to write %q: %v", fileName, err)) 105 return 1 106 } 107 108 // Success 109 c.Ui.Output(fmt.Sprintf("Example quota specification written to %s", fileName)) 110 return 0 111 } 112 113 var defaultHclQuotaSpec = strings.TrimSpace(` 114 name = "default-quota" 115 description = "Limit the shared default namespace" 116 117 # Create a limit for the global region. Additional limits may 118 # be specified in-order to limit other regions. 119 limit { 120 region = "global" 121 region_limit { 122 cpu = 2500 123 memory = 1000 124 memory_max = 1000 125 } 126 variables_limit = 1000 127 } 128 `) 129 130 var defaultJsonQuotaSpec = strings.TrimSpace(` 131 { 132 "Name": "default-quota", 133 "Description": "Limit the shared default namespace", 134 "Limits": [ 135 { 136 "Region": "global", 137 "RegionLimit": { 138 "CPU": 2500, 139 "MemoryMB": 1000, 140 "MemoryMaxMB": 1000 141 }, 142 "VariablesLimit": 1000 143 } 144 ] 145 } 146 `)