module.package.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package main
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "log"
  6. "net/http"
  7. "os"
  8. "os/exec"
  9. "runtime"
  10. "strings"
  11. )
  12. /*
  13. Pacakge management tool for Linux OS with APT
  14. ONLY USABLE under Linux environment
  15. */
  16. func module_package_init() {
  17. http.HandleFunc("/system/apt/list", module_package_listAPT)
  18. }
  19. //Install the given package if not exists. Set mustComply to true for "panic on failed to install"
  20. func module_package_installIfNotExists(pkgname string, mustComply bool) error {
  21. //Clear the pkgname
  22. pkgname = strings.ReplaceAll(pkgname, "&", "")
  23. pkgname = strings.ReplaceAll(pkgname, "|", "")
  24. if runtime.GOOS == "windows" {
  25. //Check if the command already exists in windows path paramters.
  26. cmd := exec.Command("where", pkgname, "2>", "nul")
  27. _, err := cmd.CombinedOutput()
  28. if err != nil {
  29. return errors.New("Package " + pkgname + " not found in Windows %PATH%.")
  30. }
  31. return nil
  32. }
  33. if *allow_package_autoInstall == false {
  34. return errors.New("Package auto install is disabled")
  35. }
  36. cmd := exec.Command("which", pkgname)
  37. out, _ := cmd.CombinedOutput()
  38. if len(string(out)) > 1 {
  39. return nil
  40. } else {
  41. //Package not installed. Install if now if running in sudo mode
  42. log.Println("Installing package " + pkgname + "...")
  43. cmd := exec.Command("apt-get", "install", "-y", pkgname)
  44. cmd.Stdout = os.Stdout
  45. cmd.Stderr = os.Stderr
  46. err := cmd.Run()
  47. if err != nil {
  48. if mustComply {
  49. //Panic and terminate server process
  50. log.Println("Installation failed on package: "+pkgname, string(out))
  51. os.Exit(0)
  52. } else {
  53. log.Println("Installation failed on package: " + pkgname)
  54. log.Println(string(out))
  55. }
  56. return err
  57. }
  58. return nil
  59. }
  60. return nil
  61. }
  62. func module_package_test(w http.ResponseWriter, r *http.Request) {
  63. module_package_installIfNotExists("ffmpeg", true)
  64. module_package_installIfNotExists("samba", true)
  65. }
  66. func module_package_listAPT(w http.ResponseWriter, r *http.Request) {
  67. if runtime.GOOS == "windows" {
  68. sendErrorResponse(w, "Function disabled on Windows")
  69. return
  70. }
  71. cmd := exec.Command("apt", "list", "--installed")
  72. out, err := cmd.CombinedOutput()
  73. if err != nil {
  74. sendErrorResponse(w, err.Error())
  75. return
  76. }
  77. results := [][]string{}
  78. //Parse the output string
  79. installedPackages := strings.Split(string(out), "\n")
  80. for _, thisPackage := range installedPackages {
  81. if len(thisPackage) > 0 {
  82. packageInfo := strings.Split(thisPackage, "/")
  83. packageName := packageInfo[0]
  84. if len(packageInfo) >= 2 {
  85. packageVersion := strings.Split(packageInfo[1], ",")[1]
  86. if packageVersion[:3] == "now" {
  87. packageVersion = packageVersion[4:]
  88. }
  89. if strings.Contains(packageVersion, "[installed") && packageVersion[len(packageVersion)-1:] != "]" {
  90. packageVersion = packageVersion + ",automatic]"
  91. }
  92. results = append(results, []string{packageName, packageVersion})
  93. }
  94. }
  95. }
  96. jsonString, _ := json.Marshal(results)
  97. sendJSONResponse(w, string(jsonString))
  98. return
  99. }