TT#16903 Introduce 'ngcpcfg set' and 'ngcpcfg del' functionality

root@spce:~# ngcpcfg set /etc/ngcp-config/config.yml "aaa.bbb=123"
@@ -1,4 +1,6 @@
 ---
+aaa:
+  bbb: 123

root@spce:~# ngcpcfg set /etc/ngcp-config/config.yml "aaa.bbb={'ccc','123','ddd','567'}"
@@ -1,4 +1,8 @@
 ---
+aaa:
+  bbb:
+    ccc: '123'
+    ddd: '567'

root@spce:~# ngcpcfg set /etc/ngcp-config/config.yml "aaa.bbb=['ccc','123','ddd','567']"
@@ -1,4 +1,10 @@
 ---
+aaa:
+  bbb:
+    - ccc
+    - '123'
+    - ddd
+    - '567'

root@spce:~# ngcpcfg del /etc/ngcp-config/config.yml "aaa.bbb"
@@ -1,4 +1,5 @@
 ---
+aaa: {}
 apps:
   malicious_call: no
   party_call_control:

root@spce:~# ngcpcfg del /etc/ngcp-config/config.yml "aaa"
root@spce:~#

Change-Id: Ife4810ee12c921a3e8ed5b323435802aa821fb60
changes/28/13428/12
Alexander Lutay 9 years ago
parent acfa825767
commit 4d207fc4f9

@ -20,11 +20,13 @@ scripts/build usr/share/ngcp-ngcpcfg/scripts/
scripts/check usr/share/ngcp-ngcpcfg/scripts/
scripts/clean usr/share/ngcp-ngcpcfg/scripts/
scripts/commit usr/share/ngcp-ngcpcfg/scripts/
scripts/del usr/share/ngcp-ngcpcfg/scripts/
scripts/diff usr/share/ngcp-ngcpcfg/scripts/
scripts/etckeeper usr/share/ngcp-ngcpcfg/scripts/
scripts/initialise usr/share/ngcp-ngcpcfg/scripts/
scripts/log usr/share/ngcp-ngcpcfg/scripts/
scripts/services usr/share/ngcp-ngcpcfg/scripts/
scripts/set usr/share/ngcp-ngcpcfg/scripts/
scripts/show usr/share/ngcp-ngcpcfg/scripts/
scripts/status usr/share/ngcp-ngcpcfg/scripts/
scripts/values usr/share/ngcp-ngcpcfg/scripts/

@ -319,6 +319,10 @@ doing the reverse operation of the _encrypt_ option.
Note: This feature is only available if the ngcp-ngcpcfg-locker package is
installed.
**del** <file> <option>::
Delete given <option> in <file>.
**diff** [<options_for_git_diff>]::
Show changes between ngcpcfg's Git repository and the working tree inside
@ -385,6 +389,10 @@ _--dry-run_ option is present the services won't be executed but you'll be
noticed which service files would be executed if being invoked without the
_--dry-run__ option.
**set** <file> <option>=<value>::
Set given <option> to <value> in <file>.
**show** [<change_id>]::
Display the change details for specified <change_id>. Show the latest change if

@ -59,6 +59,8 @@ Actions:
log [<opts>] show log of config changes
show [<id>] show latest config change (or <id> if specified)
clean [<opts>] clean /etc/ngcp-config folder configs/templates (see available options)
set [<opts>] set YAML option in defined file
del [<opts>] delete YAML option from defined file
" "$PN"
# display only if ngcp-ngcpcfg-ha is available
@ -114,7 +116,9 @@ case ${1:-} in
services|\
show|\
status|\
values)
values|\
set|\
del)
action "$@" ;;
--debug) export DEBUG=1 ; shift ; "$0" "$@" ;;
--no-db-sync) export NO_DB_SYNC=1 ; shift ; "$0" "$@" ;;

@ -0,0 +1,69 @@
#!/bin/bash
# Purpose: delete config option from YAML file
################################################################################
set -e
set -u
# support testsuite
FUNCTIONS="${FUNCTIONS:-/usr/share/ngcp-ngcpcfg/functions/}"
HELPER="${HELPER:-/usr/share/ngcp-ngcpcfg/helper/}"
SCRIPTS="${SCRIPTS:-/usr/share/ngcp-ngcpcfg/scripts/}"
if ! [ -r "${FUNCTIONS}"/main ] ; then
printf "Error: %s/main could not be read. Exiting.\n" "${FUNCTIONS}">&2
exit 1
fi
. "${FUNCTIONS}"/main
## functions {{{
help() {
echo "Usage: ngcpcfg del <file> <option>"
echo "Example: ngcpcfg del /etc/ngcp-config/config.yml unnecessary.option"
}
## }}}
[ "${#:-}" -ne 2 ] && (help >&2 ; exit 1)
file="$1"
option="$2"
[ -f "${file}" ] || (log_error "missing ${file}. Exiting." ; exit 1)
[ -z "${option}" ] && ( log_error "missing option to set. Exiting." ; exit 1)
log_debug "Deleting option '${option}' from '${file}'"
perl_line="delete \$yaml->[0]->{${option//./\}->\{}};"
log_debug "perl line: ${perl_line}"
tmp=$(mktemp)
log_debug "Temporary perl file: ${tmp}"
cat > "${tmp}" << EOF
use strict;
use warnings;
use YAML::Tiny;
my \$file="${file}";
my \$yaml = YAML::Tiny->read("\$file") or die "YAML file '\$file' could not be loaded";
${perl_line}
\$yaml->write(\$file) or die "Could not write YAML to \$file";
EOF
log_debug "perl -wCSD \"${tmp}\" || RC=$?"
perl -wCSD "${tmp}" || RC=$?
log_debug "ngcpcfg diff | tail -n +5 || true"
ngcpcfg diff | tail -n +5 || true
if [ -n "${DEBUG:-}" ] ; then
log_debug "Not removing temporary file ${tmp}"
else
rm -f "${tmp}"
fi
exit ${RC:-0}
## END OF FILE #################################################################

@ -0,0 +1,94 @@
#!/bin/bash
# Purpose: set config value
################################################################################
set -e
set -u
# support testsuite
FUNCTIONS="${FUNCTIONS:-/usr/share/ngcp-ngcpcfg/functions/}"
HELPER="${HELPER:-/usr/share/ngcp-ngcpcfg/helper/}"
SCRIPTS="${SCRIPTS:-/usr/share/ngcp-ngcpcfg/scripts/}"
if ! [ -r "${FUNCTIONS}"/main ] ; then
printf "Error: %s/main could not be read. Exiting.\n" "${FUNCTIONS}">&2
exit 1
fi
. "${FUNCTIONS}"/main
## functions {{{
help() {
echo "Usage: ngcpcfg set <file> <key>"
echo "Example: ngcpcfg set /etc/ngcp-config/config.yml general.maintenance=yes"
}
## }}}
[ "${#:-}" -ne 2 ] && (help >&2 ; exit 1)
file="$1"
data="$2"
RC=0
[ -f "${file}" ] || (log_error "missing ${file}. Exiting." ; exit 1)
[ -z "${data}" ] && ( log_error "missing data to set. Exiting." ; exit 1)
log_debug "Received data: ${data}"
option="${data/=*/}"
value="${data/*=/}"
[ -z "${option}" ] && ( log_error "missing option to set. Exiting." ; exit 1)
[ -z "${value}" ] && ( log_error "missing value to set. Exiting." ; exit 1)
log_debug "Saving option '${option}' value '${value}' into '${file}'"
if [[ ${value} =~ ^\'.*\'$ ]] ; then
log_debug "\$value is already quoted."
else
log_debug "\$value is NOT quoted, doing so if necessary."
case ${value} in
[0-9]*|\[*\]|\{*\})
log_debug "Do not quoting \$value for integers, arrays and hashes."
;;
*)
log_debug "Quoting \$value to prevent further Perl errors."
value="\"${value}\""
;;
esac
fi
perl_line="\$yaml->[0]->{${option//./\}->\{}}=${value};"
log_debug "perl line: ${perl_line}"
tmp=$(mktemp)
log_debug "Temporary perl file: ${tmp}"
cat > "${tmp}" << EOF
use strict;
use warnings;
use YAML::Tiny;
my \$file="${file}";
my \$yaml = YAML::Tiny->read("\$file") or die "YAML file '\$file' could not be loaded";
${perl_line}
\$yaml->write(\$file) or die "Could not write YAML to \$file";
EOF
log_debug "perl -wCSD \"${tmp}\" || RC=$?"
perl -wCSD "${tmp}" || RC=$?
if [ "${RC}" = "0" ]; then
log_debug "ngcpcfg diff | tail -n +5 || true"
ngcpcfg diff | tail -n +5 || true
fi
if [ -n "${DEBUG:-}" ] ; then
log_debug "Not removing temporary file ${tmp}"
else
rm -f "${tmp}"
fi
exit ${RC}
## END OF FILE #################################################################
Loading…
Cancel
Save