Skip to content

Commit f020c11

Browse files
committed
Fixing bugs
Resolve application installation path errors
1 parent 3713096 commit f020c11

File tree

15 files changed

+327
-24
lines changed

15 files changed

+327
-24
lines changed

main.go

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func init() {
3232
//gredis.GetRedisConn(config.RedisInfo),
3333
service.MyService = service.NewService(sqliteDB, loger2.NewOLoger())
3434
service.Cache = cache.Init()
35+
route.InitFunction()
3536
}
3637

3738
// @title casaOS API

model/sys_common.go

+2
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,6 @@ type SystemConfig struct {
6464
ConfigStr string `json:"config_str"`
6565
WidgetList string `json:"widget_list"`
6666
ConfigPath string `json:"config_path"`
67+
SyncPort string `json:"sync_port"`
68+
SyncKey string `json:"sync_key"`
6769
}

model/system_app/sync.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package system_app
2+
3+
import "encoding/xml"
4+
5+
type SyncConfig struct {
6+
XMLName xml.Name `xml:"configuration"`
7+
Key string `xml:"gui>apikey"`
8+
}

pkg/docker/volumes.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@ func GetDir(id, envName string) string {
1010
}
1111

1212
switch {
13-
case strings.Contains(strings.ToLower(envName), "config"):
13+
case strings.Contains(strings.ToLower(envName), "config") || strings.Contains(strings.ToLower(envName), "photoprism/storage") || strings.Contains(strings.ToLower(envName), "config"):
1414
path = "/DATA/AppData/" + id + "/"
1515
case strings.Contains(strings.ToLower(envName), "movie"):
1616
path = "/DATA/Media/Movies/"
1717
case strings.Contains(strings.ToLower(envName), "music"):
1818
path = "/DATA/Media/Music/"
19+
case strings.Contains(strings.ToLower(envName), "photoprism/originals"):
20+
path = "/DATA/Gallery"
1921
case strings.Contains(strings.ToLower(envName), "download"):
2022
path = "/DATA/Downloads/"
2123
case strings.Contains(strings.ToLower(envName), "photo") || strings.Contains(strings.ToLower(envName), "pictures"):
2224
path = "/DATA/Downloads/"
25+
case strings.ToLower(envName) == "/srv":
26+
path = "/DATA/"
2327
default:
2428
//path = "/media"
2529
}

pkg/utils/file/file.go

+13
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,16 @@ func IsNotExistCreateFile(src string) error {
146146

147147
return nil
148148
}
149+
150+
func ReadFullFile(path string) []byte {
151+
file, err := os.Open(path)
152+
if err != nil {
153+
return []byte("")
154+
}
155+
defer file.Close()
156+
content, err := ioutil.ReadAll(file)
157+
if err != nil {
158+
return []byte("")
159+
}
160+
return content
161+
}

route/init.go

+188
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
package route
2+
3+
import (
4+
"encoding/json"
5+
"encoding/xml"
6+
"strconv"
7+
"time"
8+
9+
"github.com/IceWhaleTech/CasaOS/model"
10+
"github.com/IceWhaleTech/CasaOS/model/system_app"
11+
"github.com/IceWhaleTech/CasaOS/pkg/config"
12+
"github.com/IceWhaleTech/CasaOS/pkg/docker"
13+
"github.com/IceWhaleTech/CasaOS/pkg/utils/env_helper"
14+
"github.com/IceWhaleTech/CasaOS/pkg/utils/file"
15+
"github.com/IceWhaleTech/CasaOS/pkg/utils/port"
16+
"github.com/IceWhaleTech/CasaOS/service"
17+
model2 "github.com/IceWhaleTech/CasaOS/service/model"
18+
uuid "github.com/satori/go.uuid"
19+
)
20+
21+
func InitFunction() {
22+
go checkSystemApp()
23+
}
24+
25+
var syncIsExistence = false
26+
27+
func installSyncthing(appId string) {
28+
29+
var appInfo model.ServerAppList
30+
m := model.CustomizationPostData{}
31+
var dockerImage string
32+
var dockerImageVersion string
33+
34+
appInfo = service.MyService.OAPI().GetServerAppInfo(appId)
35+
36+
dockerImage = appInfo.Image
37+
38+
if len(appInfo.ImageVersion) == 0 {
39+
dockerImageVersion = "latest"
40+
}
41+
42+
if appInfo.NetworkModel != "host" {
43+
for i := 0; i < len(appInfo.Ports); i++ {
44+
if p, _ := strconv.Atoi(appInfo.Ports[i].ContainerPort); port.IsPortAvailable(p, appInfo.Ports[i].Protocol) {
45+
appInfo.Ports[i].CommendPort = strconv.Itoa(p)
46+
} else {
47+
if appInfo.Ports[i].Protocol == "tcp" {
48+
if p, err := port.GetAvailablePort("tcp"); err == nil {
49+
appInfo.Ports[i].CommendPort = strconv.Itoa(p)
50+
}
51+
} else if appInfo.Ports[i].Protocol == "upd" {
52+
if p, err := port.GetAvailablePort("udp"); err == nil {
53+
appInfo.Ports[i].CommendPort = strconv.Itoa(p)
54+
}
55+
}
56+
}
57+
58+
if appInfo.Ports[i].Type == 0 {
59+
appInfo.PortMap = appInfo.Ports[i].CommendPort
60+
}
61+
}
62+
}
63+
64+
for i := 0; i < len(appInfo.Devices); i++ {
65+
if !file.CheckNotExist(appInfo.Devices[i].ContainerPath) {
66+
appInfo.Devices[i].Path = appInfo.Devices[i].ContainerPath
67+
}
68+
}
69+
if len(appInfo.Tip) > 0 {
70+
appInfo.Tip = env_helper.ReplaceStringDefaultENV(appInfo.Tip)
71+
}
72+
73+
for i := 0; i < len(appInfo.Volumes); i++ {
74+
appInfo.Volumes[i].Path = docker.GetDir("", appInfo.Volumes[i].ContainerPath)
75+
}
76+
appInfo.MaxMemory = service.MyService.ZiMa().GetMemInfo().Total >> 20
77+
78+
id := uuid.NewV4().String()
79+
80+
installLog := model2.AppNotify{}
81+
82+
// step:下载镜像
83+
err := service.MyService.Docker().DockerPullImage(dockerImage+":"+dockerImageVersion, installLog)
84+
if err != nil {
85+
//pull image error
86+
return
87+
}
88+
89+
for !service.MyService.Docker().IsExistImage(dockerImage + ":" + dockerImageVersion) {
90+
time.Sleep(time.Second)
91+
}
92+
93+
m.CpuShares = 50
94+
m.Envs = appInfo.Envs
95+
m.Memory = int64(appInfo.MaxMemory)
96+
m.Origin = "system"
97+
m.PortMap = appInfo.PortMap
98+
m.Ports = appInfo.Ports
99+
m.Restart = ""
100+
m.Volumes = appInfo.Volumes
101+
102+
containerId, err := service.MyService.Docker().DockerContainerCreate(dockerImage+":"+dockerImageVersion, id, m, appInfo.NetworkModel)
103+
104+
if err != nil {
105+
// create container error
106+
return
107+
}
108+
109+
//step:start container
110+
err = service.MyService.Docker().DockerContainerStart(id)
111+
if err != nil {
112+
//start container error
113+
return
114+
}
115+
116+
portsStr, _ := json.Marshal(appInfo.Ports)
117+
envsStr, _ := json.Marshal(appInfo.Envs)
118+
volumesStr, _ := json.Marshal(appInfo.Volumes)
119+
devicesStr, _ := json.Marshal(appInfo.Devices)
120+
//step: 保存数据到数据库
121+
md := model2.AppListDBModel{
122+
CustomId: id,
123+
Title: appInfo.Title,
124+
//ScreenshotLink: appInfo.ScreenshotLink,
125+
Slogan: appInfo.Tagline,
126+
Description: appInfo.Description,
127+
//Tags: appInfo.Tags,
128+
Icon: appInfo.Icon,
129+
Version: dockerImageVersion,
130+
ContainerId: containerId,
131+
Image: dockerImage,
132+
Index: appInfo.Index,
133+
PortMap: appInfo.PortMap,
134+
Label: appInfo.Title,
135+
EnableUPNP: false,
136+
Ports: string(portsStr),
137+
Envs: string(envsStr),
138+
Volumes: string(volumesStr),
139+
Position: true,
140+
NetModel: appInfo.NetworkModel,
141+
Restart: m.Restart,
142+
CpuShares: 50,
143+
Memory: int64(appInfo.MaxMemory),
144+
Devices: string(devicesStr),
145+
Origin: m.Origin,
146+
CreatedAt: strconv.FormatInt(time.Now().Unix(), 10),
147+
UpdatedAt: strconv.FormatInt(time.Now().Unix(), 10),
148+
}
149+
service.MyService.App().SaveContainer(md)
150+
151+
checkSystemApp()
152+
}
153+
154+
// check if the system application is installed
155+
func checkSystemApp() {
156+
list := service.MyService.App().GetSystemAppList()
157+
for _, v := range *list {
158+
if v.Image == "linuxserver/syncthing" {
159+
syncIsExistence = true
160+
if config.SystemConfigInfo.SyncPort != v.Port {
161+
config.SystemConfigInfo.SyncPort = v.Port
162+
}
163+
var paths []model.PathMap
164+
json.Unmarshal([]byte(v.Volumes), &paths)
165+
path := ""
166+
for _, i := range paths {
167+
if i.ContainerPath == "/config" {
168+
path = docker.GetDir(v.CustomId, i.ContainerPath) + "config.xml"
169+
for i := 0; i < 10; i++ {
170+
if file.CheckNotExist(path) {
171+
time.Sleep(1 * time.Second)
172+
} else {
173+
break
174+
}
175+
}
176+
break
177+
}
178+
}
179+
content := file.ReadFullFile(path)
180+
syncConfig := &system_app.SyncConfig{}
181+
xml.Unmarshal(content, &syncConfig)
182+
config.SystemConfigInfo.SyncKey = syncConfig.Key
183+
}
184+
}
185+
if !syncIsExistence {
186+
installSyncthing("44")
187+
}
188+
}

route/route.go

+1-16
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package route
22

33
import (
4-
"fmt"
54
"net/http"
6-
"net/http/httputil"
7-
"net/url"
85

96
"github.com/IceWhaleTech/CasaOS/middleware"
107
"github.com/IceWhaleTech/CasaOS/pkg/config"
@@ -41,19 +38,6 @@ func InitRouter() *gin.Engine {
4138
//get user info
4239
r.GET("/v1/user/info", v1.UserInfo)
4340

44-
r.GET("/syncthing/*url", func(c *gin.Context) {
45-
ur := c.Param("url")
46-
fmt.Println(ur)
47-
target := "http://localhost:8384" //最终要访问的服务
48-
remote, err := url.Parse(target)
49-
if err != nil {
50-
fmt.Println(err)
51-
}
52-
proxy := httputil.NewSingleHostReverseProxy(remote)
53-
c.Request.URL.Path = "/" + ur //请求API
54-
proxy.ServeHTTP(c.Writer, c.Request)
55-
})
56-
5741
v1Group := r.Group("/v1")
5842

5943
v1Group.Use(jwt2.JWT(swagHandler))
@@ -282,6 +266,7 @@ func InitRouter() *gin.Engine {
282266
{
283267
v1SearchGroup.GET("/search", v1.GetSearchList)
284268
}
269+
v1Group.GET("/sync/*url", v1.SyncToSyncthing)
285270
}
286271
return r
287272
}

route/v1/docker.go

+4
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,10 @@ func ContainerUpdateInfo(c *gin.Context) {
11351135
var vol model.PathArray
11361136
json2.Unmarshal([]byte(appInfo.Volumes), &vol)
11371137

1138+
for i := 0; i < len(vol); i++ {
1139+
vol[i].Path = strings.ReplaceAll(vol[i].Path, "$AppID", appId)
1140+
}
1141+
11381142
var dir model.PathArray
11391143
json2.Unmarshal([]byte(appInfo.Devices), &dir)
11401144

route/v1/sync.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package v1
2+
3+
import (
4+
"net/http/httputil"
5+
"net/url"
6+
7+
"github.com/IceWhaleTech/CasaOS/pkg/config"
8+
"github.com/gin-gonic/gin"
9+
)
10+
11+
func SyncToSyncthing(c *gin.Context) {
12+
u := c.Param("url")
13+
target := "http://127.0.0.1:" + config.SystemConfigInfo.SyncPort
14+
remote, err := url.Parse(target)
15+
if err != nil {
16+
return
17+
}
18+
proxy := httputil.NewSingleHostReverseProxy(remote)
19+
c.Request.Header.Add("X-API-Key", config.SystemConfigInfo.SyncKey)
20+
c.Request.URL.Path = u
21+
proxy.ServeHTTP(c.Writer, c.Request)
22+
}

0 commit comments

Comments
 (0)