github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/acceptancetests/jujupy/models.py (about) 1 # This file is part of JujuPy, a library for driving the Juju CLI. 2 # Copyright 2013-2018 Canonical Ltd. 3 # 4 # This program is free software: you can redistribute it and/or modify it 5 # under the terms of the Lesser GNU General Public License version 3, as 6 # published by the Free Software Foundation. 7 # 8 # This program is distributed in the hope that it will be useful, but WITHOUT 9 # ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, 10 # SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser 11 # GNU General Public License for more details. 12 # 13 # You should have received a copy of the Lesser GNU General Public License 14 # along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 # Functionality for handling installed or other juju binaries 17 # (including paths etc.) 18 19 from __future__ import print_function 20 21 import logging 22 23 from contextlib import contextmanager 24 from subprocess import CalledProcessError 25 26 __metaclass__ = type 27 28 log = logging.getLogger(__name__) 29 30 31 @contextmanager 32 def temporary_model(client, model_name): 33 """Create a new model that is cleaned up once it's done with.""" 34 try: 35 new_client = client.add_model(model_name) 36 yield new_client 37 finally: 38 try: 39 log.info('Destroying temp model "{}"'.format(model_name)) 40 new_client.destroy_model() 41 except CalledProcessError: 42 log.error('Failed to cleanup model.')