github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/plugin/hostname_plugin.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 plugin
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/alibaba/sealer/utils"
    22  
    23  	"github.com/alibaba/sealer/logger"
    24  	"github.com/alibaba/sealer/utils/ssh"
    25  )
    26  
    27  type HostnamePlugin struct {
    28  	data map[string]string
    29  }
    30  
    31  func NewHostnamePlugin() Interface {
    32  	return &HostnamePlugin{data: map[string]string{}}
    33  }
    34  
    35  func init() {
    36  	Register(HostNamePlugin, NewHostnamePlugin())
    37  }
    38  
    39  func (h HostnamePlugin) Run(context Context, phase Phase) error {
    40  	if (phase != PhasePreInit && phase != PhasePreJoin) || context.Plugin.Spec.Type != HostNamePlugin {
    41  		logger.Debug("hostnamePlugin nodes is not PhasePreInit!")
    42  		return nil
    43  	}
    44  	h.data = h.formatData(context.Plugin.Spec.Data)
    45  	for ip, hostname := range h.data {
    46  		if utils.NotIn(ip, context.Host) {
    47  			continue
    48  		}
    49  		sshClient, err := ssh.GetHostSSHClient(ip, context.Cluster)
    50  		if err != nil {
    51  			return err
    52  		}
    53  		err = h.changeNodeName(hostname, ip, sshClient)
    54  		if err != nil {
    55  			return fmt.Errorf("current cluster nodes hostname change failed, %v", err)
    56  		}
    57  	}
    58  	return nil
    59  }
    60  
    61  func (h HostnamePlugin) formatData(data string) map[string]string {
    62  	m := make(map[string]string)
    63  	items := strings.Split(data, "\n")
    64  	if len(items) == 0 {
    65  		logger.Debug("hostname data is empty!")
    66  		return m
    67  	}
    68  	for _, v := range items {
    69  		tmps := strings.Split(v, " ")
    70  		//skip no-compliance hostname data
    71  		if len(tmps) != 2 {
    72  			continue
    73  		}
    74  		ip := tmps[0]
    75  		hostname := tmps[1]
    76  		m[ip] = hostname
    77  	}
    78  	return m
    79  }
    80  
    81  func (h HostnamePlugin) changeNodeName(hostname, ip string, SSH ssh.Interface) error {
    82  	//cmd to change hostname temporarily
    83  	tmpCMD := fmt.Sprintf("hostname %s", hostname)
    84  	//cmd to change hostname permanently
    85  	perCMD := fmt.Sprintf(`rm -f /etc/hostname && echo "%s" >> /etc/hostname`, hostname)
    86  	if err := SSH.CmdAsync(ip, tmpCMD, perCMD); err != nil {
    87  		return fmt.Errorf("failed to change the node %v hostname,%v", ip, err)
    88  	}
    89  	logger.Info("successfully changed node %s hostname to %s.", ip, hostname)
    90  	return nil
    91  }