Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

Commit

Permalink
cli: allow specifying limits for run jobs
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Ferreira <imjorge+flynn@gmail.com>
  • Loading branch information
Jorge Ferreira authored and lmars committed Nov 25, 2016
1 parent 460311b commit 6b003e4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
35 changes: 30 additions & 5 deletions cli/run.go
Expand Up @@ -8,28 +8,32 @@ import (
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"

"github.com/docker/docker/pkg/term"
"github.com/flynn/flynn/controller/client"
ct "github.com/flynn/flynn/controller/types"
"github.com/flynn/flynn/host/resource"
"github.com/flynn/flynn/pkg/cluster"
"github.com/flynn/flynn/pkg/shutdown"
"github.com/flynn/flynn/pkg/typeconv"
"github.com/flynn/go-docopt"
)

func init() {
cmd := register("run", runRun, `
usage: flynn run [-d] [-r <release>] [-e <entrypoint>] [-l] [--] <command> [<argument>...]
usage: flynn run [-d] [-r <release>] [-e <entrypoint>] [-l] [--limits <limits>] [--] <command> [<argument>...]
Run a job.
Options:
-d, --detached run job without connecting io streams (implies --enable-log)
-r <release> id of release to run (defaults to current app release)
-e <entrypoint> [DEPRECATED] overwrite the default entrypoint of the release's image
-l, --enable-log send output to log streams
-d, --detached run job without connecting io streams (implies --enable-log)
-r <release> id of release to run (defaults to current app release)
-e <entrypoint> [DEPRECATED] overwrite the default entrypoint of the release's image
-l, --enable-log send output to log streams
--limits <limits> comma separated limits for the run job (see "flynn limit -h" for format)

This comment has been minimized.

Copy link
@the-architect

the-architect Apr 14, 2017

Can you update the docs with this new option? I couldn't find it. 👍

`)
cmd.optsFirst = true
}
Expand Down Expand Up @@ -64,6 +68,25 @@ func runRun(args *docopt.Args, client controller.Client) error {
fmt.Fprintln(os.Stderr, "WARN: The -e flag is deprecated and will be removed in future versions, use <command> as the entrypoint")
config.Args = append([]string{e}, config.Args...)
}
if args.String["--limits"] != "" {
config.Resources = resource.Defaults()
limits := strings.Split(args.String["--limits"], ",")
for _, limit := range limits {
typVal := strings.SplitN(limit, "=", 2)
if len(typVal) != 2 {
return fmt.Errorf("invalid resource limit: %q", limit)
}
typ, ok := resource.ToType(typVal[0])
if !ok {
return fmt.Errorf("invalid resource limit type: %q", typVal)
}
val, err := resource.ParseLimit(typ, typVal[1])
if err != nil {
return fmt.Errorf("invalid resource limit value: %q", typVal[1])
}
config.Resources[typ] = resource.Spec{Limit: typeconv.Int64Ptr(val)}
}
}
return runJob(client, config)
}

Expand All @@ -81,6 +104,7 @@ type runConfig struct {
DisableLog bool
Exit bool
Data bool
Resources resource.Resources

// DeprecatedArtifact is to support using an explicit artifact
// with old clusters which don't accept multiple artifacts
Expand All @@ -98,6 +122,7 @@ func runJob(client controller.Client, config runConfig) error {
ReleaseEnv: config.ReleaseEnv,
DisableLog: config.DisableLog,
Data: config.Data,
Resources: config.Resources,
}

// ensure slug apps from old clusters use /runner/init
Expand Down
8 changes: 8 additions & 0 deletions test/test_cli.go
Expand Up @@ -20,6 +20,7 @@ import (
"syscall"
"time"

units "github.com/docker/go-units"
"github.com/flynn/flynn/cli/config"
"github.com/flynn/flynn/controller/client"
ct "github.com/flynn/flynn/controller/types"
Expand Down Expand Up @@ -929,6 +930,13 @@ func (s *CLISuite) TestRunLimits(t *c.C) {
t.Assert(limits[0], c.Equals, strconv.FormatInt(*defaults[resource.TypeMemory].Limit, 10))
t.Assert(limits[1], c.Equals, strconv.FormatInt(1024, 10))
t.Assert(limits[2], c.Equals, strconv.FormatInt(*defaults[resource.TypeMaxFD].Limit, 10))
cmd = app.flynn("run", "--limits", "memory=200MB,max_fd=9000", "sh", "-c", resourceCmd)
t.Assert(cmd, Succeeds)
limits = strings.Split(strings.TrimSpace(cmd.Output), "\n")
t.Assert(limits, c.HasLen, 3)
t.Assert(limits[0], c.Equals, strconv.FormatInt(200*units.MiB, 10))
t.Assert(limits[1], c.Equals, strconv.FormatInt(1024, 10))
t.Assert(limits[2], c.Equals, strconv.FormatInt(9000, 10))
}

func assertExportContains(t *c.C, file string, paths ...string) {
Expand Down

0 comments on commit 6b003e4

Please sign in to comment.