Skip to content
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
File renamed without changes.
46 changes: 46 additions & 0 deletions AlirezaMirzaei/Problem1_PostgreSql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# My PostgreSQL Container Setup

This here is a quick way to spin up a PostgreSQL database with my schema and sample data already loaded, so here’s exactly how I did it for this assignment:

1. **Write the initialization script**

In my project root I created an `init-db.sql` file containing everything I wanted PostgreSQL to run on first boot:

- Create the database and initialize it.
- Create necessary tables (teams).
- Insert value into the tables and initialize them with the data necessary.
- Some changes to the database and confirmations that are in the container log after running the container.

2. **Create my startup script**

I wrote `setup-postgres.sh` so I could reproduce this on any machine:

- The script runs the docker container using the username and credentials provided plainly, this could be parameterized or given as a docker secret but seeing as this is a local installation with no need to access the internet i made it more simple.
- Then the script initializes, fills and updates the database using the file `init-db.sql`.
- Then the container status is checked via the command `pg_isready` in a loop.

3. **Verify the result**

After running `chmod +x setup-postgres.sh` and `./setup-postgres.sh`, I double-checked the container to be running:

```
docker exec -it postgres_ctf \
psql -U postgres -d ctf_db \
-c "SELECT * FROM teams;"
```

This should output the final data in the database after any change we make to the `teams` table

4. **Tearing down for a fresh start**

Whenever I want to start over, I simply do:

```
docker stop postgres_ctf && docker rm postgres_ctf
docker volume rm postgres_data
./setup-postgres.sh
```

### End. The video links:

https://iutbox.iut.ac.ir/index.php/s/oLbwink98GPyA36
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# Hint: SQL Commands for Question 1

```sql
-- Create a new database
CREATE DATABASE ctf_db;

-- Connect to the newly created database
\connect ctf_db;

-- Create a table to store team information
CREATE TABLE teams (
Expand All @@ -14,11 +13,13 @@ CREATE TABLE teams (

-- Insert sample data into the table
INSERT INTO teams (team_name, challenge_assigned)
VALUES
VALUES
('Red Team', true),
('Blue Team', false);
('Blue Team', false),
('Green Team', false);

-- Retrieve all records from the table
-- This is just to verify the data was inserted
-- The output will show in the container logs
SELECT * FROM teams;

-- Update a team's challenge assignment status
Expand All @@ -29,4 +30,10 @@ WHERE team_name = 'Blue Team';
-- Delete a team from the table
DELETE FROM teams
WHERE team_name = 'Red Team';
```

-- Insert an additional row as requested
INSERT INTO teams (team_name, challenge_assigned)
VALUES ('Yellow Team', true);

-- Final state of the table
SELECT * FROM teams;
22 changes: 22 additions & 0 deletions AlirezaMirzaei/Problem1_PostgreSql/setup_postgres.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

# Create volume for PostgreSQL data persistence
docker volume create postgres_data

# Run PostgreSQL container with initialization
docker run --name postgres_ctf \
-e POSTGRES_USERNAME=ctfadmin \
-e POSTGRES_PASSWORD=s#cr#tpasssswithasalt \
-d \
-p 5432:5432 \
-v postgres_data:/var/lib/postgresql/data \
-v $(pwd)/initdb.sql:/docker-entrypoint-initdb.d/init-db.sql \
postgres:14-alpine

# Wait until Postgres is actually ready
echo "Waiting for PostgreSQL to initialize…"
until docker exec postgres_ctf pg_isready -U postgres >/dev/null 2>&1; do
sleep 1
done

echo "PostgreSQL container initialized with our database schema and data!"
61 changes: 61 additions & 0 deletions AlirezaMirzaei/Problem2_Redis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# My Redis Server & IPC Demo

We also need a lightweight pub/sub demo alongside Redis key/value storage, so here’s how I put that together for this assignment:

1. **Spinning up Redis**
In `setup-redis.sh` I:
- I made a folder for redis data (volume) to be mounted in.
- I configured redis for listening address and mode of data access in `redis.conf`.
- I start the container with image `6-alpine` with the given settings and options.
- Check if redis is running successfuly using ping and pong command and output.
2. **Preparing my Python environment**
I set up a virtualenv and installed `redis`:

```bash
python -m venv .venv
source .venv/bin/activate
pip install redis
```

3. **Writing the producer** (`redis-producer.py`)

- Connect to Redis
- Set a few string keys and a hash
- Publish a “notification” every second, incrementing a counter

4. **Writing the consumer** (`redis-consumer.py`)

- Connect to Redis
- Dump all existing keys/hashes on start
- Subscribe to the `notifications` channel and print each new message
- The output is printed to stdout and can easily be inspected.

5. **Wrapping it all together**
In `run-demo.sh` I made sure Redis is running, then:

- I run the consumer file in background and set it's output to `consumer.log`.
- I run the producer file in foreground and stop the consumer when the producer is stopped.
- The logs can be viewed in `consumer.log`.

6. **Monitoring with RedisInsight**
I used `setup-redisinsight.sh` to spin up RedisInsight so I could watch keys, hashes, and pub/sub traffic through a GUI.

7. **Wrap Up**
The commands for using this module are run in this order:

- `./setup-redis.sh` to run and install redis.
- `./setup-redisinsight.sh` to run and install redis insight.
- Install requirements as mentioned in step 2 (don't forget).
- Run the demo file: `./run-demo.sh`.
- Inspect the outputs and redis insight logs.

8. **To Restart**:
- Run `docker stop redis-server` and `docker rm redis-server`.
- Run the commands above again.

### End. The video links:
https://iutbox.iut.ac.ir/index.php/s/oLbwink98GPyA36

---

That’s exactly how I went step-by-step through each setup for this assignment—hope it’s clear and easy to follow from my perspective!
76 changes: 76 additions & 0 deletions AlirezaMirzaei/Problem2_Redis/redis-consumer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env python3
import redis
import sys
import threading
import time
import logging

# Configure logging to write everything to consumer.log
logging.basicConfig(
filename="consumer.log",
filemode="a",
format="%(asctime)s %(levelname)s %(message)s",
level=logging.INFO,
)


def subscribe_to_channel(r, channel):
"""Subscribe to the specified Redis channel and log messages"""
logging.info(f"Subscribing to channel '{channel}'...")
pubsub = r.pubsub()
pubsub.subscribe(channel)

for message in pubsub.listen():
if message["type"] == "message":
data = message["data"]
logging.info(f"Received message on '{channel}': {data}")


def fetch_data(r):
"""Periodically fetch and log data from Redis"""
while True:
try:
service_status = r.get("service_status")
last_update = r.get("last_update")
message_count = r.get("message_count")
system_info = r.hgetall("system_info")

logging.info("Current Redis Data:")
logging.info(f" service_status: {service_status}")
logging.info(f" last_update: {last_update}")
logging.info(f" message_count: {message_count}")
logging.info(" system_info:")
for key, value in system_info.items():
logging.info(f" {key}: {value}")

time.sleep(5)
except Exception as e:
logging.error(f"Error fetching data: {e}", exc_info=True)
time.sleep(5)


def main():
logging.info("Connecting to Redis server...")
try:
r = redis.Redis(host="localhost", port=6379, db=0, decode_responses=True)
r.ping()
logging.info("Successfully connected to Redis server")
except redis.ConnectionError as e:
logging.error(f"Failed to connect to Redis: {e}")
sys.exit(1)

# Start data-fetch thread
data_thread = threading.Thread(target=fetch_data, args=(r,), daemon=True)
data_thread.start()

# Run subscriber in main thread
try:
subscribe_to_channel(r, "notifications")
except KeyboardInterrupt:
logging.info("Shutting down consumer due to keyboard interrupt")

logging.info("Consumer exiting")


if __name__ == "__main__":
main()
60 changes: 60 additions & 0 deletions AlirezaMirzaei/Problem2_Redis/redis-producer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python3
import redis
import time
import sys


def main():
print("Producer: Connecting to Redis server...")
try:
# Connect to Redis server
r = redis.Redis(host="localhost", port=6379, db=0, decode_responses=True)
r.ping() # Test connection
print("Producer: Successfully connected to Redis server")
except redis.ConnectionError as e:
print(f"Producer: Failed to connect to Redis: {e}", file=sys.stderr)
sys.exit(1)

# Set some key-value pairs
print("Producer: Setting key-value pairs...")
r.set("service_status", "running")
r.set("last_update", time.strftime("%Y-%m-%d %H:%M:%S"))
r.set("message_count", "0")

# Create a hash (dictionary)
r.hset(
"system_info",
mapping={
"hostname": "redis-test",
"version": "1.0.0",
"environment": "development",
},
)

print("Producer: Key-value pairs set successfully")

# Publish messages to a channel
channel = "notifications"
message_count = 0

print(f"Producer: Starting to publish messages to channel '{channel}'")
print("Producer: Press Ctrl+C to stop")

try:
while True:
message_count += 1
message = f"Message #{message_count} at {time.strftime('%H:%M:%S')}"
r.publish(channel, message)
r.set("message_count", str(message_count))
print(f"Producer: Published: {message}")

# Sleep for 2 seconds between messages
time.sleep(2)
except KeyboardInterrupt:
print("\nProducer: Stopping message publication")

print("Producer: Done")


if __name__ == "__main__":
main()
30 changes: 30 additions & 0 deletions AlirezaMirzaei/Problem2_Redis/run-demo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

echo "Ensuring Redis is running..."
docker ps | grep redis-server > /dev/null
if [ $? -ne 0 ]; then
echo "Redis server not running. Starting it now..."
./setup-redis.sh
fi

# Run the consumer in background
echo "Starting Redis consumer in background..."
./redis-consumer.py &
CONSUMER_PID=$!

echo "Consumer started with PID $CONSUMER_PID"
echo "Consumer logs are being written to consumer.log"


# Wait a bit to ensure consumer is ready
sleep 2

echo "Starting Redis producer in foreground..."
echo "Press Ctrl+C to stop the producer when done"
./redis-producer.py

# When producer is stopped, also stop the consumer
echo "Stopping consumer process..."
kill $CONSUMER_PID

echo "Demo completed. You can view the consumer logs in consumer.log"
37 changes: 37 additions & 0 deletions AlirezaMirzaei/Problem2_Redis/setup-redis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash

# Create a directory for Redis data
mkdir -p redis-data

# Create a simple Redis configuration file for our needs
cat >redis.conf <<EOL
bind 0.0.0.0
port 6379
appendonly yes
protected-mode no
EOL

echo "Starting Redis container..."
docker run -d \
--name redis-server \
-p 6379:6379 \
-v "$(pwd)/redis.conf:/usr/local/etc/redis/redis.conf" \
-v "$(pwd)/redis-data:/data" \
redis:6-alpine \
redis-server /usr/local/etc/redis/redis.conf

echo "Waiting for Redis to start..."
sleep 2

# Test in a loop if Redis is working
echo "Waiting for Redis…"
until docker exec redis-server redis-cli PING | grep -q PONG; do
sleep 1
done

if [ $? -eq 0 ]; then
echo "Redis server is running successfully!"
else
echo "Failed to start Redis server. Please check the logs:"
docker logs redis-server
fi
Loading