2016-07-11 19:47:15 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2019-03-04 14:42:28 +08:00
|
|
|
"errors"
|
2017-01-23 14:33:49 +08:00
|
|
|
"fmt"
|
2018-09-26 09:23:31 +02:00
|
|
|
"io"
|
2017-08-01 12:01:37 +08:00
|
|
|
"os"
|
2016-07-11 19:47:15 +02:00
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2017-01-29 12:57:00 +08:00
|
|
|
"sync"
|
2016-07-11 19:47:15 +02:00
|
|
|
"time"
|
|
|
|
|
|
2024-07-14 15:30:53 +08:00
|
|
|
easyssh "github.com/appleboy/easyssh-proxy"
|
2016-07-11 19:47:15 +02:00
|
|
|
)
|
|
|
|
|
|
2019-03-04 14:42:28 +08:00
|
|
|
var (
|
2025-03-29 09:39:55 +08:00
|
|
|
errMissingHost = errors.New("error: missing server host")
|
2025-11-28 21:44:27 +08:00
|
|
|
errMissingPasswordOrKey = errors.New(
|
|
|
|
|
"error: can't connect without a private SSH key or password",
|
|
|
|
|
)
|
|
|
|
|
errCommandTimeOut = errors.New("error: command timeout")
|
|
|
|
|
envsFormat = "export {NAME}={VALUE}"
|
2017-01-23 14:24:27 +08:00
|
|
|
)
|
|
|
|
|
|
2016-07-11 19:47:15 +02:00
|
|
|
type (
|
2017-01-23 10:03:54 +08:00
|
|
|
// Config for the plugin.
|
2016-07-11 19:47:15 +02:00
|
|
|
Config struct {
|
2020-05-24 10:43:11 +08:00
|
|
|
Key string
|
|
|
|
|
Passphrase string
|
|
|
|
|
KeyPath string
|
|
|
|
|
Username string
|
|
|
|
|
Password string
|
|
|
|
|
Host []string
|
|
|
|
|
Port int
|
2023-06-23 10:45:44 +02:00
|
|
|
Protocol easyssh.Protocol
|
2020-05-24 10:43:11 +08:00
|
|
|
Fingerprint string
|
|
|
|
|
Timeout time.Duration
|
|
|
|
|
CommandTimeout time.Duration
|
|
|
|
|
Script []string
|
|
|
|
|
ScriptStop bool
|
|
|
|
|
Envs []string
|
|
|
|
|
Proxy easyssh.DefaultConfig
|
|
|
|
|
Debug bool
|
|
|
|
|
Sync bool
|
|
|
|
|
Ciphers []string
|
|
|
|
|
UseInsecureCipher bool
|
2023-04-13 04:13:07 +03:00
|
|
|
EnvsFormat string
|
2023-07-23 09:41:09 +08:00
|
|
|
AllEnvs bool
|
2024-01-07 17:44:54 +08:00
|
|
|
RequireTty bool
|
2016-07-11 19:47:15 +02:00
|
|
|
}
|
|
|
|
|
|
2017-01-23 10:03:54 +08:00
|
|
|
// Plugin structure
|
2016-07-11 19:47:15 +02:00
|
|
|
Plugin struct {
|
|
|
|
|
Config Config
|
2018-02-27 22:52:15 -08:00
|
|
|
Writer io.Writer
|
2016-07-11 19:47:15 +02:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
2018-02-27 00:48:38 -06:00
|
|
|
func escapeArg(arg string) string {
|
2025-03-29 09:39:55 +08:00
|
|
|
return "'" + strings.ReplaceAll(arg, "'", `'\''`) + "'"
|
2018-02-27 00:48:38 -06:00
|
|
|
}
|
|
|
|
|
|
2020-11-17 10:14:11 +08:00
|
|
|
func (p Plugin) hostPort(host string) (string, string) {
|
|
|
|
|
hosts := strings.Split(host, ":")
|
|
|
|
|
port := strconv.Itoa(p.Config.Port)
|
2024-01-21 09:27:49 +08:00
|
|
|
if len(hosts) > 1 &&
|
|
|
|
|
(p.Config.Protocol == easyssh.PROTOCOL_TCP ||
|
|
|
|
|
p.Config.Protocol == easyssh.PROTOCOL_TCP4) {
|
2020-11-17 10:14:11 +08:00
|
|
|
host = hosts[0]
|
|
|
|
|
port = hosts[1]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return host, port
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-29 21:31:51 -05:00
|
|
|
func (p Plugin) exec(host string, wg *sync.WaitGroup, errChannel chan error) {
|
2023-04-04 16:56:50 +08:00
|
|
|
defer wg.Done()
|
2020-11-17 10:14:11 +08:00
|
|
|
host, port := p.hostPort(host)
|
2017-10-29 21:31:51 -05:00
|
|
|
// Create MakeConfig instance with remote username, server address and path to private key.
|
|
|
|
|
ssh := &easyssh.MakeConfig{
|
2020-05-24 10:43:11 +08:00
|
|
|
Server: host,
|
|
|
|
|
User: p.Config.Username,
|
|
|
|
|
Password: p.Config.Password,
|
2020-11-17 10:14:11 +08:00
|
|
|
Port: port,
|
2023-06-23 10:45:44 +02:00
|
|
|
Protocol: p.Config.Protocol,
|
2020-05-24 10:43:11 +08:00
|
|
|
Key: p.Config.Key,
|
|
|
|
|
KeyPath: p.Config.KeyPath,
|
|
|
|
|
Passphrase: p.Config.Passphrase,
|
|
|
|
|
Timeout: p.Config.Timeout,
|
|
|
|
|
Ciphers: p.Config.Ciphers,
|
|
|
|
|
Fingerprint: p.Config.Fingerprint,
|
|
|
|
|
UseInsecureCipher: p.Config.UseInsecureCipher,
|
2024-01-07 17:44:54 +08:00
|
|
|
RequestPty: p.Config.RequireTty,
|
2017-10-29 21:31:51 -05:00
|
|
|
Proxy: easyssh.DefaultConfig{
|
2020-05-24 10:43:11 +08:00
|
|
|
Server: p.Config.Proxy.Server,
|
|
|
|
|
User: p.Config.Proxy.User,
|
|
|
|
|
Password: p.Config.Proxy.Password,
|
|
|
|
|
Port: p.Config.Proxy.Port,
|
2023-06-23 10:45:44 +02:00
|
|
|
Protocol: p.Config.Proxy.Protocol,
|
2020-05-24 10:43:11 +08:00
|
|
|
Key: p.Config.Proxy.Key,
|
|
|
|
|
KeyPath: p.Config.Proxy.KeyPath,
|
|
|
|
|
Passphrase: p.Config.Proxy.Passphrase,
|
|
|
|
|
Timeout: p.Config.Proxy.Timeout,
|
|
|
|
|
Ciphers: p.Config.Proxy.Ciphers,
|
|
|
|
|
Fingerprint: p.Config.Proxy.Fingerprint,
|
|
|
|
|
UseInsecureCipher: p.Config.Proxy.UseInsecureCipher,
|
2017-10-29 21:31:51 -05:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-14 15:24:36 +08:00
|
|
|
if p.Config.Debug {
|
|
|
|
|
p.log(host, "======CMD======")
|
|
|
|
|
p.log(host, strings.Join(p.Config.Script, "\n"))
|
|
|
|
|
p.log(host, "======END======")
|
|
|
|
|
}
|
2017-10-29 21:31:51 -05:00
|
|
|
|
|
|
|
|
env := []string{}
|
2023-07-23 09:41:09 +08:00
|
|
|
if p.Config.AllEnvs {
|
|
|
|
|
allenvs := findEnvs("DRONE_", "PLUGIN_", "INPUT_", "GITHUB_")
|
|
|
|
|
p.Config.Envs = append(p.Config.Envs, allenvs...)
|
|
|
|
|
}
|
2017-10-29 21:31:51 -05:00
|
|
|
for _, key := range p.Config.Envs {
|
|
|
|
|
key = strings.ToUpper(key)
|
2018-02-27 21:44:44 -08:00
|
|
|
if val, found := os.LookupEnv(key); found {
|
2025-11-28 21:44:27 +08:00
|
|
|
env = append(
|
|
|
|
|
env,
|
|
|
|
|
p.format(p.Config.EnvsFormat, "{NAME}", key, "{VALUE}", escapeArg(val)),
|
|
|
|
|
)
|
2018-02-27 21:44:44 -08:00
|
|
|
}
|
2017-10-29 21:31:51 -05:00
|
|
|
}
|
|
|
|
|
|
2018-09-26 09:23:31 +02:00
|
|
|
p.Config.Script = append(env, p.scriptCommands()...)
|
2017-10-29 21:31:51 -05:00
|
|
|
|
2024-07-14 15:24:36 +08:00
|
|
|
if p.Config.Debug && len(env) > 0 {
|
2017-10-29 21:31:51 -05:00
|
|
|
p.log(host, "======ENV======")
|
|
|
|
|
p.log(host, strings.Join(env, "\n"))
|
|
|
|
|
p.log(host, "======END======")
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-28 21:44:27 +08:00
|
|
|
stdoutChan, stderrChan, doneChan, errChan, err := ssh.Stream(
|
|
|
|
|
strings.Join(p.Config.Script, "\n"),
|
|
|
|
|
p.Config.CommandTimeout,
|
|
|
|
|
)
|
2017-10-29 21:31:51 -05:00
|
|
|
if err != nil {
|
|
|
|
|
errChannel <- err
|
2023-04-04 16:56:50 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// read from the output channel until the done signal is passed
|
2024-11-16 09:58:21 +08:00
|
|
|
var isTimeout bool
|
2023-04-04 16:56:50 +08:00
|
|
|
loop:
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case isTimeout = <-doneChan:
|
|
|
|
|
break loop
|
|
|
|
|
case outline := <-stdoutChan:
|
|
|
|
|
if outline != "" {
|
2024-11-17 14:48:14 +08:00
|
|
|
p.log(host, outline)
|
2017-10-29 21:31:51 -05:00
|
|
|
}
|
2023-04-04 16:56:50 +08:00
|
|
|
case errline := <-stderrChan:
|
|
|
|
|
if errline != "" {
|
2024-11-17 14:48:14 +08:00
|
|
|
p.log(host, errline)
|
2023-04-04 16:56:50 +08:00
|
|
|
}
|
|
|
|
|
case err = <-errChan:
|
2017-10-29 21:31:51 -05:00
|
|
|
}
|
2023-04-04 16:56:50 +08:00
|
|
|
}
|
2017-10-29 21:31:51 -05:00
|
|
|
|
2023-04-04 16:56:50 +08:00
|
|
|
// get exit code or command error.
|
|
|
|
|
if err != nil {
|
|
|
|
|
errChannel <- err
|
2017-10-29 21:31:51 -05:00
|
|
|
}
|
|
|
|
|
|
2023-04-04 16:56:50 +08:00
|
|
|
// command time out
|
|
|
|
|
if !isTimeout {
|
|
|
|
|
errChannel <- errCommandTimeOut
|
|
|
|
|
}
|
2017-10-29 21:31:51 -05:00
|
|
|
}
|
|
|
|
|
|
2023-07-23 07:30:08 +08:00
|
|
|
// format string
|
2023-04-13 04:13:07 +03:00
|
|
|
func (p Plugin) format(format string, args ...string) string {
|
|
|
|
|
r := strings.NewReplacer(args...)
|
|
|
|
|
return r.Replace(format)
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-23 07:30:08 +08:00
|
|
|
// log output to console
|
2017-01-29 12:57:00 +08:00
|
|
|
func (p Plugin) log(host string, message ...interface{}) {
|
2018-02-27 22:52:15 -08:00
|
|
|
if p.Writer == nil {
|
|
|
|
|
p.Writer = os.Stdout
|
|
|
|
|
}
|
2017-05-10 21:17:31 +08:00
|
|
|
if count := len(p.Config.Host); count == 1 {
|
2018-02-27 22:52:15 -08:00
|
|
|
fmt.Fprintf(p.Writer, "%s", fmt.Sprintln(message...))
|
2023-07-23 07:30:08 +08:00
|
|
|
return
|
2017-05-10 21:17:31 +08:00
|
|
|
}
|
2023-07-23 07:30:08 +08:00
|
|
|
|
|
|
|
|
fmt.Fprintf(p.Writer, "%s: %s", host, fmt.Sprintln(message...))
|
2017-01-24 11:28:48 +08:00
|
|
|
}
|
|
|
|
|
|
2017-01-23 10:03:54 +08:00
|
|
|
// Exec executes the plugin.
|
2016-07-11 19:47:15 +02:00
|
|
|
func (p Plugin) Exec() error {
|
2023-04-04 16:56:50 +08:00
|
|
|
p.Config.Host = trimValues(p.Config.Host)
|
|
|
|
|
|
2019-03-04 14:42:28 +08:00
|
|
|
if len(p.Config.Host) == 0 {
|
2019-03-04 14:47:28 +08:00
|
|
|
return errMissingHost
|
2017-01-23 14:24:27 +08:00
|
|
|
}
|
|
|
|
|
|
2017-04-23 11:49:40 +08:00
|
|
|
if len(p.Config.Key) == 0 && len(p.Config.Password) == 0 && len(p.Config.KeyPath) == 0 {
|
2019-03-04 14:47:28 +08:00
|
|
|
return errMissingPasswordOrKey
|
2016-07-11 19:47:15 +02:00
|
|
|
}
|
|
|
|
|
|
2023-04-13 09:32:31 +08:00
|
|
|
if p.Config.EnvsFormat == "" {
|
|
|
|
|
p.Config.EnvsFormat = envsFormat
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-10 11:34:00 +08:00
|
|
|
wg := sync.WaitGroup{}
|
2017-01-29 12:57:00 +08:00
|
|
|
wg.Add(len(p.Config.Host))
|
2019-03-30 08:03:16 +08:00
|
|
|
errChannel := make(chan error)
|
|
|
|
|
finished := make(chan struct{})
|
2023-04-04 16:56:50 +08:00
|
|
|
if p.Config.Sync {
|
|
|
|
|
go func() {
|
|
|
|
|
for _, host := range p.Config.Host {
|
|
|
|
|
p.exec(host, &wg, errChannel)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
} else {
|
|
|
|
|
for _, host := range p.Config.Host {
|
2017-10-29 21:31:51 -05:00
|
|
|
go p.exec(host, &wg, errChannel)
|
|
|
|
|
}
|
2017-01-29 12:57:00 +08:00
|
|
|
}
|
2016-07-11 19:47:15 +02:00
|
|
|
|
2017-01-29 12:57:00 +08:00
|
|
|
go func() {
|
|
|
|
|
wg.Wait()
|
|
|
|
|
close(finished)
|
|
|
|
|
}()
|
2016-07-11 19:47:15 +02:00
|
|
|
|
2017-01-29 12:57:00 +08:00
|
|
|
select {
|
|
|
|
|
case <-finished:
|
|
|
|
|
case err := <-errChannel:
|
2016-07-11 19:47:15 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-13 03:00:20 +02:00
|
|
|
fmt.Println("===============================================")
|
|
|
|
|
fmt.Println("✅ Successfully executed commands to all hosts.")
|
|
|
|
|
fmt.Println("===============================================")
|
2017-01-29 12:57:00 +08:00
|
|
|
|
2016-07-11 19:47:15 +02:00
|
|
|
return nil
|
|
|
|
|
}
|
2018-09-26 09:23:31 +02:00
|
|
|
|
|
|
|
|
func (p Plugin) scriptCommands() []string {
|
2019-11-21 09:05:32 +08:00
|
|
|
scripts := []string{}
|
2018-09-26 09:23:31 +02:00
|
|
|
|
|
|
|
|
for _, cmd := range p.Config.Script {
|
|
|
|
|
if p.Config.ScriptStop {
|
2019-11-21 09:05:32 +08:00
|
|
|
scripts = append(scripts, strings.Split(cmd, "\n")...)
|
|
|
|
|
} else {
|
|
|
|
|
scripts = append(scripts, cmd)
|
2018-09-26 09:23:31 +02:00
|
|
|
}
|
2019-11-21 09:05:32 +08:00
|
|
|
}
|
2018-09-26 09:23:31 +02:00
|
|
|
|
2019-11-21 09:05:32 +08:00
|
|
|
commands := make([]string, 0)
|
|
|
|
|
|
|
|
|
|
for _, cmd := range scripts {
|
2020-08-08 15:31:57 +08:00
|
|
|
cmd = strings.TrimSpace(cmd)
|
2019-11-21 09:13:04 +08:00
|
|
|
if strings.TrimSpace(cmd) == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2018-09-26 09:23:31 +02:00
|
|
|
commands = append(commands, cmd)
|
2020-08-08 15:31:57 +08:00
|
|
|
if p.Config.ScriptStop && cmd[(len(cmd)-1):] != "\\" {
|
2025-11-28 21:44:27 +08:00
|
|
|
commands = append(
|
|
|
|
|
commands,
|
|
|
|
|
"DRONE_SSH_PREV_COMMAND_EXIT_CODE=$? ; if [ $DRONE_SSH_PREV_COMMAND_EXIT_CODE -ne 0 ]; then exit $DRONE_SSH_PREV_COMMAND_EXIT_CODE; fi;",
|
|
|
|
|
)
|
2019-11-21 09:05:32 +08:00
|
|
|
}
|
2018-09-26 09:23:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return commands
|
|
|
|
|
}
|
2023-04-04 16:56:50 +08:00
|
|
|
|
|
|
|
|
func trimValues(keys []string) []string {
|
|
|
|
|
var newKeys []string
|
|
|
|
|
|
|
|
|
|
for _, value := range keys {
|
|
|
|
|
value = strings.TrimSpace(value)
|
|
|
|
|
if len(value) == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newKeys = append(newKeys, value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newKeys
|
|
|
|
|
}
|
2023-07-23 09:41:09 +08:00
|
|
|
|
|
|
|
|
// Find all envs from specified prefix
|
|
|
|
|
func findEnvs(prefix ...string) []string {
|
|
|
|
|
envs := []string{}
|
|
|
|
|
for _, e := range os.Environ() {
|
|
|
|
|
for _, p := range prefix {
|
|
|
|
|
if strings.HasPrefix(e, p) {
|
|
|
|
|
e = strings.Split(e, "=")[0]
|
|
|
|
|
envs = append(envs, e)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return envs
|
|
|
|
|
}
|