#!/usr/bin/env bash # CONSTANTS BASEDIR=$(dirname "$0") CONTAINER=${CONTAINER:-} SCRIPT='' DEBUG=false QUIET=false FORCE=${FORCE:-false} RECIPE_ARGS= # FUNCTIONS function usage { echo "$(basename "$0") {CONTAINER_NAME} {PATH_TO_SCRIPT} [--debug|-d] [--quiet|-q] -- [recipe_args]" echo "execute a recipe inside a container with extra_args" echo -------------- echo call examples: echo " miaou-recipe ct1 recipes/helloworld.recipe -- one two" echo " CONTAINER=ct1 miaou-recipe recipes/helloworld.recipe" } function parse_options { while [[ $# -gt 0 ]]; do case "$1" in --help | -h) usage && exit 0 ;; --debug | -d) DEBUG=true ;; --quiet | -q) QUIET=true ;; --) shift 1 RECIPE_ARGS=("$@") # array of arguments, useful to prevent quoted strings => "${RECIPE_ARGS[@]}" break ;; *) if [[ -z $CONTAINER ]]; then CONTAINER=$1 elif [[ -z $SCRIPT ]]; then SCRIPT=$1 else echo >&2 "Unknown option: $1" && usage && exit 2 fi ;; esac shift 1 # Move to the next argument done [[ -n $CONTAINER ]] && [[ -n $SCRIPT ]] || (usage && exit 1) } function debug_option { [[ $DEBUG == 'true' ]] && echo "-x" || true } function recipe { [[ ! -f $SCRIPT ]] && echo >&2 "Script not found: $SCRIPT" && exit 3 cat "$SCRIPT" | incus exec "$CONTAINER" -- env FORCE="$FORCE" bash $(debug_option) -s -- "${RECIPE_ARGS[@]}" } function show_success { echo "Recipe <$(basename "$SCRIPT")> successfully performed on container <$CONTAINER>" } # MAIN set -Eue parse_options "$@" recipe $QUIET || show_success