Skip to content

Commit c5de615

Browse files
committed
initial commit
0 parents  commit c5de615

File tree

8 files changed

+253
-0
lines changed

8 files changed

+253
-0
lines changed

Diff for: README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# python-cli-cassandra-single-node-without-ssl-create-keyspace-client
2+
3+
## Description
4+
POC for single node without ssl cassandra in python.
5+
6+
- Features
7+
- single node cassandra
8+
- cql
9+
- create keyspace
10+
- pandas dataframe
11+
12+
## Tech stack
13+
- python
14+
- cassandra-driver
15+
- padas
16+
- cassandra
17+
18+
## Docker stack
19+
- python:3.8-bullyseye
20+
- cassandra:4.0
21+
22+
## To run
23+
`sudo ./install.sh -u`
24+
25+
## To stop (optional)
26+
`sudo ./install.sh -d`
27+
28+
## For help
29+
`sudo ./install.sh -h`

Diff for: docker-compose.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
version: '3.5'
2+
services:
3+
py-srv:
4+
build: py-srv
5+
command: sh -c "/wait && python app.py"
6+
environment:
7+
- WAIT_HOSTS=db:9042
8+
- WAIT_HOSTS_TIMEOUT=300
9+
- WAIT_SLEEP_INTERVAL=30
10+
- WAIT_HOST_CONNECT_TIMEOUT=30
11+
12+
db:
13+
image: cassandra:4.0
14+
ports:
15+
- "9042:9042"
16+
healthcheck:
17+
test: [ "CMD", "ls", "/opt/cassandra/bin" ]
18+
interval: 15s
19+
timeout: 10s
20+
retries: 10
21+
environment:
22+
- CASSANDRA_HOST=db
23+
- CASSANDRA_SEEDS=db
24+
- CASSANDRA_PASSWORD_SEEDER=yes
25+
- CASSANDRA_CLUSTER_NAME=citizix

Diff for: general.log

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[2023-12-06 10:46:31 INFO]: install::setup-logging ended
2+
================
3+
[2023-12-06 10:46:31 INFO]: install::start-up started
4+
[2023-12-06 10:46:31 INFO]: install::start-up build image

Diff for: install.sh

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#!/usr/bin/env bash
2+
basefile="install"
3+
logfile="general.log"
4+
timestamp=`date '+%Y-%m-%d %H:%M:%S'`
5+
6+
if [ "$#" -ne 1 ]; then
7+
msg="[ERROR]: $basefile failed to receive enough args"
8+
echo "$msg"
9+
echo "$msg" >> $logfile
10+
exit 1
11+
fi
12+
13+
function setup-logging(){
14+
scope="setup-logging"
15+
info_base="[$timestamp INFO]: $basefile::$scope"
16+
17+
echo "$info_base started" >> $logfile
18+
19+
echo "$info_base removing old logs" >> $logfile
20+
21+
rm -f $logfile
22+
23+
echo "$info_base ended" >> $logfile
24+
25+
echo "================" >> $logfile
26+
}
27+
28+
function root-check(){
29+
scope="root-check"
30+
info_base="[$timestamp INFO]: $basefile::$scope"
31+
32+
echo "$info_base started" >> $logfile
33+
34+
#Make sure the script is running as root.
35+
if [ "$UID" -ne "0" ]; then
36+
echo "[$timestamp ERROR]: $basefile::$scope you must be root to run $0" >> $logfile
37+
echo "==================" >> $logfile
38+
echo "You must be root to run $0. Try the following"
39+
echo "sudo $0"
40+
exit 1
41+
fi
42+
43+
echo "$info_base ended" >> $logfile
44+
echo "================" >> $logfile
45+
}
46+
47+
function docker-check() {
48+
scope="docker-check"
49+
info_base="[$timestamp INFO]: $basefile::$scope"
50+
cmd=`docker -v`
51+
52+
echo "$info_base started" >> $logfile
53+
54+
if [ -z "$cmd" ]; then
55+
echo "$info_base docker not installed"
56+
echo "$info_base docker not installed" >> $logfile
57+
fi
58+
59+
echo "$info_base ended" >> $logfile
60+
echo "================" >> $logfile
61+
62+
}
63+
64+
function docker-compose-check() {
65+
scope="docker-compose-check"
66+
info_base="[$timestamp INFO]: $basefile::$scope"
67+
cmd=`docker-compose -v`
68+
69+
echo "$info_base started" >> $logfile
70+
71+
if [ -z "$cmd" ]; then
72+
echo "$info_base docker-compose not installed"
73+
echo "$info_base docker-compose not installed" >> $logfile
74+
fi
75+
76+
echo "$info_base ended" >> $logfile
77+
echo "================" >> $logfile
78+
79+
}
80+
function usage() {
81+
echo ""
82+
echo "Usage: "
83+
echo ""
84+
echo "-u: start."
85+
echo "-d: tear down."
86+
echo "-h: Display this help and exit."
87+
echo ""
88+
}
89+
function start-up(){
90+
91+
scope="start-up"
92+
docker_img_name=`head -n 1 README.md | sed 's/# //'`
93+
info_base="[$timestamp INFO]: $basefile::$scope"
94+
95+
echo "$info_base started" >> $logfile
96+
97+
echo "$info_base build image" >> $logfile
98+
99+
sudo docker-compose up --build
100+
101+
echo "$info_base running image" >> $logfile
102+
103+
echo "$info_base ended" >> $logfile
104+
105+
echo "================" >> $logfile
106+
}
107+
function tear-down(){
108+
109+
scope="tear-down"
110+
info_base="[$timestamp INFO]: $basefile::$scope"
111+
112+
echo "$info_base started" >> $logfile
113+
114+
echo "$info_base stoping services" >> $logfile
115+
116+
sudo docker-compose down
117+
118+
echo "$info_base ended" >> $logfile
119+
120+
echo "================" >> $logfile
121+
}
122+
123+
root-check
124+
docker-check
125+
docker-compose-check
126+
127+
while getopts ":udh" opts; do
128+
case $opts in
129+
u)
130+
setup-logging
131+
start-up ;;
132+
d)
133+
tear-down ;;
134+
h)
135+
usage
136+
exit 0 ;;
137+
/?)
138+
usage
139+
exit 1 ;;
140+
esac
141+
done

Diff for: py-srv/Dockerfile

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# We downgraded python from latest to 3.8 to get
2+
# cassandra drives to work. The alpine version is missing
3+
# some system dependcies for pandas so we use bullseye.
4+
5+
FROM python:3.8-bullseye
6+
7+
WORKDIR /code
8+
9+
COPY bin/ .
10+
11+
RUN pip install -r requirements.txt
12+
13+
ENV WAIT_VERSION 2.7.2
14+
15+
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/$WAIT_VERSION/wait /wait
16+
17+
RUN chmod +x /wait
18+
19+
CMD ["python", "app.py"]

Diff for: py-srv/bin/app.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pandas as pd
2+
from cassandra.cluster import Cluster
3+
4+
def connect():
5+
cluster = Cluster(['db'],port=9042)
6+
return cluster.connect()
7+
8+
def query(session, query):
9+
return session.execute(query)
10+
11+
def seed_from_file(session):
12+
from os import listdir
13+
from os.path import isfile, join
14+
15+
mypath = 'cql'
16+
files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
17+
sorted_files = sorted(files)
18+
for file in sorted_files:
19+
with open(join(mypath, file), 'r') as f:
20+
content = f.read()
21+
query(session, content)
22+
23+
def main():
24+
session = connect()
25+
seed_from_file(session)
26+
27+
if __name__ == "__main__":
28+
main()

Diff for: py-srv/bin/cql/00-create-keyspace.cql

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE KEYSPACE IF NOT EXISTS citizix
2+
WITH REPLICATION = {
3+
'class' : 'SimpleStrategy',
4+
'replication_factor' : 1
5+
};

Diff for: py-srv/bin/requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cassandra-driver
2+
pandas

0 commit comments

Comments
 (0)