Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
masterworker/master/hooks/master-application-relation-joined
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
46 lines (34 sloc)
1.81 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import sys | |
sys.path.insert(0, os.path.join(os.environ['CHARM_DIR'], 'lib')) | |
from hookenv import relation_set, relation_get, log, relation_id | |
import random | |
def generateWorkerKey(): | |
"""Return a random key of length 4, | |
which will be passed as new workers join.""" | |
return ''.join(random.choice('0123456789ABCDEF') for i in range(4)) | |
def master_application_relation_joined(): | |
""" | |
https://discourse.juju.is/t/charm-hooks/1040 | |
[name]-relation-joined is run only when that remote unit is first observed by the unit. | |
It should be used to relation-set any local unit settings that can be determined using no more than the name of the | |
joining unit and the remote private-address setting, which is always available when the relation is created and is | |
by convention not deleted. | |
You should not depend upon any other relation settings in the -joined hook because they’re not guaranteed to be | |
present; if you need more information you should wait for a -changed hook that presents the right information. | |
""" | |
log(" ========= hook: master-application-relation-joined ========") | |
# Generate a worker-key | |
workerKey = generateWorkerKey() | |
# Get the remote unit name so that we can use that for a composite key. | |
remoteUnitName = os.environ.get('JUJU_REMOTE_UNIT', None) # remote_unit() | |
# Get the worker remote unit private-address for logging | |
workerAddr = relation_get('private-address', unit=remoteUnitName) | |
log(f"Joined with WORKER at private-address: {workerAddr}") | |
# Assemble the relation data. | |
relation_data = { f"{remoteUnitName}-worker-key": workerKey } | |
# Set the relation data on the relation. | |
relation_set(relation_id(), relation_settings=relation_data ) | |
if __name__ == "__main__": | |
master_application_relation_joined() |