github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/acceptancetests/jujupy/binaries.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  import os
    23  
    24  from deploy_stack import (
    25      BootstrapManager
    26  )
    27  from jujupy.client import (
    28      client_from_config
    29  )
    30  
    31  __metaclass__ = type
    32  
    33  log = logging.getLogger(__name__)
    34  
    35  
    36  def get_stable_juju(args, stable_juju_bin=None):
    37      """Get the installed stable version of juju.
    38  
    39      We need a stable version of juju to boostrap and migrate from to the newer
    40      development version of juju.
    41  
    42      If no juju path is provided try some well known paths in an attempt to find
    43      a system installed juju that will suffice.
    44      Note. this function does not check if the found juju is a suitable version
    45      for this test, just that the binary exists and is executable.
    46  
    47      :param stable_juju_bin: Path to the juju binary to be used and considered
    48        stable
    49      :raises RuntimeError: If there is no valid installation of juju available.
    50      :return: BootstrapManager object for the stable juju.
    51      """
    52      if stable_juju_bin is not None:
    53          try:
    54              client = client_from_config(
    55                  args.env,
    56                  stable_juju_bin,
    57                  debug=args.debug)
    58              log.info('Using {} for stable juju'.format(stable_juju_bin))
    59              return BootstrapManager.from_client(args, client)
    60          except OSError as e:
    61              raise RuntimeError(
    62                  'Provided stable juju path is not valid: {}'.format(e))
    63      known_juju_paths = (
    64          '{}/bin/juju'.format(os.environ.get('GOPATH')),
    65          '/snap/bin/juju',
    66          '/usr/bin/juju')
    67  
    68      for path in known_juju_paths:
    69          try:
    70              client = client_from_config(
    71                  args.env,
    72                  path,
    73                  debug=args.debug)
    74              log.info('Using {} for stable juju'.format(path))
    75              return BootstrapManager.from_client(args, client)
    76          except OSError:
    77              log.debug('Attempt at using {} failed.'.format(path))
    78              pass
    79  
    80      raise RuntimeError('Unable to get a stable system juju binary.')