-
Notifications
You must be signed in to change notification settings - Fork 305
/
ssh.go
104 lines (90 loc) · 2.9 KB
/
ssh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package task
import (
"fmt"
"time"
"github.com/joomcode/errorx"
"github.com/pingcap/tiup/pkg/cluster/executor"
)
var (
errNS = errorx.NewNamespace("task")
)
// RootSSH is used to establish a SSH connection to the target host with specific key
type RootSSH struct {
host string // hostname of the SSH server
port int // port of the SSH server
user string // username to login to the SSH server
password string // password of the user
keyFile string // path to the private key file
passphrase string // passphrase of the private key file
timeout int64 // timeout in seconds when connecting via SSH
}
// Execute implements the Task interface
func (s *RootSSH) Execute(ctx *Context) error {
e := executor.NewSSHExecutor(executor.SSHConfig{
Host: s.host,
Port: s.port,
User: s.user,
Password: s.password,
KeyFile: s.keyFile,
Passphrase: s.passphrase,
Timeout: time.Second * time.Duration(s.timeout),
}, s.user != "root") // using sudo by default if user is not root
ctx.SetExecutor(s.host, e)
return nil
}
// Rollback implements the Task interface
func (s *RootSSH) Rollback(ctx *Context) error {
ctx.exec.Lock()
delete(ctx.exec.executors, s.host)
ctx.exec.Unlock()
return nil
}
// String implements the fmt.Stringer interface
func (s RootSSH) String() string {
if len(s.keyFile) > 0 {
return fmt.Sprintf("RootSSH: user=%s, host=%s, port=%d, key=%s", s.user, s.host, s.port, s.keyFile)
}
return fmt.Sprintf("RootSSH: user=%s, host=%s, port=%d", s.user, s.host, s.port)
}
// UserSSH is used to establish a SSH connection to the target host with generated key
type UserSSH struct {
host string
port int
deployUser string
timeout int64
}
// Execute implements the Task interface
func (s *UserSSH) Execute(ctx *Context) error {
e := executor.NewSSHExecutor(executor.SSHConfig{
Host: s.host,
Port: s.port,
KeyFile: ctx.PrivateKeyPath,
User: s.deployUser,
Timeout: time.Second * time.Duration(s.timeout),
}, false) // not using sudo by default
ctx.SetExecutor(s.host, e)
return nil
}
// Rollback implements the Task interface
func (s *UserSSH) Rollback(ctx *Context) error {
ctx.exec.Lock()
delete(ctx.exec.executors, s.host)
ctx.exec.Unlock()
return nil
}
// String implements the fmt.Stringer interface
func (s UserSSH) String() string {
return fmt.Sprintf("UserSSH: user=%s, host=%s", s.deployUser, s.host)
}