forked from minio/minio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
198 lines (172 loc) · 4.69 KB
/
commands.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"os/user"
"path"
"strings"
"time"
"github.com/dustin/go-humanize"
"github.com/minio/cli"
"github.com/minio/minio/pkg/iodine"
"github.com/minio/minio/pkg/server"
)
var commands = []cli.Command{
modeCmd,
}
var modeCommands = []cli.Command{
memoryCmd,
fsCmd,
donutCmd,
}
var modeCmd = cli.Command{
Name: "mode",
Subcommands: modeCommands,
Description: "Mode of execution",
}
var memoryCmd = cli.Command{
Name: "memory",
Description: "Limit maximum memory usage to SIZE in [B, KB, MB, GB]",
Action: runMemory,
CustomHelpTemplate: `NAME:
minio mode {{.Name}} - {{.Description}}
USAGE:
minio mode {{.Name}} limit SIZE expire TIME
EXAMPLES:
1. Limit maximum memory usage to 64MB with 1 hour expiration
$ minio mode {{.Name}} limit 64MB expire 1h
2. Limit maximum memory usage to 4GB with no expiration
$ minio mode {{.Name}} limit 4GB
`,
}
var fsCmd = cli.Command{
Name: "fs",
Description: "Path to filesystem volume.",
Action: runFilesystem,
CustomHelpTemplate: `NAME:
minio mode {{.Name}} - {{.Description}}
USAGE:
minio mode {{.Name}} limit SIZE expire TIME
EXAMPLES:
1. Export an existing filesystem path
$ minio mode {{.Name}} /var/www
`,
}
var donutCmd = cli.Command{
Name: "donut",
Description: "[status: EXPERIMENTAL]. Path to donut volume.",
Action: runDonut,
CustomHelpTemplate: `NAME:
minio mode {{.Name}} - {{.Description}}
USAGE:
minio mode {{.Name}} PATH
EXAMPLES:
1. Create a donut volume under "/mnt/backup"
$ minio mode {{.Name}} /mnt/backup
2. Create a temporary donut volume under "/tmp"
$ minio mode {{.Name}} /tmp
3. Create a donut volume under collection of paths
$ minio mode {{.Name}} /mnt/backup2014feb /mnt/backup2014feb
`,
}
func runMemory(c *cli.Context) {
if len(c.Args()) == 0 || len(c.Args())%2 != 0 {
cli.ShowCommandHelpAndExit(c, "memory", 1) // last argument is exit code
}
apiServerConfig := getAPIServerConfig(c)
var maxMemory uint64
maxMemorySet := false
var expiration time.Duration
expirationSet := false
var err error
args := c.Args()
for len(args) > 0 {
switch args.First() {
case "limit":
{
if maxMemorySet {
Fatalln("Limit should be set only once")
}
args = args.Tail()
maxMemory, err = humanize.ParseBytes(args.First())
if err != nil {
Fatalf("Invalid memory size [%s] passed. Reason: %s\n", args.First(), iodine.New(err, nil))
}
if maxMemory < 1024*1024*10 {
Fatalf("Invalid memory size [%s] passed. Should be greater than 10M\n", args.First())
}
args = args.Tail()
maxMemorySet = true
}
case "expire":
{
if expirationSet {
Fatalln("Expiration should be set only once")
}
args = args.Tail()
expiration, err = time.ParseDuration(args.First())
if err != nil {
Fatalf("Invalid expiration time [%s] passed. Reason: %s\n", args.First(), iodine.New(err, nil))
}
args = args.Tail()
expirationSet = true
}
default:
{
cli.ShowCommandHelpAndExit(c, "memory", 1) // last argument is exit code
}
}
}
if maxMemorySet == false {
Fatalln("Memory limit must be set")
}
memoryDriver := server.MemoryFactory{
Config: apiServerConfig,
MaxMemory: maxMemory,
Expiration: expiration,
}
apiServer := memoryDriver.GetStartServerFunc()
// webServer := getWebServerConfigFunc(c)
servers := []server.StartServerFunc{apiServer} //, webServer}
server.StartMinio(servers)
}
func runDonut(c *cli.Context) {
u, err := user.Current()
if err != nil {
Fatalf("Unable to determine current user. Reason: %s\n", err)
}
if len(c.Args()) < 1 {
cli.ShowCommandHelpAndExit(c, "donut", 1) // last argument is exit code
}
// supporting multiple paths
var paths []string
if strings.TrimSpace(c.Args().First()) == "" {
p := path.Join(u.HomeDir, "minio-storage", "donut")
paths = append(paths, p)
} else {
for _, arg := range c.Args() {
paths = append(paths, strings.TrimSpace(arg))
}
}
apiServerConfig := getAPIServerConfig(c)
donutDriver := server.DonutFactory{
Config: apiServerConfig,
Paths: paths,
}
apiServer := donutDriver.GetStartServerFunc()
// webServer := getWebServerConfigFunc(c)
servers := []server.StartServerFunc{apiServer} //, webServer}
server.StartMinio(servers)
}
func runFilesystem(c *cli.Context) {
if len(c.Args()) != 1 {
cli.ShowCommandHelpAndExit(c, "fs", 1) // last argument is exit code
}
apiServerConfig := getAPIServerConfig(c)
fsDriver := server.FilesystemFactory{
Config: apiServerConfig,
Path: c.Args()[0],
}
apiServer := fsDriver.GetStartServerFunc()
// webServer := getWebServerConfigFunc(c)
servers := []server.StartServerFunc{apiServer} //, webServer}
server.StartMinio(servers)
}