github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/filesystem/cloudfilesystem/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 cloudfilesystem
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"path/filepath"
    21  
    22  	v2 "github.com/alibaba/sealer/types/api/v2"
    23  
    24  	"github.com/alibaba/sealer/common"
    25  	"github.com/alibaba/sealer/utils"
    26  	"github.com/alibaba/sealer/utils/ssh"
    27  )
    28  
    29  func copyFiles(sshEntry ssh.Interface, ip, src, target string) error {
    30  	files, err := ioutil.ReadDir(src)
    31  	if err != nil {
    32  		return fmt.Errorf("failed to copy files %s", err)
    33  	}
    34  
    35  	for _, f := range files {
    36  		if f.Name() == common.RegistryDirName {
    37  			continue
    38  		}
    39  		err = sshEntry.Copy(ip, filepath.Join(src, f.Name()), filepath.Join(target, f.Name()))
    40  		if err != nil {
    41  			return fmt.Errorf("failed to copy sub files %v", err)
    42  		}
    43  	}
    44  	return nil
    45  }
    46  
    47  func copyRegistry(regIP string, cluster *v2.Cluster, mountDir map[string]bool, target string) error {
    48  	sshClient, err := ssh.GetHostSSHClient(regIP, cluster)
    49  	if err != nil {
    50  		return err
    51  	}
    52  	for dir := range mountDir {
    53  		dir = filepath.Join(dir, common.RegistryDirName)
    54  		if !utils.IsExist(dir) {
    55  			return nil
    56  		}
    57  		err = sshClient.Copy(regIP, dir, filepath.Join(target, common.RegistryDirName))
    58  		if err != nil {
    59  			return err
    60  		}
    61  	}
    62  	return nil
    63  }
    64  
    65  func CleanFilesystem(clusterName string) error {
    66  	return utils.CleanFiles(common.GetClusterWorkDir(clusterName), common.DefaultClusterBaseDir(clusterName),
    67  		common.DefaultKubeConfigDir(), common.KubectlPath)
    68  }