Skip to content

Commit

Permalink
Merge pull request #413 from metrue/master
Browse files Browse the repository at this point in the history
Release 0.8.75
  • Loading branch information
metrue committed Dec 18, 2019
2 parents b93f423 + 3627d5b commit 537a77d
Show file tree
Hide file tree
Showing 14 changed files with 279 additions and 102 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -29,7 +29,7 @@ jobs:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
run: |
export KUBECONFIG="$(kind get kubeconfig-path)"
./scripts/coverage.sh
make unit-test
bash <(curl -s https://codecov.io/bash) -t ${CODECOV_TOKEN}
- name: build fx
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Expand Up @@ -33,7 +33,7 @@ jobs:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
run: |
export KUBECONFIG="$(kind get kubeconfig-path)"
DEBUG=true go test -v ./...
make unit-test
- name: build fx
run: |
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -26,7 +26,7 @@ clean:
rm -rf ${DIST_DIR}

unit-test:
./scripts/coverage.sh
CI=true ./scripts/coverage.sh

cli-test-ci:
./scripts/test_cli.sh 'js'
Expand Down
11 changes: 11 additions & 0 deletions config/config.go
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/user"
"path"
"path/filepath"

dockerInfra "github.com/metrue/fx/infra/docker"
"github.com/metrue/fx/types"
Expand All @@ -22,6 +23,7 @@ type Configer interface {
UseCloud(name string) error
View() ([]byte, error)
AddCloud(name string, meta []byte) error
Dir() (string, error)
}

// Config config of fx
Expand Down Expand Up @@ -191,6 +193,15 @@ func (c *Config) writeDefaultConfig() error {
return c.UseCloud("default")
}

// Dir get directory of config
func (c *Config) Dir() (string, error) {
p, err := filepath.Abs(c.configFile)
if err != nil {
return "", err
}
return path.Dir(p), nil
}

var (
_ Configer = &Config{}
)
62 changes: 41 additions & 21 deletions config/config_test.go
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"os/user"
"path/filepath"
"reflect"
"testing"

Expand Down Expand Up @@ -49,32 +50,39 @@ func TestConfig(t *testing.T) {
t.Fatalf("should get %s but got %s", "default", cloudMeta["name"])
}

// add k8s cloud
kCloud := k8sInfra.Cloud{
Type: types.CloudTypeK8S,
Config: "sample kubeconfg",
Token: "",
URL: "",
Nodes: map[string]k8sInfra.Noder{
"master-node": &k8sInfra.Node{
IP: "1.1.1.1",
User: "user-1",
Type: "k3s-master",
Name: "master-node",
},
"agent-node-1": &k8sInfra.Node{
IP: "1.1.1.1",
User: "user-1",
Type: "k3s-agent",
Name: "agent-node-1",
},
},
n1, err := k8sInfra.CreateNode(
"1.1.1.1",
"user-1",
"k3s-master",
"master-node",
)
if err != nil {
t.Fatal(err)
}
kMeta, err := kCloud.Dump()
n2, err := k8sInfra.CreateNode(
"1.1.1.1",
"user-1",
"k3s-agent",
"agent-node-1",
)
if err != nil {
t.Fatal(err)
}

kName := "k8s-1"
kubeconf := "./tmp/" + kName + "config.yml"
defer func() {
if err := os.RemoveAll(kubeconf); err != nil {
t.Fatal(err)
}
}()

// add k8s cloud
kCloud := k8sInfra.NewCloud(kubeconf, n1, n2)
kMeta, err := kCloud.Dump()
if err != nil {
t.Fatal(err)
}
if err := c.AddCloud(kName, kMeta); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -108,4 +116,16 @@ func TestConfig(t *testing.T) {
t.Fatal(err)
}
fmt.Println(string(body))

dir, err := c.Dir()
if err != nil {
t.Fatal(err)
}
here, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
if dir != filepath.Join(here, "./tmp") {
t.Fatalf("should get %s but got %s", "./tmp", dir)
}
}
2 changes: 1 addition & 1 deletion fx.go
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/urfave/cli"
)

const version = "0.8.74"
const version = "0.8.75"

func init() {
go checkForUpdate()
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Expand Up @@ -40,8 +40,6 @@ require (
github.com/urfave/cli v1.22.2
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7 // indirect
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
google.golang.org/grpc v1.21.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.2.7
gotest.tools v2.2.0+incompatible // indirect
Expand Down
12 changes: 9 additions & 3 deletions handlers/infra.go
Expand Up @@ -2,6 +2,7 @@ package handlers

import (
"fmt"
"path/filepath"
"strings"

"github.com/metrue/fx/config"
Expand All @@ -11,7 +12,7 @@ import (
"github.com/metrue/fx/pkg/spinner"
)

func setupK8S(masterInfo string, agentsInfo string) ([]byte, error) {
func setupK8S(configDir string, name, masterInfo string, agentsInfo string) ([]byte, error) {
info := strings.Split(masterInfo, "@")
if len(info) != 2 {
return nil, fmt.Errorf("incorrect master info, should be <user>@<ip> format")
Expand All @@ -36,7 +37,8 @@ func setupK8S(masterInfo string, agentsInfo string) ([]byte, error) {
nodes = append(nodes, node)
}
}
cloud := k8sInfra.NewCloud(nodes...)
kubeconfigPath := filepath.Join(configDir, name+".kubeconfig")
cloud := k8sInfra.NewCloud(kubeconfigPath, nodes...)
if err := cloud.Provision(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -91,7 +93,11 @@ func Setup(ctx context.Contexter) (err error) {

switch strings.ToLower(typ) {
case "k8s":
kubeconf, err := setupK8S(cli.String("master"), cli.String("agents"))
dir, err := fxConfig.Dir()
if err != nil {
return err
}
kubeconf, err := setupK8S(dir, name, cli.String("master"), cli.String("agents"))
if err != nil {
return err
}
Expand Down
50 changes: 45 additions & 5 deletions infra/docker/cloud.go
Expand Up @@ -2,8 +2,11 @@ package docker

import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/metrue/fx/infra"
"github.com/metrue/fx/types"
Expand Down Expand Up @@ -68,18 +71,18 @@ func Load(meta []byte) (*Cloud, error) {

// Provision a host
func (c *Cloud) Provision() error {
if err := c.sshClient.RunCommand(infra.Scripts["docker_version"].(string), ssh.CommandOptions{}); err != nil {
if err := c.sshClient.RunCommand(infra.Scripts["install_docker"].(string), ssh.CommandOptions{}); err != nil {
if err := c.runCmd(infra.Scripts["docker_version"].(string)); err != nil {
if err := c.runCmd(infra.Scripts["install_docker"].(string)); err != nil {
return err
}

if err := c.sshClient.RunCommand(infra.Scripts["start_dockerd"].(string), ssh.CommandOptions{}); err != nil {
if err := c.runCmd(infra.Scripts["start_dockerd"].(string)); err != nil {
return err
}
}

if err := c.sshClient.RunCommand(infra.Scripts["check_fx_agent"].(string), ssh.CommandOptions{}); err != nil {
if err := c.sshClient.RunCommand(infra.Scripts["start_fx_agent"].(string), ssh.CommandOptions{}); err != nil {
if err := c.runCmd(infra.Scripts["check_fx_agent"].(string)); err != nil {
if err := c.runCmd(infra.Scripts["start_fx_agent"].(string)); err != nil {
return err
}
}
Expand All @@ -103,11 +106,48 @@ func (c *Cloud) Dump() ([]byte, error) {
return json.Marshal(c)
}

// IsHealth check if cloud is in health
func (c *Cloud) IsHealth() (bool, error) {
if err := c.runCmd(infra.Scripts["check_fx_agent"].(string)); err != nil {
if err := c.runCmd(infra.Scripts["start_fx_agent"].(string)); err != nil {
return false, err
}
}
return true, nil
}

// NOTE only using for unit testing
func (c *Cloud) setsshClient(client ssh.Clienter) {
c.sshClient = client
}

// nolint:unparam
func (c *Cloud) runCmd(script string, options ...ssh.CommandOptions) error {
option := ssh.CommandOptions{}
if len(options) >= 1 {
option = options[0]
}

local := c.IP == "127.0.0.1" || c.IP == "localhost"
if local && os.Getenv("CI") == "" {
params := strings.Split(script, " ")
if len(params) == 0 {
return fmt.Errorf("invalid script: %s", script)
}
// nolint
cmd := exec.Command(params[0], params[1:]...)
cmd.Stdout = option.Stdout
cmd.Stderr = option.Stderr
err := cmd.Run()
if err != nil {
return err
}
return nil
}

return c.sshClient.RunCommand(script, option)
}

// NOTE the reason putting sshkey() and sshport here inside node.go is because
// ssh key and ssh port is related to node it self, we may extend this in future
func sshkey() (string, error) {
Expand Down
57 changes: 56 additions & 1 deletion infra/docker/cloud_test.go
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/mitchellh/go-homedir"
)

func TestCloud(t *testing.T) {
func TestCloudProvision(t *testing.T) {
t.Run("fx agent started", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
Expand Down Expand Up @@ -60,6 +60,61 @@ func TestCloud(t *testing.T) {
})
}

func TestCloudIsHealth(t *testing.T) {
t.Run("agent started", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
cloud := New("127.0.0.1", "fx", "master")
sshClient := sshMocks.NewMockClienter(ctrl)
cloud.setsshClient(sshClient)

sshClient.EXPECT().RunCommand(infra.Scripts["check_fx_agent"].(string), ssh.CommandOptions{}).Return(nil)
ok, err := cloud.IsHealth()
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatalf("cloud should be healthy")
}
})

t.Run("agent not started, and retart ok", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
cloud := New("127.0.0.1", "fx", "master")
sshClient := sshMocks.NewMockClienter(ctrl)
cloud.setsshClient(sshClient)

sshClient.EXPECT().RunCommand(infra.Scripts["check_fx_agent"].(string), ssh.CommandOptions{}).Return(fmt.Errorf("fx agent not started"))
sshClient.EXPECT().RunCommand(infra.Scripts["start_fx_agent"].(string), ssh.CommandOptions{}).Return(nil)
ok, err := cloud.IsHealth()
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatalf("cloud should be healthy")
}
})

t.Run("agent not started, but restart failed", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
cloud := New("127.0.0.1", "fx", "master")
sshClient := sshMocks.NewMockClienter(ctrl)
cloud.setsshClient(sshClient)

sshClient.EXPECT().RunCommand(infra.Scripts["check_fx_agent"].(string), ssh.CommandOptions{}).Return(fmt.Errorf("fx agent not started"))
sshClient.EXPECT().RunCommand(infra.Scripts["start_fx_agent"].(string), ssh.CommandOptions{}).Return(fmt.Errorf("fx agent started failed"))
ok, err := cloud.IsHealth()
if err == nil {
t.Fatal("should got failed starting")
}
if ok {
t.Fatalf("cloud should not be healthy")
}
})
}

func TestGetSSHKeyFile(t *testing.T) {
t.Run("defaut", func(t *testing.T) {
defau, err := sshkey()
Expand Down
1 change: 1 addition & 0 deletions infra/infra.go
Expand Up @@ -12,6 +12,7 @@ type Clouder interface {
GetConfig() (string, error)
GetType() string
Dump() ([]byte, error)
IsHealth() (bool, error)
}

// Deployer deploy interface
Expand Down

0 comments on commit 537a77d

Please sign in to comment.