|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "path" |
| 8 | + "runtime" |
| 9 | + |
| 10 | + "github.com/code-ready/crc/pkg/crc/constants" |
| 11 | + "github.com/code-ready/crc/pkg/crc/logging" |
| 12 | + "github.com/code-ready/crc/pkg/download" |
| 13 | + |
| 14 | + "github.com/code-ready/crc/pkg/crc/machine/hyperkit" |
| 15 | + "github.com/code-ready/crc/pkg/crc/machine/libvirt" |
| 16 | + |
| 17 | + "github.com/YourFin/binappend" |
| 18 | + "github.com/spf13/cobra" |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + bundleDir string |
| 23 | + goos string |
| 24 | +) |
| 25 | + |
| 26 | +func init() { |
| 27 | + embedCmd.Flags().StringVar(&bundleDir, "bundle-dir", constants.MachineCacheDir, "Directory where the OpenShift bundle can be found") |
| 28 | + embedCmd.Flags().StringVar(&goos, "goos", runtime.GOOS, "Target platform (darwin, linux or windows)") |
| 29 | + rootCmd.AddCommand(embedCmd) |
| 30 | +} |
| 31 | + |
| 32 | +var embedCmd = &cobra.Command{ |
| 33 | + Use: "embed", |
| 34 | + Short: "Embed data files in crc binary", |
| 35 | + Long: `Embed the OpenShift bundle and the binaries needed at runtime in the crc binary`, |
| 36 | + Run: func(cmd *cobra.Command, args []string) { |
| 37 | + runEmbed(args) |
| 38 | + }, |
| 39 | +} |
| 40 | + |
| 41 | +func runEmbed(args []string) { |
| 42 | + if len(args) != 1 { |
| 43 | + logging.Fatalf("embed takes exactly one argument") |
| 44 | + } |
| 45 | + binaryPath := args[0] |
| 46 | + destDir, err := ioutil.TempDir("", "crc-embedder") |
| 47 | + if err != nil { |
| 48 | + logging.Errorf(fmt.Sprintf("Failed to create temporary directory: %v", err)) |
| 49 | + } |
| 50 | + defer os.RemoveAll(destDir) |
| 51 | + downloadedFiles, err := downloadDataFiles(goos, destDir) |
| 52 | + if err != nil { |
| 53 | + logging.Errorf(fmt.Sprintf("Failed to download data files: %v", err)) |
| 54 | + } |
| 55 | + |
| 56 | + bundlePath := path.Join(bundleDir, constants.GetDefaultBundleForOs(goos)) |
| 57 | + downloadedFiles = append(downloadedFiles, bundlePath) |
| 58 | + err = embedFiles(binaryPath, downloadedFiles) |
| 59 | + if err != nil { |
| 60 | + logging.Errorf(fmt.Sprintf("Failed to embed data files: %v", err)) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func embedFiles(binary string, filenames []string) error { |
| 65 | + appender, err := binappend.MakeAppender(binary) |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + defer appender.Close() |
| 70 | + for _, filename := range filenames { |
| 71 | + logging.Debugf("Embedding %s in %s", filename, binary) |
| 72 | + f, err := os.Open(filename) // #nosec G304 |
| 73 | + if err != nil { |
| 74 | + return fmt.Errorf("Failed to open %s: %v", filename, err) |
| 75 | + } |
| 76 | + defer f.Close() |
| 77 | + |
| 78 | + err = appender.AppendStreamReader(path.Base(filename), f, false) |
| 79 | + if err != nil { |
| 80 | + return fmt.Errorf("Failed to append %s to %s: %v", filename, binary, err) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return nil |
| 85 | +} |
| 86 | + |
| 87 | +var ( |
| 88 | + dataFileUrls = map[string][]string{ |
| 89 | + "darwin": []string{ |
| 90 | + hyperkit.MachineDriverDownloadUrl, |
| 91 | + hyperkit.HyperkitDownloadUrl, |
| 92 | + constants.GetOcUrlForOs("darwin"), |
| 93 | + }, |
| 94 | + "linux": []string{ |
| 95 | + libvirt.MachineDriverDownloadUrl, |
| 96 | + constants.GetOcUrlForOs("linux"), |
| 97 | + }, |
| 98 | + "windows": []string{ |
| 99 | + constants.GetOcUrlForOs("windows"), |
| 100 | + }, |
| 101 | + } |
| 102 | +) |
| 103 | + |
| 104 | +func downloadDataFiles(goos string, destDir string) ([]string, error) { |
| 105 | + downloadedFiles := []string{} |
| 106 | + downloads := dataFileUrls[goos] |
| 107 | + for _, url := range downloads { |
| 108 | + filename, err := download.Download(url, destDir, 0644) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + downloadedFiles = append(downloadedFiles, filename) |
| 113 | + } |
| 114 | + |
| 115 | + return downloadedFiles, nil |
| 116 | +} |
0 commit comments