From a5c5e313c943512206a200b28a2e15cf76a2a5d4 Mon Sep 17 00:00:00 2001 From: Israel Ogbole Date: Tue, 8 Sep 2020 16:17:04 +0100 Subject: [PATCH] Feature/Upload custom dashboards (#36) --- .github/workflows/QAConfigMyApp.yml | 18 ++++- config.json | 2 + custom_dashboards/.gitkeep | 0 dashboards/upload_custom_dashboard.sh | 97 +++++++++++++++++++++++++++ start.sh | 32 ++++++++- 5 files changed, 145 insertions(+), 4 deletions(-) create mode 100644 custom_dashboards/.gitkeep create mode 100755 dashboards/upload_custom_dashboard.sh diff --git a/.github/workflows/QAConfigMyApp.yml b/.github/workflows/QAConfigMyApp.yml index 107e9bb..49e66b4 100644 --- a/.github/workflows/QAConfigMyApp.yml +++ b/.github/workflows/QAConfigMyApp.yml @@ -48,10 +48,22 @@ jobs: - name: TestCase4- Action suppression. Date and duration run: | - echo Running Action Suppression, - ./start.sh -a Jenkins_API -c configmyappdemo-2044no-uzyczrm0.appd-cx.com -p appd -u appd --suppress-action --suppress-start=$(date -d " + 5 minutes" -u +%FT%T) --suppress-duration=120 + echo Running Action Suppression + #--suppress-start=$(date -d " + 20 minutes" -u +%FT%T) + ./start.sh -a Jenkins_API -c configmyappdemo-2044no-uzyczrm0.appd-cx.com -p appd -u appd --suppress-action --suppress-duration=120 - name: TestCase5- SIM and DB run: | echo Running SIM and DB, - ./start.sh -a IoT_API -c configmyappdemo-2044no-uzyczrm0.appd-cx.com -p appd -u appd --include-database --database-name 'ConfigMyApp' --include-sim \ No newline at end of file + ./start.sh -a IoT_API -c configmyappdemo-2044no-uzyczrm0.appd-cx.com -p appd -u appd --include-database --database-name 'ConfigMyApp' --include-sim + + - name: TestCase6- Upload dashboard + run: | + echo Running Dash upload + curl https://gist.githubusercontent.com/iogbole/48e7568454b066132700c4fe039c2cff/raw/4aa417193e7ce9f3cce2410e67d525761cb6d678/gistfile1.txt -o ./custom_dashboards/CustomDashboard_vanilla.json + export CMA_UPLOAD_CUSTOM_DASHBOARD=true + ./start.sh -a IoT_API -c configmyappdemo-2044no-uzyczrm0.appd-cx.com -p appd -u appd + + + + \ No newline at end of file diff --git a/config.json b/config.json index 3c68ca4..e016a36 100644 --- a/config.json +++ b/config.json @@ -1,6 +1,8 @@ { "overwrite_health_rules": false, "are_passwords_encoded": false, + "upload_custom_dashboard": false, + "branding": [ { "enabled": true, diff --git a/custom_dashboards/.gitkeep b/custom_dashboards/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/dashboards/upload_custom_dashboard.sh b/dashboards/upload_custom_dashboard.sh new file mode 100755 index 0000000..6e77a1d --- /dev/null +++ b/dashboards/upload_custom_dashboard.sh @@ -0,0 +1,97 @@ +#!/bin/bash + +_controller_url=${1} # hostname + /controller +_user_credentials=${2} # ${username}:${password} + +_application_name=${3} +_proxy_details=${4} +_debug=${5} + +_custom_dash_dir="./custom_dashboards" #I might move this into dashboard folder.. let see.. +_temp_dash_dir="$_custom_dash_dir/temp" +templateAppName="ChangeApplicationName" +dt=$(date '+%Y-%m-%d_%H-%M-%S') + +function func_check_http_response() { + local http_message_body="$1" + local string_success_response_contains="$2" + if [[ "$http_message_body" =~ "$string_success_response_contains" ]]; then # contains + echo "" + echo "*********************************************************************" + echo "The $_dash_file dashboard was created successfull. " + echo "Please check the $_controller_url controller " + echo "*********************************************************************" + + echo "" + else + echo "${dt} ERROR "{$http_message_body}"" >>error.log + echo "ERROR $http_message_body" + exit 1 + fi +} + +#check if custom dashboards exist + +if [ "$(ls -A $_custom_dash_dir/*.json)" ]; then + echo "" + echo "Found custom JSON file(s) in $_custom_dash_dir" + ls -l $_custom_dash_dir/*.json +else + echo "" + echo "$_custom_dash_dir directory is empty. " + echo "Please add custom dashboard JSON files to the $_custom_dash_dir directory " + echo "Aborting.." + exit 1 +fi + +#check if App exist + +allApplications=$(curl -s --user ${_user_credentials} ${_controller_url}/rest/applications?output=JSON $_proxy_details) + +applicationObject=$(jq --arg appName "$_application_name" '.[] | select(.name == $appName)' <<<$allApplications) + +if [ "$applicationObject" = "" ]; then + func_check_http_response 404 "Application '"$_application_name"' not found. Aborting..." +fi + +#All conditions met.. processing dashboards. + +_dasboard_API_endpoint="CustomDashboardImportExportServlet" + +mkdir -p "$_temp_dash_dir" && cp -r "$_custom_dash_dir"/*.json "$_temp_dash_dir" + +for _dash_file in $_temp_dash_dir/*.json; do + echo "" + echo "Processing ${_dash_file} dashboard template. Using the '${_application_name}' application" + + if grep -q $templateAppName ${_dash_file}; then + sed -i -e "s/${templateAppName}/${_application_name}/g" "${_dash_file}" + else + echo "Error occured. Please ensure you replace the applicationName object in the json file with '$templateAppName' " + echo "Refer to the documentation for details. TODO [add link]" + exit 1 + fi + + sleep 2 # let it cool off + echo "" + echo "Uploading $_dash_file to ${_controller_url}... " + + if [ $_debug = true ]; then + response=$(curl -v -X POST --user ${_user_credentials} ${_controller_url}/${_dasboard_API_endpoint} -F file=@${_dash_file} ${_proxy_details}) + else + response=$(curl -s -X POST --user ${_user_credentials} ${_controller_url}/${_dasboard_API_endpoint} -F file=@${_dash_file} ${_proxy_details}) + fi + + expected_response='"success":true' + + func_check_http_response "\{$response}" $expected_response + sleep 1 + # save used uploaded files + mkdir -p "${_custom_dash_dir}/uploaded" + + cp -rf "${_dash_file}" "${_custom_dash_dir}/uploaded/${_application_name}"-"${dt}.json" + + #clean up - one at a time - to avoid re-processing + rm -rf "${_dash_file}" + +done diff --git a/start.sh b/start.sh index 05bf9cf..1c1aaea 100755 --- a/start.sh +++ b/start.sh @@ -60,6 +60,8 @@ _arg_suppress_upload_files=false _arg_suppress_name= _arg_suppress_delete= +_arg_upload_custom_dashboard=false + _arg_debug=false _arg_controller_port_explicitly_set=false @@ -75,6 +77,7 @@ _arg_use_branding_explicitly_set=false _arg_bt_only_explicitly_set=false _arg_suppress_action_explicitly_set=false _arg_suppress_upload_files_explicitly_set=false +_arg_upload_custom_dashboard_explicitly_set=false print_help() { @@ -122,6 +125,9 @@ print_help() printf '\t%s\n' "--suppress-delete: delete action suppression by passing action name to this parameter (no default)" + printf '\t%s\n' "--upload-custom-dashboard, --no-upload-custom-dashboard: creates custom dashboard(s) from (${_arg_upload_custom_dashboard} by default)" + + printf '%s\n' "Help options:" printf '\t%s\n' "-h, --help: Prints help" printf '\t%s\n' "--debug, --no-debug: Run in debug mode (${_arg_debug} by default)" @@ -347,6 +353,11 @@ parse_commandline() --suppress-delete=*) _arg_suppress_delete="${_key##--suppress-delete=}" ;; + --no-upload-custom-dashboard|--upload-custom-dashboard) + _arg_upload_custom_dashboard=true + _arg_upload_custom_dashboard_explicitly_set=true + test "${1:0:5}" = "--no-" && _arg_upload_custom_dashboard=false + ;; -h|--help) print_help exit 0 @@ -538,6 +549,11 @@ if ([ -z "${_arg_suppress_delete// }" ] && [ ! -z "${CMA_SUPPRESS_DELETE// }" ]) _arg_suppress_delete=${CMA_SUPPRESS_DELETE} fi +# upload custom dashboards +if ([ $_arg_upload_custom_dashboard_explicitly_set = false ] && [ ! -z "${CMA_UPLOAD_CUSTOM_DASHBOARD// }" ]); then + _arg_upload_custom_dashboard=${CMA_UPLOAD_CUSTOM_DASHBOARD} +fi + # 1.3 If value not set replace with configuration file values conf_file="config.json" @@ -636,6 +652,12 @@ if [[ -z "${_arg_suppress_delete// }" ]]; then _arg_suppress_delete=$(jq -r '.action_suppression[].suppress_delete' <${conf_file}) fi +# upload custom dashboards + +if ([[ $_arg_upload_custom_dashboard_explicitly_set = false ]] && [ -z "${CMA_UPLOAD_CUSTOM_DASHBOARD// }" ]); then + _arg_upload_custom_dashboard=$(jq -r '.upload_custom_dashboard' <${conf_file}) +fi + ### 2 VALIDATE ### # 2.1 Check if values are in expected ranges @@ -683,6 +705,8 @@ if [ $_arg_debug = true ]; then echo "Value of --use-branding: $_arg_use_branding" echo "Value of --logo-name: $_arg_logo_name" echo "Value of --background-name: $_arg_background_name" + + echo "Value of --upload-custom-dashboard: $_arg_upload_custom_dashboard" fi @@ -765,7 +789,13 @@ if [[ ! -z "${_arg_suppress_delete// }" ]]; then exit 0 # only delete action suppression fi -### 5 EXECUTE CMA SCRIPT ### +### 5 ACTION SUPRESSION ### +if [ $_arg_upload_custom_dashboard = true ]; then + ./dashboards/upload_custom_dashboard.sh "$_arg_controller_url" "$_arg_user_credentials" "$_arg_application_name" "$_arg_proxy_details" "$_arg_debug" + exit 0 # only upload files +fi + +### 6 EXECUTE CMA SCRIPT ### ./configMyApp.sh "$_arg_controller_url" "$_arg_user_credentials" "$_arg_proxy_details" "$_arg_application_name" "$_arg_include_database" "$_arg_database_name" "$_arg_include_sim" "$_arg_configure_bt" "$_arg_overwrite_health_rules" "$_arg_bt_only" "$_arg_use_branding" "$_arg_logo_name" "$_arg_background_name"