github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/acceptancetests/jujupy/configuration.py (about)

     1  # This file is part of JujuPy, a library for driving the Juju CLI.
     2  # Copyright 2013-2017 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  
    17  """Low-level access to Juju configuration."""
    18  
    19  import os
    20  import re
    21  import subprocess
    22  
    23  import yaml
    24  
    25  
    26  class NoSuchEnvironment(Exception):
    27      """Raised when a specified environment does not exist."""
    28  
    29  
    30  def get_selected_environment(selected):
    31      if selected is None:
    32          selected = default_env()
    33      environments = get_environments()
    34      env = environments.get(selected)
    35      if env is None:
    36          raise NoSuchEnvironment(
    37              'Environment "{}" does not exist.'.format(selected))
    38      return get_environments()[selected], selected
    39  
    40  
    41  def get_juju_home():
    42      home = os.environ.get('JUJU_HOME')
    43      if home is None:
    44          home = os.path.join(os.environ.get('HOME'), '.juju')
    45      return home
    46  
    47  
    48  def get_juju_data():
    49      """Return the configured juju data directory.
    50  
    51      Assumes non-Windows.  Follows Juju's algorithm.
    52  
    53      If JUJU_DATA is set, it is returned.
    54      If XDG_DATA_HOME is set, 'juju' is appended to it.
    55      Otherwise, .local/share/juju is appended to HOME.
    56      """
    57      juju_data = os.environ.get('JUJU_DATA')
    58      if juju_data is not None:
    59          return juju_data
    60      data_home = os.environ.get('XDG_DATA_HOME')
    61      if data_home is None:
    62          data_home = os.path.join(os.environ['HOME'], '.local', 'share')
    63      return os.path.join(data_home, 'juju')
    64  
    65  
    66  def get_environments_path(juju_home):
    67      return os.path.join(juju_home, 'environments.yaml')
    68  
    69  
    70  def get_bootstrap_config_path(juju_data_dir):
    71      return os.path.join(juju_data_dir, 'bootstrap-config.yaml')
    72  
    73  
    74  def get_environments():
    75      """Return the environments for juju."""
    76      home = get_juju_home()
    77      with open(get_environments_path(home)) as env:
    78          return yaml.safe_load(env)['environments']
    79  
    80  
    81  def default_env():
    82      """Determine Juju's default environment."""
    83      output = subprocess.check_output(['juju', 'switch'])
    84      match = re.search('\"(.*)\"', output)
    85      if match is None:
    86          return output.rstrip('\n')
    87      return match.group(1)