github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/acceptancetests/repository/charms/dummy-storage/hooks/config-changed (about)

     1  #!/bin/bash
     2  set -x
     3  
     4  juju-log -l INFO "Getting file fs token details."
     5  status-set maintenance "Getting file fs token details." || true
     6  
     7  function set_token() {
     8      token_name=$1           # i.e. single-fs-token
     9      fs_path=$2              # i.e. /srv/single-fs/ or /srv/multi-fs/multi-fs/13
    10  
    11      token="$(config-get $token_name)"
    12      token_file="$fs_path/token"
    13  
    14      juju-log -l INFO "Token file: $token_file"
    15  
    16      if [ -f $token_file ]; then
    17          juju-log -l INFO "Using stored token details."
    18  
    19          # If we have a token and we have a token file, check the contents if the differ update it otherwise use it.
    20          current_file_token=$(cat $token_file | grep "$token_name" | head -1 | cut -d":" -f2)
    21          if [ "$current_file_token" != "$token" ]; then
    22              token=$token
    23          else
    24              token=$(cat $token_file)
    25          fi
    26      fi
    27  
    28      if [[ -z $token ]]; then
    29          fs_token="not set"
    30          juju-log -l WARNING "$fs_token"
    31      else
    32          fs_token="$token"
    33          echo "$token" > $token_file
    34          juju-log -l INFO "$fs_token"
    35      fi
    36  
    37      # Return the token value.
    38      echo "$fs_token"
    39  }
    40  
    41  # Clear any previous tokens or state.
    42  echo > /tmp/status
    43  
    44  # First the single token
    45  single_fs_token=$(set_token "single-fs-token" "/srv/single-fs")
    46  
    47  juju-log -l INFO "Setting single token details."
    48  echo "single-fs-token:$single_fs_token" >> /tmp/status
    49  
    50  # Then the multi-fs token
    51  for unit in $(storage-list multi-fs); do
    52      fs_path=$(storage-get -s $unit | grep "^location:\ " | cut -d: -f2 | tr -d ' ')
    53      multi_fs_token=$(set_token "multi-fs-token" "$fs_path")
    54      juju-log -l INFO "Setting multi token details."
    55      unit_token_number=$(echo $unit | cut -f2 -d"/")
    56      echo "multi-fs-token/$unit_token_number:$multi_fs_token" >> /tmp/status
    57  done
    58  
    59  status-set active "Stored token: /tmp/status" || true