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

Refactor reporters and install script #920

Merged
merged 46 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
5117993
Added DiskIO Reporter
ArneTR Jun 30, 2024
0d25675
User ID should be invalid by default
ArneTR Aug 21, 2024
0594a3f
Return of function was superflous
ArneTR Aug 21, 2024
394d52c
Normalized return codes to be 1 in case of error instead of random 127
ArneTR Aug 21, 2024
e7bdc8d
fd initialized directly where applicable
ArneTR Aug 21, 2024
f2e8d63
Using PATH_MAX instead of wrongly used BUFSIZ
ArneTR Aug 21, 2024
c7cc2d3
Using snprintf where possible
ArneTR Aug 21, 2024
7aea8e2
Adding malloc checks if memory could not be allocated
ArneTR Aug 21, 2024
601be5d
Some formatting
ArneTR Aug 22, 2024
5981480
Added one missing malloc check
ArneTR Aug 22, 2024
02063b3
Added strncpy and NUL terminations
ArneTR Aug 22, 2024
1ba699b
Variable name typo
ArneTR Aug 22, 2024
7123419
Reworking base.py to have custom detail_name code in the child classes
ArneTR Sep 25, 2024
de59482
SCI Fix to not use config, but supplied value
ArneTR Sep 25, 2024
5898e49
Reducing metric providers to actually used ones
ArneTR Sep 25, 2024
1728bbd
Adding sscanf / fscanf result checks where possible
ArneTR Sep 25, 2024
d136434
Added new providers; Moved IO providers to actual IO
ArneTR Sep 25, 2024
8499956
Added providers to config [skip-ci]
ArneTR Sep 25, 2024
177c09e
Wrong field access [skip ci]
ArneTR Sep 25, 2024
ee5d031
Merge branch 'main' into blue-angel-reporters
ArneTR Sep 25, 2024
deb581c
Many small fixes to metric providers; Added metric provider value tests
ArneTR Sep 27, 2024
dc61620
Merge branch 'main' into blue-angel-reporters
ArneTR Sep 27, 2024
f0906fe
Name correction
ArneTR Sep 27, 2024
6af2426
Test fix
ArneTR Sep 27, 2024
5382339
More margin
ArneTR Sep 27, 2024
c99d74e
Refactored install script to have shared file
ArneTR Sep 27, 2024
7a3cef0
Using 512 if path is not accessible. This can happen in VMs
ArneTR Sep 27, 2024
72d433e
Memory test must be longer apparently
ArneTR Sep 28, 2024
60ed0a6
Renamed file
ArneTR Sep 28, 2024
76bd0be
Removed guard clause again. No help
ArneTR Sep 28, 2024
6c58f77
Decimal conversion
ArneTR Sep 28, 2024
71138cf
Added memory.used provider to config
ArneTR Sep 28, 2024
02cda9a
Made tests more resilient
ArneTR Sep 28, 2024
9e09e73
Skipping a test for GitHub Actions
ArneTR Sep 28, 2024
70b2b58
Slimmed testing workflow a little
ArneTR Sep 28, 2024
209e1cb
Merge branch 'blue-angel-reporters' into refactor-repoters-and-install
ArneTR Sep 28, 2024
93b8806
Replacing atoi with proper number parsing
ArneTR Sep 28, 2024
1d0d98d
Merge branch 'main' into refactor-repoters-and-install
ArneTR Sep 28, 2024
22c04f8
parseopts in install must be inline
ArneTR Sep 28, 2024
2b27ab3
Moved RAPL reporter to new re-using opened file code
ArneTR Sep 28, 2024
c2eae74
Moving getopts to end of file and marking vars as local
ArneTR Sep 28, 2024
81f2843
Superflous local
ArneTR Sep 28, 2024
d5381a2
Adding dev requirements again
ArneTR Sep 29, 2024
865512a
macOS also needs C libs [skip ci]
ArneTR Sep 29, 2024
5f56e6a
Merge branch 'main' into refactor-repoters-and-install
ArneTR Sep 29, 2024
6775117
Removed memory.used.procfs from defaults
ArneTR Sep 29, 2024
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
3 changes: 2 additions & 1 deletion .github/actions/gmt-pytest/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ runs:
./install_linux.sh -p testpw -a http://api.green-coding.internal:9142 -m http://metrics.green-coding.internal:9142 -b -t
source venv/bin/activate

- name: disable unneeded metric providers and run test setup script
- name: Install dev requirements and run test setup script
shell: bash
working-directory: ${{ inputs.gmt-directory }}
run: |
source venv/bin/activate
python3 -m pip install -r requirements-dev.txt
cd tests && python3 setup-test-env.py --no-docker-build

- name: Set up Docker Buildx
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ venv/
lib/hardware_info_root.py
tools/cluster/cleanup.sh
node_modules/
lib/c/parse_int.o
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we just ignore all *.o files?

262 changes: 15 additions & 247 deletions install_linux.sh
Original file line number Diff line number Diff line change
@@ -1,174 +1,20 @@
#!/bin/bash
set -euo pipefail

GREEN='\033[0;32m'
NC='\033[0m' # No Color

if [[ $(uname) != "Linux" ]]; then
echo "Error: This script can only be run on Linux."
exit 1
fi

function print_message {
echo ""
echo "$1"
}
source lib/install_shared.sh # will parse opts immediately

function generate_random_password() {
local length=$1
LC_ALL=C tr -dc 'A-Za-z0-9' < /dev/urandom | head -c "$length"
echo
}
prepare_config

function check_file_permissions() {
local file=$1
setup_python

# Check if the file exists
if [ ! -e "$file" ]; then
echo "File '$file' does not exist."
return 1
fi

# Check if the file is owned by root
if [ "$(stat -c %U "$file")" != "root" ]; then
echo "File '$file' is not owned by root."
return 1
fi

permissions=$(stat -c %A "$file")
if [ -L "$file" ]; then
echo "File '$file' is a symbolic link. Following ..."
check_file_permissions $(readlink -f $file)
return $?
elif [[ ! $permissions =~ ^-r..r-.r-.$ ]]; then
echo "File '$file' is not read-only for group and others or not a regular file"
return 1
fi

echo "File $file is save to create sudoers entry for"

return 0
}

db_pw=''
api_url=''
metrics_url=''
build_docker_containers=true
install_python_packages=true
modify_hosts=true
ask_tmpfs=true
install_ipmi=true
install_sensors=true
install_msr_tools=true
# The system site packages are only an option to choose if you are in temporary VMs anyway
# Not recommended for classical developer system
use_system_site_packages=false
reboot_echo_flag=false

while getopts "p:a:m:nhtbisyr" o; do
case "$o" in
p)
db_pw=${OPTARG}
;;
a)
api_url=${OPTARG}
;;
m)
metrics_url=${OPTARG}
;;
b)
build_docker_containers=false
;;
h)
modify_hosts=false
;;
n)
install_python_packages=false
;;
t)
ask_tmpfs=false
;;
i)
install_ipmi=false
;;
s)
install_sensors=false
# currently unused
;;
r)
install_msr_tools=false
;;
y)
use_system_site_packages=true
;;

esac
done

if [[ -z $api_url ]] ; then
read -p "Please enter the desired API endpoint URL: (default: http://api.green-coding.internal:9142): " api_url
api_url=${api_url:-"http://api.green-coding.internal:9142"}
fi

if [[ -z $metrics_url ]] ; then
read -p "Please enter the desired metrics dashboard URL: (default: http://metrics.green-coding.internal:9142): " metrics_url
metrics_url=${metrics_url:-"http://metrics.green-coding.internal:9142"}
fi

if [[ -f config.yml ]]; then
password_from_file=$(awk '/postgresql:/ {flag=1; next} flag && /password:/ {print $2; exit}' config.yml)
fi

default_password=${password_from_file:-$(generate_random_password 12)}

if [[ -z "$db_pw" ]] ; then
read -sp "Please enter the new password to be set for the PostgreSQL DB (default: $default_password): " db_pw
echo "" # force a newline, because read -sp will consume it
db_pw=${db_pw:-"$default_password"}
fi

if ! mount | grep -E '\s/tmp\s' | grep -Eq '\stmpfs\s' && [[ $ask_tmpfs == true ]]; then
read -p "We strongly recommend mounting /tmp on a tmpfs. Do you want to do that? (y/N)" tmpfs
if [[ "$tmpfs" == "Y" || "$tmpfs" == "y" ]] ; then
if lsb_release -is | grep -q "Fedora"; then
sudo systemctl unmask --now tmp.mount
else
sudo systemctl enable /usr/share/systemd/tmp.mount
fi
reboot_echo_flag=true
fi
fi
build_containers

print_message "Clearing old api.conf and frontend.conf files"
rm -Rf docker/nginx/api.conf
rm -Rf docker/nginx/frontend.conf

print_message "Updating compose.yml with current path ..."
cp docker/compose.yml.example docker/compose.yml
sed -i -e "s|PATH_TO_GREEN_METRICS_TOOL_REPO|$PWD|" docker/compose.yml
sed -i -e "s|PLEASE_CHANGE_THIS|$db_pw|" docker/compose.yml

print_message "Updating config.yml with new password ..."
cp config.yml.example config.yml
sed -i -e "s|PLEASE_CHANGE_THIS|$db_pw|" config.yml

print_message "Updating project with provided URLs ..."
sed -i -e "s|__API_URL__|$api_url|" config.yml
sed -i -e "s|__METRICS_URL__|$metrics_url|" config.yml
cp docker/nginx/api.conf.example docker/nginx/api.conf
host_api_url=`echo $api_url | sed -E 's/^\s*.*:\/\///g'`
host_api_url=${host_api_url%:*}
sed -i -e "s|__API_URL__|$host_api_url|" docker/nginx/api.conf
cp docker/nginx/frontend.conf.example docker/nginx/frontend.conf
host_metrics_url=`echo $metrics_url | sed -E 's/^\s*.*:\/\///g'`
host_metrics_url=${host_metrics_url%:*}
sed -i -e "s|__METRICS_URL__|$host_metrics_url|" docker/nginx/frontend.conf
cp frontend/js/helpers/config.js.example frontend/js/helpers/config.js
sed -i -e "s|__API_URL__|$api_url|" frontend/js/helpers/config.js
sed -i -e "s|__METRICS_URL__|$metrics_url|" frontend/js/helpers/config.js

print_message "Checking out further git submodules ..."
git submodule update --init

print_message "Installing needed binaries for building ..."
if lsb_release -is | grep -q "Fedora"; then
Expand All @@ -180,51 +26,16 @@ fi
sudo systemctl stop tinyproxy
sudo systemctl disable tinyproxy

print_message "Building binaries ..."
metrics_subdir="metric_providers"
parent_dir="./$metrics_subdir"
make_file="Makefile"
find "$parent_dir" -type d |
while IFS= read -r subdir; do
make_path="$subdir/$make_file"
if [[ -f "$make_path" ]] && [[ ! "$make_path" == *"/mach/"* ]]; then
echo "Installing $subdir/metric-provider-binary ..."
rm -f $subdir/metric-provider-binary 2> /dev/null
make -C $subdir
fi
done

print_message "Setting up python venv"
if [[ $use_system_site_packages == true ]] ; then
python3 -m venv venv --system_site_packages
else
python3 -m venv venv
fi
source venv/bin/activate
print_message "Building C libs"
make -C "lib/c"

print_message "Setting GMT in include path for python via .pth file"
find venv -type d -name "site-packages" -exec sh -c 'echo $PWD > "$0/gmt-lib.pth"' {} \;
build_binaries

print_message "Building sgx binaries"
make -C lib/sgx-software-enable
mv lib/sgx-software-enable/sgx_enable tools/
rm lib/sgx-software-enable/sgx_enable.o

print_message "Adding python3 lib.hardware_info_root to sudoers file"
check_file_permissions "/usr/bin/python3"
# Please note the -m as here we will later call python3 without venv. It must understand the .lib imports
# and not depend on venv installed packages
echo "ALL ALL=(ALL) NOPASSWD:/usr/bin/python3 -m lib.hardware_info_root" | sudo tee /etc/sudoers.d/green-coding-hardware-info
echo "ALL ALL=(ALL) NOPASSWD:/usr/bin/python3 -m lib.hardware_info_root --read-rapl-energy-filtering" | sudo tee -a /etc/sudoers.d/green-coding-hardware-info
sudo chmod 500 /etc/sudoers.d/green-coding-hardware-info
# remove old file name
sudo rm -f /etc/sudoers.d/green_coding_hardware_info

print_message "Setting the hardare hardware_info to be owned by root"
sudo cp -f $PWD/lib/hardware_info_root_original.py $PWD/lib/hardware_info_root.py
sudo chown root:root $PWD/lib/hardware_info_root.py
sudo chmod 755 $PWD/lib/hardware_info_root.py

print_message "Setting the cluster cleanup.sh file to be owned by root"
sudo cp -f $PWD/tools/cluster/cleanup_original.sh $PWD/tools/cluster/cleanup.sh
sudo chown root:root $PWD/tools/cluster/cleanup.sh
Expand Down Expand Up @@ -260,61 +71,18 @@ if [[ $install_ipmi == true ]] ; then
sudo rm -f /etc/sudoers.d/ipmi_get_machine_energy_stat
fi


if [[ $modify_hosts == true ]] ; then

etc_hosts_line_1="127.0.0.1 green-coding-postgres-container"
etc_hosts_line_2="127.0.0.1 ${host_api_url} ${host_metrics_url}"

print_message "Writing to /etc/hosts file..."

# Entry 1 is needed for the local resolution of the containers through the jobs.py and runner.py
if ! sudo grep -Fxq "$etc_hosts_line_1" /etc/hosts; then
echo "$etc_hosts_line_1" | sudo tee -a /etc/hosts
else
echo "Entry was already present..."
fi

# Entry 2 can be external URLs. These should not resolve to localhost if not explcitely wanted
if [[ ${host_metrics_url} == *".green-coding.internal"* ]];then
if ! sudo grep -Fxq "$etc_hosts_line_2" /etc/hosts; then
echo "$etc_hosts_line_2" | sudo tee -a /etc/hosts
if ! mount | grep -E '\s/tmp\s' | grep -Eq '\stmpfs\s' && [[ $ask_tmpfs == true ]]; then
read -p "We strongly recommend mounting /tmp on a tmpfs. Do you want to do that? (y/N)" tmpfs
if [[ "$tmpfs" == "Y" || "$tmpfs" == "y" ]] ; then
if lsb_release -is | grep -q "Fedora"; then
sudo systemctl unmask --now tmp.mount
else
echo "Entry was already present..."
sudo systemctl enable /usr/share/systemd/tmp.mount
fi
reboot_echo_flag=true
fi
fi

if [[ $build_docker_containers == true ]] ; then
print_message "Building / Updating docker containers"
if docker info 2>/dev/null | grep rootless; then
print_message "Docker is running in rootless mode. Using non-sudo call ..."
docker compose -f docker/compose.yml down
docker compose -f docker/compose.yml build
docker compose -f docker/compose.yml pull
else
print_message "Docker is running in default root mode. Using sudo call ..."
sudo docker compose -f docker/compose.yml down
sudo docker compose -f docker/compose.yml build
sudo docker compose -f docker/compose.yml pull
fi
fi

if [[ $install_python_packages == true ]] ; then
print_message "Updating python requirements"
python3 -m pip install --upgrade pip
python3 -m pip install -r requirements.txt
python3 -m pip install -r docker/requirements.txt
python3 -m pip install -r metric_providers/psu/energy/ac/xgboost/machine/model/requirements.txt
fi

# kill the sudo timestamp
sudo -k
finalize

echo ""
echo -e "${GREEN}Successfully installed Green Metrics Tool!${NC}"
echo -e "Please remember to always activate your venv when using the GMT with 'source venv/bin/activate'"

if $reboot_echo_flag; then
echo -e "${GREEN}If you have newly requested to mount /tmp as tmpfs please reboot your system now.${NC}"
fi
Loading
Loading