Skip to content

Commit

Permalink
upadte version to v1.1.0 & support async create (#504)
Browse files Browse the repository at this point in the history
* Unified error code (#491)

* add deteck

* the result change from json to fmt

* change blade binary path

* change deteck to check

* add operator command check

* change output from string to table

* change output from exist to exists

* change the word `deteck` to `check`

* use IsCommandAvailable of localChannel to judge os cmd is available or not

* delete test code

* change errno for jvm

* check log add parameter

Co-authored-by: xcaspar <[email protected]>

* feature: support async create

Signed-off-by: tiny.x <[email protected]>

* update chaosblade dependencies

Signed-off-by: xcaspar <[email protected]>

Co-authored-by: camix <[email protected]>
Co-authored-by: xcaspar <[email protected]>
  • Loading branch information
3 people authored May 31, 2021
1 parent 280cea5 commit 2c8992f
Show file tree
Hide file tree
Showing 21 changed files with 1,252 additions and 246 deletions.
18 changes: 13 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: build clean

export BLADE_VERSION=1.0.0
export BLADE_VERSION=1.1.0

ALLOWGITVERSION=1.8.5
GITVERSION:=$(shell git --version | grep ^git | sed 's/^.* //g')
Expand Down Expand Up @@ -65,6 +65,11 @@ BLADE_EXEC_CPLUS_BRANCH=master
DOCKER_YAML_FILE_NAME=chaosblade-docker-spec-$(BLADE_VERSION).yaml
DOCKER_YAML_FILE_PATH=$(BUILD_TARGET_BIN)/$(DOCKER_YAML_FILE_NAME)

# check yaml
CHECK_YAML_FILE_NAME=chaosblade-check-spec-$(BLADE_VERSION).yaml
CHECK_YANL_FILE_OSS=https://chaosblade.oss-cn-hangzhou.aliyuncs.com/agent/github/$(BLADE_VERSION)/$(CHECK_YAML_FILE_NAME)
CHECK_YAML_FILE_PATH=$(BUILD_TARGET_YAML)/$(CHECK_YAML_FILE_NAME)

ifeq ($(GOOS), linux)
GO_FLAGS=-ldflags="-linkmode external -extldflags -static $(GO_X_FLAGS) -s -w"
endif
Expand All @@ -77,7 +82,7 @@ help:
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>...\033[0m\n"} /^[a-zA-Z0-9_-]+:.*?##/ { printf " \033[36m%-20s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

##@ Build
build: pre_build cli os docker kubernetes cplus java upx package ## Build all scenarios
build: pre_build cli os docker kubernetes cplus java upx package check_yaml ## Build all scenarios

# for example: make build_with cli os_darwin
build_with: pre_build ## Select scenario build, for example `make build_with cli os docker kubernetes java cplus`
Expand All @@ -89,12 +94,12 @@ build_with_linux_arm: pre_build build_linux_arm_with_arg ## Select scenario buil

# build chaosblade linux version by docker image
build_linux: ## Build linux version of all scenarios by docker image
make build_with_linux ARGS="cli os docker kubernetes java cplus" upx package
make build_with_linux ARGS="cli os docker kubernetes java cplus check_yaml" upx package

build_linux_arm: ## Build linux arm version of all scenarios by docker image
make build_with_linux_arm ARGS="cli os docker kubernetes java cplus" upx package
make build_with_linux_arm ARGS="cli os docker kubernetes java cplus check_yaml" upx package

build_darwin: pre_build cli os_darwin docker kubernetes java cplus upx package ## Build all scenarios darwin version
build_darwin: pre_build cli os_darwin docker kubernetes java cplus upx package check_yaml ## Build all scenarios darwin version

##@ Build sub

Expand Down Expand Up @@ -231,6 +236,9 @@ clean: ## Clean
package: ## Generate the tar packages
tar zcvf $(BUILD_TARGET_PKG_FILE_PATH) -C $(BUILD_TARGET) $(BUILD_TARGET_DIR_NAME)

check_yaml:
wget "$(CHECK_YANL_FILE_OSS)" -O $(CHECK_YAML_FILE_PATH)

## Select scenario build linux version by docker image
build_linux_with_arg:
docker run --rm \
Expand Down
47 changes: 47 additions & 0 deletions cli/cmd/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cmd

import (
"fmt"

"github.com/chaosblade-io/chaosblade-spec-go/spec"
"github.com/spf13/cobra"
)

type CheckCommand struct {
baseCommand
*baseExpCommandService
}

func (dc *CheckCommand) Init() {
dc.command = &cobra.Command{
Use: "check",
Aliases: []string{"k"},
Short: "Check the environment for chaosblade",
Long: "Check the environment for chaosblade",
RunE: func(cmd *cobra.Command, args []string) error {
return spec.ReturnFail(spec.Code[spec.IllegalCommand],
fmt.Sprintf("less TARGE to check"))
},
Example: dc.detectExample(),
}
}

func (dc *CheckCommand) detectExample() string {
return "check os"
}
255 changes: 255 additions & 0 deletions cli/cmd/check_java.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cmd

import (
"context"
"errors"
"fmt"
"regexp"
"strconv"
"strings"

"github.com/chaosblade-io/chaosblade-spec-go/channel"
"github.com/chaosblade-io/chaosblade-spec-go/spec"
"github.com/chaosblade-io/chaosblade-spec-go/util"
"github.com/spf13/cobra"
)

const (
minJdkVersion = "1.6"
cmdJavaHome = "echo $JAVA_HOME"
cmdJavaVersion = "java -version"
javaToolsSubPath = "/lib/tools.jar"
)

var javaHome string

type CheckJavaCommand struct {
command *cobra.Command
object string
}

func (djc *CheckJavaCommand) CobraCmd() *cobra.Command {
return djc.command
}

func (djc *CheckJavaCommand) Name() string {
return ""
}

func (djc *CheckJavaCommand) Init() {
djc.command = &cobra.Command{
Use: "java",
Short: "Check the environment of java for chaosblade",
Long: "Check the environment of java for chaosblade",
RunE: func(cmd *cobra.Command, args []string) error {
return djc.checkJavaRunE()
},
Example: djc.detectExample(),
}

djc.command.Flags().StringVar(&djc.object, "object", "jdk,tools", "the object of java need to be checked")
}

func (djc *CheckJavaCommand) detectExample() string {
return "check os"
}

func (djc *CheckJavaCommand) checkJavaRunE() error {
objects := strings.Split(djc.object, ",")
if len(objects) == 0 || djc.object == "" {
return spec.ReturnFail(spec.Code[spec.IllegalParameters], "less object parameter")
}

for _, object := range objects {
object = strings.TrimSpace(object)
if object == "" {
continue
}
switch object {
case "jdk":
err := djc.checkJdk()
if err != nil {
fmt.Printf("[failed] %s \n", err.Error())
} else {
fmt.Print("[success] check jdk version success! \n")
}
case "tools":
err := djc.checkTools()
if err != nil {
fmt.Printf("[failed] %s \n", err.Error())
} else {
fmt.Printf("[success] check tools.jar success! \n")
}
default:
fmt.Printf("[failed] object parameter is wrong, object : %s", object)
}

}
return nil
}

// check jdk
func (djc *CheckJavaCommand) checkJdk() error {
// 1. check jdk by javaHome
if javaHome != "" {
jdkVersion, err := djc.getJdkVersionFromJdkHome()
if err != nil {
return err
}

if ok, err := djc.checkJdkVersion(jdkVersion); !ok {
return errors.New("check jdk version failed. err: " + err.Error())
}
return nil
}

// 2. check jdk by $JAVA_HOME
var jdkVersion string
response := channel.NewLocalChannel().Run(context.Background(), "", cmdJavaHome)
if response.Success {
javaResult := response.Result.(string)
javaHome = strings.Trim(javaResult, "\n")
jdkVersion, err := djc.getJdkVersionFromJdkHome()
if err != nil {
return errors.New(fmt.Sprintf("check java jdk version failed! err: %s", err.Error()))
}

ok, err := djc.checkJdkVersion(jdkVersion)
if !ok || err != nil {
return errors.New(fmt.Sprintf("check java jdk version failed! err: %s", err.Error()))
}
return nil
}

// 3. check jdk by `java -version`
response = channel.NewLocalChannel().Run(context.Background(), "", cmdJavaVersion)
if !response.Success {
return errors.New(fmt.Sprintf("check java jdk version failed! err: %s", response.Err))
}
javaResult := response.Result.(string)

jdkVersion, err := djc.getJdkVersionFromJavaVer(string(javaResult))
if err != nil {
return errors.New(fmt.Sprintf("check java jdk version failed! err: %s", err.Error()))
}
ok, err := djc.checkJdkVersion(jdkVersion)
if !ok || err != nil {
return errors.New(fmt.Sprintf("check java jdk version failed! err: %s", err.Error()))
}
return nil
}

// check java tools
func (djc *CheckJavaCommand) checkTools() error {
// 1. get java tools.jar path
var javaToolsPrePath string
if javaHome != "" {
javaToolsPrePath = javaHome
} else {
response := channel.NewLocalChannel().Run(context.Background(), "", cmdJavaHome)
if !response.Success {
return errors.New("check java tools.jar failed, $JAVA_HOME is nil")
}
javaToolsPrePath = response.Result.(string)
}

// check the path of tools.jar is exists or not
if util.IsExist(javaToolsPrePath + javaToolsSubPath) {
return nil
}
return errors.New("check java tools.jar failed, file: $JAVA_HOME/lib/tools.jar not exists")
}

// check jdk version. if current jdk version less than 1.6, return false, else return true
func (djc *CheckJavaCommand) checkJdkVersion(currentVersion string) (bool, error) {
// 1. split jdk version, eg: 1.8.0_151
currentVersions := strings.Split(currentVersion, ".")
minVersions := strings.Split(minJdkVersion, ".")
if len(currentVersions) < 2 {
return false, errors.New("jdk version error, current jdk version: " + currentVersion)
}

// 2. check current jdk
currFirst, _ := strconv.Atoi(currentVersions[0])
currSecond, _ := strconv.Atoi(currentVersions[1])
minFirst, _ := strconv.Atoi(minVersions[0])
minSecond, _ := strconv.Atoi(minVersions[1])
if ok := (currFirst*100 + currSecond) >= (minFirst*100 + minSecond); ok {
return ok, nil
}

return false, errors.New("jdk version less than 1.6, current jdk version: " + currentVersion)
}

// get jdk version from $JAVA_HOME eg: /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home
func (djc *CheckJavaCommand) getJdkVersionFromJdkHome() (string, error) {
// 1. check $JAVA_HOME
if !strings.HasPrefix(javaHome, "/") {
return "", errors.New("get jdk version failed, JavaHome is error, JavaHome : " + javaHome)
}

// 2. split $JAVA_HOME by `/`
javaHomeArr := strings.Split(javaHome, "/")
if len(javaHomeArr) == 0 {
return "", errors.New("get jdk version failed, JavaHome is error, JavaHome : " + javaHome)
}

// 3. check substr have java version or not by regexp, get java verison
reg, err := regexp.Compile(`jdk[\d+\_?\d+\.?]+[jdk]??`)
if err != nil {
return "", errors.New("get jdk version failed, regxp is wrong, err : " + err.Error())
}
for _, javaHomeOne := range javaHomeArr {
if !reg.MatchString(javaHomeOne) {
continue
}

javaVersion := javaHomeOne[len("jdk"):]
if javaVersion != "" {
return javaVersion, nil
}
}

return "", errors.New("get jdk version failed, JavaHome : " + javaHome)
}

// get jdk version from `java version` eg: java version "1.8.0_261"
// Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
// Java HotSpot(TM) 64-Bit Server VM (build 25.261-b12, mixed mode)
func (djc *CheckJavaCommand) getJdkVersionFromJavaVer(jdkVer string) (string, error) {
// 1. check `java version`
if jdkVer == "" {
return "", errors.New("get jdk version failed, jdkVer is error, jdkVer : " + jdkVer)
}

// 2. split `java version` by `\n`
jdkVerArr := strings.Split(jdkVer, "\n")
if len(jdkVerArr) < 1 {
return "", errors.New("get jdk version failed, jdkVer is error, jdkVer : " + jdkVer)
}

// 3. get java version
if ok := strings.Contains(jdkVerArr[0], "java version \""); !ok {
return "", errors.New("get jdk version failed, jdkVer is error, jdkVer : " + jdkVer)
}
jdkVersion := jdkVerArr[0][len("java version \"") : len(jdkVerArr[0])-1]
if jdkVersion == "" {
return "", errors.New("get jdk version failed, jdkVer : " + jdkVer)
}
return jdkVersion, nil
}
Loading

0 comments on commit 2c8992f

Please sign in to comment.