summaryrefslogtreecommitdiff
path: root/core/modules/package.sh
blob: 7e4116193dee59f6d0ab6dae04d2be8f5d58706c (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/bash
# Module: package
# Description: Instala, actualiza o elimina paquetes .deb/.rpm y permite actualizar el sistema
# License: GPLv3
# Author: Luis GuLo
# Version: 2.2.0
# Dependencies: ssh

package_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]:-present}"
  local become="${args[become]:-false}"
  local update_type="${args[update_type]:-full}"  # full | security

  local prefix=""
  [ "$become" = "true" ] && prefix="sudo"

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

  echo "$(render_msg "${tr[start]}" "state=$state" "name=${name:-<sistema>}")"

  local pkg_mgr
  pkg_mgr=$(ssh "$host" "command -v apt-get || command -v apt || command -v dnf || command -v yum")

  if [ -z "$pkg_mgr" ]; then
    echo "${tr[no_pkg_mgr]:-❌ [package] No se detectó gestor de paquetes compatible en el host.}"
    return 1
  fi

  case "$pkg_mgr" in
    *apt*)
      if [ "$state" = "system-update" ]; then
        system_update_apt "$host" "$prefix"
      else
        package_apt "$host" "$name" "$state" "$prefix"
      fi
      ;;
    *yum*|*dnf*)
      if [ "$state" = "system-update" ]; then
        system_update_rpm "$host" "$prefix" "$update_type"
      else
        package_rpm "$host" "$name" "$state" "$prefix"
      fi
      ;;
    *)
      echo "$(render_msg "${tr[unsupported_mgr]}" "mgr=$pkg_mgr")"
      return 1
      ;;
  esac
}

package_apt() {
  local host="$1"
  local name="$2"
  local state="$3"
  local prefix="$4"

  local check_cmd="dpkg -s '$name' &> /dev/null"
  local install_cmd="$prefix apt-get update && $prefix apt-get install -y '$name'"
  local remove_cmd="$prefix apt-get remove -y '$name'"
  local upgrade_cmd="$prefix apt-get update && $prefix apt-get install --only-upgrade -y '$name'"

  case "$state" in
    present) ssh "$host" "$check_cmd || $install_cmd" ;;
    absent)  ssh "$host" "$check_cmd && $remove_cmd" ;;
    latest)  ssh "$host" "$check_cmd && $upgrade_cmd || $install_cmd" ;;
    *) echo "$(render_msg "${tr[unsupported_state_apt]}" "state=$state")"; return 1 ;;
  esac
}

package_rpm() {
  local host="$1"
  local name="$2"
  local state="$3"
  local prefix="$4"

  local check_cmd="rpm -q '$name' &> /dev/null"
  local install_cmd="$prefix yum install -y '$name' || $prefix dnf install -y '$name'"
  local remove_cmd="$prefix yum remove -y '$name' || $prefix dnf remove -y '$name'"
  local upgrade_cmd="$prefix yum update -y '$name' || $prefix dnf upgrade -y '$name'"

  case "$state" in
    present) ssh "$host" "$check_cmd || $install_cmd" ;;
    absent)  ssh "$host" "$check_cmd && $remove_cmd" ;;
    latest)  ssh "$host" "$check_cmd && $upgrade_cmd || $install_cmd" ;;
    *) echo "$(render_msg "${tr[unsupported_state_rpm]}" "state=$state")"; return 1 ;;
  esac
}

system_update_apt() {
  local host="$1"
  local prefix="$2"
  echo "${tr[update_apt]:-🔄 [package] Actualización completa del sistema (.deb)}"
  ssh "$host" "$prefix apt-get update && $prefix apt-get upgrade -y"
}

system_update_rpm() {
  local host="$1"
  local prefix="$2"
  local update_type="$3"

  if [ "$update_type" = "security" ]; then
    echo "${tr[update_rpm_security]:-🔐 [package] Actualización de seguridad (.rpm)}"
    ssh "$host" "$prefix dnf update --security -y || $prefix yum update --security -y"
  else
    echo "${tr[update_rpm_full]:-🔄 [package] Actualización completa del sistema (.rpm)}"
    ssh "$host" "$prefix dnf upgrade --refresh -y || $prefix yum update -y"
  fi
}

check_dependencies_package() {
  if ! command -v ssh &> /dev/null; then
    echo "${tr[missing_deps]:-❌ [package] ssh no está disponible.}"
    return 1
  fi
  echo "${tr[deps_ok]:-✅ [package] ssh disponible.}"
  return 0
}