Skip to content

Commit

Permalink
feat: add watchdog for recommend install
Browse files Browse the repository at this point in the history
  • Loading branch information
yangtao committed May 10, 2024
1 parent b5deffe commit 31277e2
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 122 deletions.
6 changes: 3 additions & 3 deletions pkg/apiserver/service/v1/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,16 +346,16 @@ func (h *Handler) info(req *restful.Request, resp *restful.Response) {
infoLocal, _ := boltdb.GetLocalAppInfo(appName)

infoMarket, err := appmgr.GetAppInfo(appName)
if err != nil {
api.HandleError(resp, err)
if err != nil && infoLocal == nil {
api.HandleError(resp, errors.New("not found"))
return
}

info := infoLocal
if info == nil {
info = infoMarket
info.Source = constants.AppFromMarket
} else {
} else if infoMarket != nil {
info.Version = infoMarket.Version
}

Expand Down
80 changes: 40 additions & 40 deletions pkg/apiserver/service/v1/handler_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (h *Handler) devUpload(req *restful.Request, resp *restful.Response) {
go h.watchDogManager.NewWatchDog(watchdog.OP_INSTALL,
info.Name, uid, token, watchdog.AppFromDev, info).
Exec()
} else if info.CfgType == constants.ModelType {
} else if info.CfgType == constants.ModelType || info.CfgType == constants.RecommendType {
go h.commonWatchDogManager.NewWatchDog(watchdog.OP_INSTALL,
info.Name, uid, token, watchdog.AppFromDev, info.CfgType,
info).
Expand Down Expand Up @@ -276,45 +276,45 @@ func (h *Handler) devUpload(req *restful.Request, resp *restful.Response) {
dealWithInstallResp(resp, resBody)
}

func (h *Handler) devInstall(req *restful.Request, resp *restful.Response) {
token := getToken(req)
if token == "" {
api.HandleUnauthorized(resp, errors.New("access token not found"))
return
}

appName := req.PathParameter(ParamAppName)
info, err := boltdb.GetLocalAppInfo(appName)
if err != nil {
glog.Warningf("GetLocalAppInfo err:%s", err.Error())
api.HandleError(resp, err)
return
}

resBody, err := installByType(info, token)
if err != nil {
api.HandleError(resp, err)
return
}

uid, _, err := appservice.GetUidFromStatus(resBody)
if err != nil {
api.HandleError(resp, err)
return
}
if info.CfgType == constants.AppType {
go h.watchDogManager.NewWatchDog(watchdog.OP_INSTALL,
appName, uid, token, watchdog.AppFromDev, info).
Exec()
} else if info.CfgType == constants.ModelType {
go h.commonWatchDogManager.NewWatchDog(watchdog.OP_INSTALL,
appName, uid, token, watchdog.AppFromDev, info.CfgType,
info).
Exec()
}

dealWithInstallResp(resp, resBody)
}
//func (h *Handler) devInstall(req *restful.Request, resp *restful.Response) {
// token := getToken(req)
// if token == "" {
// api.HandleUnauthorized(resp, errors.New("access token not found"))
// return
// }
//
// appName := req.PathParameter(ParamAppName)
// info, err := boltdb.GetLocalAppInfo(appName)
// if err != nil {
// glog.Warningf("GetLocalAppInfo err:%s", err.Error())
// api.HandleError(resp, err)
// return
// }
//
// resBody, err := installByType(info, token)
// if err != nil {
// api.HandleError(resp, err)
// return
// }
//
// uid, _, err := appservice.GetUidFromStatus(resBody)
// if err != nil {
// api.HandleError(resp, err)
// return
// }
// if info.CfgType == constants.AppType {
// go h.watchDogManager.NewWatchDog(watchdog.OP_INSTALL,
// appName, uid, token, watchdog.AppFromDev, info).
// Exec()
// } else if info.CfgType == constants.ModelType {
// go h.commonWatchDogManager.NewWatchDog(watchdog.OP_INSTALL,
// appName, uid, token, watchdog.AppFromDev, info.CfgType,
// info).
// Exec()
// }
//
// dealWithInstallResp(resp, resBody)
//}

func findAppCfgFile(chartDirPath string) string {
charts, err := os.ReadDir(chartDirPath)
Expand Down
2 changes: 1 addition & 1 deletion pkg/apiserver/service/v1/handler_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (h *Handler) install(req *restful.Request, resp *restful.Response) {
appName, uid, token, watchdog.AppFromStore,
info).
Exec()
} else if info.CfgType == constants.ModelType {
} else if info.CfgType == constants.ModelType || info.CfgType == constants.RecommendType {
go h.commonWatchDogManager.NewWatchDog(watchdog.OP_INSTALL,
appName, uid, token, watchdog.AppFromStore, info.CfgType,
info).
Expand Down
133 changes: 67 additions & 66 deletions pkg/apiserver/service/v1/handler_recommend.go
Original file line number Diff line number Diff line change
@@ -1,69 +1,70 @@
package v1

import (
"errors"
"github.com/emicklei/go-restful/v3"
"github.com/golang/glog"
"market/internal/appmgr"
"market/internal/appservice"
"market/internal/boltdb"
"market/internal/constants"
"market/pkg/api"
)
//
//import (
// "errors"
// "github.com/emicklei/go-restful/v3"
// "github.com/golang/glog"
// "market/internal/appmgr"
// "market/internal/appservice"
// "market/internal/boltdb"
// "market/internal/constants"
// "market/pkg/api"
//)

func (h *Handler) installRecommend(req *restful.Request, resp *restful.Response) {
appName := req.PathParameter(ParamAppName)
token := getToken(req)
if token == "" {
api.HandleUnauthorized(resp, errors.New("access token not found"))
return
}

info, err := appmgr.GetAppInfo(appName)
if err != nil {
respErr(resp, err)
return
}
if info.ChartName == "" {
api.HandleError(resp, errors.New("get chart name failed"))
return
}

err = checkDep(info, token)
if err != nil {
respDepErr(resp, err.Error())
return
}

//todo cache chart
err = appmgr.DownloadAppTgz(info.ChartName)
if err != nil {
respErr(resp, err)
return
}

info.Source = constants.AppFromMarket
err = boltdb.UpsertLocalAppInfo(info)
if err != nil {
glog.Warningf("UpsertLocalAppInfo err:%s", err.Error())
api.HandleError(resp, err)
return
}

resBody, err := installByType(info, token)
if err != nil {
api.HandleError(resp, err)
glog.Warningf("install %s resp:%s, err:%s", appName, resBody, err.Error())
return
}

uid, _, err := appservice.GetUidFromStatus(resBody)
if err != nil {
api.HandleError(resp, err)
return
}

glog.Infof("uid:%s", uid)

dealWithInstallResp(resp, resBody)
}
//func (h *Handler) installRecommend(req *restful.Request, resp *restful.Response) {
// appName := req.PathParameter(ParamAppName)
// token := getToken(req)
// if token == "" {
// api.HandleUnauthorized(resp, errors.New("access token not found"))
// return
// }
//
// info, err := appmgr.GetAppInfo(appName)
// if err != nil {
// respErr(resp, err)
// return
// }
// if info.ChartName == "" {
// api.HandleError(resp, errors.New("get chart name failed"))
// return
// }
//
// err = checkDep(info, token)
// if err != nil {
// respDepErr(resp, err.Error())
// return
// }
//
// //todo cache chart
// err = appmgr.DownloadAppTgz(info.ChartName)
// if err != nil {
// respErr(resp, err)
// return
// }
//
// info.Source = constants.AppFromMarket
// err = boltdb.UpsertLocalAppInfo(info)
// if err != nil {
// glog.Warningf("UpsertLocalAppInfo err:%s", err.Error())
// api.HandleError(resp, err)
// return
// }
//
// resBody, err := installByType(info, token)
// if err != nil {
// api.HandleError(resp, err)
// glog.Warningf("install %s resp:%s, err:%s", appName, resBody, err.Error())
// return
// }
//
// uid, _, err := appservice.GetUidFromStatus(resBody)
// if err != nil {
// api.HandleError(resp, err)
// return
// }
//
// glog.Infof("uid:%s", uid)
//
// dealWithInstallResp(resp, resBody)
//}
5 changes: 5 additions & 0 deletions pkg/apiserver/service/v1/handler_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ func (h *Handler) upgrade(req *restful.Request, resp *restful.Response) {
appName, uid, token, watchdog.AppFromStore,
latestInfo).
Exec()
} else if latestInfo.CfgType == constants.RecommendType {
go h.commonWatchDogManager.NewWatchDog(watchdog.OP_UPGRADE,
appName, uid, token, watchdog.AppFromStore, latestInfo.CfgType,
latestInfo).
Exec()
}

dealWithInstallResp(resp, resBody)
Expand Down
24 changes: 12 additions & 12 deletions pkg/apiserver/service/v1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ func AddToContainer(c *restful.Container) error {
Param(ws.QueryParameter("source", "source")).
Returns(http.StatusOK, "Success to upload the application for dev", &models.InstallationStatusResp{}))

ws.Route(ws.POST("/applications/{"+ParamAppName+"}/dev-install").
To(handler.devInstall).
Doc("install the application for dev").
Metadata(restfulspec.KeyOpenAPITags, ModuleTags).
Param(ws.FormParameter(ParamAppName, "the name of a application")).
Returns(http.StatusOK, "Success to begin a installation of the application for dev", &models.InstallationResponse{}))
//ws.Route(ws.POST("/applications/{"+ParamAppName+"}/dev-install").
// To(handler.devInstall).
// Doc("install the application for dev").
// Metadata(restfulspec.KeyOpenAPITags, ModuleTags).
// Param(ws.FormParameter(ParamAppName, "the name of a application")).
// Returns(http.StatusOK, "Success to begin a installation of the application for dev", &models.InstallationResponse{}))

ws.Route(ws.POST("/applications/{"+ParamAppName+"}/install").
To(handler.install).
Expand All @@ -204,12 +204,12 @@ func AddToContainer(c *restful.Container) error {
Param(ws.PathParameter(ParamAppName, "the name of a application")).
Returns(http.StatusOK, "Success to begin a installation of the application", &models.InstallationResponse{}))

ws.Route(ws.POST("/recommends/{"+ParamAppName+"}/install").
To(handler.installRecommend).
Doc("install the recommend").
Metadata(restfulspec.KeyOpenAPITags, ModuleTags).
Param(ws.PathParameter(ParamAppName, "the name of a recommend")).
Returns(http.StatusOK, "Success to begin a installation of the recommend", &models.InstallationResponse{}))
//ws.Route(ws.POST("/recommends/{"+ParamAppName+"}/install").
// To(handler.installRecommend).
// Doc("install the recommend").
// Metadata(restfulspec.KeyOpenAPITags, ModuleTags).
// Param(ws.PathParameter(ParamAppName, "the name of a recommend")).
// Returns(http.StatusOK, "Success to begin a installation of the recommend", &models.InstallationResponse{}))

ws.Route(ws.POST("/applications/{"+ParamAppName+"}/upgrade").
To(handler.upgrade).
Expand Down

0 comments on commit 31277e2

Please sign in to comment.