installer.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package modules
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io/ioutil"
  6. "log"
  7. "net/http"
  8. "os"
  9. "path/filepath"
  10. "strconv"
  11. "time"
  12. "github.com/go-git/go-git/v5"
  13. uuid "github.com/satori/go.uuid"
  14. agi "imuslab.com/arozos/mod/agi"
  15. fs "imuslab.com/arozos/mod/filesystem"
  16. "imuslab.com/arozos/mod/utils"
  17. )
  18. /*
  19. Module Installer
  20. author: tobychui
  21. This script handle the installation of modules in the arozos system
  22. */
  23. //Install a module via selecting a zip file
  24. func (m *ModuleHandler) InstallViaZip(realpath string, gateway *agi.Gateway) error {
  25. //Check if file exists
  26. if !utils.FileExists(realpath) {
  27. return errors.New("*Module Installer* Installer file not found. Given: " + realpath)
  28. }
  29. //Install it
  30. unzipTmpFolder := "./tmp/installer/" + strconv.Itoa(int(time.Now().Unix()))
  31. err := fs.Unzip(realpath, unzipTmpFolder)
  32. if err != nil {
  33. return err
  34. }
  35. //Move the module(s) to the web root
  36. files, _ := filepath.Glob(unzipTmpFolder + "/*")
  37. folders := []string{}
  38. for _, file := range files {
  39. if utils.IsDir(file) && utils.FileExists(filepath.Join(file, "init.agi")) {
  40. //This looks like a module folder
  41. folders = append(folders, file)
  42. }
  43. }
  44. /*
  45. for _, folder := range folders {
  46. //Copy the module
  47. //WIP
  48. err = fs.CopyDir(folder, "./web/"+filepath.Base(folder))
  49. if err != nil {
  50. log.Println(err)
  51. continue
  52. }
  53. //Activate the module
  54. m.ActivateModuleByRoot("./web/"+filepath.Base(folder), gateway)
  55. m.ModuleSortList()
  56. }
  57. */
  58. //Remove the tmp folder
  59. os.RemoveAll(unzipTmpFolder)
  60. //OK
  61. return nil
  62. }
  63. //Reload all modules from agi file again
  64. func (m *ModuleHandler) ReloadAllModules(gateway *agi.Gateway) error {
  65. //Clear the current registered module list
  66. newModuleList := []*ModuleInfo{}
  67. for _, thisModule := range m.LoadedModule {
  68. if !thisModule.allowReload {
  69. //This module is registered by system. Do not allow reload
  70. newModuleList = append(newModuleList, thisModule)
  71. }
  72. }
  73. m.LoadedModule = newModuleList
  74. //Reload all webapp init.agi gateway script from source
  75. gateway.InitiateAllWebAppModules()
  76. m.ModuleSortList()
  77. return nil
  78. }
  79. //Install a module via git clone
  80. func (m *ModuleHandler) InstallModuleViaGit(gitURL string, gateway *agi.Gateway) error {
  81. //Download the module from the gitURL
  82. log.Println("Starting module installation by Git cloning ", gitURL)
  83. newDownloadUUID := uuid.NewV4().String()
  84. downloadFolder := filepath.Join(m.tmpDirectory, "download", newDownloadUUID)
  85. os.MkdirAll(downloadFolder, 0777)
  86. _, err := git.PlainClone(downloadFolder, false, &git.CloneOptions{
  87. URL: gitURL,
  88. Progress: os.Stdout,
  89. })
  90. if err != nil {
  91. return err
  92. }
  93. //Copy all folder within the download folder to the web root
  94. downloadedFiles, _ := filepath.Glob(downloadFolder + "/*")
  95. copyPendingList := []string{}
  96. for _, file := range downloadedFiles {
  97. if utils.IsDir(file) {
  98. //Exclude two special folder: github and images
  99. if filepath.Base(file) == ".github" || filepath.Base(file) == "images" || filepath.Base(file)[:1] == "." {
  100. //Reserved folder for putting Github readme screenshots or other things
  101. continue
  102. }
  103. //This file object is a folder. Copy to webroot
  104. copyPendingList = append(copyPendingList, file)
  105. }
  106. }
  107. //Do the copying
  108. //WIP
  109. /*
  110. for _, src := range copyPendingList {
  111. fs.FileCopy(src, "./web/", "skip", func(progress int, filename string) {
  112. log.Println("Copying ", filename)
  113. })
  114. }
  115. */
  116. //Clean up the download folder
  117. os.RemoveAll(downloadFolder)
  118. //Add the newly installed module to module list
  119. for _, moduleFolder := range copyPendingList {
  120. //This module folder has been moved to web successfully.
  121. m.ActivateModuleByRoot(moduleFolder, gateway)
  122. }
  123. //Sort the module lsit
  124. m.ModuleSortList()
  125. return nil
  126. }
  127. func (m *ModuleHandler) ActivateModuleByRoot(moduleFolder string, gateway *agi.Gateway) error {
  128. //Check if there is init.agi. If yes, load it as an module
  129. thisModuleEstimataedRoot := filepath.Join("./web/", filepath.Base(moduleFolder))
  130. if utils.FileExists(thisModuleEstimataedRoot) {
  131. if utils.FileExists(filepath.Join(thisModuleEstimataedRoot, "init.agi")) {
  132. //Load this as an module
  133. startDef, err := ioutil.ReadFile(filepath.Join(thisModuleEstimataedRoot, "init.agi"))
  134. if err != nil {
  135. log.Println("*Module Activator* Failed to read init.agi from " + filepath.Base(moduleFolder))
  136. return errors.New("Failed to read init.agi from " + filepath.Base(moduleFolder))
  137. }
  138. //Execute the init script using AGI
  139. log.Println("Starting module: ", filepath.Base(moduleFolder))
  140. err = gateway.RunScript(string(startDef))
  141. if err != nil {
  142. log.Println("*Module Activator* " + filepath.Base(moduleFolder) + " Starting failed" + err.Error())
  143. return errors.New(filepath.Base(moduleFolder) + " Starting failed: " + err.Error())
  144. }
  145. }
  146. }
  147. return nil
  148. }
  149. //Handle and return the information of the current installed modules
  150. func (m *ModuleHandler) HandleModuleInstallationListing(w http.ResponseWriter, r *http.Request) {
  151. type ModuleInstallInfo struct {
  152. Name string //Name of the module
  153. Desc string //Description of module
  154. Group string //Group of the module
  155. Version string //Version of the module
  156. IconPath string //The icon access path of the module
  157. InstallDate string //The last editing date of the module file
  158. DiskSpace int64 //Disk space used
  159. Uninstallable bool //Indicate if this can be uninstall or disabled
  160. }
  161. results := []ModuleInstallInfo{}
  162. for _, mod := range m.LoadedModule {
  163. //Get total size
  164. if mod.StartDir != "" {
  165. //Only allow uninstalling of modules with start dir (aka installable)
  166. //Check if WebApp or subservice
  167. if utils.FileExists(filepath.Join("./web", mod.StartDir)) {
  168. //This is a WebApp module
  169. totalsize, _ := fs.GetDirctorySize(filepath.Join("./web", filepath.Dir(mod.StartDir)), false)
  170. //Get mod time
  171. mtime, err := fs.GetModTime(filepath.Join("./web", filepath.Dir(mod.StartDir)))
  172. if err != nil {
  173. log.Println(err)
  174. }
  175. t := time.Unix(mtime, 0)
  176. //Check allow uninstall state
  177. canUninstall := true
  178. if mod.Name == "System Setting" || mod.Group == "System Tools" {
  179. canUninstall = false
  180. }
  181. results = append(results, ModuleInstallInfo{
  182. mod.Name,
  183. mod.Desc,
  184. mod.Group,
  185. mod.Version,
  186. mod.IconPath,
  187. t.Format("2006-01-02"),
  188. totalsize,
  189. canUninstall,
  190. })
  191. } else {
  192. //Subservice
  193. }
  194. }
  195. }
  196. js, _ := json.Marshal(results)
  197. utils.SendJSONResponse(w, string(js))
  198. }
  199. //Uninstall the given module
  200. func (m *ModuleHandler) UninstallModule(moduleName string) error {
  201. //Check if this module is allowed to be removed
  202. var targetModuleInfo *ModuleInfo = nil
  203. for _, mod := range m.LoadedModule {
  204. if mod.Name == moduleName {
  205. targetModuleInfo = mod
  206. break
  207. }
  208. }
  209. if targetModuleInfo.Group == "System Tools" || targetModuleInfo.Name == "System Setting" {
  210. //Reject Remove Operation
  211. return errors.New("Protected modules cannot be removed")
  212. }
  213. //Check if the module exists
  214. if utils.FileExists(filepath.Join("./web", moduleName)) {
  215. //Remove the module
  216. log.Println("Removing Module: ", moduleName)
  217. os.RemoveAll(filepath.Join("./web", moduleName))
  218. //Unregister the module from loaded list
  219. newLoadedModuleList := []*ModuleInfo{}
  220. for _, thisModule := range m.LoadedModule {
  221. if thisModule.Name != moduleName {
  222. newLoadedModuleList = append(newLoadedModuleList, thisModule)
  223. }
  224. }
  225. m.LoadedModule = newLoadedModuleList
  226. } else {
  227. return errors.New("Module not exists")
  228. }
  229. return nil
  230. }