| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- // owut_flow.go - ciclo owut completo (#761): lista de versiones disponibles,
- // instalación del paquete y comando de upgrade desatendido.
- //
- // `owut upgrade` es no interactivo por diseño (verificado sobre el fuente
- // ucode de owut 2026.07): aborta solo si los checks fallan, hace warn sin
- // preguntar cuando hay default packages perdidos, y si no hay cambios termina
- // con exit 0 sin tocar nada (idempotencia nativa). Sin -V reconstruye la
- // versión instalada con los paquetes al día; con -V VERSION salta de versión
- // conservando los paquetes instalados (attended sysupgrade vía ASU).
- package firmware
- import (
- "fmt"
- "regexp"
- "strconv"
- "strings"
- "time"
- )
- const (
- owutVersionsTimeout = 60 * time.Second
- owutInstallTimeout = 240 * time.Second
- // OwutUpgradeTimeout: build ASU + descarga + verificación + flash. El
- // build remoto puede tardar minutos; el flash final mata dropbear y la
- // sesión SSH muere con el router reiniciándose (resultado esperado).
- OwutUpgradeTimeout = 15 * time.Minute
- )
- // OwutVersion es una versión destino que ASU puede construir para el router.
- type OwutVersion struct {
- Version string `json:"version"`
- Branch string `json:"branch"`
- Latest bool `json:"latest,omitempty"`
- Newer bool `json:"newer,omitempty"`
- }
- var (
- owutBranchRe = regexp.MustCompile(`^\s{2}(\S+)\s+release branch\s*$`)
- owutVerRe = regexp.MustCompile(`^\s{4}(\S+)(\s+\(latest\))?\s*$`)
- )
- // ListOwutVersions parsea `owut versions` y devuelve las versiones estables
- // (se excluyen -rc* y *-SNAPSHOT) con la marca Latest y Newer respecto a la
- // versión instalada actual (vacía = todas sin comparar).
- func ListOwutVersions(runner Runner, host, current string) ([]OwutVersion, error) {
- if runner == nil || host == "" {
- return nil, fmt.Errorf("firmware: empty runner or host")
- }
- out, err := runner.Run(host, "owut versions 2>&1", owutVersionsTimeout)
- if err != nil {
- return nil, err
- }
- return ParseOwutVersions(out, current), nil
- }
- // ParseOwutVersions interpreta la salida de `owut versions` (función pura,
- // testeada): ramas con dos espacios y versiones con cuatro.
- func ParseOwutVersions(out, current string) []OwutVersion {
- var versions []OwutVersion
- branch := ""
- for _, line := range strings.Split(out, "\n") {
- if m := owutBranchRe.FindStringSubmatch(line); m != nil {
- branch = m[1]
- continue
- }
- if m := owutVerRe.FindStringSubmatch(line); m != nil {
- v := m[1]
- if !stableVersion(v) {
- continue
- }
- versions = append(versions, OwutVersion{
- Version: v,
- Branch: branch,
- Latest: m[2] != "",
- Newer: current != "" && VersionCmp(v, current) > 0,
- })
- }
- }
- return versions
- }
- // stableVersion filtra release candidates y snapshots del desplegable
- // (incluido el "SNAPSHOT" literal con que cierra cada rama del server ASU).
- func stableVersion(v string) bool {
- if v == "SNAPSHOT" || !strings.ContainsFunc(v, isDigitRune) {
- return false
- }
- return !strings.Contains(v, "-rc") && !strings.HasSuffix(v, "-SNAPSHOT")
- }
- func isDigitRune(r rune) bool { return r >= '0' && r <= '9' }
- // VersionCmp compara dos versiones OpenWrt ("25.12.5") numéricamente por
- // componentes; devuelve -1/0/1. Componentes no numéricos valen 0.
- func VersionCmp(a, b string) int {
- as := strings.Split(a, ".")
- bs := strings.Split(b, ".")
- n := len(as)
- if len(bs) > n {
- n = len(bs)
- }
- for i := 0; i < n; i++ {
- av, bv := 0, 0
- if i < len(as) {
- av, _ = strconv.Atoi(as[i])
- }
- if i < len(bs) {
- bv, _ = strconv.Atoi(bs[i])
- }
- if av != bv {
- if av < bv {
- return -1
- }
- return 1
- }
- }
- return 0
- }
- // MajorJump informa de si pasar de current a target cambia de rama mayor
- // ("23.05.5" -> "24.10.0" es salto mayor; "23.05.5" -> "23.05.7" no).
- func MajorJump(current, target string) bool {
- if current == "" || target == "" {
- return false
- }
- cs := strings.Split(current, ".")
- ts := strings.Split(target, ".")
- if len(cs) == 0 || len(ts) == 0 {
- return false
- }
- return cs[0] != ts[0]
- }
- // owutInstallCmd detecta el gestor de paquetes del router e instala owut con
- // los repositorios PROPIOS del router (los firmware vanilla lo empaquetan).
- // En releases sin paquete owut (23.05-) el comando falla con el error del
- // gestor; en firmware de fabricante (GL.iNet) el flujo va bloqueado antes
- // (cada vendor gestiona sus actualizaciones en su propio panel).
- const owutInstallCmd = `if command -v apk >/dev/null 2>&1; then ` +
- `apk update >/dev/null 2>&1; ` +
- `apk add owut; ` +
- `elif command -v opkg >/dev/null 2>&1; then opkg update && opkg install owut; ` +
- `else printf '__no_pm__\n'; fi 2>&1; printf '\n__owut_exit__=%d\n' $?`
- // InstallOwut instala el paquete owut en el router con el gestor disponible
- // (con reintento vía repo oficial para firmware de vendor). Devuelve la
- // salida del gestor; error si no hay gestor, falla la instalación o falla el
- // transporte SSH.
- func InstallOwut(runner Runner, host string) (string, error) {
- if runner == nil || host == "" {
- return "", fmt.Errorf("firmware: empty runner or host")
- }
- out, err := runner.Run(host, owutInstallCmd, owutInstallTimeout)
- if err != nil {
- return out, err
- }
- exit, raw := extractExitCode(out)
- if strings.Contains(raw, "__no_pm__") {
- return raw, fmt.Errorf("firmware: the router has neither apk nor opkg")
- }
- if exit != 0 {
- tail := raw
- if len(tail) > 300 {
- tail = "..." + tail[len(tail)-300:]
- }
- return raw, fmt.Errorf("owut install failed (exit %d): %s", exit, strings.TrimSpace(tail))
- }
- return raw, nil
- }
- // owutRemoveArg decide si el comando lleva -r. owut declara -r como opción
- // exclusiva (action "storex") y ademas carga `attendedsysupgrade.owut.remove`
- // como si fuera un argumento (argparse.add_config): si la lista ya esta en la
- // config UCI, pasar -r repite la opcion y owut aborta con "may only be used
- // once per invocation". Por eso solo se emite el flag cuando la lista NO esta
- // en UCI (#1978).
- func owutRemoveArg(removePkgs []string, removeInUCI bool) string {
- if removeInUCI {
- return ""
- }
- if pkgs := sanitizeRemovePkgs(removePkgs); pkgs != "" {
- return " -r " + shQuote(pkgs)
- }
- return ""
- }
- // OwutCmd describe el comando owut que se quiere construir.
- type OwutCmd struct {
- // Target es la version destino ("25.12.6"). Vacia o igual a Current
- // reconstruye la instalada con los paquetes al dia.
- Target string
- // Current es la version instalada segun el board info.
- Current string
- // RemovePkgs son los paquetes fuera de los feeds que ASU debe excluir.
- RemovePkgs []string
- // RemoveInUCI indica que RemovePkgs ya vive en attendedsysupgrade.owut.remove,
- // donde owut la lee sola. En ese caso no se emite -r (#1978).
- RemoveInUCI bool
- // Force acepta los avisos que owut no deja pasar solos, como los
- // downgrades de paquetes de un salto de version mayor (#1978).
- Force bool
- }
- // OwutUpgradeCmd construye el comando de upgrade desatendido.
- func OwutUpgradeCmd(o OwutCmd) string {
- cmd := "owut upgrade -q"
- if o.Target != "" && o.Current != "" && o.Target != o.Current {
- cmd += " -V " + shQuote(o.Target)
- }
- cmd += owutRemoveArg(o.RemovePkgs, o.RemoveInUCI)
- if o.Force {
- cmd += " --force"
- }
- return cmd
- }
- // pkgNameRe: nombre de paquete apk/opkg sano (defensa contra inyección en
- // el comando SSH; el shQuote ya protege, esto limita el abuso).
- var pkgNameRe = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9._+-]*$`)
- // sanitizeRemovePkgs filtra y une la lista de exclusiones (máx 20).
- func sanitizeRemovePkgs(list []string) string {
- var keep []string
- for _, p := range list {
- p = strings.TrimSpace(p)
- if p != "" && pkgNameRe.MatchString(p) && len(keep) < 20 {
- keep = append(keep, p)
- }
- }
- return strings.Join(keep, " ")
- }
- // RunOwutUpgrade ejecuta el comando de upgrade con el wrapper de exit code y
- // devuelve (exit, salida, error de transporte). Un error de transporte al
- // final del flujo es NORMAL: el sysupgrade reinicia el router y la sesión SSH
- // muere sin llegar a escribir la línea de exit (el caller lo interpreta).
- func RunOwutUpgrade(runner Runner, host, cmd string) (int, string, error) {
- wrapped := cmd + ` 2>&1; printf '\n__owut_exit__=%d\n' $?`
- out, err := runner.Run(host, wrapped, OwutUpgradeTimeout)
- exit, raw := extractExitCode(out)
- return exit, raw, err
- }
- // owutDownloadTimeout: build remoto ASU + descarga de la imagen al router.
- // `owut download` NO flashea: termina por sí solo y la sesión SSH sobrevive.
- const owutDownloadTimeout = 10 * time.Minute
- // OwutDownloadCmd construye `owut download` (paso intermedio del flujo por
- // pasos de #1414): construye la imagen en ASU y la descarga al router SIN
- // flashear, para que el admin la revise antes de `owut install`. Sin -q a
- // proposito: la salida completa es la revision.
- func OwutDownloadCmd(o OwutCmd) string {
- cmd := "owut download"
- if o.Target != "" && o.Current != "" && o.Target != o.Current {
- cmd += " -V " + shQuote(o.Target)
- }
- cmd += owutRemoveArg(o.RemovePkgs, o.RemoveInUCI)
- if o.Force {
- cmd += " --force"
- }
- return cmd
- }
- // RunOwutDownload ejecuta `owut download` y devuelve salida + exit. El comando
- // termina normalmente (no flashea), así que la sesión SSH NO muere: un exit
- // distinto de 0 o un error de sesión es fallo real.
- func RunOwutDownload(runner Runner, host, cmd string) (int, string, error) {
- wrapped := cmd + ` 2>&1; printf '\n__owut_exit__=%d\n' $?`
- out, err := runner.Run(host, wrapped, owutDownloadTimeout)
- exit, raw := extractExitCode(out)
- return exit, raw, err
- }
- // OwutInstallCmd construye `owut install`: flashea la imagen que `owut
- // download` dejó en el router y reinicia (la sesión SSH muere como en el
- // upgrade; el resultado se confirma por el board info tras el reboot).
- func OwutInstallCmd() string { return "owut install" }
- // owutImageCheckCmd: `owut download` deja la imagen en /tmp/firmware.bin
- // (su ruta por defecto para download/verify/install) y `owut install` la
- // espera ahí. Comprobarla antes de lanzar el flash evita el error crudo de
- // sysupgrade cuando no hay nada descargado (#1769).
- const owutImageCheckCmd = "test -s /tmp/firmware.bin"
- // HasOwutDownloadedImage informa de si el router tiene una imagen descargada
- // lista para `owut install`. Tolerante: runner o host vacíos → false. Un
- // fallo de transporte también cuenta como "no hay imagen": sin confirmación
- // no se flashea.
- func HasOwutDownloadedImage(runner Runner, host string) bool {
- if runner == nil || host == "" {
- return false
- }
- _, err := runner.Run(host, owutImageCheckCmd, owutDetectTimeout)
- return err == nil
- }
- // Platform es la foto de plataforma de un router: si tiene owut y si lleva
- // firmware de fabricante. Vendor != "" significa firmware compilado por el
- // vendor con su propia gestión de actualizaciones (GL.iNet): NetPulse NO
- // ofrece upgrades ASU ahí, el vendor ya resuelve los suyos en su panel.
- type Platform struct {
- Owut bool
- Vendor string // "" | "gl-inet"
- }
- // platformDetectCmd: una sola ida SSH resuelve ambas señales. La marca de
- // vendor son los repositorios propios (fw.gl-inet.com en apk u opkg): el
- // /etc/openwrt_release de los GL op25 es idéntico al vanilla y NO sirve.
- // OJO: busybox grep sale con 2 si algún fichero listado no existe (aunque
- // haya matches), así que el match se decide por salida (grep -q .), no por
- // exit code del primer grep.
- const platformDetectCmd = `command -v owut >/dev/null 2>&1 && printf '__owut__\n'; ` +
- `grep -hs fw.gl-inet.com /etc/apk/repositories /etc/apk/repositories.d/* /etc/opkg/distfeeds.conf 2>/dev/null | grep -q . && printf '__gl__\n'; true`
- // DetectPlatform sondea owut y vendor del router (tolerante: runner/host
- // vacíos → Platform vacía).
- func DetectPlatform(runner Runner, host string) Platform {
- if runner == nil || host == "" {
- return Platform{}
- }
- out, _ := runner.Run(host, platformDetectCmd, owutDetectTimeout)
- return ParsePlatform(out)
- }
- // ParsePlatform interpreta las marcas de platformDetectCmd (función pura).
- func ParsePlatform(out string) Platform {
- p := Platform{}
- if strings.Contains(out, "__owut__") {
- p.Owut = true
- }
- if strings.Contains(out, "__gl__") {
- p.Vendor = "gl-inet"
- }
- return p
- }
- // stackPreserveCmd garantiza de forma idempotente que los ficheros del
- // stack propio presente en el router (netgrip y/o agente NetPulse) estén en
- // /etc/sysupgrade.conf: ASU no puede incluir paquetes sin feed en la imagen,
- // pero los ficheros listados SOBREVIVEN al flash (el servicio arranca en el
- // primer boot; el registro apk se recupera con una reinstalación posterior).
- const stackPreserveCmd = `P=/etc/sysupgrade.conf; touch $P; ensure() { grep -qxF "$1" $P || echo "$1" >> $P; }; ` +
- `if [ -f /usr/sbin/netgrip ]; then ensure /usr/sbin/netgrip; ensure /etc/init.d/netgrip; ` +
- `ensure /usr/libexec/netgrip-restore-rules; ensure /etc/netgrip/; ` +
- `for f in /etc/rc.d/*netgrip*; do [ -e "$f" ] && ensure "$f"; done; fi; ` +
- `if [ -f /usr/sbin/netpulse-agent ]; then ensure /usr/sbin/netpulse-agent; ensure /etc/netpulse-agent.env; ` +
- `ensure /etc/init.d/netpulse-agent; ` +
- `for f in /etc/rc.d/*netpulse-agent*; do [ -e "$f" ] && ensure "$f"; done; fi; true`
- // EnsureStackPreserved ejecuta la preservación en el router (tolerante: un
- // runner nil es no-op; devuelve el error de transporte si falla y el caller
- // decide si continúa).
- func EnsureStackPreserved(runner Runner, host string) error {
- if runner == nil || host == "" {
- return nil
- }
- _, err := runner.Run(host, stackPreserveCmd, owutDetectTimeout)
- return err
- }
- // OpenWrtVersionRe: versión destino válida para -V ("25.12.5", "24.10.2").
- // Evita que textos libres guardados como target disparen upgrades sin sentido.
- var OpenWrtVersionRe = regexp.MustCompile(`^\d+\.\d+(\.\d+)?(-[a-zA-Z0-9.]+)?$`)
- // ValidOpenWrtVersion informa de si target parece una versión OpenWrt.
- func ValidOpenWrtVersion(v string) bool {
- return OpenWrtVersionRe.MatchString(strings.TrimSpace(v))
- }
- // shQuote entrecomilla un valor para shell POSIX (single-quote escaping).
- func shQuote(v string) string {
- return "'" + strings.ReplaceAll(v, "'", `'\''`) + "'"
- }
|