Compare commits

..

7 Commits

Author SHA1 Message Date
Minghe Huang
63bc06a993 add task name
replace spinner
2020-05-07 20:01:28 +08:00
Minghe Huang
833b7d06a3 bump version 2020-05-07 18:23:02 +08:00
dependabot-preview[bot]
1ae68f1076 Bump github.com/briandowns/spinner from 1.10.0 to 1.11.1 (#521)
Bumps [github.com/briandowns/spinner](https://github.com/briandowns/spinner) from 1.10.0 to 1.11.1.
- [Release notes](https://github.com/briandowns/spinner/releases)
- [Commits](https://github.com/briandowns/spinner/compare/v1.10.0...v1.11.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
Co-authored-by: Minghe <h.minghe@gmail.com>
2020-05-07 16:53:04 +08:00
Minghe
5ce61ef200 Fix the wrong arch of target host (#524)
* Fix the wrong arch of target host,
* get the OS arch with `uname -a`
* refactor the SSHConnectionTimeout constant
* only provision on remote host
2020-05-07 16:52:45 +08:00
dependabot-preview[bot]
3923c3451f Bump github.com/gin-gonic/gin from 1.6.2 to 1.6.3 (#522)
Bumps [github.com/gin-gonic/gin](https://github.com/gin-gonic/gin) from 1.6.2 to 1.6.3.
- [Release notes](https://github.com/gin-gonic/gin/releases)
- [Changelog](https://github.com/gin-gonic/gin/blob/master/CHANGELOG.md)
- [Commits](https://github.com/gin-gonic/gin/compare/v1.6.2...v1.6.3)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
2020-05-05 10:02:49 +08:00
Massimiliano Mirra
8741db25b6 use lighter-weight node image (#518) 2020-04-28 10:55:13 +08:00
Minghe
16d20b23a7 fix gosec issue (#519) 2020-04-28 10:43:04 +08:00
18 changed files with 131 additions and 103 deletions

View File

@@ -1,4 +1,4 @@
FROM node:latest
FROM node:alpine
COPY . .
RUN npm install

View File

@@ -46,7 +46,7 @@ func Restore(box *packr.Box, output string) error {
return err
}
if err := ioutil.WriteFile(dest, content, 0644); err != nil {
if err := ioutil.WriteFile(dest, content, 0600); err != nil {
return err
}

View File

@@ -1,4 +1,4 @@
FROM node:latest
FROM node:alpine
COPY . .
RUN npm install

2
fx.go
View File

@@ -19,7 +19,7 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)
const version = "0.9.35"
const version = "0.9.40"
func init() {
go checkForUpdate()

5
go.mod
View File

@@ -6,13 +6,14 @@ require (
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/Microsoft/go-winio v0.4.14 // indirect
github.com/apex/log v1.1.4
github.com/briandowns/spinner v1.10.0
github.com/briandowns/spinner v1.11.1
github.com/cheggaaa/pb/v3 v3.0.4
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v0.0.0-20190313072916-46036c230805
github.com/docker/go-connections v0.4.0
github.com/docker/go-units v0.3.3 // indirect
github.com/dsnet/compress v0.0.1 // indirect
github.com/gin-gonic/gin v1.6.2
github.com/gin-gonic/gin v1.6.3
github.com/gobuffalo/envy v1.8.1 // indirect
github.com/gobuffalo/packd v1.0.0
github.com/gobuffalo/packr/v2 v2.8.0

13
go.sum
View File

@@ -19,6 +19,8 @@ github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb0
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/PuerkitoBio/purell v1.0.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20160726150825-5bd2802263f2/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/VividCortex/ewma v1.1.1 h1:MnEK4VOv6n0RSY4vtRe3h11qjxL3+t0B8yOL8iMXdcM=
github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/apex/log v1.1.2 h1:bnDuVoi+o98wOdVqfEzNDlY0tcmBia7r4YkjS9EqGYk=
@@ -38,7 +40,13 @@ github.com/briandowns/spinner v1.9.0 h1:+OMAisemaHar1hjuJ3Z2hIvNhQl9Y7GLPWUwwz2P
github.com/briandowns/spinner v1.9.0/go.mod h1://Zf9tMcxfRUA36V23M6YGEAv+kECGfvpnLTnb8n4XQ=
github.com/briandowns/spinner v1.10.0 h1:753NIJC2NHmPyVoPVWS+wh9eDx5umqe2U+JgX+KoTag=
github.com/briandowns/spinner v1.10.0/go.mod h1:QOuQk7x+EaDASo80FEXwlwiA+j/PPIcX3FScO+3/ZPQ=
github.com/briandowns/spinner v1.11.1 h1:OixPqDEcX3juo5AjQZAnFPbeUA0jvkp2qzB5gOZJ/L0=
github.com/briandowns/spinner v1.11.1/go.mod h1:QOuQk7x+EaDASo80FEXwlwiA+j/PPIcX3FScO+3/ZPQ=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cheggaaa/pb v1.0.28 h1:kWGpdAcSp3MxMU9CCHOwz/8V0kCHN4+9yQm2MzWuI98=
github.com/cheggaaa/pb v2.0.7+incompatible h1:gLKifR1UkZ/kLkda5gC0K6c8g+jU2sINPtBeOiNlMhU=
github.com/cheggaaa/pb/v3 v3.0.4 h1:QZEPYOj2ix6d5oEg63fbHmpolrnNiwjUsk+h74Yt4bM=
github.com/cheggaaa/pb/v3 v3.0.4/go.mod h1:7rgWxLrAUcFMkvJuv09+DYi7mMUYi8nO9iOWcvGJPfw=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
@@ -90,6 +98,8 @@ github.com/gin-gonic/gin v1.4.0 h1:3tMoCCfM7ppqsR0ptz/wi1impNpT7/9wQtMZ8lr1mCQ=
github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/3rZdM=
github.com/gin-gonic/gin v1.6.2 h1:88crIK23zO6TqlQBt+f9FrPJNKm9ZEr7qjp9vl/d5TM=
github.com/gin-gonic/gin v1.6.2/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
@@ -243,6 +253,7 @@ github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
@@ -480,6 +491,8 @@ golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190515120540-06a5c4944438/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191223224216-5a3cf8467b4e h1:z2Flw7sLy7DxaQi3zDOvI9X+Kb06+G9iZJlkEyHvujE=
golang.org/x/sys v0.0.0-20191223224216-5a3cf8467b4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=

View File

@@ -1,8 +1,9 @@
package middlewares
import (
"bytes"
"fmt"
"runtime"
"strings"
"time"
"github.com/apex/log"
@@ -12,7 +13,7 @@ import (
dockerDriver "github.com/metrue/fx/driver/docker"
k8sInfra "github.com/metrue/fx/driver/k8s"
"github.com/metrue/fx/provisioner"
darwin "github.com/metrue/fx/provisioner/darwin"
"github.com/metrue/fx/provisioner/darwin"
linux "github.com/metrue/fx/provisioner/linux"
"github.com/metrue/go-ssh-client"
)
@@ -32,20 +33,34 @@ func Driver(ctx context.Contexter) (err error) {
if err := driver.Ping(ctx.GetContext()); err != nil {
log.Infof("provisioning %s ...", host)
var provisioner provisioner.Provisioner
// TODO actually we should get os of target host, not the host of fx
// running on
if runtime.GOOS == "darwin" {
provisioner = darwin.New(sshClient)
} else {
provisioner = linux.New(sshClient)
}
isRemote := (host != "127.0.0.1" && host != "localhost")
if err := provisioner.Provision(ctx.GetContext(), isRemote); err != nil {
return err
hostOS := "linux"
if isRemote {
ok, err := sshClient.Connectable(provisioner.SSHConnectionTimeout)
if err != nil {
return err
}
if !ok {
return fmt.Errorf("target host could not be connected with SSH")
}
var buf bytes.Buffer
if err := sshClient.RunCommand("uname -a", ssh.CommandOptions{Stdout: &buf}); err != nil {
return err
}
hostOS = buf.String()
}
if strings.Contains(hostOS, "darwin") {
if err := darwin.New(sshClient).Provision(ctx.GetContext(), isRemote); err != nil {
return err
}
} else {
if err := linux.New(sshClient).Provision(ctx.GetContext(), isRemote); err != nil {
return err
}
}
time.Sleep(2 * time.Second)
}
time.Sleep(2 * time.Second)
if err := docker.Initialize(); err != nil {
return fmt.Errorf("initialize docker client failed: %s", err)
}

View File

@@ -52,7 +52,8 @@ users:
defer os.Remove(kubeconf.Name())
sshClient := sshMocks.NewMockClienter(ctrl)
sshClient.EXPECT().Connectable(10*time.Second).Return(true, nil).Times(2)
sshClient.EXPECT().Connectable(10*time.Second).Return(true, nil).Times(3)
sshClient.EXPECT().RunCommand("uname -a", gomock.Any()).Return(nil)
sshClient.EXPECT().RunCommand("docker version", ssh.CommandOptions{
Timeout: 10 * time.Second,
}).Return(nil)

View File

@@ -29,7 +29,6 @@ func set(ctx context.Contexter, cli *cli.Context, fields []argsField) error {
ctx.Set("host", ip)
ctx.Set("user", user)
} else {
ctx.Set(f.Name, cli.String(f.Name))
}
} else if f.Type == "int" {

View File

@@ -44,7 +44,7 @@ func TreeToDir(tree map[string]string, outputDir string) error {
if err := utils.EnsureFile(fn); err != nil {
return err
}
if err := ioutil.WriteFile(fn, []byte(v), 0666); err != nil {
if err := ioutil.WriteFile(fn, []byte(v), 0600); err != nil {
return err
}
}

View File

@@ -2,48 +2,42 @@ package spinner
import (
"fmt"
"math/rand"
"time"
"github.com/briandowns/spinner"
aurora "github.com/logrusorgru/aurora"
"github.com/cheggaaa/pb/v3"
"github.com/logrusorgru/aurora"
)
var s *spinner.Spinner
var bars map[string]*pb.ProgressBar
func init() {
style := spinner.CharSets[36]
interval := 100 * time.Millisecond
s = spinner.New(style, interval)
bars = make(map[string]*pb.ProgressBar)
}
// Start spinner
func Start(task string) {
colors := []string{
"red",
"green",
"yellow",
"blue",
"magenta",
"cyan",
"white",
}
rand.Seed(time.Now().UnixNano())
// nolint
s.Color(colors[rand.Intn(len(colors))])
s.Prefix = task + " "
if s.Active() {
s.Restart()
} else {
s.Start()
count := 100
b, ok := bars[task]
if !ok {
b = pb.StartNew(count)
bars[task] = b
}
go func() {
fmt.Printf("Starting %s\n", task)
for i := 0; i < count; i++ {
b.Increment()
time.Sleep(50 * time.Millisecond)
}
}()
}
// Stop spinner
func Stop(task string, err error) {
if err != nil {
fmt.Println(aurora.Red("\u2717"))
b, ok := bars[task]
if ok {
b.Finish()
}
if err != nil {
fmt.Printf("%s: %s\n", task, aurora.Red("\u2717"))
}
s.Stop()
}

View File

@@ -6,14 +6,11 @@ import (
"fmt"
"os/exec"
"strings"
"time"
"github.com/metrue/fx/provisioner"
"github.com/metrue/go-ssh-client"
)
const sshConnectionTimeout = 10 * time.Second
var scripts = map[string]string{
"docker_version": "docker version",
"has_docker": "type docker",
@@ -52,7 +49,7 @@ func (d *Docker) Provision(ctx context.Context, isRemote bool) error {
func (d *Docker) runCmd(script string, isRemote bool, options ...ssh.CommandOptions) error {
option := ssh.CommandOptions{
Timeout: sshConnectionTimeout,
Timeout: provisioner.SSHConnectionTimeout,
}
if len(options) >= 1 {
option = options[0]
@@ -72,7 +69,7 @@ func (d *Docker) runCmd(script string, isRemote bool, options ...ssh.CommandOpti
}
return nil
}
ok, err := d.sshClient.Connectable(sshConnectionTimeout)
ok, err := d.sshClient.Connectable(provisioner.SSHConnectionTimeout)
if err != nil {
return fmt.Errorf("could not connect via SSH: '%s'", err)
}

View File

@@ -6,6 +6,7 @@ import (
"testing"
"github.com/golang/mock/gomock"
"github.com/metrue/fx/provisioner"
"github.com/metrue/go-ssh-client"
sshMocks "github.com/metrue/go-ssh-client/mocks"
)
@@ -18,7 +19,7 @@ func TestDriverProvision(t *testing.T) {
sshClient := sshMocks.NewMockClienter(ctrl)
n := &Docker{sshClient: sshClient}
err := errors.New("could not connect to host")
sshClient.EXPECT().Connectable(sshConnectionTimeout).Return(false, err).AnyTimes()
sshClient.EXPECT().Connectable(provisioner.SSHConnectionTimeout).Return(false, err).AnyTimes()
if err := n.Provision(context.Background(), true); err == nil {
t.Fatalf("should get error when SSH connection not ok")
}
@@ -30,7 +31,7 @@ func TestDriverProvision(t *testing.T) {
sshClient := sshMocks.NewMockClienter(ctrl)
n := New(sshClient)
sshClient.EXPECT().Connectable(sshConnectionTimeout).Return(false, nil).AnyTimes()
sshClient.EXPECT().Connectable(provisioner.SSHConnectionTimeout).Return(false, nil).AnyTimes()
if err := n.Provision(context.Background(), true); err == nil {
t.Fatalf("should get error when SSH connection not ok")
}
@@ -42,9 +43,9 @@ func TestDriverProvision(t *testing.T) {
sshClient := sshMocks.NewMockClienter(ctrl)
n := New(sshClient)
sshClient.EXPECT().Connectable(sshConnectionTimeout).Return(true, nil).AnyTimes()
sshClient.EXPECT().RunCommand(scripts["docker_version"], ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(nil)
sshClient.EXPECT().RunCommand(scripts["check_fx_agent"], ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(nil)
sshClient.EXPECT().Connectable(provisioner.SSHConnectionTimeout).Return(true, nil).AnyTimes()
sshClient.EXPECT().RunCommand(scripts["docker_version"], ssh.CommandOptions{Timeout: provisioner.SSHConnectionTimeout}).Return(nil)
sshClient.EXPECT().RunCommand(scripts["check_fx_agent"], ssh.CommandOptions{Timeout: provisioner.SSHConnectionTimeout}).Return(nil)
if err := n.Provision(context.Background(), true); err != nil {
t.Fatal(err)
}
@@ -56,10 +57,10 @@ func TestDriverProvision(t *testing.T) {
sshClient := sshMocks.NewMockClienter(ctrl)
n := New(sshClient)
sshClient.EXPECT().Connectable(sshConnectionTimeout).Return(true, nil).AnyTimes()
sshClient.EXPECT().Connectable(provisioner.SSHConnectionTimeout).Return(true, nil).AnyTimes()
err := errors.New("docker command not found")
sshClient.EXPECT().RunCommand(scripts["docker_version"], ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(err)
sshClient.EXPECT().RunCommand(scripts["has_docker"], ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(err)
sshClient.EXPECT().RunCommand(scripts["docker_version"], ssh.CommandOptions{Timeout: provisioner.SSHConnectionTimeout}).Return(err)
sshClient.EXPECT().RunCommand(scripts["has_docker"], ssh.CommandOptions{Timeout: provisioner.SSHConnectionTimeout}).Return(err)
if err := n.Provision(context.Background(), true); err == nil {
t.Fatal("should tell user to install docker first")
}
@@ -71,11 +72,11 @@ func TestDriverProvision(t *testing.T) {
sshClient := sshMocks.NewMockClienter(ctrl)
n := New(sshClient)
sshClient.EXPECT().Connectable(sshConnectionTimeout).Return(true, nil).AnyTimes()
sshClient.EXPECT().Connectable(provisioner.SSHConnectionTimeout).Return(true, nil).AnyTimes()
err := errors.New("fx agent not found")
sshClient.EXPECT().RunCommand(scripts["docker_version"], ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(nil)
sshClient.EXPECT().RunCommand(scripts["check_fx_agent"], ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(err)
sshClient.EXPECT().RunCommand(scripts["start_fx_agent"], ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(nil)
sshClient.EXPECT().RunCommand(scripts["docker_version"], ssh.CommandOptions{Timeout: provisioner.SSHConnectionTimeout}).Return(nil)
sshClient.EXPECT().RunCommand(scripts["check_fx_agent"], ssh.CommandOptions{Timeout: provisioner.SSHConnectionTimeout}).Return(err)
sshClient.EXPECT().RunCommand(scripts["start_fx_agent"], ssh.CommandOptions{Timeout: provisioner.SSHConnectionTimeout}).Return(nil)
if err := n.Provision(context.Background(), true); err != nil {
t.Fatal(err)
}
@@ -88,9 +89,9 @@ func TestDriverProvision(t *testing.T) {
sshClient := sshMocks.NewMockClienter(ctrl)
n := New(sshClient)
sshClient.EXPECT().Connectable(sshConnectionTimeout).Return(true, nil).AnyTimes()
sshClient.EXPECT().RunCommand(scripts["docker_version"], ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(nil)
sshClient.EXPECT().RunCommand(scripts["check_fx_agent"], ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(nil)
sshClient.EXPECT().Connectable(provisioner.SSHConnectionTimeout).Return(true, nil).AnyTimes()
sshClient.EXPECT().RunCommand(scripts["docker_version"], ssh.CommandOptions{Timeout: provisioner.SSHConnectionTimeout}).Return(nil)
sshClient.EXPECT().RunCommand(scripts["check_fx_agent"], ssh.CommandOptions{Timeout: provisioner.SSHConnectionTimeout}).Return(nil)
if err := n.Provision(context.Background(), true); err != nil {
t.Fatal(err)
}
@@ -107,9 +108,9 @@ func TestRunCommand(t *testing.T) {
}
script := "script"
option := ssh.CommandOptions{
Timeout: sshConnectionTimeout,
Timeout: provisioner.SSHConnectionTimeout,
}
sshClient.EXPECT().Connectable(sshConnectionTimeout).Return(true, nil)
sshClient.EXPECT().Connectable(provisioner.SSHConnectionTimeout).Return(true, nil)
sshClient.EXPECT().RunCommand(script, option).Return(nil)
if err := n.runCmd(script, true, option); err != nil {
t.Fatal(err)

View File

@@ -5,14 +5,11 @@ import (
"fmt"
"os/exec"
"strings"
"time"
"github.com/metrue/fx/provisioner"
"github.com/metrue/go-ssh-client"
)
const sshConnectionTimeout = 10 * time.Second
var scripts = map[string]interface{}{
"docker_version": "docker version",
"has_docker": "type docker",
@@ -58,7 +55,7 @@ func (d *Docker) Provision(ctx context.Context, isRemote bool) error {
func (d *Docker) runCmd(script string, isRemote bool, options ...ssh.CommandOptions) error {
option := ssh.CommandOptions{
Timeout: sshConnectionTimeout,
Timeout: provisioner.SSHConnectionTimeout,
}
if len(options) >= 1 {
option = options[0]
@@ -78,7 +75,7 @@ func (d *Docker) runCmd(script string, isRemote bool, options ...ssh.CommandOpti
}
return nil
}
ok, err := d.sshClient.Connectable(sshConnectionTimeout)
ok, err := d.sshClient.Connectable(provisioner.SSHConnectionTimeout)
if err != nil {
return fmt.Errorf("could not connect via SSH: '%s'", err)
}

View File

@@ -6,6 +6,7 @@ import (
"testing"
"github.com/golang/mock/gomock"
"github.com/metrue/fx/provisioner"
"github.com/metrue/go-ssh-client"
sshMocks "github.com/metrue/go-ssh-client/mocks"
)
@@ -18,7 +19,7 @@ func TestDriverProvision(t *testing.T) {
sshClient := sshMocks.NewMockClienter(ctrl)
n := &Docker{sshClient: sshClient}
err := errors.New("could not connect to host")
sshClient.EXPECT().Connectable(sshConnectionTimeout).Return(false, err).AnyTimes()
sshClient.EXPECT().Connectable(provisioner.SSHConnectionTimeout).Return(false, err).AnyTimes()
if err := n.Provision(context.Background(), true); err == nil {
t.Fatalf("should get error when SSH connection not ok")
}
@@ -30,7 +31,7 @@ func TestDriverProvision(t *testing.T) {
sshClient := sshMocks.NewMockClienter(ctrl)
n := New(sshClient)
sshClient.EXPECT().Connectable(sshConnectionTimeout).Return(false, nil).AnyTimes()
sshClient.EXPECT().Connectable(provisioner.SSHConnectionTimeout).Return(false, nil).AnyTimes()
if err := n.Provision(context.Background(), true); err == nil {
t.Fatalf("should get error when SSH connection not ok")
}
@@ -42,9 +43,9 @@ func TestDriverProvision(t *testing.T) {
sshClient := sshMocks.NewMockClienter(ctrl)
n := New(sshClient)
sshClient.EXPECT().Connectable(sshConnectionTimeout).Return(true, nil).AnyTimes()
sshClient.EXPECT().RunCommand(scripts["docker_version"].(string), ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(nil)
sshClient.EXPECT().RunCommand(scripts["check_fx_agent"].(string), ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(nil)
sshClient.EXPECT().Connectable(provisioner.SSHConnectionTimeout).Return(true, nil).AnyTimes()
sshClient.EXPECT().RunCommand(scripts["docker_version"].(string), ssh.CommandOptions{Timeout: provisioner.SSHConnectionTimeout}).Return(nil)
sshClient.EXPECT().RunCommand(scripts["check_fx_agent"].(string), ssh.CommandOptions{Timeout: provisioner.SSHConnectionTimeout}).Return(nil)
if err := n.Provision(context.Background(), true); err != nil {
t.Fatal(err)
}
@@ -56,13 +57,13 @@ func TestDriverProvision(t *testing.T) {
sshClient := sshMocks.NewMockClienter(ctrl)
n := New(sshClient)
sshClient.EXPECT().Connectable(sshConnectionTimeout).Return(true, nil).AnyTimes()
sshClient.EXPECT().Connectable(provisioner.SSHConnectionTimeout).Return(true, nil).AnyTimes()
err := errors.New("docker command not found")
sshClient.EXPECT().RunCommand(scripts["docker_version"].(string), ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(err)
sshClient.EXPECT().RunCommand(scripts["has_docker"].(string), ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(err)
sshClient.EXPECT().RunCommand(scripts["install_docker"].(string), ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(nil)
sshClient.EXPECT().RunCommand(scripts["start_dockerd"].(string), ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(nil)
sshClient.EXPECT().RunCommand(scripts["check_fx_agent"].(string), ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(nil)
sshClient.EXPECT().RunCommand(scripts["docker_version"].(string), ssh.CommandOptions{Timeout: provisioner.SSHConnectionTimeout}).Return(err)
sshClient.EXPECT().RunCommand(scripts["has_docker"].(string), ssh.CommandOptions{Timeout: provisioner.SSHConnectionTimeout}).Return(err)
sshClient.EXPECT().RunCommand(scripts["install_docker"].(string), ssh.CommandOptions{Timeout: provisioner.SSHConnectionTimeout}).Return(nil)
sshClient.EXPECT().RunCommand(scripts["start_dockerd"].(string), ssh.CommandOptions{Timeout: provisioner.SSHConnectionTimeout}).Return(nil)
sshClient.EXPECT().RunCommand(scripts["check_fx_agent"].(string), ssh.CommandOptions{Timeout: provisioner.SSHConnectionTimeout}).Return(nil)
if err := n.Provision(context.Background(), true); err != nil {
t.Fatal(err)
}
@@ -74,11 +75,11 @@ func TestDriverProvision(t *testing.T) {
sshClient := sshMocks.NewMockClienter(ctrl)
n := New(sshClient)
sshClient.EXPECT().Connectable(sshConnectionTimeout).Return(true, nil).AnyTimes()
sshClient.EXPECT().Connectable(provisioner.SSHConnectionTimeout).Return(true, nil).AnyTimes()
err := errors.New("fx agent not found")
sshClient.EXPECT().RunCommand(scripts["docker_version"].(string), ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(nil)
sshClient.EXPECT().RunCommand(scripts["check_fx_agent"].(string), ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(err)
sshClient.EXPECT().RunCommand(scripts["start_fx_agent"].(string), ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(nil)
sshClient.EXPECT().RunCommand(scripts["docker_version"].(string), ssh.CommandOptions{Timeout: provisioner.SSHConnectionTimeout}).Return(nil)
sshClient.EXPECT().RunCommand(scripts["check_fx_agent"].(string), ssh.CommandOptions{Timeout: provisioner.SSHConnectionTimeout}).Return(err)
sshClient.EXPECT().RunCommand(scripts["start_fx_agent"].(string), ssh.CommandOptions{Timeout: provisioner.SSHConnectionTimeout}).Return(nil)
if err := n.Provision(context.Background(), true); err != nil {
t.Fatal(err)
}
@@ -91,15 +92,15 @@ func TestDriverProvision(t *testing.T) {
sshClient := sshMocks.NewMockClienter(ctrl)
n := New(sshClient)
sshClient.EXPECT().Connectable(sshConnectionTimeout).Return(true, nil).AnyTimes()
sshClient.EXPECT().Connectable(provisioner.SSHConnectionTimeout).Return(true, nil).AnyTimes()
err2 := errors.New("fx agent not found")
err1 := errors.New("docker command not found")
sshClient.EXPECT().RunCommand(scripts["docker_version"].(string), ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(err1)
sshClient.EXPECT().RunCommand(scripts["has_docker"].(string), ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(err1)
sshClient.EXPECT().RunCommand(scripts["install_docker"].(string), ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(nil)
sshClient.EXPECT().RunCommand(scripts["start_dockerd"].(string), ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(nil)
sshClient.EXPECT().RunCommand(scripts["check_fx_agent"].(string), ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(err2)
sshClient.EXPECT().RunCommand(scripts["start_fx_agent"].(string), ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(nil)
sshClient.EXPECT().RunCommand(scripts["docker_version"].(string), ssh.CommandOptions{Timeout: provisioner.SSHConnectionTimeout}).Return(err1)
sshClient.EXPECT().RunCommand(scripts["has_docker"].(string), ssh.CommandOptions{Timeout: provisioner.SSHConnectionTimeout}).Return(err1)
sshClient.EXPECT().RunCommand(scripts["install_docker"].(string), ssh.CommandOptions{Timeout: provisioner.SSHConnectionTimeout}).Return(nil)
sshClient.EXPECT().RunCommand(scripts["start_dockerd"].(string), ssh.CommandOptions{Timeout: provisioner.SSHConnectionTimeout}).Return(nil)
sshClient.EXPECT().RunCommand(scripts["check_fx_agent"].(string), ssh.CommandOptions{Timeout: provisioner.SSHConnectionTimeout}).Return(err2)
sshClient.EXPECT().RunCommand(scripts["start_fx_agent"].(string), ssh.CommandOptions{Timeout: provisioner.SSHConnectionTimeout}).Return(nil)
if err := n.Provision(context.Background(), true); err != nil {
t.Fatal(err)
}
@@ -116,9 +117,9 @@ func TestRunCommand(t *testing.T) {
}
script := "script"
option := ssh.CommandOptions{
Timeout: sshConnectionTimeout,
Timeout: provisioner.SSHConnectionTimeout,
}
sshClient.EXPECT().Connectable(sshConnectionTimeout).Return(true, nil)
sshClient.EXPECT().Connectable(provisioner.SSHConnectionTimeout).Return(true, nil)
sshClient.EXPECT().RunCommand(script, option).Return(nil)
if err := n.runCmd(script, true, option); err != nil {
t.Fatal(err)

View File

@@ -1,6 +1,12 @@
package provisioner
import "context"
import (
"context"
"time"
)
// SSHConnectionTimeout default timeout for ssh connection
const SSHConnectionTimeout = 10 * time.Second
// Provisioner define provisioner interface
type Provisioner interface {

View File

@@ -26,7 +26,7 @@ func Merge(dest string, input ...string) error {
if err != nil {
return err
}
if err := ioutil.WriteFile(targetFilePath, body, 0644); err != nil {
if err := ioutil.WriteFile(targetFilePath, body, 0600); err != nil {
return err
}
} else if stat.Mode().IsDir() {

View File

@@ -29,6 +29,7 @@ func Download(filepath string, url string) (err error) {
}
defer resp.Body.Close()
// nolint: gosec
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
@@ -70,6 +71,7 @@ func Unzip(source string, target string) (err error) {
}
defer targetFile.Close()
// nolint: gosec
if _, err := io.Copy(targetFile, fileReader); err != nil {
return err
}
@@ -96,6 +98,7 @@ func CopyFile(src, dst string) (err error) {
}
}()
// nolint: gosec
_, err = io.Copy(out, in)
if err != nil {
return