Last active
January 18, 2017 20:17
-
-
Save jamesbeedy/dad808872e5488b43cf3fa5d5f2db87c to your computer and use it in GitHub Desktop.
simple_juju_action.py
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.5 | |
# This code runs a custom action called update-app | |
# on all units of app-web and app-worker applications | |
# Must have the following environment vars available | |
# - JUJU_USER | |
# - JUJU_PASSWORD | |
# - CONTROLLER_ENDPOINT | |
# - MODEL_UUID | |
# - GIT_COMMIT | |
import functools | |
import os | |
import sys | |
import signal | |
import asyncio | |
import aiohttp | |
import logging | |
from juju.model import Model | |
from juju.unit import Unit | |
username = os.environ[‘JUJU_USER’] | |
password = os.environ[‘JUJU_PASSWORD’] | |
controller_endpoint = os.environ[‘CONTROLLER_ENDPOINT’] | |
model_uuid = os.environ[‘MODEL_UUID’] | |
cacert = None | |
APPS_TO_UPDATE = ['app-web', 'app-worker'] | |
async def connect_to_model(model): | |
await model.connect( | |
controller_endpoint, | |
model_uuid, | |
username, | |
password, | |
cacert) | |
async def main(loop): | |
model = Model(loop=loop) | |
await connect_to_model(model) | |
# Run update-app action on all units in APPS | |
for application in APPS_TO_UPDATE: | |
for unit_name, unit in model.applications[application].units.items(): | |
run_app_action = await unit.run_action('update-app', | |
commit_or_branch=os.environ['GIT_COMMIT']) | |
app_action = await run_app_action.wait() | |
print(app_action.results) | |
await model.disconnect() | |
model.loop.stop() | |
logging.getLogger().setLevel(logging.INFO) | |
loop = asyncio.get_event_loop() | |
loop.set_debug(False) | |
loop.create_task(main(loop)) | |
loop.run_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment