file.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package nginx_controller
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/astaxie/beego/logs"
  7. "github.com/astaxie/beego/orm"
  8. config2 "nginx-ui/server/config"
  9. "nginx-ui/server/models"
  10. nginx2 "nginx-ui/server/nginx"
  11. "nginx-ui/server/utils"
  12. "os"
  13. "strings"
  14. "time"
  15. )
  16. type FileController struct {
  17. BaseController
  18. }
  19. func getRootDir() (string, error) {
  20. root := fmt.Sprintf("%s/files", config2.GetDataDir())
  21. if exist := utils.IsExist(root); exist != true {
  22. err := os.MkdirAll(root, 0777)
  23. if err != nil {
  24. return "", err
  25. }
  26. }
  27. return root, nil
  28. }
  29. // Get getAll
  30. func (c *FileController) Get() {
  31. fileName := c.GetString("filename")
  32. logs.Info("get file: {}", fileName)
  33. root, err := getRootDir()
  34. if err != nil {
  35. c.Ctx.Output.SetStatus(404)
  36. return
  37. }
  38. fromFile := fmt.Sprintf("%s/%s", root, fileName)
  39. c.Ctx.Output.Download(fromFile, fileName)
  40. }
  41. // Post file upload
  42. // POST /file
  43. func (c *FileController) Post() {
  44. f, header, err := c.GetFile("file")
  45. if err != nil {
  46. c.Ctx.Output.SetStatus(500)
  47. c.ErrorJson(err)
  48. return
  49. }
  50. var req models.FileReq
  51. err = c.ParseForm(&req)
  52. if err != nil {
  53. c.Ctx.Output.SetStatus(500)
  54. c.ErrorJson(err)
  55. return
  56. }
  57. defer f.Close()
  58. root, err := getRootDir()
  59. if err != nil {
  60. c.ErrorJson(err)
  61. return
  62. }
  63. if strings.HasPrefix(req.Path, "/") {
  64. req.Path = req.Path[1:len(req.Path)]
  65. }
  66. root = fmt.Sprintf("%s/%s", root, req.Key)
  67. index := strings.LastIndex(req.Path, "/")
  68. if index > 0 {
  69. subDir := req.Path[0:index]
  70. root = fmt.Sprintf("%s/%s", root, subDir)
  71. }
  72. if !utils.IsExist(root) {
  73. err = os.MkdirAll(root, 0777)
  74. }
  75. if err != nil {
  76. c.Ctx.Output.SetStatus(500)
  77. c.ErrorJson(err)
  78. return
  79. }
  80. toFile := fmt.Sprintf("%s/%s", root, header.Filename)
  81. err = c.SaveToFile("file", toFile)
  82. if err != nil {
  83. c.Ctx.Output.SetStatus(500)
  84. c.ErrorJson(err)
  85. return
  86. }
  87. c.SetData(toFile).Json()
  88. }
  89. // Deploy 部署到服务器
  90. // POST /nginx/:id/file/deploy
  91. func (c *FileController) Deploy() {
  92. nginx, err := c.CheckNginxPermission()
  93. if err != nil {
  94. return
  95. }
  96. logs.Info("deploy ", nginx.Id)
  97. var req models.DeployReq
  98. err = json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  99. if err != nil {
  100. logs.Error(err, string(c.Ctx.Input.RequestBody))
  101. c.ErrorJson(err)
  102. return
  103. }
  104. if err != nil {
  105. c.ErrorJson(err)
  106. return
  107. }
  108. err = HandleDeploy(req)
  109. if err != nil {
  110. c.ErrorJson(err)
  111. return
  112. }
  113. c.Json()
  114. }
  115. func HandleDeploy(req models.DeployReq) error {
  116. root, err := getRootDir()
  117. if err != nil {
  118. return err
  119. }
  120. root = fmt.Sprintf("%s/%s", root, req.Key)
  121. if !utils.IsExist(root) {
  122. logs.Warn("dir not exist: ", root)
  123. return errors.New("未上传文件或者文件已被删除!")
  124. }
  125. o := orm.NewOrm()
  126. nginx := models.Nginx{
  127. Id: req.NginxId,
  128. }
  129. err = o.Read(&nginx)
  130. if err != nil {
  131. return err
  132. }
  133. ins := nginx2.GetInstance(&nginx)
  134. dirs := ins.CheckDirs()
  135. if nginx.IsLocal {
  136. cmd := fmt.Sprintf("if [ ! -d %s ];then mkdir -p %s;fi && cp -r %s/* %s", req.Dir, req.Dir, root, req.Dir)
  137. _, err := ins.Run(cmd)
  138. return err
  139. }
  140. tarPath := fmt.Sprintf("%s.tar.gz", root)
  141. if !utils.IsExist(tarPath) {
  142. err = utils.TarXz(tarPath, root)
  143. if err != nil {
  144. return err
  145. }
  146. }
  147. timeNow := time.Now().Format("20060102150105")
  148. dst := fmt.Sprintf("%s/%s_%s.tar.gz", dirs.BackupDir, req.Key, timeNow)
  149. err = ins.SendFile(tarPath, dst)
  150. if err != nil {
  151. return err
  152. }
  153. cmd := fmt.Sprintf("if [ ! -d %s ];then mkdir -p %s;fi && tar -zxvf %s -C %s", req.Dir, req.Dir, dst, req.Dir)
  154. if req.Clear {
  155. cmd = fmt.Sprintf("rm -rf %s;%s", req.Dir, cmd)
  156. }
  157. _, err = ins.Run(cmd)
  158. if err != nil {
  159. return err
  160. }
  161. if len(req.Cmd) > 0 {
  162. _, err = ins.Run(req.Cmd)
  163. }
  164. if err != nil {
  165. return errors.New(fmt.Sprintf("部署已完成,命令执行异常:%s", err.Error()))
  166. }
  167. return nil
  168. }
  169. // Delete del certs
  170. func (c *FileController) Delete() {
  171. }