Skip to content

Commit

Permalink
fix: failed to create nginx experiment through agent
Browse files Browse the repository at this point in the history
Signed-off-by: Yuaninga <[email protected]>
  • Loading branch information
Yuaninga committed Aug 22, 2023
1 parent bd1efd9 commit 7b9994d
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 62 deletions.
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 {

This comment has been minimized.

Copy link
@fjz99

fjz99 Sep 6, 2023

Contributor

LGTM

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
29 changes: 21 additions & 8 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 @@ -176,7 +187,9 @@ func (ng *NginxConfigExecutor) stop(ctx context.Context, model *spec.ExpModel) *
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
31 changes: 22 additions & 9 deletions exec/nginx/nginx_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +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"
"strconv"
"strings"

Expand Down Expand Up @@ -90,24 +91,30 @@ func NewResponseActionSpec() spec.ExpActionCommandSpec {
Desc: "There may be many server blocks in nginx.conf, so which server you want to modify? The default server-id is 0.",
Default: "0",
},
&spec.ExpFlag{
Name: "nginx-path",
Desc: "The absolute path of nginx",
Required: true,
},
},
ActionFlags: []spec.ExpFlagSpec{},
ActionExecutor: &NginxResponseExecutor{},
ActionExample: `
# Set /test return body='ok',code=200,type=json
blade create nginx response --path /test --body ok
blade create nginx response --path /test --body ok --nginx-path /usr/local/nginx/sbin/nginx
# Set /test return body='',code=500,type=json
blade create nginx response --path /test --code 500
blade create nginx response --path /test --code 500 --nginx-path /usr/local/nginx/sbin/nginx
# Set /test return body='{"a":1}',code=200,type=json
blade create nginx response --path /test --code 200 --body '{"a":1}' --type json
blade create nginx response --path /test --code 200 --body '{"a":1}' --type json --nginx-path /usr/local/nginx/sbin/nginx
# Set /t.* return body='{"a":1}',code=200,type=json, and add header 'Server=mock' to server[0]
blade create nginx response --regex /t.* --code 200 --body '{"a":1}' --header 'Server=mock;' --server 0
blade create nginx response --regex /t.* --code 200 --body '{"a":1}' --header 'Server=mock;' --server 0 --nginx-path /usr/local/nginx/sbin/nginx
# Revert config change to the oldest config file
blade destroy nginx response`,
blade destroy nginx response --nginx-path /usr/local/nginx/sbin/nginx
`,
ActionPrograms: []string{NginxResponseBin},
ActionCategories: []string{category.Middleware},
},
Expand Down Expand Up @@ -146,13 +153,19 @@ func (ng *NginxResponseExecutor) Exec(suid string, ctx context.Context, model *s
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
}

if _, ok := spec.IsDestroy(ctx); ok {
return ng.stop(ctx)
return ng.stop(ctx, nginxPath)
}
return ng.start(ctx, activeFile, model)
}
Expand Down Expand Up @@ -324,8 +337,8 @@ func createNewBlock(path, regex, code, body, header, contentType string, useLua
return block, nil
}

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

func (ng *NginxResponseExecutor) SetChannel(channel spec.Channel) {
Expand Down
23 changes: 18 additions & 5 deletions exec/nginx/nginx_restart.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 @@ -31,12 +32,18 @@ type RestartActionSpec struct {
func NewRestartActionSpec() spec.ExpActionCommandSpec {
return &RestartActionSpec{
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: &NginxRestartExecutor{},
ActionExample: `
# Nginx restart
blade create nginx restart
blade create nginx restart --nginx-path /usr/local/nginx/sbin/nginx
`,
ActionPrograms: []string{NginxRestartBin},
ActionCategories: []string{category.Middleware},
Expand Down Expand Up @@ -72,20 +79,26 @@ func (*NginxRestartExecutor) Name() string {
}

func (ng *NginxRestartExecutor) 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 spec.ReturnSuccess("cancel 'nginx restart' is meaningless")
}
return ng.start(ctx)
return ng.start(ctx, nginxPath)
}

func (ng *NginxRestartExecutor) start(ctx context.Context) *spec.Response {
func (ng *NginxRestartExecutor) start(ctx context.Context, nginxPath string) *spec.Response {
if response := testNginxExists(ng.channel, ctx); response != nil {
return response
}
if response := killNginx(ng.channel, ctx); response != nil {
return response
}
return startNginx(ng.channel, ctx)
return startNginx(ng.channel, ctx, nginxPath)
}

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

0 comments on commit 7b9994d

Please sign in to comment.