github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/plugin/shell_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/logger"
    22  
    23  	"github.com/alibaba/sealer/utils"
    24  
    25  	"github.com/alibaba/sealer/pkg/env"
    26  
    27  	"github.com/alibaba/sealer/common"
    28  	"github.com/alibaba/sealer/utils/ssh"
    29  )
    30  
    31  type Sheller struct{}
    32  
    33  func NewShellPlugin() Interface {
    34  	return &Sheller{}
    35  }
    36  
    37  func init() {
    38  	Register(ShellPlugin, NewShellPlugin())
    39  }
    40  
    41  func (s Sheller) Run(context Context, phase Phase) (err error) {
    42  	pluginPhases := strings.Split(context.Plugin.Spec.Action, SplitSymbol)
    43  	if utils.NotIn(string(phase), pluginPhases) || context.Plugin.Spec.Type != ShellPlugin {
    44  		return nil
    45  	}
    46  	//get cmdline content
    47  	pluginCmd := context.Plugin.Spec.Data
    48  	if phase != PhaseOriginally {
    49  		pluginCmd = fmt.Sprintf(common.CdAndExecCmd, common.DefaultTheClusterRootfsDir(context.Cluster.Name), pluginCmd)
    50  	}
    51  	//get all host ip
    52  	allHostIP := append(context.Cluster.GetMasterIPList(), context.Cluster.GetNodeIPList()...)
    53  	if on := context.Plugin.Spec.On; on != "" {
    54  		allHostIP, err = GetIpsByOnField(on, context, phase)
    55  		if err != nil {
    56  			if phase == PhasePreClean {
    57  				logger.Error("failed to get ips when %s phase: %v", phase, err)
    58  				return nil
    59  			}
    60  			return err
    61  		}
    62  	}
    63  	var runPluginIPList []string
    64  	for _, ip := range allHostIP {
    65  		//skip non-cluster nodes
    66  		if utils.NotIn(ip, context.Host) {
    67  			continue
    68  		}
    69  		envProcessor := env.NewEnvProcessor(context.Cluster)
    70  		sshClient, err := ssh.NewStdoutSSHClient(ip, context.Cluster)
    71  		if err != nil {
    72  			return err
    73  		}
    74  		err = sshClient.CmdAsync(ip, envProcessor.WrapperShell(ip, pluginCmd))
    75  		if err != nil {
    76  			return fmt.Errorf("failed to run shell cmd,  %v", err)
    77  		}
    78  		runPluginIPList = append(runPluginIPList, ip)
    79  	}
    80  	logger.Info("%s phase shell plugin '%s' executing nodes: %s ", phase, context.Plugin.Name, runPluginIPList)
    81  	return nil
    82  }