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