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

CORTX-32806: Query Deployment api #863

Merged
merged 7 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions py-utils/src/utils/query_deployment/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python3

# CORTX-Py-Utils: CORTX Python common library.
# Copyright (c) 2022 Seagate Technology LLC and/or its Affiliates
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# For any questions about this software or licensing,
# please email opensource@seagate.com or cortx-questions@seagate.com.

from cortx.utils.query_deployment.query_deployment import QueryDeployment
145 changes: 145 additions & 0 deletions py-utils/src/utils/query_deployment/query_deployment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#!/usr/bin/env python3

# CORTX-Py-Utils: CORTX Python common library.
# Copyright (c) 2022 Seagate Technology LLC and/or its Affiliates
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# For any questions about this software or licensing,
# please email opensource@seagate.com or cortx-questions@seagate.com.

import os
import json
import errno
from collections import defaultdict
from cortx.utils.conf_store import Conf

class Topology:
topology = {
"cortx": {
"common": {
"release": {}
}
},
"cluster": [],
"nodes": []
}
class QueryConfData:
"""Query Data."""
lakshita-jain marked this conversation as resolved.
Show resolved Hide resolved
_query_idx = "query_idx"
_data_idx = "data_idx"
_local_file = '/tmp/local_conf.conf'
tanujashinde0405 marked this conversation as resolved.
Show resolved Hide resolved
_local_conf = "yaml://" + _local_file

def __init__(self):
_f = QueryConfData._local_file
lakshita-jain marked this conversation as resolved.
Show resolved Hide resolved
tanujashinde0405 marked this conversation as resolved.
Show resolved Hide resolved
if os.path.exists(_f): os.remove(_f)

def get_data(self, kv_url: str):
"""Get data related to the parent key from config."""
return self._get_data(kv_url)

def _get_data(self, kv_url: str):
"""Return data in dict format."""
not_required_keys=['num_','nodes']
tanujashinde0405 marked this conversation as resolved.
Show resolved Hide resolved
Conf.load(QueryConfData._query_idx, kv_url)
_data_keys = Conf.get_keys(QueryConfData._query_idx)

Conf.load(QueryConfData._data_idx, QueryConfData._local_conf)
Conf.copy(QueryConfData._query_idx, QueryConfData._data_idx, _data_keys)
Conf.save(QueryConfData._data_idx)
for key in Conf.get_keys(QueryConfData._data_idx):
if 'num_'in key:
Conf.delete(QueryConfData._data_idx, key)
# if 'nodes'in key:
tanujashinde0405 marked this conversation as resolved.
Show resolved Hide resolved
# Conf.delete(QueryConfData._data_idx, key,force=True)
Conf.save(QueryConfData._data_idx)

from cortx.utils.conf_store import ConfStore
lakshita-jain marked this conversation as resolved.
Show resolved Hide resolved
_cs = ConfStore()
tanujashinde0405 marked this conversation as resolved.
Show resolved Hide resolved
_cs.load(QueryConfData._data_idx, QueryConfData._local_conf)
_data = _cs.get_data(QueryConfData._data_idx)
nitin-seagate marked this conversation as resolved.
Show resolved Hide resolved
return _data.get_data()


class QueryDeployment:
""" Query Deployment """
_query_conf = None
@staticmethod
def init(**kwargs):
""" Static init for initialising and setting attributes."""
if QueryDeployment._query_conf is None:
QueryDeployment._query_conf = QueryConfData()

@staticmethod
def get_cortx_topology(kv_url: str) -> dict:
""" get cluster toplogy """
if QueryDeployment._query_conf is None:
QueryDeployment.init()

_data = {}
_data = QueryDeployment._query_conf.get_data(kv_url)
if not len(_data) > 0:
raise QueryDeploymentError(errno.EINVAL, f"Invalid data in {kv_url}")

return QueryDeployment._get_cortx_topology(_data)

def _get_cortx_topology(data: dict) -> dict:
lakshita-jain marked this conversation as resolved.
Show resolved Hide resolved
""" Map gconf fields to topology """
nd=lambda: defaultdict(nd)
_config=Topology.topology

#cortx_info
tanujashinde0405 marked this conversation as resolved.
Show resolved Hide resolved
_config["cortx"]["common"]["release"]=data["cortx"]["common"]["release"]

#cluster info
for cluster_key, cluster_val in data['cluster'].items():
cluster_info=nd()
storage_set_info=nd()
storage_set_list=[]
cluster_info['security']=data['cortx']['common']['security']
if cluster_key=='storage_set':
for storage_info in data['cluster']['storage_set']:
for storage_key,storage_val in storage_info.items():
if storage_key!='nodes':
tanujashinde0405 marked this conversation as resolved.
Show resolved Hide resolved
storage_set_info[storage_key]=storage_val
storage_set_list.append(storage_set_info)
cluster_info['storage_set']=storage_set_list
else:
cluster_info[cluster_key]=cluster_val
_config['cluster'].append((json.dumps(cluster_info)))

# Nodes Info
for nodes_key, nodes_value in data['node'].items():
nodes_info=nd()
nodes_info['machine_id'] = nodes_key
for key, val in data['node'][nodes_key].items():
if key=='provisioning':
tanujashinde0405 marked this conversation as resolved.
Show resolved Hide resolved
# TODO: uncomment below once deployment time is supported by provisioner
# nodes_info['deployment_time']=data['node'][nodes_key]['provisioning']['time']
nodes_info['version']=data['node'][nodes_key]['provisioning']['version']
else:
nodes_info[key]=val
#TBD json dumps to dict
_config["nodes"].append(json.dumps(nodes_info))
tanujashinde0405 marked this conversation as resolved.
Show resolved Hide resolved
return _config

class QueryDeploymentError(Exception):
tanujashinde0405 marked this conversation as resolved.
Show resolved Hide resolved
"""Generic Exception with error code and output."""

def __init__(self, rc, message, *args):
"""Initialize self."""
self._rc = rc
self._desc = message % (args)

def __str__(self):
"""Return str(self)."""
if self._rc == 0: return self._desc
return "error(%d): %s" % (self._rc, self._desc)