Skip to content

Commit

Permalink
FnCLI fn deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaytee-fn committed Aug 9, 2023
1 parent 06e5e0d commit b64346d
Showing 1 changed file with 41 additions and 12 deletions.
53 changes: 41 additions & 12 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"strings"
"time"
"unicode"
Expand All @@ -55,6 +56,7 @@ const (
MinRequiredDockerVersion = "17.5.0"
BuildxBuilderInstance = "oci_fn_builder"
DefaultAppShape = modelsv2.AppShapeGENERICX86
containerEngineTypeDocker = "docker"
)

var GlobalVerbose bool
Expand All @@ -66,6 +68,12 @@ var ShapeMap = map[string][]string{
modelsv2.AppShapeGENERICX86ARM: {"linux/amd64", "linux/arm64"},
}

var TargetPlatformMap = map[string][]string{
modelsv2.AppShapeGENERICX86: {"amd64"},
modelsv2.AppShapeGENERICARM: {"arm64"},
modelsv2.AppShapeGENERICX86ARM: {"amd64_arm64"},
}

func IsVerbose() bool {
return GlobalVerbose || CommandVerbose
}
Expand Down Expand Up @@ -435,7 +443,6 @@ func buildXDockerCommand(imageName, dockerfile string, buildArgs []string, noCac
var label = "imageName=" + imageName
args = append(args, "--build-arg", arg)
args = append(args, "--label", label)
args = append(args, "--push")
}

args = append(args,
Expand Down Expand Up @@ -512,20 +519,30 @@ func RunBuild(verbose bool, dir, imageName, dockerfile string, buildArgs []strin
go func(done chan<- error) {
var dockerBuildCmdArgs []string
// Depending whether architecture list is passed or not trigger docker buildx or docker build accordingly
var mappedArchitectures []string

if arch, ok := ShapeMap[shape]; ok {
var mappedArchitectures []string
mappedArchitectures = append(mappedArchitectures, arch...)
err := initializeContainerBuilder(containerEngineType, mappedArchitectures)
if err != nil {
done <- err
return
var hostedPlatform = runtime.GOARCH
if platform, ok := TargetPlatformMap[shape]; ok {
// create target platform string to compare with hosted platform
targetPlatform := strings.Join(platform," ")
fmt.Println("hosted platform %v target platform %v", hostedPlatform, targetPlatform)
if targetPlatform != hostedPlatform {
fmt.Println("TargetedPlatform and hostPlatform are not same")
err := initializeContainerBuilder(containerEngineType, mappedArchitectures)
if err != nil {
done <- err
return
}
dockerBuildCmdArgs = buildXDockerCommand(imageName, dockerfile, buildArgs, noCache, mappedArchitectures)
// perform cleanup
defer cleanupContainerBuilder(containerEngineType)
} else {
fmt.Println("TargetedPlatform and hostPlatform are same")
dockerBuildCmdArgs = buildDockerCommand(imageName, dockerfile, buildArgs, noCache)
}
}

dockerBuildCmdArgs = buildXDockerCommand(imageName, dockerfile, buildArgs, noCache, mappedArchitectures)
// perform cleanup
defer cleanupContainerBuilder(containerEngineType)
} else {
dockerBuildCmdArgs = buildDockerCommand(imageName, dockerfile, buildArgs, noCache)
}

cmd := exec.Command(containerEngineType, dockerBuildCmdArgs...)
Expand All @@ -551,6 +568,15 @@ func RunBuild(verbose bool, dir, imageName, dockerfile string, buildArgs []strin
fmt.Fprintln(os.Stderr)
return fmt.Errorf("build cancelled on signal %v", signal)
}
// Push to docker registry
fmt.Println("Using Container engine", containerEngineType, "to push")
fmt.Printf("Pushing %v to docker registry...", imageName)
cmd := exec.Command(containerEngineType, "push", imageName)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
return fmt.Errorf("error running %v push, are you logged?: %v", containerEngineType, err)
}
return nil
}

Expand Down Expand Up @@ -620,6 +646,9 @@ func isSupportedByDefaultBuildxPlatforms(containerEngineType string, platforms [

func initializeContainerBuilder(containerEngineType string, platforms []string) error {

if containerEngineType == containerEngineTypeDocker {
return nil
}
if isSupportedByDefaultBuildxPlatforms(containerEngineType, platforms) {
return nil
}
Expand Down

0 comments on commit b64346d

Please sign in to comment.