summaryrefslogtreecommitdiff
path: root/core/utils/shflow-check.sh
blob: 6906868519d2f3369be37eefae2e87c1d4fe8a18 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env bash
# ShFlow Environment Checker
# License: GPLv3
# Author: Luis GuLo
# Version: 1.5.1

set -e

PROJECT_ROOT="${SHFLOW_HOME:-$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)}"

MODULE_PATHS=(
  "$PROJECT_ROOT/core/modules"
  "$PROJECT_ROOT/user_modules"
  "$PROJECT_ROOT/community_modules"
)

# 🧩 Cargar funciones comunes si no están disponibles
COMMON_LIB="$PROJECT_ROOT/core/lib/translate_msg.sh"
if ! declare -f render_msg &>/dev/null; then
  [[ -f "$COMMON_LIB" ]] && source "$COMMON_LIB"
fi

# 🔧 yq segun arquitectura
ARCH=$(uname -m)
case "$ARCH" in
  x86_64) YQ_BIN="$PROJECT_ROOT/core/utils/yq_linux_amd64" ;;
  i686|i386) YQ_BIN="$PROJECT_ROOT/core/utils/yq_linux_386" ;;
  aarch64) YQ_BIN="$PROJECT_ROOT/core/utils/yq_linux_arm64" ;;
  armv7l|armv6l) YQ_BIN="$PROJECT_ROOT/core/utils/yq_linux_arm" ;;
  *) echo "❌ Arquitectura no soportada: $ARCH"; exit 1 ;;
esac

GLOBAL_TOOLS=("bash" "ssh" "scp" "git" "curl" "jq" "$YQ_BIN" "gpg")

REQUIRED_PATHS=(
  "$PROJECT_ROOT/core/modules"
  "$PROJECT_ROOT/core/utils"
  "$PROJECT_ROOT/core/inventory"
  "$PROJECT_ROOT/examples"
  "$PROJECT_ROOT/user_modules"
  "$PROJECT_ROOT/community_modules"
  "$PROJECT_ROOT/shflow.sh"
  "$PROJECT_ROOT/vault.sh"
)

# 📦 Variables Globales
declare -A shflow_vars
GLOBAL_VARS="$PROJECT_ROOT/core/inventory/vars/all.yaml"

# 🌐 Cargar traducciones
lang="${shflow_vars[language]:-es}"
trfile="$PROJECT_ROOT/core/utils/shflow-check.tr.${lang}"
declare -A tr
if [[ -f "$trfile" ]]; then while IFS='=' read -r k v; do tr["$k"]="$v"; done < "$trfile"; fi

check_global_tools() {
  echo "${tr[tools_header]:-🔍 Verificando herramientas globales...}"
  local missing=0
  for tool in "${GLOBAL_TOOLS[@]}"; do
    if ! command -v "$tool" &> /dev/null; then
      echo "$(render_msg "${tr[tool_missing]}" "tool=$tool")"
      missing=1
    else
      tool=$(basename $tool)
      echo "$(render_msg "${tr[tool_ok]}" "tool=$tool")"
    fi
  done
  return $missing
}

check_structure() {
  echo ""
  echo "${tr[structure_header]:-📁 Verificando estructura de ShFlow...}"
  local missing=0
  for path in "${REQUIRED_PATHS[@]}"; do
    if [ ! -e "$path" ]; then
      echo "$(render_msg "${tr[path_missing]}" "path=$path")"
      missing=1
    else
      echo "$(render_msg "${tr[path_ok]}" "path=$path")"
    fi
  done
  return $missing
}

load_and_check_modules() {
  echo ""
  echo "${tr[modules_header]:-🔍 Verificando módulos ShFlow...}"
  for dir in "${MODULE_PATHS[@]}"; do
    [ -d "$dir" ] || continue
    while IFS= read -r -d '' mod; do
      source "$mod"
    done < <(find "$dir" -type f -name "*.sh" -print0)
  done

  for func in $(declare -F | awk '{print $3}' | grep '^check_dependencies_'); do
    echo ""
    echo "$(render_msg "${tr[checking_func]}" "func=$func")"
    $func || echo "$(render_msg "${tr[func_warn]}" "func=$func")"
  done
}

# 📦 Carga las variables globales de all.yaml
load_global_vars() {
  if [[ -f "$GLOBAL_VARS" ]]; then
    echo "${tr[vars_loading]:-📦 Cargando variables globales de all.yaml...}"
    
    # Carga todas las claves del YAML
    GLOBAL_KEYS=$("$YQ_BIN" eval 'keys[]' "$GLOBAL_VARS" -r 2>/dev/null || true)
    
    for key in $GLOBAL_KEYS; do
      # Carga el valor en modo raw para Bash
      raw_value=$("$YQ_BIN" eval ".\"$key\"" "$GLOBAL_VARS" -r 2>/dev/null || true)
      
      # Si el valor es una lista o multilínea, yq lo habrá cargado con saltos de línea, lo almacenamos tal cual.
      # Si es un valor simple, lo almacenamos.
      shflow_vars["$key"]="$raw_value"
    done
    
    echo "$(render_msg "${tr[vars_loaded]:-✅ Variables cargadas: {count}}" "count=${#shflow_vars[@]}")"
    
    # Si la variable 'language' se ha cargado, actualizamos las traducciones del check.
    local new_lang="${shflow_vars[language]:-es}"
    if [[ "$new_lang" != "$lang" ]]; then
        lang="$new_lang"
        trfile="$PROJECT_ROOT/core/utils/shflow-check.tr.${lang}"
        if [[ -f "$trfile" ]]; then
             declare -A tr # Reset array
             while IFS='=' read -r k v; do tr["$k"]="$v"; done < "$trfile"
        fi
    fi
  else
    echo "$(render_msg "${tr[vars_missing]:-⚠️ all.yaml no encontrado en {path}}" "path=$GLOBAL_VARS")"
  fi
}

# 🧠 Mostrar variables cargadas
show_global_vars() {
  echo ""
  echo "${tr[debug_vars_header]:-🌐 VARIABLES GLOBALES CARGADAS:}"
  echo "====================================="
  local count=0
  for key in "${!shflow_vars[@]}"; do
    local display_value="${shflow_vars[$key]}"
    
    # Formateo básico para listas para una mejor lectura en la consola
    if [[ "$key" == "ntp_servers" || "$key" == "default_packages" ]]; then
        # Reemplaza los saltos de línea por comas y espacio
        display_value=$(echo "$display_value" | tr '\n' ' ' | xargs | sed 's/ /\//g')
    fi
    
    echo "- $key: $display_value"
    count=$((count + 1))
  done
  echo "-------------------------------------"
  echo "$(render_msg "${tr[debug_vars_count]:-Total de variables: {count}" "count=$count")"
}


main() {
  echo "${tr[title]:-🧪 ShFlow Environment Check}"
  echo "${tr[separator]:-=============================}"

  load_global_vars
    
  check_global_tools
  check_structure
  load_and_check_modules
  
  show_global_vars

  echo ""
  echo "${tr[done]:-✅ Verificación completada.}"
}

main "$@"