Implement Model.add_machine()
[osm/N2VC.git] / juju / model.py
1 import asyncio
2 import collections
3 import json
4 import logging
5 import os
6 import re
7 import stat
8 import tempfile
9 import weakref
10 import zipfile
11 from concurrent.futures import CancelledError
12 from functools import partial
13 from pathlib import Path
14
15 import yaml
16 from theblues import charmstore
17
18 from .client import client
19 from .client import watcher
20 from .client import connection
21 from .constraints import parse as parse_constraints, normalize_key
22 from .delta import get_entity_delta
23 from .delta import get_entity_class
24 from .exceptions import DeadEntityException
25 from .errors import JujuError, JujuAPIError
26 from .placement import parse as parse_placement
27
28 log = logging.getLogger(__name__)
29
30
31 class _Observer(object):
32 """Wrapper around an observer callable.
33
34 This wrapper allows filter criteria to be associated with the
35 callable so that it's only called for changes that meet the criteria.
36
37 """
38 def __init__(self, callable_, entity_type, action, entity_id, predicate):
39 self.callable_ = callable_
40 self.entity_type = entity_type
41 self.action = action
42 self.entity_id = entity_id
43 self.predicate = predicate
44 if self.entity_id:
45 self.entity_id = str(self.entity_id)
46 if not self.entity_id.startswith('^'):
47 self.entity_id = '^' + self.entity_id
48 if not self.entity_id.endswith('$'):
49 self.entity_id += '$'
50
51 async def __call__(self, delta, old, new, model):
52 await self.callable_(delta, old, new, model)
53
54 def cares_about(self, delta):
55 """Return True if this observer "cares about" (i.e. wants to be
56 called) for a this delta.
57
58 """
59 if (self.entity_id and delta.get_id() and
60 not re.match(self.entity_id, str(delta.get_id()))):
61 return False
62
63 if self.entity_type and self.entity_type != delta.entity:
64 return False
65
66 if self.action and self.action != delta.type:
67 return False
68
69 if self.predicate and not self.predicate(delta):
70 return False
71
72 return True
73
74
75 class ModelObserver(object):
76 async def __call__(self, delta, old, new, model):
77 handler_name = 'on_{}_{}'.format(delta.entity, delta.type)
78 method = getattr(self, handler_name, self.on_change)
79 await method(delta, old, new, model)
80
81 async def on_change(self, delta, old, new, model):
82 """Generic model-change handler.
83
84 :param delta: :class:`juju.client.overrides.Delta`
85 :param old: :class:`juju.model.ModelEntity`
86 :param new: :class:`juju.model.ModelEntity`
87 :param model: :class:`juju.model.Model`
88
89 """
90 pass
91
92
93 class ModelState(object):
94 """Holds the state of the model, including the delta history of all
95 entities in the model.
96
97 """
98 def __init__(self, model):
99 self.model = model
100 self.state = dict()
101
102 def _live_entity_map(self, entity_type):
103 """Return an id:Entity map of all the living entities of
104 type ``entity_type``.
105
106 """
107 return {
108 entity_id: self.get_entity(entity_type, entity_id)
109 for entity_id, history in self.state.get(entity_type, {}).items()
110 if history[-1] is not None
111 }
112
113 @property
114 def applications(self):
115 """Return a map of application-name:Application for all applications
116 currently in the model.
117
118 """
119 return self._live_entity_map('application')
120
121 @property
122 def machines(self):
123 """Return a map of machine-id:Machine for all machines currently in
124 the model.
125
126 """
127 return self._live_entity_map('machine')
128
129 @property
130 def units(self):
131 """Return a map of unit-id:Unit for all units currently in
132 the model.
133
134 """
135 return self._live_entity_map('unit')
136
137 def entity_history(self, entity_type, entity_id):
138 """Return the history deque for an entity.
139
140 """
141 return self.state[entity_type][entity_id]
142
143 def entity_data(self, entity_type, entity_id, history_index):
144 """Return the data dict for an entity at a specific index of its
145 history.
146
147 """
148 return self.entity_history(entity_type, entity_id)[history_index]
149
150 def apply_delta(self, delta):
151 """Apply delta to our state and return a copy of the
152 affected object as it was before and after the update, e.g.:
153
154 old_obj, new_obj = self.apply_delta(delta)
155
156 old_obj may be None if the delta is for the creation of a new object,
157 e.g. a new application or unit is deployed.
158
159 new_obj will never be None, but may be dead (new_obj.dead == True)
160 if the object was deleted as a result of the delta being applied.
161
162 """
163 history = (
164 self.state
165 .setdefault(delta.entity, {})
166 .setdefault(delta.get_id(), collections.deque())
167 )
168
169 history.append(delta.data)
170 if delta.type == 'remove':
171 history.append(None)
172
173 entity = self.get_entity(delta.entity, delta.get_id())
174 return entity.previous(), entity
175
176 def get_entity(
177 self, entity_type, entity_id, history_index=-1, connected=True):
178 """Return an object instance for the given entity_type and id.
179
180 By default the object state matches the most recent state from
181 Juju. To get an instance of the object in an older state, pass
182 history_index, an index into the history deque for the entity.
183
184 """
185
186 if history_index < 0 and history_index != -1:
187 history_index += len(self.entity_history(entity_type, entity_id))
188 if history_index < 0:
189 return None
190
191 try:
192 self.entity_data(entity_type, entity_id, history_index)
193 except IndexError:
194 return None
195
196 entity_class = get_entity_class(entity_type)
197 return entity_class(
198 entity_id, self.model, history_index=history_index,
199 connected=connected)
200
201
202 class ModelEntity(object):
203 """An object in the Model tree"""
204
205 def __init__(self, entity_id, model, history_index=-1, connected=True):
206 """Initialize a new entity
207
208 :param entity_id str: The unique id of the object in the model
209 :param model: The model instance in whose object tree this
210 entity resides
211 :history_index int: The index of this object's state in the model's
212 history deque for this entity
213 :connected bool: Flag indicating whether this object gets live updates
214 from the model.
215
216 """
217 self.entity_id = entity_id
218 self.model = model
219 self._history_index = history_index
220 self.connected = connected
221 self.connection = model.connection
222
223 def __repr__(self):
224 return '<{} entity_id="{}">'.format(type(self).__name__,
225 self.entity_id)
226
227 def __getattr__(self, name):
228 """Fetch object attributes from the underlying data dict held in the
229 model.
230
231 """
232 return self.safe_data[name]
233
234 def __bool__(self):
235 return bool(self.data)
236
237 def on_change(self, callable_):
238 """Add a change observer to this entity.
239
240 """
241 self.model.add_observer(
242 callable_, self.entity_type, 'change', self.entity_id)
243
244 def on_remove(self, callable_):
245 """Add a remove observer to this entity.
246
247 """
248 self.model.add_observer(
249 callable_, self.entity_type, 'remove', self.entity_id)
250
251 @property
252 def entity_type(self):
253 """A string identifying the entity type of this object, e.g.
254 'application' or 'unit', etc.
255
256 """
257 return self.__class__.__name__.lower()
258
259 @property
260 def current(self):
261 """Return True if this object represents the current state of the
262 entity in the underlying model.
263
264 This will be True except when the object represents an entity at a
265 non-latest state in history, e.g. if the object was obtained by calling
266 .previous() on another object.
267
268 """
269 return self._history_index == -1
270
271 @property
272 def dead(self):
273 """Returns True if this entity no longer exists in the underlying
274 model.
275
276 """
277 return (
278 self.data is None or
279 self.model.state.entity_data(
280 self.entity_type, self.entity_id, -1) is None
281 )
282
283 @property
284 def alive(self):
285 """Returns True if this entity still exists in the underlying
286 model.
287
288 """
289 return not self.dead
290
291 @property
292 def data(self):
293 """The data dictionary for this entity.
294
295 """
296 return self.model.state.entity_data(
297 self.entity_type, self.entity_id, self._history_index)
298
299 @property
300 def safe_data(self):
301 """The data dictionary for this entity.
302
303 If this `ModelEntity` points to the dead state, it will
304 raise `DeadEntityException`.
305
306 """
307 if self.data is None:
308 raise DeadEntityException(
309 "Entity {}:{} is dead - its attributes can no longer be "
310 "accessed. Use the .previous() method on this object to get "
311 "a copy of the object at its previous state.".format(
312 self.entity_type, self.entity_id))
313 return self.data
314
315 def previous(self):
316 """Return a copy of this object as was at its previous state in
317 history.
318
319 Returns None if this object is new (and therefore has no history).
320
321 The returned object is always "disconnected", i.e. does not receive
322 live updates.
323
324 """
325 return self.model.state.get_entity(
326 self.entity_type, self.entity_id, self._history_index - 1,
327 connected=False)
328
329 def next(self):
330 """Return a copy of this object at its next state in
331 history.
332
333 Returns None if this object is already the latest.
334
335 The returned object is "disconnected", i.e. does not receive
336 live updates, unless it is current (latest).
337
338 """
339 if self._history_index == -1:
340 return None
341
342 new_index = self._history_index + 1
343 connected = (
344 new_index == len(self.model.state.entity_history(
345 self.entity_type, self.entity_id)) - 1
346 )
347 return self.model.state.get_entity(
348 self.entity_type, self.entity_id, self._history_index - 1,
349 connected=connected)
350
351 def latest(self):
352 """Return a copy of this object at its current state in the model.
353
354 Returns self if this object is already the latest.
355
356 The returned object is always "connected", i.e. receives
357 live updates from the model.
358
359 """
360 if self._history_index == -1:
361 return self
362
363 return self.model.state.get_entity(self.entity_type, self.entity_id)
364
365
366 class Model(object):
367 def __init__(self, loop=None):
368 """Instantiate a new connected Model.
369
370 :param loop: an asyncio event loop
371
372 """
373 self.loop = loop or asyncio.get_event_loop()
374 self.connection = None
375 self.observers = weakref.WeakValueDictionary()
376 self.state = ModelState(self)
377 self.info = None
378 self._watcher_task = None
379 self._watch_shutdown = asyncio.Event(loop=loop)
380 self._watch_received = asyncio.Event(loop=loop)
381 self._charmstore = CharmStore(self.loop)
382
383 async def connect(self, *args, **kw):
384 """Connect to an arbitrary Juju model.
385
386 args and kw are passed through to Connection.connect()
387
388 """
389 self.connection = await connection.Connection.connect(*args, **kw)
390 await self._after_connect()
391
392 async def connect_current(self):
393 """Connect to the current Juju model.
394
395 """
396 self.connection = await connection.Connection.connect_current()
397 await self._after_connect()
398
399 async def connect_model(self, model_name):
400 """Connect to a specific Juju model by name.
401
402 :param model_name: Format [controller:][user/]model
403
404 """
405 self.connection = await connection.Connection.connect_model(model_name)
406 await self._after_connect()
407
408 async def _after_connect(self):
409 """Run initialization steps after connecting to websocket.
410
411 """
412 self._watch()
413 await self._watch_received.wait()
414 await self.get_info()
415
416 async def disconnect(self):
417 """Shut down the watcher task and close websockets.
418
419 """
420 self._stop_watching()
421 if self.connection and self.connection.is_open:
422 await self._watch_shutdown.wait()
423 log.debug('Closing model connection')
424 await self.connection.close()
425 self.connection = None
426
427 async def add_local_charm_dir(self, charm_dir, series):
428 """Upload a local charm to the model.
429
430 This will automatically generate an archive from
431 the charm dir.
432
433 :param charm_dir: Path to the charm directory
434 :param series: Charm series
435
436 """
437 fh = tempfile.NamedTemporaryFile()
438 CharmArchiveGenerator(charm_dir).make_archive(fh.name)
439 with fh:
440 func = partial(
441 self.add_local_charm, fh, series, os.stat(fh.name).st_size)
442 charm_url = await self.loop.run_in_executor(None, func)
443
444 log.debug('Uploaded local charm: %s -> %s', charm_dir, charm_url)
445 return charm_url
446
447 def add_local_charm(self, charm_file, series, size=None):
448 """Upload a local charm archive to the model.
449
450 Returns the 'local:...' url that should be used to deploy the charm.
451
452 :param charm_file: Path to charm zip archive
453 :param series: Charm series
454 :param size: Size of the archive, in bytes
455 :return str: 'local:...' url for deploying the charm
456 :raises: :class:`JujuError` if the upload fails
457
458 Uses an https endpoint at the same host:port as the wss.
459 Supports large file uploads.
460
461 .. warning::
462
463 This method will block. Consider using :meth:`add_local_charm_dir`
464 instead.
465
466 """
467 conn, headers, path_prefix = self.connection.https_connection()
468 path = "%s/charms?series=%s" % (path_prefix, series)
469 headers['Content-Type'] = 'application/zip'
470 if size:
471 headers['Content-Length'] = size
472 conn.request("POST", path, charm_file, headers)
473 response = conn.getresponse()
474 result = response.read().decode()
475 if not response.status == 200:
476 raise JujuError(result)
477 result = json.loads(result)
478 return result['charm-url']
479
480 def all_units_idle(self):
481 """Return True if all units are idle.
482
483 """
484 for unit in self.units.values():
485 unit_status = unit.data['agent-status']['current']
486 if unit_status != 'idle':
487 return False
488 return True
489
490 async def reset(self, force=False):
491 """Reset the model to a clean state.
492
493 :param bool force: Force-terminate machines.
494
495 This returns only after the model has reached a clean state. "Clean"
496 means no applications or machines exist in the model.
497
498 """
499 log.debug('Resetting model')
500 for app in self.applications.values():
501 await app.destroy()
502 for machine in self.machines.values():
503 await machine.destroy(force=force)
504 await self.block_until(
505 lambda: len(self.machines) == 0
506 )
507
508 async def block_until(self, *conditions, timeout=None, wait_period=0.5):
509 """Return only after all conditions are true.
510
511 """
512 async def _block():
513 while not all(c() for c in conditions):
514 await asyncio.sleep(wait_period)
515 await asyncio.wait_for(_block(), timeout)
516
517 @property
518 def applications(self):
519 """Return a map of application-name:Application for all applications
520 currently in the model.
521
522 """
523 return self.state.applications
524
525 @property
526 def machines(self):
527 """Return a map of machine-id:Machine for all machines currently in
528 the model.
529
530 """
531 return self.state.machines
532
533 @property
534 def units(self):
535 """Return a map of unit-id:Unit for all units currently in
536 the model.
537
538 """
539 return self.state.units
540
541 async def get_info(self):
542 """Return a client.ModelInfo object for this Model.
543
544 Retrieves latest info for this Model from the api server. The
545 return value is cached on the Model.info attribute so that the
546 valued may be accessed again without another api call, if
547 desired.
548
549 This method is called automatically when the Model is connected,
550 resulting in Model.info being initialized without requiring an
551 explicit call to this method.
552
553 """
554 facade = client.ClientFacade()
555 facade.connect(self.connection)
556
557 self.info = await facade.ModelInfo()
558 log.debug('Got ModelInfo: %s', vars(self.info))
559
560 return self.info
561
562 def add_observer(
563 self, callable_, entity_type=None, action=None, entity_id=None,
564 predicate=None):
565 """Register an "on-model-change" callback
566
567 Once the model is connected, ``callable_``
568 will be called each time the model changes. ``callable_`` should
569 be Awaitable and accept the following positional arguments:
570
571 delta - An instance of :class:`juju.delta.EntityDelta`
572 containing the raw delta data recv'd from the Juju
573 websocket.
574
575 old_obj - If the delta modifies an existing object in the model,
576 old_obj will be a copy of that object, as it was before the
577 delta was applied. Will be None if the delta creates a new
578 entity in the model.
579
580 new_obj - A copy of the new or updated object, after the delta
581 is applied. Will be None if the delta removes an entity
582 from the model.
583
584 model - The :class:`Model` itself.
585
586 Events for which ``callable_`` is called can be specified by passing
587 entity_type, action, and/or entitiy_id filter criteria, e.g.::
588
589 add_observer(
590 myfunc,
591 entity_type='application', action='add', entity_id='ubuntu')
592
593 For more complex filtering conditions, pass a predicate function. It
594 will be called with a delta as its only argument. If the predicate
595 function returns True, the ``callable_`` will be called.
596
597 """
598 observer = _Observer(
599 callable_, entity_type, action, entity_id, predicate)
600 self.observers[observer] = callable_
601
602 def _watch(self):
603 """Start an asynchronous watch against this model.
604
605 See :meth:`add_observer` to register an onchange callback.
606
607 """
608 async def _start_watch():
609 self._watch_shutdown.clear()
610 try:
611 allwatcher = watcher.AllWatcher()
612 self._watch_conn = await self.connection.clone()
613 allwatcher.connect(self._watch_conn)
614 while True:
615 results = await allwatcher.Next()
616 for delta in results.deltas:
617 delta = get_entity_delta(delta)
618 old_obj, new_obj = self.state.apply_delta(delta)
619 # XXX: Might not want to shield at this level
620 # We are shielding because when the watcher is
621 # canceled (on disconnect()), we don't want all of
622 # its children (every observer callback) to be
623 # canceled with it. So we shield them. But this means
624 # they can *never* be canceled.
625 await asyncio.shield(
626 self._notify_observers(delta, old_obj, new_obj))
627 self._watch_received.set()
628 except CancelledError:
629 log.debug('Closing watcher connection')
630 await self._watch_conn.close()
631 self._watch_shutdown.set()
632 self._watch_conn = None
633
634 log.debug('Starting watcher task')
635 self._watcher_task = self.loop.create_task(_start_watch())
636
637 def _stop_watching(self):
638 """Stop the asynchronous watch against this model.
639
640 """
641 log.debug('Stopping watcher task')
642 if self._watcher_task:
643 self._watcher_task.cancel()
644
645 async def _notify_observers(self, delta, old_obj, new_obj):
646 """Call observing callbacks, notifying them of a change in model state
647
648 :param delta: The raw change from the watcher
649 (:class:`juju.client.overrides.Delta`)
650 :param old_obj: The object in the model that this delta updates.
651 May be None.
652 :param new_obj: The object in the model that is created or updated
653 by applying this delta.
654
655 """
656 if new_obj and not old_obj:
657 delta.type = 'add'
658
659 log.debug(
660 'Model changed: %s %s %s',
661 delta.entity, delta.type, delta.get_id())
662
663 for o in self.observers:
664 if o.cares_about(delta):
665 asyncio.ensure_future(o(delta, old_obj, new_obj, self))
666
667 async def _wait(self, entity_type, entity_id, action, predicate=None):
668 """
669 Block the calling routine until a given action has happened to the
670 given entity
671
672 :param entity_type: The entity's type.
673 :param entity_id: The entity's id.
674 :param action: the type of action (e.g., 'add' or 'change')
675 :param predicate: optional callable that must take as an
676 argument a delta, and must return a boolean, indicating
677 whether the delta contains the specific action we're looking
678 for. For example, you might check to see whether a 'change'
679 has a 'completed' status. See the _Observer class for details.
680
681 """
682 q = asyncio.Queue(loop=self.loop)
683
684 async def callback(delta, old, new, model):
685 await q.put(delta.get_id())
686
687 self.add_observer(callback, entity_type, action, entity_id, predicate)
688 entity_id = await q.get()
689 return self.state._live_entity_map(entity_type)[entity_id]
690
691 async def _wait_for_new(self, entity_type, entity_id=None, predicate=None):
692 """Wait for a new object to appear in the Model and return it.
693
694 Waits for an object of type ``entity_type`` with id ``entity_id``.
695 If ``entity_id`` is ``None``, it will wait for the first new entity
696 of the correct type.
697
698 This coroutine blocks until the new object appears in the model.
699
700 """
701 # if the entity is already in the model, just return it
702 if entity_id in self.state._live_entity_map(entity_type):
703 return self.state._live_entity_map(entity_type)[entity_id]
704 # if we know the entity_id, we can trigger on any action that puts
705 # the enitty into the model; otherwise, we have to watch for the
706 # next "add" action on that entity_type
707 action = 'add' if entity_id is None else None
708 return await self._wait(entity_type, entity_id, action, predicate)
709
710 async def wait_for_action(self, action_id):
711 """Given an action, wait for it to complete."""
712
713 if action_id.startswith("action-"):
714 # if we've been passed action.tag, transform it into the
715 # id that the api deltas will use.
716 action_id = action_id[7:]
717
718 def predicate(delta):
719 return delta.data['status'] in ('completed', 'failed')
720
721 return await self._wait('action', action_id, 'change', predicate)
722
723 async def add_machine(
724 self, spec=None, constraints=None, disks=None, series=None):
725 """Start a new, empty machine and optionally a container, or add a
726 container to a machine.
727
728 :param str spec: Machine specification
729 Examples::
730
731 (None) - starts a new machine
732 'lxd' - starts a new machine with one lxd container
733 'lxd:4' - starts a new lxd container on machine 4
734 'ssh:user@10.10.0.3' - manually provisions a machine with ssh
735 'zone=us-east-1a' - starts a machine in zone us-east-1s on AWS
736 'maas2.name' - acquire machine maas2.name on MAAS
737
738 :param dict constraints: Machine constraints
739 Example::
740
741 constraints={
742 'mem': 256 * MB,
743 }
744
745 :param list disks: List of disk constraint dictionaries
746 Example::
747
748 disks=[{
749 'pool': 'rootfs',
750 'size': 10 * GB,
751 'count': 1,
752 }]
753
754 :param str series: Series, e.g. 'xenial'
755
756 Supported container types are: lxd, kvm
757
758 When deploying a container to an existing machine, constraints cannot
759 be used.
760
761 """
762 params = client.AddMachineParams()
763 params.jobs = ['JobHostUnits']
764
765 if spec:
766 placement = parse_placement(spec)
767 if placement:
768 params.placement = placement[0]
769
770 if constraints:
771 params.constraints = client.Value.from_json(constraints)
772
773 if disks:
774 params.disks = [
775 client.Constraints.from_json(o) for o in disks]
776
777 if series:
778 params.series = series
779
780 # Submit the request.
781 client_facade = client.ClientFacade()
782 client_facade.connect(self.connection)
783 results = await client_facade.AddMachines([params])
784 error = results.machines[0].error
785 if error:
786 raise ValueError("Error adding machine: %s", error.message)
787 machine_id = results.machines[0].machine
788 log.debug('Added new machine %s', machine_id)
789 return await self._wait_for_new('machine', machine_id)
790
791 async def add_relation(self, relation1, relation2):
792 """Add a relation between two applications.
793
794 :param str relation1: '<application>[:<relation_name>]'
795 :param str relation2: '<application>[:<relation_name>]'
796
797 """
798 app_facade = client.ApplicationFacade()
799 app_facade.connect(self.connection)
800
801 log.debug(
802 'Adding relation %s <-> %s', relation1, relation2)
803
804 try:
805 result = await app_facade.AddRelation([relation1, relation2])
806 except JujuAPIError as e:
807 if 'relation already exists' not in e.message:
808 raise
809 log.debug(
810 'Relation %s <-> %s already exists', relation1, relation2)
811 # TODO: if relation already exists we should return the
812 # Relation ModelEntity here
813 return None
814
815 def predicate(delta):
816 endpoints = {}
817 for endpoint in delta.data['endpoints']:
818 endpoints[endpoint['application-name']] = endpoint['relation']
819 return endpoints == result.endpoints
820
821 return await self._wait_for_new('relation', None, predicate)
822
823 def add_space(self, name, *cidrs):
824 """Add a new network space.
825
826 Adds a new space with the given name and associates the given
827 (optional) list of existing subnet CIDRs with it.
828
829 :param str name: Name of the space
830 :param \*cidrs: Optional list of existing subnet CIDRs
831
832 """
833 pass
834
835 def add_ssh_key(self, key):
836 """Add a public SSH key to this model.
837
838 :param str key: The public ssh key
839
840 """
841 pass
842 add_ssh_keys = add_ssh_key
843
844 def add_subnet(self, cidr_or_id, space, *zones):
845 """Add an existing subnet to this model.
846
847 :param str cidr_or_id: CIDR or provider ID of the existing subnet
848 :param str space: Network space with which to associate
849 :param str \*zones: Zone(s) in which the subnet resides
850
851 """
852 pass
853
854 def get_backups(self):
855 """Retrieve metadata for backups in this model.
856
857 """
858 pass
859
860 def block(self, *commands):
861 """Add a new block to this model.
862
863 :param str \*commands: The commands to block. Valid values are
864 'all-changes', 'destroy-model', 'remove-object'
865
866 """
867 pass
868
869 def get_blocks(self):
870 """List blocks for this model.
871
872 """
873 pass
874
875 def get_cached_images(self, arch=None, kind=None, series=None):
876 """Return a list of cached OS images.
877
878 :param str arch: Filter by image architecture
879 :param str kind: Filter by image kind, e.g. 'lxd'
880 :param str series: Filter by image series, e.g. 'xenial'
881
882 """
883 pass
884
885 def create_backup(self, note=None, no_download=False):
886 """Create a backup of this model.
887
888 :param str note: A note to store with the backup
889 :param bool no_download: Do not download the backup archive
890 :return str: Path to downloaded archive
891
892 """
893 pass
894
895 def create_storage_pool(self, name, provider_type, **pool_config):
896 """Create or define a storage pool.
897
898 :param str name: Name to give the storage pool
899 :param str provider_type: Pool provider type
900 :param \*\*pool_config: key/value pool configuration pairs
901
902 """
903 pass
904
905 def debug_log(
906 self, no_tail=False, exclude_module=None, include_module=None,
907 include=None, level=None, limit=0, lines=10, replay=False,
908 exclude=None):
909 """Get log messages for this model.
910
911 :param bool no_tail: Stop after returning existing log messages
912 :param list exclude_module: Do not show log messages for these logging
913 modules
914 :param list include_module: Only show log messages for these logging
915 modules
916 :param list include: Only show log messages for these entities
917 :param str level: Log level to show, valid options are 'TRACE',
918 'DEBUG', 'INFO', 'WARNING', 'ERROR,
919 :param int limit: Return this many of the most recent (possibly
920 filtered) lines are shown
921 :param int lines: Yield this many of the most recent lines, and keep
922 yielding
923 :param bool replay: Yield the entire log, and keep yielding
924 :param list exclude: Do not show log messages for these entities
925
926 """
927 pass
928
929 async def deploy(
930 self, entity_url, application_name=None, bind=None, budget=None,
931 channel=None, config=None, constraints=None, force=False,
932 num_units=1, plan=None, resources=None, series=None, storage=None,
933 to=None):
934 """Deploy a new service or bundle.
935
936 :param str entity_url: Charm or bundle url
937 :param str application_name: Name to give the service
938 :param dict bind: <charm endpoint>:<network space> pairs
939 :param dict budget: <budget name>:<limit> pairs
940 :param str channel: Charm store channel from which to retrieve
941 the charm or bundle, e.g. 'development'
942 :param dict config: Charm configuration dictionary
943 :param constraints: Service constraints
944 :type constraints: :class:`juju.Constraints`
945 :param bool force: Allow charm to be deployed to a machine running
946 an unsupported series
947 :param int num_units: Number of units to deploy
948 :param str plan: Plan under which to deploy charm
949 :param dict resources: <resource name>:<file path> pairs
950 :param str series: Series on which to deploy
951 :param dict storage: Storage constraints TODO how do these look?
952 :param to: Placement directive as a string. For example:
953
954 '23' - place on machine 23
955 'lxd:7' - place in new lxd container on machine 7
956 '24/lxd/3' - place in container 3 on machine 24
957
958 If None, a new machine is provisioned.
959
960
961 TODO::
962
963 - application_name is required; fill this in automatically if not
964 provided by caller
965 - series is required; how do we pick a default?
966
967 """
968 if to:
969 placement = parse_placement(to)
970 else:
971 placement = []
972
973 if storage:
974 storage = {
975 k: client.Constraints(**v)
976 for k, v in storage.items()
977 }
978
979 is_local = (
980 entity_url.startswith('local:') or
981 os.path.isdir(entity_url)
982 )
983 entity_id = await self.charmstore.entityId(entity_url) \
984 if not is_local else entity_url
985
986 app_facade = client.ApplicationFacade()
987 client_facade = client.ClientFacade()
988 app_facade.connect(self.connection)
989 client_facade.connect(self.connection)
990
991 is_bundle = ((is_local and
992 (Path(entity_id) / 'bundle.yaml').exists()) or
993 (not is_local and 'bundle/' in entity_id))
994
995 if is_bundle:
996 handler = BundleHandler(self)
997 await handler.fetch_plan(entity_id)
998 await handler.execute_plan()
999 extant_apps = {app for app in self.applications}
1000 pending_apps = set(handler.applications) - extant_apps
1001 if pending_apps:
1002 # new apps will usually be in the model by now, but if some
1003 # haven't made it yet we'll need to wait on them to be added
1004 await asyncio.gather(*[
1005 asyncio.ensure_future(
1006 self._wait_for_new('application', app_name))
1007 for app_name in pending_apps
1008 ])
1009 return [app for name, app in self.applications.items()
1010 if name in handler.applications]
1011 else:
1012 log.debug(
1013 'Deploying %s', entity_id)
1014
1015 if not is_local:
1016 await client_facade.AddCharm(channel, entity_id)
1017 elif not entity_id.startswith('local:'):
1018 # We have a local charm dir that needs to be uploaded
1019 charm_dir = os.path.abspath(
1020 os.path.expanduser(entity_id))
1021 series = series or get_charm_series(charm_dir)
1022 if not series:
1023 raise JujuError(
1024 "Couldn't determine series for charm at {}. "
1025 "Pass a 'series' kwarg to Model.deploy().".format(
1026 charm_dir))
1027 entity_id = await self.add_local_charm_dir(charm_dir, series)
1028
1029 app = client.ApplicationDeploy(
1030 application=application_name,
1031 channel=channel,
1032 charm_url=entity_id,
1033 config=config,
1034 constraints=parse_constraints(constraints),
1035 endpoint_bindings=bind,
1036 num_units=num_units,
1037 resources=resources,
1038 series=series,
1039 storage=storage,
1040 )
1041 app.placement = placement
1042
1043 result = await app_facade.Deploy([app])
1044 errors = [r.error.message for r in result.results if r.error]
1045 if errors:
1046 raise JujuError('\n'.join(errors))
1047 return await self._wait_for_new('application', application_name)
1048
1049 def destroy(self):
1050 """Terminate all machines and resources for this model.
1051
1052 """
1053 pass
1054
1055 async def destroy_unit(self, *unit_names):
1056 """Destroy units by name.
1057
1058 """
1059 app_facade = client.ApplicationFacade()
1060 app_facade.connect(self.connection)
1061
1062 log.debug(
1063 'Destroying unit%s %s',
1064 's' if len(unit_names) == 1 else '',
1065 ' '.join(unit_names))
1066
1067 return await app_facade.DestroyUnits(list(unit_names))
1068 destroy_units = destroy_unit
1069
1070 def get_backup(self, archive_id):
1071 """Download a backup archive file.
1072
1073 :param str archive_id: The id of the archive to download
1074 :return str: Path to the archive file
1075
1076 """
1077 pass
1078
1079 def enable_ha(
1080 self, num_controllers=0, constraints=None, series=None, to=None):
1081 """Ensure sufficient controllers exist to provide redundancy.
1082
1083 :param int num_controllers: Number of controllers to make available
1084 :param constraints: Constraints to apply to the controller machines
1085 :type constraints: :class:`juju.Constraints`
1086 :param str series: Series of the controller machines
1087 :param list to: Placement directives for controller machines, e.g.::
1088
1089 '23' - machine 23
1090 'lxc:7' - new lxc container on machine 7
1091 '24/lxc/3' - lxc container 3 or machine 24
1092
1093 If None, a new machine is provisioned.
1094
1095 """
1096 pass
1097
1098 def get_config(self):
1099 """Return the configuration settings for this model.
1100
1101 """
1102 pass
1103
1104 def get_constraints(self):
1105 """Return the machine constraints for this model.
1106
1107 """
1108 pass
1109
1110 def grant(self, username, acl='read'):
1111 """Grant a user access to this model.
1112
1113 :param str username: Username
1114 :param str acl: Access control ('read' or 'write')
1115
1116 """
1117 pass
1118
1119 def import_ssh_key(self, identity):
1120 """Add a public SSH key from a trusted indentity source to this model.
1121
1122 :param str identity: User identity in the form <lp|gh>:<username>
1123
1124 """
1125 pass
1126 import_ssh_keys = import_ssh_key
1127
1128 def get_machines(self, machine, utc=False):
1129 """Return list of machines in this model.
1130
1131 :param str machine: Machine id, e.g. '0'
1132 :param bool utc: Display time as UTC in RFC3339 format
1133
1134 """
1135 pass
1136
1137 def get_shares(self):
1138 """Return list of all users with access to this model.
1139
1140 """
1141 pass
1142
1143 def get_spaces(self):
1144 """Return list of all known spaces, including associated subnets.
1145
1146 """
1147 pass
1148
1149 def get_ssh_key(self):
1150 """Return known SSH keys for this model.
1151
1152 """
1153 pass
1154 get_ssh_keys = get_ssh_key
1155
1156 def get_storage(self, filesystem=False, volume=False):
1157 """Return details of storage instances.
1158
1159 :param bool filesystem: Include filesystem storage
1160 :param bool volume: Include volume storage
1161
1162 """
1163 pass
1164
1165 def get_storage_pools(self, names=None, providers=None):
1166 """Return list of storage pools.
1167
1168 :param list names: Only include pools with these names
1169 :param list providers: Only include pools for these providers
1170
1171 """
1172 pass
1173
1174 def get_subnets(self, space=None, zone=None):
1175 """Return list of known subnets.
1176
1177 :param str space: Only include subnets in this space
1178 :param str zone: Only include subnets in this zone
1179
1180 """
1181 pass
1182
1183 def remove_blocks(self):
1184 """Remove all blocks from this model.
1185
1186 """
1187 pass
1188
1189 def remove_backup(self, backup_id):
1190 """Delete a backup.
1191
1192 :param str backup_id: The id of the backup to remove
1193
1194 """
1195 pass
1196
1197 def remove_cached_images(self, arch=None, kind=None, series=None):
1198 """Remove cached OS images.
1199
1200 :param str arch: Architecture of the images to remove
1201 :param str kind: Image kind to remove, e.g. 'lxd'
1202 :param str series: Image series to remove, e.g. 'xenial'
1203
1204 """
1205 pass
1206
1207 def remove_machine(self, *machine_ids):
1208 """Remove a machine from this model.
1209
1210 :param str \*machine_ids: Ids of the machines to remove
1211
1212 """
1213 pass
1214 remove_machines = remove_machine
1215
1216 def remove_ssh_key(self, *keys):
1217 """Remove a public SSH key(s) from this model.
1218
1219 :param str \*keys: Keys to remove
1220
1221 """
1222 pass
1223 remove_ssh_keys = remove_ssh_key
1224
1225 def restore_backup(
1226 self, bootstrap=False, constraints=None, archive=None,
1227 backup_id=None, upload_tools=False):
1228 """Restore a backup archive to a new controller.
1229
1230 :param bool bootstrap: Bootstrap a new state machine
1231 :param constraints: Model constraints
1232 :type constraints: :class:`juju.Constraints`
1233 :param str archive: Path to backup archive to restore
1234 :param str backup_id: Id of backup to restore
1235 :param bool upload_tools: Upload tools if bootstrapping a new machine
1236
1237 """
1238 pass
1239
1240 def retry_provisioning(self):
1241 """Retry provisioning for failed machines.
1242
1243 """
1244 pass
1245
1246 def revoke(self, username, acl='read'):
1247 """Revoke a user's access to this model.
1248
1249 :param str username: Username to revoke
1250 :param str acl: Access control ('read' or 'write')
1251
1252 """
1253 pass
1254
1255 def run(self, command, timeout=None):
1256 """Run command on all machines in this model.
1257
1258 :param str command: The command to run
1259 :param int timeout: Time to wait before command is considered failed
1260
1261 """
1262 pass
1263
1264 def set_config(self, **config):
1265 """Set configuration keys on this model.
1266
1267 :param \*\*config: Config key/values
1268
1269 """
1270 pass
1271
1272 def set_constraints(self, constraints):
1273 """Set machine constraints on this model.
1274
1275 :param :class:`juju.Constraints` constraints: Machine constraints
1276
1277 """
1278 pass
1279
1280 def get_action_output(self, action_uuid, wait=-1):
1281 """Get the results of an action by ID.
1282
1283 :param str action_uuid: Id of the action
1284 :param int wait: Time in seconds to wait for action to complete
1285
1286 """
1287 pass
1288
1289 def get_action_status(self, uuid_or_prefix=None, name=None):
1290 """Get the status of all actions, filtered by ID, ID prefix, or action name.
1291
1292 :param str uuid_or_prefix: Filter by action uuid or prefix
1293 :param str name: Filter by action name
1294
1295 """
1296 pass
1297
1298 def get_budget(self, budget_name):
1299 """Get budget usage info.
1300
1301 :param str budget_name: Name of budget
1302
1303 """
1304 pass
1305
1306 def get_status(self, filter_=None, utc=False):
1307 """Return the status of the model.
1308
1309 :param str filter_: Service or unit name or wildcard ('*')
1310 :param bool utc: Display time as UTC in RFC3339 format
1311
1312 """
1313 pass
1314 status = get_status
1315
1316 def sync_tools(
1317 self, all_=False, destination=None, dry_run=False, public=False,
1318 source=None, stream=None, version=None):
1319 """Copy Juju tools into this model.
1320
1321 :param bool all_: Copy all versions, not just the latest
1322 :param str destination: Path to local destination directory
1323 :param bool dry_run: Don't do the actual copy
1324 :param bool public: Tools are for a public cloud, so generate mirrors
1325 information
1326 :param str source: Path to local source directory
1327 :param str stream: Simplestreams stream for which to sync metadata
1328 :param str version: Copy a specific major.minor version
1329
1330 """
1331 pass
1332
1333 def unblock(self, *commands):
1334 """Unblock an operation that would alter this model.
1335
1336 :param str \*commands: The commands to unblock. Valid values are
1337 'all-changes', 'destroy-model', 'remove-object'
1338
1339 """
1340 pass
1341
1342 def unset_config(self, *keys):
1343 """Unset configuration on this model.
1344
1345 :param str \*keys: The keys to unset
1346
1347 """
1348 pass
1349
1350 def upgrade_gui(self):
1351 """Upgrade the Juju GUI for this model.
1352
1353 """
1354 pass
1355
1356 def upgrade_juju(
1357 self, dry_run=False, reset_previous_upgrade=False,
1358 upload_tools=False, version=None):
1359 """Upgrade Juju on all machines in a model.
1360
1361 :param bool dry_run: Don't do the actual upgrade
1362 :param bool reset_previous_upgrade: Clear the previous (incomplete)
1363 upgrade status
1364 :param bool upload_tools: Upload local version of tools
1365 :param str version: Upgrade to a specific version
1366
1367 """
1368 pass
1369
1370 def upload_backup(self, archive_path):
1371 """Store a backup archive remotely in Juju.
1372
1373 :param str archive_path: Path to local archive
1374
1375 """
1376 pass
1377
1378 @property
1379 def charmstore(self):
1380 return self._charmstore
1381
1382 async def get_metrics(self, *tags):
1383 """Retrieve metrics.
1384
1385 :param str \*tags: Tags of entities from which to retrieve metrics.
1386 No tags retrieves the metrics of all units in the model.
1387 :return: Dictionary of unit_name:metrics
1388
1389 """
1390 log.debug("Retrieving metrics for %s",
1391 ', '.join(tags) if tags else "all units")
1392
1393 metrics_facade = client.MetricsDebugFacade()
1394 metrics_facade.connect(self.connection)
1395
1396 entities = [client.Entity(tag) for tag in tags]
1397 metrics_result = await metrics_facade.GetMetrics(entities)
1398
1399 metrics = collections.defaultdict(list)
1400
1401 for entity_metrics in metrics_result.results:
1402 error = entity_metrics.error
1403 if error:
1404 if "is not a valid tag" in error:
1405 raise ValueError(error.message)
1406 else:
1407 raise Exception(error.message)
1408
1409 for metric in entity_metrics.metrics:
1410 metrics[metric.unit].append(vars(metric))
1411
1412 return metrics
1413
1414
1415 def get_charm_series(path):
1416 """Inspects the charm directory at ``path`` and returns a default
1417 series from its metadata.yaml (the first item in the 'series' list).
1418
1419 Returns None if no series can be determined.
1420
1421 """
1422 md = Path(path) / "metadata.yaml"
1423 if not md.exists():
1424 return None
1425 data = yaml.load(md.open())
1426 series = data.get('series')
1427 return series[0] if series else None
1428
1429
1430 class BundleHandler(object):
1431 """
1432 Handle bundles by using the API to translate bundle YAML into a plan of
1433 steps and then dispatching each of those using the API.
1434 """
1435 def __init__(self, model):
1436 self.model = model
1437 self.charmstore = model.charmstore
1438 self.plan = []
1439 self.references = {}
1440 self._units_by_app = {}
1441 for unit_name, unit in model.units.items():
1442 app_units = self._units_by_app.setdefault(unit.application, [])
1443 app_units.append(unit_name)
1444 self.client_facade = client.ClientFacade()
1445 self.client_facade.connect(model.connection)
1446 self.app_facade = client.ApplicationFacade()
1447 self.app_facade.connect(model.connection)
1448 self.ann_facade = client.AnnotationsFacade()
1449 self.ann_facade.connect(model.connection)
1450
1451 async def _handle_local_charms(self, bundle):
1452 """Search for references to local charms (i.e. filesystem paths)
1453 in the bundle. Upload the local charms to the model, and replace
1454 the filesystem paths with appropriate 'local:' paths in the bundle.
1455
1456 Return the modified bundle.
1457
1458 :param dict bundle: Bundle dictionary
1459 :return: Modified bundle dictionary
1460
1461 """
1462 apps, args = [], []
1463
1464 default_series = bundle.get('series')
1465 for app_name in self.applications:
1466 app_dict = bundle['services'][app_name]
1467 charm_dir = os.path.abspath(os.path.expanduser(app_dict['charm']))
1468 if not os.path.isdir(charm_dir):
1469 continue
1470 series = (
1471 app_dict.get('series') or
1472 default_series or
1473 get_charm_series(charm_dir)
1474 )
1475 if not series:
1476 raise JujuError(
1477 "Couldn't determine series for charm at {}. "
1478 "Add a 'series' key to the bundle.".format(charm_dir))
1479
1480 # Keep track of what we need to update. We keep a list of apps
1481 # that need to be updated, and a corresponding list of args
1482 # needed to update those apps.
1483 apps.append(app_name)
1484 args.append((charm_dir, series))
1485
1486 if apps:
1487 # If we have apps to update, spawn all the coroutines concurrently
1488 # and wait for them to finish.
1489 charm_urls = await asyncio.gather(*[
1490 self.model.add_local_charm_dir(*params)
1491 for params in args
1492 ])
1493 # Update the 'charm:' entry for each app with the new 'local:' url.
1494 for app_name, charm_url in zip(apps, charm_urls):
1495 bundle['services'][app_name]['charm'] = charm_url
1496
1497 return bundle
1498
1499 async def fetch_plan(self, entity_id):
1500 is_local = not entity_id.startswith('cs:') and os.path.isdir(entity_id)
1501 if is_local:
1502 bundle_yaml = (Path(entity_id) / "bundle.yaml").read_text()
1503 else:
1504 bundle_yaml = await self.charmstore.files(entity_id,
1505 filename='bundle.yaml',
1506 read_file=True)
1507 self.bundle = yaml.safe_load(bundle_yaml)
1508 self.bundle = await self._handle_local_charms(self.bundle)
1509
1510 self.plan = await self.client_facade.GetBundleChanges(
1511 yaml.dump(self.bundle))
1512
1513 if self.plan.errors:
1514 raise JujuError('\n'.join(self.plan.errors))
1515
1516 async def execute_plan(self):
1517 for step in self.plan.changes:
1518 method = getattr(self, step.method)
1519 result = await method(*step.args)
1520 self.references[step.id_] = result
1521
1522 @property
1523 def applications(self):
1524 return list(self.bundle['services'].keys())
1525
1526 def resolve(self, reference):
1527 if reference and reference.startswith('$'):
1528 reference = self.references[reference[1:]]
1529 return reference
1530
1531 async def addCharm(self, charm, series):
1532 """
1533 :param charm string:
1534 Charm holds the URL of the charm to be added.
1535
1536 :param series string:
1537 Series holds the series of the charm to be added
1538 if the charm default is not sufficient.
1539 """
1540 # We don't add local charms because they've already been added
1541 # by self._handle_local_charms
1542 if charm.startswith('local:'):
1543 return charm
1544
1545 entity_id = await self.charmstore.entityId(charm)
1546 log.debug('Adding %s', entity_id)
1547 await self.client_facade.AddCharm(None, entity_id)
1548 return entity_id
1549
1550 async def addMachines(self, params=None):
1551 """
1552 :param params dict:
1553 Dictionary specifying the machine to add. All keys are optional.
1554 Keys include:
1555
1556 series: string specifying the machine OS series.
1557
1558 constraints: string holding machine constraints, if any. We'll
1559 parse this into the json friendly dict that the juju api
1560 expects.
1561
1562 container_type: string holding the type of the container (for
1563 instance ""lxd" or kvm"). It is not specified for top level
1564 machines.
1565
1566 parent_id: string holding a placeholder pointing to another
1567 machine change or to a unit change. This value is only
1568 specified in the case this machine is a container, in
1569 which case also ContainerType is set.
1570
1571 """
1572 params = params or {}
1573
1574 # Normalize keys
1575 params = {normalize_key(k): params[k] for k in params.keys()}
1576
1577 # Fix up values, as necessary.
1578 if 'parent_id' in params:
1579 params['parent_id'] = self.resolve(params['parent_id'])
1580
1581 params['constraints'] = parse_constraints(
1582 params.get('constraints'))
1583 params['jobs'] = params.get('jobs', ['JobHostUnits'])
1584
1585 if params.get('container_type') == 'lxc':
1586 log.warning('Juju 2.0 does not support lxc containers. '
1587 'Converting containers to lxd.')
1588 params['container_type'] = 'lxd'
1589
1590 # Submit the request.
1591 params = client.AddMachineParams(**params)
1592 results = await self.client_facade.AddMachines([params])
1593 error = results.machines[0].error
1594 if error:
1595 raise ValueError("Error adding machine: %s", error.message)
1596 machine = results.machines[0].machine
1597 log.debug('Added new machine %s', machine)
1598 return machine
1599
1600 async def addRelation(self, endpoint1, endpoint2):
1601 """
1602 :param endpoint1 string:
1603 :param endpoint2 string:
1604 Endpoint1 and Endpoint2 hold relation endpoints in the
1605 "application:interface" form, where the application is always a
1606 placeholder pointing to an application change, and the interface is
1607 optional. Examples are "$deploy-42:web" or just "$deploy-42".
1608 """
1609 endpoints = [endpoint1, endpoint2]
1610 # resolve indirect references
1611 for i in range(len(endpoints)):
1612 parts = endpoints[i].split(':')
1613 parts[0] = self.resolve(parts[0])
1614 endpoints[i] = ':'.join(parts)
1615
1616 log.info('Relating %s <-> %s', *endpoints)
1617 return await self.model.add_relation(*endpoints)
1618
1619 async def deploy(self, charm, series, application, options, constraints,
1620 storage, endpoint_bindings, resources):
1621 """
1622 :param charm string:
1623 Charm holds the URL of the charm to be used to deploy this
1624 application.
1625
1626 :param series string:
1627 Series holds the series of the application to be deployed
1628 if the charm default is not sufficient.
1629
1630 :param application string:
1631 Application holds the application name.
1632
1633 :param options map[string]interface{}:
1634 Options holds application options.
1635
1636 :param constraints string:
1637 Constraints holds the optional application constraints.
1638
1639 :param storage map[string]string:
1640 Storage holds the optional storage constraints.
1641
1642 :param endpoint_bindings map[string]string:
1643 EndpointBindings holds the optional endpoint bindings
1644
1645 :param resources map[string]int:
1646 Resources identifies the revision to use for each resource
1647 of the application's charm.
1648 """
1649 # resolve indirect references
1650 charm = self.resolve(charm)
1651 # stringify all config values for API
1652 options = {k: str(v) for k, v in options.items()}
1653 # build param object
1654 app = client.ApplicationDeploy(
1655 charm_url=charm,
1656 series=series,
1657 application=application,
1658 config=options,
1659 constraints=parse_constraints(constraints),
1660 storage=storage,
1661 endpoint_bindings=endpoint_bindings,
1662 resources=resources,
1663 )
1664 # do the do
1665 log.info('Deploying %s', charm)
1666 await self.app_facade.Deploy([app])
1667 # ensure the app is in the model for future operations
1668 await self.model._wait_for_new('application', application)
1669 return application
1670
1671 async def addUnit(self, application, to):
1672 """
1673 :param application string:
1674 Application holds the application placeholder name for which a unit
1675 is added.
1676
1677 :param to string:
1678 To holds the optional location where to add the unit, as a
1679 placeholder pointing to another unit change or to a machine change.
1680 """
1681 application = self.resolve(application)
1682 placement = self.resolve(to)
1683 if self._units_by_app.get(application):
1684 # enough units for this application already exist;
1685 # claim one, and carry on
1686 # NB: this should probably honor placement, but the juju client
1687 # doesn't, so we're not bothering, either
1688 unit_name = self._units_by_app[application].pop()
1689 log.debug('Reusing unit %s for %s', unit_name, application)
1690 return self.model.units[unit_name]
1691
1692 log.debug('Adding new unit for %s%s', application,
1693 ' to %s' % placement if placement else '')
1694 return await self.model.applications[application].add_unit(
1695 count=1,
1696 to=placement,
1697 )
1698
1699 async def expose(self, application):
1700 """
1701 :param application string:
1702 Application holds the placeholder name of the application that must
1703 be exposed.
1704 """
1705 application = self.resolve(application)
1706 log.info('Exposing %s', application)
1707 return await self.model.applications[application].expose()
1708
1709 async def setAnnotations(self, id_, entity_type, annotations):
1710 """
1711 :param id_ string:
1712 Id is the placeholder for the application or machine change
1713 corresponding to the entity to be annotated.
1714
1715 :param entity_type EntityType:
1716 EntityType holds the type of the entity, "application" or
1717 "machine".
1718
1719 :param annotations map[string]string:
1720 Annotations holds the annotations as key/value pairs.
1721 """
1722 entity_id = self.resolve(id_)
1723 try:
1724 entity = self.model.state.get_entity(entity_type, entity_id)
1725 except KeyError:
1726 entity = await self.model._wait_for_new(entity_type, entity_id)
1727 return await entity.set_annotations(annotations)
1728
1729
1730 class CharmStore(object):
1731 """
1732 Async wrapper around theblues.charmstore.CharmStore
1733 """
1734 def __init__(self, loop):
1735 self.loop = loop
1736 self._cs = charmstore.CharmStore()
1737
1738 def __getattr__(self, name):
1739 """
1740 Wrap method calls in coroutines that use run_in_executor to make them
1741 async.
1742 """
1743 attr = getattr(self._cs, name)
1744 if not callable(attr):
1745 wrapper = partial(getattr, self._cs, name)
1746 setattr(self, name, wrapper)
1747 else:
1748 async def coro(*args, **kwargs):
1749 method = partial(attr, *args, **kwargs)
1750 return await self.loop.run_in_executor(None, method)
1751 setattr(self, name, coro)
1752 wrapper = coro
1753 return wrapper
1754
1755
1756 class CharmArchiveGenerator(object):
1757 def __init__(self, path):
1758 self.path = os.path.abspath(os.path.expanduser(path))
1759
1760 def make_archive(self, path):
1761 """Create archive of directory and write to ``path``.
1762
1763 :param path: Path to archive
1764
1765 Ignored::
1766
1767 * build/\* - This is used for packing the charm itself and any
1768 similar tasks.
1769 * \*/.\* - Hidden files are all ignored for now. This will most
1770 likely be changed into a specific ignore list
1771 (.bzr, etc)
1772
1773 """
1774 zf = zipfile.ZipFile(path, 'w', zipfile.ZIP_DEFLATED)
1775 for dirpath, dirnames, filenames in os.walk(self.path):
1776 relative_path = dirpath[len(self.path) + 1:]
1777 if relative_path and not self._ignore(relative_path):
1778 zf.write(dirpath, relative_path)
1779 for name in filenames:
1780 archive_name = os.path.join(relative_path, name)
1781 if not self._ignore(archive_name):
1782 real_path = os.path.join(dirpath, name)
1783 self._check_type(real_path)
1784 if os.path.islink(real_path):
1785 self._check_link(real_path)
1786 self._write_symlink(
1787 zf, os.readlink(real_path), archive_name)
1788 else:
1789 zf.write(real_path, archive_name)
1790 zf.close()
1791 return path
1792
1793 def _check_type(self, path):
1794 """Check the path
1795 """
1796 s = os.stat(path)
1797 if stat.S_ISDIR(s.st_mode) or stat.S_ISREG(s.st_mode):
1798 return path
1799 raise ValueError("Invalid Charm at % %s" % (
1800 path, "Invalid file type for a charm"))
1801
1802 def _check_link(self, path):
1803 link_path = os.readlink(path)
1804 if link_path[0] == "/":
1805 raise ValueError(
1806 "Invalid Charm at %s: %s" % (
1807 path, "Absolute links are invalid"))
1808 path_dir = os.path.dirname(path)
1809 link_path = os.path.join(path_dir, link_path)
1810 if not link_path.startswith(os.path.abspath(self.path)):
1811 raise ValueError(
1812 "Invalid charm at %s %s" % (
1813 path, "Only internal symlinks are allowed"))
1814
1815 def _write_symlink(self, zf, link_target, link_path):
1816 """Package symlinks with appropriate zipfile metadata."""
1817 info = zipfile.ZipInfo()
1818 info.filename = link_path
1819 info.create_system = 3
1820 # Magic code for symlinks / py2/3 compat
1821 # 27166663808 = (stat.S_IFLNK | 0755) << 16
1822 info.external_attr = 2716663808
1823 zf.writestr(info, link_target)
1824
1825 def _ignore(self, path):
1826 if path == "build" or path.startswith("build/"):
1827 return True
1828 if path.startswith('.'):
1829 return True