summaryrefslogtreecommitdiff
path: root/core/modules/service.sh
blob: bda0cd33cda003da3d9d9f7a852462ff1e90ff48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
# Module: service
# Description: Controla servicios del sistema remoto (start, stop, restart, enable, disable) con idempotencia
# License: GPLv3
# Author: Luis GuLo
# Version: 1.2.0
# Dependencies: ssh, systemctl

service_task() {
  local host="$1"; shift
  declare -A args
  for arg in "$@"; do
    key="${arg%%=*}"
    value="${arg#*=}"
    args["$key"]="$value"
  done

  local name="${args[name]}"
  local state="${args[state]}"
  local become="${args[become]}"
  local prefix=""
  [ "$become" = "true" ] && prefix="sudo"

  # 🌐 Cargar traducciones
  local lang="${shflow_vars[language]:-es}"
  local trfile="$(dirname "${BASH_SOURCE[0]}")/service.tr.${lang}"
  declare -A tr
  if [[ -f "$trfile" ]]; then
    while IFS='=' read -r k v; do tr["$k"]="$v"; done < "$trfile"
  fi

  case "$state" in
    start|stop|restart|enable|disable)
      echo "$(render_msg "${tr[executing]}" "state=$state" "name=$name" "host=$host")"
      ssh "$host" "$prefix systemctl $state '$name'"
      ;;
    *)
      echo "$(render_msg "${tr[unsupported]}" "state=$state")"
      return 1
      ;;
  esac
}

check_dependencies_service() {
  local lang="${shflow_vars[language]:-es}"
  local trfile="$(dirname "${BASH_SOURCE[0]}")/service.tr.${lang}"
  declare -A tr
  if [[ -f "$trfile" ]]; then
    while IFS='=' read -r k v; do tr["$k"]="$v"; done < "$trfile"
  fi

  if ! command -v ssh &> /dev/null; then
    echo "${tr[missing_ssh]:-❌ [service] ssh no está disponible.}"
    return 1
  fi
  echo "${tr[ssh_ok]:-✅ [service] ssh disponible.}"

  if ! command -v systemctl &> /dev/null; then
    echo "${tr[missing_systemctl]:-⚠️ [service] systemctl no está disponible localmente. Se asumirá que existe en el host remoto.}"
  else
    echo "${tr[systemctl_ok]:-✅ [service] systemctl disponible localmente.}"
  fi
  return 0
}