Skip to content

Commit

Permalink
calculate agent.conf path from self.unit.name
Browse files Browse the repository at this point in the history
  • Loading branch information
barrettj12 committed Oct 6, 2023
1 parent f55ae11 commit 4b664fe
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def _on_dashboard_relation_joined(self, event):
def _on_website_relation_joined(self, event):
"""Connect a website relation."""
logger.info("got a new website relation: %r", event)
port = api_port()
port = self.api_port()
if port is None:
logger.error("machine does not appear to be a controller")
self.unit.status = BlockedStatus('machine does not appear to be a controller')
Expand Down Expand Up @@ -90,7 +90,7 @@ def _on_metrics_endpoint_relation_created(self, event: RelationJoinedEvent):
"password": password,
},
"tls_config": {
"ca_file": ca_cert(),
"ca_file": self.ca_cert(),
"server_name": "juju-apiserver",
},
}],
Expand All @@ -103,29 +103,29 @@ def _on_metrics_endpoint_relation_broken(self, event: RelationDepartedEvent):
self.control_socket.remove_metrics_user(username)


def _agent_conf(key: str):
'''
_agent_conf reads a value from the agent.conf file on disk.
If the machine does not appear to be a Juju controller, then None is
returned.
'''
# TODO: get the unit number/name from an environment variable?
path = '/var/lib/juju/agents/unit-controller-0/agent.conf'
with open(path) as f:
params = yaml.safe_load(f)
return params.get(key)
def _agent_conf(self, key: str):
'''
_agent_conf reads a value from the agent.conf file on disk.
'''
unit_name = self.unit.name.replace('/', '-')
agent_conf_path = f'/var/lib/juju/agents/unit-{unit_name}/agent.conf'

def api_port() -> str:
'''
api_port returns the port on which the controller API server is listening.
'''
return _agent_conf('apiport')
with open(agent_conf_path) as agent_conf_file:
agent_conf = yaml.safe_load(agent_conf_file)
return agent_conf.get(key)

def api_port(self) -> str:
'''
api_port returns the port on which the controller API server is listening.
'''
return self._agent_conf('apiport')

def ca_cert(self) -> str:
'''
ca_cert returns the controller's CA certificate.
'''
return self._agent_conf('cacert')

def ca_cert() -> str:
'''
ca_cert returns the controller's CA certificate.
'''
return _agent_conf('cacert')

def metrics_username(relation: Relation) -> str:
'''
Expand Down

0 comments on commit 4b664fe

Please sign in to comment.