chaosblade

Форк
0
/
executor.go 
119 строк · 3.3 Кб
1
/*
2
 * Copyright 1999-2020 Alibaba Group Holding Ltd.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package cplus
18

19
import (
20
	"context"
21
	"encoding/json"
22
	"fmt"
23
	"github.com/chaosblade-io/chaosblade-spec-go/log"
24
	neturl "net/url"
25

26
	"github.com/chaosblade-io/chaosblade-spec-go/channel"
27
	"github.com/chaosblade-io/chaosblade-spec-go/spec"
28
	"github.com/chaosblade-io/chaosblade-spec-go/util"
29

30
	"github.com/chaosblade-io/chaosblade/data"
31
)
32

33
// Executor for jvm experiment
34
type Executor struct {
35
	Uri     string
36
	channel spec.Channel
37
}
38

39
func NewExecutor() *Executor {
40
	return &Executor{
41
		channel: channel.NewLocalChannel(),
42
	}
43
}
44

45
func (e *Executor) Name() string {
46
	return "cplus"
47
}
48

49
func (e *Executor) SetChannel(channel spec.Channel) {
50
	e.channel = channel
51
}
52

53
func (e *Executor) Exec(uid string, ctx context.Context, model *spec.ExpModel) *spec.Response {
54
	var url string
55
	port, resp := e.getPortFromDB(ctx, uid, model)
56
	if resp != nil {
57
		return resp
58
	}
59

60
	if _, ok := spec.IsDestroy(ctx); ok {
61
		url = e.destroyUrl(port, uid)
62
	} else {
63
		url = e.createUrl(port, uid, model)
64
	}
65
	result, err, code := util.Curl(ctx, url)
66
	if err != nil {
67
		log.Errorf(ctx, spec.HttpExecFailed.Sprintf(url, err))
68
		return spec.ResponseFailWithFlags(spec.HttpExecFailed, url, err)
69
	}
70
	if code == 200 {
71
		var resp spec.Response
72
		err := json.Unmarshal([]byte(result), &resp)
73
		if err != nil {
74
			log.Errorf(ctx, spec.ResultUnmarshalFailed.Sprintf(result, err))
75
			return spec.ResponseFailWithFlags(spec.ResultUnmarshalFailed, result, err)
76
		}
77
		return &resp
78
	}
79
	log.Errorf(ctx, spec.HttpExecFailed.Sprintf(url, result))
80
	return spec.ResponseFailWithFlags(spec.HttpExecFailed, url, result)
81
}
82

83
func (e *Executor) createUrl(port, suid string, model *spec.ExpModel) string {
84
	url := fmt.Sprintf("http://%s:%s/create?target=%s&suid=%s&action=%s",
85
		"127.0.0.1", port, model.Target, suid, model.ActionName)
86
	for k, v := range model.ActionFlags {
87
		if v == "" || v == "false" {
88
			continue
89
		}
90
		// filter timeout because of the agent implementation by all matchers
91
		if k == "timeout" {
92
			continue
93
		}
94
		url = fmt.Sprintf("%s&%s=%s", url, k, neturl.QueryEscape(v))
95
	}
96
	return url
97
}
98

99
func (e *Executor) destroyUrl(port, uid string) string {
100
	url := fmt.Sprintf("http://%s:%s/destroy?suid=%s",
101
		"127.0.0.1", port, uid)
102
	return url
103
}
104

105
var db = data.GetSource()
106

107
func (e *Executor) getPortFromDB(ctx context.Context, uid string, model *spec.ExpModel) (string, *spec.Response) {
108
	port := model.ActionFlags["port"]
109
	record, err := db.QueryRunningPreByTypeAndProcess("cplus", port, "")
110
	if err != nil {
111
		log.Errorf(ctx, spec.DatabaseError.Sprintf("query", err))
112
		return "", spec.ResponseFailWithFlags(spec.DatabaseError, "query", err)
113
	}
114
	if record == nil {
115
		log.Errorf(ctx, spec.ParameterInvalidCplusPort.Sprintf(port))
116
		return "", spec.ResponseFailWithFlags(spec.ParameterInvalidCplusPort, port)
117
	}
118
	return record.Port, nil
119
}
120

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.