chaosblade
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
17package cplus18
19import (20"context"21"encoding/json"22"fmt"23"github.com/chaosblade-io/chaosblade-spec-go/log"24neturl "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
34type Executor struct {35Uri string36channel spec.Channel37}
38
39func NewExecutor() *Executor {40return &Executor{41channel: channel.NewLocalChannel(),42}43}
44
45func (e *Executor) Name() string {46return "cplus"47}
48
49func (e *Executor) SetChannel(channel spec.Channel) {50e.channel = channel51}
52
53func (e *Executor) Exec(uid string, ctx context.Context, model *spec.ExpModel) *spec.Response {54var url string55port, resp := e.getPortFromDB(ctx, uid, model)56if resp != nil {57return resp58}59
60if _, ok := spec.IsDestroy(ctx); ok {61url = e.destroyUrl(port, uid)62} else {63url = e.createUrl(port, uid, model)64}65result, err, code := util.Curl(ctx, url)66if err != nil {67log.Errorf(ctx, spec.HttpExecFailed.Sprintf(url, err))68return spec.ResponseFailWithFlags(spec.HttpExecFailed, url, err)69}70if code == 200 {71var resp spec.Response72err := json.Unmarshal([]byte(result), &resp)73if err != nil {74log.Errorf(ctx, spec.ResultUnmarshalFailed.Sprintf(result, err))75return spec.ResponseFailWithFlags(spec.ResultUnmarshalFailed, result, err)76}77return &resp78}79log.Errorf(ctx, spec.HttpExecFailed.Sprintf(url, result))80return spec.ResponseFailWithFlags(spec.HttpExecFailed, url, result)81}
82
83func (e *Executor) createUrl(port, suid string, model *spec.ExpModel) string {84url := fmt.Sprintf("http://%s:%s/create?target=%s&suid=%s&action=%s",85"127.0.0.1", port, model.Target, suid, model.ActionName)86for k, v := range model.ActionFlags {87if v == "" || v == "false" {88continue89}90// filter timeout because of the agent implementation by all matchers91if k == "timeout" {92continue93}94url = fmt.Sprintf("%s&%s=%s", url, k, neturl.QueryEscape(v))95}96return url97}
98
99func (e *Executor) destroyUrl(port, uid string) string {100url := fmt.Sprintf("http://%s:%s/destroy?suid=%s",101"127.0.0.1", port, uid)102return url103}
104
105var db = data.GetSource()106
107func (e *Executor) getPortFromDB(ctx context.Context, uid string, model *spec.ExpModel) (string, *spec.Response) {108port := model.ActionFlags["port"]109record, err := db.QueryRunningPreByTypeAndProcess("cplus", port, "")110if err != nil {111log.Errorf(ctx, spec.DatabaseError.Sprintf("query", err))112return "", spec.ResponseFailWithFlags(spec.DatabaseError, "query", err)113}114if record == nil {115log.Errorf(ctx, spec.ParameterInvalidCplusPort.Sprintf(port))116return "", spec.ResponseFailWithFlags(spec.ParameterInvalidCplusPort, port)117}118return record.Port, nil119}
120