init.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. package route
  2. import (
  3. "encoding/json"
  4. "encoding/xml"
  5. "fmt"
  6. "runtime"
  7. "strconv"
  8. "time"
  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/command"
  14. "github.com/IceWhaleTech/CasaOS/pkg/utils/env_helper"
  15. "github.com/IceWhaleTech/CasaOS/pkg/utils/file"
  16. "github.com/IceWhaleTech/CasaOS/pkg/utils/port"
  17. "github.com/IceWhaleTech/CasaOS/service"
  18. model2 "github.com/IceWhaleTech/CasaOS/service/model"
  19. uuid "github.com/satori/go.uuid"
  20. )
  21. func InitFunction() {
  22. go checkSystemApp()
  23. Update2_3()
  24. CheckSerialDiskMount()
  25. CheckToken2_11()
  26. }
  27. var syncIsExistence = false
  28. func installSyncthing(appId string) {
  29. var appInfo model.ServerAppList
  30. m := model.CustomizationPostData{}
  31. var dockerImage string
  32. var dockerImageVersion string
  33. appInfo = service.MyService.Casa().GetServerAppInfo(appId, "system", "us_en")
  34. dockerImage = appInfo.Image
  35. dockerImageVersion = appInfo.ImageVersion
  36. if len(appInfo.ImageVersion) == 0 {
  37. dockerImageVersion = "latest"
  38. }
  39. if appInfo.NetworkModel != "host" {
  40. for i := 0; i < len(appInfo.Ports); i++ {
  41. if p, _ := strconv.Atoi(appInfo.Ports[i].ContainerPort); port.IsPortAvailable(p, appInfo.Ports[i].Protocol) {
  42. appInfo.Ports[i].CommendPort = strconv.Itoa(p)
  43. } else {
  44. if appInfo.Ports[i].Protocol == "tcp" {
  45. if p, err := port.GetAvailablePort("tcp"); err == nil {
  46. appInfo.Ports[i].CommendPort = strconv.Itoa(p)
  47. }
  48. } else if appInfo.Ports[i].Protocol == "upd" {
  49. if p, err := port.GetAvailablePort("udp"); err == nil {
  50. appInfo.Ports[i].CommendPort = strconv.Itoa(p)
  51. }
  52. }
  53. }
  54. if appInfo.Ports[i].Type == 0 {
  55. appInfo.PortMap = appInfo.Ports[i].CommendPort
  56. }
  57. }
  58. }
  59. for i := 0; i < len(appInfo.Devices); i++ {
  60. if !file.CheckNotExist(appInfo.Devices[i].ContainerPath) {
  61. appInfo.Devices[i].Path = appInfo.Devices[i].ContainerPath
  62. }
  63. }
  64. if len(appInfo.Tip) > 0 {
  65. appInfo.Tip = env_helper.ReplaceStringDefaultENV(appInfo.Tip)
  66. }
  67. appInfo.MaxMemory = service.MyService.ZiMa().GetMemInfo().Total >> 20
  68. id := uuid.NewV4().String()
  69. installLog := model2.AppNotify{}
  70. // step:下载镜像
  71. err := service.MyService.Docker().DockerPullImage(dockerImage+":"+dockerImageVersion, installLog)
  72. if err != nil {
  73. //pull image error
  74. fmt.Println("pull image error", err, dockerImage, dockerImageVersion)
  75. return
  76. }
  77. for !service.MyService.Docker().IsExistImage(dockerImage + ":" + dockerImageVersion) {
  78. time.Sleep(time.Second)
  79. }
  80. m.CpuShares = 50
  81. m.Envs = appInfo.Envs
  82. m.Memory = int64(appInfo.MaxMemory)
  83. m.Origin = "system"
  84. m.PortMap = appInfo.PortMap
  85. m.Ports = appInfo.Ports
  86. m.Restart = "always"
  87. m.Volumes = appInfo.Volumes
  88. containerId, err := service.MyService.Docker().DockerContainerCreate(dockerImage+":"+dockerImageVersion, id, m, appInfo.NetworkModel)
  89. if err != nil {
  90. fmt.Println("container create error", err)
  91. // create container error
  92. return
  93. }
  94. //step:start container
  95. err = service.MyService.Docker().DockerContainerStart(id)
  96. if err != nil {
  97. //start container error
  98. return
  99. }
  100. portsStr, _ := json.Marshal(appInfo.Ports)
  101. envsStr, _ := json.Marshal(appInfo.Envs)
  102. volumesStr, _ := json.Marshal(appInfo.Volumes)
  103. devicesStr, _ := json.Marshal(appInfo.Devices)
  104. //step: 保存数据到数据库
  105. md := model2.AppListDBModel{
  106. CustomId: id,
  107. Title: appInfo.Title,
  108. //ScreenshotLink: appInfo.ScreenshotLink,
  109. Slogan: appInfo.Tagline,
  110. Description: appInfo.Description,
  111. //Tags: appInfo.Tags,
  112. Icon: appInfo.Icon,
  113. Version: dockerImageVersion,
  114. ContainerId: containerId,
  115. Image: dockerImage,
  116. Index: appInfo.Index,
  117. PortMap: appInfo.PortMap,
  118. Label: appInfo.Title,
  119. EnableUPNP: false,
  120. Ports: string(portsStr),
  121. Envs: string(envsStr),
  122. Volumes: string(volumesStr),
  123. Position: true,
  124. NetModel: appInfo.NetworkModel,
  125. Restart: m.Restart,
  126. CpuShares: 50,
  127. Memory: int64(appInfo.MaxMemory),
  128. Devices: string(devicesStr),
  129. Origin: m.Origin,
  130. CreatedAt: strconv.FormatInt(time.Now().Unix(), 10),
  131. UpdatedAt: strconv.FormatInt(time.Now().Unix(), 10),
  132. }
  133. service.MyService.App().SaveContainer(md)
  134. checkSystemApp()
  135. }
  136. // check if the system application is installed
  137. func checkSystemApp() {
  138. list := service.MyService.App().GetSystemAppList()
  139. for _, v := range *list {
  140. if v.Image == "linuxserver/syncthing" {
  141. if v.State != "running" {
  142. //step:start container
  143. service.MyService.Docker().DockerContainerStart(v.CustomId)
  144. }
  145. syncIsExistence = true
  146. if config.SystemConfigInfo.SyncPort != v.Port {
  147. config.SystemConfigInfo.SyncPort = v.Port
  148. }
  149. var paths []model.PathMap
  150. json.Unmarshal([]byte(v.Volumes), &paths)
  151. path := ""
  152. for _, i := range paths {
  153. if i.ContainerPath == "/config" {
  154. path = docker.GetDir(v.CustomId, i.Path) + "/config.xml"
  155. for i := 0; i < 10; i++ {
  156. if file.CheckNotExist(path) {
  157. time.Sleep(1 * time.Second)
  158. } else {
  159. break
  160. }
  161. }
  162. break
  163. }
  164. }
  165. content := file.ReadFullFile(path)
  166. syncConfig := &system_app.SyncConfig{}
  167. xml.Unmarshal(content, &syncConfig)
  168. config.SystemConfigInfo.SyncKey = syncConfig.Key
  169. }
  170. }
  171. if !syncIsExistence {
  172. installSyncthing("74")
  173. }
  174. }
  175. func CheckSerialDiskMount() {
  176. // check mount point
  177. dbList := service.MyService.Disk().GetSerialAll()
  178. list := service.MyService.Disk().LSBLK(true)
  179. mountPoint := make(map[string]string, len(dbList))
  180. //remount
  181. for _, v := range dbList {
  182. mountPoint[v.UUID] = v.MountPoint
  183. }
  184. for _, v := range list {
  185. command.ExecEnabledSMART(v.Path)
  186. if v.Children != nil {
  187. for _, h := range v.Children {
  188. if len(h.MountPoint) == 0 && len(v.Children) == 1 && h.FsType == "ext4" {
  189. if m, ok := mountPoint[h.UUID]; ok {
  190. //mount point check
  191. volume := m
  192. if !file.CheckNotExist(m) {
  193. for i := 0; file.CheckNotExist(volume); i++ {
  194. volume = m + strconv.Itoa(i+1)
  195. }
  196. }
  197. service.MyService.Disk().MountDisk(h.Path, volume)
  198. if volume != m {
  199. ms := model2.SerialDisk{}
  200. ms.UUID = v.UUID
  201. ms.MountPoint = volume
  202. service.MyService.Disk().UpdateMountPoint(ms)
  203. }
  204. }
  205. }
  206. }
  207. }
  208. }
  209. service.MyService.Disk().RemoveLSBLKCache()
  210. command.OnlyExec("source " + config.AppInfo.ProjectPath + "/shell/helper.sh ;AutoRemoveUnuseDir")
  211. }
  212. func Update2_3() {
  213. command.OnlyExec("source " + config.AppInfo.ProjectPath + "/shell/assist.sh")
  214. }
  215. func CheckToken2_11() {
  216. if len(config.ServerInfo.Token) == 0 {
  217. token := uuid.NewV4().String
  218. config.ServerInfo.Token = token()
  219. config.Cfg.Section("server").Key("Token").SetValue(token())
  220. config.Cfg.SaveTo(config.SystemConfigInfo.ConfigPath)
  221. }
  222. if len(config.AppInfo.RootPath) == 0 {
  223. config.Cfg.Section("app").Key("RootPath").SetValue("/casaOS")
  224. config.AppInfo.RootPath = "/casaOS"
  225. config.Cfg.SaveTo(config.SystemConfigInfo.ConfigPath)
  226. }
  227. // if len(config.ServerInfo.Handshake) == 0 {
  228. // config.Cfg.Section("app").Key("RootPath").SetValue("/casaOS")
  229. // config.AppInfo.RootPath = "/casaOS"
  230. // config.Cfg.SaveTo(config.SystemConfigInfo.ConfigPath)
  231. // }
  232. sysType := runtime.GOOS
  233. if len(config.FileSettingInfo.DownloadDir) == 0 {
  234. downloadPath := "/DATA/Downloads"
  235. if sysType == "windows" {
  236. downloadPath = "C:\\CasaOS\\DATA\\Downloads"
  237. }
  238. if sysType == "darwin" {
  239. downloadPath = "~/CasaOS/DATA/Downloads"
  240. }
  241. config.Cfg.Section("file").Key("DownloadDir").SetValue(downloadPath)
  242. config.FileSettingInfo.DownloadDir = downloadPath
  243. file.IsNotExistMkDir(config.FileSettingInfo.DownloadDir)
  244. config.Cfg.SaveTo(config.SystemConfigInfo.ConfigPath)
  245. }
  246. if len(config.UserInfo.Description) == 0 {
  247. config.Cfg.Section("user").Key("Description").SetValue("nothing")
  248. config.UserInfo.Description = "nothing"
  249. config.Cfg.SaveTo(config.SystemConfigInfo.ConfigPath)
  250. }
  251. if len(config.ServerInfo.Handshake) == 0 {
  252. config.Cfg.Section("server").Key("Handshake").SetValue("socket.casaos.io")
  253. config.ServerInfo.Handshake = "socket.casaos.io"
  254. config.Cfg.SaveTo(config.SystemConfigInfo.ConfigPath)
  255. }
  256. service.MyService.System().ExecUSBAutoMountShell(config.ServerInfo.USBAutoMount)
  257. // str := []string{}
  258. // str = append(str, "ddd")
  259. // str = append(str, "aaa")
  260. // ddd := strings.Join(str, "|")
  261. // config.Cfg.Section("file").Key("ShareDir").SetValue(ddd)
  262. // config.Cfg.SaveTo(config.SystemConfigInfo.ConfigPath)
  263. }