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