github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/acceptancetests/repository/xenial/mysql/hooks/lib/cluster_utils.py (about)

     1  #
     2  # Copyright 2012 Canonical Ltd.
     3  #
     4  # This file is sourced from lp:openstack-charm-helpers
     5  #
     6  # Authors:
     7  #  James Page <james.page@ubuntu.com>
     8  #  Adam Gandelman <adamg@ubuntu.com>
     9  #
    10  
    11  from lib.utils import (
    12      juju_log,
    13      relation_ids,
    14      relation_list,
    15      relation_get,
    16      get_unit_hostname,
    17      config_get
    18      )
    19  import subprocess
    20  import os
    21  
    22  
    23  def is_clustered():
    24      for r_id in (relation_ids('ha') or []):
    25          for unit in (relation_list(r_id) or []):
    26              clustered = relation_get('clustered',
    27                                       rid=r_id,
    28                                       unit=unit)
    29              if clustered:
    30                  return True
    31      return False
    32  
    33  
    34  def is_leader(resource):
    35      cmd = [
    36          "crm", "resource",
    37          "show", resource
    38          ]
    39      try:
    40          status = subprocess.check_output(cmd)
    41      except subprocess.CalledProcessError:
    42          return False
    43      else:
    44          if get_unit_hostname() in status:
    45              return True
    46          else:
    47              return False
    48  
    49  
    50  def peer_units():
    51      peers = []
    52      for r_id in (relation_ids('cluster') or []):
    53          for unit in (relation_list(r_id) or []):
    54              peers.append(unit)
    55      return peers
    56  
    57  
    58  def oldest_peer(peers):
    59      local_unit_no = os.getenv('JUJU_UNIT_NAME').split('/')[1]
    60      for peer in peers:
    61          remote_unit_no = peer.split('/')[1]
    62          if remote_unit_no < local_unit_no:
    63              return False
    64      return True
    65  
    66  
    67  def eligible_leader(resource):
    68      if is_clustered():
    69          if not is_leader(resource):
    70              juju_log('INFO', 'Deferring action to CRM leader.')
    71              return False
    72      else:
    73          peers = peer_units()
    74          if peers and not oldest_peer(peers):
    75              juju_log('INFO', 'Deferring action to oldest service unit.')
    76              return False
    77      return True
    78  
    79  
    80  def https():
    81      '''
    82      Determines whether enough data has been provided in configuration
    83      or relation data to configure HTTPS
    84      .
    85      returns: boolean
    86      '''
    87      if config_get('use-https') == "yes":
    88          return True
    89      if config_get('ssl_cert') and config_get('ssl_key'):
    90          return True
    91      for r_id in relation_ids('identity-service'):
    92          for unit in relation_list(r_id):
    93              if (relation_get('https_keystone', rid=r_id, unit=unit) and
    94                  relation_get('ssl_cert', rid=r_id, unit=unit) and
    95                  relation_get('ssl_key', rid=r_id, unit=unit) and
    96                  relation_get('ca_cert', rid=r_id, unit=unit)):
    97                  return True
    98      return False
    99  
   100  
   101  def determine_api_port(public_port):
   102      '''
   103      Determine correct API server listening port based on
   104      existence of HTTPS reverse proxy and/or haproxy.
   105  
   106      public_port: int: standard public port for given service
   107  
   108      returns: int: the correct listening port for the API service
   109      '''
   110      i = 0
   111      if len(peer_units()) > 0 or is_clustered():
   112          i += 1
   113      if https():
   114          i += 1
   115      return public_port - (i * 10)
   116  
   117  
   118  def determine_haproxy_port(public_port):
   119      '''
   120      Description: Determine correct proxy listening port based on public IP +
   121      existence of HTTPS reverse proxy.
   122  
   123      public_port: int: standard public port for given service
   124  
   125      returns: int: the correct listening port for the HAProxy service
   126      '''
   127      i = 0
   128      if https():
   129          i += 1
   130      return public_port - (i * 10)