Skip to content

Commit

Permalink
Drop support for the --interface flag of unset-static-ip script (#1719)
Browse files Browse the repository at this point in the history
Related #1710.

[As discussed in the
issue](#1710 (comment)),
this PR drops support for the `--interface` flag of the
`unset-static-ip` script. Instead of allowing to selectively unset
interface configurations, the `unset-static-ip` script now only allows
to remove all/any custom IP configs. We didn’t find to have a need for
the selective unsetting, and can therefore spare the complexity.

## Notes

- I noticed we accidentally forgot to parse the `--help` flag, and we
also forgot to document the `--help` and `--quiet` flags in the usage
description. I’ve fixed both.
- I dropped the “Examples” section from the help output, because without
the `--interface` flag the usage seems trivial enough to me.
- Using the `--interface` flag now produces an error, just in case
someone relied on it.
- The code for eliminating the marker sections is now the same as [the
code in the `change-hostname` privileged
script](https://github.com/tiny-pilot/tinypilot/blob/8e99d5a666df12d851b14dd4276e8b95ab958493/debian-pkg/opt/tinypilot-privileged/scripts/change-hostname#L62-L79)
– except for the `dhcpcd_original` variable name. Both instances will
later be replaced by a call to the [upcoming unified
script](#1710).
- Heads-up: this PR will (probably) conflict with
tiny-pilot/tinypilot-pro#1175, as both touch the
tail of the `unset-static-ip` script. It should be trivial to resolve
this, though.
<a data-ca-tag
href="https://codeapprove.com/pr/tiny-pilot/tinypilot/1719"><img
src="https://codeapprove.com/external/github-tag-allbg.png" alt="Review
on CodeApprove" /></a>

---------

Co-authored-by: Jan Heuermann <[email protected]>
  • Loading branch information
jotaen4tinypilot and jotaen authored Jan 15, 2024
1 parent 52faec1 commit 2e21119
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 53 deletions.
6 changes: 5 additions & 1 deletion debian-pkg/opt/tinypilot-privileged/scripts/set-static-ip
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ Sets a static local IP address for the TinyPilot device.
Defaults to eth0.
--help: Display this help text.
Note: there can only be one custom static IP configuration at a time. Running
this script will clear any potentially existing custom configuration
that might have been added previously by this script.
Examples:
1. Set static IP to 10.0.105 on a 10.0.0.x network.
Expand Down Expand Up @@ -134,7 +138,7 @@ fi
set -u

# Use the unset-static-ip script to remove any existing configuration.
"${SCRIPT_DIR}/unset-static-ip" --quiet --interface "${INTERFACE}"
"${SCRIPT_DIR}/unset-static-ip" --quiet

# Only proceed if no other configuration exists.
if grep -q "^interface ${INTERFACE}" "${CONFIG_FILE}" ; then
Expand Down
73 changes: 21 additions & 52 deletions debian-pkg/opt/tinypilot-privileged/scripts/unset-static-ip
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
#!/bin/bash
#
# Reverts changes made by the set-static-ip script.
# Reverts any changes made by the set-static-ip script.

# Exit on first error.
set -e

print_help() {
cat << EOF
Usage: ${0##*/} [--interface name]
Usage: ${0##*/} [--quiet] [--help]
Reverts changes made by the set-static-ip script.
--interface name: Optional. The interface to configure (e.g., eth0). Defaults
to eth0.
--quiet: Optional. Surpress the confirmation message.
Reverts any changes made by the set-static-ip script.
--quiet: Suppress the confirmation message.
--help: Display this help text.
Examples:
1. Unset the static IP address on the default eth0 interface.
unset-static-ip
2. Quietly unset a static IP address on the wlan0 interface.
unset-static-ip \\
--interface wlan0
EOF
}

Expand All @@ -34,7 +22,7 @@ readonly CONFIG_FILE="/etc/dhcpcd.conf"
. "${SCRIPT_DIR}/lib/markers.sh"

# Parse command-line arguments.
if ! NEW_ARGS=$(getopt -q -o "" -l "quiet,interface:" -- "$@"); then
if ! NEW_ARGS=$(getopt -q -o "" -l "quiet,help,interface:" -- "$@"); then
print_help
exit 1
fi
Expand All @@ -48,15 +36,13 @@ while true; do
shift 1
;;
--interface)
if [[ -z "${INTERFACE}" ]] ; then
readonly INTERFACE="$2"
else
echo "Only one interface should be provided using --interface" >&2
echo "For help on using this tool:" >&2
echo " unset-static-ip --help" >&2
exit 1
fi
shift 2
# See https://github.com/tiny-pilot/tinypilot/issues/1710#issuecomment-1885680744.
echo 'The --interface flag is no longer supported' >&2
exit 1
;;
--help)
print_help
exit
;;
--)
shift
Expand All @@ -65,11 +51,6 @@ while true; do
esac
done

# Default interface.
if [[ -z "${INTERFACE}" ]] ; then
readonly INTERFACE="eth0"
fi

# Default to not quiet.
if [[ -z "${QUIET}" ]] ; then
readonly QUIET="false"
Expand All @@ -78,35 +59,24 @@ fi
# Exit on unset variable
set -u

# Remove any existing auto-generated configuration for the interface.
# Remove any existing auto-generated configuration as populated by the
# set-static-ip script.
dhcpcd_original=()
section=()
interface_matched="false"
is_in_marker_section='false'
while IFS= read -r line; do
if [[ "${#section[@]}" -ne 0 ]] && [[ "${line}" == "${MARKER_END}" ]]; then
section+=("${line}")
if [[ "${interface_matched}" == "false" ]] ; then
dhcpcd_original=("${dhcpcd_original[@]}" "${section[@]}")
fi
section=()
interface_matched="false"
if "${is_in_marker_section}" && [[ "${line}" == "${MARKER_END}" ]]; then
is_in_marker_section='false'
continue
fi
if [[ "${#section[@]}" -ne 0 ]] || [[ "${line}" == "${MARKER_START}" ]]; then
if [[ "${line}" == "interface ${INTERFACE}" ]] ; then
interface_matched="true"
fi
section+=("${line}")
if "${is_in_marker_section}" || [[ "${line}" == "${MARKER_START}" ]]; then
is_in_marker_section='true'
continue
fi
dhcpcd_original+=("${line}")
done < "${CONFIG_FILE}"

if [[ "${#section[@]}" -ne 0 ]] ; then
echo "The contents of ${CONFIG_FILE} are not in the expected format." >&2
echo "Auto-generated changes may have been manually edited." >&2
echo "" >&2
echo "Remove the relevant configuration manually to proceed." >&2
if "${is_in_marker_section}"; then
echo 'Unclosed marker section' >&2
exit 1
fi

Expand All @@ -120,7 +90,6 @@ echo "${OLD_CONFIG_FILE}" | sudo tee "${CONFIG_FILE}" > /dev/null
# Changes require a reboot so prompt the user.
if [[ "${QUIET}" == "false" ]] ; then
echo "Any changes made by the set-static-ip script have been reverted."
echo " Interface: ${INTERFACE}"
echo ""
echo "Reboot your TinyPilot device to apply the changes."
fi

0 comments on commit 2e21119

Please sign in to comment.