From 8bca76b78b537f6abf55c335a6cfd64702083992 Mon Sep 17 00:00:00 2001 From: link Date: Fri, 29 Oct 2021 18:37:27 +0800 Subject: [PATCH] Add new features add casaOS logs add application terminal add application logs --- UI | 2 +- go.mod | 2 +- go.sum | 7 ++--- main.go | 3 +++ pkg/cache/cache.go | 11 ++++++++ pkg/utils/file/file.go | 1 + pkg/utils/httper/httper.go | 5 ++-- route/v1/docker.go | 17 ++---------- route/v1/system.go | 4 +-- service/docker.go | 1 - service/model/o_notify.go | 17 ++++++------ service/oasis.go | 54 ++++++++++++++++++++------------------ service/service.go | 3 +++ 13 files changed, 64 insertions(+), 63 deletions(-) create mode 100644 pkg/cache/cache.go diff --git a/UI b/UI index 63efca1..66ab6d9 160000 --- a/UI +++ b/UI @@ -1 +1 @@ -Subproject commit 63efca105aacbbf7ac7952f2ce9ac8e3b2e8b641 +Subproject commit 66ab6d947072bb025ae9917a223ef47ee6fbed09 diff --git a/go.mod b/go.mod index c5dd38d..5f58af2 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,6 @@ require ( github.com/forease/gotld v0.0.0-20190808124948-c50ff635576b github.com/gin-contrib/gzip v0.0.2 // indirect github.com/gin-gonic/gin v1.7.2 - github.com/gliderlabs/ssh v0.3.3 github.com/go-ini/ini v1.62.0 github.com/go-ole/go-ole v1.2.5 // indirect github.com/go-openapi/spec v0.20.4 // indirect @@ -37,6 +36,7 @@ require ( github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect github.com/morikuni/aec v1.0.0 // indirect github.com/opencontainers/selinux v1.8.5 // indirect + github.com/patrickmn/go-cache v2.1.0+incompatible github.com/pkg/errors v0.9.1 github.com/prestonTao/upnp v0.0.0-20150206124352-f4370df5e109 github.com/prometheus/procfs v0.7.3 // indirect diff --git a/go.sum b/go.sum index 9406e93..f349c6d 100644 --- a/go.sum +++ b/go.sum @@ -94,8 +94,6 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRF github.com/alexflint/go-filemutex v0.0.0-20171022225611-72bdc8eae2ae/go.mod h1:CgnQgUtFrFz9mxFNtED3jI5tLDjKlOM+oUF/sTk6ps0= github.com/andybalholm/cascadia v1.1.0 h1:BuuO6sSfQNFRu1LppgbD25Hr2vLYW25JvxHs5zzsLTo= github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= -github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8= -github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY= @@ -313,8 +311,6 @@ github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/ github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= github.com/gin-gonic/gin v1.7.2 h1:Tg03T9yM2xa8j6I3Z3oqLaQRSmKvxPd6g/2HJ6zICFA= github.com/gin-gonic/gin v1.7.2/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY= -github.com/gliderlabs/ssh v0.3.3 h1:mBQ8NiOgDkINJrZtoizkC3nDNYgSaWtxyem6S2XHBtA= -github.com/gliderlabs/ssh v0.3.3/go.mod h1:ZSS+CUoKHDrqVakTfTWUlKSr9MtMFkC4UvtQKD7O914= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= @@ -632,6 +628,8 @@ github.com/opencontainers/selinux v1.8.0/go.mod h1:RScLhm78qiWa2gbVCcGkC7tCGdgk3 github.com/opencontainers/selinux v1.8.2/go.mod h1:MUIHuUEvKB1wtJjQdOyYRgOnLD2xAPP8dBsCoU0KuF8= github.com/opencontainers/selinux v1.8.5 h1:OkT6bMHOQ1JQQO4ihjQ49sj0+wciDcjziSVTRn8VeTA= github.com/opencontainers/selinux v1.8.5/go.mod h1:HTvjPFoGMbpQsG886e3lQwnsRWtE4TC1OF3OUvG9FAo= +github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= +github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc= github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU= @@ -825,7 +823,6 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= -golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ= golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= diff --git a/main.go b/main.go index cae0f9e..e54d7b7 100644 --- a/main.go +++ b/main.go @@ -6,11 +6,13 @@ import ( "net/http" "time" + "github.com/IceWhaleTech/CasaOS/pkg/cache" "github.com/IceWhaleTech/CasaOS/pkg/config" "github.com/IceWhaleTech/CasaOS/pkg/sqlite" loger2 "github.com/IceWhaleTech/CasaOS/pkg/utils/loger" "github.com/IceWhaleTech/CasaOS/route" "github.com/IceWhaleTech/CasaOS/service" + "github.com/robfig/cron" "gorm.io/gorm" ) @@ -29,6 +31,7 @@ func init() { sqliteDB = sqlite.GetDb(config.AppInfo.ProjectPath) //gredis.GetRedisConn(config.RedisInfo), service.MyService = service.NewService(sqliteDB, loger2.NewOLoger()) + service.Cache = cache.Init() } // @title casaOS API diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go new file mode 100644 index 0000000..86df1a3 --- /dev/null +++ b/pkg/cache/cache.go @@ -0,0 +1,11 @@ +package cache + +import ( + "time" + + "github.com/patrickmn/go-cache" +) + +func Init() *cache.Cache { + return cache.New(5*time.Minute, 60*time.Second) +} diff --git a/pkg/utils/file/file.go b/pkg/utils/file/file.go index c1ff508..7ba10f3 100644 --- a/pkg/utils/file/file.go +++ b/pkg/utils/file/file.go @@ -50,6 +50,7 @@ func MkDir(src string) error { if err != nil { return err } + os.Chmod(src, 0777) return nil } diff --git a/pkg/utils/httper/httper.go b/pkg/utils/httper/httper.go index 7b8bf05..3e23c3c 100644 --- a/pkg/utils/httper/httper.go +++ b/pkg/utils/httper/httper.go @@ -3,12 +3,13 @@ package httper import ( "bytes" "encoding/json" - "github.com/IceWhaleTech/CasaOS/pkg/config" - "github.com/tidwall/gjson" "io" "io/ioutil" "net/http" "time" + + "github.com/IceWhaleTech/CasaOS/pkg/config" + "github.com/tidwall/gjson" ) //发送GET请求 diff --git a/route/v1/docker.go b/route/v1/docker.go index 2fea59e..b9e2210 100644 --- a/route/v1/docker.go +++ b/route/v1/docker.go @@ -238,10 +238,8 @@ func InstallApp(c *gin.Context) { var relyMap = make(map[string]string) go func() { installLog := model2.AppNotify{} - installLog.CustomId = id installLog.State = 0 installLog.Message = "installing rely" - installLog.Speed = 10 installLog.Type = types.NOTIFY_TYPE_UNIMPORTANT installLog.CreatedAt = strconv.FormatInt(time.Now().Unix(), 10) installLog.UpdatedAt = strconv.FormatInt(time.Now().Unix(), 10) @@ -279,7 +277,6 @@ func InstallApp(c *gin.Context) { } else { docker_base.MysqlDelete(mysqlContainerId) installLog.State = 0 - installLog.Speed = 30 installLog.Message = err.Error() service.MyService.Notify().UpdateLog(installLog) } @@ -288,7 +285,6 @@ func InstallApp(c *gin.Context) { } - installLog.Speed = 50 installLog.Message = "pulling" service.MyService.Notify().UpdateLog(installLog) @@ -296,7 +292,6 @@ func InstallApp(c *gin.Context) { err := service.MyService.Docker().DockerPullImage(dockerImage+":"+dockerImageVersion, installLog) if err != nil { installLog.State = 0 - installLog.Speed = 70 installLog.Message = err.Error() installLog.Type = types.NOTIFY_TYPE_ERROR service.MyService.Notify().UpdateLog(installLog) @@ -323,18 +318,17 @@ func InstallApp(c *gin.Context) { // return // } containerId, err := service.MyService.Docker().DockerContainerCreate(dockerImage+":"+dockerImageVersion, id, m, appInfo.NetworkModel) - installLog.ContainerId = containerId + installLog.Name = appInfo.Title + installLog.Icon = appInfo.Icon if err != nil { //service.MyService.Redis().Set(id, "{\"id\"\""+id+"\",\"state\":false,\"message\":\""+err.Error()+"\",\"speed\":80}", 100) installLog.State = 0 - installLog.Speed = 80 installLog.Type = types.NOTIFY_TYPE_ERROR installLog.Message = err.Error() service.MyService.Notify().UpdateLog(installLog) return } else { //service.MyService.Redis().Set(id, "{\"id\":\""+id+"\",\"state\":true,\"message\":\"starting\",\"speed\":80}", 100) - installLog.Speed = 80 installLog.Message = "starting" service.MyService.Notify().UpdateLog(installLog) } @@ -345,13 +339,11 @@ func InstallApp(c *gin.Context) { //service.MyService.Redis().Set(id, "{\"id\"\""+id+"\",\"state\":false,\"message\":\""+err.Error()+"\",\"speed\":90}", 100) installLog.State = 0 installLog.Type = types.NOTIFY_TYPE_ERROR - installLog.Speed = 90 installLog.Message = err.Error() service.MyService.Notify().UpdateLog(installLog) return } else { //service.MyService.Redis().Set(id, "{\"id\":\""+id+"\",\"state\":true,\"message\":\"setting upnp\",\"speed\":90}", 100) - installLog.Speed = 90 if m.Origin != CUSTOM { installLog.Message = "setting upnp" } else { @@ -396,13 +388,11 @@ func InstallApp(c *gin.Context) { if err != nil { //service.MyService.Redis().Set(id, "{\"id\"\""+id+"\",\"state\":false,\"message\":\""+err.Error()+"\",\"speed\":95}", 100) installLog.State = 0 - installLog.Speed = 95 installLog.Type = types.NOTIFY_TYPE_ERROR installLog.Message = err.Error() service.MyService.Notify().UpdateLog(installLog) } else { //service.MyService.Redis().Set(id, "{\"id\":\""+id+"\",\"state\":true,\"message\":\"checking\",\"speed\":95}", 100) - installLog.Speed = 95 installLog.Message = "checking" service.MyService.Notify().UpdateLog(installLog) } @@ -414,14 +404,12 @@ func InstallApp(c *gin.Context) { if err != nil && container.ContainerJSONBase.State.Running { //service.MyService.Redis().Set(id, "{\"id\"\""+id+"\",\"state\":false,\"message\":\""+err.Error()+"\",\"speed\":100}", 100) installLog.State = 0 - installLog.Speed = 100 installLog.Type = types.NOTIFY_TYPE_ERROR installLog.Message = err.Error() service.MyService.Notify().UpdateLog(installLog) return } else { //service.MyService.Redis().Set(id, "{\"id\":\""+id+"\",\"state\":true,\"message\":\"installed\",\"speed\":100}", 100) - installLog.Speed = 100 installLog.Message = "installed" service.MyService.Notify().UpdateLog(installLog) } @@ -802,7 +790,6 @@ func ContainerLog(c *gin.Context) { func GetInstallSpeed(c *gin.Context) { id := c.Param("id") b := service.MyService.Notify().GetLog(id) - b.Id = b.CustomId c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS), Data: b}) } diff --git a/route/v1/system.go b/route/v1/system.go index 11fbe57..69505eb 100644 --- a/route/v1/system.go +++ b/route/v1/system.go @@ -28,13 +28,12 @@ func CheckVersion(c *gin.Context) { need, version := version.IsNeedUpdate() if need { installLog := model2.AppNotify{} - installLog.CustomId = "" installLog.State = 0 installLog.Message = "New version " + version.Version + " is ready, ready to upgrade" - installLog.Speed = 100 installLog.Type = types.NOTIFY_TYPE_NEED_CONFIRM installLog.CreatedAt = strconv.FormatInt(time.Now().Unix(), 10) installLog.UpdatedAt = strconv.FormatInt(time.Now().Unix(), 10) + installLog.Name = "CasaOS System" service.MyService.Notify().AddLog(installLog) } data := make(map[string]interface{}, 1) @@ -69,7 +68,6 @@ func GetSystemConfig(c *gin.Context) { // @Produce application/json // @Accept application/json // @Tags sys -// @Param file query line false "get the number of log lines" // @Security ApiKeyAuth // @Success 200 {string} string "ok" // @Router /sys/error/logs [get] diff --git a/service/docker.go b/service/docker.go index b59b484..1f8ed2f 100644 --- a/service/docker.go +++ b/service/docker.go @@ -327,7 +327,6 @@ func (ds *dockerService) DockerPullImage(imageName string, m model2.AppNotify) e } m.Type = types2.NOTIFY_TYPE_INSTALL_LOG m.State = 0 - m.Speed = 70 m.Message = string(buf[:n]) MyService.Notify().UpdateLog(m) } diff --git a/service/model/o_notify.go b/service/model/o_notify.go index 6eac46c..9729a5c 100644 --- a/service/model/o_notify.go +++ b/service/model/o_notify.go @@ -1,15 +1,14 @@ package model type AppNotify struct { - CustomId string `gorm:"column:custom_id;primary_key" json:"custom_id"` - ContainerId string `json:"container_id,omitempty"` - State int `json:"state"` //0:一直在变动的未读消息 1:未读 2:已读 - Message string `json:"message"` - CreatedAt string `gorm:"<-:create;autoCreateTime" json:"created_at"` - UpdatedAt string `gorm:"<-:create;<-:update;autoUpdateTime" json:"updated_at"` - Speed int `json:"speed"` - Id string `gorm:"-" json:"id"` - Type int `json:"type"` // 1:显示即为已读 2:必须手动点掉 3:error + State int `json:"state"` //0:一直在变动的未读消息 1:未读 2:已读 + Message string `json:"message"` + CreatedAt string `json:"created_at"` + UpdatedAt string `json:"updated_at"` + Id string `json:"id"` + Type int `json:"type"` // 1:显示即为已读 2:info 3:warning 4:error 5:success + Icon string `json:"icon"` + Name string `json:"name"` } func (p *AppNotify) TableName() string { diff --git a/service/oasis.go b/service/oasis.go index 098cf7a..2784bf3 100644 --- a/service/oasis.go +++ b/service/oasis.go @@ -2,12 +2,13 @@ package service import ( json2 "encoding/json" + "strconv" + "github.com/IceWhaleTech/CasaOS/model" "github.com/IceWhaleTech/CasaOS/pkg/config" httper2 "github.com/IceWhaleTech/CasaOS/pkg/utils/httper" model2 "github.com/IceWhaleTech/CasaOS/service/model" "github.com/tidwall/gjson" - "strconv" ) type OasisService interface { @@ -22,14 +23,7 @@ type oasisService struct { func (o *oasisService) GetTaskList(size int) []model2.TaskDBModel { head := make(map[string]string) - t := make(chan string) - - go func() { - str := httper2.Get(config.ServerInfo.ServerApi+"/token", nil) - - t <- gjson.Get(str, "data").String() - }() - head["Authorization"] = <-t + head["Authorization"] = GetToken() listS := httper2.Get(config.ServerInfo.ServerApi+"/v1/task/list/"+strconv.Itoa(size), head) @@ -43,14 +37,7 @@ func (o *oasisService) GetServerList(index, size, tp, categoryId, key string) ([ head := make(map[string]string) - t := make(chan string) - - go func() { - str := httper2.Get(config.ServerInfo.ServerApi+"/token", nil) - - t <- gjson.Get(str, "data").String() - }() - head["Authorization"] = <-t + head["Authorization"] = GetToken() listS := httper2.Get(config.ServerInfo.ServerApi+"/v1/app/list?index="+index+"&size="+size+"&type="+tp+"&category_id="+categoryId+"&key="+key, head) @@ -65,15 +52,7 @@ func (o *oasisService) GetServerList(index, size, tp, categoryId, key string) ([ func (o *oasisService) GetServerCategoryList() []model.ServerCategoryList { head := make(map[string]string) - - t := make(chan string) - - go func() { - str := httper2.Get(config.ServerInfo.ServerApi+"/token", nil) - - t <- gjson.Get(str, "data").String() - }() - head["Authorization"] = <-t + head["Authorization"] = GetToken() listS := httper2.Get(config.ServerInfo.ServerApi+"/v1/app/category", head) @@ -84,6 +63,29 @@ func (o *oasisService) GetServerCategoryList() []model.ServerCategoryList { return list } +func GetToken() string { + t := make(chan string) + keyName := "casa_token" + + var auth string + if result, ok := Cache.Get(keyName); ok { + auth, ok = result.(string) + if ok { + + return auth + } + } + go func() { + str := httper2.Get(config.ServerInfo.ServerApi+"/token", nil) + + t <- gjson.Get(str, "data").String() + }() + auth = <-t + + Cache.SetDefault(keyName, auth) + return auth +} + func NewOasisService() OasisService { return &oasisService{} } diff --git a/service/service.go b/service/service.go index 82fedf0..0d49ba3 100644 --- a/service/service.go +++ b/service/service.go @@ -2,9 +2,12 @@ package service import ( loger2 "github.com/IceWhaleTech/CasaOS/pkg/utils/loger" + "github.com/patrickmn/go-cache" "gorm.io/gorm" ) +var Cache *cache.Cache + var MyService Repository type Repository interface {