64 lines
1.9 KiB
Bash
Executable File
64 lines
1.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
#
|
|
# lib/exec/add-host
|
|
#
|
|
# Copyright (c) 2020- RAP <contacto@partidopirata.com.ar>
|
|
# Copyright (c) 2011-2016 LibreVPN <vpn@hackcoop.com.ar>
|
|
#
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero General Public License as
|
|
# published by the Free Software Foundation; either version 3 of the
|
|
# License, or (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
# General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General
|
|
# Public License along with this program. If not, see
|
|
# <http://www.gnu.org/licenses/>.
|
|
|
|
. "${RAP_LIBDIR}"/common
|
|
|
|
UPDATE=false
|
|
FORCE=false
|
|
while getopts "ufhd" arg; do
|
|
case $arg in
|
|
u) UPDATE=true; FORCE=true ;;
|
|
f) FORCE=true ;;
|
|
h) help ${self} ; exit 0;;
|
|
esac
|
|
done
|
|
let OPTIND--; shift ${OPTIND}
|
|
|
|
node="$(get_node_name "$1")"; shift
|
|
nodedir="$(get_node_dir ${node})"
|
|
|
|
# Si estamos actualizando, la lista de hosts es la que ya esta cargada
|
|
${UPDATE} || hosts=("$@")
|
|
${UPDATE} && hosts=($(shopt -s nullglob; echo "${nodedir}"/hosts/* | sed "s,${nodedir}/hosts/,,g"))
|
|
|
|
# Recorrer todos los host que se pasaron
|
|
for _host in "${hosts[@]}"; do
|
|
# Generar el archivo y encontrarlo, saltear si no existe
|
|
_hostfile="$(get_host_file "${_host}")"
|
|
_hostdest="${nodedir}/hosts/${_host}"
|
|
|
|
if test ! -f "${_hostfile}"; then
|
|
error "El archivo host de %s no existe, salteando..." ${_host}
|
|
continue
|
|
fi
|
|
|
|
# Saltear si no el host existe y no estamos forzando o actualizando
|
|
if test -f "${_hostdest}"; then
|
|
${FORCE} || continue
|
|
fi
|
|
|
|
msg "Copiando el archivo host de %s" ${_host}
|
|
# Copiar el contenido del archivo
|
|
cat "${_hostfile}" > "${nodedir}/hosts/${_host}"
|
|
done
|
|
|
|
exit $?
|