utils

package
v1.18.0-beta34 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DockerDownload = func(tmpdir string, params GetBinaryParams) (string, error) {
	goos := runtime.GOOS

	if goos == "darwin" {
		return "", fmt.Errorf("unsupported operating system: %s", goos)
	}

	log.Printf("Using %s from docker image: %s", params.Filename, params.DockerImage)

	bash := fmt.Sprintf(`
set -ex
CID=$(docker run -d  %s /bin/sh -c exit)

# just print the image sha for reproducibility
echo "Using %s Image:"
docker inspect %s -f "{{.RepoDigests}}"

docker cp $CID:%s ./%s
docker rm -f $CID
    `, params.DockerImage, params.Filename, params.DockerImage, params.DockerPath, params.Filename)
	scriptFile := filepath.Join(tmpdir, "get_binary.sh")

	err := os.WriteFile(scriptFile, []byte(bash), 0755)
	if err != nil {
		return "", err
	}

	cmd := exec.Command("bash", scriptFile)
	cmd.Dir = tmpdir
	cmd.Stdout = ginkgo.GinkgoWriter
	cmd.Stderr = ginkgo.GinkgoWriter
	if err := cmd.Run(); err != nil {
		return "", err
	}

	return filepath.Join(tmpdir, params.Filename), nil
}

DockerDownload extracts a binary from a Docker image by running a temporary Docker container, copying the binary from the container's filesystem, and saving it to a local temporary directory. This function is primarily used when a binary is not available via environment variables or on the system's PATH.

tmpdir: The temporary directory where the binary will be saved. params: A struct containing parameters such as the filename, Docker image,

and Docker path to locate the binary in the container.

Returns the path to the saved binary on success or an error if the operation fails.

View Source
var DownloadAndExtractBinary = func(tmpDir, filename, version string) (string, error) {
	goos := runtime.GOOS
	goarch := runtime.GOARCH

	url := fmt.Sprintf("https://releases.hashicorp.com/%s/%s/%s_%s_%s_%s.zip", filename, version, filename, version, goos, goarch)
	log.Printf("Downloading %s binary from: %s", filename, url)

	zipPath := filepath.Join(tmpDir, fmt.Sprintf("%s.zip", filename))
	out, err := os.Create(zipPath)
	if err != nil {
		return "", fmt.Errorf("failed to create temp zip file: %w", err)
	}
	defer out.Close()

	resp, err := http.Get(url)
	if err != nil {
		return "", fmt.Errorf("failed to download %s binary: %w", filename, err)
	}
	defer resp.Body.Close()

	_, err = out.ReadFrom(resp.Body)
	if err != nil {
		return "", fmt.Errorf("failed to download %s binary: %w", filename, err)
	}

	err = Unzip(zipPath, tmpDir)
	if err != nil {
		return "", fmt.Errorf("failed to unzip %s binary: %w", filename, err)
	}

	binaryPath := filepath.Join(tmpDir, filename)
	if err := os.Chmod(binaryPath, 0755); err != nil {
		return "", fmt.Errorf("failed to make %s binary executable: %w", filename, err)
	}

	log.Printf("%s binary extracted to: %s", filename, binaryPath)
	return binaryPath, nil
}

DownloadAndExtractBinary downloads and extracts the Vault or Consul binary for darwin OS

View Source
var ExecLookPathWrapper = exec.LookPath

ExecLookPathWrapper is a wrapper around exec.LookPath so it can be mocked in tests.

Functions

func GetBinary

func GetBinary(params GetBinaryParams) (string, error)

GetBinary checks for a binary in the following order: 1. From the environment variable if specified 2. Locally available on the $PATH 3. If `darwin` is the OS, download the Vault or Consul binary from the HashiCorp release page 4. As a last resort, pull the binary from a Docker image

func Unzip added in v1.18.0

func Unzip(src, dest string) error

Unzip unzips the given archive to the specified destination

Types

type GetBinaryParams

type GetBinaryParams struct {
	Filename    string // the name of the binary on the $PATH or in the docker container
	DockerImage string // the docker image to use if Env or Local are not present
	DockerPath  string // the location of the binary in the docker container, including the filename
	EnvKey      string // the environment var to look at for a user-specified service binary
	TmpDir      string // the temp directory to store a downloaded binary if needed
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL