Navigation Menu

Skip to content

Commit

Permalink
Provide a flag to skip pulling images - helps reduce startup and exec…
Browse files Browse the repository at this point in the history
…ution time. (#94)
  • Loading branch information
johnewart authored and sanathkr committed Aug 26, 2017
1 parent abaebb4 commit 0d57b5e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 24 deletions.
1 change: 1 addition & 0 deletions invoke.go
Expand Up @@ -121,6 +121,7 @@ func invoke(c *cli.Context) {
Basedir: baseDir,
CheckWorkingDirExist: checkWorkingDirExist,
DebugPort: c.String("debug-port"),
SkipPullImage: c.Bool("skip-pull-image"),
})
if err != nil {
log.Fatalf("Could not initiate %s runtime: %s\n", function.Runtime, err)
Expand Down
14 changes: 13 additions & 1 deletion main.go
Expand Up @@ -96,7 +96,13 @@ func main() {
"you must mount the path where the SAM file exists on the docker machine and modify this value to match the remote machine.",
EnvVar: "SAM_DOCKER_VOLUME_BASEDIR",
},
},
cli.BoolFlag {
Name: "skip-pull-image, p",
Usage: "Optional. Specify whether SAM should skip pulling down the latest Docker image. Default is false.",
EnvVar: "SAM_SKIP_PULL_IMAGE",
},

},
},
cli.Command{
Name: "invoke",
Expand Down Expand Up @@ -135,6 +141,12 @@ func main() {
"you must mount the path where the SAM file exists on the docker machine and modify this value to match the remote machine.",
EnvVar: "SAM_DOCKER_VOLUME_BASEDIR",
},
cli.BoolFlag {
Name: "skip-pull-image, p",
Usage: "Optional. Specify whether SAM should skip pulling down the latest Docker image. Default is false.",
EnvVar: "SAM_SKIP_PULL_IMAGE",
},

},
},
cli.Command{
Expand Down
61 changes: 39 additions & 22 deletions runtime.go
Expand Up @@ -98,6 +98,7 @@ type NewRuntimeOpt struct {
Basedir string
CheckWorkingDirExist bool
DebugPort string
SkipPullImage bool
}

// NewRuntime instantiates a Lambda runtime container
Expand Down Expand Up @@ -144,35 +145,51 @@ func NewRuntime(opt NewRuntimeOpt) (Invoker, error) {
return nil, err
}

log.Printf("Fetching %s image for %s runtime...\n", r.Image, opt.Function.Runtime)
progress, err := cli.ImagePull(r.Context, r.Image, types.ImagePullOptions{})
if len(images) < 0 && err != nil {
log.Fatalf("Could not fetch %s Docker image\n%s", r.Image, err)
return nil, err
// By default, pull images unless we are told not to
pullImage := true

if (opt.SkipPullImage) {
log.Printf("Requested to skip pulling images ...\n")
pullImage = false
}

if err != nil {
log.Printf("Could not fetch %s Docker image: %s\n", r.Image, err)
} else {
// However, if we don't have the image we will need it...
if (len(images) == 0) {
log.Printf("Runtime image missing, will pull....\n")
pullImage = true
}

// Use Docker's standard progressbar to show image pull progress.
// However there is a bug that we are working around. We'll do the same
// as Docker does, and temporarily set the TERM to a non-existant
// terminal
// More info here:
// https://github.com/Nvveen/Gotty/pull/1
if (pullImage) {
log.Printf("Fetching %s image for %s runtime...\n", r.Image, opt.Function.Runtime)
progress, err := cli.ImagePull(r.Context, r.Image, types.ImagePullOptions{})
if len(images) < 0 && err != nil {
log.Fatalf("Could not fetch %s Docker image\n%s", r.Image, err)
return nil, err
}

origTerm := os.Getenv("TERM")
os.Setenv("TERM", "eifjccgifcdekgnbtlvrgrinjjvfjggrcudfrriivjht")
defer os.Setenv("TERM", origTerm)
if err != nil {
log.Printf("Could not fetch %s Docker image: %s\n", r.Image, err)
} else {

// Use Docker's standard progressbar to show image pull progress.
// However there is a bug that we are working around. We'll do the same
// as Docker does, and temporarily set the TERM to a non-existant
// terminal
// More info here:
// https://github.com/Nvveen/Gotty/pull/1

// Show the Docker pull messages in green
color.Output = colorable.NewColorableStderr()
color.Set(color.FgGreen)
defer color.Unset()
origTerm := os.Getenv("TERM")
os.Setenv("TERM", "eifjccgifcdekgnbtlvrgrinjjvfjggrcudfrriivjht")
defer os.Setenv("TERM", origTerm)

jsonmessage.DisplayJSONMessagesStream(progress, os.Stderr, os.Stderr.Fd(), term.IsTerminal(os.Stderr.Fd()), nil)
// Show the Docker pull messages in green
color.Output = colorable.NewColorableStderr()
color.Set(color.FgGreen)
defer color.Unset()

jsonmessage.DisplayJSONMessagesStream(progress, os.Stderr, os.Stderr.Fd(), term.IsTerminal(os.Stderr.Fd()), nil)

}
}

return r, nil
Expand Down
2 changes: 1 addition & 1 deletion start.go
Expand Up @@ -117,13 +117,13 @@ func start(c *cli.Context) {

// Find the env-vars map for the function
funcEnvVarsOverrides := envVarsOverrides[name]

runt, err := NewRuntime(NewRuntimeOpt{
Function: function,
EnvVarsOverrides: funcEnvVarsOverrides,
Basedir: filepath.Dir(filename),
CheckWorkingDirExist: checkWorkingDirExist,
DebugPort: c.String("debug-port"),
SkipPullImage: c.Bool("skip-pull-image"),
})
if err != nil {
if err == ErrRuntimeNotSupported {
Expand Down

0 comments on commit 0d57b5e

Please sign in to comment.