mirror of
https://github.com/appleboy/drone-scp.git
synced 2026-06-04 18:23:59 +08:00
Compare commits
176 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 531df19c8c | |||
| df8214b645 | |||
| c85ca1ffd2 | |||
| 933b45bc15 | |||
| 15344d67ae | |||
| cf9e6f260d | |||
| cfa325a8c4 | |||
| c8dbddab25 | |||
| 1b27d28b27 | |||
| 8a0b0f3c0c | |||
| ec489106f9 | |||
| 9ed20ee32d | |||
| 2aee5a3df1 | |||
| bc633e27cf | |||
| 9dad691d4f | |||
| 66579b6dae | |||
| 06609f35cf | |||
| c9771cce78 | |||
| 6088f7da5a | |||
| 6de8f74170 | |||
| bb63d55f89 | |||
| da44b071c4 | |||
| a7eddc4b11 | |||
| 813faf56be | |||
| 4117d2ca30 | |||
| 15bd1a11e4 | |||
| be0d85245a | |||
| 9cc27c6724 | |||
| b36ffb0d5d | |||
| ca7485443c | |||
| f8a64791a4 | |||
| fe8e5e3483 | |||
| 2344618287 | |||
| 24926998c1 | |||
| a82a76903c | |||
| 8e1c446a58 | |||
| a2e6a07e44 | |||
| bc7d0d7a7a | |||
| 26223d7b71 | |||
| b3f09b7592 | |||
| 75d2ee1e10 | |||
| 699675c8fa | |||
| d921289474 | |||
| 4f7d31bd86 | |||
| d798a8cc75 | |||
| 147005589b | |||
| 7fa57c038a | |||
| e6e048f198 | |||
| 8096419bd2 | |||
| 985badff42 | |||
| f68e09cf01 | |||
| 9150f669c9 | |||
| ceb2b47958 | |||
| acc220475c | |||
| 43d4f0d781 | |||
| 357e2db0ff | |||
| 7764e754fe | |||
| 5bf5231d41 | |||
| 0261361be4 | |||
| f410f5e6ca | |||
| e8f0048a45 | |||
| 41ace25862 | |||
| 9c958f8637 | |||
| 9352413b30 | |||
| f22cd46f46 | |||
| 2cc69e2c03 | |||
| 637dcaaccc | |||
| 7c9b1d8b79 | |||
| 7a88a784b2 | |||
| 13e681cc3b | |||
| a363fa22bf | |||
| 2ee4b54624 | |||
| a2708638cf | |||
| 35c2f7a14c | |||
| 8dba1e8e32 | |||
| 049263e847 | |||
| 88f5a95f3e | |||
| 7bdc9fc05e | |||
| 3b2dbe955b | |||
| 464f0edc7e | |||
| 6aa6653378 | |||
| d326bfe5a4 | |||
| 2ebb608269 | |||
| f223505d46 | |||
| b1ca6d49a2 | |||
| c612c8c5c0 | |||
| e8dfaea058 | |||
| 3121cb2aaa | |||
| 72c123f390 | |||
| 04c639cfa8 | |||
| ef521d1e98 | |||
| 496e23a7e9 | |||
| 3e7ff1f928 | |||
| 6179afcde8 | |||
| 4f2e0c2096 | |||
| e2b890d217 | |||
| cdfa646abf | |||
| ed37a75097 | |||
| baa413d348 | |||
| 79f352a5fa | |||
| 9787fcfb60 | |||
| ae6b001406 | |||
| 0d84586fb1 | |||
| d673dc3a68 | |||
| 1f05c75ba3 | |||
| 6472931282 | |||
| bb6e686f9a | |||
| 3911edcd30 | |||
| 212aa456c6 | |||
| 87fcf060aa | |||
| 57d83dcfe0 | |||
| e5ead0fb9c | |||
| 4e0a131533 | |||
| 2704d9d275 | |||
| e9832010f8 | |||
| 28824dfd1a | |||
| 22b6f8b9b4 | |||
| 1d806cc9ed | |||
| dd049e69b9 | |||
| 61940b4c0e | |||
| f20fc0b114 | |||
| ed00c6ef85 | |||
| cc822cd578 | |||
| 77dbb22aed | |||
| b69d758088 | |||
| 65e19227c4 | |||
| df18b64cb4 | |||
| f987106211 | |||
| eddd7a9661 | |||
| 1355c4edc1 | |||
| 2eb54a15ca | |||
| 42540b5d45 | |||
| 5f176416d7 | |||
| 6c2d597d31 | |||
| 9aa3cdb828 | |||
| 392c7c90e3 | |||
| a0c61bbd03 | |||
| 5a6851415d | |||
| e4f3a180c8 | |||
| 9dbfdad07c | |||
| debd2db692 | |||
| 5bd128025d | |||
| d3b4925aff | |||
| 291c006b5c | |||
| dd7f593c06 | |||
| 9559a5b600 | |||
| 974cd420af | |||
| d31fe06287 | |||
| 9384d1261d | |||
| 43037f5cef | |||
| f43d0a817e | |||
| 6cb6510fed | |||
| da5dda1f0d | |||
| 4fc0ebcd98 | |||
| e3418f8441 | |||
| d5baf6186d | |||
| f5d5e54a57 | |||
| 031c1e4801 | |||
| 1bae57a838 | |||
| 91ef54d3d6 | |||
| bfb8c6795c | |||
| 339a0d497f | |||
| cc1520024e | |||
| 455b6dd7ae | |||
| f12fc6e476 | |||
| 807dff2951 | |||
| 8171197bad | |||
| 3277383d4f | |||
| 2bb7d842f2 | |||
| 6e7710e445 | |||
| 658203c26d | |||
| 7cd8855a53 | |||
| 42851ed07b | |||
| 7b90afbf37 | |||
| 21d349362b | |||
| 7dfdbbde89 |
@@ -0,0 +1,16 @@
|
||||
local pipeline = import 'pipeline.libsonnet';
|
||||
local name = 'drone-scp';
|
||||
|
||||
[
|
||||
pipeline.test,
|
||||
pipeline.build(name, 'linux', 'amd64'),
|
||||
pipeline.build(name, 'linux', 'arm64'),
|
||||
pipeline.build(name, 'linux', 'arm'),
|
||||
pipeline.release,
|
||||
pipeline.notifications(depends_on=[
|
||||
'linux-amd64',
|
||||
'linux-arm64',
|
||||
'linux-arm',
|
||||
'release-binary',
|
||||
]),
|
||||
]
|
||||
+365
@@ -0,0 +1,365 @@
|
||||
---
|
||||
kind: pipeline
|
||||
name: testing
|
||||
|
||||
platform:
|
||||
os: linux
|
||||
arch: amd64
|
||||
|
||||
steps:
|
||||
- name: vet
|
||||
pull: always
|
||||
image: golang:1.13
|
||||
commands:
|
||||
- make vet
|
||||
volumes:
|
||||
- name: gopath
|
||||
path: /go
|
||||
|
||||
- name: lint
|
||||
pull: always
|
||||
image: golang:1.13
|
||||
commands:
|
||||
- make lint
|
||||
volumes:
|
||||
- name: gopath
|
||||
path: /go
|
||||
|
||||
- name: misspell
|
||||
pull: always
|
||||
image: golang:1.13
|
||||
commands:
|
||||
- make misspell-check
|
||||
volumes:
|
||||
- name: gopath
|
||||
path: /go
|
||||
|
||||
- name: test
|
||||
pull: always
|
||||
image: golang:1.13-alpine
|
||||
commands:
|
||||
- apk add git make curl perl bash build-base zlib-dev ucl-dev
|
||||
- make ssh-server
|
||||
- make test
|
||||
- make coverage
|
||||
volumes:
|
||||
- name: gopath
|
||||
path: /go
|
||||
|
||||
- name: codecov
|
||||
pull: always
|
||||
image: robertstettner/drone-codecov
|
||||
settings:
|
||||
token:
|
||||
from_secret: codecov_token
|
||||
|
||||
volumes:
|
||||
- name: gopath
|
||||
temp: {}
|
||||
|
||||
---
|
||||
kind: pipeline
|
||||
name: linux-amd64
|
||||
|
||||
platform:
|
||||
os: linux
|
||||
arch: amd64
|
||||
|
||||
steps:
|
||||
- name: build-push
|
||||
pull: always
|
||||
image: golang:1.13
|
||||
commands:
|
||||
- go build -v -ldflags '-X main.build=${DRONE_BUILD_NUMBER}' -a -o release/linux/amd64/drone-scp
|
||||
environment:
|
||||
CGO_ENABLED: 0
|
||||
when:
|
||||
event:
|
||||
exclude:
|
||||
- tag
|
||||
|
||||
- name: build-tag
|
||||
pull: always
|
||||
image: golang:1.13
|
||||
commands:
|
||||
- go build -v -ldflags '-X main.version=${DRONE_TAG##v} -X main.build=${DRONE_BUILD_NUMBER}' -a -o release/linux/amd64/drone-scp
|
||||
environment:
|
||||
CGO_ENABLED: 0
|
||||
when:
|
||||
event:
|
||||
- tag
|
||||
|
||||
- name: executable
|
||||
pull: always
|
||||
image: golang:1.13
|
||||
commands:
|
||||
- ./release/linux/amd64/drone-scp --help
|
||||
|
||||
- name: dryrun
|
||||
pull: always
|
||||
image: plugins/docker:linux-amd64
|
||||
settings:
|
||||
cache_from: appleboy/drone-scp
|
||||
dockerfile: docker/Dockerfile.linux.amd64
|
||||
dry_run: true
|
||||
repo: appleboy/drone-scp
|
||||
tags: linux-amd64
|
||||
when:
|
||||
event:
|
||||
- pull_request
|
||||
|
||||
- name: publish
|
||||
pull: always
|
||||
image: plugins/docker:linux-amd64
|
||||
settings:
|
||||
auto_tag: true
|
||||
auto_tag_suffix: linux-amd64
|
||||
cache_from: appleboy/drone-scp
|
||||
daemon_off: false
|
||||
dockerfile: docker/Dockerfile.linux.amd64
|
||||
password:
|
||||
from_secret: docker_password
|
||||
repo: appleboy/drone-scp
|
||||
username:
|
||||
from_secret: docker_username
|
||||
when:
|
||||
event:
|
||||
exclude:
|
||||
- pull_request
|
||||
|
||||
trigger:
|
||||
ref:
|
||||
- refs/heads/master
|
||||
- refs/pull/**
|
||||
- refs/tags/**
|
||||
|
||||
depends_on:
|
||||
- testing
|
||||
|
||||
---
|
||||
kind: pipeline
|
||||
name: linux-arm64
|
||||
|
||||
platform:
|
||||
os: linux
|
||||
arch: arm64
|
||||
|
||||
steps:
|
||||
- name: build-push
|
||||
pull: always
|
||||
image: golang:1.13
|
||||
commands:
|
||||
- go build -v -ldflags '-X main.build=${DRONE_BUILD_NUMBER}' -a -o release/linux/arm64/drone-scp
|
||||
environment:
|
||||
CGO_ENABLED: 0
|
||||
when:
|
||||
event:
|
||||
exclude:
|
||||
- tag
|
||||
|
||||
- name: build-tag
|
||||
pull: always
|
||||
image: golang:1.13
|
||||
commands:
|
||||
- go build -v -ldflags '-X main.version=${DRONE_TAG##v} -X main.build=${DRONE_BUILD_NUMBER}' -a -o release/linux/arm64/drone-scp
|
||||
environment:
|
||||
CGO_ENABLED: 0
|
||||
when:
|
||||
event:
|
||||
- tag
|
||||
|
||||
- name: executable
|
||||
pull: always
|
||||
image: golang:1.13
|
||||
commands:
|
||||
- ./release/linux/arm64/drone-scp --help
|
||||
|
||||
- name: dryrun
|
||||
pull: always
|
||||
image: plugins/docker:linux-arm64
|
||||
settings:
|
||||
cache_from: appleboy/drone-scp
|
||||
dockerfile: docker/Dockerfile.linux.arm64
|
||||
dry_run: true
|
||||
repo: appleboy/drone-scp
|
||||
tags: linux-arm64
|
||||
when:
|
||||
event:
|
||||
- pull_request
|
||||
|
||||
- name: publish
|
||||
pull: always
|
||||
image: plugins/docker:linux-arm64
|
||||
settings:
|
||||
auto_tag: true
|
||||
auto_tag_suffix: linux-arm64
|
||||
cache_from: appleboy/drone-scp
|
||||
daemon_off: false
|
||||
dockerfile: docker/Dockerfile.linux.arm64
|
||||
password:
|
||||
from_secret: docker_password
|
||||
repo: appleboy/drone-scp
|
||||
username:
|
||||
from_secret: docker_username
|
||||
when:
|
||||
event:
|
||||
exclude:
|
||||
- pull_request
|
||||
|
||||
trigger:
|
||||
ref:
|
||||
- refs/heads/master
|
||||
- refs/pull/**
|
||||
- refs/tags/**
|
||||
|
||||
depends_on:
|
||||
- testing
|
||||
|
||||
---
|
||||
kind: pipeline
|
||||
name: linux-arm
|
||||
|
||||
platform:
|
||||
os: linux
|
||||
arch: arm
|
||||
|
||||
steps:
|
||||
- name: build-push
|
||||
pull: always
|
||||
image: golang:1.13
|
||||
commands:
|
||||
- go build -v -ldflags '-X main.build=${DRONE_BUILD_NUMBER}' -a -o release/linux/arm/drone-scp
|
||||
environment:
|
||||
CGO_ENABLED: 0
|
||||
when:
|
||||
event:
|
||||
exclude:
|
||||
- tag
|
||||
|
||||
- name: build-tag
|
||||
pull: always
|
||||
image: golang:1.13
|
||||
commands:
|
||||
- go build -v -ldflags '-X main.version=${DRONE_TAG##v} -X main.build=${DRONE_BUILD_NUMBER}' -a -o release/linux/arm/drone-scp
|
||||
environment:
|
||||
CGO_ENABLED: 0
|
||||
when:
|
||||
event:
|
||||
- tag
|
||||
|
||||
- name: executable
|
||||
pull: always
|
||||
image: golang:1.13
|
||||
commands:
|
||||
- ./release/linux/arm/drone-scp --help
|
||||
|
||||
- name: dryrun
|
||||
pull: always
|
||||
image: plugins/docker:linux-arm
|
||||
settings:
|
||||
cache_from: appleboy/drone-scp
|
||||
dockerfile: docker/Dockerfile.linux.arm
|
||||
dry_run: true
|
||||
repo: appleboy/drone-scp
|
||||
tags: linux-arm
|
||||
when:
|
||||
event:
|
||||
- pull_request
|
||||
|
||||
- name: publish
|
||||
pull: always
|
||||
image: plugins/docker:linux-arm
|
||||
settings:
|
||||
auto_tag: true
|
||||
auto_tag_suffix: linux-arm
|
||||
cache_from: appleboy/drone-scp
|
||||
daemon_off: false
|
||||
dockerfile: docker/Dockerfile.linux.arm
|
||||
password:
|
||||
from_secret: docker_password
|
||||
repo: appleboy/drone-scp
|
||||
username:
|
||||
from_secret: docker_username
|
||||
when:
|
||||
event:
|
||||
exclude:
|
||||
- pull_request
|
||||
|
||||
trigger:
|
||||
ref:
|
||||
- refs/heads/master
|
||||
- refs/pull/**
|
||||
- refs/tags/**
|
||||
|
||||
depends_on:
|
||||
- testing
|
||||
|
||||
---
|
||||
kind: pipeline
|
||||
name: release-binary
|
||||
|
||||
platform:
|
||||
os: linux
|
||||
arch: amd64
|
||||
|
||||
steps:
|
||||
- name: build-all-binary
|
||||
pull: always
|
||||
image: golang:1.13
|
||||
commands:
|
||||
- make release
|
||||
when:
|
||||
event:
|
||||
- tag
|
||||
|
||||
- name: deploy-all-binary
|
||||
pull: always
|
||||
image: plugins/github-release
|
||||
settings:
|
||||
api_key:
|
||||
from_secret: github_release_api_key
|
||||
files:
|
||||
- dist/release/*
|
||||
when:
|
||||
event:
|
||||
- tag
|
||||
|
||||
trigger:
|
||||
ref:
|
||||
- refs/tags/**
|
||||
|
||||
depends_on:
|
||||
- testing
|
||||
|
||||
---
|
||||
kind: pipeline
|
||||
name: notifications
|
||||
|
||||
platform:
|
||||
os: linux
|
||||
arch: amd64
|
||||
|
||||
steps:
|
||||
- name: manifest
|
||||
pull: always
|
||||
image: plugins/manifest
|
||||
settings:
|
||||
ignore_missing: true
|
||||
password:
|
||||
from_secret: docker_password
|
||||
spec: docker/manifest.tmpl
|
||||
username:
|
||||
from_secret: docker_username
|
||||
|
||||
trigger:
|
||||
ref:
|
||||
- refs/heads/master
|
||||
- refs/tags/**
|
||||
|
||||
depends_on:
|
||||
- linux-amd64
|
||||
- linux-arm64
|
||||
- linux-arm
|
||||
- release-binary
|
||||
|
||||
...
|
||||
+4
-1
@@ -22,6 +22,9 @@ _testmain.go
|
||||
*.exe
|
||||
*.test
|
||||
*.prof
|
||||
vendor
|
||||
drone-scp
|
||||
coverage.txt
|
||||
.env
|
||||
dist
|
||||
.cover
|
||||
release
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
ignoreGeneratedHeader = false
|
||||
severity = "warning"
|
||||
confidence = 0.8
|
||||
errorCode = 1
|
||||
warningCode = 1
|
||||
|
||||
[rule.blank-imports]
|
||||
[rule.context-as-argument]
|
||||
[rule.context-keys-type]
|
||||
[rule.dot-imports]
|
||||
[rule.error-return]
|
||||
[rule.error-strings]
|
||||
[rule.error-naming]
|
||||
[rule.exported]
|
||||
[rule.if-return]
|
||||
[rule.increment-decrement]
|
||||
[rule.var-naming]
|
||||
[rule.var-declaration]
|
||||
[rule.package-comments]
|
||||
[rule.range]
|
||||
[rule.receiver-naming]
|
||||
[rule.time-naming]
|
||||
[rule.unexported-return]
|
||||
[rule.indent-error-flow]
|
||||
[rule.errorf]
|
||||
-53
@@ -1,53 +0,0 @@
|
||||
sudo: required
|
||||
language: go
|
||||
|
||||
services:
|
||||
- docker
|
||||
|
||||
go:
|
||||
- 1.5.4
|
||||
- 1.6.3
|
||||
- 1.7.3
|
||||
- tip
|
||||
|
||||
env:
|
||||
global:
|
||||
- DOCKER_CACHE_FILE=${HOME}/docker/cache.tar.gz
|
||||
|
||||
cache:
|
||||
directories:
|
||||
- vendor
|
||||
- ${HOME}/.glide
|
||||
- ${HOME}/docker
|
||||
|
||||
before_install:
|
||||
- mkdir -p $GOPATH/bin
|
||||
- curl https://glide.sh/get | sh
|
||||
- if [ -f ${DOCKER_CACHE_FILE} ]; then gunzip -c ${DOCKER_CACHE_FILE} | docker load; fi
|
||||
|
||||
install:
|
||||
- export GO15VENDOREXPERIMENT=1
|
||||
- make install
|
||||
|
||||
script:
|
||||
- make test
|
||||
- make docker
|
||||
- if [ "$TRAVIS_BRANCH" == "master" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; then
|
||||
mkdir -p $(dirname ${DOCKER_CACHE_FILE});
|
||||
docker save $(docker history -q $TRAVIS_REPO_SLUG:latest | grep -v '<missing>') | gzip > ${DOCKER_CACHE_FILE};
|
||||
fi
|
||||
|
||||
after_success:
|
||||
# ignore main.go coverage
|
||||
- sed -i '/main.go/d' coverage.txt
|
||||
- bash <(curl -s https://codecov.io/bash) -f coverage.txt
|
||||
# deploy from master
|
||||
- if [ "$TRAVIS_BRANCH" == "master" ] && [ "$TRAVIS_GO_VERSION" == "1.7.3" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; then
|
||||
docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD";
|
||||
make docker_deploy tag=latest;
|
||||
fi
|
||||
# deploy from tag
|
||||
- if [ "$TRAVIS_GO_VERSION" == "1.7.3" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_TAG" != "" ]; then
|
||||
docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD";
|
||||
make docker_deploy tag=$TRAVIS_TAG;
|
||||
fi
|
||||
@@ -0,0 +1,286 @@
|
||||
---
|
||||
date: 2017-01-06T00:00:00+00:00
|
||||
title: SCP
|
||||
author: appleboy
|
||||
tags: [ publish, ssh, scp ]
|
||||
logo: term.svg
|
||||
repo: appleboy/drone-scp
|
||||
image: appleboy/drone-scp
|
||||
---
|
||||
|
||||
The SCP plugin copy files and artifacts to target host machine via SSH. The below pipeline configuration demonstrates simple usage:
|
||||
|
||||
```yaml
|
||||
pipeline:
|
||||
scp:
|
||||
image: appleboy/drone-scp
|
||||
settings:
|
||||
host: example.com
|
||||
target: /home/deploy/web
|
||||
source: release.tar.gz
|
||||
```
|
||||
|
||||
Example configuration with custom username, password and port:
|
||||
|
||||
```diff
|
||||
pipeline:
|
||||
scp:
|
||||
image: appleboy/drone-scp
|
||||
settings:
|
||||
host: example.com
|
||||
+ username: appleboy
|
||||
+ password: 12345678
|
||||
+ port: 4430
|
||||
target: /home/deploy/web
|
||||
source: release.tar.gz
|
||||
```
|
||||
|
||||
Example configuration with multiple source and target folder:
|
||||
|
||||
```diff
|
||||
pipeline:
|
||||
scp:
|
||||
image: appleboy/drone-scp
|
||||
settings:
|
||||
host: example.com
|
||||
target:
|
||||
+ - /home/deploy/web1
|
||||
+ - /home/deploy/web2
|
||||
source:
|
||||
+ - release_1.tar.gz
|
||||
+ - release_2.tar.gz
|
||||
```
|
||||
|
||||
Example configuration with multiple host:
|
||||
|
||||
```diff
|
||||
pipeline:
|
||||
scp:
|
||||
image: appleboy/drone-scp
|
||||
settings:
|
||||
- host: example.com
|
||||
+ host:
|
||||
+ - example1.com
|
||||
+ - example2.com
|
||||
target: /home/deploy/web
|
||||
source: release.tar.gz
|
||||
```
|
||||
|
||||
Example configuration with wildcard pattern of source list:
|
||||
|
||||
```diff
|
||||
pipeline:
|
||||
scp:
|
||||
image: appleboy/drone-scp
|
||||
settings:
|
||||
host:
|
||||
- example1.com
|
||||
- example2.com
|
||||
target: /home/deploy/web
|
||||
source:
|
||||
- - release/backend.tar.gz
|
||||
- - release/images.tar.gz
|
||||
+ - release/*.tar.gz
|
||||
```
|
||||
|
||||
Remove target folder before copy files and artifacts to target:
|
||||
|
||||
```diff
|
||||
scp:
|
||||
image: appleboy/drone-scp
|
||||
host: example.com
|
||||
settings:
|
||||
target: /home/deploy/web
|
||||
source: release.tar.gz
|
||||
+ rm: true
|
||||
```
|
||||
|
||||
Example for remove the specified number of leading path elements:
|
||||
|
||||
```diff
|
||||
scp:
|
||||
image: appleboy/drone-scp
|
||||
settings:
|
||||
host: example.com
|
||||
target: /home/deploy/web
|
||||
source: dist/release.tar.gz
|
||||
+ strip_components: 1
|
||||
```
|
||||
|
||||
Example configuration using `SSHProxyCommand`:
|
||||
|
||||
```diff
|
||||
pipeline:
|
||||
scp:
|
||||
image: appleboy/drone-scp
|
||||
settings:
|
||||
host:
|
||||
- example1.com
|
||||
- example2.com
|
||||
target: /home/deploy/web
|
||||
source:
|
||||
- release/*.tar.gz
|
||||
+ proxy_host: 10.130.33.145
|
||||
+ proxy_user: ubuntu
|
||||
+ proxy_port: 22
|
||||
+ proxy_password: 1234
|
||||
```
|
||||
|
||||
Example configuration using password from secrets:
|
||||
|
||||
```diff
|
||||
pipeline:
|
||||
scp:
|
||||
image: appleboy/drone-scp
|
||||
settings:
|
||||
host:
|
||||
- example1.com
|
||||
- example2.com
|
||||
user: ubuntu
|
||||
port: 22
|
||||
- password: 1234
|
||||
+ password:
|
||||
+ from_secret: ssh_password
|
||||
target: /home/deploy/web
|
||||
source:
|
||||
- release/*.tar.gz
|
||||
```
|
||||
|
||||
Example configuration using command timeout:
|
||||
|
||||
```diff
|
||||
pipeline:
|
||||
scp:
|
||||
image: appleboy/drone-scp
|
||||
settings:
|
||||
host:
|
||||
- example1.com
|
||||
- example2.com
|
||||
user: ubuntu
|
||||
password:
|
||||
from_secret: ssh_password
|
||||
port: 22
|
||||
- command_timeout: 120
|
||||
+ command_timeout: 2m
|
||||
target: /home/deploy/web
|
||||
source:
|
||||
- release/*.tar.gz
|
||||
```
|
||||
|
||||
Example configuration for ignore list:
|
||||
|
||||
```diff
|
||||
pipeline:
|
||||
scp:
|
||||
image: appleboy/drone-scp
|
||||
settings:
|
||||
host:
|
||||
- example1.com
|
||||
- example2.com
|
||||
user: ubuntu
|
||||
password:
|
||||
from_secret: ssh_password
|
||||
port: 22
|
||||
command_timeout: 2m
|
||||
target: /home/deploy/web
|
||||
source:
|
||||
+ - !release/README.md
|
||||
- release/*
|
||||
```
|
||||
|
||||
## Parameter Reference
|
||||
|
||||
host
|
||||
: target hostname or IP
|
||||
|
||||
port
|
||||
: ssh port of target host
|
||||
|
||||
username
|
||||
: account for target host user
|
||||
|
||||
password
|
||||
: password for target host user
|
||||
|
||||
key
|
||||
: plain text of user private key
|
||||
|
||||
target
|
||||
: folder path of target host
|
||||
|
||||
source
|
||||
: source lists you want to copy
|
||||
|
||||
rm
|
||||
: remove target folder before copy files and artifacts
|
||||
|
||||
timeout
|
||||
: timeout is the maximum amount of time for the TCP connection to establish
|
||||
|
||||
command_timeout
|
||||
: timeout is the maximum amount of time for execute command
|
||||
|
||||
strip_components
|
||||
: remove the specified number of leading path elements
|
||||
|
||||
tar_tmp_path
|
||||
: temporary path for tar file on the dest host
|
||||
|
||||
tar_exec
|
||||
: alternative `tar` executable to on the dest host
|
||||
|
||||
overwrite
|
||||
: use `--overwrite` flag with tar
|
||||
|
||||
proxy_host
|
||||
: proxy hostname or IP
|
||||
|
||||
proxy_port
|
||||
: ssh port of proxy host
|
||||
|
||||
proxy_username
|
||||
: account for proxy host user
|
||||
|
||||
proxy_password
|
||||
: password for proxy host user
|
||||
|
||||
proxy_key
|
||||
: plain text of proxy private key
|
||||
|
||||
proxy_key_path
|
||||
: key path of proxy private key
|
||||
|
||||
## Template Reference
|
||||
|
||||
repo.owner
|
||||
: repository owner
|
||||
|
||||
repo.name
|
||||
: repository name
|
||||
|
||||
build.status
|
||||
: build status type enumeration, either `success` or `failure`
|
||||
|
||||
build.event
|
||||
: build event type enumeration, one of `push`, `pull_request`, `tag`, `deployment`
|
||||
|
||||
build.number
|
||||
: build number
|
||||
|
||||
build.commit
|
||||
: git sha for current commit
|
||||
|
||||
build.branch
|
||||
: git branch for current commit
|
||||
|
||||
build.tag
|
||||
: git tag for current commit
|
||||
|
||||
build.ref
|
||||
: git ref for current commit
|
||||
|
||||
build.author
|
||||
: git author for current commit
|
||||
|
||||
build.link
|
||||
: link the the build results in drone
|
||||
@@ -1,9 +0,0 @@
|
||||
FROM alpine:3.4
|
||||
|
||||
RUN apk update && \
|
||||
apk add ca-certificates && \
|
||||
rm -rf /var/cache/apk/*
|
||||
|
||||
ADD drone-scp /
|
||||
|
||||
ENTRYPOINT ["/drone-scp"]
|
||||
@@ -1,8 +0,0 @@
|
||||
FROM armhfbuild/alpine:3.4
|
||||
|
||||
RUN apk update && \
|
||||
apk add ca-certificates && \
|
||||
rm -rf /var/cache/apk/*
|
||||
|
||||
ADD drone-scp /bin/
|
||||
ENTRYPOINT ["/bin/drone-scp"]
|
||||
@@ -1,8 +1,18 @@
|
||||
.PHONY: install build test html update docker_build docker_image docker docker_deploy clean
|
||||
DIST := dist
|
||||
EXECUTABLE := drone-scp
|
||||
GOFMT ?= gofmt "-s"
|
||||
GO ?= go
|
||||
|
||||
VERSION := $(shell git describe --tags || git rev-parse --short HEAD)
|
||||
DEPLOY_ACCOUNT := "appleboy"
|
||||
DEPLOY_IMAGE := "drone-scp"
|
||||
# for dockerhub
|
||||
DEPLOY_ACCOUNT := appleboy
|
||||
DEPLOY_IMAGE := $(EXECUTABLE)
|
||||
|
||||
TARGETS ?= linux darwin windows
|
||||
ARCHS ?= amd64 386
|
||||
PACKAGES ?= $(shell $(GO) list ./...)
|
||||
SOURCES ?= $(shell find . -name "*.go" -type f)
|
||||
TAGS ?=
|
||||
LDFLAGS ?= -X 'main.Version=$(VERSION)'
|
||||
|
||||
ifneq ($(shell uname), Darwin)
|
||||
EXTLDFLAGS = -extldflags "-static" $(null)
|
||||
@@ -10,36 +20,122 @@ else
|
||||
EXTLDFLAGS =
|
||||
endif
|
||||
|
||||
install:
|
||||
glide install
|
||||
ifneq ($(DRONE_TAG),)
|
||||
VERSION ?= $(DRONE_TAG)
|
||||
else
|
||||
VERSION ?= $(shell git describe --tags --always || git rev-parse --short HEAD)
|
||||
endif
|
||||
|
||||
build:
|
||||
go build -ldflags="$(EXTLDFLAGS)-s -w -X main.Version=$(VERSION)"
|
||||
all: build
|
||||
|
||||
test:
|
||||
go test -v -coverprofile=coverage.txt
|
||||
fmt:
|
||||
$(GOFMT) -w $(SOURCES)
|
||||
|
||||
html:
|
||||
go tool cover -html=coverage.txt
|
||||
vet:
|
||||
$(GO) vet $(PACKAGES)
|
||||
|
||||
update:
|
||||
glide up
|
||||
lint:
|
||||
@hash revive > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
||||
$(GO) get -u github.com/mgechev/revive; \
|
||||
fi
|
||||
revive -config .revive.toml ./... || exit 1
|
||||
|
||||
docker_build:
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -tags netgo -ldflags="-X main.Version=$(VERSION)"
|
||||
.PHONY: misspell-check
|
||||
misspell-check:
|
||||
@hash misspell > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
||||
$(GO) get -u github.com/client9/misspell/cmd/misspell; \
|
||||
fi
|
||||
misspell -error $(SOURCES)
|
||||
|
||||
.PHONY: misspell
|
||||
misspell:
|
||||
@hash misspell > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
||||
$(GO) get -u github.com/client9/misspell/cmd/misspell; \
|
||||
fi
|
||||
misspell -w $(SOURCES)
|
||||
|
||||
.PHONY: fmt-check
|
||||
fmt-check:
|
||||
@diff=$$($(GOFMT) -d $(SOURCES)); \
|
||||
if [ -n "$$diff" ]; then \
|
||||
echo "Please run 'make fmt' and commit the result:"; \
|
||||
echo "$${diff}"; \
|
||||
exit 1; \
|
||||
fi;
|
||||
|
||||
test: fmt-check
|
||||
@$(GO) test -v -cover -coverprofile coverage.txt $(PACKAGES) && echo "\n==>\033[32m Ok\033[m\n" || exit 1
|
||||
|
||||
install: $(SOURCES)
|
||||
$(GO) install -v -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)'
|
||||
|
||||
build: $(EXECUTABLE)
|
||||
|
||||
$(EXECUTABLE): $(SOURCES)
|
||||
$(GO) build -v -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)' -o $@
|
||||
|
||||
release: release-dirs release-build release-copy release-check
|
||||
|
||||
release-dirs:
|
||||
mkdir -p $(DIST)/binaries $(DIST)/release
|
||||
|
||||
release-build:
|
||||
@which gox > /dev/null; if [ $$? -ne 0 ]; then \
|
||||
$(GO) get -u github.com/mitchellh/gox; \
|
||||
fi
|
||||
gox -os="$(TARGETS)" -arch="$(ARCHS)" -tags="$(TAGS)" -ldflags="-s -w $(LDFLAGS)" -output="$(DIST)/binaries/$(EXECUTABLE)-$(VERSION)-{{.OS}}-{{.Arch}}"
|
||||
|
||||
release-copy:
|
||||
$(foreach file,$(wildcard $(DIST)/binaries/$(EXECUTABLE)-*),cp $(file) $(DIST)/release/$(notdir $(file));)
|
||||
|
||||
release-check:
|
||||
cd $(DIST)/release; $(foreach file,$(wildcard $(DIST)/release/$(EXECUTABLE)-*),sha256sum $(notdir $(file)) > $(notdir $(file)).sha256;)
|
||||
|
||||
build_linux_amd64:
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GO) build -a -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)' -o release/linux/amd64/$(DEPLOY_IMAGE)
|
||||
|
||||
build_linux_i386:
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=386 $(GO) build -a -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)' -o release/linux/i386/$(DEPLOY_IMAGE)
|
||||
|
||||
build_linux_arm64:
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 $(GO) build -a -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)' -o release/linux/arm64/$(DEPLOY_IMAGE)
|
||||
|
||||
build_linux_arm:
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=7 $(GO) build -a -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)' -o release/linux/arm/$(DEPLOY_IMAGE)
|
||||
|
||||
docker_image:
|
||||
docker build --rm -t $(DEPLOY_ACCOUNT)/$(DEPLOY_IMAGE) .
|
||||
docker build -t $(DEPLOY_ACCOUNT)/$(DEPLOY_IMAGE) .
|
||||
|
||||
docker: docker_build docker_image
|
||||
docker: docker_image
|
||||
|
||||
docker_deploy:
|
||||
ifeq ($(tag),)
|
||||
@echo "Usage: make $@ tag=<tag>"
|
||||
@exit 1
|
||||
endif
|
||||
# deploy image
|
||||
docker tag $(DEPLOY_ACCOUNT)/$(DEPLOY_IMAGE):latest $(DEPLOY_ACCOUNT)/$(DEPLOY_IMAGE):$(tag)
|
||||
docker push $(DEPLOY_ACCOUNT)/$(DEPLOY_IMAGE):$(tag)
|
||||
|
||||
ssh-server:
|
||||
adduser -h /home/drone-scp -s /bin/bash -D -S drone-scp
|
||||
echo drone-scp:1234 | chpasswd
|
||||
mkdir -p /home/drone-scp/.ssh
|
||||
chmod 700 /home/drone-scp/.ssh
|
||||
cp tests/.ssh/id_rsa.pub /home/drone-scp/.ssh/authorized_keys
|
||||
chown -R drone-scp /home/drone-scp/.ssh
|
||||
# install ssh and start server
|
||||
apk add --update openssh openrc
|
||||
rm -rf /etc/ssh/ssh_host_rsa_key /etc/ssh/ssh_host_dsa_key
|
||||
sed -i 's/AllowTcpForwarding no/AllowTcpForwarding yes/g' /etc/ssh/sshd_config
|
||||
./tests/entrypoint.sh /usr/sbin/sshd -D &
|
||||
|
||||
coverage:
|
||||
sed -i '/main.go/d' coverage.txt
|
||||
|
||||
clean:
|
||||
rm -rf coverage.txt ${DEPLOY_IMAGE}
|
||||
$(GO) clean -x -i ./...
|
||||
rm -rf coverage.txt $(EXECUTABLE) $(DIST)
|
||||
|
||||
version:
|
||||
@echo $(VERSION)
|
||||
|
||||
@@ -1,63 +1,262 @@
|
||||
# drone-scp
|
||||
|
||||
[](https://travis-ci.org/appleboy/drone-scp) [](https://codecov.io/gh/appleboy/drone-scp) [](https://goreportcard.com/report/github.com/appleboy/drone-scp)
|
||||
[](https://godoc.org/github.com/appleboy/drone-scp)
|
||||
[](https://cloud.drone.io/appleboy/drone-scp)
|
||||
[](https://codecov.io/gh/appleboy/drone-scp)
|
||||
[](https://goreportcard.com/report/github.com/appleboy/drone-scp)
|
||||
[](https://hub.docker.com/r/appleboy/drone-scp/)
|
||||
[](https://microbadger.com/images/appleboy/drone-scp "Get your own image badge on microbadger.com")
|
||||
|
||||
[Drone](https://github.com/drone/drone) plugin to copy files and artifacts via SSH.
|
||||
Copy files and artifacts via SSH using a binary, docker or [Drone CI](http://docs.drone.io/).
|
||||
|
||||
## Build
|
||||
## Feature
|
||||
|
||||
Build the binary with the following commands:
|
||||
* [x] Support routines.
|
||||
* [x] Support wildcard pattern on source list.
|
||||
* [x] Support send files to multiple host.
|
||||
* [x] Support send files to multiple target folder on host.
|
||||
* [x] Support load ssh key from absolute path or raw body.
|
||||
* [x] Support SSH ProxyCommand.
|
||||
|
||||
```
|
||||
$ make build
|
||||
```sh
|
||||
+--------+ +----------+ +-----------+
|
||||
| Laptop | <--> | Jumphost | <--> | FooServer |
|
||||
+--------+ +----------+ +-----------+
|
||||
|
||||
OR
|
||||
|
||||
+--------+ +----------+ +-----------+
|
||||
| Laptop | <--> | Firewall | <--> | FooServer |
|
||||
+--------+ +----------+ +-----------+
|
||||
192.168.1.5 121.1.2.3 10.10.29.68
|
||||
```
|
||||
|
||||
## Testing
|
||||
## Breaking changes
|
||||
|
||||
Test the package with the following command:
|
||||
`v1.5.0`: change command timeout flag to `Duration`. See the following setting:
|
||||
|
||||
```diff
|
||||
pipeline:
|
||||
scp:
|
||||
image: appleboy/drone-scp
|
||||
settings:
|
||||
host:
|
||||
- example1.com
|
||||
- example2.com
|
||||
username: ubuntu
|
||||
password:
|
||||
from_secret: ssh_password
|
||||
port: 22
|
||||
- command_timeout: 120
|
||||
+ command_timeout: 2m
|
||||
target: /home/deploy/web
|
||||
source:
|
||||
- release/*.tar.gz
|
||||
```
|
||||
$ make test
|
||||
|
||||
## Build or Download a binary
|
||||
|
||||
The pre-compiled binaries can be downloaded from [release page](https://github.com/appleboy/drone-scp/releases). Support the following OS type.
|
||||
|
||||
* Windows amd64/386
|
||||
* Linux arm/amd64/386
|
||||
* Darwin amd64/386
|
||||
|
||||
With `Go` installed
|
||||
|
||||
```sh
|
||||
go get -u -v github.com/appleboy/drone-scp
|
||||
```
|
||||
|
||||
or build the binary with the following command:
|
||||
|
||||
```sh
|
||||
export GOOS=linux
|
||||
export GOARCH=amd64
|
||||
export CGO_ENABLED=0
|
||||
export GO111MODULE=on
|
||||
|
||||
go test -cover ./...
|
||||
|
||||
go build -v -a -tags netgo -o release/linux/amd64/drone-scp .
|
||||
```
|
||||
|
||||
## Docker
|
||||
|
||||
Build the docker image with the following commands:
|
||||
|
||||
```
|
||||
$ make docker
|
||||
```
|
||||
|
||||
Please note incorrectly building the image for the correct x64 linux and with
|
||||
GCO disabled will result in an error when running the Docker image:
|
||||
|
||||
```
|
||||
docker: Error response from daemon: Container command
|
||||
'/bin/drone-scp' not found or does not exist..
|
||||
```sh
|
||||
make docker
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Execute from the working directory:
|
||||
There are three ways to send notification.
|
||||
|
||||
* [usage from binary](#usage-from-binary)
|
||||
* [usage from docker](#usage-from-docker)
|
||||
* [usage from drone ci](#usage-from-drone-ci)
|
||||
|
||||
### Usage from binary
|
||||
|
||||
#### Using public key
|
||||
|
||||
```bash
|
||||
drone-scp --host example.com \
|
||||
--port 22 \
|
||||
--username appleboy \
|
||||
--key-path "${HOME}/.ssh/id_rsa" \
|
||||
--target /home/appleboy/test \
|
||||
--source your_local_folder_path
|
||||
```
|
||||
|
||||
#### Using password
|
||||
|
||||
```diff
|
||||
drone-scp --host example.com \
|
||||
--port 22 \
|
||||
--username appleboy \
|
||||
+ --password xxxxxxx \
|
||||
--target /home/appleboy/test \
|
||||
--source your_local_folder_path
|
||||
```
|
||||
|
||||
#### Using ssh-agent
|
||||
|
||||
Start your local ssh agent:
|
||||
|
||||
```bash
|
||||
eval `ssh-agent -s`
|
||||
```
|
||||
|
||||
Import your local public key `~/.ssh/id_rsa`
|
||||
|
||||
```sh
|
||||
ssh-add
|
||||
```
|
||||
|
||||
You don't need to add `--password` or `--key-path` arguments.
|
||||
|
||||
```bash
|
||||
drone-scp --host example.com \
|
||||
--port 22 \
|
||||
--username appleboy \
|
||||
--target /home/appleboy/test \
|
||||
--source your_local_folder_path
|
||||
```
|
||||
|
||||
#### Send multiple source or target folder and hosts
|
||||
|
||||
```diff
|
||||
drone-scp --host example1.com \
|
||||
+ --host example2.com \
|
||||
--port 22 \
|
||||
--username appleboy \
|
||||
--password xxxxxxx
|
||||
--target /home/appleboy/test1 \
|
||||
+ --target /home/appleboy/test2 \
|
||||
--source your_local_folder_path_1
|
||||
+ --source your_local_folder_path_2
|
||||
```
|
||||
|
||||
### Usage from docker
|
||||
|
||||
Using public key
|
||||
|
||||
```bash
|
||||
docker run --rm \
|
||||
-e PLUGIN_HOST=http://example.com \
|
||||
-e PLUGIN_USERNAME=xxxxxxx \
|
||||
-e PLUGIN_PASSWORD=xxxxxxx \
|
||||
-e PLUGIN_PORT=xxxxxxx \
|
||||
-e PLUGIN_KEY="$(cat ${HOME}/.ssh/id_rsa)"
|
||||
-e PLUGIN_SOURCE=SOURCE_FILE_LIST \
|
||||
-e PLUGIN_TARGET=TARGET_FOLDER_PATH \
|
||||
-e DRONE_REPO_OWNER=appleboy \
|
||||
-e DRONE_REPO_NAME=go-hello \
|
||||
-e DRONE_COMMIT_SHA=e5e82b5eb3737205c25955dcc3dcacc839b7be52 \
|
||||
-e DRONE_COMMIT_BRANCH=master \
|
||||
-e DRONE_COMMIT_AUTHOR=appleboy \
|
||||
-e DRONE_BUILD_NUMBER=1 \
|
||||
-e DRONE_BUILD_STATUS=success \
|
||||
-e DRONE_BUILD_LINK=http://github.com/appleboy/go-hello \
|
||||
-e SCP_HOST=example.com \
|
||||
-e SCP_USERNAME=xxxxxxx \
|
||||
-e SCP_PORT=22 \
|
||||
-e SCP_KEY_PATH="${HOME}/.ssh/id_rsa"
|
||||
-e SCP_SOURCE=SOURCE_FILE_LIST \
|
||||
-e SCP_TARGET=TARGET_FOLDER_PATH \
|
||||
-v $(pwd):$(pwd) \
|
||||
-w $(pwd) \
|
||||
appleboy/drone-scp
|
||||
```
|
||||
|
||||
Using password
|
||||
|
||||
```diff
|
||||
docker run --rm \
|
||||
-e SCP_HOST=example.com \
|
||||
-e SCP_USERNAME=xxxxxxx \
|
||||
-e SCP_PORT=22 \
|
||||
+ -e SCP_PASSWORD="xxxxxxx"
|
||||
-e SCP_SOURCE=SOURCE_FILE_LIST \
|
||||
-e SCP_TARGET=TARGET_FOLDER_PATH \
|
||||
-v $(pwd):$(pwd) \
|
||||
-w $(pwd) \
|
||||
appleboy/drone-scp
|
||||
```
|
||||
|
||||
Using ssh-agent, start your local ssh agent:
|
||||
|
||||
```bash
|
||||
eval `ssh-agent -s`
|
||||
```
|
||||
|
||||
Import your local public key `~/.ssh/id_rsa`
|
||||
|
||||
```sh
|
||||
ssh-add
|
||||
```
|
||||
|
||||
You don't need to add `SCP_PASSWORD` or `SCP_KEY_PATH` arguments.
|
||||
|
||||
```bash
|
||||
docker run --rm \
|
||||
-e SCP_HOST=example.com \
|
||||
-e SCP_USERNAME=xxxxxxx \
|
||||
-e SCP_PORT=22 \
|
||||
-e SCP_SOURCE=SOURCE_FILE_LIST \
|
||||
-e SCP_TARGET=TARGET_FOLDER_PATH \
|
||||
-v $(pwd):$(pwd) \
|
||||
-w $(pwd) \
|
||||
appleboy/drone-scp
|
||||
```
|
||||
|
||||
Send multiple source or target folder and hosts
|
||||
|
||||
```bash
|
||||
docker run --rm \
|
||||
-e SCP_HOST=example1.com,example2.com \
|
||||
-e SCP_USERNAME=xxxxxxx \
|
||||
-e SCP_PASSWORD=xxxxxxx \
|
||||
-e SCP_PORT=22 \
|
||||
-e SCP_SOURCE=SOURCE_FILE_LIST_1,SOURCE_FILE_LIST_2 \
|
||||
-e SCP_TARGET=TARGET_FOLDER_PATH_1,TARGET_FOLDER_PATH_2 \
|
||||
-v $(pwd):$(pwd) \
|
||||
-w $(pwd) \
|
||||
appleboy/drone-scp
|
||||
```
|
||||
|
||||
### Usage from drone ci
|
||||
|
||||
Execute from the working directory:
|
||||
|
||||
```bash
|
||||
docker run --rm \
|
||||
-e PLUGIN_HOST=example.com \
|
||||
-e PLUGIN_USERNAME=xxxxxxx \
|
||||
-e PLUGIN_PASSWORD=xxxxxxx \
|
||||
-e PLUGIN_PORT=xxxxxxx \
|
||||
-e PLUGIN_SOURCE=SOURCE_FILE_LIST \
|
||||
-e PLUGIN_TARGET=TARGET_FOLDER_PATH \
|
||||
-e PLUGIN_RM=false \
|
||||
-e PLUGIN_DEBUG=true \
|
||||
-v $(pwd):$(pwd) \
|
||||
-w $(pwd) \
|
||||
appleboy/drone-scp
|
||||
```
|
||||
|
||||
You can get more [information](http://plugins.drone.io/appleboy/drone-scp/) about how to use scp in drone.
|
||||
|
||||
## Testing
|
||||
|
||||
Test the package with the following command:
|
||||
|
||||
```sh
|
||||
make test
|
||||
```
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
FROM plugins/base:linux-amd64
|
||||
|
||||
LABEL maintainer="Bo-Yi Wu <appleboy.tw@gmail.com>" \
|
||||
org.label-schema.name="Drone SCP" \
|
||||
org.label-schema.vendor="Bo-Yi Wu" \
|
||||
org.label-schema.schema-version="1.0"
|
||||
|
||||
RUN apk add --no-cache ca-certificates && \
|
||||
rm -rf /var/cache/apk/*
|
||||
|
||||
COPY release/linux/amd64/drone-scp /bin/
|
||||
ENTRYPOINT ["/bin/drone-scp"]
|
||||
@@ -0,0 +1,12 @@
|
||||
FROM plugins/base:linux-arm
|
||||
|
||||
LABEL maintainer="Bo-Yi Wu <appleboy.tw@gmail.com>" \
|
||||
org.label-schema.name="Drone SCP" \
|
||||
org.label-schema.vendor="Bo-Yi Wu" \
|
||||
org.label-schema.schema-version="1.0"
|
||||
|
||||
RUN apk add --no-cache ca-certificates && \
|
||||
rm -rf /var/cache/apk/*
|
||||
|
||||
COPY release/linux/arm/drone-scp /bin/
|
||||
ENTRYPOINT ["/bin/drone-scp"]
|
||||
@@ -0,0 +1,12 @@
|
||||
FROM plugins/base:linux-arm64
|
||||
|
||||
LABEL maintainer="Bo-Yi Wu <appleboy.tw@gmail.com>" \
|
||||
org.label-schema.name="Drone SCP" \
|
||||
org.label-schema.vendor="Bo-Yi Wu" \
|
||||
org.label-schema.schema-version="1.0"
|
||||
|
||||
RUN apk add --no-cache ca-certificates && \
|
||||
rm -rf /var/cache/apk/*
|
||||
|
||||
COPY release/linux/arm64/drone-scp /bin/
|
||||
ENTRYPOINT ["/bin/drone-scp"]
|
||||
@@ -0,0 +1,10 @@
|
||||
FROM microsoft/nanoserver:10.0.14393.1884
|
||||
|
||||
LABEL maintainer="Bo-Yi Wu <appleboy.tw@gmail.com>" \
|
||||
org.label-schema.name="Drone SCP" \
|
||||
org.label-schema.vendor="Bo-Yi Wu" \
|
||||
org.label-schema.schema-version="1.0"
|
||||
|
||||
COPY drone-scp.exe /drone-scp.exe
|
||||
|
||||
ENTRYPOINT [ "\\drone-scp.exe" ]
|
||||
@@ -0,0 +1,25 @@
|
||||
image: appleboy/drone-scp:{{#if build.tag}}{{trimPrefix "v" build.tag}}{{else}}latest{{/if}}
|
||||
{{#if build.tags}}
|
||||
tags:
|
||||
{{#each build.tags}}
|
||||
- {{this}}
|
||||
{{/each}}
|
||||
{{/if}}
|
||||
manifests:
|
||||
-
|
||||
image: appleboy/drone-scp:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-amd64
|
||||
platform:
|
||||
architecture: amd64
|
||||
os: linux
|
||||
-
|
||||
image: appleboy/drone-scp:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-arm64
|
||||
platform:
|
||||
architecture: arm64
|
||||
os: linux
|
||||
variant: v8
|
||||
-
|
||||
image: appleboy/drone-scp:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-arm
|
||||
platform:
|
||||
architecture: arm
|
||||
os: linux
|
||||
variant: v7
|
||||
@@ -1,173 +0,0 @@
|
||||
// Package easyssh provides a simple implementation of some SSH protocol
|
||||
// features in Go. You can simply run a command on a remote server or get a file
|
||||
// even simpler than native console SSH client. You don't need to think about
|
||||
// Dials, sessions, defers, or public keys... Let easyssh think about it!
|
||||
package easyssh
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
"golang.org/x/crypto/ssh/agent"
|
||||
)
|
||||
|
||||
// Contains main authority information.
|
||||
// User field should be a name of user on remote server (ex. john in ssh john@example.com).
|
||||
// Server field should be a remote machine address (ex. example.com in ssh john@example.com)
|
||||
// Key is a path to private key on your local machine.
|
||||
// Port is SSH server port on remote machine.
|
||||
// Note: easyssh looking for private key in user's home directory (ex. /home/john + Key).
|
||||
// Then ensure your Key begins from '/' (ex. /.ssh/id_rsa)
|
||||
type MakeConfig struct {
|
||||
User string
|
||||
Server string
|
||||
Key string
|
||||
Port string
|
||||
Password string
|
||||
}
|
||||
|
||||
// connects to remote server using MakeConfig struct and returns *ssh.Session
|
||||
func (ssh_conf *MakeConfig) connect() (*ssh.Session, error) {
|
||||
// auths holds the detected ssh auth methods
|
||||
auths := []ssh.AuthMethod{}
|
||||
|
||||
// figure out what auths are requested, what is supported
|
||||
if ssh_conf.Password != "" {
|
||||
auths = append(auths, ssh.Password(ssh_conf.Password))
|
||||
}
|
||||
|
||||
if sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
|
||||
auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers))
|
||||
defer sshAgent.Close()
|
||||
}
|
||||
|
||||
if ssh_conf.Key != "" {
|
||||
signer, _ := ssh.ParsePrivateKey([]byte(ssh_conf.Key))
|
||||
auths = append(auths, ssh.PublicKeys(signer))
|
||||
}
|
||||
|
||||
config := &ssh.ClientConfig{
|
||||
User: ssh_conf.User,
|
||||
Auth: auths,
|
||||
}
|
||||
|
||||
client, err := ssh.Dial("tcp", ssh_conf.Server+":"+ssh_conf.Port, config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
session, err := client.NewSession()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return session, nil
|
||||
}
|
||||
|
||||
// Stream returns one channel that combines the stdout and stderr of the command
|
||||
// as it is run on the remote machine, and another that sends true when the
|
||||
// command is done. The sessions and channels will then be closed.
|
||||
func (ssh_conf *MakeConfig) Stream(command string) (output chan string, done chan bool, err error) {
|
||||
// connect to remote host
|
||||
session, err := ssh_conf.connect()
|
||||
if err != nil {
|
||||
return output, done, err
|
||||
}
|
||||
// connect to both outputs (they are of type io.Reader)
|
||||
outReader, err := session.StdoutPipe()
|
||||
if err != nil {
|
||||
return output, done, err
|
||||
}
|
||||
errReader, err := session.StderrPipe()
|
||||
if err != nil {
|
||||
return output, done, err
|
||||
}
|
||||
// combine outputs, create a line-by-line scanner
|
||||
outputReader := io.MultiReader(outReader, errReader)
|
||||
err = session.Start(command)
|
||||
scanner := bufio.NewScanner(outputReader)
|
||||
// continuously send the command's output over the channel
|
||||
outputChan := make(chan string)
|
||||
done = make(chan bool)
|
||||
go func(scanner *bufio.Scanner, out chan string, done chan bool) {
|
||||
defer close(outputChan)
|
||||
defer close(done)
|
||||
for scanner.Scan() {
|
||||
outputChan <- scanner.Text()
|
||||
}
|
||||
// close all of our open resources
|
||||
done <- true
|
||||
session.Close()
|
||||
}(scanner, outputChan, done)
|
||||
return outputChan, done, err
|
||||
}
|
||||
|
||||
// Runs command on remote machine and returns its stdout as a string
|
||||
func (ssh_conf *MakeConfig) Run(command string) (outStr string, err error) {
|
||||
outChan, doneChan, err := ssh_conf.Stream(command)
|
||||
if err != nil {
|
||||
return outStr, err
|
||||
}
|
||||
// read from the output channel until the done signal is passed
|
||||
stillGoing := true
|
||||
for stillGoing {
|
||||
select {
|
||||
case <-doneChan:
|
||||
stillGoing = false
|
||||
case line := <-outChan:
|
||||
outStr += line + "\n"
|
||||
}
|
||||
}
|
||||
// return the concatenation of all signals from the output channel
|
||||
return outStr, err
|
||||
}
|
||||
|
||||
// Scp uploads sourceFile to remote machine like native scp console app.
|
||||
func (ssh_conf *MakeConfig) Scp(sourceFile string) error {
|
||||
session, err := ssh_conf.connect()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer session.Close()
|
||||
|
||||
targetFile := filepath.Base(sourceFile)
|
||||
|
||||
src, srcErr := os.Open(sourceFile)
|
||||
|
||||
if srcErr != nil {
|
||||
return srcErr
|
||||
}
|
||||
|
||||
srcStat, statErr := src.Stat()
|
||||
|
||||
if statErr != nil {
|
||||
return statErr
|
||||
}
|
||||
|
||||
go func() {
|
||||
w, _ := session.StdinPipe()
|
||||
|
||||
fmt.Fprintln(w, "C0644", srcStat.Size(), targetFile)
|
||||
|
||||
if srcStat.Size() > 0 {
|
||||
io.Copy(w, src)
|
||||
fmt.Fprint(w, "\x00")
|
||||
w.Close()
|
||||
} else {
|
||||
fmt.Fprint(w, "\x00")
|
||||
w.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
if err := session.Run(fmt.Sprintf("scp -t %s", targetFile)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Generated
-30
@@ -1,30 +0,0 @@
|
||||
hash: d4830515599820a37217ef2806e6f9a3e7be66a417c46bb515fdae02b2e3b830
|
||||
updated: 2016-10-19T16:23:55.991113936+08:00
|
||||
imports:
|
||||
- name: github.com/joho/godotenv
|
||||
version: 4ed13390c0acd2ff4e371e64d8b97c8954138243
|
||||
subpackages:
|
||||
- autoload
|
||||
- name: github.com/urfave/cli
|
||||
version: 55f715e28c46073d0e217e2ce8eb46b0b45e3db6
|
||||
- name: golang.org/x/crypto
|
||||
version: c367d6eeb7c6158125f2f47e049f7eb7e251c09a
|
||||
subpackages:
|
||||
- ssh
|
||||
- ssh/agent
|
||||
- curve25519
|
||||
- ed25519
|
||||
- ed25519/internal/edwards25519
|
||||
testImports:
|
||||
- name: github.com/davecgh/go-spew
|
||||
version: 6d212800a42e8ab5c146b8ace3490ee17e5225f9
|
||||
subpackages:
|
||||
- spew
|
||||
- name: github.com/pmezard/go-difflib
|
||||
version: d8ed2627bdf02c080bf22230dbb337003b7aba2d
|
||||
subpackages:
|
||||
- difflib
|
||||
- name: github.com/stretchr/testify
|
||||
version: 976c720a22c8eb4eb6a0b4348ad85ad12491a506
|
||||
subpackages:
|
||||
- assert
|
||||
-14
@@ -1,14 +0,0 @@
|
||||
package: github.com/appleboy/drone-scp
|
||||
import:
|
||||
- package: github.com/joho/godotenv
|
||||
subpackages:
|
||||
- autoload
|
||||
- package: github.com/urfave/cli
|
||||
- package: golang.org/x/crypto
|
||||
subpackages:
|
||||
- ssh
|
||||
- ssh/agent
|
||||
testImport:
|
||||
- package: github.com/stretchr/testify
|
||||
subpackages:
|
||||
- assert
|
||||
@@ -0,0 +1,14 @@
|
||||
module github.com/appleboy/drone-scp
|
||||
|
||||
go 1.13
|
||||
|
||||
require (
|
||||
github.com/appleboy/com v0.0.2
|
||||
github.com/appleboy/easyssh-proxy v1.2.0
|
||||
github.com/fatih/color v1.7.0
|
||||
github.com/joho/godotenv v1.3.0
|
||||
github.com/mattn/go-colorable v0.1.2 // indirect
|
||||
github.com/mattn/go-isatty v0.0.9 // indirect
|
||||
github.com/stretchr/testify v1.4.0
|
||||
github.com/urfave/cli v1.22.1
|
||||
)
|
||||
@@ -0,0 +1,41 @@
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/appleboy/com v0.0.2 h1:sexcPX7gp7peXMlOJMxEYcRucW7DW0XHgFZqUB6PI6g=
|
||||
github.com/appleboy/com v0.0.2/go.mod h1:jnufjIC3opMlReyPPPye+8JqNvUzLm25o7h6SOy8nv0=
|
||||
github.com/appleboy/easyssh-proxy v1.2.0 h1:KvaUGC18WkBFet+N1oofQy03jkC5HaKFn2XGxFxCTtg=
|
||||
github.com/appleboy/easyssh-proxy v1.2.0/go.mod h1:vHskChUNhxwW4dXMe2MNE/k+UBCkBagrQDm70UWZrS0=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
|
||||
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
|
||||
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
|
||||
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
|
||||
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
|
||||
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
|
||||
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||
github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg=
|
||||
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
|
||||
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
|
||||
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY=
|
||||
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
|
||||
golang.org/x/crypto v0.0.0-20190228161510-8dd112bcdc25 h1:jsG6UpNLt9iAsb0S2AGW28DveNzzgmbXR+ENoPjUeIU=
|
||||
golang.org/x/crypto v0.0.0-20190228161510-8dd112bcdc25/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ=
|
||||
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
@@ -1,63 +1,92 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/appleboy/easyssh-proxy"
|
||||
"github.com/joho/godotenv"
|
||||
_ "github.com/joho/godotenv/autoload"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
// Version for command line
|
||||
var Version string
|
||||
// Version set at compile-time
|
||||
var (
|
||||
Version string
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := cli.NewApp()
|
||||
app.Name = "telegram plugin"
|
||||
app.Usage = "telegram plugin"
|
||||
app.Name = "Drone SCP"
|
||||
app.Usage = "Copy files and artifacts via SSH."
|
||||
app.Copyright = "Copyright (c) 2019 Bo-Yi Wu"
|
||||
app.Version = Version
|
||||
app.Authors = []cli.Author{
|
||||
{
|
||||
Name: "Bo-Yi Wu",
|
||||
Email: "appleboy.tw@gmail.com",
|
||||
},
|
||||
}
|
||||
app.Action = run
|
||||
app.Version = Version
|
||||
app.Flags = []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "host",
|
||||
cli.StringSliceFlag{
|
||||
Name: "host, H",
|
||||
Usage: "Server host",
|
||||
EnvVar: "PLUGIN_HOST,SCP_HOST",
|
||||
EnvVar: "PLUGIN_HOST,SCP_HOST,SSH_HOST,HOST,INPUT_HOST",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "port",
|
||||
Name: "port, P",
|
||||
Value: "22",
|
||||
Usage: "Server port, default to 22",
|
||||
EnvVar: "PLUGIN_PORT,SCP_PORT",
|
||||
EnvVar: "PLUGIN_PORT,SCP_PORT,SSH_PORT,PORT,INPUT_PORT",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "username",
|
||||
Name: "username, u",
|
||||
Usage: "Server username",
|
||||
EnvVar: "PLUGIN_USERNAME,SCP_USERNAME",
|
||||
EnvVar: "PLUGIN_USERNAME,PLUGIN_USER,SCP_USERNAME,SSH_USERNAME,USERNAME,INPUT_USERNAME",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "password",
|
||||
Name: "password, p",
|
||||
Usage: "Password for password-based authentication",
|
||||
EnvVar: "PLUGIN_PASSWORD,SCP_PASSWORD",
|
||||
EnvVar: "PLUGIN_PASSWORD,SCP_PASSWORD,SSH_PASSWORD,PASSWORD,INPUT_PASSWORD",
|
||||
},
|
||||
cli.DurationFlag{
|
||||
Name: "timeout",
|
||||
Usage: "connection timeout",
|
||||
EnvVar: "PLUGIN_TIMEOUT,SCP_TIMEOUT,INPUT_TIMEOUT",
|
||||
},
|
||||
cli.DurationFlag{
|
||||
Name: "command.timeout,T",
|
||||
Usage: "command timeout",
|
||||
EnvVar: "PLUGIN_COMMAND_TIMEOUT,SSH_COMMAND_TIMEOUT,INPUT_COMMAND_TIMEOUT",
|
||||
Value: 60 * time.Second,
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "key",
|
||||
Name: "key, k",
|
||||
Usage: "ssh private key",
|
||||
EnvVar: "PLUGIN_KEY,SCP_KEY",
|
||||
EnvVar: "PLUGIN_KEY,SCP_KEY,SSH_KEY,KEY,INPUT_KEY",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "target",
|
||||
Value: "/",
|
||||
Usage: "Target path on the server, default to '/'",
|
||||
EnvVar: "PLUGIN_TARGET",
|
||||
Name: "key-path, i",
|
||||
Usage: "ssh private key path",
|
||||
EnvVar: "PLUGIN_KEY_PATH,SCP_KEY_PATH,SSH_KEY_PATH,INPUT_KEY_PATH",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "source",
|
||||
Name: "target, t",
|
||||
Usage: "Target path on the server",
|
||||
EnvVar: "PLUGIN_TARGET,SCP_TARGET,TARGET,INPUT_TARGET",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "source, s",
|
||||
Usage: "scp file list",
|
||||
EnvVar: "PLUGIN_SOURCE",
|
||||
EnvVar: "PLUGIN_SOURCE,SCP_SOURCE,SOURCE,INPUT_SOURCE",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "debug",
|
||||
Usage: "display message from command",
|
||||
EnvVar: "PLUGIN_DEBUG",
|
||||
Name: "rm, r",
|
||||
Usage: "remove target folder before upload data",
|
||||
EnvVar: "PLUGIN_RM,SCP_RM,RM,INPUT_RM",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "repo.owner",
|
||||
@@ -112,11 +141,118 @@ func main() {
|
||||
Usage: "build link",
|
||||
EnvVar: "DRONE_BUILD_LINK",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "env-file",
|
||||
Usage: "source env file",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "proxy.ssh-key",
|
||||
Usage: "private ssh key of proxy",
|
||||
EnvVar: "PLUGIN_PROXY_SSH_KEY,PLUGIN_PROXY_KEY,PROXY_SSH_KEY,PROXY_KEY,INPUT_PROXY_SSH_KEY",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "proxy.key-path",
|
||||
Usage: "ssh private key path of proxy",
|
||||
EnvVar: "PLUGIN_PROXY_KEY_PATH,PROXY_SSH_KEY_PATH,INPUT_PROXY_SSH_KEY_PATH",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "proxy.username",
|
||||
Usage: "connect as user of proxy",
|
||||
EnvVar: "PLUGIN_PROXY_USERNAME,PLUGIN_PROXY_USER,PROXY_SSH_USERNAME,PROXY_USERNAME,INPUT_PROXY_USERNAME",
|
||||
Value: "root",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "proxy.password",
|
||||
Usage: "user password of proxy",
|
||||
EnvVar: "PLUGIN_PROXY_PASSWORD,PROXY_SSH_PASSWORD,PROXY_PASSWORD,INPUT_PROXY_PASSWORD",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "proxy.host",
|
||||
Usage: "connect to host of proxy",
|
||||
EnvVar: "PLUGIN_PROXY_HOST,PROXY_SSH_HOST,PROXY_HOST,INPUT_PROXY_HOST",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "proxy.port",
|
||||
Usage: "connect to port of proxy",
|
||||
EnvVar: "PLUGIN_PROXY_PORT,PROXY_SSH_PORT,PROXY_PORT,INPUT_PROXY_PORT",
|
||||
Value: "22",
|
||||
},
|
||||
cli.DurationFlag{
|
||||
Name: "proxy.timeout",
|
||||
Usage: "proxy connection timeout",
|
||||
EnvVar: "PLUGIN_PROXY_TIMEOUT,PROXY_SSH_TIMEOUT,INPUT_PROXY_TIMEOUT",
|
||||
},
|
||||
cli.IntFlag{
|
||||
Name: "strip.components",
|
||||
Usage: "Remove the specified number of leading path elements.",
|
||||
EnvVar: "PLUGIN_STRIP_COMPONENTS,TAR_STRIP_COMPONENTS,INPUT_STRIP_COMPONENTS",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "tar.exec",
|
||||
Usage: "Alternative `tar` executable to on the dest host",
|
||||
EnvVar: "PLUGIN_TAR_EXEC,SCP_TAR_EXEC,INPUT_TAR_EXEC",
|
||||
Value: "tar",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "tar.tmp-path",
|
||||
Usage: "Temporary path for tar file on the dest host",
|
||||
EnvVar: "PLUGIN_TAR_TMP_PATH,SCP_TAR_TMP_PATH",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "debug",
|
||||
Usage: "remove target folder before upload data",
|
||||
EnvVar: "PLUGIN_DEBUG,DEBUG,INPUT_DEBUG",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "overwrite",
|
||||
Usage: "use --overwrite flag with tar",
|
||||
EnvVar: "PLUGIN_OVERWRITE,SCP_OVERWRITE,INPUT_OVERWRITE",
|
||||
},
|
||||
}
|
||||
|
||||
// Override a template
|
||||
cli.AppHelpTemplate = `
|
||||
________ ____________________________
|
||||
\______ \_______ ____ ____ ____ / _____/\_ ___ \______ \
|
||||
| | \_ __ \/ _ \ / \_/ __ \ ______ \_____ \ / \ \/| ___/
|
||||
| | \ | \( <_> ) | \ ___/ /_____/ / \\ \___| |
|
||||
/_______ /__| \____/|___| /\___ > /_______ / \______ /____|
|
||||
\/ \/ \/ \/ \/
|
||||
version: {{.Version}}
|
||||
NAME:
|
||||
{{.Name}} - {{.Usage}}
|
||||
|
||||
USAGE:
|
||||
{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}
|
||||
{{if len .Authors}}
|
||||
AUTHOR:
|
||||
{{range .Authors}}{{ . }}{{end}}
|
||||
{{end}}{{if .Commands}}
|
||||
COMMANDS:
|
||||
{{range .Commands}}{{if not .HideHelp}} {{join .Names ", "}}{{ "\t"}}{{.Usage}}{{ "\n" }}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
|
||||
GLOBAL OPTIONS:
|
||||
{{range .VisibleFlags}}{{.}}
|
||||
{{end}}{{end}}{{if .Copyright }}
|
||||
COPYRIGHT:
|
||||
{{.Copyright}}
|
||||
{{end}}{{if .Version}}
|
||||
VERSION:
|
||||
{{.Version}}
|
||||
{{end}}
|
||||
REPOSITORY:
|
||||
Github: https://github.com/appleboy/drone-scp
|
||||
`
|
||||
|
||||
if err := app.Run(os.Args); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
app.Run(os.Args)
|
||||
}
|
||||
|
||||
func run(c *cli.Context) error {
|
||||
if c.String("env-file") != "" {
|
||||
_ = godotenv.Load(c.String("env-file"))
|
||||
}
|
||||
|
||||
plugin := Plugin{
|
||||
Repo: Repo{
|
||||
Owner: c.String("repo.owner"),
|
||||
@@ -133,14 +269,31 @@ func run(c *cli.Context) error {
|
||||
Link: c.String("build.link"),
|
||||
},
|
||||
Config: Config{
|
||||
Host: c.String("host"),
|
||||
Port: c.String("port"),
|
||||
Username: c.String("username"),
|
||||
Password: c.String("password"),
|
||||
Key: c.String("key"),
|
||||
Target: c.String("target"),
|
||||
Source: c.StringSlice("source"),
|
||||
Debug: c.Bool("debug"),
|
||||
Host: c.StringSlice("host"),
|
||||
Port: c.String("port"),
|
||||
Username: c.String("username"),
|
||||
Password: c.String("password"),
|
||||
Timeout: c.Duration("timeout"),
|
||||
CommandTimeout: c.Duration("command.timeout"),
|
||||
Key: c.String("key"),
|
||||
KeyPath: c.String("key-path"),
|
||||
Target: c.StringSlice("target"),
|
||||
Source: c.StringSlice("source"),
|
||||
Remove: c.Bool("rm"),
|
||||
Debug: c.Bool("debug"),
|
||||
StripComponents: c.Int("strip.components"),
|
||||
TarExec: c.String("tar.exec"),
|
||||
TarTmpPath: c.String("tar.tmp-path"),
|
||||
Overwrite: c.Bool("overwrite"),
|
||||
Proxy: easyssh.DefaultConfig{
|
||||
Key: c.String("proxy.ssh-key"),
|
||||
KeyPath: c.String("proxy.key-path"),
|
||||
User: c.String("proxy.username"),
|
||||
Password: c.String("proxy.password"),
|
||||
Server: c.String("proxy.host"),
|
||||
Port: c.String("proxy.port"),
|
||||
Timeout: c.Duration("proxy.timeout"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
// +build !windows
|
||||
|
||||
package main
|
||||
|
||||
func getRealPath(path string) string {
|
||||
return path
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// +build windows
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func getRealPath(path string) string {
|
||||
return "/" + strings.Replace(strings.Replace(path, ":", "", -1), "\\", "/", -1)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetRealPath(t *testing.T) {
|
||||
type args struct {
|
||||
path string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
{
|
||||
"Test Windows Path",
|
||||
"C:\\Users\\appleboy\\test.txt",
|
||||
"/C/Users/appleboy/test.txt",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := getRealPath(tt.args.path); got != tt.want {
|
||||
t.Errorf("%q. getRealPath() = %v, want %v", tt.name, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,256 @@
|
||||
{
|
||||
test:: {
|
||||
kind: 'pipeline',
|
||||
name: 'testing',
|
||||
platform: {
|
||||
os: 'linux',
|
||||
arch: 'amd64',
|
||||
},
|
||||
steps: [
|
||||
{
|
||||
name: 'vet',
|
||||
image: 'golang:1.13',
|
||||
pull: 'always',
|
||||
commands: [
|
||||
'make vet',
|
||||
],
|
||||
volumes: [
|
||||
{
|
||||
name: 'gopath',
|
||||
path: '/go',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'lint',
|
||||
image: 'golang:1.13',
|
||||
pull: 'always',
|
||||
commands: [
|
||||
'make lint',
|
||||
],
|
||||
volumes: [
|
||||
{
|
||||
name: 'gopath',
|
||||
path: '/go',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'misspell',
|
||||
image: 'golang:1.13',
|
||||
pull: 'always',
|
||||
commands: [
|
||||
'make misspell-check',
|
||||
],
|
||||
volumes: [
|
||||
{
|
||||
name: 'gopath',
|
||||
path: '/go',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'test',
|
||||
image: 'golang:1.13-alpine',
|
||||
pull: 'always',
|
||||
commands: [
|
||||
'apk add git make curl perl bash build-base zlib-dev ucl-dev',
|
||||
'make ssh-server',
|
||||
'make test',
|
||||
'make coverage',
|
||||
],
|
||||
volumes: [
|
||||
{
|
||||
name: 'gopath',
|
||||
path: '/go',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'codecov',
|
||||
image: 'robertstettner/drone-codecov',
|
||||
pull: 'always',
|
||||
settings: {
|
||||
token: { 'from_secret': 'codecov_token' },
|
||||
},
|
||||
},
|
||||
],
|
||||
volumes: [
|
||||
{
|
||||
name: 'gopath',
|
||||
temp: {},
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
build(name, os='linux', arch='amd64'):: {
|
||||
kind: 'pipeline',
|
||||
name: os + '-' + arch,
|
||||
platform: {
|
||||
os: os,
|
||||
arch: arch,
|
||||
},
|
||||
steps: [
|
||||
{
|
||||
name: 'build-push',
|
||||
image: 'golang:1.13',
|
||||
pull: 'always',
|
||||
environment: {
|
||||
CGO_ENABLED: '0',
|
||||
},
|
||||
commands: [
|
||||
'go build -v -ldflags \'-X main.build=${DRONE_BUILD_NUMBER}\' -a -o release/' + os + '/' + arch + '/' + name,
|
||||
],
|
||||
when: {
|
||||
event: {
|
||||
exclude: [ 'tag' ],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'build-tag',
|
||||
image: 'golang:1.13',
|
||||
pull: 'always',
|
||||
environment: {
|
||||
CGO_ENABLED: '0',
|
||||
},
|
||||
commands: [
|
||||
'go build -v -ldflags \'-X main.version=${DRONE_TAG##v} -X main.build=${DRONE_BUILD_NUMBER}\' -a -o release/' + os + '/' + arch + '/' + name,
|
||||
],
|
||||
when: {
|
||||
event: [ 'tag' ],
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'executable',
|
||||
image: 'golang:1.13',
|
||||
pull: 'always',
|
||||
commands: [
|
||||
'./release/' + os + '/' + arch + '/' + name + ' --help',
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'dryrun',
|
||||
image: 'plugins/docker:' + os + '-' + arch,
|
||||
pull: 'always',
|
||||
settings: {
|
||||
daemon_off: false,
|
||||
dry_run: true,
|
||||
tags: os + '-' + arch,
|
||||
dockerfile: 'docker/Dockerfile.' + os + '.' + arch,
|
||||
repo: 'appleboy/' + name,
|
||||
cache_from: 'appleboy/' + name,
|
||||
},
|
||||
when: {
|
||||
event: [ 'pull_request' ],
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'publish',
|
||||
image: 'plugins/docker:' + os + '-' + arch,
|
||||
pull: 'always',
|
||||
settings: {
|
||||
daemon_off: 'false',
|
||||
auto_tag: true,
|
||||
auto_tag_suffix: os + '-' + arch,
|
||||
dockerfile: 'docker/Dockerfile.' + os + '.' + arch,
|
||||
repo: 'appleboy/' + name,
|
||||
cache_from: 'appleboy/' + name,
|
||||
username: { 'from_secret': 'docker_username' },
|
||||
password: { 'from_secret': 'docker_password' },
|
||||
},
|
||||
when: {
|
||||
event: {
|
||||
exclude: [ 'pull_request' ],
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
depends_on: [
|
||||
'testing',
|
||||
],
|
||||
trigger: {
|
||||
ref: [
|
||||
'refs/heads/master',
|
||||
'refs/pull/**',
|
||||
'refs/tags/**',
|
||||
],
|
||||
},
|
||||
},
|
||||
|
||||
release:: {
|
||||
kind: 'pipeline',
|
||||
name: 'release-binary',
|
||||
platform: {
|
||||
os: 'linux',
|
||||
arch: 'amd64',
|
||||
},
|
||||
steps: [
|
||||
{
|
||||
name: 'build-all-binary',
|
||||
image: 'golang:1.13',
|
||||
pull: 'always',
|
||||
commands: [
|
||||
'make release'
|
||||
],
|
||||
when: {
|
||||
event: [ 'tag' ],
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'deploy-all-binary',
|
||||
image: 'plugins/github-release',
|
||||
pull: 'always',
|
||||
settings: {
|
||||
files: [ 'dist/release/*' ],
|
||||
api_key: { 'from_secret': 'github_release_api_key' },
|
||||
},
|
||||
when: {
|
||||
event: [ 'tag' ],
|
||||
},
|
||||
},
|
||||
],
|
||||
depends_on: [
|
||||
'testing',
|
||||
],
|
||||
trigger: {
|
||||
ref: [
|
||||
'refs/tags/**',
|
||||
],
|
||||
},
|
||||
},
|
||||
|
||||
notifications(os='linux', arch='amd64', depends_on=[]):: {
|
||||
kind: 'pipeline',
|
||||
name: 'notifications',
|
||||
platform: {
|
||||
os: os,
|
||||
arch: arch,
|
||||
},
|
||||
steps: [
|
||||
{
|
||||
name: 'manifest',
|
||||
image: 'plugins/manifest',
|
||||
pull: 'always',
|
||||
settings: {
|
||||
username: { from_secret: 'docker_username' },
|
||||
password: { from_secret: 'docker_password' },
|
||||
spec: 'docker/manifest.tmpl',
|
||||
ignore_missing: true,
|
||||
},
|
||||
},
|
||||
],
|
||||
depends_on: depends_on,
|
||||
trigger: {
|
||||
ref: [
|
||||
'refs/heads/master',
|
||||
'refs/tags/**',
|
||||
],
|
||||
},
|
||||
},
|
||||
|
||||
signature(key):: {
|
||||
kind: 'signature',
|
||||
hmac: key,
|
||||
}
|
||||
}
|
||||
@@ -4,13 +4,24 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/appleboy/drone-scp/easyssh"
|
||||
"github.com/appleboy/com/random"
|
||||
"github.com/appleboy/easyssh-proxy"
|
||||
"github.com/fatih/color"
|
||||
)
|
||||
|
||||
var (
|
||||
errMissingHost = errors.New("Error: missing server host")
|
||||
errMissingPasswordOrKey = errors.New("Error: can't connect without a private SSH key or password")
|
||||
errSetPasswordandKey = errors.New("can't set password and key at the same time")
|
||||
errMissingSourceOrTarget = errors.New("missing source or target config")
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -34,24 +45,43 @@ type (
|
||||
|
||||
// Config for the plugin.
|
||||
Config struct {
|
||||
Host string
|
||||
Port string
|
||||
Username string
|
||||
Password string
|
||||
Key string
|
||||
Target string
|
||||
Source []string
|
||||
Debug bool
|
||||
Host []string
|
||||
Port string
|
||||
Username string
|
||||
Password string
|
||||
Key string
|
||||
KeyPath string
|
||||
Timeout time.Duration
|
||||
CommandTimeout time.Duration
|
||||
Target []string
|
||||
Source []string
|
||||
Remove bool
|
||||
StripComponents int
|
||||
TarExec string
|
||||
TarTmpPath string
|
||||
Proxy easyssh.DefaultConfig
|
||||
Debug bool
|
||||
Overwrite bool
|
||||
}
|
||||
|
||||
// Plugin values.
|
||||
Plugin struct {
|
||||
Repo Repo
|
||||
Build Build
|
||||
Config Config
|
||||
Repo Repo
|
||||
Build Build
|
||||
Config Config
|
||||
DestFile string
|
||||
}
|
||||
|
||||
copyError struct {
|
||||
host string
|
||||
message string
|
||||
}
|
||||
)
|
||||
|
||||
func (e copyError) Error() string {
|
||||
return fmt.Sprintf("error copy file to dest: %s, error message: %s\n", e.host, e.message)
|
||||
}
|
||||
|
||||
func trimPath(keys []string) []string {
|
||||
var newKeys []string
|
||||
|
||||
@@ -67,98 +97,293 @@ func trimPath(keys []string) []string {
|
||||
return newKeys
|
||||
}
|
||||
|
||||
func globList(paths []string) fileList {
|
||||
var list fileList
|
||||
|
||||
for _, pattern := range paths {
|
||||
ignore := false
|
||||
pattern = strings.Trim(pattern, " ")
|
||||
if string(pattern[0]) == "!" {
|
||||
pattern = pattern[1:]
|
||||
ignore = true
|
||||
}
|
||||
matches, err := filepath.Glob(pattern)
|
||||
if err != nil {
|
||||
fmt.Printf("Glob error for %q: %s\n", pattern, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if ignore {
|
||||
list.Ignore = append(list.Ignore, matches...)
|
||||
} else {
|
||||
list.Source = append(list.Source, matches...)
|
||||
}
|
||||
}
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
func buildArgs(tar string, files fileList) []string {
|
||||
args := []string{}
|
||||
if len(files.Ignore) > 0 {
|
||||
for _, v := range files.Ignore {
|
||||
args = append(args, "--exclude")
|
||||
args = append(args, v)
|
||||
}
|
||||
}
|
||||
args = append(args, "-cf")
|
||||
args = append(args, getRealPath(tar))
|
||||
args = append(args, files.Source...)
|
||||
|
||||
return args
|
||||
}
|
||||
|
||||
func (p Plugin) log(host string, message ...interface{}) {
|
||||
if count := len(p.Config.Host); count == 1 {
|
||||
fmt.Printf("%s", fmt.Sprintln(message...))
|
||||
} else {
|
||||
fmt.Printf("%s: %s", host, fmt.Sprintln(message...))
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Plugin) removeDestFile(ssh *easyssh.MakeConfig) error {
|
||||
p.log(ssh.Server, "remove file", p.DestFile)
|
||||
_, errStr, _, err := ssh.Run(fmt.Sprintf("rm -rf %s", p.DestFile), p.Config.CommandTimeout)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if errStr != "" {
|
||||
return errors.New(errStr)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Plugin) removeAllDestFile() error {
|
||||
for _, host := range p.Config.Host {
|
||||
ssh := &easyssh.MakeConfig{
|
||||
Server: host,
|
||||
User: p.Config.Username,
|
||||
Password: p.Config.Password,
|
||||
Port: p.Config.Port,
|
||||
Key: p.Config.Key,
|
||||
KeyPath: p.Config.KeyPath,
|
||||
Timeout: p.Config.Timeout,
|
||||
Proxy: easyssh.DefaultConfig{
|
||||
Server: p.Config.Proxy.Server,
|
||||
User: p.Config.Proxy.User,
|
||||
Password: p.Config.Proxy.Password,
|
||||
Port: p.Config.Proxy.Port,
|
||||
Key: p.Config.Proxy.Key,
|
||||
KeyPath: p.Config.Proxy.KeyPath,
|
||||
Timeout: p.Config.Proxy.Timeout,
|
||||
},
|
||||
}
|
||||
|
||||
// remove tar file
|
||||
err := p.removeDestFile(ssh)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type fileList struct {
|
||||
Ignore []string
|
||||
Source []string
|
||||
}
|
||||
|
||||
func (p *Plugin) buildArgs(target string) []string {
|
||||
args := []string{}
|
||||
|
||||
args = append(args,
|
||||
p.Config.TarExec,
|
||||
"-xf",
|
||||
p.DestFile,
|
||||
)
|
||||
|
||||
if p.Config.StripComponents > 0 {
|
||||
args = append(args, "--strip-components")
|
||||
args = append(args, strconv.Itoa(p.Config.StripComponents))
|
||||
}
|
||||
|
||||
if p.Config.Overwrite {
|
||||
args = append(args, "--overwrite")
|
||||
}
|
||||
|
||||
args = append(args,
|
||||
"-C",
|
||||
target,
|
||||
)
|
||||
|
||||
return args
|
||||
}
|
||||
|
||||
// Exec executes the plugin.
|
||||
func (p Plugin) Exec() error {
|
||||
|
||||
if len(p.Config.Host) == 0 || len(p.Config.Username) == 0 || (len(p.Config.Password) == 0 && len(p.Config.Key) == 0) {
|
||||
log.Println("missing ssh config (Host, Username, Password or Key)")
|
||||
|
||||
return errors.New("missing ssh config (Host, Username, Password or Key)")
|
||||
func (p *Plugin) Exec() error {
|
||||
if len(p.Config.Host) == 0 {
|
||||
return errMissingHost
|
||||
}
|
||||
|
||||
if len(p.Config.Source) == 0 {
|
||||
log.Println("missing source file list config")
|
||||
|
||||
return errors.New("missing source file list config")
|
||||
if len(p.Config.Key) == 0 && len(p.Config.Password) == 0 && len(p.Config.KeyPath) == 0 {
|
||||
return errMissingPasswordOrKey
|
||||
}
|
||||
|
||||
files := trimPath(p.Config.Source)
|
||||
src := strings.Join(files, " ")
|
||||
dest := fmt.Sprintf("%s-%s.tar", p.Repo.Name, p.Build.Commit[:7])
|
||||
if len(p.Config.Key) != 0 && len(p.Config.Password) != 0 {
|
||||
return errSetPasswordandKey
|
||||
}
|
||||
|
||||
if len(p.Config.Source) == 0 || len(p.Config.Target) == 0 {
|
||||
return errMissingSourceOrTarget
|
||||
}
|
||||
|
||||
files := globList(trimPath(p.Config.Source))
|
||||
p.DestFile = fmt.Sprintf("%s.tar", random.String(10))
|
||||
|
||||
// create a temporary file for the archive
|
||||
dir, err := ioutil.TempDir("", "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tar := filepath.Join(dir, dest)
|
||||
tar := filepath.Join(dir, p.DestFile)
|
||||
|
||||
// run archive command
|
||||
log.Println("tar all files into " + tar)
|
||||
cmd := exec.Command("tar", "-cf", tar, src)
|
||||
fmt.Println("tar all files into " + tar)
|
||||
args := buildArgs(tar, files)
|
||||
cmd := exec.Command(p.Config.TarExec, args...)
|
||||
if p.Config.Debug {
|
||||
fmt.Println("$", strings.Join(cmd.Args, " "))
|
||||
}
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create MakeConfig instance with remote username, server address and path to private key.
|
||||
ssh := &easyssh.MakeConfig{
|
||||
Server: p.Config.Host,
|
||||
User: p.Config.Username,
|
||||
Password: p.Config.Password,
|
||||
Port: p.Config.Port,
|
||||
Key: p.Config.Key,
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(len(p.Config.Host))
|
||||
errChannel := make(chan error)
|
||||
finished := make(chan struct{})
|
||||
for _, host := range p.Config.Host {
|
||||
go func(host string) {
|
||||
// Create MakeConfig instance with remote username, server address and path to private key.
|
||||
ssh := &easyssh.MakeConfig{
|
||||
Server: host,
|
||||
User: p.Config.Username,
|
||||
Password: p.Config.Password,
|
||||
Port: p.Config.Port,
|
||||
Key: p.Config.Key,
|
||||
KeyPath: p.Config.KeyPath,
|
||||
Timeout: p.Config.Timeout,
|
||||
Proxy: easyssh.DefaultConfig{
|
||||
Server: p.Config.Proxy.Server,
|
||||
User: p.Config.Proxy.User,
|
||||
Password: p.Config.Proxy.Password,
|
||||
Port: p.Config.Proxy.Port,
|
||||
Key: p.Config.Proxy.Key,
|
||||
KeyPath: p.Config.Proxy.KeyPath,
|
||||
Timeout: p.Config.Proxy.Timeout,
|
||||
},
|
||||
}
|
||||
|
||||
// upload file to the tmp path
|
||||
p.DestFile = fmt.Sprintf("%s%s", p.Config.TarTmpPath, p.DestFile)
|
||||
|
||||
// Call Scp method with file you want to upload to remote server.
|
||||
p.log(host, "scp file to server.")
|
||||
err := ssh.Scp(tar, p.DestFile)
|
||||
|
||||
if err != nil {
|
||||
errChannel <- copyError{host, err.Error()}
|
||||
return
|
||||
}
|
||||
|
||||
for _, target := range p.Config.Target {
|
||||
// remove target before upload data
|
||||
if p.Config.Remove {
|
||||
p.log(host, "Remove target folder:", target)
|
||||
|
||||
_, _, _, err := ssh.Run(fmt.Sprintf("rm -rf %s", target), p.Config.CommandTimeout)
|
||||
|
||||
if err != nil {
|
||||
errChannel <- err
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// mkdir path
|
||||
p.log(host, "create folder", target)
|
||||
_, errStr, _, err := ssh.Run(fmt.Sprintf("mkdir -p %s", target), p.Config.CommandTimeout)
|
||||
if err != nil {
|
||||
errChannel <- err
|
||||
return
|
||||
}
|
||||
|
||||
if len(errStr) != 0 {
|
||||
errChannel <- fmt.Errorf(errStr)
|
||||
return
|
||||
}
|
||||
|
||||
// untar file
|
||||
p.log(host, "untar file", p.DestFile)
|
||||
commamd := strings.Join(p.buildArgs(target), " ")
|
||||
if p.Config.Debug {
|
||||
fmt.Println("$", commamd)
|
||||
}
|
||||
outStr, errStr, _, err := ssh.Run(commamd, p.Config.CommandTimeout)
|
||||
|
||||
if outStr != "" {
|
||||
p.log(host, "output: ", outStr)
|
||||
}
|
||||
|
||||
if errStr != "" {
|
||||
p.log(host, "error: ", errStr)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
errChannel <- err
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// remove tar file
|
||||
err = p.removeDestFile(ssh)
|
||||
if err != nil {
|
||||
errChannel <- err
|
||||
return
|
||||
}
|
||||
|
||||
wg.Done()
|
||||
|
||||
}(host)
|
||||
}
|
||||
|
||||
// Call Scp method with file you want to upload to remote server.
|
||||
log.Println("scp file to remote server remote server.")
|
||||
err = ssh.Scp(tar)
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(finished)
|
||||
}()
|
||||
|
||||
// Handle errors
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
return err
|
||||
select {
|
||||
case <-finished:
|
||||
case err := <-errChannel:
|
||||
if err != nil {
|
||||
c := color.New(color.FgRed)
|
||||
c.Println("drone-scp error: ", err)
|
||||
if _, ok := err.(copyError); !ok {
|
||||
fmt.Println("drone-scp rollback: remove all target tmp file")
|
||||
p.removeAllDestFile()
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// mkdir path
|
||||
log.Println("create remote folder " + p.Config.Target)
|
||||
response, err := ssh.Run(fmt.Sprintf("mkdir -p %s", p.Config.Target))
|
||||
|
||||
if p.Config.Debug {
|
||||
log.Println(response)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
// untar file
|
||||
log.Println("untar remote file " + dest)
|
||||
response, err = ssh.Run(fmt.Sprintf("tar -xf %s -C %s", dest, p.Config.Target))
|
||||
|
||||
if p.Config.Debug {
|
||||
log.Println(response)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
// remove tar file
|
||||
log.Println("remove remote file " + dest)
|
||||
response, err = ssh.Run(fmt.Sprintf("rm -rf %s", dest))
|
||||
|
||||
if p.Config.Debug {
|
||||
log.Println(response)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
return err
|
||||
}
|
||||
fmt.Println("================================================")
|
||||
fmt.Println("Successfully executed transfer data to all host.")
|
||||
fmt.Println("================================================")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
+484
-5
@@ -1,12 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/appleboy/easyssh-proxy"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMissingConfig(t *testing.T) {
|
||||
func TestMissingAllConfig(t *testing.T) {
|
||||
var plugin Plugin
|
||||
|
||||
err := plugin.Exec()
|
||||
@@ -17,7 +24,7 @@ func TestMissingConfig(t *testing.T) {
|
||||
func TestMissingSSHConfig(t *testing.T) {
|
||||
plugin := Plugin{
|
||||
Config: Config{
|
||||
Host: "example.com",
|
||||
Host: []string{"example.com"},
|
||||
Username: "ubuntu",
|
||||
},
|
||||
}
|
||||
@@ -30,7 +37,7 @@ func TestMissingSSHConfig(t *testing.T) {
|
||||
func TestMissingSourceConfig(t *testing.T) {
|
||||
plugin := Plugin{
|
||||
Config: Config{
|
||||
Host: "example.com",
|
||||
Host: []string{"example.com"},
|
||||
Username: "ubuntu",
|
||||
Port: "443",
|
||||
Password: "1234",
|
||||
@@ -42,6 +49,23 @@ func TestMissingSourceConfig(t *testing.T) {
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestSetPasswordAndKey(t *testing.T) {
|
||||
plugin := Plugin{
|
||||
Config: Config{
|
||||
Host: []string{"example.com"},
|
||||
Username: "ubuntu",
|
||||
Port: "443",
|
||||
Password: "1234",
|
||||
Key: "test",
|
||||
},
|
||||
}
|
||||
|
||||
err := plugin.Exec()
|
||||
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, errSetPasswordandKey, err)
|
||||
}
|
||||
|
||||
func TestTrimElement(t *testing.T) {
|
||||
var input, result []string
|
||||
|
||||
@@ -55,3 +79,458 @@ func TestTrimElement(t *testing.T) {
|
||||
|
||||
assert.Equal(t, result, trimPath(input))
|
||||
}
|
||||
|
||||
func TestSCPFileFromPublicKey(t *testing.T) {
|
||||
if os.Getenv("SSH_AUTH_SOCK") != "" {
|
||||
if err := exec.Command("eval", "`ssh-agent -k`").Run(); err != nil {
|
||||
t.Fatalf("exec: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
u, err := user.Lookup("drone-scp")
|
||||
if err != nil {
|
||||
t.Fatalf("Lookup: %v", err)
|
||||
}
|
||||
|
||||
plugin := Plugin{
|
||||
Config: Config{
|
||||
Host: []string{"localhost"},
|
||||
Username: "drone-scp",
|
||||
Port: "22",
|
||||
KeyPath: "tests/.ssh/id_rsa",
|
||||
Source: []string{"tests/a.txt", "tests/b.txt"},
|
||||
Target: []string{filepath.Join(u.HomeDir, "/test")},
|
||||
CommandTimeout: 60 * time.Second,
|
||||
TarExec: "tar",
|
||||
},
|
||||
}
|
||||
|
||||
err = plugin.Exec()
|
||||
assert.Nil(t, err)
|
||||
|
||||
// check file exist
|
||||
if _, err := os.Stat(filepath.Join(u.HomeDir, "/test/tests/a.txt")); os.IsNotExist(err) {
|
||||
t.Fatalf("SCP-error: %v", err)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filepath.Join(u.HomeDir, "/test/tests/b.txt")); os.IsNotExist(err) {
|
||||
t.Fatalf("SCP-error: %v", err)
|
||||
}
|
||||
|
||||
// Test -rm flag
|
||||
plugin.Config.Source = []string{"tests/a.txt"}
|
||||
plugin.Config.Remove = true
|
||||
|
||||
err = plugin.Exec()
|
||||
assert.Nil(t, err)
|
||||
|
||||
// check file exist
|
||||
if _, err := os.Stat(filepath.Join(u.HomeDir, "/test/tests/b.txt")); os.IsExist(err) {
|
||||
t.Fatalf("SCP-error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCPWildcardFileList(t *testing.T) {
|
||||
if os.Getenv("SSH_AUTH_SOCK") != "" {
|
||||
if err := exec.Command("eval", "`ssh-agent -k`").Run(); err != nil {
|
||||
t.Fatalf("exec: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
u, err := user.Lookup("drone-scp")
|
||||
if err != nil {
|
||||
t.Fatalf("Lookup: %v", err)
|
||||
}
|
||||
|
||||
plugin := Plugin{
|
||||
Config: Config{
|
||||
Host: []string{"localhost"},
|
||||
Username: "drone-scp",
|
||||
Port: "22",
|
||||
KeyPath: "tests/.ssh/id_rsa",
|
||||
Source: []string{"tests/global/*"},
|
||||
Target: []string{filepath.Join(u.HomeDir, "abc")},
|
||||
CommandTimeout: 60 * time.Second,
|
||||
TarExec: "tar",
|
||||
},
|
||||
}
|
||||
|
||||
err = plugin.Exec()
|
||||
assert.Nil(t, err)
|
||||
|
||||
// check file exist
|
||||
if _, err := os.Stat(filepath.Join(u.HomeDir, "abc/tests/global/c.txt")); os.IsNotExist(err) {
|
||||
t.Fatalf("SCP-error: %v", err)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filepath.Join(u.HomeDir, "abc/tests/global/d.txt")); os.IsNotExist(err) {
|
||||
t.Fatalf("SCP-error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCPFromProxySetting(t *testing.T) {
|
||||
u, err := user.Lookup("drone-scp")
|
||||
if err != nil {
|
||||
t.Fatalf("Lookup: %v", err)
|
||||
}
|
||||
|
||||
plugin := Plugin{
|
||||
Config: Config{
|
||||
Host: []string{"localhost"},
|
||||
Username: "drone-scp",
|
||||
Port: "22",
|
||||
KeyPath: "tests/.ssh/id_rsa",
|
||||
Source: []string{"tests/global/*"},
|
||||
Target: []string{filepath.Join(u.HomeDir, "def")},
|
||||
CommandTimeout: 60 * time.Second,
|
||||
TarExec: "tar",
|
||||
Proxy: easyssh.DefaultConfig{
|
||||
Server: "localhost",
|
||||
User: "drone-scp",
|
||||
Port: "22",
|
||||
KeyPath: "./tests/.ssh/id_rsa",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err = plugin.Exec()
|
||||
assert.Nil(t, err)
|
||||
|
||||
// check file exist
|
||||
if _, err := os.Stat(filepath.Join(u.HomeDir, "def/tests/global/c.txt")); os.IsNotExist(err) {
|
||||
t.Fatalf("SCP-error: %v", err)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filepath.Join(u.HomeDir, "def/tests/global/d.txt")); os.IsNotExist(err) {
|
||||
t.Fatalf("SCP-error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStripComponentsFlag(t *testing.T) {
|
||||
if os.Getenv("SSH_AUTH_SOCK") != "" {
|
||||
if err := exec.Command("eval", "`ssh-agent -k`").Run(); err != nil {
|
||||
t.Fatalf("exec: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
u, err := user.Lookup("drone-scp")
|
||||
if err != nil {
|
||||
t.Fatalf("Lookup: %v", err)
|
||||
}
|
||||
|
||||
plugin := Plugin{
|
||||
Config: Config{
|
||||
Host: []string{"localhost"},
|
||||
Username: "drone-scp",
|
||||
Port: "22",
|
||||
KeyPath: "tests/.ssh/id_rsa",
|
||||
Source: []string{"tests/global/*"},
|
||||
StripComponents: 2,
|
||||
Target: []string{filepath.Join(u.HomeDir, "123")},
|
||||
CommandTimeout: 60 * time.Second,
|
||||
TarExec: "tar",
|
||||
},
|
||||
}
|
||||
|
||||
err = plugin.Exec()
|
||||
assert.Nil(t, err)
|
||||
|
||||
// check file exist
|
||||
if _, err := os.Stat(filepath.Join(u.HomeDir, "123/c.txt")); os.IsNotExist(err) {
|
||||
t.Fatalf("SCP-error: %v", err)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filepath.Join(u.HomeDir, "123/d.txt")); os.IsNotExist(err) {
|
||||
t.Fatalf("SCP-error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIgnoreList(t *testing.T) {
|
||||
if os.Getenv("SSH_AUTH_SOCK") != "" {
|
||||
if err := exec.Command("eval", "`ssh-agent -k`").Run(); err != nil {
|
||||
t.Fatalf("exec: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
u, err := user.Lookup("drone-scp")
|
||||
if err != nil {
|
||||
t.Fatalf("Lookup: %v", err)
|
||||
}
|
||||
|
||||
plugin := Plugin{
|
||||
Config: Config{
|
||||
Host: []string{"localhost"},
|
||||
Username: "drone-scp",
|
||||
Port: "22",
|
||||
KeyPath: "tests/.ssh/id_rsa",
|
||||
Source: []string{"tests/global/*", "!tests/global/c.txt", "!tests/global/e.txt"},
|
||||
StripComponents: 2,
|
||||
Target: []string{filepath.Join(u.HomeDir, "ignore")},
|
||||
CommandTimeout: 60 * time.Second,
|
||||
TarExec: "tar",
|
||||
Debug: true,
|
||||
},
|
||||
}
|
||||
|
||||
err = plugin.Exec()
|
||||
assert.Nil(t, err)
|
||||
|
||||
// check file exist
|
||||
if _, err := os.Stat(filepath.Join(u.HomeDir, "ignore/c.txt")); err == nil {
|
||||
t.Fatal("c.txt file exist")
|
||||
}
|
||||
|
||||
// check file exist
|
||||
if _, err := os.Stat(filepath.Join(u.HomeDir, "ignore/e.txt")); err == nil {
|
||||
t.Fatal("c.txt file exist")
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filepath.Join(u.HomeDir, "ignore/d.txt")); os.IsNotExist(err) {
|
||||
t.Fatalf("SCP-error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// func TestSCPFileFromSSHAgent(t *testing.T) {
|
||||
// if os.Getenv("SSH_AUTH_SOCK") == "" {
|
||||
// exec.Command("eval", "`ssh-agent -s`").Run()
|
||||
// exec.Command("ssh-add", "tests/.ssh/id_rsa").Run()
|
||||
// }
|
||||
|
||||
// u, err := user.Lookup("drone-scp")
|
||||
// if err != nil {
|
||||
// t.Fatalf("Lookup: %v", err)
|
||||
// }
|
||||
|
||||
// plugin := Plugin{
|
||||
// Config: Config{
|
||||
// Host: []string{"localhost"},
|
||||
// Username: "drone-scp",
|
||||
// Port: "22",
|
||||
// Source: []string{"tests/a.txt", "tests/b.txt"},
|
||||
// Target: []string{u.HomeDir + "/test"},
|
||||
// },
|
||||
// }
|
||||
|
||||
// err = plugin.Exec()
|
||||
// assert.Nil(t, err)
|
||||
// }
|
||||
|
||||
// func TestSCPFileFromPassword(t *testing.T) {
|
||||
// u, err := user.Lookup("drone-scp")
|
||||
// if err != nil {
|
||||
// t.Fatalf("Lookup: %v", err)
|
||||
// }
|
||||
|
||||
// plugin := Plugin{
|
||||
// Config: Config{
|
||||
// Host: []string{"localhost"},
|
||||
// Username: "drone-scp",
|
||||
// Port: "22",
|
||||
// Password: "1234",
|
||||
// Source: []string{"tests/a.txt", "tests/b.txt"},
|
||||
// Target: []string{u.HomeDir + "/test"},
|
||||
// },
|
||||
// }
|
||||
|
||||
// err = plugin.Exec()
|
||||
// assert.Nil(t, err)
|
||||
// }
|
||||
|
||||
func TestIncorrectPassword(t *testing.T) {
|
||||
plugin := Plugin{
|
||||
Config: Config{
|
||||
Host: []string{"localhost"},
|
||||
Username: "drone-scp",
|
||||
Port: "22",
|
||||
Password: "123456",
|
||||
Source: []string{"tests/a.txt", "tests/b.txt"},
|
||||
Target: []string{"/home"},
|
||||
CommandTimeout: 60 * time.Second,
|
||||
TarExec: "tar",
|
||||
},
|
||||
}
|
||||
|
||||
err := plugin.Exec()
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestNoPermissionCreateFolder(t *testing.T) {
|
||||
u, err := user.Lookup("drone-scp")
|
||||
if err != nil {
|
||||
t.Fatalf("Lookup: %v", err)
|
||||
}
|
||||
|
||||
plugin := Plugin{
|
||||
Config: Config{
|
||||
Host: []string{"localhost"},
|
||||
Username: "drone-scp",
|
||||
Port: "22",
|
||||
KeyPath: "tests/.ssh/id_rsa",
|
||||
Source: []string{"tests/a.txt", "tests/b.txt"},
|
||||
Target: []string{"/etc/test"},
|
||||
CommandTimeout: 60 * time.Second,
|
||||
TarExec: "tar",
|
||||
},
|
||||
}
|
||||
|
||||
err = plugin.Exec()
|
||||
assert.NotNil(t, err)
|
||||
|
||||
// check tmp file exist
|
||||
if _, err = os.Stat(filepath.Join(u.HomeDir, plugin.DestFile)); os.IsExist(err) {
|
||||
t.Fatalf("SCP-error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGlobList(t *testing.T) {
|
||||
// wrong patern
|
||||
paterns := []string{"[]a]", "tests/?.txt"}
|
||||
expects := []string{"tests/a.txt", "tests/b.txt"}
|
||||
assert.Equal(t, expects, globList(paterns).Source)
|
||||
|
||||
paterns = []string{"tests/*.txt", "tests/.ssh/*", "abc*"}
|
||||
expects = []string{"tests/a.txt", "tests/b.txt", "tests/.ssh/id_rsa", "tests/.ssh/id_rsa.pub"}
|
||||
assert.Equal(t, expects, globList(paterns).Source)
|
||||
|
||||
paterns = []string{"tests/?.txt"}
|
||||
expects = []string{"tests/a.txt", "tests/b.txt"}
|
||||
assert.Equal(t, expects, globList(paterns).Source)
|
||||
|
||||
// remove item which file not found.
|
||||
paterns = []string{"tests/aa.txt", "tests/b.txt"}
|
||||
expects = []string{"tests/b.txt"}
|
||||
assert.Equal(t, expects, globList(paterns).Source)
|
||||
|
||||
paterns = []string{"./tests/b.txt"}
|
||||
expects = []string{"./tests/b.txt"}
|
||||
assert.Equal(t, expects, globList(paterns).Source)
|
||||
|
||||
paterns = []string{"./tests/*.txt", "!./tests/b.txt"}
|
||||
expectSources := []string{"tests/a.txt", "tests/b.txt"}
|
||||
expectIgnores := []string{"./tests/b.txt"}
|
||||
result := globList(paterns)
|
||||
assert.Equal(t, expectSources, result.Source)
|
||||
assert.Equal(t, expectIgnores, result.Ignore)
|
||||
}
|
||||
|
||||
func TestBuildArgs(t *testing.T) {
|
||||
list := fileList{
|
||||
Source: []string{"tests/a.txt", "tests/b.txt", "tests/c.txt"},
|
||||
Ignore: []string{"tests/a.txt", "tests/b.txt"},
|
||||
}
|
||||
|
||||
result := buildArgs("test.tar.gz", list)
|
||||
expects := []string{"--exclude", "tests/a.txt", "--exclude", "tests/b.txt", "-cf", "test.tar.gz", "tests/a.txt", "tests/b.txt", "tests/c.txt"}
|
||||
assert.Equal(t, expects, result)
|
||||
|
||||
list = fileList{
|
||||
Source: []string{"tests/a.txt", "tests/b.txt"},
|
||||
}
|
||||
|
||||
result = buildArgs("test.tar.gz", list)
|
||||
expects = []string{"-cf", "test.tar.gz", "tests/a.txt", "tests/b.txt"}
|
||||
assert.Equal(t, expects, result)
|
||||
}
|
||||
|
||||
func TestRemoveDestFile(t *testing.T) {
|
||||
ssh := &easyssh.MakeConfig{
|
||||
Server: "localhost",
|
||||
User: "drone-scp",
|
||||
Port: "22",
|
||||
KeyPath: "tests/.ssh/id_rsa",
|
||||
// io timeout
|
||||
Timeout: 1,
|
||||
}
|
||||
plugin := Plugin{
|
||||
Config: Config{
|
||||
CommandTimeout: 60 * time.Second,
|
||||
},
|
||||
DestFile: "/etc/resolv.conf",
|
||||
}
|
||||
|
||||
// ssh io timeout
|
||||
err := plugin.removeDestFile(ssh)
|
||||
assert.Error(t, err)
|
||||
|
||||
ssh.Timeout = 0
|
||||
|
||||
// permission denied
|
||||
err = plugin.removeDestFile(ssh)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestPlugin_buildArgs(t *testing.T) {
|
||||
type fields struct {
|
||||
Repo Repo
|
||||
Build Build
|
||||
Config Config
|
||||
DestFile string
|
||||
}
|
||||
type args struct {
|
||||
target string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "default command",
|
||||
fields: fields{
|
||||
Config: Config{
|
||||
Overwrite: false,
|
||||
TarExec: "tar",
|
||||
},
|
||||
DestFile: "foo.tar",
|
||||
},
|
||||
args: args{
|
||||
target: "foo",
|
||||
},
|
||||
want: []string{"tar", "-xf", "foo.tar", "-C", "foo"},
|
||||
},
|
||||
{
|
||||
name: "strip components",
|
||||
fields: fields{
|
||||
Config: Config{
|
||||
Overwrite: false,
|
||||
TarExec: "tar",
|
||||
StripComponents: 2,
|
||||
},
|
||||
DestFile: "foo.tar",
|
||||
},
|
||||
args: args{
|
||||
target: "foo",
|
||||
},
|
||||
want: []string{"tar", "-xf", "foo.tar", "--strip-components", "2", "-C", "foo"},
|
||||
},
|
||||
{
|
||||
name: "overwrite",
|
||||
fields: fields{
|
||||
Config: Config{
|
||||
TarExec: "tar",
|
||||
StripComponents: 2,
|
||||
Overwrite: true,
|
||||
},
|
||||
DestFile: "foo.tar",
|
||||
},
|
||||
args: args{
|
||||
target: "foo",
|
||||
},
|
||||
want: []string{"tar", "-xf", "foo.tar", "--strip-components", "2", "--overwrite", "-C", "foo"},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
p := &Plugin{
|
||||
Repo: tt.fields.Repo,
|
||||
Build: tt.fields.Build,
|
||||
Config: tt.fields.Config,
|
||||
DestFile: tt.fields.DestFile,
|
||||
}
|
||||
if got := p.buildArgs(tt.args.target); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("Plugin.buildArgs() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
MIIEpAIBAAKCAQEA4e2D/qPN08pzTac+a8ZmlP1ziJOXk45CynMPtva0rtK/RB26
|
||||
VbfAF0hIJji7ltvnYnqCU9oFfvEM33cTn7T96+od8ib/Vz25YU8ZbstqtIskPuwC
|
||||
bv3K0mAHgsviJyRD7yM+QKTbBQEgbGuW6gtbMKhiYfiIB4Dyj7AdS/fk3v26wDgz
|
||||
7SHI5OBqu9bv1KhxQYdFEnU3PAtAqeccgzNpbH3eYLyGzuUxEIJlhpZ/uU2G9ppj
|
||||
/cSrONVPiI8Ahi4RrlZjmP5l57/sq1ClGulyLpFcMw68kP5FikyqHpHJHRBNgU57
|
||||
1y0Ph33SjBbs0haCIAcmreWEhGe+/OXnJe6VUQIDAQABAoIBAH97emORIm9DaVSD
|
||||
7mD6DqA7c5m5Tmpgd6eszU08YC/Vkz9oVuBPUwDQNIX8tT0m0KVs42VVPIyoj874
|
||||
bgZMJoucC1G8V5Bur9AMxhkShx9g9A7dNXJTmsKilRpk2TOk7wBdLp9jZoKoZBdJ
|
||||
jlp6FfaazQjjKD6zsCsMATwAoRCBpBNsmT6QDN0n0bIgY0tE6YGQaDdka0dAv68G
|
||||
R0VZrcJ9voT6+f+rgJLoojn2DAu6iXaM99Gv8FK91YCymbQlXXgrk6CyS0IHexN7
|
||||
V7a3k767KnRbrkqd3o6JyNun/CrUjQwHs1IQH34tvkWScbseRaFehcAm6mLT93RP
|
||||
muauvMECgYEA9AXGtfDMse0FhvDPZx4mx8x+vcfsLvDHcDLkf/lbyPpu97C27b/z
|
||||
ia07bu5TAXesUZrWZtKA5KeRE5doQSdTOv1N28BEr8ZwzDJwfn0DPUYUOxsN2iIy
|
||||
MheO5A45Ko7bjKJVkZ61Mb1UxtqCTF9mqu9R3PBdJGthWOd+HUvF460CgYEA7QRf
|
||||
Z8+vpGA+eSuu29e0xgRKnRzed5zXYpcI4aERc3JzBgO4Z0er9G8l66OWVGdMfpe6
|
||||
CBajC5ToIiT8zqoYxXwqJgN+glir4gJe3mm8J703QfArZiQrdk0NTi5bY7+vLLG/
|
||||
knTrtpdsKih6r3kjhuPPaAsIwmMxIydFvATKjLUCgYEAh/y4EihRSk5WKC8GxeZt
|
||||
oiZ58vT4z+fqnMIfyJmD5up48JuQNcokw/LADj/ODiFM7GUnWkGxBrvDA3H67WQm
|
||||
49bJjs8E+BfUQFdTjYnJRlpJZ+7Zt1gbNQMf5ENw5CCchTDqEq6pN0DVf8PBnSIF
|
||||
KvkXW9KvdV5J76uCAn15mDkCgYA1y8dHzbjlCz9Cy2pt1aDfTPwOew33gi7U3skS
|
||||
RTerx29aDyAcuQTLfyrROBkX4TZYiWGdEl5Bc7PYhCKpWawzrsH2TNa7CRtCOh2E
|
||||
R+V/84+GNNf04ALJYCXD9/ugQVKmR1XfDRCvKeFQFE38Y/dvV2etCswbKt5tRy2p
|
||||
xkCe/QKBgQCkLqafD4S20YHf6WTp3jp/4H/qEy2X2a8gdVVBi1uKkGDXr0n+AoVU
|
||||
ib4KbP5ovZlrjL++akMQ7V2fHzuQIFWnCkDA5c2ZAqzlM+ZN+HRG7gWur7Bt4XH1
|
||||
7XC9wlRna4b3Ln8ew3q1ZcBjXwD4ppbTlmwAfQIaZTGJUgQbdsO9YA==
|
||||
-----END RSA PRIVATE KEY-----
|
||||
@@ -0,0 +1 @@
|
||||
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDh7YP+o83TynNNpz5rxmaU/XOIk5eTjkLKcw+29rSu0r9EHbpVt8AXSEgmOLuW2+dieoJT2gV+8QzfdxOftP3r6h3yJv9XPblhTxluy2q0iyQ+7AJu/crSYAeCy+InJEPvIz5ApNsFASBsa5bqC1swqGJh+IgHgPKPsB1L9+Te/brAODPtIcjk4Gq71u/UqHFBh0USdTc8C0Cp5xyDM2lsfd5gvIbO5TEQgmWGln+5TYb2mmP9xKs41U+IjwCGLhGuVmOY/mXnv+yrUKUa6XIukVwzDryQ/kWKTKoekckdEE2BTnvXLQ+HfdKMFuzSFoIgByat5YSEZ7785ecl7pVR drone-scp@localhost
|
||||
@@ -0,0 +1 @@
|
||||
appleboy
|
||||
Executable
+13
@@ -0,0 +1,13 @@
|
||||
#!/bin/sh
|
||||
|
||||
if [ ! -f "/etc/ssh/ssh_host_rsa_key" ]; then
|
||||
# generate fresh rsa key
|
||||
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N '' -t rsa
|
||||
fi
|
||||
|
||||
if [ ! -f "/etc/ssh/ssh_host_dsa_key" ]; then
|
||||
# generate fresh dsa key
|
||||
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -N '' -t dsa
|
||||
fi
|
||||
|
||||
exec "$@"
|
||||
Reference in New Issue
Block a user