dce4697030eab67eb57517d0d6c9f2d05a44c71b
1 from .client
import watcher
2 from .delta
import get_entity_delta
5 class ModelEntity(object):
6 """An object in the Model tree"""
8 def __init__(self
, data
, model
):
9 """Initialize a new entity
11 :param data: dict of data from a watcher delta
12 :param model: The model instance in whose object tree this
19 def __getattr__(self
, name
):
20 return self
.data
[name
]
24 def __init__(self
, connection
):
25 """Instantiate a new connected Model.
27 :param connection: `juju.client.connection.Connection` instance
30 self
.connection
= connection
31 self
.observers
= set()
34 def add_observer(self
, callable_
):
35 """Register an "on-model-change" callback
37 Once a watch is started (Model.watch() is called), ``callable_``
38 will be called each time the model changes. callable_ should
39 accept the following positional arguments:
41 delta - An instance of :class:`juju.delta.EntityDelta`
42 containing the raw delta data recv'd from the Juju
45 old_obj - If the delta modifies an existing object in the model,
46 old_obj will be a copy of that object, as it was before the
47 delta was applied. Will be None if the delta creates a new
50 new_obj - A copy of the new or updated object, after the delta
51 is applied. Will be None if the delta removes an entity
54 model - The :class:`Model` itself.
57 self
.observers
.add(callable_
)
59 async def watch(self
):
60 """Start an asynchronous watch against this model.
62 See :meth:`add_observer` to register an onchange callback.
65 allwatcher
= watcher
.AllWatcher()
66 allwatcher
.connect(self
.connection
)
68 results
= await allwatcher
.Next()
69 for delta
in results
.deltas
:
70 delta
= get_entity_delta(delta
)
71 old_obj
, new_obj
= self
._apply
_delta
(delta
)
72 self
._notify
_observers
(delta
, old_obj
, new_obj
)
74 def _apply_delta(self
, delta
):
75 """Apply delta to our model state and return the a copy of the
76 affected object as it was before and after the update, e.g.:
78 old_obj, new_obj = self._apply_delta(delta)
80 old_obj may be None if the delta is for the creation of a new object,
81 e.g. a new application or unit is deployed.
83 new_obj may be if no object was created or updated, or if an object
84 was deleted as a result of the delta being applied.
87 old_obj
, new_obj
= None, None
89 if (delta
.entity
in self
.state
and
90 delta
.get_id() in self
.state
[delta
.entity
]):
91 old_obj
= self
.state
[delta
.entity
][delta
.get_id()]
92 if delta
.type == 'remove':
93 del self
.state
[delta
.entity
][delta
.get_id()]
94 return old_obj
, new_obj
96 new_obj
= self
.state
.setdefault(delta
.entity
, {})[delta
.get_id()] = (
97 self
._create
_model
_entity
(delta
))
99 return old_obj
, new_obj
101 def _create_model_entity(self
, delta
):
102 """Return an object instance representing the entity created or
106 entity_class
= delta
.get_entity_class()
107 return entity_class(delta
.data
, self
)
109 def _notify_observers(self
, delta
, old_obj
, new_obj
):
110 """Call observing callbacks, notifying them of a change in model state
112 :param delta: The raw change from the watcher
113 (:class:`juju.client.overrides.Delta`)
114 :param old_obj: The object in the model that this delta updates.
116 :param new_obj: The object in the model that is created or updated
117 by applying this delta.
120 for o
in self
.observers
:
121 o(delta
, old_obj
, new_obj
, self
)
124 self
, spec
=None, constraints
=None, disks
=None, series
=None,
126 """Start a new, empty machine and optionally a container, or add a
127 container to a machine.
129 :param str spec: Machine specification
132 (None) - starts a new machine
133 'lxc' - starts a new machine with on lxc container
134 'lxc:4' - starts a new lxc container on machine 4
135 'ssh:user@10.10.0.3' - manually provisions a machine with ssh
136 'zone=us-east-1a' - starts a machine in zone us-east-1s on AWS
137 'maas2.name' - acquire machine maas2.name on MAAS
138 :param constraints: Machine constraints
139 :type constraints: :class:`juju.Constraints`
140 :param list disks: List of disk :class:`constraints <juju.Constraints>`
141 :param str series: Series
142 :param int count: Number of machines to deploy
144 Supported container types are: lxc, lxd, kvm
146 When deploying a container to an existing machine, constraints cannot
151 add_machines
= add_machine
153 def add_relation(self
, relation1
, relation2
):
154 """Add a relation between two services.
156 :param str relation1: '<service>[:<relation_name>]'
157 :param str relation2: '<service>[:<relation_name>]'
162 def add_space(self
, name
, *cidrs
):
163 """Add a new network space.
165 Adds a new space with the given name and associates the given
166 (optional) list of existing subnet CIDRs with it.
168 :param str name: Name of the space
169 :param \*cidrs: Optional list of existing subnet CIDRs
174 def add_ssh_key(self
, key
):
175 """Add a public SSH key to this model.
177 :param str key: The public ssh key
181 add_ssh_keys
= add_ssh_key
183 def add_subnet(self
, cidr_or_id
, space
, *zones
):
184 """Add an existing subnet to this model.
186 :param str cidr_or_id: CIDR or provider ID of the existing subnet
187 :param str space: Network space with which to associate
188 :param str \*zones: Zone(s) in which the subnet resides
193 def get_backups(self
):
194 """Retrieve metadata for backups in this model.
199 def block(self
, *commands
):
200 """Add a new block to this model.
202 :param str \*commands: The commands to block. Valid values are
203 'all-changes', 'destroy-model', 'remove-object'
208 def get_blocks(self
):
209 """List blocks for this model.
214 def get_cached_images(self
, arch
=None, kind
=None, series
=None):
215 """Return a list of cached OS images.
217 :param str arch: Filter by image architecture
218 :param str kind: Filter by image kind, e.g. 'lxd'
219 :param str series: Filter by image series, e.g. 'xenial'
224 def create_backup(self
, note
=None, no_download
=False):
225 """Create a backup of this model.
227 :param str note: A note to store with the backup
228 :param bool no_download: Do not download the backup archive
229 :return str: Path to downloaded archive
234 def create_storage_pool(self
, name
, provider_type
, **pool_config
):
235 """Create or define a storage pool.
237 :param str name: Name to give the storage pool
238 :param str provider_type: Pool provider type
239 :param \*\*pool_config: key/value pool configuration pairs
245 self
, no_tail
=False, exclude_module
=None, include_module
=None,
246 include
=None, level
=None, limit
=0, lines
=10, replay
=False,
248 """Get log messages for this model.
250 :param bool no_tail: Stop after returning existing log messages
251 :param list exclude_module: Do not show log messages for these logging
253 :param list include_module: Only show log messages for these logging
255 :param list include: Only show log messages for these entities
256 :param str level: Log level to show, valid options are 'TRACE',
257 'DEBUG', 'INFO', 'WARNING', 'ERROR,
258 :param int limit: Return this many of the most recent (possibly
259 filtered) lines are shown
260 :param int lines: Yield this many of the most recent lines, and keep
262 :param bool replay: Yield the entire log, and keep yielding
263 :param list exclude: Do not show log messages for these entities
269 self
, entity_url
, service_name
=None, bind
=None, budget
=None,
270 channel
=None, config
=None, constraints
=None, force
=False,
271 num_units
=1, plan
=None, resource
=None, series
=None, storage
=None,
273 """Deploy a new service or bundle.
275 :param str entity_url: Charm or bundle url
276 :param str service_name: Name to give the service
277 :param dict bind: <charm endpoint>:<network space> pairs
278 :param dict budget: <budget name>:<limit> pairs
279 :param str channel: Charm store channel from which to retrieve
280 the charm or bundle, e.g. 'development'
281 :param dict config: Charm configuration dictionary
282 :param constraints: Service constraints
283 :type constraints: :class:`juju.Constraints`
284 :param bool force: Allow charm to be deployed to a machine running
285 an unsupported series
286 :param int num_units: Number of units to deploy
287 :param str plan: Plan under which to deploy charm
288 :param dict resource: <resource name>:<file path> pairs
289 :param str series: Series on which to deploy
290 :param dict storage: Storage constraints TODO how do these look?
291 :param str to: Placement directive, e.g.::
294 'lxc:7' - new lxc container on machine 7
295 '24/lxc/3' - lxc container 3 or machine 24
297 If None, a new machine is provisioned.
303 """Terminate all machines and resources for this model.
308 def get_backup(self
, archive_id
):
309 """Download a backup archive file.
311 :param str archive_id: The id of the archive to download
312 :return str: Path to the archive file
318 self
, num_controllers
=0, constraints
=None, series
=None, to
=None):
319 """Ensure sufficient controllers exist to provide redundancy.
321 :param int num_controllers: Number of controllers to make available
322 :param constraints: Constraints to apply to the controller machines
323 :type constraints: :class:`juju.Constraints`
324 :param str series: Series of the controller machines
325 :param list to: Placement directives for controller machines, e.g.::
328 'lxc:7' - new lxc container on machine 7
329 '24/lxc/3' - lxc container 3 or machine 24
331 If None, a new machine is provisioned.
336 def get_config(self
):
337 """Return the configuration settings for this model.
342 def get_constraints(self
):
343 """Return the machine constraints for this model.
348 def grant(self
, username
, acl
='read'):
349 """Grant a user access to this model.
351 :param str username: Username
352 :param str acl: Access control ('read' or 'write')
357 def import_ssh_key(self
, identity
):
358 """Add a public SSH key from a trusted indentity source to this model.
360 :param str identity: User identity in the form <lp|gh>:<username>
364 import_ssh_keys
= import_ssh_key
366 def get_machines(self
, machine
, utc
=False):
367 """Return list of machines in this model.
369 :param str machine: Machine id, e.g. '0'
370 :param bool utc: Display time as UTC in RFC3339 format
375 def get_shares(self
):
376 """Return list of all users with access to this model.
381 def get_spaces(self
):
382 """Return list of all known spaces, including associated subnets.
387 def get_ssh_key(self
):
388 """Return known SSH keys for this model.
392 get_ssh_keys
= get_ssh_key
394 def get_storage(self
, filesystem
=False, volume
=False):
395 """Return details of storage instances.
397 :param bool filesystem: Include filesystem storage
398 :param bool volume: Include volume storage
403 def get_storage_pools(self
, names
=None, providers
=None):
404 """Return list of storage pools.
406 :param list names: Only include pools with these names
407 :param list providers: Only include pools for these providers
412 def get_subnets(self
, space
=None, zone
=None):
413 """Return list of known subnets.
415 :param str space: Only include subnets in this space
416 :param str zone: Only include subnets in this zone
421 def remove_blocks(self
):
422 """Remove all blocks from this model.
427 def remove_backup(self
, backup_id
):
430 :param str backup_id: The id of the backup to remove
435 def remove_cached_images(self
, arch
=None, kind
=None, series
=None):
436 """Remove cached OS images.
438 :param str arch: Architecture of the images to remove
439 :param str kind: Image kind to remove, e.g. 'lxd'
440 :param str series: Image series to remove, e.g. 'xenial'
445 def remove_machine(self
, *machine_ids
):
446 """Remove a machine from this model.
448 :param str \*machine_ids: Ids of the machines to remove
452 remove_machines
= remove_machine
454 def remove_ssh_key(self
, *keys
):
455 """Remove a public SSH key(s) from this model.
457 :param str \*keys: Keys to remove
461 remove_ssh_keys
= remove_ssh_key
464 self
, bootstrap
=False, constraints
=None, archive
=None,
465 backup_id
=None, upload_tools
=False):
466 """Restore a backup archive to a new controller.
468 :param bool bootstrap: Bootstrap a new state machine
469 :param constraints: Model constraints
470 :type constraints: :class:`juju.Constraints`
471 :param str archive: Path to backup archive to restore
472 :param str backup_id: Id of backup to restore
473 :param bool upload_tools: Upload tools if bootstrapping a new machine
478 def retry_provisioning(self
):
479 """Retry provisioning for failed machines.
484 def revoke(self
, username
, acl
='read'):
485 """Revoke a user's access to this model.
487 :param str username: Username to revoke
488 :param str acl: Access control ('read' or 'write')
493 def run(self
, command
, timeout
=None):
494 """Run command on all machines in this model.
496 :param str command: The command to run
497 :param int timeout: Time to wait before command is considered failed
502 def set_config(self
, **config
):
503 """Set configuration keys on this model.
505 :param \*\*config: Config key/values
510 def set_constraints(self
, constraints
):
511 """Set machine constraints on this model.
513 :param :class:`juju.Constraints` constraints: Machine constraints
518 def get_action_output(self
, action_uuid
, wait
=-1):
519 """Get the results of an action by ID.
521 :param str action_uuid: Id of the action
522 :param int wait: Time in seconds to wait for action to complete
527 def get_action_status(self
, uuid_or_prefix
=None, name
=None):
528 """Get the status of all actions, filtered by ID, ID prefix, or action name.
530 :param str uuid_or_prefix: Filter by action uuid or prefix
531 :param str name: Filter by action name
536 def get_budget(self
, budget_name
):
537 """Get budget usage info.
539 :param str budget_name: Name of budget
544 def get_status(self
, filter_
=None, utc
=False):
545 """Return the status of the model.
547 :param str filter_: Service or unit name or wildcard ('*')
548 :param bool utc: Display time as UTC in RFC3339 format
555 self
, all_
=False, destination
=None, dry_run
=False, public
=False,
556 source
=None, stream
=None, version
=None):
557 """Copy Juju tools into this model.
559 :param bool all_: Copy all versions, not just the latest
560 :param str destination: Path to local destination directory
561 :param bool dry_run: Don't do the actual copy
562 :param bool public: Tools are for a public cloud, so generate mirrors
564 :param str source: Path to local source directory
565 :param str stream: Simplestreams stream for which to sync metadata
566 :param str version: Copy a specific major.minor version
571 def unblock(self
, *commands
):
572 """Unblock an operation that would alter this model.
574 :param str \*commands: The commands to unblock. Valid values are
575 'all-changes', 'destroy-model', 'remove-object'
580 def unset_config(self
, *keys
):
581 """Unset configuration on this model.
583 :param str \*keys: The keys to unset
588 def upgrade_gui(self
):
589 """Upgrade the Juju GUI for this model.
595 self
, dry_run
=False, reset_previous_upgrade
=False,
596 upload_tools
=False, version
=None):
597 """Upgrade Juju on all machines in a model.
599 :param bool dry_run: Don't do the actual upgrade
600 :param bool reset_previous_upgrade: Clear the previous (incomplete)
602 :param bool upload_tools: Upload local version of tools
603 :param str version: Upgrade to a specific version
608 def upload_backup(self
, archive_path
):
609 """Store a backup archive remotely in Juju.
611 :param str archive_path: Path to local archive