[ADD] Add rainbond plugin share

This commit is contained in:
pujielan 2018-01-18 13:45:24 +08:00
parent 06a2cf40c9
commit e7f3860745
14 changed files with 169 additions and 21 deletions

View File

@ -35,6 +35,7 @@ type TenantInterface interface {
SumTenants(w http.ResponseWriter, r *http.Request)
SingleTenantResources(w http.ResponseWriter, r *http.Request)
GetSupportProtocols(w http.ResponseWriter, r *http.Request)
TransPlugins(w http.ResponseWriter, r *http.Request)
}
//ServiceInterface ServiceInterface

View File

@ -61,6 +61,8 @@ func (v2 *V2) tenantNameRouter() chi.Router {
//租户中的日志
r.Post("/event-log", controller.GetManager().TenantLogByAction)
r.Get("/protocols", controller.GetManager().GetSupportProtocols)
//插件预安装
r.Post("/transplugins", controller.GetManager().TransPlugins)
//代码检测
r.Post("/code-check", controller.GetManager().CheckCode)
r.Post("/cloud-share", controller.GetManager().ShareCloud)

View File

@ -105,11 +105,12 @@ func (t *TenantStruct) UpdatePlugin(w http.ResponseWriter, r *http.Request) {
// description: 统一返回格式
pluginID := r.Context().Value(middleware.ContextKey("plugin_id")).(string)
tenantID := r.Context().Value(middleware.ContextKey("tenant_id")).(string)
var ups api_model.UpdatePluginStruct
if ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &ups.Body, nil); !ok {
return
}
if err := handler.GetPluginManager().UpdatePluginAct(pluginID, &ups); err != nil {
if err := handler.GetPluginManager().UpdatePluginAct(pluginID,tenantID, &ups); err != nil {
err.Handle(r, w)
return
}
@ -139,7 +140,8 @@ func (t *TenantStruct) DeletePlugin(w http.ResponseWriter, r *http.Request) {
// "$ref": "#/responses/commandResponse"
// description: 统一返回格式
pluginID := r.Context().Value(middleware.ContextKey("plugin_id")).(string)
if err := handler.GetPluginManager().DeletePluginAct(pluginID); err != nil {
tenantID := r.Context().Value(middleware.ContextKey("tenant_id")).(string)
if err := handler.GetPluginManager().DeletePluginAct(pluginID, tenantID); err != nil {
err.Handle(r, w)
return
}

View File

@ -1716,3 +1716,45 @@ func (t *TenantStruct) GetSupportProtocols(w http.ResponseWriter, r *http.Reques
httputil.ReturnSuccess(r, w, rps)
return
}
//TransPlugins transPlugins
// swagger:operation POST /v2/tenants/{tenant_name}/transplugins v2 transPlugins
//
// 安装云帮默认plugins
//
// trans plugins
//
// ---
// produces:
// - application/json
// - application/xml
// parameters:
// - name: tenant_name
// in: path
// description: tenant name
// required: true
// type: string
//
// responses:
// default:
// schema:
// "$ref": "#/responses/commandResponse"
// description: 统一返回格式
func (t *TenantStruct) TransPlugins(w http.ResponseWriter, r *http.Request) {
var tps api_model.TransPlugins
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &tps.Body, nil)
if !ok {
return
}
tenantID := r.Context().Value(middleware.ContextKey("tenant_id")).(string)
tenantName := r.Context().Value(middleware.ContextKey("tenant_name")).(string)
rc := make(map[string]string)
err := handler.GetTenantManager().TransPlugins(tenantID, tenantName, tps.Body.FromTenantName, tps.Body.PluginsID)
if err != nil {
err.Handle(r, w)
return
}
rc["result"] = "success"
httputil.ReturnSuccess(r, w, rc)
return
}

View File

@ -28,8 +28,8 @@ import (
//PluginHandler plugin handler
type PluginHandler interface {
CreatePluginAct(cps *api_model.CreatePluginStruct) *util.APIHandleError
UpdatePluginAct(pluginID string, cps *api_model.UpdatePluginStruct) *util.APIHandleError
DeletePluginAct(pluginID string) *util.APIHandleError
UpdatePluginAct(pluginID,tenantID string, cps *api_model.UpdatePluginStruct) *util.APIHandleError
DeletePluginAct(pluginID, tenantID string) *util.APIHandleError
GetPlugins(tenantID string) ([]*dbmodel.TenantPlugin, *util.APIHandleError)
AddDefaultEnv(est *api_model.ENVStruct) *util.APIHandleError
UpdateDefaultEnv(est *api_model.ENVStruct) *util.APIHandleError

View File

@ -39,6 +39,7 @@ type TenantHandler interface {
GetTenantsResources(tr *api_model.TenantResources) ([]map[string]interface{}, error)
TenantsSum() (int, error)
GetProtocols() ([]*dbmodel.RegionProcotols, *util.APIHandleError)
TransPlugins(tenantID, tenantName, fromTenant string, pluginList []string) *util.APIHandleError
}
var defaultTenantHandler TenantHandler

View File

@ -84,8 +84,8 @@ func (p *PluginAction) CreatePluginAct(cps *api_model.CreatePluginStruct) *util.
}
//UpdatePluginAct UpdatePluginAct
func (p *PluginAction) UpdatePluginAct(pluginID string, cps *api_model.UpdatePluginStruct) *util.APIHandleError {
tp, err := db.GetManager().TenantPluginDao().GetPluginByID(pluginID)
func (p *PluginAction) UpdatePluginAct(pluginID, tenantID string, cps *api_model.UpdatePluginStruct) *util.APIHandleError {
tp, err := db.GetManager().TenantPluginDao().GetPluginByID(pluginID, tenantID)
if err != nil {
return util.CreateAPIHandleErrorFromDBError("get old plugin info", err)
}
@ -105,10 +105,10 @@ func (p *PluginAction) UpdatePluginAct(pluginID string, cps *api_model.UpdatePlu
}
//DeletePluginAct DeletePluginAct
func (p *PluginAction) DeletePluginAct(pluginID string) *util.APIHandleError {
func (p *PluginAction) DeletePluginAct(pluginID,tenantID string) *util.APIHandleError {
//TODO: 事务
tx := db.GetManager().Begin()
err := db.GetManager().TenantPluginDaoTransactions(tx).DeletePluginByID(pluginID)
err := db.GetManager().TenantPluginDaoTransactions(tx).DeletePluginByID(pluginID, tenantID)
if err != nil {
tx.Rollback()
return util.CreateAPIHandleErrorFromDBError("delete plugin", err)
@ -213,10 +213,24 @@ func (p *PluginAction) BuildPluginManual(bps *api_model.BuildPluginStruct) (*dbm
eventID := bps.Body.EventID
logger := event.GetManager().GetLogger(eventID)
defer event.CloseManager()
plugin, err := db.GetManager().TenantPluginDao().GetPluginByID(bps.PluginID)
plugin, err := db.GetManager().TenantPluginDao().GetPluginByID(bps.PluginID, bps.Body.TenantID)
if err != nil {
return nil, util.CreateAPIHandleErrorFromDBError(fmt.Sprintf("get plugin by %v", bps.PluginID), err)
}
if bps.Body.PluginFrom != "" {
switch bps.Body.PluginFrom{
case "yb":
pbv, err := p.InstallPluginFromYB(bps, plugin)
if err != nil {
logrus.Debugf("install plugin from yb error %s", err.Error())
return nil, util.CreateAPIHandleError(500, fmt.Errorf("install plugin from yb error"))
}
return pbv, nil
case "ys":
default:
return nil, util.CreateAPIHandleError(400, fmt.Errorf("unexpect plugin from"))
}
}
switch plugin.BuildModel {
case "image":
pbv, err := p.ImageBuildPlugin(bps, plugin)
@ -244,6 +258,34 @@ func createVersionID(s []byte) string {
return hex.EncodeToString(h.Sum(nil))
}
//InstallPluginFromYB InstallPluginFromYB
func (p *PluginAction) InstallPluginFromYB(b *api_model.BuildPluginStruct, plugin *dbmodel.TenantPlugin)(
*dbmodel.TenantPluginBuildVersion, error) {
if b.Body.Operator == "" {
b.Body.Operator = "define"
}
pbv := &dbmodel.TenantPluginBuildVersion{
VersionID: b.Body.BuildVersion,
PluginID: b.PluginID,
Kind: plugin.BuildModel,
BaseImage: plugin.ImageURL,
BuildLocalImage: b.Body.BuildImage,
ContainerCPU: b.Body.PluginCPU,
ContainerMemory: b.Body.PluginMemory,
ContainerCMD: b.Body.PluginCMD,
BuildTime: time.Now().Format(time.RFC3339),
Info: b.Body.Info,
Status: "complete",
}
if err := db.GetManager().TenantPluginBuildVersionDao().AddModel(pbv); err != nil {
if !strings.Contains(err.Error(), "exist") {
logrus.Errorf("build plugin error: %s", err.Error())
return nil, err
}
}
return pbv, nil
}
//ImageBuildPlugin ImageBuildPlugin
func (p *PluginAction) ImageBuildPlugin(b *api_model.BuildPluginStruct, plugin *dbmodel.TenantPlugin) (
*dbmodel.TenantPluginBuildVersion, error) {

View File

@ -1752,7 +1752,7 @@ func (s *ServiceAction) TenantServiceDeletePluginRelation(serviceID, pluginID st
//SetTenantServicePluginRelation SetTenantServicePluginRelation
func (s *ServiceAction) SetTenantServicePluginRelation(tenantID, serviceID string, pss *api_model.PluginSetStruct) *util.APIHandleError {
tx := db.GetManager().Begin()
plugin, err := db.GetManager().TenantPluginDao().GetPluginByID(pss.Body.PluginID)
plugin, err := db.GetManager().TenantPluginDao().GetPluginByID(pss.Body.PluginID, tenantID)
if err != nil {
tx.Rollback()
return util.CreateAPIHandleErrorFromDBError("get plugin by plugin id", err)

View File

@ -247,3 +247,34 @@ func (t *TenantAction) GetProtocols() ([]*dbmodel.RegionProcotols, *util.APIHand
}
return rps, nil
}
//TransPlugins TransPlugins
func (t *TenantAction) TransPlugins(tenantID, tenantName, fromTenant string, pluginList []string) *util.APIHandleError {
tenantInfo, err := db.GetManager().TenantDao().GetTenantIDByName(fromTenant)
if err != nil {
return util.CreateAPIHandleErrorFromDBError("get tenant infos", err)
}
goodrainID := tenantInfo.UUID
tx := db.GetManager().Begin()
for _, p := range pluginList {
pluginInfo, err := db.GetManager().TenantPluginDao().GetPluginByID(p, goodrainID)
if err != nil {
tx.Rollback()
return util.CreateAPIHandleErrorFromDBError("get plugin infos", err)
}
pluginInfo.TenantID = tenantID
pluginInfo.Domain = tenantName
pluginInfo.ID = 0
err = db.GetManager().TenantPluginDaoTransactions(tx).AddModel(pluginInfo)
if err != nil {
if !strings.Contains(err.Error(), "is exist") {
tx.Rollback()
return util.CreateAPIHandleErrorFromDBError("add plugin Info", err)
}
}
}
if err := tx.Commit().Error; err != nil {
return util.CreateAPIHandleErrorFromDBError("trans plugins infos", err)
}
return nil
}

View File

@ -93,11 +93,12 @@ func InitService(next http.Handler) http.Handler {
func InitPlugin(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
pluginID := chi.URLParam(r, "plugin_id")
tenantID := r.Context().Value(ContextKey("tenant_id")).(string)
if pluginID == "" {
httputil.ReturnError(r, w, 404, "need plugin id")
return
}
_, err := db.GetManager().TenantPluginDao().GetPluginByID(pluginID)
_, err := db.GetManager().TenantPluginDao().GetPluginByID(pluginID,tenantID)
if err != nil {
if err.Error() == gorm.ErrRecordNotFound.Error() {
httputil.ReturnError(r, w, 404, "cant find plugin")

View File

@ -44,7 +44,7 @@ func InitProxy(conf option.Config) {
func GetNodeProxy() proxy.Proxy {
return nodeProxy
}
//GetNodeProxy GetNodeProxy
//GetBuilderProxy GetNodeProxy
func GetBuilderProxy() proxy.Proxy {
return builderProxy
}

View File

@ -224,8 +224,16 @@ type BuildPluginStruct struct {
Operator string `json:"operator" validate:"operator"`
//租户id
// in: body
// required: false
// required: true
TenantID string `json:"tenant_id" validate:"tenant_id"`
//安装来源
// in: body
// required: false
PluginFrom string `json:"plugin_from" validate:"plugin_from"`
// 镜像地址
// in: body
// required: false
BuildImage string `json:"build_image" validate:"build_image"`
}
}
@ -387,3 +395,21 @@ type VersionEnv struct {
//required: true
EnvValue string `json:"env_value" validate:"env_value"`
}
//TransPlugins TransPlugins
type TransPlugins struct {
// in: path
// required: true
TenantName string `json:"tenant_name"`
//in: body
Body struct {
// 从该租户安装
// in: body
// required: true
FromTenantName string `json:"from_tenant_name" validate:"from_tenant_name"`
// 插件id
// in: body
// required: true
PluginsID []string `json:"plugins_id" validate:"plugins_id"`
}
}

View File

@ -88,8 +88,8 @@ type TenantServicesPortDao interface {
//TenantPluginDao TenantPluginDao
type TenantPluginDao interface {
Dao
GetPluginByID(pluginID string) (*model.TenantPlugin, error)
DeletePluginByID(pluginID string) error
GetPluginByID(pluginID, tenantID string) (*model.TenantPlugin, error)
DeletePluginByID(pluginID, tenantID string) error
GetPluginsByTenantID(tenantID string) ([]*model.TenantPlugin, error)
}

View File

@ -34,12 +34,12 @@ type PluginDaoImpl struct {
func (t *PluginDaoImpl) AddModel(mo model.Interface) error {
plugin := mo.(*model.TenantPlugin)
var oldPlugin model.TenantPlugin
if ok := t.DB.Where("plugin_id = ?", plugin.PluginID).Find(&oldPlugin).RecordNotFound(); ok {
if ok := t.DB.Where("plugin_id = ? and tenant_id = ?", plugin.PluginID, plugin.TenantID).Find(&oldPlugin).RecordNotFound(); ok {
if err := t.DB.Create(plugin).Error; err != nil {
return err
}
} else {
return fmt.Errorf("plugin %s is exist", plugin.PluginName)
return fmt.Errorf("plugin %s in tenant %s is exist", plugin.PluginName, plugin.TenantID)
}
return nil
}
@ -54,20 +54,20 @@ func (t *PluginDaoImpl) UpdateModel(mo model.Interface) error {
}
//GetPluginByID GetPluginByID
func (t *PluginDaoImpl) GetPluginByID(id string) (*model.TenantPlugin, error) {
func (t *PluginDaoImpl) GetPluginByID(id, tenantID string) (*model.TenantPlugin, error) {
var plugin model.TenantPlugin
if err := t.DB.Where("plugin_id = ? ", id).Find(&plugin).Error; err != nil {
if err := t.DB.Where("plugin_id = ? and tenant_id = ?", id, tenantID).Find(&plugin).Error; err != nil {
return nil, err
}
return &plugin, nil
}
//DeletePluginByID DeletePluginByID
func (t *PluginDaoImpl) DeletePluginByID(id string) error {
func (t *PluginDaoImpl) DeletePluginByID(id, tenantID string) error {
relation := &model.TenantPlugin{
PluginID: id,
}
if err := t.DB.Where("plugin_id=?", id).Delete(relation).Error; err != nil {
if err := t.DB.Where("plugin_id=? and tenant_id=?", id, tenantID).Delete(relation).Error; err != nil {
return err
}
return nil