Skip to content

Commit

Permalink
changed dhcpcd timer from cron to systemd
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvinSchiller committed Jan 25, 2024
1 parent 177f57f commit 773a3a9
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 55 deletions.
3 changes: 2 additions & 1 deletion installation/includes/01_default_config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ BUILD_LIBZMQ_WITH_DRAFTS_ON_DEVICE=${BUILD_LIBZMQ_WITH_DRAFTS_ON_DEVICE:-"false"
ENABLE_STATIC_IP=true
DISABLE_IPv6=true
ENABLE_AUTOHOTSPOT=false
AUTOHOTSPOT_SSID="Phoniebox_Hotspot"
AUTOHOTSPOT_PROFILE="Phoniebox_Hotspot"
AUTOHOTSPOT_SSID="$AUTOHOTSPOT_PROFILE"
AUTOHOTSPOT_PASSWORD="PlayItLoud!"
AUTOHOTSPOT_IP="10.0.0.1"
AUTOHOTSPOT_COUNTRYCODE="DE"
Expand Down
2 changes: 2 additions & 0 deletions installation/routines/setup_autohotspot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ AUTOHOTSPOT_INTERFACES_CONF_FILE="/etc/network/interfaces"
AUTOHOTSPOT_TARGET_PATH="/usr/bin/autohotspot"
AUTOHOTSPOT_SERVICE="autohotspot.service"
AUTOHOTSPOT_SERVICE_PATH="${SYSTEMD_PATH}/${AUTOHOTSPOT_SERVICE}"
AUTOHOTSPOT_TIMER="autohotspot.timer"
AUTOHOTSPOT_TIMER_PATH="${SYSTEMD_PATH}/${AUTOHOTSPOT_TIMER}"

_get_interface() {
# interfaces may vary
Expand Down
10 changes: 6 additions & 4 deletions installation/routines/setup_autohotspot_NetworkManager.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env bash

AUTOHOTSPOT_TIMER="autohotspot.timer"
AUTOHOTSPOT_TIMER_PATH="${SYSTEMD_PATH}/${AUTOHOTSPOT_TIMER}"
AUTOHOTSPOT_NETWORKMANAGER_RESOURCES_PATH="${INSTALLATION_PATH}/resources/autohotspot/NetworkManager"
AUTOHOTSPOT_NETWORKMANAGER_CONNECTIONS_PATH="/etc/NetworkManager/system-connections"

_install_packages_NetworkManager() {
sudo apt-get -y install iw
Expand All @@ -18,6 +17,7 @@ _install_autohotspot_NetworkManager() {
local ip_without_last_segment=$(_get_last_ip_segment $AUTOHOTSPOT_IP)
sudo cp "${AUTOHOTSPOT_NETWORKMANAGER_RESOURCES_PATH}"/autohotspot "${AUTOHOTSPOT_TARGET_PATH}"
sudo sed -i "s|%%WIFI_INTERFACE%%|${WIFI_INTERFACE}|g" "${AUTOHOTSPOT_TARGET_PATH}"
sudo sed -i "s|%%AUTOHOTSPOT_PROFILE%%|${AUTOHOTSPOT_PROFILE}|g" "${AUTOHOTSPOT_TARGET_PATH}"
sudo sed -i "s|%%AUTOHOTSPOT_SSID%%|${AUTOHOTSPOT_SSID}|g" "${AUTOHOTSPOT_TARGET_PATH}"
sudo sed -i "s|%%AUTOHOTSPOT_PASSWORD%%|${AUTOHOTSPOT_PASSWORD}|g" "${AUTOHOTSPOT_TARGET_PATH}"
sudo sed -i "s|%%AUTOHOTSPOT_IP%%|${AUTOHOTSPOT_IP}|g" "${AUTOHOTSPOT_TARGET_PATH}"
Expand Down Expand Up @@ -45,10 +45,10 @@ _uninstall_autohotspot_NetworkManager() {

# stop services and clear services
if systemctl list-unit-files "${AUTOHOTSPOT_SERVICE}" >/dev/null 2>&1 ; then
sudo systemctl stop "${AUTOHOTSPOT_SERVICE}"
sudo systemctl stop "${AUTOHOTSPOT_TIMER}"
sudo systemctl disable "${AUTOHOTSPOT_SERVICE}"
sudo systemctl disable "${AUTOHOTSPOT_TIMER}"
sudo systemctl stop "${AUTOHOTSPOT_SERVICE}"
sudo systemctl disable "${AUTOHOTSPOT_SERVICE}"
sudo rm "${AUTOHOTSPOT_SERVICE_PATH}"
sudo rm "${AUTOHOTSPOT_TIMER_PATH}"
fi
Expand All @@ -57,6 +57,8 @@ _uninstall_autohotspot_NetworkManager() {
sudo rm "${AUTOHOTSPOT_TARGET_PATH}"
fi

sudo rm -f "${AUTOHOTSPOT_NETWORKMANAGER_CONNECTIONS_PATH}/${AUTOHOTSPOT_PROFILE}"

# remove config files
_config_file_revert "${AUTOHOTSPOT_INTERFACES_CONF_FILE}"
}
Expand Down
34 changes: 18 additions & 16 deletions installation/routines/setup_autohotspot_dhcpcd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,11 @@ _install_autohotspot_dhcpcd() {
sudo cp "${AUTOHOTSPOT_DHCPCD_RESOURCES_PATH}"/autohotspot.service "${AUTOHOTSPOT_SERVICE_PATH}"
sudo sed -i "s|%%AUTOHOTSPOT_SCRIPT%%|${AUTOHOTSPOT_TARGET_PATH}|g" "${AUTOHOTSPOT_SERVICE_PATH}"

sudo systemctl enable "${AUTOHOTSPOT_SERVICE}"
sudo cp "${AUTOHOTSPOT_DHCPCD_RESOURCES_PATH}"/autohotspot.timer "${AUTOHOTSPOT_TIMER_PATH}"
sudo sed -i "s|%%AUTOHOTSPOT_SERVICE%%|${AUTOHOTSPOT_SERVICE}|g" "${AUTOHOTSPOT_TIMER_PATH}"

# create crontab entry
local crontab_user=$(crontab -l 2>/dev/null)
if [[ -z "${crontab_user}" || ! $(echo "${crontab_user}" | grep -w "${AUTOHOTSPOT_TARGET_PATH}") ]]; then
(echo "${crontab_user}"; echo "*/5 * * * * sudo ${AUTOHOTSPOT_TARGET_PATH} 2>&1 | logger -t autohotspot") | crontab -
(echo "${crontab_user}"; echo "@reboot sudo ${AUTOHOTSPOT_TARGET_PATH} 2>&1 | logger -t autohotspot") | crontab -
fi
sudo systemctl enable "${AUTOHOTSPOT_SERVICE}"
sudo systemctl enable "${AUTOHOTSPOT_TIMER}"
}


Expand All @@ -95,16 +92,14 @@ _uninstall_autohotspot_dhcpcd() {
if systemctl list-unit-files "${AUTOHOTSPOT_SERVICE}" >/dev/null 2>&1 ; then
sudo systemctl stop hostapd
sudo systemctl stop dnsmasq
sudo systemctl stop "${AUTOHOTSPOT_TIMER}"
sudo systemctl disable "${AUTOHOTSPOT_TIMER}"
sudo systemctl stop "${AUTOHOTSPOT_SERVICE}"
sudo systemctl disable "${AUTOHOTSPOT_SERVICE}"
sudo rm "${AUTOHOTSPOT_SERVICE_PATH}"
sudo rm "${AUTOHOTSPOT_TIMER_PATH}"
fi

# remove crontab entry and script
local crontab_user=$(crontab -l 2>/dev/null)
if [[ ! -z "${crontab_user}" && $(echo "${crontab_user}" | grep -w "${AUTOHOTSPOT_TARGET_PATH}") ]]; then
echo "${crontab_user}" | sed "s|^.*\s${AUTOHOTSPOT_TARGET_PATH}\s.*$||g" | crontab -
fi

if [ -f "${AUTOHOTSPOT_TARGET_PATH}" ]; then
sudo rm "${AUTOHOTSPOT_TARGET_PATH}"
Expand All @@ -116,6 +111,15 @@ _uninstall_autohotspot_dhcpcd() {
_config_file_revert "${AUTOHOTSPOT_HOSTAPD_DAEMON_CONF_FILE}"
_config_file_revert "${AUTOHOTSPOT_DHCPCD_CONF_FILE}"
_config_file_revert "${AUTOHOTSPOT_INTERFACES_CONF_FILE}"


# remove artefacts from previous versions

# remove crontab entries
local crontab_user=$(crontab -l 2>/dev/null)
if [[ ! -z "${crontab_user}" && $(echo "${crontab_user}" | grep -w "${AUTOHOTSPOT_TARGET_PATH}") ]]; then
echo "${crontab_user}" | sed "s|^.*\s${AUTOHOTSPOT_TARGET_PATH}\s.*$||g" | crontab -
fi
}


Expand Down Expand Up @@ -154,10 +158,8 @@ _autohotspot_check_dhcpcd() {
verify_files_exists "${AUTOHOTSPOT_SERVICE_PATH}"
verify_file_contains_string "ExecStart=${AUTOHOTSPOT_TARGET_PATH}" "${AUTOHOTSPOT_SERVICE_PATH}"

local crontab_user=$(crontab -l 2>/dev/null)
if [[ ! $(echo "${crontab_user}" | grep -w "${AUTOHOTSPOT_TARGET_PATH}") ]]; then
exit_on_error "ERROR: crontab for user not installed"
fi
verify_files_exists "${AUTOHOTSPOT_TIMER_PATH}"
verify_file_contains_string "Unit=${AUTOHOTSPOT_SERVICE}" "${AUTOHOTSPOT_TIMER_PATH}"
}

_run_setup_autohotspot_dhcpcd() {
Expand Down
2 changes: 1 addition & 1 deletion resources/autohotspot/NetworkManager/autohotspot
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
wdev0='%%WIFI_INTERFACE%%' #wifi device that AP will work on

#AP setup
ap_profile_name='Phoniebox_Access_Point'
ap_profile_name='%%AUTOHOTSPOT_PROFILE%%'
ap_ssid='%%AUTOHOTSPOT_SSID%%'
ap_pw='%%AUTOHOTSPOT_PASSWORD%%'
ap_ip='%%AUTOHOTSPOT_IP%%/24'
Expand Down
6 changes: 3 additions & 3 deletions resources/autohotspot/dhcpcd/autohotspot.service
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[Unit]
Description=Automatically generates an wifi hotspot when a valid ssid is not in range
Description=Automatically generates an wifi hotspot when a valid SSID is not in range
After=multi-user.target
Requires=network-online.target

[Service]
Type=oneshot
RemainAfterExit=yes
Type=simple
ExecStart=%%AUTOHOTSPOT_SCRIPT%%

[Install]
Expand Down
10 changes: 10 additions & 0 deletions resources/autohotspot/dhcpcd/autohotspot.timer
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Unit]
Description=Timer to run the %%AUTOHOTSPOT_SERVICE%% every 2 mins

[Timer]
OnBootSec=0min
OnCalendar=*:0/2
Unit=%%AUTOHOTSPOT_SERVICE%%

[Install]
WantedBy=timers.target
44 changes: 14 additions & 30 deletions src/jukebox/components/hostif/linux/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def get_autohotspot_status():
if os.path.isfile("/etc/systemd/system/autohotspot.service"):
status = 'inactive'

ret = subprocess.run(['systemctl', 'is-active', 'autohotspot'],
ret = subprocess.run(['systemctl', 'is-active', 'autohotspot.timer'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=False,
stdin=subprocess.DEVNULL)
# 0 = active, 3 = inactive
Expand All @@ -260,22 +260,15 @@ def get_autohotspot_status():
def stop_autohotspot():
"""Stop auto hotspot functionality
Basically disabling the cronjob and running the script one last time manually
Stopping and disabling the timer and running the service one last time manually
"""
if os.path.isfile("/etc/systemd/system/autohotspot.service"):
cron_job = "/etc/cron.d/autohotspot"
subprocess.run(["sudo", "sed", "-i", r"s/^\*.*/#&/", cron_job],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=False)
subprocess.run(['sudo', '/usr/bin/systemctl', 'stop', 'autohotspot'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=False)
subprocess.run(['sudo', '/usr/bin/systemctl', 'disable', 'autohotspot'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=False)
ret = subprocess.run(['sudo', 'autohotspot'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
check=False)
if ret.returncode != 0:
msg = f"Error 'stop_autohotspot': {ret.stdout} (Code: {ret.returncode})"
logger.error(msg)
return {'error': {'code': -1, 'message': msg}}
subprocess.run(['sudo', '/usr/bin/systemctl', 'stop', 'autohotspot.timer'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=False)
subprocess.run(['sudo', '/usr/bin/systemctl', 'disable', 'autohotspot.timer'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=False)
subprocess.run(['sudo', '/usr/bin/systemctl', 'start', 'autohotspot.service'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=False)

return 'inactive'
else:
Expand All @@ -285,24 +278,15 @@ def stop_autohotspot():

@plugin.register()
def start_autohotspot():
"""start auto hotspot functionality
"""Start auto hotspot functionality
Basically enabling the cronjob and running the script one time manually
Enabling and starting the timer (timer will start the service)
"""
if os.path.isfile("/etc/systemd/system/autohotspot.service"):
cron_job = "/etc/cron.d/autohotspot"
subprocess.run(["sudo", "sed", "-i", "-r", r"s/(^#)(\*[0-9]*)/\*/", cron_job],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=False)
subprocess.run(['sudo', '/usr/bin/systemctl', 'start', 'autohotspot'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=False)
subprocess.run(['sudo', '/usr/bin/systemctl', 'enable', 'autohotspot'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=False)
ret = subprocess.run(['sudo', 'autohotspot'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
check=False)
if ret.returncode != 0:
msg = f"Error 'start_autohotspot': {ret.stdout} (Code: {ret.returncode})"
logger.error(msg)
return {'error': {'code': -1, 'message': msg}}
subprocess.run(['sudo', '/usr/bin/systemctl', 'enable', 'autohotspot.timer'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=False)
subprocess.run(['sudo', '/usr/bin/systemctl', 'start', 'autohotspot.timer'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=False)

return 'active'
else:
Expand Down

0 comments on commit 773a3a9

Please sign in to comment.