Skip to content
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
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ func main() {
Usage: "execute commands",
EnvVar: "PLUGIN_SCRIPT,SSH_SCRIPT",
},
cli.BoolFlag{
Name: "script.stop",
Usage: "stop script after first failure",
EnvVar: "PLUGIN_SCRIPT_STOP",
},
cli.StringFlag{
Name: "proxy.ssh-key",
Usage: "private ssh key of proxy",
Expand Down Expand Up @@ -197,6 +202,7 @@ func run(c *cli.Context) error {
Timeout: c.Duration("timeout"),
CommandTimeout: c.Int("command.timeout"),
Script: c.StringSlice("script"),
ScriptStop: c.Bool("script.stop"),
Secrets: c.StringSlice("secrets"),
Envs: c.StringSlice("envs"),
Debug: c.Bool("debug"),
Expand Down
24 changes: 22 additions & 2 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package main

import (
"fmt"
"io"
"os"
"strconv"
"strings"
"sync"
"time"

"github.com/appleboy/easyssh-proxy"
"io"
)

const (
Expand All @@ -31,6 +31,7 @@ type (
Timeout time.Duration
CommandTimeout int
Script []string
ScriptStop bool
Secrets []string
Envs []string
Proxy easyssh.DefaultConfig
Expand Down Expand Up @@ -82,7 +83,7 @@ func (p Plugin) exec(host string, wg *sync.WaitGroup, errChannel chan error) {
}
}

p.Config.Script = append(env, p.Config.Script...)
p.Config.Script = append(env, p.scriptCommands()...)

if p.Config.Debug {
p.log(host, "======ENV======")
Expand Down Expand Up @@ -179,3 +180,22 @@ func (p Plugin) Exec() error {

return nil
}

func (p Plugin) scriptCommands() []string {
numCommands := len(p.Config.Script)
if p.Config.ScriptStop {
numCommands *= 2
}

commands := make([]string, numCommands)

for _, cmd := range p.Config.Script {
if p.Config.ScriptStop {
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;")
}

commands = append(commands, cmd)
}

return commands
}