Skip to content

Commit

Permalink
embed: Allow to embed pre-downloaded binaries
Browse files Browse the repository at this point in the history
This commit adds --cache-dir and --no-download options to crc-embedder
so that `make release` can be used with pre-downloaded
admin-helper/machine-driver-libvirt binaries.
This expands on the changes started in 2c231c3, and will be useful
when making rpm builds.
  • Loading branch information
cfergeau authored and praveenkumar committed Feb 23, 2022
1 parent d1712a9 commit 3943be1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,11 @@ linux-release: clean lint linux-release-binary embed_crc_helpers gen_release_inf

.PHONY: embed_crc_helpers
embed_crc_helpers: $(BUILD_DIR)/linux-amd64/crc $(HOST_BUILD_DIR)/crc-embedder
ifeq ($(CUSTOM_EMBED),false)
$(HOST_BUILD_DIR)/crc-embedder embed --log-level debug --goos=linux $(BUILD_DIR)/linux-amd64/crc
else
$(HOST_BUILD_DIR)/crc-embedder embed --log-level debug --cache-dir=$(EMBED_DOWNLOAD_DIR) --no-download --goos=linux $(BUILD_DIR)/linux-amd64/crc
endif

.PHONY: update-go-version
update-go-version:
Expand Down
42 changes: 33 additions & 9 deletions cmd/crc-embedder/cmd/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"path"
"path/filepath"
"runtime"

"github.com/code-ready/crc/pkg/crc/constants"
Expand All @@ -19,11 +20,15 @@ import (
)

var (
goos string
goos string
cacheDir string
noDownload bool
)

func init() {
embedCmd.Flags().StringVar(&goos, "goos", runtime.GOOS, "Target platform (darwin, linux or windows)")
embedCmd.Flags().StringVar(&cacheDir, "cache-dir", "", "Destination directory for the downloaded files")
embedCmd.Flags().BoolVar(&noDownload, "no-download", false, "Only embed files, don't download")
rootCmd.AddCommand(embedCmd)
}

Expand All @@ -38,17 +43,25 @@ var embedCmd = &cobra.Command{
}

func runEmbed(args []string) {
var err error
executablePath := args[0]
destDir, err := ioutil.TempDir("", "crc-embedder")
if err != nil {
logging.Fatalf("Failed to create temporary directory: %v", err)
if cacheDir == "" {
cacheDir, err = ioutil.TempDir("", "crc-embedder")
if err != nil {
logging.Fatalf("Failed to create temporary directory: %v", err)
}
defer os.RemoveAll(cacheDir)
}
defer os.RemoveAll(destDir)
downloadedFiles, err := downloadDataFiles(goos, destDir)
if err != nil {
logging.Fatalf("Failed to download data files: %v", err)
var embedFileList []string
if noDownload {
embedFileList = getEmbedFileList(goos, cacheDir)
} else {
embedFileList, err = downloadDataFiles(goos, cacheDir)
if err != nil {
logging.Fatalf("Failed to download data files: %v", err)
}
}
err = embedFiles(executablePath, downloadedFiles)
err = embedFiles(executablePath, embedFileList)
if err != nil {
logging.Fatalf("Failed to embed data files: %v", err)
}
Expand Down Expand Up @@ -102,6 +115,17 @@ var (
}
)

func getEmbedFileList(goos string, destDir string) []string {
fileList := []string{}
urls := dataFileUrls[goos]
for _, dlDetails := range urls {
filename := filepath.Base(dlDetails.url)
fileList = append(fileList, filepath.Join(destDir, filename))
}

return fileList
}

func downloadDataFiles(goos string, destDir string) ([]string, error) {
downloadedFiles := []string{}
downloads := dataFileUrls[goos]
Expand Down

0 comments on commit 3943be1

Please sign in to comment.