github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/container-runtime/default.go (about) 1 // Copyright © 2022 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 containerruntime 16 17 import ( 18 "fmt" 19 "net" 20 "path/filepath" 21 22 "github.com/sealerio/sealer/pkg/infradriver" 23 ) 24 25 type DefaultInstaller struct { 26 Info 27 envs map[string]string 28 rootfs string 29 driver infradriver.InfraDriver 30 } 31 32 func (d *DefaultInstaller) InstallOn(hosts []net.IP) error { 33 installCmd := fmt.Sprintf("bash %s", filepath.Join(d.rootfs, "scripts", d.getInstallScriptName())) 34 for _, ip := range hosts { 35 err := d.driver.CmdAsync(ip, d.envs, installCmd) 36 if err != nil { 37 return fmt.Errorf("failed to install %s: execute command(%s) on host (%s): error(%v)", d.Type, installCmd, ip, err) 38 } 39 } 40 return nil 41 } 42 43 func (d *DefaultInstaller) UnInstallFrom(hosts []net.IP) error { 44 cleanCmd := fmt.Sprintf("bash %s", filepath.Join(d.rootfs, "scripts", d.getUnInstallScriptName())) 45 for _, ip := range hosts { 46 err := d.driver.CmdAsync(ip, d.envs, cleanCmd) 47 if err != nil { 48 return fmt.Errorf("failed to uninstall %s: execute command(%s) on host (%s): error(%v)", d.Type, cleanCmd, ip, err) 49 } 50 } 51 return nil 52 } 53 54 func (d *DefaultInstaller) GetInfo() (Info, error) { 55 return d.Info, nil 56 } 57 58 func (d *DefaultInstaller) getInstallScriptName() string { 59 return fmt.Sprintf("%s.sh", d.Type) 60 } 61 62 func (d *DefaultInstaller) getUnInstallScriptName() string { 63 return fmt.Sprintf("uninstall-%s.sh", d.Type) 64 }