Created
December 23, 2016 23:46
-
-
Save jamesbeedy/4126ccd238771a0c2e4b96336209959f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import asyncio | |
import functools | |
import logging | |
import signal | |
import sys | |
from concurrent.futures import CancelledError | |
from juju.model import Model | |
username = sys.argv[1] | |
password = sys.argv[2] | |
controller_endpoint = sys.argv[3] | |
model_uuid = sys.argv[4] | |
cacert = None | |
model = Model() | |
async def connect_to_model(): | |
await model.connect( | |
controller_endpoint, | |
model_uuid, | |
username, | |
password, | |
cacert) | |
def shutdown(loop): | |
logging.info('received stop signal, cancelling tasks...') | |
for task in asyncio.Task.all_tasks(): | |
task.cancel() | |
logging.info('bye, exiting in a minute...') | |
def main(): | |
logging.getLogger().setLevel(logging.INFO) | |
loop = asyncio.get_event_loop() | |
loop.add_signal_handler(signal.SIGHUP, functools.partial(shutdown, loop)) | |
loop.add_signal_handler(signal.SIGTERM, functools.partial(shutdown, loop)) | |
try: | |
loop.run_until_complete(connect_to_model()) | |
except KeyboardInterrupt: | |
pass | |
loop.close() | |
sys.exit(1) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment