summaryrefslogtreecommitdiff
path: root/core/modules/cron.sh
blob: c1c4533fd28f4a475534b4bf8192a7bed085d3c7 (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
#!/bin/bash
# Module: cron
# Description: Gestiona entradas de cron para usuarios del sistema (crear, modificar, eliminar, listar)
# License: GPLv3
# Author: Luis GuLo
# Version: 1.1.0
# Dependencies: bash, crontab, grep, id, sudo

cron_task() {
  local host="$1"; shift
  local alias="" user="" state="" schedule="" command=""
  for arg in "$@"; do
    case "$arg" in
      alias=*) alias="${arg#alias=}" ;;
      user=*) user="${arg#user=}" ;;
      state=*) state="${arg#state=}" ;;
      schedule=*) schedule="${arg#schedule=}" ;;
      command=*) command="${arg#command=}" ;;
    esac
  done

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

  if [[ -z "$user" || -z "$state" ]]; then
    echo "${tr[missing_args]:-❌ [cron] Faltan argumentos obligatorios: 'user' y 'state'}"
    return 1
  fi

  if ! id "$user" &>/dev/null; then
    echo "$(render_msg "${tr[user_not_found]}" "user=$user")"
    return 1
  fi

  local tag="# shflow:$alias"
  local tmpfile
  tmpfile=$(mktemp)

  echo "$(render_msg "${tr[checking]}" "user=$user")"
  sudo crontab -u "$user" -l 2>/dev/null > "$tmpfile" || true

  case "$state" in
    list)
      echo "$(render_msg "${tr[list]}" "user=$user")"
      grep -E "^.*$tag|^[^#]" "$tmpfile" || echo "${tr[no_entries]:-⚠️ [cron] No hay entradas visibles}"
      rm -f "$tmpfile"
      return 0
      ;;
    absent)
      if grep -q "$tag" "$tmpfile"; then
        grep -v "$tag" "$tmpfile" > "${tmpfile}.new"
        sudo crontab -u "$user" "${tmpfile}.new"
        echo "$(render_msg "${tr[removed]}" "alias=$alias")"
        rm -f "${tmpfile}.new"
      else
        echo "$(render_msg "${tr[not_found]}" "alias=$alias")"
      fi
      rm -f "$tmpfile"
      return 0
      ;;
    present)
      if [[ -z "$alias" || -z "$schedule" || -z "$command" ]]; then
        echo "${tr[missing_present]:-❌ [cron] Para 'present' se requieren: alias, schedule y command}"
        rm -f "$tmpfile"
        return 1
      fi
      grep -v "$tag" "$tmpfile" > "${tmpfile}.new"
      echo "$schedule $command $tag" >> "${tmpfile}.new"
      sudo crontab -u "$user" "${tmpfile}.new"
      echo "$(render_msg "${tr[added]}" "alias=$alias")"
      rm -f "$tmpfile" "${tmpfile}.new"
      return 0
      ;;
    *)
      echo "$(render_msg "${tr[unsupported]}" "state=$state")"
      rm -f "$tmpfile"
      return 1
      ;;
  esac
}

check_dependencies_cron() {
  local lang="${shflow_vars[language]:-es}"
  local trfile="$(dirname "${BASH_SOURCE[0]}")/cron.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 sudo &>/dev/null; then
    echo "${tr[missing_sudo]:-❌ [cron] El comando 'sudo' no está disponible}"
    return 1
  fi
  echo "${tr[deps_ok]:-✅ [cron] Dependencias OK}"
  return 0
}