Skip to content

Commit

Permalink
Feature/Upload custom dashboards (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
iogbole committed Sep 8, 2020
1 parent 58e7525 commit a5c5e31
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 4 deletions.
18 changes: 15 additions & 3 deletions .github/workflows/QAConfigMyApp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
./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
2 changes: 2 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"overwrite_health_rules": false,
"are_passwords_encoded": false,
"upload_custom_dashboard": false,

"branding": [
{
"enabled": true,
Expand Down
Empty file added custom_dashboards/.gitkeep
Empty file.
97 changes: 97 additions & 0 deletions dashboards/upload_custom_dashboard.sh
Original file line number Diff line number Diff line change
@@ -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
32 changes: 31 additions & 1 deletion start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
{
Expand Down Expand Up @@ -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)"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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"


Expand Down

0 comments on commit a5c5e31

Please sign in to comment.