Tomáš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | set -eux -o pipefail |
| 4 | shopt -s failglob |
| 5 | |
| 6 | SYSREPOCTL="${1}" |
| 7 | shift |
| 8 | if [[ ! -x "${SYSREPOCTL}" ]]; then |
| 9 | echo "Cannot locate \$SYSREPOCTL" |
| 10 | exit 1 |
| 11 | fi |
| 12 | |
| 13 | SYSREPOCFG="${1}" |
| 14 | shift |
| 15 | if [[ ! -x "${SYSREPOCFG}" ]]; then |
| 16 | echo "Cannot locate \$SYSREPOCFG" |
| 17 | exit 1 |
| 18 | fi |
| 19 | |
| 20 | MODE="${1}" |
| 21 | shift |
| 22 | [ "${MODE}" != "prepare" ] && [ "${MODE}" != "uninstall" ] && error "Invalid mode of operation (neither prepare nor uninstall)" |
| 23 | |
| 24 | while [[ $# -gt 0 ]]; do |
| 25 | if [[ "${1}" == "YANG" ]]; then |
| 26 | shift |
| 27 | else |
| 28 | error "Error: Expected 'YANG yangfile' clause" |
| 29 | fi |
| 30 | |
| 31 | YANG_FILE="${1}" |
| 32 | shift |
| 33 | if [[ ! -f "${YANG_FILE}" ]]; then |
| 34 | echo "Error: Specified YANG file does not exist" |
| 35 | exit 1 |
| 36 | fi |
| 37 | |
| 38 | MODULE=$(basename --suffix .yang "${YANG_FILE}" | sed 's/@.*//') # also remove part after @ from module name |
| 39 | YANG_DIR=$(dirname "${YANG_FILE}") |
| 40 | |
| 41 | if [[ "${MODE}" == "prepare" ]]; then |
| 42 | ${SYSREPOCTL} --uninstall "${MODULE}" -a || true |
| 43 | # not using -a here, because modules with mandatory need startup data first |
| 44 | ${SYSREPOCTL} --search-dirs "${YANG_DIR}" --install "${YANG_FILE}" |
| 45 | elif [[ "${MODE}" == "uninstall" ]]; then |
| 46 | MODULE_LIST=( "${MODULE}" "${MODULE_LIST[@]}" ) # save for later; uninstall in reverse order |
| 47 | fi |
| 48 | |
| 49 | # parse and process all arguments up to next "YANG" directive |
| 50 | while [[ $# -gt 0 && ${1} != "YANG" ]]; do |
| 51 | if [[ "${1}" == "FEATURE" ]]; then |
| 52 | shift |
| 53 | FEATURE="${1}" |
| 54 | shift |
| 55 | [ -z "${FEATURE}" ] && error "Error: FEATURE requires an argument" |
| 56 | |
| 57 | if [[ "${MODE}" == "prepare" ]]; then |
| 58 | ${SYSREPOCTL} --change ${MODULE} --enable-feature ${FEATURE} -a |
| 59 | fi |
| 60 | elif [[ "${1}" == "JSON" || "${1}" == "XML" ]]; then |
| 61 | FORMAT="${1}" |
| 62 | shift |
| 63 | DATA_FILE="${1}" |
| 64 | shift |
| 65 | if [[ "${MODE}" == "prepare" ]]; then |
| 66 | if [[ ! -f "${DATA_FILE}" ]]; then |
| 67 | echo "Error: ${FORMAT} data file ${DATA_FILE} does not exist" |
| 68 | exit 1 |
| 69 | fi |
| 70 | # If the module is already installed (not just scheduled for installation), then --import should be used. If the |
| 71 | # module needs starting data (mandatory nodes), then --new-data should be used (which completes the |
| 72 | # installation). Let's just try both the methods and one of them should work |
| 73 | ${SYSREPOCFG} --datastore=startup --format="${FORMAT,,}" --module="${MODULE}" --import="${DATA_FILE}" -w || \ |
| 74 | ${SYSREPOCFG} --datastore=startup --format="${FORMAT,,}" --module="${MODULE}" --new-data="${DATA_FILE}" -w |
| 75 | fi |
| 76 | fi |
| 77 | done |
| 78 | done |
| 79 | |
| 80 | if [[ "${MODE}" == "uninstall" ]]; then |
| 81 | for module in "${MODULE_LIST[@]}"; do |
| 82 | ${SYSREPOCTL} --uninstall "${module}" -a |
| 83 | done |
| 84 | fi |