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

Commit 6572c40

Browse files
authored
CORTX-33949:CSM consul Connection handling (#898)
* CORTX-33949:CSM consul Connection handling Signed-off-by: Pranali Ugale <[email protected]> * CORTX-33949:CSM consul Connection handling Signed-off-by: Pranali Ugale <[email protected]> * CORTX-33949:CSM consul Connection handling Signed-off-by: Pranali Ugale <[email protected]> * CORTX-33949:CSM consul Connection handling Signed-off-by: Pranali Ugale <[email protected]> * CORTX-33949:resolve codeacy issues Signed-off-by: Pranali Ugale <[email protected]> * CORTX-33949:CSM consul Connection handling Signed-off-by: Pranali Ugale <[email protected]> * CORTX-33949:CSM consul Connection handling Signed-off-by: Pranali Ugale <[email protected]> * CORTX-33949:CSM consul Connection handling Signed-off-by: Pranali Ugale <[email protected]> * CORTX-33949:resolve codeacy issues Signed-off-by: Pranali Ugale <[email protected]> Signed-off-by: Pranali Ugale <[email protected]>
1 parent 4ab4165 commit 6572c40

File tree

13 files changed

+280
-138
lines changed

13 files changed

+280
-138
lines changed

csm/common/utility.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313
# For any questions about this software or licensing,
1414
1515

16+
import time
17+
from cortx.utils.conf_store import Conf
18+
from cortx.utils.validator.error import VError
19+
from csm.core.blogic import const
20+
from cortx.utils.log import Log
21+
from cortx.utils.validator.v_consul import ConsulV
22+
from csm.common.service_urls import ServiceUrls
23+
from cortx.utils.kv_store.error import KvError
1624

1725
class Utility:
1826
"""
@@ -37,3 +45,83 @@ def remove_json_key(payload, key):
3745
return [Utility.remove_json_key(element, key) for element in payload]
3846
else:
3947
return payload
48+
49+
@staticmethod
50+
def is_consul_backend(conf):
51+
"""
52+
Check if backend for config is consul or not
53+
Args:
54+
conf (str): config path for configurations
55+
Returns:
56+
Boolean value
57+
"""
58+
result = False
59+
if const.CONSUL in str(conf):
60+
result = True
61+
return result
62+
63+
@staticmethod
64+
def get_consul_config():
65+
"""
66+
Get consul endpoint related details
67+
"""
68+
secret = Conf.get(const.CONSUMER_INDEX, const.CONSUL_SECRET_KEY)
69+
protocol, host, port, consul_endpoint = '','','',''
70+
count_endpoints : str = Conf.get(const.CONSUMER_INDEX,
71+
const.CONSUL_NUM_ENDPOINTS_KEY)
72+
try:
73+
count_endpoints = int(count_endpoints)
74+
except ValueError as e:
75+
raise e
76+
for count in range(count_endpoints):
77+
endpoint = Conf.get(const.CONSUMER_INDEX,
78+
f'{const.CONSUL_ENDPOINTS_KEY}[{count}]')
79+
if endpoint:
80+
protocol, host, port = ServiceUrls.parse_url(endpoint)
81+
if protocol == "https" or protocol == "http":
82+
consul_endpoint = endpoint
83+
Log.info(f"Fetching consul endpoint : {consul_endpoint}")
84+
break
85+
return protocol, host, port, secret, consul_endpoint
86+
87+
@staticmethod
88+
def validate_consul():
89+
"""
90+
Validate consul service
91+
"""
92+
_, consul_host, consul_port, _, _ = Utility.get_consul_config()
93+
for retry in range(0, const.MAX_RETRY):
94+
try:
95+
Log.info(f"Connecting to consul retry count : {retry}")
96+
ConsulV().validate_service_status(consul_host,consul_port)
97+
break
98+
except VError as e:
99+
Log.error(f"Failed to connect with consul: {e}")
100+
if retry == const.MAX_RETRY-1:
101+
raise e
102+
time.sleep(const.SLEEP_DURATION)
103+
104+
@staticmethod
105+
def load_csm_config_indices(conf):
106+
Log.info("Loading CSM configuration")
107+
for retry in range(0, const.MAX_RETRY):
108+
try:
109+
Conf.load(const.CONSUMER_INDEX, conf)
110+
break
111+
except KvError as e:
112+
if retry == const.MAX_RETRY-1:
113+
raise e
114+
Log.error(f"Unable to fetch the configuration: {e}")
115+
time.sleep(const.SLEEP_DURATION)
116+
_, consul_host, consul_port, _, _ = Utility.get_consul_config()
117+
if consul_host and consul_port:
118+
try:
119+
if not Utility.is_consul_backend(conf):
120+
Utility.validate_consul()
121+
Conf.load(const.CSM_GLOBAL_INDEX,
122+
f"consul://{consul_host}:{consul_port}/{const.CSM_CONF_BASE}")
123+
Conf.load(const.DATABASE_INDEX,
124+
f"consul://{consul_host}:{consul_port}/{const.DATABASE_CONF_BASE}")
125+
except (VError, KvError) as e:
126+
Log.error(f"Unable to fetch the configurations from consul: {e}")
127+
raise e

csm/conf/configure.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
from csm.common.errors import CSM_OPERATION_SUCESSFUL
2727
from cortx.utils.message_bus import MessageBusAdmin,MessageBus
2828
from cortx.utils.message_bus.error import MessageBusError
29+
from csm.common.utility import Utility
30+
from cortx.utils.validator.error import VError
2931

3032

3133
class Configure(Setup):
@@ -50,11 +52,11 @@ async def execute(self, command):
5052
:return:
5153
"""
5254
try:
53-
Conf.load(const.CONSUMER_INDEX, command.options.get(const.CONFIG_URL))
55+
conf = command.options.get(const.CONFIG_URL)
56+
Utility.load_csm_config_indices(conf)
5457
Setup.setup_logs_init()
5558
Log.info("Setup: Initiating Config phase.")
56-
Setup.load_csm_config_indices()
57-
except KvError as e:
59+
except (KvError, VError) as e:
5860
Log.error(f"Config: Configuration loading failed {e}")
5961
raise CsmSetupError("Could Not Load Url Provided in Kv Store.")
6062

@@ -134,6 +136,11 @@ def create(self):
134136
if self._is_env_dev:
135137
Conf.set(const.CSM_GLOBAL_INDEX, f"{const.DEPLOYMENT}>{const.MODE}",
136138
const.DEV)
139+
try:
140+
Utility.validate_consul()
141+
except VError as e:
142+
Log.error(f"Unable to save the configurations to consul: {e}")
143+
raise CsmSetupError("Unable to save the configurations")
137144
Conf.save(const.CSM_GLOBAL_INDEX)
138145
Conf.save(const.DATABASE_INDEX)
139146

csm/conf/init.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
1515

1616
from cortx.utils.log import Log
17-
from cortx.utils.conf_store.conf_store import Conf
1817
from cortx.utils.kv_store.error import KvError
1918
from csm.conf.setup import Setup, CsmSetupError
2019
from csm.core.blogic import const
2120
from csm.core.providers.providers import Response
2221
from csm.common.errors import CSM_OPERATION_SUCESSFUL
22+
from csm.common.utility import Utility
23+
from cortx.utils.validator.error import VError
2324

2425
class Init(Setup):
2526
"""Perform init operation for csm_setup."""
@@ -36,10 +37,11 @@ async def execute(self, command):
3637
:return:
3738
"""
3839
try:
39-
Conf.load(const.CONSUMER_INDEX, command.options.get(const.CONFIG_URL))
40+
conf = command.options.get(const.CONFIG_URL)
41+
Utility.load_csm_config_indices(conf)
4042
Setup.setup_logs_init()
4143
Log.info("Init: Initiating Init phase.")
42-
except KvError as e:
44+
except (KvError, VError) as e:
4345
Log.error(f"Init: Configuration Loading Failed {e}")
4446
raise CsmSetupError("Could Not Load Url Provided in Kv Store.")
4547

csm/conf/post_install.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from csm.common.errors import CSM_OPERATION_SUCESSFUL
2626
from cortx.utils.errors import SSLCertificateError
2727
from csm.common.service_urls import ServiceUrls
28+
from csm.common.utility import Utility
2829

2930
class PostInstall(Setup):
3031
"""
@@ -48,13 +49,12 @@ async def execute(self, command):
4849
:return:
4950
"""
5051
try:
51-
Conf.load(const.CONSUMER_INDEX, command.options.get(
52-
const.CONFIG_URL))
52+
conf = command.options.get(const.CONFIG_URL)
53+
Utility.load_csm_config_indices(conf)
5354
Setup.setup_logs_init()
5455
Log.info("Setup: Initiating Post Install phase.")
55-
Setup.load_csm_config_indices()
5656
Setup.copy_base_configs()
57-
except KvError as e:
57+
except (KvError, VError) as e:
5858
Log.error(f"Post Install: Configuration Loading Failed {e}")
5959
raise CsmSetupError("Could Not Load Url Provided in Kv Store.")
6060
services = command.options.get("services")
@@ -121,4 +121,9 @@ def create(self):
121121
if self._is_env_dev:
122122
Conf.set(const.CSM_GLOBAL_INDEX, f"{const.DEPLOYMENT}>{const.MODE}",
123123
const.DEV)
124+
try:
125+
Utility.validate_consul()
126+
except VError as e:
127+
Log.error(f"Unable to save the configurations to consul: {e}")
128+
raise CsmSetupError("Unable to save the configurations")
124129
Conf.save(const.CSM_GLOBAL_INDEX)

csm/conf/prepare.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from csm.core.blogic import const
2323
from csm.common.errors import CSM_OPERATION_SUCESSFUL
2424
from cortx.utils.validator.error import VError
25+
from csm.common.utility import Utility
2526

2627

2728
class Prepare(Setup):
@@ -44,11 +45,11 @@ async def execute(self, command):
4445
:return:
4546
"""
4647
try:
47-
Conf.load(const.CONSUMER_INDEX, command.options.get(const.CONFIG_URL))
48+
conf = command.options.get(const.CONFIG_URL)
49+
Utility.load_csm_config_indices(conf)
4850
Setup.setup_logs_init()
4951
Log.info("Setup: Initiating Prepare phase.")
50-
Setup.load_csm_config_indices()
51-
except KvError as e:
52+
except (KvError, VError) as e:
5253
Log.error(f"Prepare: Configuration Loading Failed {e}")
5354
raise CsmSetupError("Could Not Load Url Provided in Kv Store.")
5455

@@ -113,7 +114,7 @@ def _set_db_host_addr():
113114
:return:
114115
"""
115116
Log.info("Prepare: Setting database host address")
116-
_, consul_host, consul_port, secret, _ = Setup.get_consul_config()
117+
_, consul_host, consul_port, secret, _ = Utility.get_consul_config()
117118
consul_login = Conf.get(const.CONSUMER_INDEX, const.CONSUL_ADMIN_KEY)
118119
try:
119120
if consul_host and consul_port:
@@ -145,5 +146,10 @@ def create(self):
145146
if self._is_env_dev:
146147
Conf.set(const.CSM_GLOBAL_INDEX, const.CSM_DEPLOYMENT_MODE,
147148
const.DEV)
149+
try:
150+
Utility.validate_consul()
151+
except VError as e:
152+
Log.error(f"Unable to save the configurations to consul: {e}")
153+
raise CsmSetupError("Unable to save the configurations")
148154
Conf.save(const.CSM_GLOBAL_INDEX)
149155
Conf.save(const.DATABASE_INDEX)

csm/conf/setup.py

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -52,58 +52,6 @@ def _set_csm_conf_path():
5252
Log.info(f"Setting Config saving path:{conf_path} from confstore")
5353
return conf_path
5454

55-
@staticmethod
56-
def get_consul_config():
57-
result : bool = False
58-
secret = Conf.get(const.CONSUMER_INDEX, const.CONSUL_SECRET_KEY)
59-
protocol, host, port, consul_endpoint = '','','',''
60-
count_endpoints : str = Conf.get(const.CONSUMER_INDEX,
61-
const.CONSUL_NUM_ENDPOINTS_KEY)
62-
try:
63-
count_endpoints = int(count_endpoints)
64-
except ValueError:
65-
raise CsmSetupError("Consul num_endpoints value is not a valid"
66-
" integer.")
67-
for count in range(count_endpoints):
68-
endpoint = Conf.get(const.CONSUMER_INDEX,
69-
f'{const.CONSUL_ENDPOINTS_KEY}[{count}]')
70-
if endpoint:
71-
protocol, host, port = ServiceUrls.parse_url(endpoint)
72-
if protocol == "https" or protocol == "http":
73-
result = True
74-
consul_endpoint = endpoint
75-
Log.info(f"Fetching consul endpoint : {consul_endpoint}")
76-
break
77-
if not result:
78-
raise CsmSetupError("Consul endpoint not found.")
79-
return protocol, host, port, secret, consul_endpoint
80-
81-
@staticmethod
82-
def load_csm_config_indices():
83-
Log.info("Loading CSM configuration")
84-
set_config_flag = False
85-
_, consul_host, consul_port, _, _ = Setup.get_consul_config()
86-
if consul_host and consul_port:
87-
try:
88-
ConsulV().validate_service_status(consul_host,consul_port)
89-
Conf.load(const.CSM_GLOBAL_INDEX,
90-
f"consul://{consul_host}:{consul_port}/{const.CSM_CONF_BASE}")
91-
Conf.load(const.DATABASE_INDEX,
92-
f"consul://{consul_host}:{consul_port}/{const.DATABASE_CONF_BASE}")
93-
set_config_flag = True
94-
except VError as ve:
95-
Log.error(f"Unable to fetch the configurations from consul: {ve}")
96-
raise CsmSetupError("Unable to fetch the configurations")
97-
98-
if not set_config_flag:
99-
config_path = Setup._set_csm_conf_path()
100-
Log.info(f"Setting CSM configuration to local storage: {config_path}")
101-
Conf.load(const.CSM_GLOBAL_INDEX,
102-
f"yaml://{config_path}/{const.CSM_CONF_FILE_NAME}")
103-
Conf.load(const.DATABASE_INDEX,
104-
f"yaml://{config_path}/{const.DB_CONF_FILE_NAME}")
105-
set_config_flag = True
106-
10755
@staticmethod
10856
def copy_base_configs():
10957
Log.info("Copying Csm base configurations to destination indices")

csm/conf/upgrade.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
from cortx.utils.conf_store import Conf
2121
from csm.conf.setup import Setup, CsmSetupError
2222
from cortx.utils.kv_store.error import KvError
23+
from csm.common.utility import Utility
24+
from cortx.utils.validator.error import VError
2325

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

0 commit comments

Comments
 (0)