]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1077] script installs YANG modules: reinstall.sh
authorAndrei Pavel <andrei@isc.org>
Thu, 15 Jul 2021 12:03:41 +0000 (15:03 +0300)
committerTomek Mrugalski <tomek@isc.org>
Fri, 23 Jul 2021 10:45:36 +0000 (10:45 +0000)
.gitlab-ci.yml
src/share/yang/modules/utils/reinstall.sh [new file with mode: 0755]

index 6092b176d1e7d016513c940e63c886c8ed88b467..1187f589a605dd8d5b119528acda944c19e5faf9 100644 (file)
@@ -86,6 +86,7 @@ shellcheck:
     - SCRIPTS+="src/share/yang/modules/utils/check-hashes.sh "
     - SCRIPTS+="src/share/yang/modules/utils/check-revisions.sh "
     - SCRIPTS+="src/share/yang/modules/utils/gen-revisions.sh "
+    - SCRIPTS+="src/share/yang/modules/utils/reinstall.sh "
     - SCRIPTS+="tools/add-config-h.sh "
     - SCRIPTS+="tools/bump-lib-versions.sh "
     - SCRIPTS+="tools/check-for-duplicate-includes.sh "
diff --git a/src/share/yang/modules/utils/reinstall.sh b/src/share/yang/modules/utils/reinstall.sh
new file mode 100755 (executable)
index 0000000..ccc7c12
--- /dev/null
@@ -0,0 +1,144 @@
+#!/bin/sh
+
+# Copyright (C) 2021 Internet Systems Consortium, Inc. ("ISC")
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+# Usage:
+#
+# ./src/share/yang/modules/reinstall.sh [-d|--debug] [-h|--help] [-s|--sysrepo ${SYSREPO_INSTALLATION}]
+
+# Exit with error if commands exit with non-zero and if undefined variables are
+# used.
+set -eu
+
+script_path=$(cd "$(dirname "${0}")" && pwd)
+kea_sources=$(cd "${script_path}/../../../../.." && pwd)
+modules="${kea_sources}/src/share/yang/modules"
+
+# Print usage.
+# Expressions don't expand in single quotes, use double quotes for that. [SC2016]
+# shellcheck disable=SC2016
+print_usage() {
+  printf \
+'Usage: %s {{options}}
+Options:
+  [-d|--debug]                              enable debug mode, showing every executed command
+  [-h|--help]                               print usage (this text)
+  [-s|--sysrepo ${SYSREPO_INSTALLATION}]    point to sysrepo installation which is needed for sysrepoctl
+  [-r|--repository ${SYSREPO_REPOSITORY}]   point to sysrepo repository
+' \
+    "$(basename "${0}")"
+}
+
+# Define some ANSI color codes.
+if test -t 1; then
+  red='\033[91m'
+  reset='\033[0m'
+else
+  red=
+  reset=
+fi
+
+# Parse parameters.
+while test ${#} -gt 0; do
+  case "${1}" in
+    # [-d|--debug]                              enable debug mode, showing every executed command
+    '-d'|'--debug') set -vx ;;
+
+    # [-h|--help]                               print usage (this text)
+    '-h'|'--help') print_usage ;;
+
+    # [-s|--sysrepo ${SYSREPO_INSTALLATION}]    point to sysrepo installation which is needed for sysrepoctl
+    '-s'|'--sysrepo') shift; sysrepo=${1} ;;
+
+    # Unrecognized argument
+    *)
+    printf "${red}ERROR: Unrecognized argument '%s'${reset}\\n" "${1}" 1>&2; print_usage; exit 1 ;;
+  esac; shift
+done
+
+# Default arguments
+test -z "${sysrepo+x}" && sysrepo='/usr/local'
+
+#------------------------------------------------------------------------------#
+
+# Check if model is installed.
+is_model_installed() {
+  model=${1}
+  if test "$("${sysrepo}/bin/sysrepoctl" -l | grep -F '| I' | cut -d ' ' -f 1 | tail -n +7 | head -n -1 | grep -Ec "^${model}")" -eq 0; then
+    # not installed
+    return 1
+  fi
+  # installed
+  return 0
+}
+
+# Install a model from the Kea sources. Should upgrade automatically to a newer
+# revision.
+install_kea_model() {
+  model=${1}
+  find "${modules}" -maxdepth 1 -type f -name "${model}*.yang" -exec \
+    ${sysrepo}/bin/sysrepoctl -i {} -s "${modules}" -v 4 \;
+}
+
+# Uninstall a model if installed.
+uninstall_model() {
+  model=${1}
+  if ! is_model_installed "${model}"; then
+    return;
+  fi
+  "${sysrepo}/bin/sysrepoctl" -u "${model}" -v 4
+}
+
+# Install all YANG models in dependency order.
+install_yang_models() {
+  install_kea_model 'keatest-module'
+  install_kea_model 'ietf-interfaces'
+  install_kea_model 'ietf-dhcpv6-common'
+  install_kea_model 'ietf-dhcpv6-client'
+  install_kea_model 'ietf-dhcpv6-relay'
+  install_kea_model 'ietf-dhcpv6-server'
+  install_kea_model 'ietf-yang-types'
+  install_kea_model 'ietf-dhcpv6-options'
+  install_kea_model 'ietf-dhcpv6-types'
+  install_kea_model 'ietf-inet-types'
+  install_kea_model 'kea-types'
+  install_kea_model 'kea-dhcp-types'
+  install_kea_model 'kea-dhcp-ddns'
+  install_kea_model 'kea-ctrl-agent'
+  install_kea_model 'kea-dhcp4-server'
+  install_kea_model 'kea-dhcp6-server'
+}
+
+# Uninstall all YANG models in reverse dependency order.
+# Currently not working. It complains:
+#   Internal module "ietf-inet-types" cannot be uninstalled.
+# Something about another module depending on ietf-inet-types.
+# Might be related to a module that is internal to sysrepo.
+# Might be for the better since installing YANG modules is idempotent and
+# actually has logic to only install if the revision is newer which is arguably
+# beneficial.
+uninstall_yang_models() {
+  uninstall_model 'kea-dhcp6-server'
+  uninstall_model 'kea-dhcp4-server'
+  uninstall_model 'kea-ctrl-agent'
+  uninstall_model 'kea-dhcp-ddns'
+  uninstall_model 'kea-dhcp-types'
+  uninstall_model 'kea-types'
+  uninstall_model 'ietf-inet-types'
+  uninstall_model 'ietf-dhcpv6-types'
+  uninstall_model 'ietf-dhcpv6-options'
+  uninstall_model 'ietf-yang-types'
+  uninstall_model 'ietf-dhcpv6-server'
+  uninstall_model 'ietf-dhcpv6-relay'
+  uninstall_model 'ietf-dhcpv6-client'
+  uninstall_model 'ietf-dhcpv6-common'
+  uninstall_model 'ietf-interfaces'
+  uninstall_model 'keatest-module'
+}
+
+# uninstall_yang_models
+install_yang_models