Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SFAM-1589 #199

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions simplyblock_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,8 @@ def __init__(self):
sub_command = self.add_sub_command(subparser, "remove", 'Remove Management node')
sub_command.add_argument("id", help='Mgmt node uuid')

sub_command = self.add_sub_command(subparser, "get-secret", 'get admin secret')

# pool ops
subparser = self.add_command('pool', 'Pool commands')
# add pool
Expand Down Expand Up @@ -1133,6 +1135,8 @@ def run(self):
ret = mgmt_ops.list_mgmt_nodes(args.json)
elif sub_command == "remove":
ret = mgmt_ops.remove_mgmt_node(args.id)
elif sub_command == "get-secret":
ret = mgmt_ops.get_secret()
else:
self.parser.print_help()

Expand Down
59 changes: 35 additions & 24 deletions simplyblock_core/cluster_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def _add_graylog_input(cluster_ip, password):

def create_cluster(blk_size, page_size_in_blocks, cli_pass,
cap_warn, cap_crit, prov_cap_warn, prov_cap_crit, ifname, log_del_interval, metrics_retention_period,
contact_point, grafana_endpoint, distr_ndcs, distr_npcs, distr_bs, distr_chunk_bs, ha_type,
contact_point, gr_endpoint, distr_ndcs, distr_npcs, distr_bs, distr_chunk_bs, ha_type,
enable_node_affinity, qpair_count, max_queue_size, inflight_io_threshold, enable_qos, strict_node_anti_affinity):

logger.info("Installing dependencies...")
Expand Down Expand Up @@ -149,7 +149,7 @@ def create_cluster(blk_size, page_size_in_blocks, cli_pass,
c.nqn = f"{constants.CLUSTER_NQN}:{c.uuid}"
c.cli_pass = cli_pass
c.secret = utils.generate_string(20)
c.grafana_secret = c.secret
grafana_secret = utils.generate_string(20)
c.db_connection = db_connection
if cap_warn and cap_warn > 0:
c.cap_warn = cap_warn
Expand All @@ -168,10 +168,10 @@ def create_cluster(blk_size, page_size_in_blocks, cli_pass,
c.distr_bs = distr_bs
c.distr_chunk_bs = distr_chunk_bs
c.ha_type = ha_type
if grafana_endpoint:
c.grafana_endpoint = grafana_endpoint
if gr_endpoint:
grafana_endpoint = gr_endpoint
else:
c.grafana_endpoint = f"http://{DEV_IP}/grafana"
grafana_endpoint = f"http://{DEV_IP}/grafana"
c.enable_node_affinity = enable_node_affinity
c.qpair_count = qpair_count or constants.QPAIR_COUNT

Expand Down Expand Up @@ -199,7 +199,7 @@ def create_cluster(blk_size, page_size_in_blocks, cli_pass,

values = {
'CONTACT_POINT': contact_point,
'GRAFANA_ENDPOINT': c.grafana_endpoint,
'GRAFANA_ENDPOINT': grafana_endpoint,
'ALERT_TYPE': ALERT_TYPE,
}

Expand All @@ -220,8 +220,8 @@ def create_cluster(blk_size, page_size_in_blocks, cli_pass,

logger.info("Deploying swarm stack ...")
log_level = "DEBUG" if constants.LOG_WEB_DEBUG else "INFO"
ret = scripts.deploy_stack(cli_pass, DEV_IP, constants.SIMPLY_BLOCK_DOCKER_IMAGE, c.secret, c.uuid,
log_del_interval, metrics_retention_period, log_level, c.grafana_endpoint)
ret = scripts.deploy_stack(cli_pass, DEV_IP, constants.SIMPLY_BLOCK_DOCKER_IMAGE, grafana_secret, c.uuid,
log_del_interval, metrics_retention_period, log_level, grafana_endpoint)
logger.info("Deploying swarm stack > Done")

if ret == 0:
Expand All @@ -235,7 +235,7 @@ def create_cluster(blk_size, page_size_in_blocks, cli_pass,

_add_graylog_input(DEV_IP, c.secret)

_create_update_user(c.uuid, c.grafana_endpoint, c.grafana_secret, c.secret)
_create_update_user(c.uuid, grafana_endpoint, grafana_secret, c.secret)

c.status = Cluster.STATUS_UNREADY
c.create_dt = str(datetime.datetime.now())
Expand All @@ -244,7 +244,7 @@ def create_cluster(blk_size, page_size_in_blocks, cli_pass,

cluster_events.cluster_create(c)

mgmt_node_ops.add_mgmt_node(DEV_IP, c.uuid)
mgmt_node_ops.add_mgmt_node(DEV_IP, c.uuid, grafana_secret, grafana_endpoint)

logger.info("New Cluster has been created")
logger.info(c.uuid)
Expand All @@ -255,10 +255,10 @@ def add_cluster(blk_size, page_size_in_blocks, cap_warn, cap_crit, prov_cap_warn
distr_ndcs, distr_npcs, distr_bs, distr_chunk_bs, ha_type, enable_node_affinity, qpair_count,
max_queue_size, inflight_io_threshold, enable_qos, strict_node_anti_affinity):
db_controller = DBController()
clusters = db_controller.get_clusters()
if not clusters:
logger.error("No previous clusters found!")
return False
# clusters = db_controller.get_clusters()
# if not clusters:
# logger.error("No previous clusters found!")
# return False

logger.info("Adding new cluster")
cluster = Cluster()
Expand All @@ -269,12 +269,16 @@ def add_cluster(blk_size, page_size_in_blocks, cap_warn, cap_crit, prov_cap_warn
cluster.secret = utils.generate_string(20)
cluster.strict_node_anti_affinity = strict_node_anti_affinity

default_cluster = clusters[0]
cluster.db_connection = default_cluster.db_connection
cluster.grafana_secret = default_cluster.grafana_secret
cluster.grafana_endpoint = default_cluster.grafana_endpoint
grafana_secret = ""
grafana_endpoint = ""
for node in db_controller.get_mgmt_nodes():
if node.secret:
grafana_secret = node.secret
grafana_endpoint = node.endpoint
cluster.db_connection = node.db_connection
break

_create_update_user(cluster.uuid, cluster.grafana_endpoint, cluster.grafana_secret, cluster.secret)
_create_update_user(cluster.uuid, grafana_endpoint, grafana_secret, cluster.secret)

if distr_ndcs == 0 and distr_npcs == 0:
cluster.distr_ndcs = 2
Expand Down Expand Up @@ -628,8 +632,15 @@ def set_secret(cluster_id, secret):
secret = secret.strip()
if len(secret) < 20:
return "Secret must be at least 20 char"

_create_update_user(cluster_id, cluster.grafana_endpoint, cluster.grafana_secret, secret, update_secret=True)

grafana_secret = ""
grafana_endpoint = ""
for node in db_controller.get_mgmt_nodes():
if node.secret:
grafana_secret = node.secret
grafana_endpoint = node.endpoint
break
_create_update_user(cluster_id, grafana_endpoint, grafana_secret, secret, update_secret=True)

cluster.secret = secret
cluster.write_to_db(db_controller.kv_store)
Expand Down Expand Up @@ -781,9 +792,9 @@ def delete_cluster(cl_id):
logger.error("Can only remove Empty cluster, Pools found")
return False

if len(db_controller.get_clusters()) == 1 :
logger.error("Can not remove the last cluster!")
return False
# if len(db_controller.get_clusters()) == 1 :
# logger.error("Can not remove the last cluster!")
# return False

logger.info(f"Deleting Cluster {cl_id}")
cluster_events.cluster_delete(cluster)
Expand Down
6 changes: 3 additions & 3 deletions simplyblock_core/env_var
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SIMPLY_BLOCK_COMMAND_NAME=sbcli-dev
SIMPLY_BLOCK_VERSION=10.4.5
SIMPLY_BLOCK_COMMAND_NAME=sbcli-admin
SIMPLY_BLOCK_VERSION=0.0.4

SIMPLY_BLOCK_DOCKER_IMAGE=simplyblock/simplyblock:main
SIMPLY_BLOCK_DOCKER_IMAGE=simplyblock/simplyblock:admin
DOCKER_USER=hamdysimplyblock
14 changes: 12 additions & 2 deletions simplyblock_core/mgmt_node_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def deploy_mgmt_node(cluster_ip, cluster_id, ifname, cluster_secret):
return node_id


def add_mgmt_node(mgmt_ip, cluster_id=None):
def add_mgmt_node(mgmt_ip, cluster_id=None, secret=None, endpoint=None):
db_controller = DBController()
hostname = utils.get_hostname()
node = db_controller.get_mgmt_node_by_hostname(hostname)
Expand All @@ -155,10 +155,13 @@ def add_mgmt_node(mgmt_ip, cluster_id=None):
node.cluster_id = cluster_id
node.mgmt_ip = mgmt_ip
node.status = MgmtNode.STATUS_ONLINE
if secret:
node.secret = secret
if endpoint:
node.endpoint = endpoint
node.create_dt = str(datetime.datetime.now())

node.write_to_db(db_controller.kv_store)

mgmt_events.mgmt_add(node)
logger.info("Done")
return node.uuid
Expand Down Expand Up @@ -207,3 +210,10 @@ def remove_mgmt_node(uuid):
mgmt_events.mgmt_remove(snode)
logging.info("done")


def get_secret():
db_controller = DBController()
nodes = db_controller.get_mgmt_nodes()
for node in nodes:
if node.secret:
return node.secret
2 changes: 0 additions & 2 deletions simplyblock_core/models/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ class Cluster(BaseModel):
distr_npcs: int = 0
enable_node_affinity: bool = False
enable_qos: bool = False
grafana_endpoint: str = ""
grafana_secret: str = ""
ha_type: str = "single"
inflight_io_threshold: int = 4
iscsi: str = ""
Expand Down
3 changes: 3 additions & 0 deletions simplyblock_core/models/mgmt_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ class MgmtNode(BaseNodeObject):
docker_ip_port: str = ""
hostname: str = ""
mgmt_ip: str = ""
secret: str = ""
db_connection: str = ""
endpoint: str = ""
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ services:
promagent:
image: simplyblock/promagent
environment:
ClusterID: "${CLUSTER_ID}"
ClusterID: "admin"
ClusterIP: "HAProxy"
ClusterSecret: "${CLUSTER_SECRET}"
deploy:
Expand Down
4 changes: 4 additions & 0 deletions simplyblock_web/auth_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def decorated(*args, **kwargs):
}, 401
try:
db_controller = DBController()
if cluster_id == "admin":
for node in db_controller.get_mgmt_nodes():
if node.secret and node.secret == cluster_secret:
return f(*args, **kwargs)
cluster = db_controller.get_cluster_by_id(cluster_id)
if not cluster:
return {
Expand Down
19 changes: 10 additions & 9 deletions simplyblock_web/blueprints/web_api_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,18 @@ def add_cluster():
@bp.route('/cluster', methods=['GET'], defaults={'uuid': None})
@bp.route('/cluster/<string:uuid>', methods=['GET'])
def list_clusters(uuid):
cluster_id = utils.get_cluster_id(request)
clusters_list = []
if uuid:
cl = db_controller.get_cluster_by_id(uuid)
if cl:
clusters_list.append(cl)
else:
return utils.get_response_error(f"Cluster not found: {uuid}", 404)
if cluster_id == "admin":
clusters_list = db_controller.get_clusters()
if uuid:
cl = db_controller.get_cluster_by_id(uuid)
if cl:
clusters_list = [cl]
else:
cls = db_controller.get_clusters()
if cls:
clusters_list.extend(cls)
cl = db_controller.get_cluster_by_id(cluster_id)
if cl:
clusters_list = [cl]

data = []
for cluster in clusters_list:
Expand Down
2 changes: 2 additions & 0 deletions simplyblock_web/blueprints/web_api_mgmt_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,7 @@ def list_mgmt_nodes(uuid):
for node in nodes:
d = node.get_clean_dict()
d['status_code'] = node.get_status_code()
if "secret" in d:
del d['secret']
data.append(d)
return utils.get_response(data)
Loading