ipk.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package openwrtpkg
  2. import (
  3. "archive/tar"
  4. "bytes"
  5. "compress/gzip"
  6. "embed"
  7. "fmt"
  8. "time"
  9. )
  10. // files: copia de deploy/openwrt/netpulse-agent/files. go:embed no puede salir
  11. // del modulo, asi que el paquete lleva su propia copia y un test comprueba que
  12. // sigue identica a la del repo (#1476).
  13. //
  14. //go:embed files/*
  15. var files embed.FS
  16. // PkgName es el nombre del paquete OpenWrt y de la seccion UCI que crea.
  17. const PkgName = "netpulse-agent"
  18. // pkgEpoch fija los mtime del tar para que dos generaciones del mismo binario
  19. // produzcan exactamente los mismos bytes.
  20. var pkgEpoch = time.Unix(1700000000, 0).UTC()
  21. // OpenWrtArch traduce el GOARCH del agente al nombre de arquitectura que usan
  22. // los repos de OpenWrt. Devuelve "" para una arquitectura no soportada.
  23. func OpenWrtArch(goarch string) string {
  24. switch goarch {
  25. case "amd64":
  26. return "x86_64"
  27. case "arm64":
  28. return "aarch64_cortex-a53"
  29. case "arm":
  30. return "arm_cortex-a7_neon-vfpv4"
  31. case "mipsle":
  32. return "mipsel_24kc"
  33. case "mips":
  34. return "mips_24kc"
  35. }
  36. return ""
  37. }
  38. // BuildIPK construye el paquete .ipk del agente para una arquitectura, a
  39. // partir del binario ya compilado.
  40. //
  41. // Desde OpenWrt 24.10 el .ipk NO es un archivo ar: es un tar.gz exterior que
  42. // contiene ./debian-binary, ./control.tar.gz y ./data.tar.gz, comprobado
  43. // contra un paquete real de downloads.openwrt.org. Los tres miembros llevan
  44. // prefijo "./" y el control declara /etc/config/netpulse-agent como conffile,
  45. // que es lo que hace que la config sobreviva a un sysupgrade.
  46. func BuildIPK(goarch, version string, binary []byte) ([]byte, error) {
  47. arch := OpenWrtArch(goarch)
  48. if arch == "" {
  49. return nil, fmt.Errorf("openwrt pkg: unsupported architecture %q", goarch)
  50. }
  51. if len(binary) == 0 {
  52. return nil, fmt.Errorf("openwrt pkg: empty binary")
  53. }
  54. if version == "" {
  55. version = "0.0.0"
  56. }
  57. initScript, err := files.ReadFile("files/netpulse-agent.init")
  58. if err != nil {
  59. return nil, err
  60. }
  61. configFile, err := files.ReadFile("files/netpulse-agent.config")
  62. if err != nil {
  63. return nil, err
  64. }
  65. defaults, err := files.ReadFile("files/netpulse-agent.defaults")
  66. if err != nil {
  67. return nil, err
  68. }
  69. control, err := tarGz([]tarEntry{
  70. {name: "./control", mode: 0o644, body: controlFile(version, arch)},
  71. {name: "./conffiles", mode: 0o644, body: []byte("/etc/config/netpulse-agent\n")},
  72. {name: "./postinst", mode: 0o755, body: postinst},
  73. {name: "./prerm", mode: 0o755, body: prerm},
  74. })
  75. if err != nil {
  76. return nil, err
  77. }
  78. data, err := tarGz([]tarEntry{
  79. {name: "./usr/sbin/netpulse-agent", mode: 0o755, body: binary},
  80. {name: "./etc/init.d/netpulse-agent", mode: 0o755, body: initScript},
  81. {name: "./etc/config/netpulse-agent", mode: 0o644, body: configFile},
  82. {name: "./etc/uci-defaults/90-netpulse-agent", mode: 0o755, body: defaults},
  83. })
  84. if err != nil {
  85. return nil, err
  86. }
  87. return tarGzNoDir([]tarEntry{
  88. {name: "./debian-binary", mode: 0o644, body: []byte("2.0\n")},
  89. {name: "./data.tar.gz", mode: 0o644, body: data},
  90. {name: "./control.tar.gz", mode: 0o644, body: control},
  91. })
  92. }
  93. // controlFile es el CONTROL/control del paquete. Depends replica el Makefile
  94. // del repo: el agente sondea las radios con iw.
  95. func controlFile(version, arch string) []byte {
  96. return []byte(fmt.Sprintf(`Package: %s
  97. Version: %s
  98. Depends: libc, iw
  99. License: AGPL-3.0-only
  100. Section: utils
  101. Architecture: %s
  102. Maintainer: Nacho <netpulse@cloudless.club>
  103. Description: Native OpenWrt agent for NetPulse network monitor.
  104. `, PkgName, version, arch))
  105. }
  106. // postinst arranca el servicio si ya estaba habilitado y aplica los
  107. // uci-defaults, que migran una instalacion manual previa al formato UCI.
  108. var postinst = []byte(`#!/bin/sh
  109. [ -n "${IPKG_INSTROOT}" ] && exit 0
  110. if [ -x /etc/init.d/netpulse-agent ]; then
  111. /etc/init.d/netpulse-agent enabled && /etc/init.d/netpulse-agent restart
  112. fi
  113. [ -x /etc/uci-defaults/90-netpulse-agent ] && sh /etc/uci-defaults/90-netpulse-agent
  114. exit 0
  115. `)
  116. // prerm para el servicio y retira el watchdog cron de versiones anteriores.
  117. var prerm = []byte(`#!/bin/sh
  118. [ -n "${IPKG_INSTROOT}" ] && exit 0
  119. if [ -x /etc/init.d/netpulse-agent ]; then
  120. /etc/init.d/netpulse-agent stop 2>/dev/null
  121. /etc/init.d/netpulse-agent disable 2>/dev/null
  122. fi
  123. crontab -l 2>/dev/null | grep -v netpulse-watchdog | crontab - 2>/dev/null
  124. exit 0
  125. `)
  126. type tarEntry struct {
  127. name string
  128. mode int64
  129. body []byte
  130. dir bool
  131. }
  132. // tarGz empaqueta entradas con la cabecera de directorio "./" delante, como
  133. // hacen ipkg-build y apk en los paquetes reales de OpenWrt.
  134. func tarGz(entries []tarEntry) ([]byte, error) {
  135. return tarGzNoDir(append([]tarEntry{{name: "./", mode: 0o755, dir: true}}, entries...))
  136. }
  137. // tarGzNoDir empaqueta sin cabecera de directorio (es el tar exterior del ipk,
  138. // que solo lleva los tres miembros).
  139. func tarGzNoDir(entries []tarEntry) ([]byte, error) {
  140. var buf bytes.Buffer
  141. gz := gzip.NewWriter(&buf)
  142. gz.Header.ModTime = pkgEpoch
  143. tw := tar.NewWriter(gz)
  144. for _, e := range entries {
  145. hdr := &tar.Header{
  146. Name: e.name,
  147. Mode: e.mode,
  148. ModTime: pkgEpoch,
  149. Uid: 0,
  150. Gid: 0,
  151. Format: tar.FormatGNU,
  152. }
  153. if e.dir {
  154. hdr.Typeflag = tar.TypeDir
  155. } else {
  156. hdr.Typeflag = tar.TypeReg
  157. hdr.Size = int64(len(e.body))
  158. }
  159. if err := tw.WriteHeader(hdr); err != nil {
  160. return nil, err
  161. }
  162. if !e.dir && len(e.body) > 0 {
  163. if _, err := tw.Write(e.body); err != nil {
  164. return nil, err
  165. }
  166. }
  167. }
  168. if err := tw.Close(); err != nil {
  169. return nil, err
  170. }
  171. if err := gz.Close(); err != nil {
  172. return nil, err
  173. }
  174. return buf.Bytes(), nil
  175. }