Skip to content

Commit

Permalink
1.add http deployment method
Browse files Browse the repository at this point in the history
Signed-off-by: liuminjian <[email protected]>
  • Loading branch information
liuminjian committed Nov 24, 2023
1 parent 580b26d commit e17126e
Show file tree
Hide file tree
Showing 80 changed files with 829 additions and 174 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ GO := go

# output
OUTPUT := bin/curveadm
SERVER_OUTPUT := bin/pigeon

# build flags
LDFLAGS := -s -w
Expand Down Expand Up @@ -52,12 +53,14 @@ TEST_FLAGS += -run $(CASE)

# packages
PACKAGES := $(PWD)/cmd/curveadm/main.go
SERVER_PACKAGES := $(PWD)/cmd/service/main.go

# tar
VERSION := "unknown"

build:
$(GOENV) $(GO) build -o $(OUTPUT) $(BUILD_FLAGS) $(PACKAGES)
$(GO) build -o $(SERVER_OUTPUT) $(SERVER_PACKAGES)

debug:
$(GOENV) $(GO) build -o $(OUTPUT) $(DEBUG_FLAGS) $(PACKAGES)
Expand Down
27 changes: 27 additions & 0 deletions cmd/service/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2023 NetEase Inc.
*
* 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 main

import (
"github.com/opencurve/curveadm/http"
"github.com/opencurve/pigeon"
)

func main() {
admServer := http.NewServer()
pigeon.Serve(admServer)
}
49 changes: 43 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ require (
github.com/jpillora/longestcommon v0.0.0-20161227235612-adb9d91ee629
github.com/kpango/glg v1.6.14
github.com/mattn/go-sqlite3 v1.14.16
github.com/mcuadros/go-defaults v1.2.0
github.com/melbahja/goph v1.3.0
github.com/mitchellh/hashstructure/v2 v2.0.2
github.com/moby/term v0.0.0-20221205130635-1aeaba878587
github.com/opencurve/pigeon v0.0.0-20230512031044-d5a430bb02a4
github.com/pingcap/log v1.1.0
github.com/sergi/go-diff v1.2.0
github.com/spf13/cobra v1.7.0
Expand All @@ -25,10 +27,46 @@ require (
golang.org/x/crypto v0.8.0
)

require (
github.com/Wine93/grace v0.0.0-20221021033009-7d0348013a3c // indirect
github.com/bytedance/sonic v1.8.7 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect
github.com/facebookgo/grace v0.0.0-20180706040059-75cf19382434 // indirect
github.com/facebookgo/httpdown v0.0.0-20180706035922-5979d39b15c2 // indirect
github.com/facebookgo/stats v0.0.0-20151006221625-1b76add642e4 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.9.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.12.0 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/google/pprof v0.0.0-20230406165453-00490a63f317 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/imroc/req/v3 v3.33.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.3 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/onsi/ginkgo/v2 v2.9.2 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/qtls-go1-19 v0.3.2 // indirect
github.com/quic-go/qtls-go1-20 v0.2.2 // indirect
github.com/quic-go/quic-go v0.33.0 // indirect
github.com/sevlyar/go-daemon v0.1.6 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect
)

require (
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d // indirect
github.com/VividCortex/ewma v1.2.0 // indirect
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
github.com/benbjohnson/clock v1.3.0 // indirect
Expand Down Expand Up @@ -81,18 +119,17 @@ require (
github.com/subosito/gotenv v1.4.2 // indirect
github.com/theupdateframework/notary v0.7.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/mod v0.9.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/term v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/tools v0.7.0 // indirect
google.golang.org/protobuf v1.29.1 // indirect
golang.org/x/tools v0.8.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.0.3 // indirect
)

replace github.com/melbahja/goph v1.3.0 => github.com/Wine93/goph v0.0.0-20220907033045-3b286d827fb3
71 changes: 71 additions & 0 deletions http/core/core.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2023 NetEase Inc.
*
* 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 core

import (
"fmt"
"strconv"

"github.com/opencurve/curveadm/internal/errno"
"github.com/opencurve/pigeon"
)

func Exit(r *pigeon.Request, err error) bool {
var status int
if err == nil {
status = 200
r.SendJSON(pigeon.JSON{
"errorCode": "0",
"errorMsg": "success",
})
} else {
code := err.(*errno.ErrorCode)
if code.IsHttpErr() {
status = code.HttpCode()
} else {
status = 503
}
r.SendJSON(pigeon.JSON{
"errorCode": strconv.Itoa(code.GetCode()),
"errorMsg": fmt.Sprintf("desc: %s; clue: %s", code.GetDescription(), code.GetClue()),
})
}
return r.Exit(status)
}

func Default(r *pigeon.Request) bool {
r.Logger().Warn("unupport request uri", pigeon.Field("uri", r.Uri))
return Exit(r, errno.ERR_UNSUPPORT_REQUEST_URI)
}

func ExitSuccessWithData(r *pigeon.Request, data interface{}) bool {
r.SendJSON(pigeon.JSON{
"data": data,
"errorCode": "0",
"errorMsg": "success",
})
return r.Exit(200)
}

func ExitFailWithData(r *pigeon.Request, data interface{}, message string) bool {
r.SendJSON(pigeon.JSON{
"errorCode": "503",
"errorMsg": message,
"data": data,
})
return r.Exit(503)
}
81 changes: 81 additions & 0 deletions http/manager/bind.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2023 NetEase Inc.
*
* 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 manager

import (
"github.com/opencurve/pigeon"
"mime/multipart"
"net/http"
)

var METHOD_REQUEST map[string]Request

type (
HandlerFunc func(r *pigeon.Request, ctx *Context) bool

Context struct {
Data interface{}
}

Request struct {
httpMethod string
method string
vType interface{}
handler HandlerFunc
}
)

func init() {
METHOD_REQUEST = map[string]Request{}
for _, request := range requests {
METHOD_REQUEST[request.method] = request
}
}

type DeployClusterCmdRequest struct {
Command string `json:"command" binding:"required"`
}

type DeployClusterUploadRequest struct {
FilePath string `json:"filepath" form:"filepath" binding:"required"`
File *multipart.FileHeader `form:"file" binding:"required"`
}

type DeployClusterDownloadRequest struct {
FilePath string `json:"filepath" form:"filepath" binding:"required"`
}

var requests = []Request{
{
http.MethodPost,
"cluster.deploy.cmd",
DeployClusterCmdRequest{},
DeployClusterCmd,
},
{
http.MethodPost,
"cluster.deploy.upload",
DeployClusterUploadRequest{},
DeployClusterUpload,
},
{
http.MethodGet,
"cluster.deploy.download",
DeployClusterDownloadRequest{},
DeployClusterDownload,
},
}
50 changes: 50 additions & 0 deletions http/manager/entrypoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2023 NetEase Inc.
*
* 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 manager

import (
"reflect"

"github.com/mcuadros/go-defaults"
"github.com/opencurve/curveadm/http/core"
"github.com/opencurve/curveadm/internal/errno"
"github.com/opencurve/pigeon"
)

func Entrypoint(r *pigeon.Request) bool {
if r.Method != pigeon.HTTP_METHOD_GET &&
r.Method != pigeon.HTTP_METHOD_POST {
return core.Exit(r, errno.ERR_UNSUPPORT_HTTP_METHOD)
}

request, ok := METHOD_REQUEST[r.Args["method"]]
if !ok {
return core.Exit(r, errno.ERR_UNSUPPORT_METHOD_ARGUMENT)
} else if request.httpMethod != r.Method {
return core.Exit(r, errno.ERR_HTTP_METHOD_MISMATCHED)
}

vType := reflect.TypeOf(request.vType)
data := reflect.New(vType).Interface()
if err := r.BindBody(data); err != nil {
r.Logger().Error("bad request form param",
pigeon.Field("error", err))
return core.Exit(r, errno.ERR_BAD_REQUEST_FORM_PARAM)
}
defaults.SetDefaults(data)
return request.handler(r, &Context{data})
}
Loading

0 comments on commit e17126e

Please sign in to comment.