forked from crc-org/crc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Embed podman (remote) binary in crc binary
Fixes crc-org#961.
- Loading branch information
Showing
7 changed files
with
142 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package constants | ||
|
||
const ( | ||
OcBinaryName = "oc" | ||
OcBinaryName = "oc" | ||
PodmanBinaryName = "podman" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package constants | ||
|
||
const ( | ||
OcBinaryName = "oc.exe" | ||
OcBinaryName = "oc.exe" | ||
PodmanBinaryName = "podman.exe" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package podman | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/code-ready/crc/pkg/crc/constants" | ||
"github.com/code-ready/crc/pkg/crc/logging" | ||
"github.com/code-ready/crc/pkg/download" | ||
"github.com/code-ready/crc/pkg/embed" | ||
"github.com/code-ready/crc/pkg/extract" | ||
crcos "github.com/code-ready/crc/pkg/os" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// Podman is a struct with methods designed for dealing with the podman binary | ||
type PodmanCached struct{} | ||
|
||
func (podman *PodmanCached) EnsureIsCached() error { | ||
if !podman.IsCached() { | ||
err := podman.cachePodman() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
} | ||
return nil | ||
} | ||
|
||
func (podman *PodmanCached) IsCached() bool { | ||
if _, err := os.Stat(filepath.Join(constants.CrcBinDir, constants.PodmanBinaryName)); os.IsNotExist(err) { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func matchPodmanBinaryName(filename string) bool { | ||
return filepath.Base(filename) == constants.PodmanBinaryName | ||
} | ||
|
||
func (podman *PodmanCached) getPodman(destDir string) (string, error) { | ||
logging.Debug("Trying to extract podman from crc binary") | ||
archiveName := filepath.Base(constants.GetPodmanUrl()) | ||
destPath := filepath.Join(destDir, archiveName) | ||
err := embed.Extract(archiveName, destPath) | ||
if err != nil { | ||
logging.Debug("Downloading podman") | ||
return download.Download(constants.GetPodmanUrl(), destDir, 0600) | ||
} | ||
|
||
return destPath, err | ||
} | ||
|
||
// cachePodman downloads and caches the podman binary into the minishift directory | ||
func (podman *PodmanCached) cachePodman() error { | ||
if podman.IsCached() { | ||
return nil | ||
} | ||
|
||
// Create tmp dir to download the podman tarball | ||
tmpDir, err := ioutil.TempDir("", "crc") | ||
if err != nil { | ||
return err | ||
} | ||
defer os.RemoveAll(tmpDir) | ||
assetTmpFile, err := podman.getPodman(tmpDir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Extract the tarball and put it the cache directory. | ||
err = extract.UncompressWithFilter(assetTmpFile, tmpDir, matchPodmanBinaryName) | ||
if err != nil { | ||
return errors.Wrapf(err, "Cannot uncompress '%s'", assetTmpFile) | ||
} | ||
|
||
binaryName := constants.PodmanBinaryName | ||
binaryPath := filepath.Join(tmpDir, binaryName) | ||
|
||
// Copy the requested asset into its final destination | ||
outputPath := constants.CrcBinDir | ||
err = os.MkdirAll(outputPath, 0750) | ||
if err != nil && !os.IsExist(err) { | ||
return errors.Wrap(err, "Cannot create the target directory.") | ||
} | ||
|
||
finalBinaryPath := filepath.Join(outputPath, binaryName) | ||
err = crcos.CopyFileContents(binaryPath, finalBinaryPath, 0500) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters