-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
94 lines (78 loc) · 1.76 KB
/
main.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
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"regexp"
"github.com/codegangsta/cli"
"github.com/inoc603/go-limit/limit"
"github.com/satori/go.uuid"
)
func main() {
app := cli.NewApp()
app.Name = "limit"
app.Usage = "Limit cgroup usage"
app.Flags = []cli.Flag{
cli.IntFlag{
Name: "cpu",
Usage: "CPU percentage limit",
Value: 0,
},
cli.StringFlag{
Name: "memory, m",
Usage: "Memory limit",
},
}
app.Action = func(c *cli.Context) {
if err := checkArgs(c); err != nil {
fmt.Println(err.Error())
return
}
uid := "limit-" + uuid.NewV4().String()
cgroupHelper := limit.NewCgroupHelper(uid)
defer cgroupHelper.Delete()
if memory := c.String("memory"); memory != "" {
err := cgroupHelper.SetMemory(memory)
if err != nil {
fmt.Println(err.Error())
return
}
}
if cpu := c.Int("cpu"); cpu != 0 {
err := cgroupHelper.SetCPUPercentage(c.Int("cpu"))
if err != nil {
fmt.Println(err.Error())
return
}
}
// Add this process to the cgroup, so its child process can
// be controlled
err := cgroupHelper.AddTask(os.Getpid())
if err != nil {
fmt.Println(err.Error())
return
}
// Run command as child process
cmd := exec.Command(c.Args()[0], c.Args()[1:]...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Start()
// Wait for command to stop
cmd.Wait()
}
app.Run(os.Args)
}
func checkArgs(c *cli.Context) error {
if len(c.Args()) == 0 {
return errors.New("No command")
}
if c.Int("cpu") > 100 || c.Int("cpu") < 0 {
return errors.New("CPU percentage out of range")
}
memReg := regexp.MustCompile(`^(\d+)([kKmMgG]*)$`)
if c.String("memory") != "" && !memReg.MatchString(c.String("memory")) {
return errors.New("Invalid memory limit format")
}
return nil
}