本文整理汇总了Python中juju.state.service.ServiceStateManager.watch_service_states方法的典型用法代码示例。如果您正苦于以下问题:Python ServiceStateManager.watch_service_states方法的具体用法?Python ServiceStateManager.watch_service_states怎么用?Python ServiceStateManager.watch_service_states使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类juju.state.service.ServiceStateManager
的用法示例。
在下文中一共展示了ServiceStateManager.watch_service_states方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ProvisioningAgent
# 需要导入模块: from juju.state.service import ServiceStateManager [as 别名]
# 或者: from juju.state.service.ServiceStateManager import watch_service_states [as 别名]
class ProvisioningAgent(BaseAgent):
name = "juju-provisoning-agent"
_current_machines = ()
# time in seconds
machine_check_period = 60
def get_agent_name(self):
return "provision:%s" % (self.environment.type)
@inlineCallbacks
def start(self):
self._running = True
self.environment = yield self.configure_environment()
self.provider = self.environment.get_machine_provider()
self.machine_state_manager = MachineStateManager(self.client)
self.service_state_manager = ServiceStateManager(self.client)
self.firewall_manager = FirewallManager(
self.client, self.is_running, self.provider)
if self.get_watch_enabled():
self.machine_state_manager.watch_machine_states(
self.watch_machine_changes)
self.service_state_manager.watch_service_states(
self.firewall_manager.watch_service_changes)
from twisted.internet import reactor
reactor.callLater(
self.machine_check_period, self.periodic_machine_check)
log.info("Started provisioning agent")
else:
log.info("Started provisioning agent without watches enabled")
def stop(self):
log.info("Stopping provisioning agent")
self._running = False
return succeed(True)
def is_running(self):
"""Whether this agent is running or not."""
return self._running
@inlineCallbacks
def configure_environment(self):
"""The provisioning agent configure its environment on start or change.
The environment contains the configuration th agent needs to interact
with its machine provider, in order to do its work. This configuration
data is deployed lazily over an encrypted connection upon first usage.
The agent waits for this data to exist before completing its startup.
"""
try:
get_d, watch_d = self.client.get_and_watch("/environment")
environment_data, stat = yield get_d
watch_d.addCallback(self._on_environment_changed)
except NoNodeException:
# Wait till the environment node appears. play twisted gymnastics
exists_d, watch_d = self.client.exists_and_watch("/environment")
stat = yield exists_d
if stat:
environment = yield self.configure_environment()
else:
watch_d.addCallback(
lambda result: self.configure_environment())
if not stat:
environment = yield watch_d
returnValue(environment)
config = EnvironmentsConfig()
config.parse(environment_data)
returnValue(config.get_default())
@inlineCallbacks
def _on_environment_changed(self, event):
"""Reload the environment if its data changes."""
if event.type_name == "deleted":
return
self.environment = yield self.configure_environment()
self.provider = self.environment.get_machine_provider()
def periodic_machine_check(self):
"""A periodic checking of machine states and provider machines.
In addition to the on demand changes to zookeeper states that are
monitored by L{watch_machine_changes}, the periodic machine check
performs non zookeeper state related verification by periodically
checking the last current provider machine states against the
last known zookeeper state.
Primarily this helps in recovering from transient error conditions
which may have prevent processing of an individual machine state, as
well as verifying the current state of the provider's running machines
against the zk state, thus pruning unused resources.
"""
from twisted.internet import reactor
#.........这里部分代码省略.........
示例2: FirewallTestBase
# 需要导入模块: from juju.state.service import ServiceStateManager [as 别名]
# 或者: from juju.state.service.ServiceStateManager import watch_service_states [as 别名]
class FirewallTestBase(ServiceStateManagerTestBase):
@inlineCallbacks
def setUp(self):
yield super(FirewallTestBase, self).setUp()
self._running = True
self.environment = yield self.get_test_environment()
self.provider = self.environment.get_machine_provider()
self.firewall_manager = FirewallManager(self.client, self.is_running, self.provider)
self.service_state_manager = ServiceStateManager(self.client)
self.output = self.capture_logging(level=logging.DEBUG)
def get_test_environment_config(self):
sample_config = SAMPLE_ENV % self.makeDir()
config = EnvironmentsConfig()
config.parse(sample_config)
return config
def get_test_environment(self):
return self.get_test_environment_config().get_default()
# The following methods are used to provide the scaffolding given
# by the provisioning agent, which normally runs the FirewallManager
def is_running(self):
return self._running
def start(self):
self.service_state_manager.watch_service_states(
self.firewall_manager.watch_service_changes)
def stop(self):
self._running = False
@inlineCallbacks
def provide_machine(self, machine_state):
machines = yield self.provider.start_machine(
{"machine-id": machine_state.id})
instance_id = machines[0].instance_id
yield machine_state.set_instance_id(instance_id)
def wait_on_expected_units(self, expected):
"""Returns deferred for waiting on `expected` unit names.
These unit names may require the firewall to have ports opened
and/or closed.
"""
condition_met = Deferred()
seen = set()
def observer(unit_state):
unit_name = unit_state.unit_name
seen.add(unit_name)
if seen >= expected:
# Call the callback just once, since it is possible
# for this condition to be satisfied multiple times in
# using tests because of background activity
if not condition_met.called:
condition_met.callback(True)
return succeed(True)
self.firewall_manager.add_open_close_ports_observer(observer)
return condition_met
def wait_on_expected_machines(self, expected):
"""Returns deferred for waiting on `expected` machine IDs.
These machines may require the firewall to have ports opened
and/or closed.
"""
condition_met = Deferred()
seen = set()
def observer(machine_id):
seen.add(machine_id)
if seen >= expected:
# Call the callback just once, since it is possible
# for this condition to be satisfied multiple times in
# using tests because of background activity
if not condition_met.called:
condition_met.callback(True)
return succeed(True)
self.firewall_manager.add_open_close_ports_on_machine_observer(
observer)
return condition_met