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