~ibmcharmers/layer-ibm-base/trunk

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
#!/bin/bash
source charms.reactive.sh
set -e


# Utility function to verify a downloaded resource
# :param: file name
# :param: checksum type
# :param: checksum value
function verify_curl_resource() {
  local FILE=$1
  local TYPE=$2
  local EXPECTED_SUM=$3
  local CALCULATED_SUM=""
  local PROG=""

  if [ ! -r ${FILE} ]; then
    status-set blocked "ibm-base: could not read ${FILE}"
    juju-log "Could not verify the downloaded resource. File could not be read: ${FILE}"
  fi

  # Set our checksum utility based on the requested type
  case "${TYPE}" in
    md5)
      PROG='md5sum'
      ;;
    sha256)
      PROG='sha256sum'
      ;;
    sha512)
      PROG='sha512sum'
      ;;
    *)
      status-set blocked "ibm-base: checksum type must be md5, sha215, or sha512"
      juju-log "Could not verify the downloaded resource ${FILE}. Unknown checksum type: ${TYPE}"
      return 1
  esac

  CALCULATED_SUM=`${PROG} ${FILE} | awk '{print $1}'`
  if [ "${CALCULATED_SUM}" = "${EXPECTED_SUM}" ]; then
    juju-log "Checksum verified for ${FILE}."
    return 0
  else
    status-set blocked "ibm-base: checksums did not match"
    juju-log "Checksum mismatch for ${FILE}. Expected ${EXPECTED_SUM}, got ${CALCULATED_SUM}"
    return 1
  fi
}


# Fetch curl resources if a URL is configured
@when 'config.set.curl_url'
@when_any 'config.new.curl_url' 'config.changed.curl_url' 'config.new.curl_opts' 'config.changed.curl_opts'
function fetch_curl_resource() {
  local ARCHIVE_DIR="${CHARM_DIR}/files/archives"
  local CURL_URL=$(config-get 'curl_url')
  local CURL_OPTS=$(config-get 'curl_opts')

  status-set maintenance "ibm-base: fetching resource(s)"

  mkdir -p ${ARCHIVE_DIR}
  cd ${ARCHIVE_DIR}
  # Multiple URLs may be separated by a space, so loop.
  for URL_STRING in ${CURL_URL}
  do
    # For each URL_STRING, set the url, checksum type, and checksum value.
    local URL=${URL_STRING%%\?*}        # string before the first '?'
    local FILE_NAME=${URL##*\/}         # string after the last '/'
    local SUM_STRING=${URL_STRING#*\?}  # string after the first '?'
    local SUM_TYPE=${SUM_STRING%%\=*}   # string before the first '='
    local SUM_VALUE=${SUM_STRING#*\=}   # string after the first '='

    if [ -z ${FILE_NAME} ]; then
      FILE_NAME="juju-${RANDOM}"
    fi
    curl --silent --show-error ${CURL_OPTS} -o ${FILE_NAME} ${URL}

    # Verify our resource checksum. If this fails, let verify_resource log
    # the reason and exit successfully. Exiting non-zero would fail the hook,
    # so return 0 and simply inform the user that verification failed.
    verify_curl_resource ${FILE_NAME} ${SUM_TYPE} ${SUM_VALUE} || return 0
  done
  cd -

  set_state 'ibm-base.curl.resource.fetched'
  status-set active "ibm-base: curl resource(s) fetched"
  juju-log 'Curl resource fetched'
}


# Handle license acceptance
@when 'config.changed.license_accepted'
function check_license_acceptance() {
  local LIC_ACCEPTED=$(config-get 'license_accepted')

  # compare lowercase LIC_ACCEPTED (requires bash > 4)
  if [ "${LIC_ACCEPTED,,}" = "true" ]; then
    set_state 'ibm-base.license.accepted'
    juju-log 'License accepted'
  else
    remove_state 'ibm-base.license.accepted'
    juju-log 'License NOT accepted'
  fi
}

# Main reactive entry point
reactive_handler_main