Skip to content

Commit

Permalink
Improve build.sh script
Browse files Browse the repository at this point in the history
Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Nov 1, 2024
1 parent 51dd2c5 commit 7e0952b
Showing 1 changed file with 119 additions and 7 deletions.
126 changes: 119 additions & 7 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,80 @@ builddir="cmake/"
for var in "$@"
do
case "${var}" in
"-c" | "clean" ) clean=1;;
"-C" | "CLEAN" ) clean=1 && nobuild=1;;
"-i" | "install" ) install=1;;
"-t" | "test" ) test=1;;
"clang" ) clang=1;;
"ci" ) builddir="cmake_ci/";;
"-c" | "clean" ) clean=1;;
"-C" | "CLEAN" ) clean=1 && nobuild=1;;
"-i" | "install" ) install=1;;
"-r" | "restart" ) restart=1;;
"-f" | "tail" ) tail=1;;
"-d" | "debug" ) debug=1;;
"-D" | "dev" ) dev=1;;
"-t" | "test" ) test=1;;
"clean-logs" ) clean_logs=1;;
"clang" ) clang=1;;
"ci" ) builddir="cmake_ci/";;
"-h" | "--help" ) help=1;;
esac
done

# Display help text
if [[ -n "${help}" ]]; then
cat << EOF
Usage: $0 [options]
Helper script simplifying the build process of Pi-hole FTL.
Options:
-d, dev Build, install, restart, and tail logs.
-c, clean Clean the build environment and trigger a build.
-C, CLEAN Clean the build environment and DO NOT build.
-i, install Install the built binaries (requires sudo).
-r, restart Restart the pihole-FTL service (requires sudo).
-h, --help Display this help text.
clean-logs Clean the FTL and dnsmasq log files.
-f, tail Tail (follow) the FTL and dnsmasq log files.
Special CI options:
ci Use the CI build directory (cmake_ci/).
clang Use clang as the compiler.
-t, test Run tests after building.
If no options are provided, the script will build the sources.
If the -d option is provided, the script will build, install, restart,
and tail the two most important log file. The -d option is intended
for development purposes.
EOF

exit 0
fi

# debug and tail are mutually exclusive
if [[ -n "${debug}" && -n "${tail}" ]]; then
echo "Error: -d and -f are mutually exclusive"
exit 1
fi

# If we are in debug mode, we also want to build, install, and restart
if [[ -n "${debug}" ]]; then
install=1
restart=1
fi

# If we are in dev mode, we want to build, install, restart, and tail the logs
# by default
if [[ -n "${dev}" ]]; then
install=1
restart=1
tail=1
fi

# Check if we need sudo
SUDO=""
if [[ -n "${install}" || -n "${restart}" || -n "${clean_logs}" ]]; then
# Check if we are root, if not, we need sudo
if [[ $(id -u) -ne 0 ]]; then
SUDO="sudo"
fi
fi

# Prepare build environment
if [[ -n "${clean}" ]]; then
echo "Cleaning build environment"
Expand Down Expand Up @@ -89,7 +154,6 @@ cmake --build . -- -j $(nproc)
# Otherwise, we simply copy the binary one level down
if [[ -n "${install}" ]]; then
echo "Installing pihole-FTL"
SUDO=$(command -v sudo)
${SUDO} cmake --install .
else
echo "Copying compiled pihole-FTL binary to repository root"
Expand All @@ -102,3 +166,51 @@ if [[ -n "${test}" ]]; then
bash test/arch_test.sh
bash test/run.sh
fi

# If we are asked to restart, we do this here
if [[ -n "${restart}" ]]; then
echo "Restarting pihole-FTL"

# First, reset the failure-counter in case a previous error caused a
# restarting loop now preventing systemd from starting FTL
${SUDO} systemctl reset-failed pihole-FTL

# Restart FTL
${SUDO} systemctl restart pihole-FTL
fi

# If we are asked to clean the logs, we do this here
if [[ -n "${clean_logs}" ]]; then
echo "Cleaning log files"
for log_file in "$(pihole-FTL --config files.log.ftl)" "$(pihole-FTL --config files.log.dnsmasq)"; do
echo "Cleaning ${log_file}"
echo "" | ${SUDO} tee "$log_file"
done
fi

# If we want to attach the debugger, we do this here
if [[ -n "${debug}" ]]; then
echo "Waiting for pihole-FTL to start..."
pid_file=$(pihole-FTL --config files.pid)

# Loop until the pid file is created and non-empty
while [ ! -f "${pid_file}" ] || [ ! -s "${pid_file}" ]; do
sleep 0.1
done

# Get the pid from the pid file
pid=$(cat "${pid_file}")

# Attach gdb to the process
echo "Attaching debugger to pihole-FTL (PID: ${pid})..."
${SUDO} gdb -p "${pid}"
fi

# If we are asked to tail the log, we do this here
if [[ -n "${tail}" ]]; then
ftl_log=$(pihole-FTL --config files.log.ftl)
dnsmasq_log=$(pihole-FTL --config files.log.dnsmasq)

# Create tmux sub-session with two panes next to each other each running a tail command
tmux new-session -d "tail -f ${ftl_log}" \; split-window -h "tail -f ${dnsmasq_log}" \; attach
fi

0 comments on commit 7e0952b

Please sign in to comment.