Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 4 additions & 27 deletions .github/workflows/pr-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1056,33 +1056,10 @@ jobs:
shell: pwsh
timeout-minutes: 5
run: |
$msiPath = "$env:RUNNER_TEMP\podman.msi"
if ("${{ steps.podman-cache-windows.outputs.cache-hit }}" -ne "true") {
$msiUrl = "https://github.com/containers/podman/releases/download/v5.8.2/podman-installer-windows-amd64.msi"
Invoke-WebRequest -Uri $msiUrl -OutFile $msiPath
}

$expectedHash = "eda54f26f9695d198d9a679fa45ae24ba35b78444f432b5fe0c122c5a3624c57"
$actualHash = (Get-FileHash -Path $msiPath -Algorithm SHA256).Hash.ToLower()
if ($actualHash -ne $expectedHash) {
throw "SHA256 mismatch for podman MSI! Expected: $expectedHash, Got: $actualHash"
}
Write-Host "Podman MSI checksum verified: $actualHash"

$installDir = "C:\Program Files\RedHat\Podman"
$logFile = "$env:RUNNER_TEMP\podman-install.log"
$proc = Start-Process msiexec.exe -Wait -PassThru -ArgumentList "/i `"$msiPath`" /qn /norestart /l*v `"$logFile`" INSTALLDIR=`"$installDir`""
Write-Host "MSI exit code: $($proc.ExitCode)"

if (!(Test-Path "$installDir\podman.exe")) {
Write-Host "--- MSI install log (last 50 lines) ---"
Get-Content $logFile -Tail 50
Write-Host "--- Searching for podman.exe ---"
Get-ChildItem "C:\" -Filter "podman.exe" -Recurse -Depth 4 -ErrorAction SilentlyContinue | ForEach-Object { Write-Host $_.FullName }
throw "podman.exe not found at $installDir"
}

echo "$installDir" | Out-File -Append -FilePath $env:GITHUB_PATH -Encoding utf8
& "$env:RUNNER_TEMP\devsy-ci-podman\devsy-ci-podman.exe" install `
--url "https://github.com/containers/podman/releases/download/v5.8.2/podman-installer-windows-amd64.msi" `
--sha256 "eda54f26f9695d198d9a679fa45ae24ba35b78444f432b5fe0c122c5a3624c57" --msi-path "$env:RUNNER_TEMP\podman.msi" `
--install-dir "C:\Program Files\RedHat\Podman" --log-path "$env:RUNNER_TEMP\podman-install.log"

- name: start podman (Windows)
if: (matrix.install-kind == true || matrix.install-podman == true) && runner.os == 'Windows' && (matrix.requires-secret == false || needs.can-read-secret.outputs.secret-set == 'true')
Expand Down
2 changes: 1 addition & 1 deletion hack/podman_ci/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func usageError(format string, args ...any) error {
return &ExitError{Code: 2, Err: fmt.Errorf(format, args...)}
}

func classifySetupError(err error) error {
func classifyOperationalError(err error) error {
if err == nil {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions hack/podman_ci/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ func TestExitCodeForError(t *testing.T) {
want int
}{
{name: "cobra usage error", err: errors.New("required flag not set"), want: 2},
{name: "typed usage error", err: classifySetupError(usageError("invalid mode")), want: 2},
{name: "typed usage error", err: classifyOperationalError(usageError("invalid mode")), want: 2},
{
name: "operational error",
err: classifySetupError(errors.New("setup failed")),
err: classifyOperationalError(errors.New("setup failed")),
want: 1,
},
} {
Expand Down
206 changes: 206 additions & 0 deletions hack/podman_ci/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
package main

import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
"strings"
"time"

"github.com/spf13/cobra"
)

const (
msiexecPath = "msiexec.exe"
podmanExe = "podman.exe"
msiInstallTimeout = 4 * time.Minute
commandInstall = "install"
msiLogTailLines = 50
podmanSearchDepth = 4
)

type Downloader func(ctx context.Context, url, dest string) error

type InstallConfig struct {
URL string
SHA256 string
MSIPath string
InstallDir string
LogPath string
GitHubPath string
}

func newInstallCommand() *cobra.Command {
var cfg InstallConfig
cmd := &cobra.Command{
Use: commandInstall,
Short: "Install Podman from a verified MSI",
RunE: func(*cobra.Command, []string) error {
logger := newLogger()
deps := Dependencies{
Runner: &ExecRunner{Log: logger},
Log: logger,
Files: osFileSystem{},
Download: downloadFile,
}
return classifyOperationalError(platformInstall(context.Background(), deps, cfg))
},
}
cmd.Flags().StringVar(&cfg.URL, "url", "", "podman MSI download URL")
cmd.Flags().StringVar(&cfg.SHA256, "sha256", "", "expected SHA256 of the podman MSI")
cmd.Flags().StringVar(&cfg.MSIPath, "msi-path", "", "local path for the podman MSI")
cmd.Flags().StringVar(&cfg.InstallDir, "install-dir", "", "msiexec INSTALLDIR")
cmd.Flags().StringVar(&cfg.LogPath, "log-path", "", "msiexec verbose log path")
cmd.Flags().
StringVar(&cfg.GitHubPath, "github-path", os.Getenv("GITHUB_PATH"), "file to append the install dir to")
for _, name := range []string{"url", "sha256", "msi-path", "install-dir", "log-path"} {
_ = cmd.MarkFlagRequired(name)
}
return cmd
}

func installPodmanMSI(
ctx context.Context,
deps Dependencies,
cfg InstallConfig,
searchRoot string,
) error {
// An MSI restored by actions/cache sits at MSIPath; the checksum below still guards it.
if _, statErr := deps.Files.Stat(cfg.MSIPath); statErr != nil {
if err := deps.Download(ctx, cfg.URL, cfg.MSIPath); err != nil {
return fmt.Errorf("download podman MSI: %w", err)
}
}

msi, err := deps.Files.ReadFile(cfg.MSIPath)
if err != nil {
return fmt.Errorf("read podman MSI: %w", err)
}
if err := verifyMSIChecksum(msi, cfg.SHA256); err != nil {
return err
}
deps.Log.Printf("Podman MSI checksum verified: %s", installSum(msi))

res := deps.Runner.Run(ctx, CommandSpec{
Path: msiexecPath,
CmdLine: msiexecCommandLine(cfg),
Timeout: msiInstallTimeout,
})
deps.Log.Printf("MSI exit code: %s", exitCodeString(res.ExitCode))

podman := filepath.Join(cfg.InstallDir, podmanExe)
if _, err := deps.Files.Stat(podman); err != nil {
logMissingPodman(deps, cfg, searchRoot)
return fmt.Errorf("podman.exe not found at %s", cfg.InstallDir)
}

return appendGitHubPath(deps.Files, cfg.GitHubPath, cfg.InstallDir)
}

func msiexecCommandLine(cfg InstallConfig) string {
return fmt.Sprintf(`%s /i "%s" /qn /norestart /l*v "%s" INSTALLDIR="%s"`,
msiexecPath, cfg.MSIPath, cfg.LogPath, cfg.InstallDir)
}

func installSum(data []byte) string {
sum := sha256.Sum256(data)
return hex.EncodeToString(sum[:])
}

func verifyMSIChecksum(data []byte, expected string) error {
actual := installSum(data)
if !strings.EqualFold(actual, expected) {
return fmt.Errorf("SHA256 mismatch for podman MSI! Expected: %s, Got: %s", expected, actual)
}
return nil
}

func logMissingPodman(deps Dependencies, cfg InstallConfig, searchRoot string) {
deps.Log.Printf("podman.exe not found at %s", cfg.InstallDir)
deps.Log.Printf("MSI install log (last %d lines):", msiLogTailLines)
if data, err := deps.Files.ReadFile(cfg.LogPath); err != nil {
deps.Log.Printf("install log unavailable: %v", err)
} else {
deps.Log.Printf("%s", tailLines(data, msiLogTailLines))
}
found := findFilesNamed(searchRoot, podmanExe, podmanSearchDepth)
deps.Log.Printf("podman.exe found under %s:", searchRoot)
for _, path := range found {
deps.Log.Printf("%s", path)
}
}

func tailLines(data []byte, n int) string {
text := strings.TrimRight(strings.ReplaceAll(string(data), "\r\n", "\n"), "\n")
lines := strings.Split(text, "\n")
lines = lines[max(0, len(lines)-n):]
return strings.Join(lines, "\n")
}

// findFilesNamed mirrors PowerShell Get-ChildItem -Depth: items up to maxDepth+1 levels below root.
func findFilesNamed(root, name string, maxDepth int) []string {
var found []string
rootDepth := strings.Count(filepath.Clean(root), string(filepath.Separator))
_ = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return nil
}
depth := strings.Count(filepath.Clean(path), string(filepath.Separator)) - rootDepth
if d.IsDir() && depth > maxDepth {
return filepath.SkipDir
}
if !d.IsDir() && d.Name() == name {
found = append(found, path)
}
return nil
})
return found
}

func appendGitHubPath(files FileSystem, githubPath, dir string) error {
if githubPath == "" {
return nil
}
w, err := files.OpenAppend(githubPath)
if err != nil {
return fmt.Errorf("open GITHUB_PATH: %w", err)
}
_, writeErr := io.WriteString(w, dir+"\n")
if err := errors.Join(writeErr, w.Close()); err != nil {
return fmt.Errorf("write GITHUB_PATH: %w", err)
}
return nil
}

func downloadFile(ctx context.Context, url, dest string) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("GET %s: %s", url, resp.Status)
}
f, err := os.Create(dest)
if err != nil {
return err
}
_, copyErr := io.Copy(f, resp.Body)
closeErr := f.Close()
if err := errors.Join(copyErr, closeErr); err != nil {
_ = os.Remove(dest)
return err
}
return nil
}
Loading
Loading