k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/measurement/util/ssh.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package util
    18  
    19  import (
    20  	"k8s.io/perf-tests/clusterloader2/pkg/provider"
    21  	"net/url"
    22  )
    23  
    24  // GetMasterHost turns host name (without prefix and port).
    25  func GetMasterHost(host string) (string, error) {
    26  	masterURL, err := url.Parse(host)
    27  	if err != nil {
    28  		return "", err
    29  	}
    30  	return masterURL.Hostname(), nil
    31  }
    32  
    33  // SSHResult represents result of ssh command.
    34  type SSHResult struct {
    35  	User   string
    36  	Host   string
    37  	Cmd    string
    38  	Stdout string
    39  	Stderr string
    40  	Code   int
    41  }
    42  
    43  // SSH runs command on given host using ssh.
    44  func SSH(cmd, host string, provider provider.Provider) (SSHResult, error) {
    45  	result := SSHResult{Host: host, Cmd: cmd}
    46  	stdout, stderr, code, err := provider.RunSSHCommand(cmd, host)
    47  	result.Stdout = stdout
    48  	result.Stderr = stderr
    49  	result.Code = code
    50  
    51  	return result, err
    52  }