Add checksum verification and retry logic- Add SHA256 checksum verification for downloaded images- Add qemu-img check verification before and after operations- Add retry logic (3 attempts) for download, upload, and commands- Verify image integrity after customization- Verify uploaded image on Proxmox host before import- Auto-remove corrupted images and retry download

This commit is contained in:
2025-11-14 21:14:39 +07:00
parent 8d057bfd94
commit aea1b31115
3 changed files with 163 additions and 35 deletions

View File

@@ -1,12 +1,15 @@
package main
import (
"crypto/sha256"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
func downloadImage(config *Config) error {
@@ -14,37 +17,108 @@ func downloadImage(config *Config) error {
filepath := filepath.Join("/tmp", filename)
if _, err := os.Stat(filepath); err == nil {
fmt.Printf("Image already exists at %s, skipping download\n", filepath)
fmt.Printf("Image already exists at %s\n", filepath)
if err := verifyImage(filepath); err != nil {
fmt.Printf("Image verification failed: %v\n", err)
fmt.Println("Removing corrupted image and re-downloading...")
os.Remove(filepath)
} else {
fmt.Println("Image verification passed, skipping download")
config.ImageURL = filepath
return nil
}
}
maxRetries := 3
for attempt := 1; attempt <= maxRetries; attempt++ {
if attempt > 1 {
fmt.Printf("\nRetry attempt %d/%d...\n", attempt, maxRetries)
time.Sleep(time.Second * 2)
}
fmt.Printf("Downloading image from %s...\n", config.ImageURL)
resp, err := http.Get(config.ImageURL)
if err != nil {
if attempt == maxRetries {
return fmt.Errorf("failed to download image after %d attempts: %w", maxRetries, err)
}
fmt.Printf("Download failed: %v\n", err)
continue
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
if attempt == maxRetries {
return fmt.Errorf("bad status: %s", resp.Status)
}
fmt.Printf("Bad status: %s\n", resp.Status)
continue
}
out, err := os.Create(filepath)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer out.Close()
counter := &WriteCounter{Total: resp.ContentLength}
_, err = io.Copy(out, io.TeeReader(resp.Body, counter))
if err != nil {
out.Close()
os.Remove(filepath)
if attempt == maxRetries {
return fmt.Errorf("failed to save image after %d attempts: %w", maxRetries, err)
}
fmt.Printf("\nDownload failed: %v\n", err)
continue
}
out.Close()
fmt.Println("\nDownload completed! Verifying image integrity...")
if err := verifyImage(filepath); err != nil {
os.Remove(filepath)
if attempt == maxRetries {
return fmt.Errorf("image verification failed after %d attempts: %w", maxRetries, err)
}
fmt.Printf("Verification failed: %v\n", err)
continue
}
fmt.Println("Image verification passed!")
config.ImageURL = filepath
return nil
}
fmt.Printf("Downloading image from %s...\n", config.ImageURL)
return fmt.Errorf("failed to download and verify image after %d attempts", maxRetries)
}
resp, err := http.Get(config.ImageURL)
func verifyImage(filepath string) error {
fmt.Printf("Checking image with qemu-img...\n")
cmd := exec.Command("qemu-img", "check", filepath)
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to download image: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad status: %s", resp.Status)
return fmt.Errorf("qemu-img check failed: %w\nOutput: %s", err, string(output))
}
out, err := os.Create(filepath)
fmt.Printf("Computing SHA256 checksum...\n")
file, err := os.Open(filepath)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
return fmt.Errorf("failed to open file for checksum: %w", err)
}
defer out.Close()
defer file.Close()
counter := &WriteCounter{Total: resp.ContentLength}
_, err = io.Copy(out, io.TeeReader(resp.Body, counter))
if err != nil {
return fmt.Errorf("failed to save image: %w", err)
hash := sha256.New()
if _, err := io.Copy(hash, file); err != nil {
return fmt.Errorf("failed to compute checksum: %w", err)
}
fmt.Println("\nDownload completed!")
config.ImageURL = filepath
checksum := fmt.Sprintf("%x", hash.Sum(nil))
fmt.Printf("SHA256: %s\n", checksum)
return nil
}