github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/testcharms/charm-hub/charms/juju-qa-test/src/charm.py (about)

     1  #!/usr/bin/env python3
     2  # Copyright 2020 juju-qa@canonical.com
     3  # See LICENSE file for licensing details.
     4  
     5  import logging
     6  
     7  from ops.charm import CharmBase
     8  from ops.model import (
     9      ActiveStatus,
    10      ModelError,
    11      Resources,
    12  )
    13  from ops.main import main
    14  from ops.framework import StoredState
    15  from datetime import datetime
    16  
    17  logger = logging.getLogger(__name__)
    18  
    19  class ApitestUbuntuQaCharm(CharmBase):
    20      _stored = StoredState()
    21  
    22      def __init__(self, *args):
    23          logger.debug('Initializing Charm')
    24          super().__init__(*args)
    25          self.framework.observe(self.on.config_changed, self._on_config_changed)
    26          self.framework.observe(self.on.fortune_action, self._on_fortune_action)
    27          self.framework.observe(self.on.update_status, self._on_update_status)
    28          self._stored.set_default(things=[])
    29          self._stored.set_default(foo="")
    30          self._stored.set_default(status="")
    31  
    32      def _on_config_changed(self, _):
    33          current = self.model.config["thing"]
    34          if current not in self._stored.things:
    35              logger.info("found a new thing: %r", current)
    36              self._stored.things.append(current)
    37          self._check_foo_file_config()
    38          status = self.model.config["status"]
    39          if status != self._stored.status:
    40              logger.info("found a new status: %r", status)
    41              self._stored.status = status
    42              self.unit.status = ActiveStatus(status)
    43  
    44      def _on_fortune_action(self, event):
    45          fail = event.params["fail"]
    46          if fail:
    47              event.fail(fail)
    48          else:
    49              event.set_results({"fortune": "A bug in the code is worth two in the documentation."})
    50  
    51      def _on_update_status(self, _):
    52          now = datetime.now()
    53          date_time = now.strftime("%m/%d/%Y, %H:%M:%S")
    54          details = "it is now: {0}".format(date_time)
    55          self.unit.status = ActiveStatus(details)
    56  
    57      def _check_foo_file_config(self):
    58          if self.model.config["foo-file"] == False:
    59              self._stored.foo = ""
    60              return
    61          try:
    62             path = self.model.resources.fetch("foo-file")
    63          except ModelError as me:
    64              logger.debug("failed to fetch resource")
    65              return
    66          f = open(path, "r")
    67          line = f.readline()
    68          if not line:
    69              logger.info("resource file empty")
    70              return
    71          if line in self._stored.foo:
    72              logger.info("already printed first line")
    73              return
    74          self._stored.foo = line
    75          f.close()
    76          status = "resource line one: {}".format( line.strip())
    77          self.unit.status = ActiveStatus(status)
    78  
    79  
    80  if __name__ == "__main__":
    81      main(ApitestUbuntuQaCharm)