Skip to content

Commit

Permalink
Update comment/fix typo
Browse files Browse the repository at this point in the history
set default env vars

support VERSION build arg

Signed-off-by: Qiang Li <[email protected]>
  • Loading branch information
qiangli committed Jul 11, 2024
1 parent 1d3047e commit 7076d4b
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 31 deletions.
53 changes: 41 additions & 12 deletions cli/command/app/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"time"

"github.com/docker/cli/cli"
Expand Down Expand Up @@ -109,17 +110,18 @@ func addInstallFlags(flags *pflag.FlagSet, dest string, trust bool) *AppOptions
// `docker cp` flags
include(flags, eflags, []string{"archive", "follow-link"})

// install specific build args
addBuildArgs(flags)

return options
}

func addBuildArgs(flags *pflag.FlagSet) {
flag := flags.Lookup("build-arg")
if flag != nil {
flag.Value.Set("HOSTOS=" + runtime.GOOS)
flag.Value.Set("HOSTARCH=" + runtime.GOARCH)
func setBuildArgs(options *AppOptions) {
bopts := options.buildOpts
if bopts != nil {
bopts.SetBuildArg("HOSTOS=" + runtime.GOOS)
bopts.SetBuildArg("HOSTARCH=" + runtime.GOARCH)
}
version := options.buildVersion()
if version != "" {
bopts.SetBuildArg("VERSION=" + options.buildVersion())
}
}

Expand All @@ -146,8 +148,22 @@ func installApp(ctx context.Context, adapter cliAdapter, flags *pflag.FlagSet, o
return nil
}

func setDefaultEnv() {
if os.Getenv("DOCKER_BUILDKIT") == "" {
os.Setenv("DOCKER_BUILDKIT", "1")
}

platform := fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
if os.Getenv("DOCKER_DEFAULT_PLATFORM") == "" {
os.Setenv("DOCKER_DEFAULT_PLATFORM", platform)
}
}

// runInstall calls the build, run, and cp commands
func runInstall(ctx context.Context, dockerCli cliAdapter, flags *pflag.FlagSet, options *AppOptions) (string, error) {
setDefaultEnv()
setBuildArgs(options)

iid, err := buildImage(ctx, dockerCli, options)
if err != nil {
return "", err
Expand Down Expand Up @@ -272,8 +288,8 @@ func runPostInstall(dockerCli cliAdapter, dir string, options *AppOptions) (stri

// installOne creates a symlink to the only file in appPath
func installOne(egress, runPath, binPath, appPath string) (string, error) {
appName := filepath.Base(runPath)
if err := validateName(appName); err != nil {
appName, err := makeAppName(runPath)
if err != nil {
return "", err
}
link := filepath.Join(binPath, appName)
Expand All @@ -284,10 +300,11 @@ func installOne(egress, runPath, binPath, appPath string) (string, error) {
// installRunFile creates a symlink to the run file in appPath
// use the base name as the app name
func installRunFile(egress, runPath, binPath, appPath string) (string, error) {
appName := filepath.Base(appPath)
if err := validateName(appName); err != nil {
appName, err := makeAppName(appPath)
if err != nil {
return "", err
}

link := filepath.Join(binPath, appName)
target := filepath.Join(appPath, filepath.Base(runPath))
return install(link, target, egress, binPath, appPath)
Expand Down Expand Up @@ -367,3 +384,15 @@ func installCustom(dir string, appPath string, options *AppOptions) error {

return launch(installer, options)
}

// makeAppName derives the app name from the base name of the path
// after removing the version and extension
func makeAppName(path string) (string, error) {
n := filepath.Base(path)
n = strings.SplitN(n, "@", 2)[0]
n = strings.SplitN(n, ".", 2)[0]
if err := validateName(n); err != nil {
return "", err
}
return n, nil
}
5 changes: 5 additions & 0 deletions cli/command/app/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ func launch(app string, options *AppOptions) error {
}

envs["DOCKER_APP_BASE"] = options._appBase
appPath, err := options.appPath()
if err != nil {
return err
}
envs["DOCKER_APP_PATH"] = appPath

return spawn(app, options.launchArgs(), envs, options.detach)
}
44 changes: 33 additions & 11 deletions cli/command/app/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const installerName = "install"
// uninstallerName is the executable name for custom installation
const uninstallerName = "uninstall"

// namePattern is for validating egress and app name
// namePattern is for validating app name
const namePattern = "^[a-zA-Z0-9][a-zA-Z0-9_.+-]+$"

var nameRegexp = regexp.MustCompile(namePattern)
Expand All @@ -35,8 +35,29 @@ func validateName(s string) error {
return nil
}

// semverPattern is for splitting semver from a context path/URL
const semverPattern = `@v?\d+(\.\d+)?(\.\d+)?$`

var semverRegexp = regexp.MustCompile(semverPattern)

func splitSemver(s string) (string, string) {
if semverRegexp.MatchString(s) {
idx := strings.LastIndex(s, "@")
// unlikely otherwise ignore
if idx == -1 {
return s, ""
}
v := s[idx+1:]
if v[0] == 'v' {
v = v[1:]
}
return s[:idx], v
}
return s, ""
}

// defaultAppBase is docker app's base location specified by
// DOCKER_APP_BASE environment variable defaulted to ~/.docker-app/
// DOCKER_APP_BASE environment variable defaulted to ~/.docker/app/
func defaultAppBase() string {
if base := os.Getenv("DOCKER_APP_BASE"); base != "" {
return filepath.Clean(base)
Expand Down Expand Up @@ -100,7 +121,16 @@ func (o *AppOptions) buildContext() string {
if len(o._args) == 0 {
return "."
}
return o._args[0]
c, _ := splitSemver(o._args[0])
return c
}

func (o *AppOptions) buildVersion() string {
if len(o._args) == 0 {
return ""
}
_, v := splitSemver(o._args[0])
return v
}

// runArgs returns the command line args for running the container
Expand Down Expand Up @@ -182,10 +212,6 @@ func validateAppOptions(options *AppOptions) error {
return errors.New("egress is required")
}

name := filepath.Base(options.egress)
if err := validateName(name); err != nil {
return fmt.Errorf("invalid egress path: %s %v", options.egress, err)
}
return nil
}

Expand Down Expand Up @@ -226,9 +252,5 @@ func makeAppPath(appBase, s string) (string, error) {
return "", fmt.Errorf("missing path: %v", u)
}

name := filepath.Base(u.Path)
if err := validateName(name); err != nil {
return "", fmt.Errorf("invalid path: %s %v", u.Path, err)
}
return filepath.Join(appBase, "pkg", u.Scheme, u.Host, u.Path), nil
}
3 changes: 1 addition & 2 deletions cli/command/app/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (
"syscall"
)

// spawn runs the specified command and returns the PID
// of the spawned process
// spawn runs the specified command
func spawn(bin string, args []string, envMap map[string]string, detach bool) error {
toEnv := func() []string {
var env []string
Expand Down
4 changes: 4 additions & 0 deletions cli/command/image/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ func (o *BuildOptions) SetImageIDFile(imageIDFile string) {
o.imageIDFile = imageIDFile
}

func (o *BuildOptions) SetBuildArg(value string) {
o.buildArgs.Set(value)
}

func newBuildOptions() BuildOptions {
ulimits := make(map[string]*container.Ulimit)
return BuildOptions{
Expand Down
8 changes: 4 additions & 4 deletions e2e/app/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestInstallOne(t *testing.T) {
fs.WithDir(buildCtx,
fs.WithFile("Dockerfile", fmt.Sprintf(`
FROM %s
ARG HOSTOS HOSTARCH
ARG HOSTOS HOSTARCH VERSION
COPY cool /egress/%s
CMD ["echo", "'cool' app successfully built!"]
`, fixtures.AlpineImage, coolApp)),
Expand Down Expand Up @@ -74,7 +74,7 @@ func TestInstallMulti(t *testing.T) {
fs.WithDir(buildCtx,
fs.WithFile("Dockerfile", fmt.Sprintf(`
FROM %s
ARG HOSTOS HOSTARCH
ARG HOSTOS HOSTARCH VERSION
COPY . /egress
CMD ["echo", "'multi' app successfully built!"]
`, fixtures.AlpineImage)),
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestInstallCustom(t *testing.T) {
fs.WithDir(buildCtx,
fs.WithFile("Dockerfile", fmt.Sprintf(`
FROM %s
ARG HOSTOS HOSTARCH
ARG HOSTOS HOSTARCH VERSION
COPY . /egress
CMD ["echo", "'custom' app successfully built!"]
`, fixtures.AlpineImage)),
Expand Down Expand Up @@ -208,7 +208,7 @@ func TestInstallCustomDestination(t *testing.T) {
fs.WithDir(buildCtx,
fs.WithFile("Dockerfile", fmt.Sprintf(`
FROM %s
ARG HOSTOS HOSTARCH
ARG HOSTOS HOSTARCH VERSION
COPY . %s
CMD ["echo", "'service' successfully built!"]
`, fixtures.AlpineImage, egress)),
Expand Down
4 changes: 2 additions & 2 deletions e2e/app/launch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestLaunchOne(t *testing.T) {
fs.WithDir(buildCtx,
fs.WithFile("Dockerfile", fmt.Sprintf(`
FROM %s
ARG HOSTOS HOSTARCH
ARG HOSTOS HOSTARCH VERSION
COPY cool /egress/%s
CMD ["echo", "'cool' app successfully built!"]
`, fixtures.AlpineImage, coolApp)),
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestLaunchMulti(t *testing.T) {
fs.WithDir(buildCtx,
fs.WithFile("Dockerfile", fmt.Sprintf(`
FROM %s
ARG HOSTOS HOSTARCH
ARG HOSTOS HOSTARCH VERSION
COPY . /egress
CMD ["echo", "'multi' app successfully built!"]
`, fixtures.AlpineImage)),
Expand Down

0 comments on commit 7076d4b

Please sign in to comment.