2021-08-21 02:46:11 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# <Filename> - <Description>
|
|
|
|
# zookeeper.py - Utility functions for pvcnoded Zookeeper connections
|
|
|
|
# Part of the Parallel Virtual Cluster (PVC) system
|
|
|
|
#
|
2023-12-29 11:16:59 -05:00
|
|
|
# Copyright (C) 2018-2024 Joshua M. Boniface <joshua@boniface.me>
|
2021-08-21 02:46:11 -04:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, version 3.
|
|
|
|
#
|
|
|
|
# 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 General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
from daemon_lib.zkhandler import ZKHandler
|
|
|
|
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
def connect(logger, config):
|
|
|
|
# Create an instance of the handler
|
|
|
|
zkhandler = ZKHandler(config, logger)
|
|
|
|
|
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out(
|
|
|
|
"Connecting to Zookeeper on coordinator nodes {}".format(
|
|
|
|
config["coordinators"]
|
|
|
|
),
|
|
|
|
state="i",
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
# Start connection
|
|
|
|
zkhandler.connect(persistent=True)
|
|
|
|
except Exception as e:
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out(
|
|
|
|
"ERROR: Failed to connect to Zookeeper cluster: {}".format(e), state="e"
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
os._exit(1)
|
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out("Validating Zookeeper schema", state="i")
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
node_schema_version = int(
|
|
|
|
zkhandler.read(("node.data.active_schema", config["node_hostname"]))
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
except Exception:
|
2021-11-06 03:02:43 -04:00
|
|
|
node_schema_version = int(zkhandler.read("base.schema.version"))
|
|
|
|
zkhandler.write(
|
|
|
|
[
|
|
|
|
(
|
|
|
|
("node.data.active_schema", config["node_hostname"]),
|
|
|
|
node_schema_version,
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
# Load in the current node schema version
|
|
|
|
zkhandler.schema.load(node_schema_version)
|
|
|
|
|
|
|
|
# Record the latest intalled schema version
|
|
|
|
latest_schema_version = zkhandler.schema.find_latest()
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out("Latest installed schema is {}".format(latest_schema_version), state="i")
|
|
|
|
zkhandler.write(
|
|
|
|
[(("node.data.latest_schema", config["node_hostname"]), latest_schema_version)]
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
# If we are the last node to get a schema update, fire the master update
|
|
|
|
if latest_schema_version > node_schema_version:
|
|
|
|
node_latest_schema_version = list()
|
2021-11-06 03:02:43 -04:00
|
|
|
for node in zkhandler.children("base.node"):
|
|
|
|
node_latest_schema_version.append(
|
|
|
|
int(zkhandler.read(("node.data.latest_schema", node)))
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
# This is true if all elements of the latest schema version are identical to the latest version,
|
|
|
|
# i.e. they have all had the latest schema installed and ready to load.
|
2021-11-06 03:02:43 -04:00
|
|
|
if node_latest_schema_version.count(latest_schema_version) == len(
|
|
|
|
node_latest_schema_version
|
|
|
|
):
|
|
|
|
zkhandler.write([("base.schema.version", latest_schema_version)])
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
return zkhandler, node_schema_version
|
|
|
|
|
|
|
|
|
|
|
|
def validate_schema(logger, zkhandler):
|
|
|
|
# Validate our schema against the active version
|
|
|
|
if not zkhandler.schema.validate(zkhandler, logger):
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out("Found schema violations, applying", state="i")
|
2024-01-11 00:06:11 -05:00
|
|
|
try:
|
|
|
|
zkhandler.schema.apply(zkhandler)
|
|
|
|
except Exception as e:
|
|
|
|
logger.out(f"Failed to apply schema updates: {e}", state="w")
|
2021-08-21 02:46:11 -04:00
|
|
|
else:
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out("Schema successfully validated", state="o")
|
2021-08-21 02:46:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
def setup_node(logger, config, zkhandler):
|
|
|
|
# Check if our node exists in Zookeeper, and create it if not
|
2021-11-06 03:02:43 -04:00
|
|
|
if config["daemon_mode"] == "coordinator":
|
|
|
|
init_routerstate = "secondary"
|
2021-08-21 02:46:11 -04:00
|
|
|
else:
|
2021-11-06 03:02:43 -04:00
|
|
|
init_routerstate = "client"
|
2021-08-21 02:46:11 -04:00
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
if zkhandler.exists(("node", config["node_hostname"])):
|
|
|
|
logger.out(
|
|
|
|
f"Node is {logger.fmt_green}present{logger.fmt_end} in Zookeeper", state="i"
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
# Update static data just in case it's changed
|
2021-11-06 03:02:43 -04:00
|
|
|
zkhandler.write(
|
|
|
|
[
|
|
|
|
(("node", config["node_hostname"]), config["daemon_mode"]),
|
|
|
|
(("node.mode", config["node_hostname"]), config["daemon_mode"]),
|
|
|
|
(("node.state.daemon", config["node_hostname"]), "init"),
|
|
|
|
(("node.state.router", config["node_hostname"]), init_routerstate),
|
|
|
|
(
|
|
|
|
("node.data.static", config["node_hostname"]),
|
|
|
|
" ".join(config["static_data"]),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
("node.data.pvc_version", config["node_hostname"]),
|
2023-11-29 14:53:01 -05:00
|
|
|
config["daemon_version"],
|
2021-11-06 03:02:43 -04:00
|
|
|
),
|
|
|
|
(
|
|
|
|
("node.ipmi.hostname", config["node_hostname"]),
|
|
|
|
config["ipmi_hostname"],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
("node.ipmi.username", config["node_hostname"]),
|
|
|
|
config["ipmi_username"],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
("node.ipmi.password", config["node_hostname"]),
|
|
|
|
config["ipmi_password"],
|
|
|
|
),
|
|
|
|
]
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
else:
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out(
|
|
|
|
f"Node is {logger.fmt_red}absent{logger.fmt_end} in Zookeeper; adding new node",
|
|
|
|
state="i",
|
|
|
|
)
|
2021-08-21 02:46:11 -04:00
|
|
|
keepalive_time = int(time.time())
|
2021-11-06 03:02:43 -04:00
|
|
|
zkhandler.write(
|
|
|
|
[
|
|
|
|
(("node", config["node_hostname"]), config["daemon_mode"]),
|
|
|
|
(("node.keepalive", config["node_hostname"]), str(keepalive_time)),
|
|
|
|
(("node.mode", config["node_hostname"]), config["daemon_mode"]),
|
|
|
|
(("node.state.daemon", config["node_hostname"]), "init"),
|
|
|
|
(("node.state.domain", config["node_hostname"]), "flushed"),
|
|
|
|
(("node.state.router", config["node_hostname"]), init_routerstate),
|
|
|
|
(
|
|
|
|
("node.data.static", config["node_hostname"]),
|
|
|
|
" ".join(config["static_data"]),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
("node.data.pvc_version", config["node_hostname"]),
|
2023-11-29 14:53:01 -05:00
|
|
|
config["daemon_version"],
|
2021-11-06 03:02:43 -04:00
|
|
|
),
|
|
|
|
(
|
|
|
|
("node.ipmi.hostname", config["node_hostname"]),
|
|
|
|
config["ipmi_hostname"],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
("node.ipmi.username", config["node_hostname"]),
|
|
|
|
config["ipmi_username"],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
("node.ipmi.password", config["node_hostname"]),
|
|
|
|
config["ipmi_password"],
|
|
|
|
),
|
|
|
|
(("node.memory.total", config["node_hostname"]), "0"),
|
|
|
|
(("node.memory.used", config["node_hostname"]), "0"),
|
|
|
|
(("node.memory.free", config["node_hostname"]), "0"),
|
|
|
|
(("node.memory.allocated", config["node_hostname"]), "0"),
|
|
|
|
(("node.memory.provisioned", config["node_hostname"]), "0"),
|
|
|
|
(("node.vcpu.allocated", config["node_hostname"]), "0"),
|
|
|
|
(("node.cpu.load", config["node_hostname"]), "0.0"),
|
|
|
|
(("node.running_domains", config["node_hostname"]), "0"),
|
|
|
|
(("node.count.provisioned_domains", config["node_hostname"]), "0"),
|
|
|
|
(("node.count.networks", config["node_hostname"]), "0"),
|
|
|
|
]
|
|
|
|
)
|
2024-10-15 10:39:09 -04:00
|
|
|
|
2024-10-15 10:57:33 -04:00
|
|
|
logger.out("Waiting 3 seconds for Zookeeper to synchronize", state="s")
|
|
|
|
time.sleep(3)
|