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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
// 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 (
"context"
"fmt"
"time"
"github.com/joomcode/errorx"
"github.com/pingcap/tiup/pkg/cluster/ctxt"
"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 uint64 // timeout in seconds when connecting via SSH
exeTimeout uint64 // timeout in seconds waiting command to finish
proxyHost string // hostname of the proxy SSH server
proxyPort int // port of the proxy SSH server
proxyUser string // username to login to the proxy SSH server
proxyPassword string // password of the proxy user
proxyKeyFile string // path to the private key file
proxyPassphrase string // passphrase of the private key file
proxyTimeout uint64 // timeout in seconds when connecting via SSH
sshType executor.SSHType // the type of SSH channel
sudo bool
}
// Execute implements the Task interface
func (s *RootSSH) Execute(ctx context.Context) error {
sc := 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),
ExeTimeout: time.Second * time.Duration(s.exeTimeout),
}
if len(s.proxyHost) > 0 {
sc.Proxy = &executor.SSHConfig{
Host: s.proxyHost,
Port: s.proxyPort,
User: s.proxyUser,
Password: s.proxyPassword,
KeyFile: s.proxyKeyFile,
Passphrase: s.proxyPassphrase,
Timeout: time.Second * time.Duration(s.proxyTimeout),
}
}
e, err := executor.New(s.sshType, s.sudo, sc)
if err != nil {
return err
}
ctxt.GetInner(ctx).SetExecutor(s.host, e)
return nil
}
// Rollback implements the Task interface
func (s *RootSSH) Rollback(ctx context.Context) error {
ctxt.GetInner(ctx).SetExecutor(s.host, nil)
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 an SSH connection to the target host with generated key
type UserSSH struct {
host string
port int
deployUser string
timeout uint64
exeTimeout uint64 // timeout in seconds waiting command to finish
proxyHost string // hostname of the proxy SSH server
proxyPort int // port of the proxy SSH server
proxyUser string // username to login to the proxy SSH server
proxyPassword string // password of the proxy user
proxyKeyFile string // path to the private key file
proxyPassphrase string // passphrase of the private key file
proxyTimeout uint64 // timeout in seconds when connecting via SSH
sshType executor.SSHType
}
// Execute implements the Task interface
func (s *UserSSH) Execute(ctx context.Context) error {
sc := executor.SSHConfig{
Host: s.host,
Port: s.port,
KeyFile: ctxt.GetInner(ctx).PrivateKeyPath,
User: s.deployUser,
Timeout: time.Second * time.Duration(s.timeout),
ExeTimeout: time.Second * time.Duration(s.exeTimeout),
}
if len(s.proxyHost) > 0 {
sc.Proxy = &executor.SSHConfig{
Host: s.proxyHost,
Port: s.proxyPort,
User: s.proxyUser,
Password: s.proxyPassword,
KeyFile: s.proxyKeyFile,
Passphrase: s.proxyPassphrase,
Timeout: time.Second * time.Duration(s.proxyTimeout),
}
}
e, err := executor.New(s.sshType, false, sc)
if err != nil {
return err
}
ctxt.GetInner(ctx).SetExecutor(s.host, e)
return nil
}
// Rollback implements the Task interface
func (s *UserSSH) Rollback(ctx context.Context) error {
ctxt.GetInner(ctx).SetExecutor(s.host, nil)
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)
}
|