156 lines
3.9 KiB
Go
156 lines
3.9 KiB
Go
package controller
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/gin-gonic/gin"
|
|
cp "github.com/otiai10/copy"
|
|
log "github.com/sirupsen/logrus"
|
|
"github.com/thoas/go-funk"
|
|
"k8s-manager/src/model"
|
|
k8s_manager "k8s-manager/src/utils"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
type AppViewModel struct {
|
|
*model.AppModel
|
|
Status string
|
|
ServiceDisplayName string
|
|
}
|
|
|
|
type AppsViewModel struct {
|
|
Username string
|
|
Apps []AppViewModel
|
|
}
|
|
|
|
func statusString(a *AppViewModel) string {
|
|
_, err := K.GetDeploymentStatus(a.AppModel.DeploymentName())
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "not found") {
|
|
return "Halt"
|
|
}
|
|
return err.Error()
|
|
} else {
|
|
pod := K.GetPodFromDeployment(a.AppModel.DeploymentName())
|
|
if pod == nil {
|
|
return "Internal Error 1."
|
|
}
|
|
if len(pod.Status.ContainerStatuses) == 0 {
|
|
return "Internal Error 2."
|
|
}
|
|
containerState := pod.Status.ContainerStatuses[0].State
|
|
if containerState.Waiting != nil {
|
|
return containerState.Waiting.Reason
|
|
} else if containerState.Running != nil {
|
|
return "Running"
|
|
} else if containerState.Terminated != nil {
|
|
b, _ := json.Marshal(containerState.Terminated)
|
|
return "Terminated " + string(b)
|
|
}
|
|
return pod.Status.Message + "," + pod.Status.Reason
|
|
}
|
|
}
|
|
|
|
//func AppPage(c *gin.Context) {
|
|
// c.File("template/app_view.html")
|
|
//}
|
|
//
|
|
//func AppMiddleware(c *gin.Context) {
|
|
// app := &model.AppModel{}
|
|
// err := c.ShouldBindJSON(&app)
|
|
// if err == nil {
|
|
// username := c.MustGet("username")
|
|
// if app.UserName != username {
|
|
// c.JSON(http.StatusUnauthorized, gin.H{"msg": "user error."})
|
|
// c.Abort()
|
|
// }
|
|
// c.Set("app", app)
|
|
// }
|
|
//}
|
|
|
|
func CreateUpdateDeleteApp(c *gin.Context) {
|
|
app := &model.AppModel{}
|
|
err := c.BindJSON(app)
|
|
if err != nil {
|
|
log.WithError(err).Error("CreateUpdateDeleteApp")
|
|
c.JSON(http.StatusBadRequest, gin.H{"msg": "Cannot get data."})
|
|
} else {
|
|
username := c.MustGet("username").(string)
|
|
if username != app.UserName {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"msg": "user error."})
|
|
return
|
|
}
|
|
if c.Request.Method == http.MethodPost {
|
|
err = app.Create()
|
|
} else if c.Request.Method == http.MethodPut {
|
|
err = app.Update()
|
|
} else if c.Request.Method == http.MethodDelete {
|
|
err = app.Delete()
|
|
}
|
|
if err != nil {
|
|
log.WithError(err).Error("CreateUpdateDeleteApp", c.Request.Method)
|
|
c.JSON(http.StatusInternalServerError, gin.H{"msg": "DB error."})
|
|
} else {
|
|
c.JSON(http.StatusOK, gin.H{"msg": "ok"})
|
|
}
|
|
}
|
|
}
|
|
|
|
func GetAppsController(c *gin.Context) {
|
|
username := c.MustGet("username").(string)
|
|
|
|
appsvm := funk.Map(model.GetApps(username), func(a model.AppModel) AppViewModel { return AppViewModel{AppModel: &a} }).([]AppViewModel)
|
|
for i := range appsvm {
|
|
a := &appsvm[i]
|
|
a.Status = statusString(a)
|
|
a.ServiceDisplayName = a.URLPrefix()
|
|
}
|
|
vm := &AppsViewModel{
|
|
Username: username,
|
|
Apps: appsvm,
|
|
}
|
|
c.JSON(http.StatusOK, vm)
|
|
}
|
|
|
|
func GetUsername(c *gin.Context) {
|
|
username := c.MustGet("username").(string)
|
|
c.JSON(http.StatusOK, gin.H{"username": username})
|
|
}
|
|
|
|
func ForkApp(c *gin.Context) {
|
|
newApp := &model.AppModel{}
|
|
err := c.BindJSON(newApp)
|
|
if err != nil {
|
|
log.WithError(err).Error("ForkApp BindJson")
|
|
k8s_manager.GinError(c, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
originApp := &model.AppModel{Id: newApp.OriginId}
|
|
err = originApp.Fill()
|
|
if err != nil {
|
|
log.WithError(err).Error("ForkApp Fill")
|
|
k8s_manager.GinError(c, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
err = os.RemoveAll(newApp.NFSPath())
|
|
if err != nil {
|
|
log.WithError(err).Error("ForkApp RemoveAll")
|
|
k8s_manager.GinError(c, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
err = cp.Copy(originApp.NFSPath(), newApp.NFSPath())
|
|
if err != nil {
|
|
log.WithError(err).Error("ForkApp Copy")
|
|
k8s_manager.GinError(c, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
err = newApp.Create()
|
|
if err != nil {
|
|
log.WithError(err).Error("ForkApp Create")
|
|
k8s_manager.GinError(c, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
k8s_manager.GinOk(c, "ok")
|
|
}
|