@when 'db.ready'
@when_not 'db.sshconfigured'
function configure_sshkeys(){
juju-log "************************In db available function ***********"
#hostname=$(relation_call --state=db.available get_remotehostname) || true
#juju-log "I got the remote host anme as $hostname"
key=$(relation_call --state=db.available get_sshkeys) || true
juju-log "I got the ssh key - $key"
dbnames=$(relation_call --state=db.available get_dbnames) || true
juju-log "I got db names - $dbnames"
if [ -z "$key" ]; then
juju-log "No data sent yet"
exit 0
fi
#code from db-relation-changed of db2 charm
# Add the key to the authorized_kesy file, if it exists
set +e
if [ -f $AUTH_KEY_FILE ]; then
# Append only if the key is not present
juju-log "IBM DB2: Updating the key file"
grep "$key" $AUTH_KEY_FILE > /dev/null
if [ $? != 0 ]; then
juju-log "IBM DB2: Updating authorized keys file"
echo "$key" >> $AUTH_KEY_FILE
else
juju-log "Key already present"
fi
else
juju-log "IBM DB2: Creating authorized keys file"
echo "$key" > $AUTH_KEY_FILE
fi
set -e
juju-log "IBM DB2: Completed updating the authorized key"
set_state 'db.sshconfigured'
}