github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/infrastructure/tasks/download.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package tasks 21 22 import ( 23 "fmt" 24 "os" 25 "os/exec" 26 27 "github.com/kubesphere/kubekey/v3/cmd/kk/pkg/common" 28 "github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/logger" 29 "github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/util" 30 "github.com/kubesphere/kubekey/v3/cmd/kk/pkg/files" 31 32 "github.com/1aal/kubeblocks/pkg/cli/cmd/infrastructure/types" 33 kbutils "github.com/1aal/kubeblocks/pkg/cli/cmd/infrastructure/utils" 34 cfgcore "github.com/1aal/kubeblocks/pkg/configuration/core" 35 ) 36 37 const ( 38 CurlDownloadURLFormat = "curl -L -o %s %s" 39 WgetDownloadURLFormat = "wget -O %s %s" 40 41 defaultDownloadURL = CurlDownloadURLFormat 42 ) 43 44 func downloadKubernetesBinaryWithArch(downloadPath string, arch string, binaryVersion types.InfraVersionInfo) (map[string]*files.KubeBinary, error) { 45 downloadCommand := func(path, url string) string { 46 return fmt.Sprintf(defaultDownloadURL, path, url) 47 } 48 49 binaries := []*files.KubeBinary{ 50 files.NewKubeBinary("etcd", arch, binaryVersion.EtcdVersion, downloadPath, downloadCommand), 51 files.NewKubeBinary("kubeadm", arch, binaryVersion.KubernetesVersion, downloadPath, downloadCommand), 52 files.NewKubeBinary("kubelet", arch, binaryVersion.KubernetesVersion, downloadPath, downloadCommand), 53 files.NewKubeBinary("kubectl", arch, binaryVersion.KubernetesVersion, downloadPath, downloadCommand), 54 files.NewKubeBinary("kubecni", arch, binaryVersion.CniVersion, downloadPath, downloadCommand), 55 files.NewKubeBinary("helm", arch, binaryVersion.HelmVersion, downloadPath, downloadCommand), 56 // for containerd 57 files.NewKubeBinary("crictl", arch, binaryVersion.CRICtlVersion, downloadPath, downloadCommand), 58 files.NewKubeBinary("containerd", arch, binaryVersion.ContainerVersion, downloadPath, downloadCommand), 59 files.NewKubeBinary("runc", arch, binaryVersion.RuncVersion, downloadPath, downloadCommand), 60 } 61 62 binariesMap := make(map[string]*files.KubeBinary) 63 for _, binary := range binaries { 64 if err := binary.CreateBaseDir(); err != nil { 65 return nil, cfgcore.WrapError(err, "failed to create file %s base dir.", binary.FileName) 66 } 67 logger.Log.Messagef(common.LocalHost, "downloading %s %s %s ...", arch, binary.ID, binary.Version) 68 binariesMap[binary.ID] = binary 69 if checkDownloadBinary(binary) { 70 continue 71 } 72 if err := download(binary); err != nil { 73 return nil, cfgcore.WrapError(err, "failed to download %s binary: %s", binary.ID, binary.GetCmd()) 74 } 75 } 76 return binariesMap, nil 77 } 78 79 func checkDownloadBinary(binary *files.KubeBinary) bool { 80 if !util.IsExist(binary.Path()) { 81 return false 82 } 83 err := kbutils.CheckSha256sum(binary) 84 if err != nil { 85 logger.Log.Messagef(common.LocalHost, "failed to check %s sha256, error: %v", binary.ID, err) 86 _ = os.Remove(binary.Path()) 87 return false 88 } 89 logger.Log.Messagef(common.LocalHost, "%s is existed", binary.ID) 90 return true 91 } 92 93 func download(binary *files.KubeBinary) error { 94 if err := kbutils.RunCommand(exec.Command("/bin/sh", "-c", binary.GetCmd())); err != nil { 95 return err 96 } 97 return kbutils.WriteSha256sum(binary) 98 }