0ba79df6c9762422d03ee8aa860a0a4ddf2f3cc8
7 from concurrent
.futures
import CancelledError
8 from functools
import partial
9 from pathlib
import Path
12 from theblues
import charmstore
14 from .client
import client
15 from .client
import watcher
16 from .client
import connection
17 from .constraints
import parse
as parse_constraints
18 from .delta
import get_entity_delta
19 from .delta
import get_entity_class
20 from .exceptions
import DeadEntityException
21 from .errors
import JujuAPIError
23 log
= logging
.getLogger(__name__
)
26 class _Observer(object):
27 """Wrapper around an observer callable.
29 This wrapper allows filter criteria to be associated with the
30 callable so that it's only called for changes that meet the criteria.
33 def __init__(self
, callable_
, entity_type
, action
, entity_id
, predicate
):
34 self
.callable_
= callable_
35 self
.entity_type
= entity_type
37 self
.entity_id
= entity_id
38 self
.predicate
= predicate
40 self
.entity_id
= str(self
.entity_id
)
41 if not self
.entity_id
.startswith('^'):
42 self
.entity_id
= '^' + self
.entity_id
43 if not self
.entity_id
.endswith('$'):
46 async def __call__(self
, delta
, old
, new
, model
):
47 await self
.callable_(delta
, old
, new
, model
)
49 def cares_about(self
, delta
):
50 """Return True if this observer "cares about" (i.e. wants to be
51 called) for a this delta.
54 if (self
.entity_id
and delta
.get_id() and
55 not re
.match(self
.entity_id
, str(delta
.get_id()))):
58 if self
.entity_type
and self
.entity_type
!= delta
.entity
:
61 if self
.action
and self
.action
!= delta
.type:
64 if self
.predicate
and not self
.predicate(delta
):
70 class ModelObserver(object):
71 async def __call__(self
, delta
, old
, new
, model
):
72 handler_name
= 'on_{}_{}'.format(delta
.entity
, delta
.type)
73 method
= getattr(self
, handler_name
, self
.on_change
)
74 await method(delta
, old
, new
, model
)
76 async def on_change(self
, delta
, old
, new
, model
):
80 class ModelState(object):
81 """Holds the state of the model, including the delta history of all
82 entities in the model.
85 def __init__(self
, model
):
89 def _live_entity_map(self
, entity_type
):
90 """Return an id:Entity map of all the living entities of
95 entity_id
: self
.get_entity(entity_type
, entity_id
)
96 for entity_id
, history
in self
.state
.get(entity_type
, {}).items()
97 if history
[-1] is not None
101 def applications(self
):
102 """Return a map of application-name:Application for all applications
103 currently in the model.
106 return self
._live
_entity
_map
('application')
110 """Return a map of machine-id:Machine for all machines currently in
114 return self
._live
_entity
_map
('machine')
118 """Return a map of unit-id:Unit for all units currently in
122 return self
._live
_entity
_map
('unit')
124 def entity_history(self
, entity_type
, entity_id
):
125 """Return the history deque for an entity.
128 return self
.state
[entity_type
][entity_id
]
130 def entity_data(self
, entity_type
, entity_id
, history_index
):
131 """Return the data dict for an entity at a specific index of its
135 return self
.entity_history(entity_type
, entity_id
)[history_index
]
137 def apply_delta(self
, delta
):
138 """Apply delta to our state and return a copy of the
139 affected object as it was before and after the update, e.g.:
141 old_obj, new_obj = self.apply_delta(delta)
143 old_obj may be None if the delta is for the creation of a new object,
144 e.g. a new application or unit is deployed.
146 new_obj will never be None, but may be dead (new_obj.dead == True)
147 if the object was deleted as a result of the delta being applied.
152 .setdefault(delta
.entity
, {})
153 .setdefault(delta
.get_id(), collections
.deque())
156 history
.append(delta
.data
)
157 if delta
.type == 'remove':
160 entity
= self
.get_entity(delta
.entity
, delta
.get_id())
161 return entity
.previous(), entity
164 self
, entity_type
, entity_id
, history_index
=-1, connected
=True):
165 """Return an object instance for the given entity_type and id.
167 By default the object state matches the most recent state from
168 Juju. To get an instance of the object in an older state, pass
169 history_index, an index into the history deque for the entity.
173 if history_index
< 0 and history_index
!= -1:
174 history_index
+= len(self
.entity_history(entity_type
, entity_id
))
175 if history_index
< 0:
179 self
.entity_data(entity_type
, entity_id
, history_index
)
183 entity_class
= get_entity_class(entity_type
)
185 entity_id
, self
.model
, history_index
=history_index
,
189 class ModelEntity(object):
190 """An object in the Model tree"""
192 def __init__(self
, entity_id
, model
, history_index
=-1, connected
=True):
193 """Initialize a new entity
195 :param entity_id str: The unique id of the object in the model
196 :param model: The model instance in whose object tree this
198 :history_index int: The index of this object's state in the model's
199 history deque for this entity
200 :connected bool: Flag indicating whether this object gets live updates
204 self
.entity_id
= entity_id
206 self
._history
_index
= history_index
207 self
.connected
= connected
208 self
.connection
= model
.connection
210 def __getattr__(self
, name
):
211 """Fetch object attributes from the underlying data dict held in the
215 if self
.data
is None:
216 raise DeadEntityException(
217 "Entity {}:{} is dead - its attributes can no longer be "
218 "accessed. Use the .previous() method on this object to get "
219 "a copy of the object at its previous state.".format(
220 self
.entity_type
, self
.entity_id
))
221 return self
.data
[name
]
224 return bool(self
.data
)
226 def on_change(self
, callable_
):
227 """Add a change observer to this entity.
230 self
.model
.add_observer(
231 callable_
, self
.entity_type
, 'change', self
.entity_id
)
233 def on_remove(self
, callable_
):
234 """Add a remove observer to this entity.
237 self
.model
.add_observer(
238 callable_
, self
.entity_type
, 'remove', self
.entity_id
)
241 def entity_type(self
):
242 """A string identifying the entity type of this object, e.g.
243 'application' or 'unit', etc.
246 return self
.__class
__.__name
__.lower()
250 """Return True if this object represents the current state of the
251 entity in the underlying model.
253 This will be True except when the object represents an entity at a
254 non-latest state in history, e.g. if the object was obtained by calling
255 .previous() on another object.
258 return self
._history
_index
== -1
262 """Returns True if this entity no longer exists in the underlying
268 self
.model
.state
.entity_data(
269 self
.entity_type
, self
.entity_id
, -1) is None
274 """Returns True if this entity still exists in the underlying
282 """The data dictionary for this entity.
285 return self
.model
.state
.entity_data(
286 self
.entity_type
, self
.entity_id
, self
._history
_index
)
289 """Return a copy of this object as was at its previous state in
292 Returns None if this object is new (and therefore has no history).
294 The returned object is always "disconnected", i.e. does not receive
298 return self
.model
.state
.get_entity(
299 self
.entity_type
, self
.entity_id
, self
._history
_index
- 1,
303 """Return a copy of this object at its next state in
306 Returns None if this object is already the latest.
308 The returned object is "disconnected", i.e. does not receive
309 live updates, unless it is current (latest).
312 if self
._history
_index
== -1:
315 new_index
= self
._history
_index
+ 1
317 new_index
== len(self
.model
.state
.entity_history(
318 self
.entity_type
, self
.entity_id
)) - 1
320 return self
.model
.state
.get_entity(
321 self
.entity_type
, self
.entity_id
, self
._history
_index
- 1,
325 """Return a copy of this object at its current state in the model.
327 Returns self if this object is already the latest.
329 The returned object is always "connected", i.e. receives
330 live updates from the model.
333 if self
._history
_index
== -1:
336 return self
.model
.state
.get_entity(self
.entity_type
, self
.entity_id
)
340 def __init__(self
, loop
=None):
341 """Instantiate a new connected Model.
343 :param loop: an asyncio event loop
346 self
.loop
= loop
or asyncio
.get_event_loop()
347 self
.connection
= None
348 self
.observers
= weakref
.WeakValueDictionary()
349 self
.state
= ModelState(self
)
351 self
._watcher
_task
= None
352 self
._watch
_shutdown
= asyncio
.Event(loop
=loop
)
353 self
._watch
_received
= asyncio
.Event(loop
=loop
)
354 self
._charmstore
= CharmStore(self
.loop
)
356 async def connect(self
, *args
, **kw
):
357 """Connect to an arbitrary Juju model.
359 args and kw are passed through to Connection.connect()
362 self
.connection
= await connection
.Connection
.connect(*args
, **kw
)
363 await self
._after
_connect
()
365 async def connect_current(self
):
366 """Connect to the current Juju model.
369 self
.connection
= await connection
.Connection
.connect_current()
370 await self
._after
_connect
()
372 async def connect_model(self
, model_name
):
373 """Connect to a specific Juju model by name.
375 :param model_name: Format [controller:][user/]model
378 self
.connection
= await connection
.Connection
.connect_model(model_name
)
379 await self
._after
_connect
()
381 async def _after_connect(self
):
382 """Run initialization steps after connecting to websocket.
386 await self
._watch
_received
.wait()
387 await self
.get_info()
389 async def disconnect(self
):
390 """Shut down the watcher task and close websockets.
393 self
._stop
_watching
()
394 if self
.connection
and self
.connection
.is_open
:
395 await self
._watch
_shutdown
.wait()
396 log
.debug('Closing model connection')
397 await self
.connection
.close()
398 self
.connection
= None
400 def all_units_idle(self
):
401 """Return True if all units are idle.
404 for unit
in self
.units
.values():
405 unit_status
= unit
.data
['agent-status']['current']
406 if unit_status
!= 'idle':
410 async def reset(self
, force
=False):
411 """Reset the model to a clean state.
413 :param bool force: Force-terminate machines.
415 This returns only after the model has reached a clean state. "Clean"
416 means no applications or machines exist in the model.
419 log
.debug('Resetting model')
420 for app
in self
.applications
.values():
422 for machine
in self
.machines
.values():
423 await machine
.destroy(force
=force
)
424 await self
.block_until(
425 lambda: len(self
.machines
) == 0
428 async def block_until(self
, *conditions
, timeout
=None):
429 """Return only after all conditions are true.
433 while not all(c() for c
in conditions
):
434 await asyncio
.sleep(0)
435 await asyncio
.wait_for(_block(), timeout
)
438 def applications(self
):
439 """Return a map of application-name:Application for all applications
440 currently in the model.
443 return self
.state
.applications
447 """Return a map of machine-id:Machine for all machines currently in
451 return self
.state
.machines
455 """Return a map of unit-id:Unit for all units currently in
459 return self
.state
.units
461 async def get_info(self
):
462 """Return a client.ModelInfo object for this Model.
464 Retrieves latest info for this Model from the api server. The
465 return value is cached on the Model.info attribute so that the
466 valued may be accessed again without another api call, if
469 This method is called automatically when the Model is connected,
470 resulting in Model.info being initialized without requiring an
471 explicit call to this method.
474 facade
= client
.ClientFacade()
475 facade
.connect(self
.connection
)
477 self
.info
= await facade
.ModelInfo()
478 log
.debug('Got ModelInfo: %s', vars(self
.info
))
483 self
, callable_
, entity_type
=None, action
=None, entity_id
=None,
485 """Register an "on-model-change" callback
487 Once the model is connected, ``callable_``
488 will be called each time the model changes. callable_ should
489 be Awaitable and accept the following positional arguments:
491 delta - An instance of :class:`juju.delta.EntityDelta`
492 containing the raw delta data recv'd from the Juju
495 old_obj - If the delta modifies an existing object in the model,
496 old_obj will be a copy of that object, as it was before the
497 delta was applied. Will be None if the delta creates a new
500 new_obj - A copy of the new or updated object, after the delta
501 is applied. Will be None if the delta removes an entity
504 model - The :class:`Model` itself.
506 Events for which ``callable_`` is called can be specified by passing
507 entity_type, action, and/or id_ filter criteria, e.g.:
510 myfunc, entity_type='application', action='add', id_='ubuntu')
512 For more complex filtering conditions, pass a predicate function. It
513 will be called with a delta as its only argument. If the predicate
514 function returns True, the callable_ will be called.
517 observer
= _Observer(
518 callable_
, entity_type
, action
, entity_id
, predicate
)
519 self
.observers
[observer
] = callable_
522 """Start an asynchronous watch against this model.
524 See :meth:`add_observer` to register an onchange callback.
527 async def _start_watch():
528 self
._watch
_shutdown
.clear()
530 allwatcher
= watcher
.AllWatcher()
531 self
._watch
_conn
= await self
.connection
.clone()
532 allwatcher
.connect(self
._watch
_conn
)
534 results
= await allwatcher
.Next()
535 for delta
in results
.deltas
:
536 delta
= get_entity_delta(delta
)
537 old_obj
, new_obj
= self
.state
.apply_delta(delta
)
538 # XXX: Might not want to shield at this level
539 # We are shielding because when the watcher is
540 # canceled (on disconnect()), we don't want all of
541 # its children (every observer callback) to be
542 # canceled with it. So we shield them. But this means
543 # they can *never* be canceled.
544 await asyncio
.shield(
545 self
._notify
_observers
(delta
, old_obj
, new_obj
))
546 self
._watch
_received
.set()
547 except CancelledError
:
548 log
.debug('Closing watcher connection')
549 await self
._watch
_conn
.close()
550 self
._watch
_shutdown
.set()
551 self
._watch
_conn
= None
553 log
.debug('Starting watcher task')
554 self
._watcher
_task
= self
.loop
.create_task(_start_watch())
556 def _stop_watching(self
):
557 """Stop the asynchronous watch against this model.
560 log
.debug('Stopping watcher task')
561 if self
._watcher
_task
:
562 self
._watcher
_task
.cancel()
564 async def _notify_observers(self
, delta
, old_obj
, new_obj
):
565 """Call observing callbacks, notifying them of a change in model state
567 :param delta: The raw change from the watcher
568 (:class:`juju.client.overrides.Delta`)
569 :param old_obj: The object in the model that this delta updates.
571 :param new_obj: The object in the model that is created or updated
572 by applying this delta.
575 if new_obj
and not old_obj
:
579 'Model changed: %s %s %s',
580 delta
.entity
, delta
.type, delta
.get_id())
582 for o
in self
.observers
:
583 if o
.cares_about(delta
):
584 asyncio
.ensure_future(o(delta
, old_obj
, new_obj
, self
))
586 async def _wait(self
, entity_type
, entity_id
, action
, predicate
=None):
588 Block the calling routine until a given action has happened to the
591 :param entity_type: The entity's type.
592 :param entity_id: The entity's id.
593 :param action: the type of action (e.g., 'add' or 'change')
594 :param predicate: optional callable that must take as an
595 argument a delta, and must return a boolean, indicating
596 whether the delta contains the specific action we're looking
597 for. For example, you might check to see whether a 'change'
598 has a 'completed' status. See the _Observer class for details.
601 q
= asyncio
.Queue(loop
=self
.loop
)
603 async def callback(delta
, old
, new
, model
):
604 await q
.put(delta
.get_id())
606 self
.add_observer(callback
, entity_type
, action
, entity_id
, predicate
)
607 entity_id
= await q
.get()
608 return self
.state
._live
_entity
_map
(entity_type
)[entity_id
]
610 async def _wait_for_new(self
, entity_type
, entity_id
=None, predicate
=None):
611 """Wait for a new object to appear in the Model and return it.
613 Waits for an object of type ``entity_type`` with id ``entity_id``.
614 If ``entity_id`` is ``None``, it will wait for the first new entity
617 This coroutine blocks until the new object appears in the model.
620 # if the entity is already in the model, just return it
621 if entity_id
in self
.state
._live
_entity
_map
(entity_type
):
622 return self
.state
._live
_entity
_map
(entity_type
)[entity_id
]
623 # if we know the entity_id, we can trigger on any action that puts
624 # the enitty into the model; otherwise, we have to watch for the
625 # next "add" action on that entity_type
626 action
= 'add' if entity_id
is None else None
627 return await self
._wait
(entity_type
, entity_id
, action
, predicate
)
629 async def wait_for_action(self
, action_id
):
630 """Given an action, wait for it to complete."""
632 if action_id
.startswith("action-"):
633 # if we've been passed action.tag, transform it into the
634 # id that the api deltas will use.
635 action_id
= action_id
[7:]
637 def predicate(delta
):
638 return delta
.data
['status'] in ('completed', 'failed')
640 return await self
._wait
('action', action_id
, 'change', predicate
)
643 self
, spec
=None, constraints
=None, disks
=None, series
=None,
645 """Start a new, empty machine and optionally a container, or add a
646 container to a machine.
648 :param str spec: Machine specification
651 (None) - starts a new machine
652 'lxc' - starts a new machine with on lxc container
653 'lxc:4' - starts a new lxc container on machine 4
654 'ssh:user@10.10.0.3' - manually provisions a machine with ssh
655 'zone=us-east-1a' - starts a machine in zone us-east-1s on AWS
656 'maas2.name' - acquire machine maas2.name on MAAS
657 :param constraints: Machine constraints
658 :type constraints: :class:`juju.Constraints`
659 :param list disks: List of disk :class:`constraints <juju.Constraints>`
660 :param str series: Series
661 :param int count: Number of machines to deploy
663 Supported container types are: lxc, lxd, kvm
665 When deploying a container to an existing machine, constraints cannot
670 add_machines
= add_machine
672 async def add_relation(self
, relation1
, relation2
):
673 """Add a relation between two applications.
675 :param str relation1: '<application>[:<relation_name>]'
676 :param str relation2: '<application>[:<relation_name>]'
679 app_facade
= client
.ApplicationFacade()
680 app_facade
.connect(self
.connection
)
683 'Adding relation %s <-> %s', relation1
, relation2
)
686 result
= await app_facade
.AddRelation([relation1
, relation2
])
687 except JujuAPIError
as e
:
688 if 'relation already exists' not in e
.message
:
691 'Relation %s <-> %s already exists', relation1
, relation2
)
692 # TODO: if relation already exists we should return the
693 # Relation ModelEntity here
696 def predicate(delta
):
698 for endpoint
in delta
.data
['endpoints']:
699 endpoints
[endpoint
['application-name']] = endpoint
['relation']
700 return endpoints
== result
.endpoints
702 return await self
._wait
_for
_new
('relation', None, predicate
)
704 def add_space(self
, name
, *cidrs
):
705 """Add a new network space.
707 Adds a new space with the given name and associates the given
708 (optional) list of existing subnet CIDRs with it.
710 :param str name: Name of the space
711 :param \*cidrs: Optional list of existing subnet CIDRs
716 def add_ssh_key(self
, key
):
717 """Add a public SSH key to this model.
719 :param str key: The public ssh key
723 add_ssh_keys
= add_ssh_key
725 def add_subnet(self
, cidr_or_id
, space
, *zones
):
726 """Add an existing subnet to this model.
728 :param str cidr_or_id: CIDR or provider ID of the existing subnet
729 :param str space: Network space with which to associate
730 :param str \*zones: Zone(s) in which the subnet resides
735 def get_backups(self
):
736 """Retrieve metadata for backups in this model.
741 def block(self
, *commands
):
742 """Add a new block to this model.
744 :param str \*commands: The commands to block. Valid values are
745 'all-changes', 'destroy-model', 'remove-object'
750 def get_blocks(self
):
751 """List blocks for this model.
756 def get_cached_images(self
, arch
=None, kind
=None, series
=None):
757 """Return a list of cached OS images.
759 :param str arch: Filter by image architecture
760 :param str kind: Filter by image kind, e.g. 'lxd'
761 :param str series: Filter by image series, e.g. 'xenial'
766 def create_backup(self
, note
=None, no_download
=False):
767 """Create a backup of this model.
769 :param str note: A note to store with the backup
770 :param bool no_download: Do not download the backup archive
771 :return str: Path to downloaded archive
776 def create_storage_pool(self
, name
, provider_type
, **pool_config
):
777 """Create or define a storage pool.
779 :param str name: Name to give the storage pool
780 :param str provider_type: Pool provider type
781 :param \*\*pool_config: key/value pool configuration pairs
787 self
, no_tail
=False, exclude_module
=None, include_module
=None,
788 include
=None, level
=None, limit
=0, lines
=10, replay
=False,
790 """Get log messages for this model.
792 :param bool no_tail: Stop after returning existing log messages
793 :param list exclude_module: Do not show log messages for these logging
795 :param list include_module: Only show log messages for these logging
797 :param list include: Only show log messages for these entities
798 :param str level: Log level to show, valid options are 'TRACE',
799 'DEBUG', 'INFO', 'WARNING', 'ERROR,
800 :param int limit: Return this many of the most recent (possibly
801 filtered) lines are shown
802 :param int lines: Yield this many of the most recent lines, and keep
804 :param bool replay: Yield the entire log, and keep yielding
805 :param list exclude: Do not show log messages for these entities
811 self
, entity_url
, service_name
=None, bind
=None, budget
=None,
812 channel
=None, config
=None, constraints
=None, force
=False,
813 num_units
=1, plan
=None, resources
=None, series
=None, storage
=None,
815 """Deploy a new service or bundle.
817 :param str entity_url: Charm or bundle url
818 :param str service_name: Name to give the service
819 :param dict bind: <charm endpoint>:<network space> pairs
820 :param dict budget: <budget name>:<limit> pairs
821 :param str channel: Charm store channel from which to retrieve
822 the charm or bundle, e.g. 'development'
823 :param dict config: Charm configuration dictionary
824 :param constraints: Service constraints
825 :type constraints: :class:`juju.Constraints`
826 :param bool force: Allow charm to be deployed to a machine running
827 an unsupported series
828 :param int num_units: Number of units to deploy
829 :param str plan: Plan under which to deploy charm
830 :param dict resources: <resource name>:<file path> pairs
831 :param str series: Series on which to deploy
832 :param dict storage: Storage constraints TODO how do these look?
833 :param str to: Placement directive, e.g.::
836 'lxc:7' - new lxc container on machine 7
837 '24/lxc/3' - lxc container 3 or machine 24
839 If None, a new machine is provisioned.
844 - service_name is required; fill this in automatically if not
846 - series is required; how do we pick a default?
851 client
.Placement(**p
) for p
in to
858 k
: client
.Constraints(**v
)
859 for k
, v
in storage
.items()
862 is_local
= not entity_url
.startswith('cs:') and \
863 os
.path
.isdir(entity_url
)
864 entity_id
= await self
.charmstore
.entityId(entity_url
) \
865 if not is_local
else entity_url
867 app_facade
= client
.ApplicationFacade()
868 client_facade
= client
.ClientFacade()
869 app_facade
.connect(self
.connection
)
870 client_facade
.connect(self
.connection
)
872 is_bundle
= ((is_local
and
873 (Path(entity_id
) / 'bundle.yaml').exists()) or
874 (not is_local
and 'bundle/' in entity_id
))
877 handler
= BundleHandler(self
)
878 await handler
.fetch_plan(entity_id
)
879 await handler
.execute_plan()
880 extant_apps
= {app
for app
in self
.applications
}
881 pending_apps
= set(handler
.applications
) - extant_apps
883 # new apps will usually be in the model by now, but if some
884 # haven't made it yet we'll need to wait on them to be added
885 await asyncio
.gather(*[
886 asyncio
.ensure_future(
887 self
.model
._wait
_for
_new
('application', app_name
))
888 for app_name
in pending_apps
890 return [app
for name
, app
in self
.applications
.items()
891 if name
in handler
.applications
]
894 'Deploying %s', entity_id
)
896 await client_facade
.AddCharm(channel
, entity_id
)
897 app
= client
.ApplicationDeploy(
898 application
=service_name
,
902 constraints
=constraints
,
903 endpoint_bindings
=bind
,
911 await app_facade
.Deploy([app
])
912 return await self
._wait
_for
_new
('application', service_name
)
915 """Terminate all machines and resources for this model.
920 async def destroy_unit(self
, *unit_names
):
921 """Destroy units by name.
924 app_facade
= client
.ApplicationFacade()
925 app_facade
.connect(self
.connection
)
928 'Destroying unit%s %s',
929 's' if len(unit_names
) == 1 else '',
930 ' '.join(unit_names
))
932 return await app_facade
.Destroy(self
.name
)
933 destroy_units
= destroy_unit
935 def get_backup(self
, archive_id
):
936 """Download a backup archive file.
938 :param str archive_id: The id of the archive to download
939 :return str: Path to the archive file
945 self
, num_controllers
=0, constraints
=None, series
=None, to
=None):
946 """Ensure sufficient controllers exist to provide redundancy.
948 :param int num_controllers: Number of controllers to make available
949 :param constraints: Constraints to apply to the controller machines
950 :type constraints: :class:`juju.Constraints`
951 :param str series: Series of the controller machines
952 :param list to: Placement directives for controller machines, e.g.::
955 'lxc:7' - new lxc container on machine 7
956 '24/lxc/3' - lxc container 3 or machine 24
958 If None, a new machine is provisioned.
963 def get_config(self
):
964 """Return the configuration settings for this model.
969 def get_constraints(self
):
970 """Return the machine constraints for this model.
975 def grant(self
, username
, acl
='read'):
976 """Grant a user access to this model.
978 :param str username: Username
979 :param str acl: Access control ('read' or 'write')
984 def import_ssh_key(self
, identity
):
985 """Add a public SSH key from a trusted indentity source to this model.
987 :param str identity: User identity in the form <lp|gh>:<username>
991 import_ssh_keys
= import_ssh_key
993 def get_machines(self
, machine
, utc
=False):
994 """Return list of machines in this model.
996 :param str machine: Machine id, e.g. '0'
997 :param bool utc: Display time as UTC in RFC3339 format
1002 def get_shares(self
):
1003 """Return list of all users with access to this model.
1008 def get_spaces(self
):
1009 """Return list of all known spaces, including associated subnets.
1014 def get_ssh_key(self
):
1015 """Return known SSH keys for this model.
1019 get_ssh_keys
= get_ssh_key
1021 def get_storage(self
, filesystem
=False, volume
=False):
1022 """Return details of storage instances.
1024 :param bool filesystem: Include filesystem storage
1025 :param bool volume: Include volume storage
1030 def get_storage_pools(self
, names
=None, providers
=None):
1031 """Return list of storage pools.
1033 :param list names: Only include pools with these names
1034 :param list providers: Only include pools for these providers
1039 def get_subnets(self
, space
=None, zone
=None):
1040 """Return list of known subnets.
1042 :param str space: Only include subnets in this space
1043 :param str zone: Only include subnets in this zone
1048 def remove_blocks(self
):
1049 """Remove all blocks from this model.
1054 def remove_backup(self
, backup_id
):
1057 :param str backup_id: The id of the backup to remove
1062 def remove_cached_images(self
, arch
=None, kind
=None, series
=None):
1063 """Remove cached OS images.
1065 :param str arch: Architecture of the images to remove
1066 :param str kind: Image kind to remove, e.g. 'lxd'
1067 :param str series: Image series to remove, e.g. 'xenial'
1072 def remove_machine(self
, *machine_ids
):
1073 """Remove a machine from this model.
1075 :param str \*machine_ids: Ids of the machines to remove
1079 remove_machines
= remove_machine
1081 def remove_ssh_key(self
, *keys
):
1082 """Remove a public SSH key(s) from this model.
1084 :param str \*keys: Keys to remove
1088 remove_ssh_keys
= remove_ssh_key
1091 self
, bootstrap
=False, constraints
=None, archive
=None,
1092 backup_id
=None, upload_tools
=False):
1093 """Restore a backup archive to a new controller.
1095 :param bool bootstrap: Bootstrap a new state machine
1096 :param constraints: Model constraints
1097 :type constraints: :class:`juju.Constraints`
1098 :param str archive: Path to backup archive to restore
1099 :param str backup_id: Id of backup to restore
1100 :param bool upload_tools: Upload tools if bootstrapping a new machine
1105 def retry_provisioning(self
):
1106 """Retry provisioning for failed machines.
1111 def revoke(self
, username
, acl
='read'):
1112 """Revoke a user's access to this model.
1114 :param str username: Username to revoke
1115 :param str acl: Access control ('read' or 'write')
1120 def run(self
, command
, timeout
=None):
1121 """Run command on all machines in this model.
1123 :param str command: The command to run
1124 :param int timeout: Time to wait before command is considered failed
1129 def set_config(self
, **config
):
1130 """Set configuration keys on this model.
1132 :param \*\*config: Config key/values
1137 def set_constraints(self
, constraints
):
1138 """Set machine constraints on this model.
1140 :param :class:`juju.Constraints` constraints: Machine constraints
1145 def get_action_output(self
, action_uuid
, wait
=-1):
1146 """Get the results of an action by ID.
1148 :param str action_uuid: Id of the action
1149 :param int wait: Time in seconds to wait for action to complete
1154 def get_action_status(self
, uuid_or_prefix
=None, name
=None):
1155 """Get the status of all actions, filtered by ID, ID prefix, or action name.
1157 :param str uuid_or_prefix: Filter by action uuid or prefix
1158 :param str name: Filter by action name
1163 def get_budget(self
, budget_name
):
1164 """Get budget usage info.
1166 :param str budget_name: Name of budget
1171 def get_status(self
, filter_
=None, utc
=False):
1172 """Return the status of the model.
1174 :param str filter_: Service or unit name or wildcard ('*')
1175 :param bool utc: Display time as UTC in RFC3339 format
1182 self
, all_
=False, destination
=None, dry_run
=False, public
=False,
1183 source
=None, stream
=None, version
=None):
1184 """Copy Juju tools into this model.
1186 :param bool all_: Copy all versions, not just the latest
1187 :param str destination: Path to local destination directory
1188 :param bool dry_run: Don't do the actual copy
1189 :param bool public: Tools are for a public cloud, so generate mirrors
1191 :param str source: Path to local source directory
1192 :param str stream: Simplestreams stream for which to sync metadata
1193 :param str version: Copy a specific major.minor version
1198 def unblock(self
, *commands
):
1199 """Unblock an operation that would alter this model.
1201 :param str \*commands: The commands to unblock. Valid values are
1202 'all-changes', 'destroy-model', 'remove-object'
1207 def unset_config(self
, *keys
):
1208 """Unset configuration on this model.
1210 :param str \*keys: The keys to unset
1215 def upgrade_gui(self
):
1216 """Upgrade the Juju GUI for this model.
1222 self
, dry_run
=False, reset_previous_upgrade
=False,
1223 upload_tools
=False, version
=None):
1224 """Upgrade Juju on all machines in a model.
1226 :param bool dry_run: Don't do the actual upgrade
1227 :param bool reset_previous_upgrade: Clear the previous (incomplete)
1229 :param bool upload_tools: Upload local version of tools
1230 :param str version: Upgrade to a specific version
1235 def upload_backup(self
, archive_path
):
1236 """Store a backup archive remotely in Juju.
1238 :param str archive_path: Path to local archive
1244 def charmstore(self
):
1245 return self
._charmstore
1247 async def get_metrics(self
, *tags
):
1248 """Retrieve metrics.
1250 :param str \*tags: Tags of entities from which to retrieve metrics.
1251 No tags retrieves the metrics of all units in the model.
1253 log
.debug("Retrieving metrics for %s",
1254 ', '.join(tags
) if tags
else "all units")
1256 metrics_facade
= client
.MetricsDebugFacade()
1257 metrics_facade
.connect(self
.connection
)
1259 entities
= [client
.Entity(tag
) for tag
in tags
]
1260 metrics_result
= await metrics_facade
.GetMetrics(entities
)
1262 metrics
= collections
.defaultdict(list)
1264 for entity_metrics
in metrics_result
.results
:
1265 error
= entity_metrics
.error
1267 if "is not a valid tag" in error
:
1268 raise ValueError(error
.message
)
1270 raise Exception(error
.message
)
1272 for metric
in entity_metrics
.metrics
:
1273 metrics
[metric
.unit
].append(vars(metric
))
1278 class BundleHandler(object):
1280 Handle bundles by using the API to translate bundle YAML into a plan of
1281 steps and then dispatching each of those using the API.
1283 def __init__(self
, model
):
1285 self
.charmstore
= model
.charmstore
1287 self
.references
= {}
1288 self
._units
_by
_app
= {}
1289 for unit_name
, unit
in model
.units
.items():
1290 app_units
= self
._units
_by
_app
.setdefault(unit
.application
, [])
1291 app_units
.append(unit_name
)
1292 self
.client_facade
= client
.ClientFacade()
1293 self
.client_facade
.connect(model
.connection
)
1294 self
.app_facade
= client
.ApplicationFacade()
1295 self
.app_facade
.connect(model
.connection
)
1296 self
.ann_facade
= client
.AnnotationsFacade()
1297 self
.ann_facade
.connect(model
.connection
)
1299 async def fetch_plan(self
, entity_id
):
1300 is_local
= not entity_id
.startswith('cs:') and os
.path
.isdir(entity_id
)
1302 bundle_yaml
= (Path(entity_id
) / "bundle.yaml").read_text()
1304 bundle_yaml
= await self
.charmstore
.files(entity_id
,
1305 filename
='bundle.yaml',
1307 self
.bundle
= yaml
.safe_load(bundle_yaml
)
1308 self
.plan
= await self
.client_facade
.GetBundleChanges(bundle_yaml
)
1310 async def execute_plan(self
):
1311 for step
in self
.plan
.changes
:
1312 method
= getattr(self
, step
.method
)
1313 result
= await method(*step
.args
)
1314 self
.references
[step
.id_
] = result
1317 def applications(self
):
1318 return list(self
.bundle
['services'].keys())
1320 def resolve(self
, reference
):
1321 if reference
and reference
.startswith('$'):
1322 reference
= self
.references
[reference
[1:]]
1325 async def addCharm(self
, charm
, series
):
1327 :param charm string:
1328 Charm holds the URL of the charm to be added.
1330 :param series string:
1331 Series holds the series of the charm to be added
1332 if the charm default is not sufficient.
1334 entity_id
= await self
.charmstore
.entityId(charm
)
1335 log
.debug('Adding %s', entity_id
)
1336 await self
.client_facade
.AddCharm(None, entity_id
)
1339 async def addMachines(self
, params
=None):
1342 Dictionary specifying the machine to add. All keys are optional.
1345 series: string specifying the machine OS series.
1346 constraints: string holding machine constraints, if any. We'll
1347 parse this into the json friendly dict that the juju api
1349 container_type: string holding the type of the container (for
1350 instance ""lxc" or kvm"). It is not specified for top level
1352 parent_id: string holding a placeholder pointing to another
1353 machine change or to a unit change. This value is only
1354 specified in the case this machine is a container, in
1355 which case also ContainerType is set.
1357 params
= params
or {}
1359 if 'parent_id' in params
:
1360 params
['parent_id'] = self
.resolve(params
['parent_id'])
1362 params
['constraints'] = parse_constraints(
1363 params
.get('constraints'))
1364 params
['jobs'] = params
.get('jobs', ['JobHostUnits'])
1366 params
= client
.AddMachineParams(**params
)
1367 results
= await self
.client_facade
.AddMachines([params
])
1368 error
= results
.machines
[0].error
1370 raise ValueError("Error adding machine: %s", error
.message
)
1371 machine
= results
.machines
[0].machine
1372 log
.debug('Added new machine %s', machine
)
1375 async def addRelation(self
, endpoint1
, endpoint2
):
1377 :param endpoint1 string:
1378 :param endpoint2 string:
1379 Endpoint1 and Endpoint2 hold relation endpoints in the
1380 "application:interface" form, where the application is always a
1381 placeholder pointing to an application change, and the interface is
1382 optional. Examples are "$deploy-42:web" or just "$deploy-42".
1384 endpoints
= [endpoint1
, endpoint2
]
1385 # resolve indirect references
1386 for i
in range(len(endpoints
)):
1387 parts
= endpoints
[i
].split(':')
1388 parts
[0] = self
.resolve(parts
[0])
1389 endpoints
[i
] = ':'.join(parts
)
1391 log
.info('Relating %s <-> %s', *endpoints
)
1392 return await self
.model
.add_relation(*endpoints
)
1394 async def deploy(self
, charm
, series
, application
, options
, constraints
,
1395 storage
, endpoint_bindings
, resources
):
1397 :param charm string:
1398 Charm holds the URL of the charm to be used to deploy this
1401 :param series string:
1402 Series holds the series of the application to be deployed
1403 if the charm default is not sufficient.
1405 :param application string:
1406 Application holds the application name.
1408 :param options map[string]interface{}:
1409 Options holds application options.
1411 :param constraints string:
1412 Constraints holds the optional application constraints.
1414 :param storage map[string]string:
1415 Storage holds the optional storage constraints.
1417 :param endpoint_bindings map[string]string:
1418 EndpointBindings holds the optional endpoint bindings
1420 :param resources map[string]int:
1421 Resources identifies the revision to use for each resource
1422 of the application's charm.
1424 # resolve indirect references
1425 charm
= self
.resolve(charm
)
1426 # stringify all config values for API
1427 options
= {k
: str(v
) for k
, v
in options
.items()}
1428 # build param object
1429 app
= client
.ApplicationDeploy(
1432 application
=application
,
1434 constraints
=constraints
,
1436 endpoint_bindings
=endpoint_bindings
,
1437 resources
=resources
,
1440 log
.info('Deploying %s', charm
)
1441 await self
.app_facade
.Deploy([app
])
1442 # ensure the app is in the model for future operations
1443 await self
.model
._wait
_for
_new
('application', application
)
1446 async def addUnit(self
, application
, to
):
1448 :param application string:
1449 Application holds the application placeholder name for which a unit
1453 To holds the optional location where to add the unit, as a
1454 placeholder pointing to another unit change or to a machine change.
1456 application
= self
.resolve(application
)
1457 placement
= self
.resolve(to
)
1458 if self
._units
_by
_app
.get(application
):
1459 # enough units for this application already exist;
1460 # claim one, and carry on
1461 # NB: this should probably honor placement, but the juju client
1462 # doesn't, so we're not bothering, either
1463 unit_name
= self
._units
_by
_app
[application
].pop()
1464 log
.debug('Reusing unit %s for %s', unit_name
, application
)
1465 return self
.model
.units
[unit_name
]
1467 log
.debug('Adding new unit for %s%s', application
,
1468 ' to %s' % placement
if placement
else '')
1469 return await self
.model
.applications
[application
].add_unit(
1474 async def expose(self
, application
):
1476 :param application string:
1477 Application holds the placeholder name of the application that must
1480 application
= self
.resolve(application
)
1481 log
.info('Exposing %s', application
)
1482 return await self
.model
.applications
[application
].expose()
1484 async def setAnnotations(self
, id_
, entity_type
, annotations
):
1487 Id is the placeholder for the application or machine change
1488 corresponding to the entity to be annotated.
1490 :param entity_type EntityType:
1491 EntityType holds the type of the entity, "application" or
1494 :param annotations map[string]string:
1495 Annotations holds the annotations as key/value pairs.
1497 entity_id
= self
.resolve(id_
)
1499 entity
= self
.model
.state
.get_entity(entity_type
, entity_id
)
1501 entity
= await self
.model
._wait
_for
_new
(entity_type
, entity_id
)
1502 return await entity
.set_annotations(annotations
)
1505 class CharmStore(object):
1507 Async wrapper around theblues.charmstore.CharmStore
1509 def __init__(self
, loop
):
1511 self
._cs
= charmstore
.CharmStore()
1513 def __getattr__(self
, name
):
1515 Wrap method calls in coroutines that use run_in_executor to make them
1518 attr
= getattr(self
._cs
, name
)
1519 if not callable(attr
):
1520 wrapper
= partial(getattr, self
._cs
, name
)
1521 setattr(self
, name
, wrapper
)
1523 async def coro(*args
, **kwargs
):
1524 method
= partial(attr
, *args
, **kwargs
)
1525 return await self
.loop
.run_in_executor(None, method
)
1526 setattr(self
, name
, coro
)