installer.go 7.3 KB

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