K8s Juju connector
[osm/N2VC.git] / modules / libjuju / examples / action.py
1 """
2 This example:
3
4 1. Connects to current model and resets it.
5 2. Deploys a git unit.
6 3. Runs an action against the unit.
7 4. Waits for the action results to come back, then exits.
8
9 """
10 import logging
11
12 from juju import loop
13 from juju.model import Model
14
15
16 async def run_action(unit):
17 logging.debug('Running action on unit %s', unit.name)
18
19 # unit.run() returns a juju.action.Action instance
20 action = await unit.run_action('add-repo', repo='myrepo')
21 # wait for the action to complete
22 action = await action.wait()
23
24 logging.debug("Action results: %s", action.results)
25
26
27 async def main():
28 model = Model()
29 # connect to current model with current user, per Juju CLI
30 await model.connect()
31
32 app = await model.deploy(
33 'git',
34 application_name='git',
35 series='trusty',
36 channel='stable',
37 )
38
39 for unit in app.units:
40 await run_action(unit)
41
42 await model.disconnect()
43
44
45 if __name__ == '__main__':
46 logging.basicConfig(level=logging.DEBUG)
47 ws_logger = logging.getLogger('websockets.protocol')
48 ws_logger.setLevel(logging.INFO)
49 loop.run(main())