Skip to content

Commit

Permalink
Add scripts for Ceph usecase
Browse files Browse the repository at this point in the history
  • Loading branch information
cb-github-robot authored Jun 10, 2024
2 parents 0b123a9 + 63aa716 commit a014218
Show file tree
Hide file tree
Showing 5 changed files with 333 additions and 2 deletions.
67 changes: 67 additions & 0 deletions scripts/usecases/ceph/1.setupCephMaster.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/bin/bash

# This script should be run as root, so we check for root privileges
# if [ "$EUID" -ne 0 ]; then
# echo "Please run as root"
# exit
# fi

##############################################################################################
echo "[Start to setup Ceph master node]"

SECONDS=0

# Setup variables
# serverName=${1:-ELK-Stack-8.3.0-by-Cloud-Barista} # Specify the server name
CEPH_RELEASE_VERSION="18.2.0" # Specify the version you want to install
CEPH_DASHBOARD_PORT="8443" # Specify the port number for the dashboard (default: 8443)

# Determine OS ID and Version
OS_ID=$(grep '^ID=' /etc/os-release | cut -d= -f2)
OS_VERSION=$(grep '^VERSION_ID=' /etc/os-release | cut -d= -f2 | tr -d '"')

# Check if the OS is Ubuntu 22.04
if [ "${OS_ID}" != "ubuntu" ] || [ "${OS_VERSION}" != "22.04" ]; then
echo "This script has been confirmed to work on Ubuntu 22.04."
exit 1
fi
# Install docker
sudo apt update
sudo apt install -y docker.io
sudo systemctl start docker
sudo systemctl enable docker
docker --version

# Install Ceph master node
curl --silent --remote-name --location https://download.ceph.com/rpm-${CEPH_RELEASE_VERSION}/el9/noarch/cephadm
chmod +x cephadm
sudo ./cephadm add-repo --release reef
sudo ./cephadm install
which cephadm

IP=$(hostname -I | awk '{print $1}')
echo $IP
sudo cephadm bootstrap --mon-ip $IP --ssh-user cb-user
sudo cephadm add-repo --release reef
sudo cephadm install ceph-common
sudo ceph status

sudo ceph telemetry on --license sharing-1-0
sudo ceph orch host ls --detail

##############################################################################################
echo "Displaying information about the Ceph dashboard..."

# Get the public IP address
EXTERNAL_IP=$(curl -s https://api.ipify.org)

# Display the result and the access information
echo "[Ceph master setup: complete]"
echo "Access to https://$EXTERNAL_IP:$CEPH_DASHBOARD_PORT to use the Ceph dashboard."

# Display notice
echo ""
echo "[NOTICE]"
echo "It may take some time to start up the dashboard. Please wait for a moment."
echo "The default username: admin"
echo "The default password: see the above, it was genereted during the installation process"
45 changes: 45 additions & 0 deletions scripts/usecases/ceph/2.setupCephMemberWithPubKey.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash

# This script should be run as root, so we check for root privileges
# if [ "$EUID" -ne 0 ]; then
# echo "Please run as root"
# exit
# fi

##############################################################################################
echo "[Start to setup Ceph member node]"

SECONDS=0

# Setup variables
PUBKEY=${1:?"the ceph public key is required"} # public key

# Determine OS ID and Version
OS_ID=$(grep '^ID=' /etc/os-release | cut -d= -f2)
OS_VERSION=$(grep '^VERSION_ID=' /etc/os-release | cut -d= -f2 | tr -d '"')

# Check if the OS is Ubuntu 22.04
if [ "${OS_ID}" != "ubuntu" ] || [ "${OS_VERSION}" != "22.04" ]; then
echo "This script has been confirmed to work on Ubuntu 22.04."
exit 1
fi
# Install docker
sudo apt update
sudo apt install -y docker.io
sudo systemctl start docker
sudo systemctl enable docker
docker --version

echo ${PUBKEY} >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

# Display the result
echo "[Ceph member node setup: complete]"

# Display notice
IP=$(hostname -I | awk '{print $1}')
HOSTNAME=$(hostname)
echo ""
echo "[NOTICE]"
echo "On the master node, execute below command "
echo "sudo ceph orch host add $HOSTNAME $IP"
39 changes: 39 additions & 0 deletions scripts/usecases/ceph/3.setupDemoAppDepenancies.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash

# This script should be run as root, so we check for root privileges
# if [ "$EUID" -ne 0 ]; then
# echo "Please run as root"
# exit
# fi

##############################################################################################
echo "[Start to setup demo app dependency]"

SECONDS=0

# Setup variables

# Determine OS ID and Version
OS_ID=$(grep '^ID=' /etc/os-release | cut -d= -f2)
OS_VERSION=$(grep '^VERSION_ID=' /etc/os-release | cut -d= -f2 | tr -d '"')

# Check if the OS is Ubuntu 22.04
if [ "${OS_ID}" != "ubuntu" ] || [ "${OS_VERSION}" != "22.04" ]; then
echo "This script has been confirmed to work on Ubuntu 22.04."
exit 1
fi

# Install dependencies
sudo apt update
sudo apt install python3-pip
sudo pip3 install Faker
sudo pip3 install flask

# Display the result
echo "[Demo app dependency setup: complete]"

# Display notice
echo ""
echo "[NOTICE]"
echo "Execute below command"
echo "sudo python3 app.py"
180 changes: 180 additions & 0 deletions scripts/usecases/ceph/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import threading
import time
import urllib.request
from datetime import datetime

import rados
from faker import Faker
from flask import Flask, render_template_string

fake = Faker()

app = Flask(__name__)

# Ceph cluster connection info
CEPH_CONF_FILE = "/etc/ceph/ceph.conf"
POOL_NAME = "mypool"
OBJECT_NAME = "myobject"

# Ceph cluster connection settings
CEPH_CONF_FILE = "/etc/ceph/ceph.conf"
POOL_NAME = "mypool"
OBJECT_NAME = "myobject"
try:
cluster = rados.Rados(conffile=CEPH_CONF_FILE)
cluster.connect()
except Exception as e:
print(f"Error connecting to the cluster: {e}")
exit(1)

print("\nCluster ID: {}".format(cluster.get_fsid()))

print("\n\nCluster Statistics")
print("==================")
cluster_stats = cluster.get_cluster_stats()

for key, value in cluster_stats.items():
print(key, value)

# Create a pool if it does not exist
if not cluster.pool_exists(POOL_NAME):
cluster.create_pool(POOL_NAME)

ioctx = cluster.open_ioctx(POOL_NAME)

is_generating = threading.Event()
generate_thread = None


def data_generation_thread():
while not is_generating.is_set():

# t = datetime.now().strftime("%Y-%m-%d%H:%M:%S")
(dt, micro) = datetime.utcnow().strftime("%Y%m%d%H%M%S.%f").split(".")
t = "%s%03d" % (dt, int(micro) / 1000)
data = fake.text()
ioctx.write_full(t, data.encode("utf-8"))

time.sleep(0.01)


@app.route("/")
def index():
return render_template_string(
"""
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ceph Read/Write Demo</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h1>Ceph Read/Write Demo</h1>
<button id="start-btn">Start Generating Data</button>
<button id="stop-btn">Stop Generating Data</button>
<button id="delete-btn">Delete all generated data</button>
<br>
<h2> Dummy data generated by the python Faker module</h2>
<div id="data-container" style="white-space: pre-line;">
</div>
<script>
function fetchData() {
$.get("/data", function(data) {
$("#data-container").text(JSON.stringify(data, null, 4));
});
}
$(document).ready(function() {
$("#start-btn").click(function() {
$.post("/start");
});
$("#stop-btn").click(function() {
$.post("/stop");
});
$("#delete-btn").click(function() {
$.post("/delete");
});
fetchData();
setInterval(fetchData, 1000); // 1초마다 데이터 갱신
});
</script>
</body>
</html>
"""
)


@app.route("/data")
def get_data():
# Read all data from Ceph
try:
objects = ioctx.list_objects()
read_data = [str(obj.read().decode("utf-8")) for obj in objects]
except rados.ObjectNotFound:
read_data = "No data found."
return read_data


@app.route("/start", methods=["POST"])
def start_generating():
# Start generating dummy data
global is_generating, generate_thread
if not is_generating.is_set():
is_generating.clear()
generate_thread = threading.Thread(target=data_generation_thread)
generate_thread.start()
return "Started data generation."


@app.route("/stop", methods=["POST"])
def stop_generating():
# Stop generating dummy data
global is_generating
is_generating.set()
if generate_thread:
generate_thread.join()
return "Stopped data generation."


@app.route("/delete", methods=["POST"])
def delete_data():
# Delete all dummy data
object_iterator = ioctx.list_objects()
while True:
try:
rados_object = object_iterator.__next__()
# print(rados_object.key)
ioctx.remove_object(rados_object.key)
except StopIteration:
break
return "Deleted all data."


@app.route("/shutdown")
def shutdown():
ioctx.close()
cluster.shutdown()
return "Ceph cluster connection closed."


def get_public_ip():
try:
with urllib.request.urlopen("https://api.ipify.org") as url:
ip = url.read().decode()
print(f"Public IP: {ip}")
except Exception as e:
print(f"Error fetching public IP: {e}")

return ip


if __name__ == "__main__":
pub_ip = get_public_ip()
print(f"Access the web app at: http://{pub_ip}:5000")
app.run(host="0.0.0.0", port=5000, debug=True)
4 changes: 2 additions & 2 deletions src/api/rest/server/mcis/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ func RestDeleteVpnGcpToAws(c echo.Context) error {

// Flush a response
res = common.SimpleMsg{
Message: resDeleteEnrichments.Message,
Message: resDeleteEnv.Message,
}
if err := enc.Encode(res); err != nil {
return err
Expand Down Expand Up @@ -776,7 +776,7 @@ func RestDeleteVpnGcpToAws(c echo.Context) error {

// Flush a response
res = common.SimpleMsg{
Message: resDeleteEnrichments.Message,
Message: resDeleteTr.Message,
}
if err := enc.Encode(res); err != nil {
return err
Expand Down

0 comments on commit a014218

Please sign in to comment.