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

Basic_Configuration_Managment_With_Zookeeper #984

Open
wants to merge 2 commits into
base: master
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
20 changes: 20 additions & 0 deletions Zookeeper Project/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use an official ZooKeeper image as a base
FROM zookeeper:3.7.0

# Install Python and other dependencies
RUN apt-get update && apt-get install -y python3 python3-pip

# Install Kazoo library
RUN pip3 install kazoo jupyter

# Set the working directory
WORKDIR /app

# Copy the application code into the container
COPY . /app

# Expose ZooKeeper default port
EXPOSE 2181

# Start ZooKeeper server
CMD ["zkServer.sh", "start-foreground"]
17 changes: 17 additions & 0 deletions Zookeeper Project/Dockerfile.jupyter
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Use an official Python image as a base
FROM python:3.9

# Install Jupyter and Kazoo
RUN pip install jupyter kazoo

# Set the working directory
WORKDIR /app

# Copy the application code into the container
COPY . /app

# Expose the Jupyter Notebook port
EXPOSE 8888

# Start Jupyter Notebook
CMD ["jupyter", "notebook", "--ip=0.0.0.0", "--port=8888", "--no-browser", "--allow-root"]
51 changes: 51 additions & 0 deletions Zookeeper Project/config_manager.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import json
from kazoo.client import KazooClient

ZK_SERVER = 'zookeeper:2181'

class ConfigManager:
def __init__(self):
self.zk = KazooClient(hosts=ZK_SERVER)
self.zk.start()

def create_node(self, path, value):
if not self.zk.exists(path):
self.zk.create(path, json.dumps(value).encode('utf-8'))
else:
self.update_node(path, value)

def read_node(self, path):
if self.zk.exists(path):
data, _ = self.zk.get(path)
return json.loads(data.decode('utf-8'))
else:
return None

def update_node(self, path, value):
if self.zk.exists(path):
self.zk.set(path, json.dumps(value).encode('utf-8'))
else:
self.create_node(path, value)

def delete_node(self, path):
if self.zk.exists(path):
self.zk.delete(path)

def close(self):
self.zk.stop()

cm = ConfigManager()
config_path = "/app/config"
initial_config = {"database": {"host": "localhost", "port": 5432}}

cm.create_node(config_path, initial_config)
config = cm.read_node(config_path)
print("Current Configuration:", config)

updated_config = {"database": {"host": "localhost", "port": 3306}}
cm.update_node(config_path, updated_config)
config = cm.read_node(config_path)
print("Updated Configuration:", config)

cm.delete_node(config_path)
cm.close()
99 changes: 99 additions & 0 deletions Zookeeper Project/config_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
from kazoo.client import KazooClient
import json
import datetime

ZK_SERVER = 'zookeeper:2181'

class ConfigManager:
def __init__(self):
self.zk = KazooClient(hosts=ZK_SERVER)
self.zk.start()

def create_node(self, path, value):
if not self.zk.exists(path):
self.zk.create(path, json.dumps(value).encode('utf-8'), makepath=True)
else:
self.update_node(path, value)

def read_node(self, path):
if self.zk.exists(path):
data, _ = self.zk.get(path)
return json.loads(data.decode('utf-8'))
else:
return None

def update_node(self, path, value):
if self.zk.exists(path):
self.zk.set(path, json.dumps(value).encode('utf-8'))
else:
self.create_node(path, value)

def delete_node(self, path):
if self.zk.exists(path):
self.zk.delete(path, recursive=True)

def close(self):
self.zk.stop()

def create_versioned_node(self, base_path, value):
current_time = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
versioned_path = f"{base_path}/{current_time}"
self.create_node(versioned_path, value)
self.update_node(base_path, value) # Update the latest version at the base path

def read_versioned_node(self, base_path, version=None):
if version:
path = f"{base_path}/{version}"
else:
path = base_path
return self.read_node(path)

def watch_node(self, path, callback):
@self.zk.DataWatch(path)
def watch_node(data, stat):
if data:
callback(json.loads(data.decode('utf-8')))
else:
print(f"Node {path} does not exist or has been deleted.")

def config_change_callback(data):
print(f"Configuration changed: {data}")

if __name__ == "__main__":
cm = ConfigManager()
config_path = "/app/config"
initial_config = {"database": {"host": "localhost", "port": 5432}}

# Create initial configuration
cm.create_versioned_node(config_path, initial_config)

# Read and print current configuration
config = cm.read_versioned_node(config_path)
print("Current Configuration:", config)

# Update configuration
updated_config = {"database": {"host": "localhost", "port": 3306}}
cm.create_versioned_node(config_path, updated_config)

# Read and print updated configuration
config = cm.read_versioned_node(config_path)
print("Updated Configuration:", config)

# Set the watch
cm.watch_node(config_path, config_change_callback)

# Simulate a change to trigger the watch
another_update = {"database": {"host": "localhost", "port": 6380}}
cm.create_versioned_node(config_path, another_update)

# Keep the script running to observe changes
import time
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
pass

# Clean up by deleting the node
cm.delete_node(config_path)
cm.close()
19 changes: 19 additions & 0 deletions Zookeeper Project/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: '3.8'
services:
zookeeper:
build:
context: .
dockerfile: Dockerfile
container_name: zookeeper
ports:
- "2181:2181"

jupyter:
build:
context: .
dockerfile: Dockerfile.jupyter
container_name: jupyter
ports:
- "8888:8888"
depends_on:
- zookeeper