Skip to content

Commit

Permalink
feat: d4g-tools first pending draft for discussion
Browse files Browse the repository at this point in the history
  • Loading branch information
gaudinnicolas committed Jul 18, 2024
1 parent ee1bf2f commit ec00cdf
Show file tree
Hide file tree
Showing 42 changed files with 3,211 additions and 159 deletions.
41 changes: 41 additions & 0 deletions .d4g-tools/bin/check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash
# shellcheck disable=SC1091
# shellcheck disable=SC1090
set -Eeuo pipefail

# Folder containing this script
RUN_DIR="$(dirname "${BASH_SOURCE[0]}")"
SELF_PATH="$(basename "${BASH_SOURCE[0]}")"
echo "RUN_DIR: $RUN_DIR"
echo "SELF_PATH: $SELF_PATH"
# IMPORTANT AND NECESSARY: Load common functions
source "$LIB_DIR"/common.sh

source "$VENV_DIR"/bin/activate

# Array of services to check
services=("nginx" "apache2" "uwsgi")

# Loop through the services
for service in "${services[@]}"; do
# Check if the service is installed
if dpkg -l | grep -q "$service"; then
echo "$service is installed."

# Print the service's binary location
bin_location=$(which "$service")
info "Binary location for $service: $bin_location"

# Print the port the service uses
if [ "$service" == "nginx" ] || [ "$service" == "apache2" ]; then
port=$(sudo lsof -i -P -n | grep "$service" | awk '{print $9}' | cut -d: -f2)
echo "Port for $service: $port"
elif [ "$service" == "uwsgi" ]; then
echo "uwsgi does not listen on a port. It is a protocol implemented over TCP."
fi
else
echo "$service is not installed."
fi
done
deactivate
cleanup
44 changes: 44 additions & 0 deletions .d4g-tools/bin/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env bash
set -Eeuo pipefail

# Function to display usage
usage() {
echo "Usage: $0 --platform=python|taipy"
exit 1
}

# Parse command line arguments
for i in "$@"; do
case $i in
--platform=*)
PLATFORM="${i#*=}"
shift
;;
*)
usage
;;
esac
done

# Check if PLATFORM is set
if [ -z "$PLATFORM" ]; then
usage
fi

# Create project directory if it doesn't exist
if [ ! -d "$PROJECT_NAME" ]; then
mkdir "$PROJECT_NAME"
echo "Created directory: $PROJECT_NAME"
fi

# Execute scripts based on platform
if [ "$PLATFORM" == "python" ] || [ "$PLATFORM" == "taipy" ]; then
./python.sh
echo "Executed python.sh"
if [ "$PLATFORM" == "taipy" ]; then
./taipy.sh
echo "Executed taipy.sh"
fi
fi

echo "Initialization complete for platform: $PLATFORM"
65 changes: 65 additions & 0 deletions .d4g-tools/bin/locust/locust.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env bash
# shellcheck disable=SC1091
# shellcheck disable=SC1090
# shellcheck disable=SC2034

set -Eeuo pipefail

# IMPORTANT AND NECESSARY: Load dependencies
source "$LIB_DIR"/common.sh

usage() {
cat <<EOF
USAGE ${0} [-v] [-h]
This is a description of the script.
Honestly, write whatever you want.
Supported parameters :
-h, --help : display this message
-v, --verbose : enable enhanced logging
EOF
exit 1
}

parse_params() {
if [ $# -gt 2 ]; then
echo "Too many parameters provided"
usage
fi

# Sane defaults
DEBUG="false"
RUN_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)

while :; do
case "${1-}" in
-h | --help)
usage
;;
-v | --verbose)
DEBUG="true"
;;
--dummy-flag*)
DUMMY_FLAG="true"
;;
--dummy-param=*)
DUMMY_PARAM="${1#*=}"
;;
-?*)
echo "Unknown option: $1"
usage
;;
*)
break
;;
esac
shift
done

return 0
}

parse_params "$@"

echo -n "Ready to rumble."
21 changes: 21 additions & 0 deletions .d4g-tools/bin/locust/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Tests de charge

Pour vérifier le bon fonctionnement du site avec plusieurs utilisateurs des tests de charge ont été mis en place, à l'aide de [Locust](https://locust.io/), un outil de test de charge.

<!-- Installation :
```shell
cd load_testing
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
``` -->

Lancement :
```shell
cd load_testing
source .venv/bin/activate
locust -f load_testing.py
```

Puis se rendre à l'adresse http://0.0.0.0:8089 pour lancer un test avec le nombre d'utilisateurs souhaités.

149 changes: 149 additions & 0 deletions .d4g-tools/bin/memory.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#!/usr/bin/env bash
# shellcheck disable=SC1091
# shellcheck disable=SC1090
# shellcheck disable=SC2034
set -Eeuo pipefail

usage() {
cat <<EOF
USAGE ${0} [-v] [-h] --action=<action>
Report memory usage (linux, WSL2)
Supported parameters :
-h, --help : display this message
-v, --verbose : enable enhanced logging
<action>: start, stop, show
EOF
exit 1
}

parse_params() {
if [ $# -lt 1 ]; then
echo "Some parameters are missing"
usage
elif [ $# -gt 2 ]; then
echo "Too many parameters provided"
usage
fi

RUN_SCRIPT="${BASH_SOURCE[0]}"
RUN_DIR="$(dirname "${RUN_SCRIPT}")"

source "$LIB_DIR/common.sh"
# Format the date as YYYYMMDDHHMM
date=$(date +"%Y%m%d")
USAGE_LOG="$LOG_DIR"/usage.$date.log
USAGE_PLOT="$LOG_DIR"/usage.$date.png

# Parameters
ACTION="false"

while :; do
case "${1-}" in
-h | --help)
usage
;;
-v | --verbose)
DEBUG="true"
;;
--action=*)
ACTION="${1#*=}"
;;
-?*)
echo "Unknown option: $1"
usage
;;
*)
break
;;
esac
shift
done

debug "ACTION: $ACTION"
debug "Log file: $USAGE_LOG"

return 0
}

parse_params "$@"

# Function to get memory usage
get_memory_usage() {
free -m | awk '/Mem/{printf("%.2f"), $3/$2*100}'
}

# Function to get CPU usage
get_cpu_usage() {
top -bn1 | grep "Cpu(s)" |
sed "s/.*, *\([0-9.]*\)%* id.*/\1/" |
awk '{print 100 - $1"%"}'
}

case "$ACTION" in
"record")
# Report memory usage
time=$(date +"%Y-%m-%d-%H:%M:%S")

# Get memory usage in MB and CPU usage
memory_usage=$(free -m | awk 'NR==2{printf "%.2f", $3 }')
cpu_usage=$(top -bn1 | grep load | awk '{printf "%.2f", $(NF-2)}')
# memory_usage=$(vmstat -s | grep "used memory" | awk '{print $1/1024}')
# cpu_usage=$(vmstat 1 2 | tail -1 | awk '{print $13+$14}')

echo "$time $memory_usage $cpu_usage" >>"$USAGE_LOG"
"$PROJECT_DIR/d4g" --memory=show
;;
"start")
"$PROJECT_DIR/d4g" --memory=record
# Add to user crontab if not already added
if ! crontab -l | grep -q "$PROJECT_DIR/d4g --memory=record"; then
(
crontab -l >/dev/null 2>&1
echo "* * * * * $PROJECT_DIR/d4g --memory=record"
) | crontab -
fi

;;
"show")
gnuplot <<EOF
set terminal png size 800,600
set output "$USAGE_PLOT"
set title 'Memory and CPU Usage Over Time'
set xlabel 'Time'
set xdata time
set timefmt '%Y-%m-%d-%H:%M:%S'
set format x '%H:%M'
set ylabel 'Used Memory (MB)'
set y2label 'CPU Usage (%)'
set ytics nomirror
set y2tics
set y2range [0:100] # Set upper value for y scale with 100 for CPU
set grid
plot "$USAGE_LOG" using 1:2 title 'Used Memory (MB)' with lines, \
"$USAGE_LOG" using 1:3 axes x1y2 title 'CPU Usage (%)' with lines
EOF
;;
"stop")
# Remove memory_usage.sh from crontab
# crontab -l | grep -v "$RUN_SCRIPT" | crontab -
crontab -l >tmpcron
grep -v "$PROJECT_DIR/d4g --memory=record" tmpcron >cron || true
crontab cron
;;
*) ;;
esac
# set title "System Usage Over Time"
# set xlabel "Time"
# set ylabel "Usage (%)"
# set xdata time
# set timefmt "%Y-%m-%d %H:%M:%S"
# set format x "%H:%M"
# set autoscale
# set term png
# set output "usage.png"
# plot "/tmp/usage_data.txt" using 1:2 title 'Memory' with lines, \
# "/tmp/usage_data.txt" using 1:3 title 'CPU' with lines
cleanup
58 changes: 58 additions & 0 deletions .d4g-tools/bin/nginx/nginx.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env bash
# shellcheck disable=SC1091
# shellcheck disable=SC1090

set -Eeuo pipefail

RUN_DIR="$(dirname "${BASH_SOURCE[0]}")"
# IMPORTANT AND NECESSARY: Load common functions
source "$LIB_DIR"/common.sh

# Create a self-signed SSL certificate
# sudo openssl req -x509 -newkey rsa:4096 -keyout /etc/ssl/certs/nginx-selfsigned.key -out /etc/ssl/certs/nginx-selfsigned.crt -sha256 -days 3650 -nodes -subj "/CN=localhost"
# Update the Nginx configuration to expose the application

NGINX_CONF="$PROJECT_NAME.$STAGE.nginx.conf"

if [ ! -f "$GENERATED/$NGINX_CONF" ]; then
error "Missing $GENERATED/$NGINX_CONF"
exit 1
fi

# Check if nginx is installed, if not then install it
if ! command -v nginx &>/dev/null; then
info "Installing nginx..."
# TODO use brew?
sudo apt install -y nginx
fi

sudo systemctl stop nginx

# Format the date as YYYYMMDDHHMM
date=$(date +"%Y%m%d")

nginx_sites_available="/etc/nginx/sites-available"
nginx_sites_enabled="/etc/nginx/sites-enabled"

if [ -f "$nginx_sites_available/$PROJECT_NAME" ]; then
sudo mv "$nginx_sites_available/$PROJECT_NAME" "$nginx_sites_available/$PROJECT_NAME.$date.bak"
fi
sudo cp "$GENERATED/$NGINX_CONF" "$nginx_sites_available/$PROJECT_NAME"

default_link="$nginx_sites_enabled/default"
if [ -L "${default_link}" ]; then
rm "$default_link"
fi

sudo ln -fs "$nginx_sites_available/$PROJECT_NAME" "$nginx_sites_enabled/$PROJECT_NAME"

# Restart Nginx to apply the changes
sudo systemctl start nginx

echo "GOOD JOB, YOU ARE NOW RUNNING THE APP IN DEV MODE that mimics production."
echo "Running services:"

echo "VISIT http://$DOMAIN:$HTTP_PORT via installed nginx proxy "
# echo " or http://$DOMAIN:$APP_PORT, directly via installed uwsgi server"

cleanup
Loading

0 comments on commit ec00cdf

Please sign in to comment.