install.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package install
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "log"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "github.com/cnlh/nps/lib/common"
  12. )
  13. func InstallNps() {
  14. unit := `[Unit]
  15. Description=nps - convenient proxy server
  16. Documentation=https://github.com/cnlh/nps/
  17. After=network-online.target remote-fs.target nss-lookup.target
  18. Wants=network-online.target`
  19. service := `[Service]
  20. Type=simple
  21. KillMode=process
  22. Restart=always
  23. RestartSec=15s
  24. StandardOutput=append:/var/log/nps/nps.log
  25. ExecStartPre=/bin/echo 'Starting nps'
  26. ExecStopPost=/bin/echo 'Stopping nps'
  27. ExecStart=`
  28. install := `[Install]
  29. WantedBy=multi-user.target`
  30. path := common.GetInstallPath()
  31. if common.FileExists(path) {
  32. log.Fatalf("the path %s has exist, does not support install", path)
  33. }
  34. MkidrDirAll(path, "conf", "web/static", "web/views")
  35. //复制文件到对应目录
  36. if err := CopyDir(filepath.Join(common.GetAppPath(), "web", "views"), filepath.Join(path, "web", "views")); err != nil {
  37. log.Fatalln(err)
  38. }
  39. if err := CopyDir(filepath.Join(common.GetAppPath(), "web", "static"), filepath.Join(path, "web", "static")); err != nil {
  40. log.Fatalln(err)
  41. }
  42. if err := CopyDir(filepath.Join(common.GetAppPath(), "conf"), filepath.Join(path, "conf")); err != nil {
  43. log.Fatalln(err)
  44. }
  45. if !common.IsWindows() {
  46. if _, err := copyFile(filepath.Join(common.GetAppPath(), "nps"), "/usr/bin/nps"); err != nil {
  47. if _, err := copyFile(filepath.Join(common.GetAppPath(), "nps"), "/usr/local/bin/nps"); err != nil {
  48. log.Fatalln(err)
  49. } else {
  50. os.Chmod("/usr/local/bin/nps", 0755)
  51. service += "/usr/local/bin/nps"
  52. log.Println("Executable files have been copied to", "/usr/local/bin/nps")
  53. }
  54. } else {
  55. os.Chmod("/usr/bin/nps", 0755)
  56. service += "/usr/bin/nps"
  57. log.Println("Executable files have been copied to", "/usr/bin/nps")
  58. }
  59. systemd := unit + "\n\n" + service + "\n\n" + install
  60. _ = os.Remove("/usr/lib/systemd/system/nps.service")
  61. err := ioutil.WriteFile("/usr/lib/systemd/system/nps.service", []byte(systemd), 0644)
  62. if err != nil {
  63. log.Println("Write systemd service err ", err)
  64. }
  65. _ = os.Mkdir("/var/log/nps", 644)
  66. }
  67. log.Println("install ok!")
  68. log.Println("Static files and configuration files in the current directory will be useless")
  69. log.Println("The new configuration file is located in", path, "you can edit them")
  70. if !common.IsWindows() {
  71. log.Println(`You can start with:
  72. sudo systemctl enable|disable|start|stop|restart|status nps
  73. or:
  74. nps test|start|stop|restart|status
  75. anywhere!`)
  76. } else {
  77. log.Println(`You can copy executable files to any directory and start working with:
  78. nps.exe test|start|stop|restart|status
  79. now!`)
  80. }
  81. }
  82. func MkidrDirAll(path string, v ...string) {
  83. for _, item := range v {
  84. if err := os.MkdirAll(filepath.Join(path, item), 0755); err != nil {
  85. log.Fatalf("Failed to create directory %s error:%s", path, err.Error())
  86. }
  87. }
  88. }
  89. func CopyDir(srcPath string, destPath string) error {
  90. //检测目录正确性
  91. if srcInfo, err := os.Stat(srcPath); err != nil {
  92. fmt.Println(err.Error())
  93. return err
  94. } else {
  95. if !srcInfo.IsDir() {
  96. e := errors.New("SrcPath is not the right directory!")
  97. return e
  98. }
  99. }
  100. if destInfo, err := os.Stat(destPath); err != nil {
  101. return err
  102. } else {
  103. if !destInfo.IsDir() {
  104. e := errors.New("DestInfo is not the right directory!")
  105. return e
  106. }
  107. }
  108. err := filepath.Walk(srcPath, func(path string, f os.FileInfo, err error) error {
  109. if f == nil {
  110. return err
  111. }
  112. if !f.IsDir() {
  113. destNewPath := strings.Replace(path, srcPath, destPath, -1)
  114. log.Println("copy file ::" + path + " to " + destNewPath)
  115. copyFile(path, destNewPath)
  116. }
  117. return nil
  118. })
  119. return err
  120. }
  121. //生成目录并拷贝文件
  122. func copyFile(src, dest string) (w int64, err error) {
  123. srcFile, err := os.Open(src)
  124. if err != nil {
  125. return
  126. }
  127. defer srcFile.Close()
  128. //分割path目录
  129. destSplitPathDirs := strings.Split(dest, string(filepath.Separator))
  130. //检测时候存在目录
  131. destSplitPath := ""
  132. for index, dir := range destSplitPathDirs {
  133. if index < len(destSplitPathDirs)-1 {
  134. destSplitPath = destSplitPath + dir + string(filepath.Separator)
  135. b, _ := pathExists(destSplitPath)
  136. if b == false {
  137. log.Println("mkdir:" + destSplitPath)
  138. //创建目录
  139. err := os.Mkdir(destSplitPath, os.ModePerm)
  140. if err != nil {
  141. log.Fatalln(err)
  142. }
  143. }
  144. }
  145. }
  146. dstFile, err := os.Create(dest)
  147. if err != nil {
  148. return
  149. }
  150. defer dstFile.Close()
  151. return io.Copy(dstFile, srcFile)
  152. }
  153. //检测文件夹路径时候存在
  154. func pathExists(path string) (bool, error) {
  155. _, err := os.Stat(path)
  156. if err == nil {
  157. return true, nil
  158. }
  159. if os.IsNotExist(err) {
  160. return false, nil
  161. }
  162. return false, err
  163. }