Skip to content

Commit

Permalink
Merge pull request #2254 from will4j/feature/os-iso-checksum
Browse files Browse the repository at this point in the history
feature/support checksum of downloaded repository iso file
  • Loading branch information
ks-ci-bot committed May 29, 2024
2 parents 2cd49db + 6301f9b commit 0ff0715
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/kk/apis/kubekey/v1alpha2/manifest_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
type Iso struct {
LocalPath string `yaml:"localPath" json:"localPath"`
Url string `yaml:"url" json:"url"`
Checksum string `yaml:"checksum" json:"checksum"`
}

type Repository struct {
Expand Down
12 changes: 12 additions & 0 deletions cmd/kk/pkg/artifact/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/connector"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/logger"
coreutil "github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/util"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/files"
)

type DownloadISOFile struct {
Expand All @@ -44,6 +45,17 @@ func (d *DownloadISOFile) Execute(runtime connector.Runtime) error {

fileName := fmt.Sprintf("%s-%s-%s.iso", sys.Id, sys.Version, sys.Arch)
filePath := filepath.Join(runtime.GetWorkDir(), fileName)

checksumEqual, err := files.SHA256CheckEqual(filePath, sys.Repository.Iso.Checksum)
if err != nil {
return err
}
if checksumEqual {
logger.Log.Infof("Skip download exists iso file %s", fileName)
d.Manifest.Spec.OperatingSystems[i].Repository.Iso.LocalPath = filePath
continue
}

getCmd := d.Manifest.Arg.DownloadCommand(filePath, sys.Repository.Iso.Url)

cmd := exec.Command("/bin/sh", "-c", getCmd)
Expand Down
17 changes: 17 additions & 0 deletions cmd/kk/pkg/files/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,23 @@ func (b *KubeBinary) SHA256Check() error {
return nil
}

func SHA256CheckEqual(filePath string, checksum string) (bool, error) {
if strings.TrimSpace(checksum) == "" {
return false, nil
}

if !util.IsExist(filePath) {
return false, nil
}

output, err := sha256sum(filePath)
if err != nil {
return false, errors.Wrap(err, fmt.Sprintf("Failed to check SHA256 of %s", filePath))
}

return output == checksum, nil
}

func sha256sum(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
Expand Down

0 comments on commit 0ff0715

Please sign in to comment.