summaryrefslogtreecommitdiffstats
path: root/src/share/yang/modules/utils/reinstall.sh.in
blob: 5924a6cd66030cc040e6d0f2131d39954682cea3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/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:
#
# 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

# 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
  [-u|--uninstall-first]                    uninstall before installing
' \
    "$(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; exit 0 ;;

    # [-s|--sysrepo ${SYSREPO_INSTALLATION}]    point to sysrepo installation which is needed for sysrepoctl
    '-s'|'--sysrepo') shift; sysrepo=${1} ;;

    # [-u|--uninstall-first]                    uninstall before installing
    '-u'|'--uninstall-first') uninstall_first=true ;;

    # 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'
test -z "${uninstall_first+x}" && uninstall_first='false'

#------------------------------------------------------------------------------#

# Get script path.
script_path=$(cd "$(dirname "${0}")" && pwd)

# shellcheck disable=SC2034
# prefix appears unused. Verify use (or export if used externally).
# reason: prefix is used in datarootdir (@datarootdir@) below.
prefix="@prefix@"

# Find modules location.
# If script is in sources, use modules from sources.
# If script is in installation, use modules from installation.
for i in \
  "@datarootdir@/@PACKAGE_NAME@/yang/modules" \
  "@abs_top_builddir@/src/share/yang/modules" \
; do
  if test "${script_path}" = "${i}/utils"; then
    modules="${i}"
    break
  fi
done
if test -z "${modules+x}"; then
  printf 'ERROR: cannot find location of modules. Use this script from sources or from installation.' >&2
  exit 1
fi

# sysrepoctl does not set its rpath. Set LD_LIBRARY_PATH instead.
export LD_LIBRARY_PATH="${LD_LIBRARY_PATH-}:${sysrepo}/lib:${sysrepo}/lib64"

# Check if module is installed.
is_module_installed() {
  module=${1}
  if test "$("${sysrepo}/bin/sysrepoctl" -l | grep -F '| I' | cut -d ' ' -f 1 | tail -n +7 | head -n -1 | grep -Ec "^${module}")" -eq 0; then
    # not installed
    return 1
  fi
  # installed
  return 0
}

# Install a module from the Kea sources. Should upgrade automatically to a newer
# revision.
install_kea_module() {
  module=${1}

  if is_module_installed "${module}"; then
    # Upgrade.
    flag="-U"
  else
    # Install.
    flag="-i"
  fi

  # Find a module starting with given name and act on it.
  find "${modules}" -maxdepth 1 -type f -name "${module}*.yang" -exec \
    "${sysrepo}/bin/sysrepoctl" "${flag}" {} -s "${modules}" -v 4 \;
}

# Uninstall a module if installed.
uninstall_module() {
  module=${1}
  if ! is_module_installed "${module}"; then
    return;
  fi
  "${sysrepo}/bin/sysrepoctl" -u "${module}" -v 4
}

# Install all YANG modules in dependency order.
install_yang_modules() {
  install_kea_module 'keatest-module'
  install_kea_module 'ietf-interfaces'
  install_kea_module 'ietf-dhcpv6-common'
  install_kea_module 'ietf-dhcpv6-client'
  install_kea_module 'ietf-dhcpv6-relay'
  install_kea_module 'ietf-dhcpv6-server'
  install_kea_module 'ietf-yang-types'
  install_kea_module 'ietf-dhcpv6-options'
  install_kea_module 'ietf-dhcpv6-types'
  install_kea_module 'ietf-inet-types'
  install_kea_module 'kea-types'
  install_kea_module 'kea-dhcp-types'
  install_kea_module 'kea-dhcp-ddns'
  install_kea_module 'kea-ctrl-agent'
  install_kea_module 'kea-dhcp4-server'
  install_kea_module 'kea-dhcp6-server'
}

# Uninstall all YANG modules in reverse dependency order. Only uninstalls Kea
# modules. IETF modules might be dependencies to sysrepo internal modules.
uninstall_yang_modules() {
  uninstall_module 'kea-dhcp6-server'
  uninstall_module 'kea-dhcp4-server'
  uninstall_module 'kea-ctrl-agent'
  uninstall_module 'kea-dhcp-ddns'
  uninstall_module 'kea-dhcp-types'
  uninstall_module 'kea-types'
  uninstall_module 'keatest-module'
}

if "${uninstall_first}"; then
  uninstall_yang_modules
fi
install_yang_modules