github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/checker/host_checker.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 checker
    16  
    17  import (
    18  	"fmt"
    19  	"strconv"
    20  	"time"
    21  
    22  	v2 "github.com/alibaba/sealer/types/api/v2"
    23  
    24  	"github.com/alibaba/sealer/utils/ssh"
    25  )
    26  
    27  type HostChecker struct {
    28  }
    29  
    30  func (a HostChecker) Check(cluster *v2.Cluster, phase string) error {
    31  	var ipList []string
    32  	for _, hosts := range cluster.Spec.Hosts {
    33  		ipList = append(ipList, hosts.IPS...)
    34  	}
    35  
    36  	if err := checkHostnameUnique(cluster, ipList); err != nil {
    37  		return err
    38  	}
    39  	return checkTimeSync(cluster, ipList)
    40  }
    41  
    42  func NewHostChecker() Interface {
    43  	return &HostChecker{}
    44  }
    45  
    46  func checkHostnameUnique(cluster *v2.Cluster, ipList []string) error {
    47  	hostnameList := map[string]bool{}
    48  	for _, ip := range ipList {
    49  		s, err := ssh.GetHostSSHClient(ip, cluster)
    50  		if err != nil {
    51  			return fmt.Errorf("checker: failed to get host %s client,%v", ip, err)
    52  		}
    53  		hostname, err := s.CmdToString(ip, "hostname", "")
    54  		if err != nil {
    55  			return fmt.Errorf("checker: failed to get host %s hostname, %v", ip, err)
    56  		}
    57  		if hostnameList[hostname] {
    58  			return fmt.Errorf("checker: hostname cannot be repeated, please set diffent hostname")
    59  		}
    60  		hostnameList[hostname] = true
    61  	}
    62  	return nil
    63  }
    64  
    65  //Check whether the node time is synchronized
    66  func checkTimeSync(cluster *v2.Cluster, ipList []string) error {
    67  	for _, ip := range ipList {
    68  		s, err := ssh.GetHostSSHClient(ip, cluster)
    69  		if err != nil {
    70  			return fmt.Errorf("checker: failed to get host %s client,%v", ip, err)
    71  		}
    72  		timeStamp, err := s.CmdToString(ip, "date +%s", "")
    73  		if err != nil {
    74  			return fmt.Errorf("checker: failed to get %s timestamp, %v", ip, err)
    75  		}
    76  		ts, err := strconv.Atoi(timeStamp)
    77  		if err != nil {
    78  			return fmt.Errorf("checker: failed to reverse timestamp %s, %v", timeStamp, err)
    79  		}
    80  		timeDiff := time.Since(time.Unix(int64(ts), 0)).Minutes()
    81  		if timeDiff < -1 || timeDiff > 1 {
    82  			return fmt.Errorf("checker: the time of %s node is not synchronized", ip)
    83  		}
    84  	}
    85  	return nil
    86  }