Skip to content

Commit bd01785

Browse files
authored
Merge pull request #725 from cderici/example-revisit-unitrun
#725 #### Description This revisits the example located at `examples/unitrun.py` and reflects the change happened to the `unit.run()`. The most crucial thing is the added line : `await action.wait()`. #### QA Steps At the top directory, run: ``` python examples/unitrun.py ``` Aside from the asyncio dump (will be fixed in the near future), we should see: ``` Action status: completed Action results: {'return-code': 0, 'stdout': '10.42.51.209 '} ```
2 parents 94a3677 + 9b77c54 commit bd01785

1 file changed

Lines changed: 26 additions & 6 deletions

File tree

examples/unitrun.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,20 @@
1515

1616
async def run_command(unit):
1717
logging.debug('Running command on unit %s', unit.name)
18-
19-
# unit.run() returns a juju.action.Action instance
18+
# unit.run() returns a juju.action.Action instance,
19+
# it needs to be wait()'ed to get the results
2020
action = await unit.run('unit-get public-address')
21-
logging.debug("Action results: %s", action.results)
21+
await action.wait()
22+
23+
out1 = f"Action status: {action.status}"
24+
logging.debug(out1)
25+
print(out1)
26+
# 'completed'
27+
28+
out2 = f"Action results: {action.results}"
29+
logging.debug(out2)
30+
print(out2)
31+
# {'return-code': 0, 'stdout': '<IP Addr>\n'}
2232

2333

2434
async def main():
@@ -36,11 +46,21 @@ async def main():
3646
for unit in app.units:
3747
await run_command(unit)
3848

49+
await app.remove()
50+
3951
await model.disconnect()
4052

4153

4254
if __name__ == '__main__':
43-
logging.basicConfig(level=logging.DEBUG)
44-
ws_logger = logging.getLogger('websockets.protocol')
45-
ws_logger.setLevel(logging.INFO)
55+
# Uncomment below to get logging output
56+
57+
# logging.basicConfig(level=logging.DEBUG)
58+
# ws_logger = logging.getLogger('websockets.protocol')
59+
# ws_logger.setLevel(logging.INFO)
60+
"""
61+
Should see something like the following in the output if using LOGGING
62+
63+
DEBUG:root:Action status: completed
64+
DEBUG:root:Action results: {'return-code': 0, 'stdout': '10.42.51.101\n'}
65+
"""
4666
jasyncio.run(main())

0 commit comments

Comments
 (0)