github.com/engineyard/workflow-cli@v2.21.6+incompatible/parser/limits.go (about)

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