Monday, May 19, 2014

Use JuJu "run" to discover charm relationships

Juju run is cool because it lets you execute stuff inside an anonymous hook context. So if you were say prompted with the following topology.
services:
  mediawiki:
    charm: cs:precise/mediawiki-16
    exposed: true
    relations:
      db:
      - mysql
      website:
      - siege
    units:
      mediawiki/0:
        agent-state: started
        agent-version: 1.18.3
        machine: "5"
        open-ports:
        - 80/tcp
        public-address: XXX
  mysql:
    charm: cs:precise/mysql-44
    exposed: false
    relations:
      cluster:
      - mysql
      db:
      - mediawiki
    units:
      mysql/0:
        agent-state: started
        agent-version: 1.18.3
        machine: "3"
        public-address: XXX
  siege:
    charm: cs:~dannf/precise/siege-4
    exposed: false
    relations:
      website:
      - mediawiki
    units:
      siege/0:
        agent-state: started
        agent-version: 1.18.3
        machine: "4"
        public-address: XXX
Which is just mediawiki powered by mysql with siege attached to mediawiki. Say you want to know more details mysql's relationships.
# bash shell
unit='mysql/0'
relations=$(juju run --unit ${unit} 'ls ./hooks/ | grep relation | cut -d '-' -f1 | uniq')

for rel in $relations
do
  echo "testing for relationships in $rel"
  juju run --unit ${unit} "relation-ids   ${rel} --format=json"
done

...

testing for relationships in ceph
[]
testing for relationships in cluster
["cluster:2"]
testing for relationships in db
["db:4"]
testing for relationships in ha
[]
testing for relationships in ha_relations.py
[]
testing for relationships in local
[]
testing for relationships in master
[]
testing for relationships in monitors
[]
testing for relationships in munin
[]
testing for relationships in shared
[]
testing for relationships in shared_db_relations.py
[]
testing for relationships in slave
[]
Cool, there's the DB relationship, now you can probe it.
ppetraki@:cabs-sandbox$ juju run --unit ${unit} "relation-get database ${unit} -r db:4 --format=json"
"mediawiki"

ppetraki@:cabs-sandbox$ juju run --unit ${unit} "relation-get password ${unit} -r db:4 --format=json"
"alaeyomooxaesee"