migration-034-035.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * @Author: LinkLeong link@icewhale.org
  3. * @Date: 2022-08-24 17:36:00
  4. * @LastEditors: LinkLeong
  5. * @LastEditTime: 2022-09-05 11:24:27
  6. * @FilePath: /CasaOS/cmd/migration-tool/migration-034-035.go
  7. * @Description:
  8. * @Website: https://www.casaos.io
  9. * Copyright (c) 2022 by icewhale, All Rights Reserved.
  10. */
  11. package main
  12. import (
  13. "io"
  14. "io/ioutil"
  15. "os"
  16. "path"
  17. "strings"
  18. interfaces "github.com/IceWhaleTech/CasaOS-Common"
  19. "github.com/IceWhaleTech/CasaOS-Common/utils/version"
  20. "github.com/IceWhaleTech/CasaOS/pkg/config"
  21. "github.com/IceWhaleTech/CasaOS/pkg/utils/command"
  22. "github.com/IceWhaleTech/CasaOS/pkg/utils/file"
  23. "github.com/IceWhaleTech/CasaOS/service"
  24. )
  25. type migrationTool036 struct{}
  26. func (u *migrationTool036) IsMigrationNeeded() (bool, error) {
  27. majorVersion, minorVersion, patchVersion, err := version.DetectLegacyVersion()
  28. if err != nil {
  29. if err == version.ErrLegacyVersionNotFound {
  30. return false, nil
  31. }
  32. return false, err
  33. }
  34. if majorVersion > 0 {
  35. return false, nil
  36. }
  37. if minorVersion > 3 {
  38. return false, nil
  39. }
  40. if minorVersion == 3 && patchVersion > 5 {
  41. return false, nil
  42. }
  43. _logger.Info("Migration is needed for a CasaOS version 0.3.5 and older...")
  44. return true, nil
  45. }
  46. func (u *migrationTool036) PreMigrate() error {
  47. return nil
  48. }
  49. func (u *migrationTool036) Migrate() error {
  50. if service.MyService.System().GetSysInfo().KernelArch == "aarch64" && config.ServerInfo.USBAutoMount != "True" && strings.Contains(service.MyService.System().GetDeviceTree(), "Raspberry Pi") {
  51. service.MyService.System().UpdateUSBAutoMount("False")
  52. service.MyService.System().ExecUSBAutoMountShell("False")
  53. }
  54. newAPIUrl := "https://api.casaos.io/casaos-api"
  55. if config.ServerInfo.ServerApi == "https://api.casaos.zimaboard.com" {
  56. config.ServerInfo.ServerApi = newAPIUrl
  57. config.Cfg.Section("server").Key("ServerApi").SetValue(newAPIUrl)
  58. config.Cfg.SaveTo(config.SystemConfigInfo.ConfigPath)
  59. }
  60. command.OnlyExec("curl -fsSL https://raw.githubusercontent.com/IceWhaleTech/get/main/assist.sh | bash")
  61. if !file.CheckNotExist("/casaOS") {
  62. command.OnlyExec("source /casaOS/server/shell/update.sh ;")
  63. command.OnlyExec("source " + config.AppInfo.ShellPath + "/delete-old-service.sh ;")
  64. }
  65. service.MyService.App().ImportApplications(true)
  66. src := "/casaOS/server/conf/conf.ini"
  67. if file.Exists(src) {
  68. dst := "/etc/casaos/casaos.conf"
  69. source, err := os.Open(src)
  70. if err != nil {
  71. return err
  72. }
  73. defer source.Close()
  74. destination, err := os.Create(dst)
  75. if err != nil {
  76. return err
  77. }
  78. defer destination.Close()
  79. _, err = io.Copy(destination, source)
  80. if err != nil {
  81. return err
  82. }
  83. }
  84. if file.Exists("/casaOS/server/db") {
  85. var fds []os.FileInfo
  86. var err error
  87. to := "/var/lib/casaos/db"
  88. file.IsNotExistMkDir(to)
  89. from := "/casaOS/server/db"
  90. if fds, err = ioutil.ReadDir(from); err != nil {
  91. return err
  92. }
  93. for _, fd := range fds {
  94. srcfp := path.Join(from, fd.Name())
  95. dstfp := path.Join(to, fd.Name())
  96. source, err := os.Open(srcfp)
  97. if err != nil {
  98. return err
  99. }
  100. defer source.Close()
  101. destination, err := os.Create(dstfp)
  102. if err != nil {
  103. return err
  104. }
  105. defer destination.Close()
  106. _, err = io.Copy(destination, source)
  107. if err != nil {
  108. return err
  109. }
  110. }
  111. }
  112. if file.Exists("/casaOS/server/conf") {
  113. var fds []os.FileInfo
  114. var err error
  115. to := "/var/lib/casaos/conf"
  116. file.IsNotExistMkDir(to)
  117. from := "/casaOS/server/conf"
  118. if fds, err = ioutil.ReadDir(from); err != nil {
  119. return err
  120. }
  121. for _, fd := range fds {
  122. fExt := path.Ext(fd.Name())
  123. if fExt != ".json" {
  124. continue
  125. }
  126. srcfp := path.Join(from, fd.Name())
  127. dstfp := path.Join(to, fd.Name())
  128. source, err := os.Open(srcfp)
  129. if err != nil {
  130. return err
  131. }
  132. defer source.Close()
  133. destination, err := os.Create(dstfp)
  134. if err != nil {
  135. return err
  136. }
  137. defer destination.Close()
  138. _, err = io.Copy(destination, source)
  139. if err != nil {
  140. return err
  141. }
  142. }
  143. }
  144. _logger.Info("update done")
  145. return nil
  146. }
  147. func (u *migrationTool036) PostMigrate() error {
  148. return nil
  149. }
  150. func NewMigrationToolFor_035() interfaces.MigrationTool {
  151. return &migrationTool{}
  152. }