Creating administration scripts for Cognos Analytics

Create administration scripts to manage deployments and to shutdown or restart Cognos Analytics.

Procedure

  1. Create the deployment.sh script to manage deployments.

    The deployment.sh script is a file with the following contents:

    #!/usr/bin/env bash
    # -----------------------------------------------------------------------------
    #      Licensed Materials - Property of IBM
    #
    #      IBM Cognos Products: ca
    #
    #      (C) Copyright IBM Corp. 2023
    #
    #      US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
    # -----------------------------------------------------------------------------
    set -e
    ​
    function usage {
      echo $0: usage: $0 [-h] -c command -d deployment -n namespace [-t tethered_namespace] -u user -p password
    }
    function help {
      usage
      echo "-h                    prints help to the console"
      echo "-c command            one of list, download, upload or delete"
      echo "-d deployment         deployment (not required for list)"
      echo "-n namespace          namespace or project"
      echo "-t tethered_namespace tethered namespace or project"
      echo "-u user               CP4D user name"
      echo "-p password           CP4D user password"
      echo
      echo "Examples:"
      echo
      echo "./deployment.sh -c list -n cpd-instance -t cat1 -u admin -p ***"
      echo "./deployment.sh -c upload -n cpd-instance -t cat1 -u admin -p *** -d your_deployment.zip"
      echo "./deployment.sh -c download -n cpd-instance -t cat1 -u admin -p *** -d your_deployment.zip"
      echo "./deployment.sh -c delete -n cpd-instance -t cat1 -u admin -p *** -d your_deployment.zip"
      echo
      exit 0
    }
    while getopts ":hc:d:n:t:u:p:" opt; do
       case ${opt} in
       h)
          help
          ;;
       c)
          deployment_cmd=$OPTARG
          ;;
       d)
          deployment=$OPTARG
          ;;
       n)
          namespace=$OPTARG
          ;;
       t)
          tethered_namespace=$OPTARG
          ;;
       u)
          user_name=$OPTARG
          ;;
       p)
          user_password=$OPTARG
          ;;
       \?)
          usage
          exit 0
          ;;
       esac
    done
    ​
    jq --version
    if [[ $? -ne 0 ]]
    then
      echo "jq is a requirement please install"
      exit 1
    fi
    ​
    if [ -z ${deployment_cmd} ]; then
      echo "A deployment command must be provided"
      help
    fi
    case ${deployment_cmd} in
      list|upload|download|delete)
        ;;
    ​
      *)
        echo "Unknown command: ${deployment_cmd}"
        help
    esac
    if [ -z ${namespace} ]; then
      echo "A namespace must be provided"
      help
    fi
    if [ -z ${tethered_namespace} ]; then
      tethered_namespace=${namespace}
      echo "A tethered namespace has not been provided, defaulting to ${tethered_namespace}"
    fi
    if [ -z ${user_name} ]; then
      echo "A user name must be provided"
      help
    fi
    if [ -z ${user_password} ]; then
      echo "A user password must be provided"
      help
    fi
    ​
    if [ "${deployment_cmd}" != "list" ]; then
      if [ -z $deployment ]; then
          echo "A deployment must be provided"
          help
      fi
    fi
    ​
    # Get CPD route and URLs
    cpd_route=$(oc get route -n ${namespace} | grep 'cpd.*ibm-nginx' | awk '{print $2}')
    cpd_url="https://"${cpd_route}
    echo "CP4D URL: ${cpd_url}"
    cpd_authurl=${cpd_url}/icp4d-api/v1/authorize
    deployments_url=${cpd_url}/cognosanalytics/${tethered_namespace}/artifacts/v1/deployments
    ​
    # Get CPD bearer token
    cpd_bearer="$( curl -s -k -X POST -H 'Content-Type: application/json'  -d "{ \"username\":\"${user_name}\", \"password\":\"${user_password}\" }" "${cpd_authurl}" | jq -r '. | .token' )"
    jwt_token="Authorization: Bearer ${cpd_bearer}"
    ​
    case ${deployment_cmd} in
      list)
        curl --silent "${deployments_url}" -H "${jwt_token}" -k | jq '.[]'
        echo
        ;;
    ​
      upload)
        if [ -r ${deployment} ]; then
          echo "Uploading ${deployment} ..."
          curl --silent -X POST -L "${deployments_url}" -H "${jwt_token}" -F "fileName=@${deployment}" -k
          echo
        else
          echo "Unable to read deployment: ${deployment}"
          exit 1
        fi
        ;;
    ​
      download)
        deployment_id=$(curl --silent "${deployments_url}" -H "${jwt_token}" -k | jq -r ".[] | select(.deploymentFileName == \"${deployment}\") | .id")
        echo "id: ${deployment_id}"
        if [ -z ${deployment_id} ]; then
          echo "Unable to find deployment ${deployment}"
          exit 1
        else
          echo "Downloading ${deployment} ..."
          wget -O ${deployment} --header="${jwt_token}" --no-check-certificate -nc --no-use-server-timestamps "${deployments_url}/${deployment_id}"
        fi
        ;;
    ​
      delete)
        deployment_id=$(curl --silent "${deployments_url}" -H "${jwt_token}" -k | jq -r ".[] | select(.deploymentFileName == \"${deployment}\") | .id")
        echo "id: ${deployment_id}"
        if [ -z ${deployment_id} ]; then
          echo "Unable to find deployment ${deployment}"
          exit 1
        else
          echo "Deleting ${deployment} ..."
          curl --request DELETE "${deployments_url}/${deployment_id}" -H "${jwt_token}" -k
        fi
        ;;
    esac
  2. Create the shutdown.sh script to shut down or restart Cognos Analytics.

    The shutdown.sh script is a file with the following contents:

    #!/usr/bin/env bash
    #
    # -----------------------------------------------------------------------------
    #         Licensed Materials - Property of IBM
    #
    #         IBM Cognos Products: ca
    #
    #         (C) Copyright IBM Corp. 2022, 2023
    #
    #         US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule
    # -----------------------------------------------------------------------------
    #
    #
    ​
    set -e
    #set -x
    ​
    restart_flag="false"
    shutdown_flag="true"
    check_status=""
    ​
    function usage {
        echo $0: usage: $0 [-h] -t tethered_namespace [-r]
    }
    ​
    function help {
        usage
        echo "-h                    prints help to the console"
        echo "-t tethered_namespace tethered namespace to shutdown/restart (required)"
        echo "-r                    restart (default: shutdown)"
        echo ""
        exit 0
    }
    ​
    while getopts ":ht:r" opt; do
         case ${opt} in
         h)
            help
            ;;
         t)
            tethered_namespace=$OPTARG
            ;;
         r)
            restart_flag="true"
            shutdown_flag="false"
            ;;
         \?)
            usage
            exit 0
            ;;
         esac
    done
    ​
    if [ -z ${tethered_namespace} ]; then
      echo "A tethered namespace must be provided"
      help
    fi
    ​
    cr_name=$(oc -n ${tethered_namespace} get caserviceinstance --no-headers -o custom-columns=NAME:.metadata.name)
    if [ -z ${cr_name} ]; then
      echo "Unable to find the CR for Cognos Analytics in this tethered namespace."
      exit 1
    fi
    ​
    # Patch shutdown option into CR
    if [ "${restart_flag}" == "true" ]; then
      echo "Restarting ${cr_name} ..."
      oc patch caserviceinstance ${cr_name} --type merge -p "{\"spec\":{\"shutdown\":\"false\"}}" -n ${tethered_namespace}
      check_status="Completed"
    fi
    if [ "${shutdown_flag}" == "true" ]; then
      echo "Shutting down ${cr_name} ..."
      oc patch caserviceinstance ${cr_name} --type merge -p "{\"spec\":{\"shutdown\":\"true\"}}" -n ${tethered_namespace}
      check_status="shutdown"
    fi
    ​
    sleep 10
    ​
    # Checking status of ca shutdown action
    for i in {1..240};do
        caStatus=$(oc get caserviceinstance ${cr_name} -o jsonpath="{.status.caStatus}" -n ${tethered_namespace})
    ​
        if [[ ${caStatus} == ${check_status} ]];then
            echo "${check_status} Successfully"
            break
        elif [[ ${caStatus} == "Failed" ]];then
            echo "${caStatus}!"
            exit 1
        fi
        echo "Status: ${caStatus}"
        sleep 30
    done