feat(bash/infra): servicios systemd locales — 6 funciones atómicas + 1 pipeline
Nuevas primitivas para gestionar servicios systemd del sistema desde
el registry (antes solo había versiones remotas via SSH para deploy VPS):
bash/functions/infra/
systemd_local_install_unit — escribir unit en /etc/systemd/system + daemon-reload
systemd_local_enable — systemctl enable
systemd_local_start — systemctl start + MainPID
systemd_local_restart — systemctl restart + MainPID
systemd_local_status — ActiveState/SubState/pid/enabled + journal tail (no sudo)
systemd_local_uninstall — stop + disable + rm unit + daemon-reload (idempotente)
bash/functions/pipelines/
install_systemd_service — pipeline que compone las anteriores; args
--name --exec [--workdir --user --env KEY=VAL
--after --restart --type]
Requisito: sudo sin password para systemctl + escritura en /etc/systemd/system/.
En WSL: systemd=true en /etc/wsl.conf.
Registrado sqlite_api como servicio del sistema con este pipeline, queda
vivo al arrancar WSL. Dashboard ya no necesita arrancar la API manualmente.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
---
|
||||
name: systemd_local_enable
|
||||
kind: function
|
||||
lang: bash
|
||||
domain: infra
|
||||
version: "1.0.0"
|
||||
purity: impure
|
||||
signature: "systemd_local_enable(name: string) -> json"
|
||||
description: "Habilita un servicio systemd local con systemctl enable para que arranque automáticamente al boot. Requiere sudo."
|
||||
tags: [systemd, service, local, infra, enable]
|
||||
uses_functions: []
|
||||
uses_types: []
|
||||
returns: []
|
||||
returns_optional: false
|
||||
error_type: "error_go_core"
|
||||
imports: []
|
||||
params:
|
||||
- name: name
|
||||
desc: "nombre del servicio sin sufijo .service"
|
||||
output: "JSON {name, enabled:true}. Errores a stderr, exit 1."
|
||||
tested: false
|
||||
tests: []
|
||||
test_file_path: ""
|
||||
file_path: "bash/functions/infra/systemd_local_enable.sh"
|
||||
---
|
||||
|
||||
## Ejemplo
|
||||
|
||||
```bash
|
||||
source bash/functions/infra/systemd_local_enable.sh
|
||||
systemd_local_enable "sqlite_api"
|
||||
# {"name":"sqlite_api","enabled":true}
|
||||
```
|
||||
|
||||
## Notas
|
||||
|
||||
- El unit debe existir en `/etc/systemd/system/` (usar `systemd_local_install_unit` primero).
|
||||
- No arranca el servicio — solo lo habilita para el próximo boot. Usar `systemd_local_start` para lanzarlo ahora.
|
||||
@@ -0,0 +1,21 @@
|
||||
#!/usr/bin/env bash
|
||||
# systemd_local_enable — Habilita un servicio systemd local (arranque automático).
|
||||
set -euo pipefail
|
||||
|
||||
systemd_local_enable() {
|
||||
local name="$1"
|
||||
|
||||
if [[ -z "$name" ]]; then
|
||||
echo "systemd_local_enable: se requiere name" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
# systemctl enable imprime "Created symlink ..." en stdout — redirigir a stderr
|
||||
# para que $(systemd_local_enable ...) capture sólo el JSON final.
|
||||
if ! sudo systemctl enable "${name}.service" >&2; then
|
||||
echo "systemd_local_enable: enable falló para '$name'" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
printf '{"name":"%s","enabled":true}\n' "$name"
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
---
|
||||
name: systemd_local_install_unit
|
||||
kind: function
|
||||
lang: bash
|
||||
domain: infra
|
||||
version: "1.0.0"
|
||||
purity: impure
|
||||
signature: "systemd_local_install_unit(name: string, unit_content: string) -> json"
|
||||
description: "Instala un unit file de systemd en /etc/systemd/system/<name>.service y ejecuta daemon-reload. Requiere sudo sin password para install y systemctl. Sobrescribe si el unit ya existe."
|
||||
tags: [systemd, service, local, infra, wsl]
|
||||
uses_functions: []
|
||||
uses_types: []
|
||||
returns: []
|
||||
returns_optional: false
|
||||
error_type: "error_go_core"
|
||||
imports: []
|
||||
params:
|
||||
- name: name
|
||||
desc: "nombre del servicio sin sufijo (se añade .service automáticamente)"
|
||||
- name: unit_content
|
||||
desc: "contenido completo del archivo unit como texto (con secciones [Unit], [Service], [Install])"
|
||||
output: "JSON {name, path, installed:true}. Errores a stderr, exit 1."
|
||||
tested: false
|
||||
tests: []
|
||||
test_file_path: ""
|
||||
file_path: "bash/functions/infra/systemd_local_install_unit.sh"
|
||||
---
|
||||
|
||||
## Ejemplo
|
||||
|
||||
```bash
|
||||
source bash/functions/infra/systemd_local_install_unit.sh
|
||||
|
||||
unit=$(cat <<'EOF'
|
||||
[Unit]
|
||||
Description=my service
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
ExecStart=/usr/local/bin/my_service
|
||||
Restart=on-failure
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
)
|
||||
|
||||
systemd_local_install_unit "my_service" "$unit"
|
||||
# {"name":"my_service","path":"/etc/systemd/system/my_service.service","installed":true}
|
||||
```
|
||||
|
||||
## Notas
|
||||
|
||||
- Usa `install -m 0644 -o root -g root` para escribir el unit con permisos correctos.
|
||||
- Llama a `sudo systemctl daemon-reload` al final — imprescindible para que systemd vea el unit.
|
||||
- No hace `enable` ni `start` — esas son funciones separadas (principio de composabilidad).
|
||||
- En WSL requiere `systemd=true` en `/etc/wsl.conf`.
|
||||
@@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env bash
|
||||
# systemd_local_install_unit — Instala un unit file en /etc/systemd/system y recarga systemd.
|
||||
set -euo pipefail
|
||||
|
||||
systemd_local_install_unit() {
|
||||
local name="$1"
|
||||
local unit_content="$2"
|
||||
|
||||
if [[ -z "$name" || -z "$unit_content" ]]; then
|
||||
echo "systemd_local_install_unit: se requieren name y unit_content" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
local unit_path="/etc/systemd/system/${name}.service"
|
||||
local tmp
|
||||
tmp="$(mktemp)"
|
||||
printf '%s' "$unit_content" > "$tmp"
|
||||
|
||||
if ! sudo install -m 0644 -o root -g root "$tmp" "$unit_path"; then
|
||||
rm -f "$tmp"
|
||||
echo "systemd_local_install_unit: no se pudo instalar '$unit_path'" >&2
|
||||
return 1
|
||||
fi
|
||||
rm -f "$tmp"
|
||||
|
||||
if ! sudo systemctl daemon-reload; then
|
||||
echo "systemd_local_install_unit: daemon-reload falló" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
printf '{"name":"%s","path":"%s","installed":true}\n' "$name" "$unit_path"
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
---
|
||||
name: systemd_local_restart
|
||||
kind: function
|
||||
lang: bash
|
||||
domain: infra
|
||||
version: "1.0.0"
|
||||
purity: impure
|
||||
signature: "systemd_local_restart(name: string) -> json"
|
||||
description: "Reinicia un servicio systemd local con systemctl restart. Útil tras actualizar el binario o cambiar el unit. Requiere sudo."
|
||||
tags: [systemd, service, local, infra, restart]
|
||||
uses_functions: []
|
||||
uses_types: []
|
||||
returns: []
|
||||
returns_optional: false
|
||||
error_type: "error_go_core"
|
||||
imports: []
|
||||
params:
|
||||
- name: name
|
||||
desc: "nombre del servicio sin sufijo .service"
|
||||
output: "JSON {name, restarted:true, pid:int}. Errores a stderr, exit 1."
|
||||
tested: false
|
||||
tests: []
|
||||
test_file_path: ""
|
||||
file_path: "bash/functions/infra/systemd_local_restart.sh"
|
||||
---
|
||||
|
||||
## Ejemplo
|
||||
|
||||
```bash
|
||||
source bash/functions/infra/systemd_local_restart.sh
|
||||
systemd_local_restart "sqlite_api"
|
||||
# {"name":"sqlite_api","restarted":true,"pid":54321}
|
||||
```
|
||||
|
||||
## Notas
|
||||
|
||||
- Si modificaste el unit, primero ejecuta `sudo systemctl daemon-reload` (o llama a `systemd_local_install_unit` que ya lo hace).
|
||||
- Equivalente a stop+start pero systemd lo gestiona atómicamente.
|
||||
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env bash
|
||||
# systemd_local_restart — Reinicia un servicio systemd local.
|
||||
set -euo pipefail
|
||||
|
||||
systemd_local_restart() {
|
||||
local name="$1"
|
||||
|
||||
if [[ -z "$name" ]]; then
|
||||
echo "systemd_local_restart: se requiere name" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
if ! sudo systemctl restart "${name}.service" >&2; then
|
||||
echo "systemd_local_restart: restart falló para '$name'" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
local pid
|
||||
pid=$(systemctl show -p MainPID --value "${name}.service" 2>/dev/null || echo 0)
|
||||
|
||||
printf '{"name":"%s","restarted":true,"pid":%s}\n' "$name" "${pid:-0}"
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
---
|
||||
name: systemd_local_start
|
||||
kind: function
|
||||
lang: bash
|
||||
domain: infra
|
||||
version: "1.0.0"
|
||||
purity: impure
|
||||
signature: "systemd_local_start(name: string) -> json"
|
||||
description: "Arranca un servicio systemd local con systemctl start. Devuelve el MainPID asignado. Requiere sudo."
|
||||
tags: [systemd, service, local, infra, start]
|
||||
uses_functions: []
|
||||
uses_types: []
|
||||
returns: []
|
||||
returns_optional: false
|
||||
error_type: "error_go_core"
|
||||
imports: []
|
||||
params:
|
||||
- name: name
|
||||
desc: "nombre del servicio sin sufijo .service"
|
||||
output: "JSON {name, started:true, pid:int}. Errores a stderr, exit 1."
|
||||
tested: false
|
||||
tests: []
|
||||
test_file_path: ""
|
||||
file_path: "bash/functions/infra/systemd_local_start.sh"
|
||||
---
|
||||
|
||||
## Ejemplo
|
||||
|
||||
```bash
|
||||
source bash/functions/infra/systemd_local_start.sh
|
||||
systemd_local_start "sqlite_api"
|
||||
# {"name":"sqlite_api","started":true,"pid":12345}
|
||||
```
|
||||
|
||||
## Notas
|
||||
|
||||
- Si el servicio ya está corriendo, `systemctl start` es idempotente (no hace nada).
|
||||
- El PID devuelto es el `MainPID` según systemd. 0 si el arranque falló en silencio (usar `systemd_local_status` para diagnóstico).
|
||||
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env bash
|
||||
# systemd_local_start — Arranca un servicio systemd local.
|
||||
set -euo pipefail
|
||||
|
||||
systemd_local_start() {
|
||||
local name="$1"
|
||||
|
||||
if [[ -z "$name" ]]; then
|
||||
echo "systemd_local_start: se requiere name" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
if ! sudo systemctl start "${name}.service" >&2; then
|
||||
echo "systemd_local_start: start falló para '$name'" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
local pid
|
||||
pid=$(systemctl show -p MainPID --value "${name}.service" 2>/dev/null || echo 0)
|
||||
|
||||
printf '{"name":"%s","started":true,"pid":%s}\n' "$name" "${pid:-0}"
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
---
|
||||
name: systemd_local_status
|
||||
kind: function
|
||||
lang: bash
|
||||
domain: infra
|
||||
version: "1.0.0"
|
||||
purity: impure
|
||||
signature: "systemd_local_status(name: string, log_lines: int = 10) -> json"
|
||||
description: "Devuelve el estado de un servicio systemd local: active state, sub state, PID, enabled, y las N últimas líneas de journalctl. No requiere sudo."
|
||||
tags: [systemd, service, local, infra, status, journalctl]
|
||||
uses_functions: []
|
||||
uses_types: []
|
||||
returns: []
|
||||
returns_optional: false
|
||||
error_type: "error_go_core"
|
||||
imports: []
|
||||
params:
|
||||
- name: name
|
||||
desc: "nombre del servicio sin sufijo .service"
|
||||
- name: log_lines
|
||||
desc: "número de líneas de log a incluir en el JSON (default 10)"
|
||||
output: "JSON {name, active, sub, enabled, pid, logs:[...]}. Errores a stderr, exit 1."
|
||||
tested: false
|
||||
tests: []
|
||||
test_file_path: ""
|
||||
file_path: "bash/functions/infra/systemd_local_status.sh"
|
||||
---
|
||||
|
||||
## Ejemplo
|
||||
|
||||
```bash
|
||||
source bash/functions/infra/systemd_local_status.sh
|
||||
systemd_local_status "sqlite_api" 5
|
||||
# {"name":"sqlite_api","active":"active","sub":"running","enabled":"enabled","pid":12345,"logs":["...","..."]}
|
||||
```
|
||||
|
||||
## Valores típicos
|
||||
|
||||
- `active`: `active`, `inactive`, `failed`, `activating`, `deactivating`
|
||||
- `sub`: `running`, `dead`, `exited`, `start-pre`, etc.
|
||||
- `enabled`: `enabled`, `disabled`, `static`, `masked`
|
||||
|
||||
## Notas
|
||||
|
||||
- No requiere sudo (solo lectura).
|
||||
- Si el unit no existe, `active` será `inactive` y `sub` será `dead`.
|
||||
- Los logs vienen de `journalctl -u <name> -n N --no-pager -o cat` (sin prefijos, solo el mensaje).
|
||||
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env bash
|
||||
# systemd_local_status — Estado + últimos logs de un servicio systemd local.
|
||||
set -euo pipefail
|
||||
|
||||
systemd_local_status() {
|
||||
local name="$1"
|
||||
local log_lines="${2:-10}"
|
||||
|
||||
if [[ -z "$name" ]]; then
|
||||
echo "systemd_local_status: se requiere name" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
local active sub pid enabled
|
||||
active=$(systemctl show -p ActiveState --value "${name}.service" 2>/dev/null || echo unknown)
|
||||
sub=$(systemctl show -p SubState --value "${name}.service" 2>/dev/null || echo unknown)
|
||||
pid=$(systemctl show -p MainPID --value "${name}.service" 2>/dev/null || echo 0)
|
||||
enabled=$(systemctl is-enabled "${name}.service" 2>/dev/null || echo disabled)
|
||||
|
||||
# logs como array JSON
|
||||
local logs_json
|
||||
logs_json=$(journalctl -u "${name}.service" -n "$log_lines" --no-pager -o cat 2>/dev/null \
|
||||
| python3 -c 'import sys, json; print(json.dumps([l.rstrip() for l in sys.stdin if l.strip()]))' \
|
||||
2>/dev/null || echo "[]")
|
||||
|
||||
printf '{"name":"%s","active":"%s","sub":"%s","enabled":"%s","pid":%s,"logs":%s}\n' \
|
||||
"$name" "$active" "$sub" "$enabled" "${pid:-0}" "$logs_json"
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
---
|
||||
name: systemd_local_uninstall
|
||||
kind: function
|
||||
lang: bash
|
||||
domain: infra
|
||||
version: "1.0.0"
|
||||
purity: impure
|
||||
signature: "systemd_local_uninstall(name: string) -> json"
|
||||
description: "Detiene, deshabilita y elimina el unit file de un servicio systemd local. Idempotente: no falla si el servicio ya está parado o el unit no existe. Requiere sudo."
|
||||
tags: [systemd, service, local, infra, uninstall, cleanup]
|
||||
uses_functions: []
|
||||
uses_types: []
|
||||
returns: []
|
||||
returns_optional: false
|
||||
error_type: "error_go_core"
|
||||
imports: []
|
||||
params:
|
||||
- name: name
|
||||
desc: "nombre del servicio sin sufijo .service"
|
||||
output: "JSON {name, uninstalled:true}. Errores a stderr, exit 1."
|
||||
tested: false
|
||||
tests: []
|
||||
test_file_path: ""
|
||||
file_path: "bash/functions/infra/systemd_local_uninstall.sh"
|
||||
---
|
||||
|
||||
## Ejemplo
|
||||
|
||||
```bash
|
||||
source bash/functions/infra/systemd_local_uninstall.sh
|
||||
systemd_local_uninstall "sqlite_api"
|
||||
# {"name":"sqlite_api","uninstalled":true}
|
||||
```
|
||||
|
||||
## Notas
|
||||
|
||||
- Secuencia: stop → disable → rm unit → daemon-reload → reset-failed.
|
||||
- `stop` y `disable` con `|| true` para idempotencia (si ya no estaba corriendo/enabled, no es error).
|
||||
- `reset-failed` limpia el estado "failed" si el servicio había fallado previamente.
|
||||
@@ -0,0 +1,27 @@
|
||||
#!/usr/bin/env bash
|
||||
# systemd_local_uninstall — Detiene, deshabilita y elimina un servicio systemd local.
|
||||
set -euo pipefail
|
||||
|
||||
systemd_local_uninstall() {
|
||||
local name="$1"
|
||||
|
||||
if [[ -z "$name" ]]; then
|
||||
echo "systemd_local_uninstall: se requiere name" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
local unit_path="/etc/systemd/system/${name}.service"
|
||||
|
||||
# stop (idempotente: no falla si ya parado)
|
||||
sudo systemctl stop "${name}.service" 2>/dev/null || true
|
||||
sudo systemctl disable "${name}.service" 2>/dev/null || true
|
||||
|
||||
if [[ -f "$unit_path" ]]; then
|
||||
sudo rm -f "$unit_path"
|
||||
fi
|
||||
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl reset-failed "${name}.service" 2>/dev/null || true
|
||||
|
||||
printf '{"name":"%s","uninstalled":true}\n' "$name"
|
||||
}
|
||||
Reference in New Issue
Block a user