This is an automated email from the ASF dual-hosted git repository.

zhangke pushed a commit to branch initializer
in repository https://gitbox.apache.org/repos/asf/skywalking-infra-e2e.git

commit 0ca17a7ade2b469fe8b6967ce3699ab67082597d
Author: HumbertZhang <[email protected]>
AuthorDate: Sun Jan 3 18:56:12 2021 +0800

    setup: add kind create and clean cluster
---
 commands/setup/setup.go              | 25 +++++++++++-
 internal/components/setup/compose.go |  6 +++
 internal/components/setup/kind.go    | 74 ++++++++++++++++++++++++++++++++++++
 internal/components/setup/which.go   | 10 +++++
 internal/flags/setup.go              |  4 ++
 5 files changed, 117 insertions(+), 2 deletions(-)

diff --git a/commands/setup/setup.go b/commands/setup/setup.go
index 4db194b..fd519b5 100644
--- a/commands/setup/setup.go
+++ b/commands/setup/setup.go
@@ -18,15 +18,36 @@
 package setup
 
 import (
-       "fmt"
+       "github.com/apache/skywalking-infra-e2e/internal/components/setup"
+       "github.com/apache/skywalking-infra-e2e/internal/logger"
 
        "github.com/spf13/cobra"
+
+       "github.com/apache/skywalking-infra-e2e/internal/flags"
 )
 
+func init() {
+       Setup.Flags().StringVar(&flags.Env, "env", "kind", "specify the run 
environment")
+       Setup.Flags().StringVar(&flags.File, "file", "kind.yaml", "specify 
configuration file")
+}
+
 var Setup = &cobra.Command{
        Use:   "setup",
        Short: "",
        Run: func(cmd *cobra.Command, args []string) {
-               fmt.Println("Not implemented.")
+               // check if env commands in PATH
+               if flags.Env == setup.COMPOSE {
+                       if setup.Which(setup.COMPOSECOMMAND) != nil {
+                               logger.Log.Errorf("command %s not found, is it 
in the PATH?", setup.COMPOSECOMMAND)
+                       }
+                       logger.Log.Info("env for docker-compose not 
implemented")
+               } else if flags.Env == setup.KIND {
+                       if setup.Which(setup.KINDCOMMAND) != nil {
+                               logger.Log.Errorf("command %s not found, is it 
in the PATH?", setup.COMPOSECOMMAND)
+                       }
+                       setup.KindSetupInCommand()
+               } else {
+                       logger.Log.Errorf("No such env for setup: [%s]. Should 
use kind or compose instead", flags.Env)
+               }
        },
 }
diff --git a/internal/components/setup/compose.go 
b/internal/components/setup/compose.go
new file mode 100644
index 0000000..9cab2e8
--- /dev/null
+++ b/internal/components/setup/compose.go
@@ -0,0 +1,6 @@
+package setup
+
+const (
+       COMPOSE        = "compose"
+       COMPOSECOMMAND = "docker-compose"
+)
diff --git a/internal/components/setup/kind.go 
b/internal/components/setup/kind.go
new file mode 100644
index 0000000..e601e88
--- /dev/null
+++ b/internal/components/setup/kind.go
@@ -0,0 +1,74 @@
+package setup
+
+import (
+       "bytes"
+       "os/exec"
+       "strings"
+
+       "github.com/apache/skywalking-infra-e2e/internal/flags"
+       "github.com/apache/skywalking-infra-e2e/internal/logger"
+)
+
+type ExecResult struct {
+       Command []string
+       Error   error
+       Stdout  string
+       StdErr  string
+}
+
+const (
+       KIND        = "kind"
+       KINDCOMMAND = "kind"
+)
+
+var (
+       // kind cluster create config
+       kindConfigFile string
+)
+
+// setup for kind, invoke from command line
+func KindSetupInCommand() {
+       kindConfigFile = flags.File
+
+       execResult := createKindCluster()
+       err := execResult.Error
+       if err != nil {
+               cmd := strings.Join(execResult.Command, " ")
+               logger.Log.Errorf("Kind cluster create exited abnormally whilst 
running [%s]\n"+
+                       "err: %s\nstdout: %s\nstderr: %s", cmd, err, 
execResult.Stdout, execResult.StdErr)
+       } else {
+               defer cleanupKindCluster()
+       }
+}
+
+func kindExec(args []string) ExecResult {
+       cmd := exec.Command(KINDCOMMAND, args...)
+       var stdoutBytes, stderrBytes bytes.Buffer
+       cmd.Stdout = &stdoutBytes
+       cmd.Stderr = &stderrBytes
+
+       err := cmd.Run()
+       execCmd := []string{KINDCOMMAND}
+       execCmd = append(execCmd, args...)
+
+       return ExecResult{
+               Command: execCmd,
+               Error:   err,
+               Stdout:  stdoutBytes.String(),
+               StdErr:  stderrBytes.String(),
+       }
+}
+
+func createKindCluster() ExecResult {
+       args := []string{"create", "cluster", "--config", kindConfigFile}
+
+       logger.Log.Info("creating kind cluster...")
+       return kindExec(args)
+}
+
+func cleanupKindCluster() ExecResult {
+       args := []string{"delete", "cluster"}
+
+       logger.Log.Info("deleting kind cluster...")
+       return kindExec(args)
+}
diff --git a/internal/components/setup/which.go 
b/internal/components/setup/which.go
new file mode 100644
index 0000000..cce2c01
--- /dev/null
+++ b/internal/components/setup/which.go
@@ -0,0 +1,10 @@
+package setup
+
+import "os/exec"
+
+// check if binary is present in PATH
+func Which(binary string) error {
+       _, err := exec.LookPath(binary)
+
+       return err
+}
diff --git a/internal/flags/setup.go b/internal/flags/setup.go
new file mode 100644
index 0000000..1256a57
--- /dev/null
+++ b/internal/flags/setup.go
@@ -0,0 +1,4 @@
+package flags
+
+var Env string
+var File string

Reply via email to