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" "strings"
"time" "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" "golang.org/x/crypto/ssh"
) )
// Params stores the git clone parameters used to var (
// configure and customzie the git clone behavior. build string
type Params struct { buildDate string
Commands []string `json:"commands"` )
Login string `json:"user"`
Port int `json:"port"`
Host StrSlice `json:"host"`
Sleep int `json:"sleep"`
}
func main() { func main() {
v := new(Params) fmt.Printf("Drone SSH Plugin built at %s\n", buildDate)
w := new(plugin.Workspace)
plugin.Param("workspace", w) workspace := drone.Workspace{}
plugin.Param("vargs", &v) vargs := Params{}
plugin.Param("workspace", &workspace)
plugin.Param("vargs", &vargs)
plugin.MustParse() plugin.MustParse()
for i, host := range v.Host.Slice() { for i, host := range vargs.Host.Slice() {
err := run(w.Keys, v, host) err := run(workspace.Keys, &vargs, host)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }
if v.Sleep != 0 && i != v.Host.Len()-1 {
fmt.Printf("$ sleep %d\n", v.Sleep) if vargs.Sleep != 0 && i != vargs.Host.Len()-1 {
time.Sleep(time.Duration(v.Sleep) * time.Second) 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 { func run(key *drone.Key, params *Params, host string) error {
if params.Login == "" {
// if no username is provided assume root
if len(params.Login) == 0 {
params.Login = "root" params.Login = "root"
} }
// if no port is provided use default
if params.Port == 0 { if params.Port == 0 {
params.Port = 22 params.Port = 22
} }
// join the host and port if necessary
addr := net.JoinHostPort( addr := net.JoinHostPort(
host, host,
strconv.Itoa(params.Port), 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) 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 { 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{ config := &ssh.ClientConfig{
User: params.Login, User: params.Login,
Auth: []ssh.AuthMethod{ssh.PublicKeys(signer)}, Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
} }
client, err := ssh.Dial("tcp", addr, config) client, err := ssh.Dial("tcp", addr, config)
if err != nil { 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() session, err := client.NewSession()
if err != nil { 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() defer session.Close()
session.Stdout = os.Stdout session.Stdout = os.Stdout
session.Stderr = os.Stderr session.Stderr = os.Stderr
return session.Run(strings.Join(params.Commands, "\n")) return session.Run(strings.Join(params.Commands, "\n"))
} }

View File

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