本文整理汇总了Python中juju.state.service.ServiceStateManager.get_unit_state方法的典型用法代码示例。如果您正苦于以下问题:Python ServiceStateManager.get_unit_state方法的具体用法?Python ServiceStateManager.get_unit_state怎么用?Python ServiceStateManager.get_unit_state使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类juju.state.service.ServiceStateManager
的用法示例。
在下文中一共展示了ServiceStateManager.get_unit_state方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_ip_address_for_unit
# 需要导入模块: from juju.state.service import ServiceStateManager [as 别名]
# 或者: from juju.state.service.ServiceStateManager import get_unit_state [as 别名]
def get_ip_address_for_unit(client, provider, unit_name):
"""Returns public DNS name and unit state for the service unit.
:param client: a connected zookeeper client.
:param provider: the `MachineProvider` in charge of the juju.
:param unit_name: service unit running on a machine to connect to.
:return: tuple of the DNS name and a `MachineState`.
:raises: :class:`juju.state.errors.ServiceUnitStateMachineNotAssigned`
"""
manager = ServiceStateManager(client)
service_unit = yield manager.get_unit_state(unit_name)
machine_id = yield service_unit.get_assigned_machine_id()
if machine_id is None:
raise ServiceUnitStateMachineNotAssigned(unit_name)
returnValue(
((yield service_unit.get_public_address()), service_unit))
示例2: constraints_get
# 需要导入模块: from juju.state.service import ServiceStateManager [as 别名]
# 或者: from juju.state.service.ServiceStateManager import get_unit_state [as 别名]
def constraints_get(env_config, environment, entity_names, log):
"""
Show the complete set of applicable constraints for each specified entity.
This will show the final computed values of all constraints (including
internal constraints which cannot be set directly via set-constraints).
"""
provider = environment.get_machine_provider()
client = yield provider.connect()
result = {}
try:
yield sync_environment_state(client, env_config, environment.name)
if entity_names:
msm = MachineStateManager(client)
ssm = ServiceStateManager(client)
for name in entity_names:
if name.isdigit():
kind = "machine"
entity = yield msm.get_machine_state(name)
elif "/" in name:
kind = "service unit"
entity = yield ssm.get_unit_state(name)
else:
kind = "service"
entity = yield ssm.get_service_state(name)
log.info("Fetching constraints for %s %s", kind, name)
constraints = yield entity.get_constraints()
result[name] = dict(constraints)
else:
esm = EnvironmentStateManager(client)
log.info("Fetching constraints for environment")
constraints = yield esm.get_constraints()
result = dict(constraints)
yaml.safe_dump(result, sys.stdout)
finally:
yield client.close()
示例3: get_local_unit_state
# 需要导入模块: from juju.state.service import ServiceStateManager [as 别名]
# 或者: from juju.state.service.ServiceStateManager import get_unit_state [as 别名]
def get_local_unit_state(self):
"""Return ServiceUnitState for the local service unit."""
service_state_manager = ServiceStateManager(self._client)
unit_state = yield service_state_manager.get_unit_state(self._unit_name)
returnValue(unit_state)
示例4: MachineAgent
# 需要导入模块: from juju.state.service import ServiceStateManager [as 别名]
# 或者: from juju.state.service.ServiceStateManager import get_unit_state [as 别名]
#.........这里部分代码省略.........
log.info("Machine agent started id:%s deploy:%r provider:%r" % (
self.get_machine_id(), self.deploy_factory, self.provider_type))
def download_charm(self, charm_state):
"""Retrieve a charm from the provider storage to the local machine.
Utilizes a local charm cache to avoid repeated downloading of the
same charm.
"""
log.debug("Downloading charm %s to %s",
charm_state.id, self.charms_directory)
return download_charm(
self.client, charm_state.id, self.charms_directory)
@inlineCallbacks
def watch_service_units(self, old_units, new_units):
"""Callback invoked when the assigned service units change.
"""
if old_units is None:
old_units = set()
log.debug(
"Units changed old:%s new:%s", old_units, new_units)
stopped = old_units - new_units
started = new_units - old_units
for unit_name in stopped:
log.debug("Stopping service unit: %s ...", unit_name)
try:
yield self.kill_service_unit(unit_name)
except Exception:
log.exception("Error stopping unit: %s", unit_name)
for unit_name in started:
log.debug("Starting service unit: %s ...", unit_name)
try:
yield self.start_service_unit(unit_name)
except Exception:
log.exception("Error starting unit: %s", unit_name)
@inlineCallbacks
def start_service_unit(self, service_unit_name):
"""Start a service unit on the machine.
Downloads the charm, and extract it to the service unit directory,
and launch the service unit agent within the unit directory.
"""
# Retrieve the charm state to get at the charm.
unit_state = yield self.service_state_manager.get_unit_state(
service_unit_name)
charm_id = yield unit_state.get_charm_id()
charm_state = yield self.charm_state_manager.get_charm_state(
charm_id)
# Download the charm.
bundle = yield self.download_charm(charm_state)
# Use deployment to setup the workspace and start the unit agent.
deployment = self.deploy_factory(
service_unit_name, self.config["juju_directory"])
running = yield deployment.is_running()
if not running:
log.debug("Starting service unit %s", service_unit_name)
yield deployment.start(
self.get_machine_id(), self.client.servers, bundle)
log.info("Started service unit %s", service_unit_name)
def kill_service_unit(self, service_unit_name):
"""Stop service unit and destroy disk state, ala SIGKILL or lxc-destroy
"""
deployment = self.deploy_factory(
service_unit_name, self.config["juju_directory"])
log.info("Stopping service unit %s...", service_unit_name)
return deployment.destroy()
def get_machine_id(self):
"""Get the id of the machine as known within the zk state."""
return self.config["machine_id"]
def get_agent_name(self):
return "Machine:%s" % (self.get_machine_id())
def configure(self, options):
super(MachineAgent, self).configure(options)
if not options.get("machine_id"):
msg = ("--machine-id must be provided in the command line, "
"or $JUJU_MACHINE_ID in the environment")
raise JujuError(msg)
@classmethod
def setup_options(cls, parser):
super(MachineAgent, cls).setup_options(parser)
machine_id = os.environ.get("JUJU_MACHINE_ID", "")
parser.add_argument(
"--machine-id", default=machine_id)
return parser
示例5: resolved
# 需要导入模块: from juju.state.service import ServiceStateManager [as 别名]
# 或者: from juju.state.service.ServiceStateManager import get_unit_state [as 别名]
def resolved(
config, environment, verbose, log, unit_name, relation_name, retry):
"""Mark an error as resolved in a unit or unit relation.
If one of a unit's charm non-relation hooks returns a non-zero exit
status, the entire unit can be considered to be in a non-running state.
As a resolution, the the unit can be manually returned a running state
via the juju resolved command. Optionally this command can also
rerun the failed hook.
This resolution also applies separately to each of the unit's relations.
If one of the relation-hooks failed. In that case there is no
notion of retrying (the change is gone), but resolving will allow
additional relation hooks for that relation to proceed.
"""
provider = environment.get_machine_provider()
client = yield provider.connect()
service_manager = ServiceStateManager(client)
relation_manager = RelationStateManager(client)
unit_state = yield service_manager.get_unit_state(unit_name)
service_state = yield service_manager.get_service_state(
unit_name.split("/")[0])
retry = retry and RETRY_HOOKS or NO_HOOKS
if not relation_name:
running, workflow_state = yield is_unit_running(client, unit_state)
if running:
log.info("Unit %r already running: %s", unit_name, workflow_state)
client.close()
returnValue(False)
yield unit_state.set_resolved(retry)
log.info("Marked unit %r as resolved", unit_name)
returnValue(True)
# Check for the matching relations
service_relations = yield relation_manager.get_relations_for_service(
service_state)
service_relations = [
sr for sr in service_relations if sr.relation_name == relation_name]
if not service_relations:
raise RelationStateNotFound()
# Verify the relations are in need of resolution.
resolved_relations = {}
for service_relation in service_relations:
unit_relation = yield service_relation.get_unit_state(unit_state)
running, state = yield is_relation_running(client, unit_relation)
if not running:
resolved_relations[unit_relation.internal_relation_id] = retry
if not resolved_relations:
log.warning("Matched relations are all running")
client.close()
returnValue(False)
# Mark the relations as resolved.
yield unit_state.set_relation_resolved(resolved_relations)
log.info(
"Marked unit %r relation %r as resolved", unit_name, relation_name)
client.close()
示例6: UnitDeployer
# 需要导入模块: from juju.state.service import ServiceStateManager [as 别名]
# 或者: from juju.state.service.ServiceStateManager import get_unit_state [as 别名]
class UnitDeployer(object):
"""Manages service unit deployment for an agent.
"""
def __init__(self, client, machine_id, juju_directory):
"""Initialize a Unit Deployer.
:param client: A connected zookeeper client.
:param str machine_id: the ID of the machine the agent is being run on.
:param str juju_directory: the directory the agent is running in.
"""
self.client = client
self.machine_id = machine_id
self.juju_directory = juju_directory
self.service_state_manager = ServiceStateManager(self.client)
self.charm_state_manager = CharmStateManager(self.client)
@property
def charms_directory(self):
return os.path.join(self.juju_directory, "charms")
@inlineCallbacks
def start(self, provider_type=None):
"""Starts the unit deployer."""
# Find out what provided the machine, and how to deploy units.
if provider_type is None:
settings = GlobalSettingsStateManager(self.client)
provider_type = yield settings.get_provider_type()
self.deploy_factory = get_deploy_factory(provider_type)
if not os.path.exists(self.charms_directory):
os.makedirs(self.charms_directory)
def download_charm(self, charm_state):
"""Retrieve a charm from the provider storage to the local machine.
:param charm_state: Charm to be downloaded
"""
log.debug("Downloading charm %s to %s",
charm_state.id, self.charms_directory)
return download_charm(
self.client, charm_state.id, self.charms_directory)
@inlineCallbacks
def start_service_unit(self, service_unit_name):
"""Start a service unit on the machine.
Downloads the charm, and extract it to the service unit directory,
and launch the service unit agent within the unit directory.
:param str service_unit_name: Service unit name to be started
"""
# Retrieve the charm state to get at the charm.
unit_state = yield self.service_state_manager.get_unit_state(
service_unit_name)
charm_id = yield unit_state.get_charm_id()
charm_state = yield self.charm_state_manager.get_charm_state(
charm_id)
# Download the charm.
bundle = yield self.download_charm(charm_state)
# Use deployment to setup the workspace and start the unit agent.
deployment = self.deploy_factory(
service_unit_name, self.juju_directory)
log.debug("Using %r for %s in %s",
deployment,
service_unit_name,
self.juju_directory)
running = yield deployment.is_running()
if not running:
log.debug("Starting service unit %s...", service_unit_name)
yield deployment.start(
self.machine_id, self.client.servers, bundle)
log.info("Started service unit %s", service_unit_name)
@inlineCallbacks
def kill_service_unit(self, service_unit_name):
"""Stop service unit and destroy disk state, ala SIGKILL or lxc-destroy
:param str service_unit_name: Service unit name to be killed
"""
deployment = self.deploy_factory(
service_unit_name, self.juju_directory)
log.info("Stopping service unit %s...", service_unit_name)
yield deployment.destroy()
log.info("Stopped service unit %s", service_unit_name)