mirror of
https://github.com/appleboy/drone-ssh.git
synced 2025-05-09 18:23:21 +08:00
initial commit for ssh plugin
This commit is contained in:
parent
6ad0e68641
commit
8739246151
1
.drone.sec
Normal file
1
.drone.sec
Normal file
@ -0,0 +1 @@
|
||||
eyJhbGciOiJSU0EtT0FFUCIsImVuYyI6IkExMjhHQ00ifQ.1v4prWhimHOoJmXNSKBt5IjNBsTYNaKLZOtxZO_n7iv-nKiTFEQnxbNtIvLVRTPAbhBIlr8XqR-zDlttB_B9PHrLxfKFe7emNgHa66xKSmLNbT2f2oPwv1l-gGGAHQSrpbz3VfG9JtLjsci-UaDPf7ovUe_KqnPPVhw92shvY_8pt6IIBnM_trG7nzwL9sKOVz6kOkPi1rXsTCiylgShCp7yIFWXCJPAQXygdBfy2U0CiIS5u2d6kS9kiOEmq5j7oDSw8kkglM3PPRWXH2iYGA7W1LuKTthX904S55iAJ9vanAo7uuDKtRBfz6reYG-O6wNGP9H8QQOe87lA5GMdPQ.KMKq9cLqmgUF6LOT.UtS2QLKukG_mcUB4z6OddpZftBpUG6IAGTMHKMYfZd170pBVWpApW5jzPpb1CzIzMc-8muuMbKHu6f6YeZlkpOCet8uO19nlSmk8Tx9IAQUk_e2mhFysaUpljqMLDNGKWKZiFRoJ4uQMmcZlkiozQDhEkcHbBGQ2TnzRZlSJ9DhFDo1-WqLZks56btnh-Iat79Vb3IkEzT8Bpybq9bSREy2YK8wBe6XqSf8qUQ.EEfQBB-UuDyuf_ZERANIPA
|
29
.drone.yml
Normal file
29
.drone.yml
Normal file
@ -0,0 +1,29 @@
|
||||
build:
|
||||
image: golang:1.5
|
||||
environment:
|
||||
- GO15VENDOREXPERIMENT=1
|
||||
- GOOS=linux
|
||||
- GOARCH=amd64
|
||||
- CGO_ENABLED=0
|
||||
commands:
|
||||
- go get
|
||||
- go build
|
||||
- go test
|
||||
|
||||
publish:
|
||||
docker:
|
||||
username: drone
|
||||
password: $$DOCKER_PASS
|
||||
email: $$DOCKER_EMAIL
|
||||
repo: plugins/drone-ssh
|
||||
when:
|
||||
branch: master
|
||||
|
||||
plugin:
|
||||
name: SSH
|
||||
desc: Use SSH to execute commands on a remote host
|
||||
type: deploy
|
||||
image: plugins/drone-ssh
|
||||
labels:
|
||||
- deploy
|
||||
- ssh
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -22,3 +22,5 @@ _testmain.go
|
||||
*.exe
|
||||
*.test
|
||||
*.prof
|
||||
|
||||
drone-ssh
|
19
DOCS.md
Normal file
19
DOCS.md
Normal file
@ -0,0 +1,19 @@
|
||||
Use the SSH plugin to execute commands on a remote server. The following parameters are used to configure this plugin:
|
||||
|
||||
* `host` - address or IP of the remote machine
|
||||
* `port` - port to connect to on the remote machine
|
||||
* `user` - user to log in as on the remote machine
|
||||
* `commands` - list of commands to execute
|
||||
|
||||
The following is a sample SSH configuration in your .drone.yml file:
|
||||
|
||||
```yaml
|
||||
deploy:
|
||||
ssh:
|
||||
host: foo.com
|
||||
user: root
|
||||
port: 22
|
||||
commands:
|
||||
- echo hello
|
||||
- echo world
|
||||
```
|
12
Dockerfile
Normal file
12
Dockerfile
Normal file
@ -0,0 +1,12 @@
|
||||
# Docker image for the Drone build runner
|
||||
#
|
||||
# CGO_ENABLED=0 go build -a -tags netgo
|
||||
# docker build --rm=true -t plugins/drone-ssh .
|
||||
|
||||
FROM gliderlabs/alpine:3.1
|
||||
RUN apk add --update \
|
||||
python \
|
||||
py-pip \
|
||||
&& pip install awscli
|
||||
ADD drone-ssh /bin/
|
||||
ENTRYPOINT ["/bin/drone-ssh"]
|
7
logo.svg
Normal file
7
logo.svg
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<svg width="256px" height="210px" viewBox="0 0 256 210" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid">
|
||||
<g>
|
||||
<rect fill="#000000" x="0" y="0" width="256" height="209.342334" rx="5"></rect>
|
||||
<path d="M28.2414658,21.7735776 C27.3856638,20.9177756 26.3529412,20.4898746 25.143298,20.4898746 C23.9336548,20.4898746 22.8968177,20.9218901 22.0327869,21.7859209 C21.1810993,22.6376085 20.7490839,23.6744455 20.7490839,24.8840887 C20.7490839,26.0937319 21.1810993,27.1305689 22.0327869,27.9822565 L35.4746384,41.424108 L22.0451302,54.8536162 C21.1810993,55.7176471 20.7490839,56.7544841 20.7367406,57.9641273 C20.7490839,59.1737705 21.1810993,60.2106075 22.0327869,61.0622951 C22.8968177,61.9139826 23.9336548,62.3459981 25.1309547,62.3583414 C26.3529412,62.3583414 27.3897782,61.9263259 28.2538091,61.0622951 L43.1891996,46.1145612 C46.3243973,42.9917068 46.3243973,39.8565092 43.1891996,36.7213115 L28.2414658,21.7735776 L28.2414658,21.7735776 Z M86.7857281,54.8783028 C85.9216972,54.0142719 84.8725169,53.5822565 83.6505304,53.5822565 L83.6505304,53.5760849 L54.8165863,53.5760849 L54.8165863,53.5822565 C53.5945998,53.5822565 52.5577628,54.0142719 51.6937319,54.8783028 C50.8297011,55.7423337 50.3976856,56.7791707 50.3976856,58.0011572 C50.3976856,59.2231437 50.8297011,60.272324 51.6937319,61.1363549 C52.5577628,62.0003857 53.5945998,62.4324012 54.8165863,62.4324012 L54.8165863,62.4262295 L83.6505304,62.4262295 L83.6505304,62.4324012 C84.8725169,62.4324012 85.9216972,62.0003857 86.7857281,61.1363549 C87.6497589,60.272324 88.0817743,59.2231437 88.0817743,58.0011572 C88.0817743,56.7791707 87.6497589,55.7423337 86.7857281,54.8783028 L86.7857281,54.8783028 L86.7857281,54.8783028 Z" fill="#FFFFFF"></path>
|
||||
</g>
|
||||
</svg>
|
After Width: | Height: | Size: 1.8 KiB |
78
main.go
Normal file
78
main.go
Normal file
@ -0,0 +1,78 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/drone/drone-plugin-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 string `json:"host"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
v := new(Params)
|
||||
w := new(plugin.Workspace)
|
||||
plugin.Param("workspace", w)
|
||||
plugin.Param("vargs", &v)
|
||||
plugin.MustParse()
|
||||
|
||||
err := run(w.Keys, v)
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run(keys *plugin.Keypair, params *Params) error {
|
||||
|
||||
// if no username is provided assume root
|
||||
if len(params.Login) == 0 {
|
||||
params.Login = "root"
|
||||
}
|
||||
|
||||
// if no username is provided assume root
|
||||
if params.Port == 0 {
|
||||
params.Port = 22
|
||||
}
|
||||
|
||||
// join the host and port if necessary
|
||||
host := net.JoinHostPort(
|
||||
params.Host,
|
||||
strconv.Itoa(params.Port),
|
||||
)
|
||||
|
||||
signer, err := ssh.ParsePrivateKey([]byte(keys.Private))
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error parsing private key. %s.", err)
|
||||
}
|
||||
|
||||
config := &ssh.ClientConfig{
|
||||
User: params.Login,
|
||||
Auth: []ssh.AuthMethod{ssh.PublicKeys(signer)},
|
||||
}
|
||||
|
||||
client, err := ssh.Dial("tcp", host, config)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error dialing server. %s.", err)
|
||||
}
|
||||
|
||||
session, err := client.NewSession()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error starting ssh session. %s.", err)
|
||||
}
|
||||
defer session.Close()
|
||||
|
||||
session.Stdout = os.Stdout
|
||||
session.Stderr = os.Stderr
|
||||
return session.Run(strings.Join(params.Commands, "\n"))
|
||||
}
|
44
main_test.go
Normal file
44
main_test.go
Normal file
@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/drone/drone-plugin-go/plugin"
|
||||
)
|
||||
|
||||
var (
|
||||
host = os.Getenv("TEST_SSH_HOST")
|
||||
user = os.Getenv("TEST_SSH_USER")
|
||||
key = os.Getenv("TEST_SSH_KEY")
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
params := &Params{
|
||||
Commands: []string{"whoami", "time", "ps -ax"},
|
||||
Login: user,
|
||||
Host: host,
|
||||
}
|
||||
|
||||
keys := &plugin.Keypair{
|
||||
Private: string(out),
|
||||
}
|
||||
|
||||
err = run(keys, params)
|
||||
if err != nil {
|
||||
t.Errorf("Unable to run SSH commands. %s.", err)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user