<commit_before>"""
Deploy a charm, wait until it's idle, then destroy the unit and application.

"""
import asyncio
import logging

from juju.model import Model, ModelObserver
from juju.client.connection import Connection


class MyModelObserver(ModelObserver):
    def on_unit_add(self, delta, old, new, model):
        logging.info(
            'New unit added: %s', new.name)

    def on_change(self, delta, old, new, model):
        for unit in model.units.values():
            unit_status = unit.data['agent-status']['current']
            logging.info(
                'Unit %s status: %s', unit.name, unit_status)
            if unit_status == 'idle':
                logging.info(
                    'Destroying unit %s', unit.name)
                loop.create_task(unit.destroy())

    def on_unit_remove(self, delta, old, new, model):
        app_name = old.application
        app = model.applications[app_name]
        if not app.units:
            logging.info(
                'Destroying application %s', app.name)
            loop.create_task(app.destroy())


async def run():
    conn = await Connection.connect_current()
    model = Model(conn)
    model.add_observer(MyModelObserver())
    await model.deploy(
        'ubuntu-0',
        service_name='ubuntu',
        series='trusty',
        channel='stable',
    )
    await model.watch()


logging.basicConfig(level=logging.INFO)
loop = asyncio.get_event_loop()
loop.run_until_complete(run())<commit_msg>Fix the deploy example<commit_after>"""
This example:

1. Connects to the current model
2. Resets it
3. Deploy a charm and waits until it's idle
4. Destroys the unit and application

"""
import asyncio
import logging

from juju.model import Model, ModelObserver


class MyModelObserver(ModelObserver):
    async def on_unit_add(self, delta, old, new, model):
        logging.info(
            'New unit added: %s', new.name)

    async def on_change(self, delta, old, new, model):
        for unit in model.units.values():
            unit_status = unit.data['agent-status']['current']
            logging.info(
                'Unit %s status: %s', unit.name, unit_status)
            if unit_status == 'idle':
                logging.info(
                    'Destroying unit %s', unit.name)
                loop.create_task(unit.destroy())

    async def on_unit_remove(self, delta, old, new, model):
        app_name = old.application
        app = model.applications[app_name]
        if not app.units:
            logging.info(
                'Destroying application %s', app.name)
            loop.create_task(app.destroy())
            await model.block_until(
                lambda: len(model.applications) == 0
            )
            await model.disconnect()
            model.loop.stop()


async def run():
    model = Model()
    await model.connect_current()

    await model.reset(force=True)
    model.add_observer(MyModelObserver())

    await model.deploy(
        'ubuntu-0',
        service_name='ubuntu',
        series='trusty',
        channel='stable',
    )


logging.basicConfig(level=logging.DEBUG)
ws_logger = logging.getLogger('websockets.protocol')
ws_logger.setLevel(logging.INFO)
loop = asyncio.get_event_loop()
loop.set_debug(False)
loop.create_task(run())
loop.run_forever()
{'fraction_non_alphanumeric': 0.11269744835965978, 'fraction_numerical': 0.00212636695018226, 'mean_word_length': 2.4481675392670157, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5172316', 'n_tokens_mistral': 984, 'n_tokens_neox': 950, 'n_words': 244}