Minor code refactoring

This commit is contained in:
Thomas Boerger 2016-01-01 21:36:08 +01:00
parent 35b2a960fc
commit 4bfff6f1d6
2 changed files with 42 additions and 36 deletions

64
main.go
View File

@ -8,83 +8,85 @@ import (
"strings"
"time"
"github.com/drone/drone-plugin-go/plugin"
"github.com/drone/drone-go/drone"
"github.com/drone/drone-go/plugin"
"golang.org/x/crypto/ssh"
)
// Params stores the git clone parameters used to
// configure and customzie the git clone behavior.
type Params struct {
Commands []string `json:"commands"`
Login string `json:"user"`
Port int `json:"port"`
Host StrSlice `json:"host"`
Sleep int `json:"sleep"`
}
var (
build string
buildDate string
)
func main() {
v := new(Params)
w := new(plugin.Workspace)
plugin.Param("workspace", w)
plugin.Param("vargs", &v)
fmt.Printf("Drone SSH Plugin built at %s\n", buildDate)
workspace := drone.Workspace{}
vargs := Params{}
plugin.Param("workspace", &workspace)
plugin.Param("vargs", &vargs)
plugin.MustParse()
for i, host := range v.Host.Slice() {
err := run(w.Keys, v, host)
for i, host := range vargs.Host.Slice() {
err := run(workspace.Keys, &vargs, host)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if v.Sleep != 0 && i != v.Host.Len()-1 {
fmt.Printf("$ sleep %d\n", v.Sleep)
time.Sleep(time.Duration(v.Sleep) * time.Second)
if vargs.Sleep != 0 && i != vargs.Host.Len()-1 {
fmt.Printf("$ sleep %d\n", vargs.Sleep)
time.Sleep(time.Duration(vargs.Sleep) * time.Second)
}
}
}
func run(keys *plugin.Keypair, params *Params, host string) error {
// if no username is provided assume root
if len(params.Login) == 0 {
func run(key *drone.Key, params *Params, host string) error {
if params.Login == "" {
params.Login = "root"
}
// if no port is provided use default
if params.Port == 0 {
params.Port = 22
}
// join the host and port if necessary
addr := net.JoinHostPort(
host,
strconv.Itoa(params.Port),
)
// trace command used for debugging in the build logs
fmt.Printf("$ ssh %s@%s -p %d\n", params.Login, addr, params.Port)
signer, err := ssh.ParsePrivateKey([]byte(key.Private))
signer, err := ssh.ParsePrivateKey([]byte(keys.Private))
if err != nil {
return fmt.Errorf("Error parsing private key. %s.", err)
return fmt.Errorf("Error: Failed to parse private key. %s", err)
}
config := &ssh.ClientConfig{
User: params.Login,
Auth: []ssh.AuthMethod{ssh.PublicKeys(signer)},
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
}
client, err := ssh.Dial("tcp", addr, config)
if err != nil {
return fmt.Errorf("Error dialing server. %s.", err)
return fmt.Errorf("Error: Failed to dial to server. %s", err)
}
session, err := client.NewSession()
if err != nil {
return fmt.Errorf("Error starting ssh session. %s.", err)
return fmt.Errorf("Error: Failed to start a SSH session. %s", err)
}
defer session.Close()
session.Stdout = os.Stdout
session.Stderr = os.Stderr
return session.Run(strings.Join(params.Commands, "\n"))
}

View File

@ -5,7 +5,7 @@ import (
"os"
"testing"
"github.com/drone/drone-plugin-go/plugin"
"github.com/drone/drone-go/drone"
)
var (
@ -15,14 +15,13 @@ var (
)
func TestRun(t *testing.T) {
// only runs the test if a host server is provided
if len(host) == 0 {
t.Skipf("TEST_SSH_HOST not provided")
return
}
out, err := ioutil.ReadFile(key)
if err != nil {
t.Errorf("Unable to read or find a test privte key. %s", err)
}
@ -30,14 +29,19 @@ func TestRun(t *testing.T) {
params := &Params{
Commands: []string{"whoami", "time", "ps -ax"},
Login: user,
Host: StrSlice{[]string{host}},
Host: drone.NewStringSlice(
[]string{
host,
},
),
}
keys := &plugin.Keypair{
keys := &drone.Key{
Private: string(out),
}
err = run(keys, params, host)
if err != nil {
t.Errorf("Unable to run SSH commands. %s.", err)
}