blob: 8a290f2037e415074ebcdafe1f93ec465d436fe6 [file] [log] [blame]
Tomáš Pecka98ad18d2020-11-13 15:39:55 +01001#!/usr/bin/env bash
2
3set -eux -o pipefail
4shopt -s failglob
5
6SYSREPOCTL="${1}"
7shift
8if [[ ! -x "${SYSREPOCTL}" ]]; then
9 echo "Cannot locate \$SYSREPOCTL"
10 exit 1
11fi
12
13SYSREPOCFG="${1}"
14shift
15if [[ ! -x "${SYSREPOCFG}" ]]; then
16 echo "Cannot locate \$SYSREPOCFG"
17 exit 1
18fi
19
20MODE="${1}"
21shift
22[ "${MODE}" != "prepare" ] && [ "${MODE}" != "uninstall" ] && error "Invalid mode of operation (neither prepare nor uninstall)"
23
24while [[ $# -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
36fi
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
Václav Kubernát7efd6d52021-11-09 01:31:11 +010042 ${SYSREPOCTL} --uninstall "${MODULE}" || true
Tomáš Pecka98ad18d2020-11-13 15:39:55 +010043 ${SYSREPOCTL} --search-dirs "${YANG_DIR}" --install "${YANG_FILE}"
44 elif [[ "${MODE}" == "uninstall" ]]; then
45 MODULE_LIST=( "${MODULE}" "${MODULE_LIST[@]}" ) # save for later; uninstall in reverse order
46 fi
47
48 # parse and process all arguments up to next "YANG" directive
49 while [[ $# -gt 0 && ${1} != "YANG" ]]; do
50 if [[ "${1}" == "FEATURE" ]]; then
51 shift
52 FEATURE="${1}"
53 shift
54 [ -z "${FEATURE}" ] && error "Error: FEATURE requires an argument"
55
56 if [[ "${MODE}" == "prepare" ]]; then
Václav Kubernát7efd6d52021-11-09 01:31:11 +010057 ${SYSREPOCTL} --change ${MODULE} --enable-feature ${FEATURE}
Tomáš Pecka98ad18d2020-11-13 15:39:55 +010058 fi
59 elif [[ "${1}" == "JSON" || "${1}" == "XML" ]]; then
60 FORMAT="${1}"
61 shift
62 DATA_FILE="${1}"
63 shift
64 if [[ "${MODE}" == "prepare" ]]; then
65 if [[ ! -f "${DATA_FILE}" ]]; then
66 echo "Error: ${FORMAT} data file ${DATA_FILE} does not exist"
67 exit 1
68 fi
69 # If the module is already installed (not just scheduled for installation), then --import should be used. If the
70 # module needs starting data (mandatory nodes), then --new-data should be used (which completes the
71 # installation). Let's just try both the methods and one of them should work
72 ${SYSREPOCFG} --datastore=startup --format="${FORMAT,,}" --module="${MODULE}" --import="${DATA_FILE}" -w || \
73 ${SYSREPOCFG} --datastore=startup --format="${FORMAT,,}" --module="${MODULE}" --new-data="${DATA_FILE}" -w
74 fi
75 fi
76 done
77done
78
79if [[ "${MODE}" == "uninstall" ]]; then
80 for module in "${MODULE_LIST[@]}"; do
Václav Kubernát7efd6d52021-11-09 01:31:11 +010081 ${SYSREPOCTL} --uninstall "${module}"
Tomáš Pecka98ad18d2020-11-13 15:39:55 +010082 done
83fi