initd_openwrt.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package zdaemon
  2. import (
  3. "fmt"
  4. "git.swzry.com/zry/pathutils"
  5. "html/template"
  6. "os"
  7. "path"
  8. "strconv"
  9. )
  10. const tpl_initd_openwrt = `#!/bin/sh /etc/rc.common
  11. START={{ .startnum }}
  12. STOP={{ .stopnum }}
  13. cmd={{ .executable }}
  14. start() {
  15. $cmd start
  16. }
  17. stop() {
  18. $cmd stop
  19. }
  20. `
  21. func (zd *ZDaemon) install_openwrt() {
  22. if zd.daemonConfig.InstallMode != "free" {
  23. fmt.Println("Failed install: already installed.")
  24. return
  25. }
  26. initsp := path.Join("/etc/init.d/", zd.daemonName)
  27. pe, _ := pathutils.PathExists(initsp)
  28. if pe {
  29. err := os.Remove(initsp)
  30. if err != nil {
  31. fmt.Println("Failed install, old file exist and error in deleting old file: ", err.Error())
  32. return
  33. }
  34. }
  35. otpl := template.New(zd.daemonName)
  36. tpl, err := otpl.Parse(tpl_initd_openwrt)
  37. if err != nil {
  38. fmt.Println("Failed install: ", err.Error())
  39. return
  40. }
  41. fids, err := os.OpenFile(initsp, os.O_WRONLY|os.O_CREATE, 0777)
  42. if err != nil {
  43. fmt.Println("Failed install: ", err.Error())
  44. return
  45. }
  46. tdat := map[string]string{
  47. "startnum": strconv.Itoa(zd.startOrder),
  48. "stopnum": strconv.Itoa(zd.stopOrder),
  49. "executable": zd.executableFileName,
  50. }
  51. err = tpl.Execute(fids, tdat)
  52. if err != nil {
  53. fmt.Println("Failed install: ", err.Error())
  54. _ = fids.Close()
  55. return
  56. }
  57. _ = fids.Close()
  58. zd.daemonConfig.InstallMode = "openwrt"
  59. zd.writeStatus()
  60. fmt.Println("Installed.")
  61. }
  62. func (zd *ZDaemon) uninstall_openwrt() {
  63. initsp := path.Join("/etc/init.d/", zd.daemonName)
  64. pe, _ := pathutils.PathExists(initsp)
  65. if pe {
  66. err := os.Remove(initsp)
  67. if err != nil {
  68. fmt.Println("Failed uninstall: ", err.Error())
  69. return
  70. }
  71. }
  72. zd.daemonConfig.InstallMode = "free"
  73. zd.writeStatus()
  74. fmt.Println("Uninstalled.")
  75. }