|
| 1 | +package parser |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/deis/deis/client-go/cmd" |
| 7 | + docopt "github.com/docopt/docopt-go" |
| 8 | +) |
| 9 | + |
| 10 | +// Limits routes limits commands to their specific function |
| 11 | +func Limits(argv []string) error { |
| 12 | + usage := ` |
| 13 | +Valid commands for limits: |
| 14 | +
|
| 15 | +limits:list list resource limits for an app |
| 16 | +limits:set set resource limits for an app |
| 17 | +limits:unset unset resource limits for an app |
| 18 | +
|
| 19 | +Use 'deis help [command]' to learn more. |
| 20 | +` |
| 21 | + if len(argv) < 2 { |
| 22 | + return limitsList([]string{"limits:list"}) |
| 23 | + } |
| 24 | + |
| 25 | + switch argv[1] { |
| 26 | + case "list": |
| 27 | + return limitsList(combineCommand(argv)) |
| 28 | + case "set": |
| 29 | + return limitSet(combineCommand(argv)) |
| 30 | + case "unset": |
| 31 | + return limitUnset(combineCommand(argv)) |
| 32 | + case "--help": |
| 33 | + fmt.Print(usage) |
| 34 | + return nil |
| 35 | + default: |
| 36 | + PrintUsage() |
| 37 | + return nil |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func limitsList(argv []string) error { |
| 42 | + usage := ` |
| 43 | +Lists resource limits for an application. |
| 44 | +
|
| 45 | +Usage: deis limits:list [options] |
| 46 | +
|
| 47 | +Options: |
| 48 | + -a --app=<app> |
| 49 | + the uniquely identifiable name of the application. |
| 50 | +` |
| 51 | + |
| 52 | + args, err := docopt.Parse(usage, argv, true, "", false, true) |
| 53 | + |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + |
| 58 | + return cmd.LimitsList(safeGetValue(args, "--app")) |
| 59 | +} |
| 60 | + |
| 61 | +func limitSet(argv []string) error { |
| 62 | + usage := ` |
| 63 | +Sets resource limits for an application. |
| 64 | +
|
| 65 | +A resource limit is a finite resource within a container which we can apply |
| 66 | +restrictions to either through the scheduler or through the Docker API. This limit |
| 67 | +is applied to each individual container, so setting a memory limit of 1G for an |
| 68 | +application means that each container gets 1G of memory. |
| 69 | +
|
| 70 | +Usage: deis limits:set [options] <type>=<limit>... |
| 71 | +
|
| 72 | +Arguments: |
| 73 | + <type> |
| 74 | + the process type as defined in your Procfile, such as 'web' or 'worker'. |
| 75 | + Note that Dockerfile apps have a default 'cmd' process type. |
| 76 | + <limit> |
| 77 | + The limit to apply to the process type. By default, this is set to --memory. |
| 78 | + You can only set one type of limit per call. |
| 79 | +
|
| 80 | + With --memory, units are represented in Bytes (B), Kilobytes (K), Megabytes |
| 81 | + (M), or Gigabytes (G). For example, 'deis limit:set cmd=1G' will restrict all |
| 82 | + "cmd" processes to a maximum of 1 Gigabyte of memory each. |
| 83 | +
|
| 84 | + With --cpu, units are represented in the number of cpu shares. For example, |
| 85 | + 'deis limit:set --cpu cmd=1024' will restrict all "cmd" processes to a |
| 86 | + maximum of 1024 cpu shares. |
| 87 | +
|
| 88 | +Options: |
| 89 | + -a --app=<app> |
| 90 | + the uniquely identifiable name for the application. |
| 91 | + -c --cpu |
| 92 | + limits cpu shares. |
| 93 | + -m --memory |
| 94 | + limits memory. [default: true] |
| 95 | +` |
| 96 | + |
| 97 | + args, err := docopt.Parse(usage, argv, true, "", false, true) |
| 98 | + |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + |
| 103 | + app := safeGetValue(args, "--app") |
| 104 | + limits := args["<type>=<limit>"].([]string) |
| 105 | + limitType := "memory" |
| 106 | + |
| 107 | + if args["--cpu"].(bool) { |
| 108 | + limitType = "cpu" |
| 109 | + } |
| 110 | + |
| 111 | + return cmd.LimitsSet(app, limits, limitType) |
| 112 | +} |
| 113 | + |
| 114 | +func limitUnset(argv []string) error { |
| 115 | + usage := ` |
| 116 | +Unsets resource limits for an application. |
| 117 | +
|
| 118 | +Usage: deis limits:unset [options] [--memory | --cpu] <type>... |
| 119 | +
|
| 120 | +Arguments: |
| 121 | + <type> |
| 122 | + the process type as defined in your Procfile, such as 'web' or 'worker'. |
| 123 | + Note that Dockerfile apps have a default 'cmd' process type. |
| 124 | +
|
| 125 | +Options: |
| 126 | + -a --app=<app> |
| 127 | + the uniquely identifiable name for the application. |
| 128 | + -c --cpu |
| 129 | + limits cpu shares. |
| 130 | + -m --memory |
| 131 | + limits memory. [default: true] |
| 132 | +` |
| 133 | + |
| 134 | + args, err := docopt.Parse(usage, argv, true, "", false, true) |
| 135 | + |
| 136 | + if err != nil { |
| 137 | + return err |
| 138 | + } |
| 139 | + |
| 140 | + app := safeGetValue(args, "--app") |
| 141 | + limits := args["<type>"].([]string) |
| 142 | + limitType := "memory" |
| 143 | + |
| 144 | + if args["--cpu"].(bool) { |
| 145 | + limitType = "cpu" |
| 146 | + } |
| 147 | + |
| 148 | + return cmd.LimitsUnset(app, limits, limitType) |
| 149 | +} |
0 commit comments