131 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			131 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
| #!/bin/bash
 | |
| # -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
 | |
| source "$(dirname -- "$0")/lib/ulib/auto" || exit 1
 | |
| urequire template runs
 | |
| 
 | |
| function display_help() {
 | |
|     uecho "$scriptname: Gérer un répertoire d'hôte de runs
 | |
| 
 | |
| USAGE
 | |
|     $scriptname -c [host [destdir]]
 | |
|     $scriptname -t -- args...
 | |
| 
 | |
| OPTIONS
 | |
|     -c, --create
 | |
|         Créer un nouveau répertoire de configuration pour un hôte
 | |
|     -d, --destdir DESTDIR[=$TEMPLATECTL_NAME]
 | |
|         Nom du répertoire local de configuration.
 | |
| 
 | |
|     -t, --template [OPT]
 | |
|         Gérer les fichiers du répertoire local avec templatectl. La valeur de
 | |
|         cette option est utilisée comme argument court pour l'invocation de
 | |
|         templatectl, e.g
 | |
|             $scriptname -tm args
 | |
|         est équivalent à
 | |
|             templatectl -m args
 | |
|         Les arguments qui restent sont passés tels quels à templatectl
 | |
|         Les options courantes de templatectl -l, -v, -m, -L sont disponibles
 | |
|         directement
 | |
|     --help-template
 | |
|         Afficher l'aide concernent la gestion des templates.
 | |
|         Equivalent à -t -- --help
 | |
|     -h, --host HOST
 | |
|         Spécifier l'hôte. Equivalent à -v host=HOST"
 | |
| }
 | |
| 
 | |
| ## Valeurs par défaut des variables de template
 | |
| set_defaults runs
 | |
| 
 | |
| # essayer de déterminer l'hôte à partir du répertoire courant
 | |
| DEFAULT_HOST=
 | |
| array_split __hostsdirs "$RUNSHOSTSPATH" :
 | |
| setx __cwd=pwd
 | |
| for __hostsdir in "${__hostsdirs[@]}"; do
 | |
|     setx __hostsdir=abspath "$__hostsdir"
 | |
|     if [ "${__cwd#$__hostsdir/}" != "$__cwd" ]; then
 | |
|         DEFAULT_HOST="${__cwd#$__hostsdir/}"
 | |
|         DEFAULT_HOST="${DEFAULT_HOST%%/*}"
 | |
|         break
 | |
|     fi
 | |
| done
 | |
| 
 | |
| # sinon lire l'environnement
 | |
| if [ -z "$DEFAULT_HOST" ]; then
 | |
|     runs_initdomains
 | |
|     setx DEFAULT_HOST=myhost
 | |
|     splithost "$DEFAULT_HOST" __hostname __domain
 | |
|     if [ -z "$__domain" ]; then
 | |
|         DEFAULT_HOST="$(runs_find_host "$DEFAULT_HOST")"
 | |
|         splithost "$DEFAULT_HOST" __hostname __domain
 | |
|     fi
 | |
|     [ -n "$__domain" ] || DEFAULT_HOST="$(runs_add_domain "$DEFAULT_HOST")"
 | |
| fi
 | |
| 
 | |
| TEMPLATE_STATIC_VARS=(host hostname)
 | |
| TEMPLATE_DYNAMIC_VARS=()
 | |
| TEMPLATE_NOWRITE_VARS=(hostname)
 | |
| 
 | |
| __TEMPLATE_DEFAULTF_host=__template_defaultf_host
 | |
| __TEMPLATE_UPDATEF_host=__template_updatef_host
 | |
| function __template_defaultf_host() {
 | |
|     echo "$DEFAULT_HOST"
 | |
| }
 | |
| function __template_updatef_host() {
 | |
|     __template_set_var hostname "${host%%.*}"
 | |
| }
 | |
| 
 | |
| TEMPLATECTL_NAME=runs
 | |
| TEMPLATECTL_SRCDIRS=(runsconfig)
 | |
| TEMPLATECTL_CONFIG=runsconfig
 | |
| TEMPLATECTL_DEFAULTS=()
 | |
| TEMPLATECTL_VARS=()
 | |
| 
 | |
| action=
 | |
| destdir=
 | |
| templateopt=
 | |
| certsdir=
 | |
| args=(
 | |
|     --help '$exit_with display_help'
 | |
|     -t::,--template:: '$set@ templateopt; action=template'
 | |
|     --help-template '$templateopt=-help; action=template'
 | |
|     -l,--list '$templateopt=l; action=template'
 | |
|     -v:,--var: TEMPLATECTL_VARS
 | |
|     -m,--merge '$templateopt=m; action=template'
 | |
|     -L,--list-vars '$templateopt=L; action=template'
 | |
|     -c,--create action=create
 | |
|     -h:,--host: '$array_add TEMPLATECTL_VARS host="$value_"'
 | |
|     -d:,--destdir: destdir=
 | |
| )
 | |
| parse_args "$@"; set -- "${args[@]}"
 | |
| 
 | |
| __template_set_destdir destdir autocreate "$TEMPLATECTL_NAME" || die
 | |
| setx config=templatectl_config "$destdir"
 | |
| templatectl_loadvars "$config"
 | |
| 
 | |
| if [ -z "$host" ]; then
 | |
|     [ -n "$host" ] || host="$1"
 | |
|     [ -n "$host" ] || host="$DEFAULT_HOST"
 | |
|     __template_set_var host "$host"
 | |
| fi
 | |
| 
 | |
| ################################################################################
 | |
| if [ "$action" == create ]; then
 | |
|     if [ -n "$autocreate" -a ! -d "$destdir" ]; then
 | |
|         estepn "Création automatique de $(ppath "$destdir")"
 | |
|         mkdir -p "$destdir" || die
 | |
|     fi
 | |
|     [ -d "$destdir" ] || die "$destdir: répertoire introuvable"
 | |
| 
 | |
|     read_value ${host:+-i} "Veuillez entrer le nom d'hôte" host "$host"
 | |
|     __template_set_var host "$host"
 | |
| 
 | |
|     templatectl -d "$destdir" --no-load-vars -m
 | |
| 
 | |
| ################################################################################
 | |
| elif [ "$action" == template ]; then
 | |
|     __TEMPLATECTL_HELP="USAGE: $scriptname -t -- args...
 | |
| 
 | |
| $__TEMPLATECTL_HELP"
 | |
|     templatectl -d "$destdir" --no-load-vars ${templateopt:+-$templateopt} "$@"
 | |
| fi
 |