Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #15

Open
wants to merge 13 commits into
base: dev
Choose a base branch
from
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ GO_FLAGS=-ldflags="-s -w"
UNAME := $(shell uname)

ifeq ($(BLADE_VERSION), )
BLADE_VERSION=1.7.1
BLADE_VERSION=1.7.4
endif

BUILD_TARGET=target
Expand Down
40 changes: 20 additions & 20 deletions build/image/musl/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
FROM golang:1.13.1
LABEL maintainer="Changjun Xiao"
# # The image is used to build chaosblade for musl
RUN wget http://www.musl-libc.org/releases/musl-1.1.21.tar.gz \
&& tar -zxvf musl-1.1.21.tar.gz \
&& rm musl-1.1.21.tar.gz \
&& cd musl* \
&& ./configure \
&& make \
&& make install \
&& rm -rf musl*
RUN apt-get update \
&& apt-get install unzip
ENV CC /usr/local/musl/bin/musl-gcc
ENV GOOS linux
ENTRYPOINT [ "make" ]
FROM golang:1.20
LABEL maintainer="Changjun Xiao"

# # The image is used to build chaosblade for musl
RUN wget http://www.musl-libc.org/releases/musl-1.1.21.tar.gz \
&& tar -zxvf musl-1.1.21.tar.gz \
&& rm musl-1.1.21.tar.gz \
&& cd musl* \
&& ./configure \
&& make \
&& make install \
&& rm -rf musl*

RUN apt-get update \
&& apt-get install unzip

ENV CC /usr/local/musl/bin/musl-gcc
ENV GOOS linux

ENTRYPOINT [ "make" ]
7 changes: 5 additions & 2 deletions build/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
package main

import (
"github.com/chaosblade-io/chaosblade-exec-middleware/exec/nginx"
"github.com/chaosblade-io/chaosblade-exec-os/exec/model"
"log"
"os"

"github.com/chaosblade-io/chaosblade-exec-middleware/exec/nginx"
"github.com/chaosblade-io/chaosblade-exec-middleware/exec/redis"
"github.com/chaosblade-io/chaosblade-exec-os/exec/model"

"github.com/chaosblade-io/chaosblade-spec-go/spec"
"github.com/chaosblade-io/chaosblade-spec-go/util"
)
Expand All @@ -41,6 +43,7 @@ func main() {
func getModels() *spec.Models {
modelCommandSpecs := []spec.ExpModelCommandSpec{
nginx.NewNginxCommandSpec(),
redis.NewRedisCommandSpec(),
}
specModels := make([]*spec.Models, 0)
for _, modeSpec := range modelCommandSpecs {
Expand Down
7 changes: 4 additions & 3 deletions exec/executor_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ package exec
import (
"context"
"fmt"
"github.com/chaosblade-io/chaosblade-spec-go/log"
"io/ioutil"
"net"
"os"
"path"
"strconv"
"strings"
"time"

"github.com/chaosblade-io/chaosblade-spec-go/log"

"github.com/chaosblade-io/chaosblade-exec-os/version"

"github.com/chaosblade-io/chaosblade-spec-go/spec"
Expand Down Expand Up @@ -284,7 +285,7 @@ func (c *SSHClient) connect() error {
if c.Key == "" {
auth = append(auth, ssh.Password(c.Password))
} else {
pemBytes, err := ioutil.ReadFile(c.Key)
pemBytes, err := os.ReadFile(c.Key)
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions exec/model/model_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package model

import (
"github.com/chaosblade-io/chaosblade-exec-middleware/exec/nginx"
"github.com/chaosblade-io/chaosblade-exec-middleware/exec/redis"
"github.com/chaosblade-io/chaosblade-spec-go/spec"
)

Expand All @@ -26,5 +27,6 @@ import (
func GetAllExpModels() []spec.ExpModelCommandSpec {
return []spec.ExpModelCommandSpec{
nginx.NewNginxCommandSpec(),
redis.NewRedisCommandSpec(),
}
}
2 changes: 2 additions & 0 deletions exec/model/model_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package model

import (
"github.com/chaosblade-io/chaosblade-exec-middleware/exec/nginx"
"github.com/chaosblade-io/chaosblade-exec-middleware/exec/redis"
"github.com/chaosblade-io/chaosblade-spec-go/spec"
)

Expand All @@ -26,5 +27,6 @@ import (
func GetAllExpModels() []spec.ExpModelCommandSpec {
return []spec.ExpModelCommandSpec{
nginx.NewNginxCommandSpec(),
redis.NewRedisCommandSpec(),
}
}
22 changes: 12 additions & 10 deletions exec/nginx/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ func NewNginxCommandSpec() spec.ExpModelCommandSpec {
}

// Start nginx process.
func startNginx(channel spec.Channel, ctx context.Context) *spec.Response {
return runNginxCommand(channel, ctx, "")
func startNginx(channel spec.Channel, ctx context.Context, nginxPath string) *spec.Response {
return runNginxCommand(channel, ctx, nginxPath, "")
}

// Find nginx config directory, return dir, activeFile, backup.
func getNginxConfigLocation(channel spec.Channel, ctx context.Context) (string, string, string, *spec.Response) {
response := runNginxCommand(channel, ctx, "-t")
func getNginxConfigLocation(channel spec.Channel, ctx context.Context, nginxPath string) (string, string, string, *spec.Response) {
response := runNginxCommand(channel, ctx, nginxPath, "-t")
if !response.Success {
return "", "", "", response
}
Expand Down Expand Up @@ -103,8 +103,8 @@ func parseMultipleKvPairs(newKV string) [][]string {
}

// Reload nginx.conf backup file and send nginx process a reload signal.
func reloadNginxConfig(channel spec.Channel, ctx context.Context) *spec.Response {
_, activeFile, backup, response := getNginxConfigLocation(channel, ctx)
func reloadNginxConfig(channel spec.Channel, ctx context.Context, nginxPath string) *spec.Response {
_, activeFile, backup, response := getNginxConfigLocation(channel, ctx, nginxPath)
if response != nil {
return response
}
Expand All @@ -116,19 +116,21 @@ func reloadNginxConfig(channel spec.Channel, ctx context.Context) *spec.Response
if response := restoreConfigFile(channel, ctx, backup, activeFile); !response.Success {
return response
}
if response := runNginxCommand(channel, ctx, "-s reload"); !response.Success {
if response := runNginxCommand(channel, ctx, nginxPath, "-s reload"); !response.Success {
return response
}
return spec.ReturnSuccess("nginx config restored")
}

// Backup and swap nginx.conf, then send nginx process a reload signal.
func swapNginxConfig(channel spec.Channel, ctx context.Context, newFile string, model *spec.ExpModel) *spec.Response {
dir, activeFile, backup, response := getNginxConfigLocation(channel, ctx)

nginxPath := model.ActionFlags["nginx-path"]
dir, activeFile, backup, response := getNginxConfigLocation(channel, ctx, nginxPath)
if response != nil {
return response
}
if response := testNginxConfig(channel, ctx, newFile, dir); response != nil {
if response := testNginxConfig(channel, ctx, newFile, dir, nginxPath); response != nil {
return response
}

Expand All @@ -138,7 +140,7 @@ func swapNginxConfig(channel spec.Channel, ctx context.Context, newFile string,
return response
}

if response := runNginxCommand(channel, ctx, "-s reload"); !response.Success {
if response := runNginxCommand(channel, ctx, nginxPath, "-s reload"); !response.Success {
return response
}
return spec.ReturnSuccess("nginx config changed")
Expand Down
32 changes: 20 additions & 12 deletions exec/nginx/nginx_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"fmt"
"github.com/chaosblade-io/chaosblade-exec-middleware/exec/category"
"github.com/chaosblade-io/chaosblade-exec-middleware/exec/nginx/parser"

"github.com/chaosblade-io/chaosblade-spec-go/log"
"github.com/chaosblade-io/chaosblade-spec-go/spec"
"github.com/chaosblade-io/chaosblade-spec-go/util"
"path/filepath"
Expand Down Expand Up @@ -58,21 +58,26 @@ func NewConfigActionSpec() spec.ExpActionCommandSpec {
Name: "set-config",
Desc: "Set multiple key-value config paris for specified block",
},
&spec.ExpFlag{
Name: "nginx-path",
Desc: "The absolute path of nginx",
Required: true,
},
},
ActionFlags: []spec.ExpFlagSpec{},
ActionExecutor: &NginxConfigExecutor{},
ActionExample: `
# Change config file to my.conf
blade create nginx config --mode file --file my.conf
blade create nginx config --mode file --file my.conf --nginx-path /usr/local/nginx/sbin/nginx

# Change 'server[0]' exposed on port 8899
blade create nginx config --mode cmd --block 'http.server[0]' --set-config='listen=8899'
blade create nginx config --mode cmd --block 'http.server[0]' --set-config='listen=8899' --nginx-path /usr/local/nginx/sbin/nginx

# Set 'http.server[0].location[0]' proxy_pass to www.baidu.com
blade create nginx config --mode cmd --block 'http.server[0].location[0]' --set-config='proxy_pass=www.baidu.com'
blade create nginx config --mode cmd --block 'http.server[0].location[0]' --set-config='proxy_pass=www.baidu.com' --nginx-path /usr/local/nginx/sbin/nginx

# Revert config change to the oldest config file
blade destroy nginx config
blade destroy nginx config --nginx-path /usr/local/nginx/sbin/nginx
`,
ActionPrograms: []string{NginxConfigBin},
ActionCategories: []string{category.Middleware},
Expand Down Expand Up @@ -111,8 +116,13 @@ func (ng *NginxConfigExecutor) Exec(suid string, ctx context.Context, model *spe
if response := testNginxExists(ng.channel, ctx); response != nil {
return response
}

_, activeFile, _, response := getNginxConfigLocation(ng.channel, ctx)
nginxPath := model.ActionFlags["nginx-path"]
if nginxPath == "" {
errMsg := "the nginx-path flag is required"
log.Errorf(ctx, errMsg)
return spec.ResponseFailWithFlags(spec.ActionNotSupport, errMsg)
}
_, activeFile, _, response := getNginxConfigLocation(ng.channel, ctx, nginxPath)
if response != nil {
return response
}
Expand All @@ -127,6 +137,7 @@ func (ng *NginxConfigExecutor) start(ctx context.Context, activeFile string, mod
var config *parser.Config
mode := model.ActionFlags["mode"]
newFile := model.ActionFlags["file"]

switch mode {
case fileMode:
if newFile == "" || !util.IsExist(newFile) || util.IsDir(newFile) {
Expand Down Expand Up @@ -172,11 +183,8 @@ func createNewConfig(config *parser.Config, locator string, newKV string) (strin
}

func (ng *NginxConfigExecutor) stop(ctx context.Context, model *spec.ExpModel) *spec.Response {
mode := model.ActionFlags["mode"]
if mode != "" {
return spec.ResponseFailWithFlags(spec.ParameterInvalid, "--mode", mode, fmt.Sprintf("--mode cannot be %s when destroying Nginx config experiment", mode))
}
return reloadNginxConfig(ng.channel, ctx)
nginxPath := model.ActionFlags["nginx-path"]
return reloadNginxConfig(ng.channel, ctx, nginxPath)
}

func (ng *NginxConfigExecutor) SetChannel(channel spec.Channel) {
Expand Down
25 changes: 19 additions & 6 deletions exec/nginx/nginx_crash.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package nginx
import (
"context"
"github.com/chaosblade-io/chaosblade-exec-middleware/exec/category"
"github.com/chaosblade-io/chaosblade-spec-go/log"

"github.com/chaosblade-io/chaosblade-spec-go/spec"
)
Expand All @@ -32,15 +33,21 @@ type CrashActionSpec struct {
func NewCrashActionSpec() spec.ExpActionCommandSpec {
return &CrashActionSpec{
spec.BaseExpActionCommandSpec{
ActionMatchers: []spec.ExpFlagSpec{},
ActionMatchers: []spec.ExpFlagSpec{
&spec.ExpFlag{
Name: "nginx-path",
Desc: "The absolute path of nginx",
Required: true,
},
},
ActionFlags: []spec.ExpFlagSpec{},
ActionExecutor: &NginxCrashExecutor{},
ActionExample: `
# Nginx crash
blade create nginx crash
blade create nginx crash --nginx-path /usr/local/nginx/sbin/nginx
# Nginx restart
blade destroy nginx crash
blade destroy nginx crash --nginx-path /usr/local/nginx/sbin/nginx
`,
ActionPrograms: []string{NginxCrashBin},
ActionCategories: []string{category.Middleware},
Expand Down Expand Up @@ -76,8 +83,14 @@ func (*NginxCrashExecutor) Name() string {
}

func (ng *NginxCrashExecutor) Exec(suid string, ctx context.Context, model *spec.ExpModel) *spec.Response {
nginxPath := model.ActionFlags["nginx-path"]
if nginxPath == "" {
errMsg := "the nginx-path flag is required"
log.Errorf(ctx, errMsg)
return spec.ResponseFailWithFlags(spec.ActionNotSupport, errMsg)
}
if _, ok := spec.IsDestroy(ctx); ok {
return ng.stop(ctx)
return ng.stop(ctx, nginxPath)
}
return ng.start(ctx)
}
Expand All @@ -93,8 +106,8 @@ func (ng *NginxCrashExecutor) start(ctx context.Context) *spec.Response {
}
}

func (ng *NginxCrashExecutor) stop(ctx context.Context) *spec.Response {
return startNginx(ng.channel, ctx)
func (ng *NginxCrashExecutor) stop(ctx context.Context, nginxPath string) *spec.Response {
return startNginx(ng.channel, ctx, nginxPath)
}

func (ng *NginxCrashExecutor) SetChannel(channel spec.Channel) {
Expand Down
Loading