Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor/ssh: introduce TIUP_SSH_PATH,TIUP_SCP_PATH #734

Merged
merged 6 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions pkg/cluster/executor/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,17 @@ func (e *NativeSSHExecutor) Execute(cmd string, sudo bool, timeout ...time.Durat
defer cancel()
}

args := []string{"ssh", "-o", "StrictHostKeyChecking=no"}
ssh := "ssh"

if val := os.Getenv(localdata.EnvNameSSHPath); val != "" {
if isExec := utils.IsExecBinary(val); !isExec {
return nil, nil, fmt.Errorf("specified SSH in the environment variable `%s` does not exist or is not executable", localdata.EnvNameSSHPath)
}
ssh = val
}

args := []string{ssh, "-o", "StrictHostKeyChecking=no"}

args = e.configArgs(args) // prefix and postfix args
args = append(args, fmt.Sprintf("%s@%s", e.Config.User, e.Config.Host), cmd)

Expand Down Expand Up @@ -348,7 +358,16 @@ func (e *NativeSSHExecutor) Transfer(src string, dst string, download bool) erro
return e.ConnectionTestResult
}

args := []string{"scp", "-r", "-o", "StrictHostKeyChecking=no"}
scp := "scp"

if val := os.Getenv(localdata.EnvNameSCPPath); val != "" {
if isExec := utils.IsExecBinary(val); !isExec {
return fmt.Errorf("specified SCP in the environment variable `%s` does not exist or is not executable", localdata.EnvNameSCPPath)
}
scp = val
}

args := []string{scp, "-r", "-o", "StrictHostKeyChecking=no"}
args = e.configArgs(args) // prefix and postfix args

if download {
Expand Down
8 changes: 7 additions & 1 deletion pkg/localdata/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,15 @@ const (
// EnvNameSSHPassPrompt is the variable name by which user specific the password prompt for sshpass
EnvNameSSHPassPrompt = "TIUP_SSHPASS_PROMPT"

// EnvNameNativeSSHClient is the variable name by which user can specific use natiive ssh client or not
// EnvNameNativeSSHClient is the variable name by which user can specific use native ssh client or not
EnvNameNativeSSHClient = "TIUP_NATIVE_SSH"

// EnvNameSSHPath is the variable name by which user can specific the executable ssh binary path
EnvNameSSHPath = "TIUP_SSH_PATH"

// EnvNameSCPPath is the variable name by which user can specific the executable scp binary path
EnvNameSCPPath = "TIUP_SCP_PATH"

// MetaFilename represents the process meta file name
MetaFilename = "tiup_process_meta"
)
9 changes: 9 additions & 0 deletions pkg/utils/ioutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ func IsEmptyDir(path string) (bool, error) {
return false, err
}

// IsExecBinary check whether a path is a valid executable
func IsExecBinary(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
}
return !info.IsDir() && info.Mode()&0111 == 0111
}

// Untar decompresses the tarball
func Untar(reader io.Reader, to string) error {
gr, err := gzip.NewReader(reader)
Expand Down
20 changes: 20 additions & 0 deletions pkg/utils/ioutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ func (s *TestIOUtilSuite) TestIOUtil(c *C) {}

func (s *TestIOUtilSuite) SetUpSuite(c *C) {
os.RemoveAll(path.Join(currentDir(), "testdata", "parent"))
os.RemoveAll(path.Join(currentDir(), "testdata", "ssh-exec"))
os.RemoveAll(path.Join(currentDir(), "testdata", "nop-nop"))
}

func (s *TestIOUtilSuite) TearDownSuite(c *C) {
os.RemoveAll(path.Join(currentDir(), "testdata", "parent"))
os.RemoveAll(path.Join(currentDir(), "testdata", "ssh-exec"))
os.RemoveAll(path.Join(currentDir(), "testdata", "nop-nop"))
}

func (s *TestIOUtilSuite) TestIsExist(c *C) {
Expand All @@ -39,6 +43,22 @@ func (s *TestIOUtilSuite) TestIsNotExist(c *C) {
c.Assert(IsNotExist("/tmp/"+uuid.New().String()), IsTrue)
}

func (s *TestIOUtilSuite) TestIsExecBinary(c *C) {
c.Assert(IsExecBinary("/tmp"), IsFalse)

e := path.Join(currentDir(), "testdata", "ssh-exec")
f, err := os.OpenFile(e, os.O_CREATE, 0777)
c.Assert(err, IsNil)
defer f.Close()
c.Assert(IsExecBinary(e), IsTrue)

e = path.Join(currentDir(), "testdata", "nop-nop")
f, err = os.OpenFile(e, os.O_CREATE, 0666)
c.Assert(err, IsNil)
defer f.Close()
c.Assert(IsExecBinary(e), IsFalse)
}

func (s *TestIOUtilSuite) TestUntar(c *C) {
c.Assert(IsNotExist(path.Join(currentDir(), "testdata", "parent")), IsTrue)
f, err := os.Open(path.Join(currentDir(), "testdata", "test.tar.gz"))
Expand Down