示例#1
0
 def run(self):
     while True:
         # Get current time to retrieve state
         env, now, now_string = self.environment, datetime_now(format=None), datetime_now()
         try:
             env.auto = True  # Really better like that ;-)
             index, event = env.events.get(now, default_value={})
             print(u'[{0}] Handle scaling at index {1}.'.format(self.name, index))
             for service, stats in env.statistics.iteritems():
                 label = SERVICE_TO_LABEL.get(service, service)
                 units_api = SERVICE_TO_UNITS_API[service]
                 planned = event.get(service, None)
                 if env.enable_units_api:
                     api_client = env.api_client
                     api_client.auth = env.daemons_auth
                     units = getattr(api_client, units_api).list()
                 else:
                     units = env.get_units(service)
                 if len(units) != planned:
                     print(u'[{0}] Ensure {1} instances of service {2}'.format(self.name, planned, label))
                     env.ensure_num_units(service, service, num_units=planned)
                     env.cleanup_machines()  # Safer way to terminate machines !
                 else:
                     print(u'[{0}] Nothing to do !'.format(self.name))
                 # Recover faulty units
                 # FIXME only once and then destroy and warn admin by mail ...
                 for number, unit_dict in units.iteritems():
                     if unit_dict.get(u'agent-state') in ERROR_STATES:
                         unit = u'{0}/{1}'.format(service, number)
                         juju_do(u'resolved', environment=env.name, options=[u'--retry', unit], fail=False)
         except (ConnectionError, Timeout) as e:
             # FIXME do something here ...
             print(u'[{0}] WARNING! Communication error, details: {1}.'.format(self.name, e))
         self.sleep()
示例#2
0
 def get_unit_local_config(self, service, number, cls=None, local_config=LOCAL_CONFIG_FILENAME):
     u"""Return an instance of ``cls`` with the content of the local configuration of an instance of a charm !"""
     config_dict = juju_do(u'ssh', environment=self.environment, options=[u'{0}/{1}'.format(service, number),
                           u'sudo cat {0}'.format(get_unit_path(service, number, local_config))])
     return dict2object(cls, config_dict, inspect_constructor=False) if cls else config_dict