Skip to content

Commit

Permalink
feat: supports lua plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
duandaa committed Jul 30, 2024
1 parent 3dfb69d commit 33e5200
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 13 deletions.
10 changes: 10 additions & 0 deletions cli/ctl/common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,16 @@ const (
// attention: following line comments are used by `stringer`
PLUGIN_TYPE_WASM PluginType = 1 + iota
PLUGIN_TYPE_SO
PLUGIN_TYPE_LUA
)

type PluginUser int

//go:generate stringer -type=PluginUser -trimprefix=PLUGIN_USER_ -linecomment
const (
// attention: following line comments are used by `stringer`
PLUGIN_USER_AGENT PluginUser = 1 + iota
PLUGIN_USER_SERVER
)

var (
Expand Down
38 changes: 28 additions & 10 deletions cli/ctl/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"mime/multipart"
"os"
"path"
"strings"

"github.com/spf13/cobra"

Expand All @@ -40,24 +41,27 @@ func RegisterPluginCommand() *cobra.Command {
},
}

var createType, image, name string
var createType, image, name, user string
create := &cobra.Command{
Use: "create",
Short: "create plugin",
Example: "deepflow-ctl plugin create --type wasm --image /home/tom/hello.wasm --name hello",
Use: "create",
Short: "create plugin",
Example: "deepflow-ctl plugin create --type wasm --image /home/tom/hello.wasm --name hello\n" +
"deepflow-ctl plugin create --type so --image /home/tom/hello.so --name hello\n" +
"deepflow-ctl plugin create --type lua --image /home/tom/hello.lua --name hello --user server",
Run: func(cmd *cobra.Command, args []string) {
if _, err := os.Stat(image); errors.Is(err, os.ErrNotExist) {
fmt.Printf("file(%s) not found\n", image)
return
}
if err := createPlugin(cmd, createType, image, name); err != nil {
if err := createPlugin(cmd, createType, image, name, user); err != nil {
fmt.Println(err)
}
},
}
create.Flags().StringVarP(&createType, "type", "", "", "type of image file, currently supports: wasm | so")
create.Flags().StringVarP(&createType, "type", "", "", "type of image file, currently supports: wasm | so | lua")
create.Flags().StringVarP(&image, "image", "", "", "plugin image to upload")
create.Flags().StringVarP(&name, "name", "", "", "specify a unique alias for image")
create.Flags().StringVarP(&user, "user", "", "agent", "specify the component for which plugin is used. the optional value is agent/server")
create.MarkFlagsRequiredTogether("type", "image", "name")

list := &cobra.Command{
Expand Down Expand Up @@ -86,19 +90,31 @@ func RegisterPluginCommand() *cobra.Command {
return plugin
}

func createPlugin(cmd *cobra.Command, t, image, name string) error {
func createPlugin(cmd *cobra.Command, t, image, name, user string) error {
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)
bodyWriter.WriteField("NAME", name)

switch t {
case "wasm":
bodyWriter.WriteField("TYPE", "1")
case "so":
bodyWriter.WriteField("TYPE", "2")
case "lua":
bodyWriter.WriteField("TYPE", "3")
default:
return errors.New(fmt.Sprintf("unknown type %s", t))
}
switch user {
case "agent":
bodyWriter.WriteField("USER", "1")
case "server":
if !strings.HasSuffix(image,"lua") || t != "lua" {
return errors.New(fmt.Sprintf("if user is server, expected image: <filename>.lua, but got image: %s\n expected type: lua, but got type: %s ",image,t ))
}
bodyWriter.WriteField("USER", "2")
default:
return errors.New(fmt.Sprintf("unknown user %s", user))
}

fileWriter, err := bodyWriter.CreateFormFile("IMAGE", path.Base(image))
f, err := os.Open(image)
Expand Down Expand Up @@ -130,15 +146,17 @@ func listPlugin(cmd *cobra.Command) {
var (
typeMaxSize = jsonparser.GetTheMaxSizeOfAttr(data, "TYPE")
nameMaxSize = jsonparser.GetTheMaxSizeOfAttr(data, "NAME")
userMaxSize = jsonparser.GetTheMaxSizeOfAttr(data, "USER")
)
cmdFormat := "%-*s %-*s %-19s\n"
fmt.Printf(cmdFormat, typeMaxSize, "TYPE", nameMaxSize, "NAME", "UPDATED_AT")
cmdFormat := "%-*s %-*s %-*s %-19s\n"
fmt.Printf(cmdFormat, typeMaxSize, "TYPE", nameMaxSize, "NAME", userMaxSize, "USER", "UPDATED_AT")
for i := range data.MustArray() {
d := data.GetIndex(i)

fmt.Printf(cmdFormat,
typeMaxSize, common.PluginType(d.Get("TYPE").MustInt()),
nameMaxSize, d.Get("NAME").MustString(),
userMaxSize, common.PluginUser(d.Get("USER").MustInt()),
d.Get("UPDATED_AT").MustString(),
)
}
Expand Down
3 changes: 2 additions & 1 deletion server/controller/db/mysql/migration/rawsql/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ TRUNCATE TABLE db_version;
CREATE TABLE IF NOT EXISTS plugin (
id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(256) NOT NULL,
type INTEGER NOT NULL COMMENT '1: wasm',
type INTEGER NOT NULL COMMENT '1: wasm 2: so 3: lua',
user INTEGER NOT NULL DEFAULT 1 COMMENT '1: agent 2: server',
image LONGBLOB NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
Expand Down
14 changes: 14 additions & 0 deletions server/controller/db/mysql/migration/rawsql/issu/6.6.1.5.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- This template is for upgrade using INSERT/UPDATE/DELETE
-- Tractions are needed for these commands to avoid manual rollback if error occurs.

START TRANSACTION;

/*ALTER TABLE*/
ALTER TABLE plugin
ADD COLUMN user INTEGER NOT NULL COMMENT '1: agent 2: server',
ALTER COLUMN user SET DEFAULT 1;
-- update db_version to latest, remember update DB_VERSION_EXPECTED in migration/version.go
UPDATE db_version SET version='6.6.1.5';
-- modify end

COMMIT;
2 changes: 1 addition & 1 deletion server/controller/db/mysql/migration/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ package migration

const (
DB_VERSION_TABLE = "db_version"
DB_VERSION_EXPECTED = "6.6.1.4"
DB_VERSION_EXPECTED = "6.6.1.5"
)
3 changes: 2 additions & 1 deletion server/controller/db/mysql/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,8 @@ func (VTapRepo) TableName() string {
type Plugin struct {
ID int `gorm:"primaryKey;column:id;type:int;not null" json:"ID"`
Name string `gorm:"column:name;type:varchar(256);not null" json:"NAME"`
Type int `gorm:"column:type;type:int" json:"TYPE"` // 1: wasm
Type int `gorm:"column:type;type:int" json:"TYPE"` // 1: wasm 2: so 3: lua
User int `gorm:"column:user;type:int;default:1" json:"USER"` // 1: agent 2: server
Image compressedBytes `gorm:"column:image;type:logblob;not null" json:"IMAGE"`
CreatedAt time.Time `gorm:"column:created_at;type:timestamp;not null;default:CURRENT_TIMESTAMP" json:"CREATED_AT"`
UpdatedAt time.Time `gorm:"column:updated_at;type:timestamp;not null;default:CURRENT_TIMESTAMP" json:"UPDATED_AT"`
Expand Down
6 changes: 6 additions & 0 deletions server/controller/http/router/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,15 @@ func createPlugin(c *gin.Context) {
JsonResponse(c, nil, err)
return
}
u, err := strconv.Atoi(c.PostForm("USER"))
if err != nil {
JsonResponse(c, nil, err)
return
}
plugin := &mysql.Plugin{
Name: c.PostForm("NAME"),
Type: t,
User: u,
}

// get file
Expand Down
1 change: 1 addition & 0 deletions server/controller/http/service/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func GetPlugin(db *mysql.DB, filter map[string]interface{}) ([]model.Plugin, err
Name: plugin.Name,
Type: plugin.Type,
UpdatedAt: plugin.UpdatedAt.Format(common.GO_BIRTHDAY),
User: plugin.User,
}
resp = append(resp, temp)
}
Expand Down
1 change: 1 addition & 0 deletions server/controller/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ type CSVHeader struct {
type Plugin struct {
Name string `json:"NAME" binding:"required"`
Type int `json:"TYPE" binding:"required"`
User int `json:"USER" binding:"required"`
Image []byte `json:"IMAGE,omitempty" binding:"required"`
UpdatedAt string `json:"UPDATED_AT"`
}
Expand Down

0 comments on commit 33e5200

Please sign in to comment.