mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-07-20 17:49:10 +00:00
allow setting cache dir via env var
Signed-off-by: Avi Deitcher <avi@deitcher.net>
This commit is contained in:
parent
e532e73108
commit
cec33f0c84
@ -51,9 +51,10 @@ func build(args []string) {
|
|||||||
buildPull := buildCmd.Bool("pull", false, "Always pull images")
|
buildPull := buildCmd.Bool("pull", false, "Always pull images")
|
||||||
buildDocker := buildCmd.Bool("docker", false, "Check for images in docker before linuxkit cache")
|
buildDocker := buildCmd.Bool("docker", false, "Check for images in docker before linuxkit cache")
|
||||||
buildDecompressKernel := buildCmd.Bool("decompress-kernel", false, "Decompress the Linux kernel (default false)")
|
buildDecompressKernel := buildCmd.Bool("decompress-kernel", false, "Decompress the Linux kernel (default false)")
|
||||||
buildCacheDir := buildCmd.String("cache", defaultLinuxkitCache(), "Directory for caching and finding cached image")
|
|
||||||
buildCmd.Var(&buildFormats, "format", "Formats to create [ "+strings.Join(outputTypes, " ")+" ]")
|
buildCmd.Var(&buildFormats, "format", "Formats to create [ "+strings.Join(outputTypes, " ")+" ]")
|
||||||
buildArch := buildCmd.String("arch", runtime.GOARCH, "target architecture for which to build")
|
buildArch := buildCmd.String("arch", runtime.GOARCH, "target architecture for which to build")
|
||||||
|
cacheDir := flagOverEnvVarOverDefaultString{def: defaultLinuxkitCache(), envVar: envVarCacheDir}
|
||||||
|
buildCmd.Var(&cacheDir, "cache", fmt.Sprintf("Directory for caching and finding cached image, overrides env var %s", envVarCacheDir))
|
||||||
|
|
||||||
if err := buildCmd.Parse(args); err != nil {
|
if err := buildCmd.Parse(args); err != nil {
|
||||||
log.Fatal("Unable to parse args")
|
log.Fatal("Unable to parse args")
|
||||||
@ -98,8 +99,6 @@ func build(args []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheDir := *buildCacheDir
|
|
||||||
|
|
||||||
if len(buildFormats) == 1 && moby.Streamable(buildFormats[0]) {
|
if len(buildFormats) == 1 && moby.Streamable(buildFormats[0]) {
|
||||||
if *buildOutputFile == "" {
|
if *buildOutputFile == "" {
|
||||||
*buildOutputFile = filepath.Join(*buildDir, name+"."+buildFormats[0])
|
*buildOutputFile = filepath.Join(*buildDir, name+"."+buildFormats[0])
|
||||||
@ -108,7 +107,7 @@ func build(args []string) {
|
|||||||
*buildDir = ""
|
*buildDir = ""
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
err := moby.ValidateFormats(buildFormats, cacheDir)
|
err := moby.ValidateFormats(buildFormats, cacheDir.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error parsing formats: %v", err)
|
log.Errorf("Error parsing formats: %v", err)
|
||||||
buildCmd.Usage()
|
buildCmd.Usage()
|
||||||
@ -204,7 +203,7 @@ func build(args []string) {
|
|||||||
if moby.Streamable(buildFormats[0]) {
|
if moby.Streamable(buildFormats[0]) {
|
||||||
tp = buildFormats[0]
|
tp = buildFormats[0]
|
||||||
}
|
}
|
||||||
err = moby.Build(m, w, moby.BuildOpts{Pull: *buildPull, BuilderType: tp, DecompressKernel: *buildDecompressKernel, CacheDir: cacheDir, DockerCache: *buildDocker, Arch: *buildArch})
|
err = moby.Build(m, w, moby.BuildOpts{Pull: *buildPull, BuilderType: tp, DecompressKernel: *buildDecompressKernel, CacheDir: cacheDir.String(), DockerCache: *buildDocker, Arch: *buildArch})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("%v", err)
|
log.Fatalf("%v", err)
|
||||||
}
|
}
|
||||||
@ -216,7 +215,7 @@ func build(args []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("Create outputs:")
|
log.Infof("Create outputs:")
|
||||||
err = moby.Formats(filepath.Join(*buildDir, name), image, buildFormats, size, cacheDir)
|
err = moby.Formats(filepath.Join(*buildDir, name), image, buildFormats, size, cacheDir.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error writing outputs: %v", err)
|
log.Fatalf("Error writing outputs: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
@ -10,14 +11,15 @@ import (
|
|||||||
func cacheClean(args []string) {
|
func cacheClean(args []string) {
|
||||||
flags := flag.NewFlagSet("clean", flag.ExitOnError)
|
flags := flag.NewFlagSet("clean", flag.ExitOnError)
|
||||||
|
|
||||||
cacheDir := flags.String("cache", defaultLinuxkitCache(), "Directory for caching and finding cached image")
|
cacheDir := flagOverEnvVarOverDefaultString{def: defaultLinuxkitCache(), envVar: envVarCacheDir}
|
||||||
|
flags.Var(&cacheDir, "cache", fmt.Sprintf("Directory for caching and finding cached image, overrides env var %s", envVarCacheDir))
|
||||||
|
|
||||||
if err := flags.Parse(args); err != nil {
|
if err := flags.Parse(args); err != nil {
|
||||||
log.Fatal("Unable to parse args")
|
log.Fatal("Unable to parse args")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.RemoveAll(*cacheDir); err != nil {
|
if err := os.RemoveAll(cacheDir.String()); err != nil {
|
||||||
log.Fatalf("Unable to clean cache %s: %v", *cacheDir, err)
|
log.Fatalf("Unable to clean cache %s: %v", cacheDir, err)
|
||||||
}
|
}
|
||||||
log.Infof("Cache cleaned: %s", *cacheDir)
|
log.Infof("Cache cleaned: %s", cacheDir)
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -15,7 +16,8 @@ import (
|
|||||||
func cacheExport(args []string) {
|
func cacheExport(args []string) {
|
||||||
fs := flag.NewFlagSet("export", flag.ExitOnError)
|
fs := flag.NewFlagSet("export", flag.ExitOnError)
|
||||||
|
|
||||||
cacheDir := fs.String("cache", defaultLinuxkitCache(), "Directory for caching and finding cached image")
|
cacheDir := flagOverEnvVarOverDefaultString{def: defaultLinuxkitCache(), envVar: envVarCacheDir}
|
||||||
|
fs.Var(&cacheDir, "cache", fmt.Sprintf("Directory for caching and finding cached image, overrides env var %s", envVarCacheDir))
|
||||||
arch := fs.String("arch", runtime.GOARCH, "Architecture to resolve an index to an image, if the provided image name is an index")
|
arch := fs.String("arch", runtime.GOARCH, "Architecture to resolve an index to an image, if the provided image name is an index")
|
||||||
outfile := fs.String("outfile", "", "Path to file to save output, '-' for stdout")
|
outfile := fs.String("outfile", "", "Path to file to save output, '-' for stdout")
|
||||||
format := fs.String("format", "oci", "export format, one of 'oci', 'filesystem'")
|
format := fs.String("format", "oci", "export format, one of 'oci', 'filesystem'")
|
||||||
@ -33,7 +35,7 @@ func cacheExport(args []string) {
|
|||||||
name := names[0]
|
name := names[0]
|
||||||
fullname := util.ReferenceExpand(name)
|
fullname := util.ReferenceExpand(name)
|
||||||
|
|
||||||
p, err := cachepkg.NewProvider(*cacheDir)
|
p, err := cachepkg.NewProvider(cacheDir.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("unable to read a local cache: %v", err)
|
log.Fatalf("unable to read a local cache: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
cachepkg "github.com/linuxkit/linuxkit/src/cmd/linuxkit/cache"
|
cachepkg "github.com/linuxkit/linuxkit/src/cmd/linuxkit/cache"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
@ -10,14 +11,15 @@ import (
|
|||||||
func cacheList(args []string) {
|
func cacheList(args []string) {
|
||||||
flags := flag.NewFlagSet("list", flag.ExitOnError)
|
flags := flag.NewFlagSet("list", flag.ExitOnError)
|
||||||
|
|
||||||
cacheDir := flags.String("cache", defaultLinuxkitCache(), "Directory for caching and finding cached image")
|
cacheDir := flagOverEnvVarOverDefaultString{def: defaultLinuxkitCache(), envVar: envVarCacheDir}
|
||||||
|
flags.Var(&cacheDir, "cache", fmt.Sprintf("Directory for caching and finding cached image, overrides env var %s", envVarCacheDir))
|
||||||
|
|
||||||
if err := flags.Parse(args); err != nil {
|
if err := flags.Parse(args); err != nil {
|
||||||
log.Fatal("Unable to parse args")
|
log.Fatal("Unable to parse args")
|
||||||
}
|
}
|
||||||
|
|
||||||
// list all of the images and content in the cache
|
// list all of the images and content in the cache
|
||||||
p, err := cachepkg.Get(*cacheDir)
|
p, err := cachepkg.Get(cacheDir.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("unable to read a local cache: %v", err)
|
log.Fatalf("unable to read a local cache: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
buildersEnvVar = "LINUXKIT_BUILDERS"
|
buildersEnvVar = "LINUXKIT_BUILDERS"
|
||||||
|
envVarCacheDir = "LINUXKIT_CACHE"
|
||||||
// this is the most recent manifest pointed to by moby/buildkit:master as of 2022-07-22, so it includes
|
// this is the most recent manifest pointed to by moby/buildkit:master as of 2022-07-22, so it includes
|
||||||
// our required commit. Once there is a normal semver tag later than this, we should switch to it.
|
// our required commit. Once there is a normal semver tag later than this, we should switch to it.
|
||||||
defaultBuilderImage = "moby/buildkit@sha256:19c4637f8809f21af01dedf65f7f0d64636165d8191381ec9d5150fccedbae48"
|
defaultBuilderImage = "moby/buildkit@sha256:19c4637f8809f21af01dedf65f7f0d64636165d8191381ec9d5150fccedbae48"
|
||||||
@ -44,7 +45,8 @@ func pkgBuildPush(args []string, withPush bool) {
|
|||||||
builders := flags.String("builders", "", "Which builders to use for which platforms, e.g. linux/arm64=docker-context-arm64, overrides defaults and environment variables, see https://github.com/linuxkit/linuxkit/blob/master/docs/packages.md#Providing-native-builder-nodes")
|
builders := flags.String("builders", "", "Which builders to use for which platforms, e.g. linux/arm64=docker-context-arm64, overrides defaults and environment variables, see https://github.com/linuxkit/linuxkit/blob/master/docs/packages.md#Providing-native-builder-nodes")
|
||||||
builderImage := flags.String("builder-image", defaultBuilderImage, "buildkit builder container image to use")
|
builderImage := flags.String("builder-image", defaultBuilderImage, "buildkit builder container image to use")
|
||||||
builderRestart := flags.Bool("builder-restart", false, "force restarting builder, even if container with correct name and image exists")
|
builderRestart := flags.Bool("builder-restart", false, "force restarting builder, even if container with correct name and image exists")
|
||||||
buildCacheDir := flags.String("cache", defaultLinuxkitCache(), "Directory for storing built image, incompatible with --docker")
|
cacheDir := flagOverEnvVarOverDefaultString{def: defaultLinuxkitCache(), envVar: envVarCacheDir}
|
||||||
|
flags.Var(&cacheDir, "cache", fmt.Sprintf("Directory for caching and finding cached image, overrides env var %s", envVarCacheDir))
|
||||||
|
|
||||||
// some logic clarification:
|
// some logic clarification:
|
||||||
// pkg build - always builds unless is in cache
|
// pkg build - always builds unless is in cache
|
||||||
@ -84,7 +86,8 @@ func pkgBuildPush(args []string, withPush bool) {
|
|||||||
if *ignoreCache {
|
if *ignoreCache {
|
||||||
opts = append(opts, pkglib.WithBuildIgnoreCache())
|
opts = append(opts, pkglib.WithBuildIgnoreCache())
|
||||||
}
|
}
|
||||||
opts = append(opts, pkglib.WithBuildCacheDir(*buildCacheDir))
|
|
||||||
|
opts = append(opts, pkglib.WithBuildCacheDir(cacheDir.String()))
|
||||||
|
|
||||||
if withPush {
|
if withPush {
|
||||||
opts = append(opts, pkglib.WithBuildPush())
|
opts = append(opts, pkglib.WithBuildPush())
|
||||||
|
@ -114,6 +114,31 @@ func stringToIntArray(l string, sep string) ([]int, error) {
|
|||||||
return i, nil
|
return i, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handle string with built-in default, overridden by env var, overridden by CLI flag
|
||||||
|
type flagOverEnvVarOverDefaultString struct {
|
||||||
|
value string
|
||||||
|
def string
|
||||||
|
envVar string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *flagOverEnvVarOverDefaultString) String() string {
|
||||||
|
val := f.def
|
||||||
|
if f.envVar != "" {
|
||||||
|
if e := os.Getenv(f.envVar); e != "" {
|
||||||
|
val = e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if f.value != "" {
|
||||||
|
val = f.value
|
||||||
|
}
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *flagOverEnvVarOverDefaultString) Set(value string) error {
|
||||||
|
f.value = value
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Convert a multi-line string into an array of strings
|
// Convert a multi-line string into an array of strings
|
||||||
func splitLines(in string) []string {
|
func splitLines(in string) []string {
|
||||||
res := []string{}
|
res := []string{}
|
||||||
|
Loading…
Reference in New Issue
Block a user