github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/infra/container/utils.go (about) 1 // Copyright © 2021 Alibaba Group Holding Ltd. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package container 16 17 import ( 18 "crypto/rand" 19 "fmt" 20 "net" 21 "strconv" 22 "strings" 23 24 v1 "github.com/sealerio/sealer/types/api/v1" 25 "github.com/sealerio/sealer/utils/exec" 26 ) 27 28 func IsDockerAvailable() bool { 29 lines, err := exec.RunSimpleCmd("docker -v") 30 if err != nil || len(lines) != 1 { 31 return false 32 } 33 return strings.Contains(lines, "docker version") 34 } 35 36 func getDiff(host v1.Hosts) (int, []net.IP, error) { 37 var num int 38 var iplist []net.IP 39 count, err := strconv.Atoi(host.Count) 40 if err != nil { 41 return 0, nil, err 42 } 43 if count > len(host.IPList) { 44 //scale up 45 num = count - len(host.IPList) 46 } 47 48 if count < len(host.IPList) { 49 //scale down 50 iplist = host.IPList[count:] 51 } 52 53 return num, iplist, nil 54 } 55 56 func GenUniqueID(n int) string { 57 randBytes := make([]byte, n/2) 58 if _, err := rand.Read(randBytes); err != nil { 59 return "" 60 } 61 return fmt.Sprintf("%x", randBytes) 62 }