Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

CORTX-33949:CSM consul Connection handling #898

Merged
merged 10 commits into from
Aug 30, 2022
88 changes: 88 additions & 0 deletions csm/common/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
# For any questions about this software or licensing,
# please email opensource@seagate.com or cortx-questions@seagate.com.

import time
from cortx.utils.conf_store import Conf
from cortx.utils.validator.error import VError
from csm.core.blogic import const
from cortx.utils.log import Log
from cortx.utils.validator.v_consul import ConsulV
from csm.common.service_urls import ServiceUrls
from cortx.utils.kv_store.error import KvError

class Utility:
"""
Expand All @@ -37,3 +45,83 @@ def remove_json_key(payload, key):
return [Utility.remove_json_key(element, key) for element in payload]
else:
return payload

@staticmethod
def is_consul_backend(conf):
"""
Check if backend for config is consul or not
Args:
conf (str): config path for configurations
Returns:
Boolean value
"""
result = False
if const.CONSUL in str(conf):
result = True
return result

@staticmethod
def get_consul_config():
"""
Get consul endpoint related details
pawankumarsrivastava marked this conversation as resolved.
Show resolved Hide resolved
"""
secret = Conf.get(const.CONSUMER_INDEX, const.CONSUL_SECRET_KEY)
protocol, host, port, consul_endpoint = '','','',''
count_endpoints : str = Conf.get(const.CONSUMER_INDEX,
const.CONSUL_NUM_ENDPOINTS_KEY)
try:
count_endpoints = int(count_endpoints)
except ValueError as e:
raise e
for count in range(count_endpoints):
endpoint = Conf.get(const.CONSUMER_INDEX,
f'{const.CONSUL_ENDPOINTS_KEY}[{count}]')
if endpoint:
protocol, host, port = ServiceUrls.parse_url(endpoint)
if protocol == "https" or protocol == "http":
consul_endpoint = endpoint
Log.info(f"Fetching consul endpoint : {consul_endpoint}")
break
return protocol, host, port, secret, consul_endpoint

@staticmethod
def validate_consul():
"""
Validate consul service
"""
_, consul_host, consul_port, _, _ = Utility.get_consul_config()
for retry in range(0, const.MAX_RETRY):
try:
Log.info(f"Connecting to consul retry count : {retry}")
ConsulV().validate_service_status(consul_host,consul_port)
break
except VError as e:
Log.error(f"Failed to connect with consul: {e}")
pawankumarsrivastava marked this conversation as resolved.
Show resolved Hide resolved
if retry == const.MAX_RETRY-1:
raise e
time.sleep(const.SLEEP_DURATION)

@staticmethod
def load_csm_config_indices(conf):
Log.info("Loading CSM configuration")
for retry in range(0, const.MAX_RETRY):
try:
Conf.load(const.CONSUMER_INDEX, conf)
break
except KvError as e:
if retry == const.MAX_RETRY-1:
raise e
Log.error(f"Unable to fetch the configuration: {e}")
time.sleep(const.SLEEP_DURATION)
_, consul_host, consul_port, _, _ = Utility.get_consul_config()
if consul_host and consul_port:
try:
if not Utility.is_consul_backend(conf):
Utility.validate_consul()
Conf.load(const.CSM_GLOBAL_INDEX,
f"consul://{consul_host}:{consul_port}/{const.CSM_CONF_BASE}")
Conf.load(const.DATABASE_INDEX,
f"consul://{consul_host}:{consul_port}/{const.DATABASE_CONF_BASE}")
except (VError, KvError) as e:
Log.error(f"Unable to fetch the configurations from consul: {e}")
raise e
13 changes: 10 additions & 3 deletions csm/conf/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
from csm.common.errors import CSM_OPERATION_SUCESSFUL
from cortx.utils.message_bus import MessageBusAdmin,MessageBus
from cortx.utils.message_bus.error import MessageBusError
from csm.common.utility import Utility
from cortx.utils.validator.error import VError


class Configure(Setup):
Expand All @@ -50,11 +52,11 @@ async def execute(self, command):
:return:
"""
try:
Conf.load(const.CONSUMER_INDEX, command.options.get(const.CONFIG_URL))
conf = command.options.get(const.CONFIG_URL)
Utility.load_csm_config_indices(conf)
Setup.setup_logs_init()
Log.info("Setup: Initiating Config phase.")
Setup.load_csm_config_indices()
except KvError as e:
except (KvError, VError) as e:
Log.error(f"Config: Configuration loading failed {e}")
raise CsmSetupError("Could Not Load Url Provided in Kv Store.")

Expand Down Expand Up @@ -134,6 +136,11 @@ def create(self):
if self._is_env_dev:
Conf.set(const.CSM_GLOBAL_INDEX, f"{const.DEPLOYMENT}>{const.MODE}",
const.DEV)
try:
Utility.validate_consul()
except VError as e:
Log.error(f"Unable to save the configurations to consul: {e}")
raise CsmSetupError("Unable to save the configurations")
Conf.save(const.CSM_GLOBAL_INDEX)
Conf.save(const.DATABASE_INDEX)

Expand Down
8 changes: 5 additions & 3 deletions csm/conf/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
# please email opensource@seagate.com or cortx-questions@seagate.com.

from cortx.utils.log import Log
from cortx.utils.conf_store.conf_store import Conf
from cortx.utils.kv_store.error import KvError
from csm.conf.setup import Setup, CsmSetupError
from csm.core.blogic import const
from csm.core.providers.providers import Response
from csm.common.errors import CSM_OPERATION_SUCESSFUL
from csm.common.utility import Utility
from cortx.utils.validator.error import VError

class Init(Setup):
"""Perform init operation for csm_setup."""
Expand All @@ -36,10 +37,11 @@ async def execute(self, command):
:return:
"""
try:
Conf.load(const.CONSUMER_INDEX, command.options.get(const.CONFIG_URL))
conf = command.options.get(const.CONFIG_URL)
Utility.load_csm_config_indices(conf)
Setup.setup_logs_init()
Log.info("Init: Initiating Init phase.")
except KvError as e:
except (KvError, VError) as e:
Log.error(f"Init: Configuration Loading Failed {e}")
raise CsmSetupError("Could Not Load Url Provided in Kv Store.")

Expand Down
13 changes: 9 additions & 4 deletions csm/conf/post_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from csm.common.errors import CSM_OPERATION_SUCESSFUL
from cortx.utils.errors import SSLCertificateError
from csm.common.service_urls import ServiceUrls
from csm.common.utility import Utility

class PostInstall(Setup):
"""
Expand All @@ -48,13 +49,12 @@ async def execute(self, command):
:return:
"""
try:
Conf.load(const.CONSUMER_INDEX, command.options.get(
const.CONFIG_URL))
conf = command.options.get(const.CONFIG_URL)
Utility.load_csm_config_indices(conf)
Setup.setup_logs_init()
Log.info("Setup: Initiating Post Install phase.")
Setup.load_csm_config_indices()
Setup.copy_base_configs()
except KvError as e:
except (KvError, VError) as e:
Log.error(f"Post Install: Configuration Loading Failed {e}")
raise CsmSetupError("Could Not Load Url Provided in Kv Store.")
services = command.options.get("services")
Expand Down Expand Up @@ -121,4 +121,9 @@ def create(self):
if self._is_env_dev:
Conf.set(const.CSM_GLOBAL_INDEX, f"{const.DEPLOYMENT}>{const.MODE}",
const.DEV)
try:
Utility.validate_consul()
except VError as e:
Log.error(f"Unable to save the configurations to consul: {e}")
raise CsmSetupError("Unable to save the configurations")
Conf.save(const.CSM_GLOBAL_INDEX)
14 changes: 10 additions & 4 deletions csm/conf/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from csm.core.blogic import const
from csm.common.errors import CSM_OPERATION_SUCESSFUL
from cortx.utils.validator.error import VError
from csm.common.utility import Utility


class Prepare(Setup):
Expand All @@ -44,11 +45,11 @@ async def execute(self, command):
:return:
"""
try:
Conf.load(const.CONSUMER_INDEX, command.options.get(const.CONFIG_URL))
conf = command.options.get(const.CONFIG_URL)
Utility.load_csm_config_indices(conf)
Setup.setup_logs_init()
Log.info("Setup: Initiating Prepare phase.")
Setup.load_csm_config_indices()
except KvError as e:
except (KvError, VError) as e:
Log.error(f"Prepare: Configuration Loading Failed {e}")
raise CsmSetupError("Could Not Load Url Provided in Kv Store.")

Expand Down Expand Up @@ -113,7 +114,7 @@ def _set_db_host_addr():
:return:
"""
Log.info("Prepare: Setting database host address")
_, consul_host, consul_port, secret, _ = Setup.get_consul_config()
_, consul_host, consul_port, secret, _ = Utility.get_consul_config()
consul_login = Conf.get(const.CONSUMER_INDEX, const.CONSUL_ADMIN_KEY)
try:
if consul_host and consul_port:
Expand Down Expand Up @@ -145,5 +146,10 @@ def create(self):
if self._is_env_dev:
Conf.set(const.CSM_GLOBAL_INDEX, const.CSM_DEPLOYMENT_MODE,
const.DEV)
try:
Utility.validate_consul()
except VError as e:
Log.error(f"Unable to save the configurations to consul: {e}")
raise CsmSetupError("Unable to save the configurations")
Conf.save(const.CSM_GLOBAL_INDEX)
Conf.save(const.DATABASE_INDEX)
52 changes: 0 additions & 52 deletions csm/conf/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,58 +52,6 @@ def _set_csm_conf_path():
Log.info(f"Setting Config saving path:{conf_path} from confstore")
return conf_path

@staticmethod
def get_consul_config():
result : bool = False
secret = Conf.get(const.CONSUMER_INDEX, const.CONSUL_SECRET_KEY)
protocol, host, port, consul_endpoint = '','','',''
count_endpoints : str = Conf.get(const.CONSUMER_INDEX,
const.CONSUL_NUM_ENDPOINTS_KEY)
try:
count_endpoints = int(count_endpoints)
except ValueError:
raise CsmSetupError("Consul num_endpoints value is not a valid"
" integer.")
for count in range(count_endpoints):
endpoint = Conf.get(const.CONSUMER_INDEX,
f'{const.CONSUL_ENDPOINTS_KEY}[{count}]')
if endpoint:
protocol, host, port = ServiceUrls.parse_url(endpoint)
if protocol == "https" or protocol == "http":
result = True
consul_endpoint = endpoint
Log.info(f"Fetching consul endpoint : {consul_endpoint}")
break
if not result:
raise CsmSetupError("Consul endpoint not found.")
return protocol, host, port, secret, consul_endpoint

@staticmethod
def load_csm_config_indices():
Log.info("Loading CSM configuration")
set_config_flag = False
_, consul_host, consul_port, _, _ = Setup.get_consul_config()
if consul_host and consul_port:
try:
ConsulV().validate_service_status(consul_host,consul_port)
Conf.load(const.CSM_GLOBAL_INDEX,
f"consul://{consul_host}:{consul_port}/{const.CSM_CONF_BASE}")
Conf.load(const.DATABASE_INDEX,
f"consul://{consul_host}:{consul_port}/{const.DATABASE_CONF_BASE}")
set_config_flag = True
except VError as ve:
Log.error(f"Unable to fetch the configurations from consul: {ve}")
raise CsmSetupError("Unable to fetch the configurations")

if not set_config_flag:
config_path = Setup._set_csm_conf_path()
Log.info(f"Setting CSM configuration to local storage: {config_path}")
Conf.load(const.CSM_GLOBAL_INDEX,
f"yaml://{config_path}/{const.CSM_CONF_FILE_NAME}")
Conf.load(const.DATABASE_INDEX,
f"yaml://{config_path}/{const.DB_CONF_FILE_NAME}")
set_config_flag = True

@staticmethod
def copy_base_configs():
Log.info("Copying Csm base configurations to destination indices")
Expand Down
8 changes: 5 additions & 3 deletions csm/conf/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from cortx.utils.conf_store import Conf
from csm.conf.setup import Setup, CsmSetupError
from cortx.utils.kv_store.error import KvError
from csm.common.utility import Utility
from cortx.utils.validator.error import VError

class Upgrade(Setup):
"""Perform upgrade operation for csm_setup."""
Expand All @@ -31,12 +33,12 @@ def __init__(self):
async def execute(self, command):
Log.info("Performing upgrade and loading config files")
try:
Conf.load(const.CONSUMER_INDEX, command.options.get(const.CONFIG_URL))
conf = command.options.get(const.CONFIG_URL)
Utility.load_csm_config_indices(conf)
Setup.setup_logs_init()
Log.info("Executing csm_setup: upgrade phase.")
Setup.load_csm_config_indices()
Setup.load_default_config()
except KvError as e:
except (KvError, VError) as e:
Log.error(f"Configuration Loading Failed {e}")
raise CsmSetupError("Could Not Load Url Provided in Kv Store, Unable to load configurations")

Expand Down
Loading