Skip to content

Commit

Permalink
Shrink-wrap for build
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis <[email protected]>
  • Loading branch information
alexellis committed Nov 4, 2017
1 parent 5cd3ea2 commit 6863e2d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
42 changes: 24 additions & 18 deletions builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

// BuildImage construct Docker image from function parameters
func BuildImage(image string, handler string, functionName string, language string, nocache bool, squash bool) {
func BuildImage(image string, handler string, functionName string, language string, nocache bool, squash bool, shrinkwrap bool) {

switch language {
case "node", "python", "ruby", "csharp", "python3", "go":
Expand All @@ -22,29 +22,35 @@ func BuildImage(image string, handler string, functionName string, language stri

flagStr := buildFlagString(nocache, squash, os.Getenv("http_proxy"), os.Getenv("https_proxy"))

builder := strings.Split(fmt.Sprintf("docker build %s-t %s .", flagStr, image), " ")
fmt.Println(strings.Join(builder, " "))
ExecCommand(tempPath, builder)
fmt.Printf("Image: %s built.\n", image)
if shrinkwrap {
fmt.Printf("%s shrink-wrapped to %s\n", functionName, tempPath)
} else {
builder := strings.Split(fmt.Sprintf("docker build %s-t %s .", flagStr, image), " ")

ExecCommand(tempPath, builder)
fmt.Printf("Image: %s built.\n", image)
}
break
case "Dockerfile", "dockerfile":
tempPath := handler
if _, err := os.Stat(handler); err != nil {
fmt.Printf("Unable to build %s, %s is an invalid path\n", image, handler)
fmt.Printf("Image: %s not built.\n", image)

break
}
fmt.Printf("Building: %s with Dockerfile. Please wait..\n", image)
if shrinkwrap {
fmt.Printf("Nothing to do for: %s.\n", functionName)
} else {
tempPath := handler
if _, err := os.Stat(handler); err != nil {
fmt.Printf("Unable to build %s, %s is an invalid path\n", image, handler)
fmt.Printf("Image: %s not built.\n", image)

flagStr := buildFlagString(nocache, squash, os.Getenv("http_proxy"), os.Getenv("https_proxy"))
break
}
fmt.Printf("Building: %s with Dockerfile. Please wait..\n", image)

builder := strings.Split(fmt.Sprintf("docker build %s-t %s .", flagStr, image), " ")
fmt.Println(strings.Join(builder, " "))
ExecCommand(tempPath, builder)
fmt.Printf("Image: %s built.\n", image)
flagStr := buildFlagString(nocache, squash, os.Getenv("http_proxy"), os.Getenv("https_proxy"))

builder := strings.Split(fmt.Sprintf("docker build %s-t %s .", flagStr, image), " ")
fmt.Println(strings.Join(builder, " "))
ExecCommand(tempPath, builder)
fmt.Printf("Image: %s built.\n", image)
}
default:
log.Fatalf("Language template: %s not supported. Build a custom Dockerfile instead.", language)
}
Expand Down
18 changes: 11 additions & 7 deletions commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import (

// Flags that are to be added to commands.
var (
nocache bool
squash bool
parallel int
nocache bool
squash bool
parallel int
shrinkwrap bool
)

func init() {
Expand All @@ -34,6 +35,8 @@ func init() {
[experimental] `)
buildCmd.Flags().IntVar(&parallel, "parallel", 1, "Build in parallel to depth specified.")

buildCmd.Flags().BoolVar(&shrinkwrap, "shrinkwrap", false, "Just write files to ./build/ folder for shrink-wrapping")

// Set bash-completion.
_ = buildCmd.Flags().SetAnnotation("handler", cobra.BashCompSubdirsInDir, []string{})

Expand Down Expand Up @@ -83,7 +86,7 @@ func runBuild(cmd *cobra.Command, args []string) error {
}

if len(services.Functions) > 0 {
build(&services, parallel)
build(&services, parallel, shrinkwrap)
} else {
if len(image) == 0 {
return fmt.Errorf("please provide a valid -image name for your Docker image")
Expand All @@ -94,13 +97,13 @@ func runBuild(cmd *cobra.Command, args []string) error {
if len(functionName) == 0 {
return fmt.Errorf("please provide the deployed -name of your function")
}
builder.BuildImage(image, handler, functionName, language, nocache, squash)
builder.BuildImage(image, handler, functionName, language, nocache, squash, shrinkwrap)
}

return nil
}

func build(services *stack.Services, queueDepth int) {
func build(services *stack.Services, queueDepth int, shrinkwrap bool) {
wg := sync.WaitGroup{}

workChannel := make(chan stack.Function)
Expand All @@ -115,7 +118,7 @@ func build(services *stack.Services, queueDepth int) {
fmt.Println("Please provide a valid -lang or 'Dockerfile' for your function.")

} else {
builder.BuildImage(function.Image, function.Handler, function.Name, function.Language, nocache, squash)
builder.BuildImage(function.Image, function.Handler, function.Name, function.Language, nocache, squash, shrinkwrap)
}
}

Expand All @@ -132,6 +135,7 @@ func build(services *stack.Services, queueDepth int) {
workChannel <- function
}
}

close(workChannel)

wg.Wait()
Expand Down

0 comments on commit 6863e2d

Please sign in to comment.