Skip to content

Commit 82be12e

Browse files
committed
fix: add cron library
1 parent aeaeae2 commit 82be12e

File tree

5 files changed

+45
-28
lines changed

5 files changed

+45
-28
lines changed

packages/ns-api/files/ns.ha

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -137,23 +137,13 @@ def setup(role, lan_interface, ha_interface, virtual_ip, ha_main_ipaddress, ha_s
137137
u.set('keepalived', 'backup', 'track_interface', [f'{lan_interface}_ha'])
138138
u.set('keepalived', 'backup', 'track_script', ['receiver'])
139139

140-
# Fix OpenVPN permissions
141-
group_file = '/etc/group'
142-
rsync_dir = '/usr/share/keepalived/rsync/etc/'
143-
144-
# Update the group file, if needed
145-
with open(group_file, 'r') as file:
146-
group_data = file.read()
147-
148-
if 'root:x:0:keepalived' not in group_data:
149-
group_data = group_data.replace('root:x:0:', 'root:x:0:keepalived')
150-
151-
with open(group_file, 'w') as file:
152-
file.write(group_data)
140+
# Append publick key to root dropbear authorized_keys
141+
with open('/etc/dropbear/authorized_keys', 'a') as file:
142+
file.write(pubkey + '\n')
153143

154144
# Create the rsync directory if it doesn't exist
145+
rsync_dir = '/usr/share/keepalived/rsync/etc/'
155146
os.makedirs(rsync_dir, exist_ok=True)
156-
157147
# Change permissions of the rsync directory
158148
os.chmod(rsync_dir, 0o2775)
159149

packages/ns-ha/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ endef
3737
define Package/ns-ha/install
3838
$(INSTALL_DIR) $(1)/usr/sbin
3939
$(INSTALL_DIR) $(1)/etc/hotplug.d/keepalived
40+
$(INSTALL_DIR) $(1)/lib/functions/keepalived
4041
$(INSTALL_BIN) ./files/keepalived-config $(1)/usr/sbin
4142
$(INSTALL_DATA) ./files/600-openvpn $(1)/etc/hotplug.d/keepalived
4243
$(INSTALL_DATA) ./files/900-ns-plug $(1)/etc/hotplug.d/keepalived
44+
$(INSTALL_DATA) ./files/ns.sh $(1)/lib/functions/keepalived
4345
endef
4446

4547
$(eval $(call BuildPackage,ns-ha))

packages/ns-ha/files/600-openvpn

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@ set_stop_if_backup
1111
add_sync_file /etc/openvpn
1212
add_sync_file /etc/config/openvpn
1313

14+
if [ "$ACTION" == "NOTIFY_SYNC" ]; then
15+
home=$(get_rsync_user_home)
16+
rsync -avr $home/etc/openvpn/ /etc/openvpn/
17+
fi
18+
1419
keepalived_hotplug

packages/ns-ha/files/900-ns-plug

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,17 @@
22

33
# shellcheck source=/dev/null
44
. /lib/functions/keepalived/hotplug.sh
5-
6-
disable_cron() {
7-
crontab -l | sed '/send-heartbeat/s/^/#/' | crontab -
8-
crontab -l | sed '/send-backup/s/^/#/' | crontab -
9-
crontab -l | sed '/send-inventory/s/^/#/' | crontab -
10-
}
11-
12-
enable_command() {
13-
crontab -l | sed '/send-heartbeat/s/^#//' | crontab -
14-
crontab -l | sed '/send-backup/s/^#//' | crontab -
15-
crontab -l | sed '/send-inventory/s/^#//' | crontab -
16-
}
5+
. /lib/functions/keepalived/ns.sh
176

187
set_service_name ns-plug
198

209
set_restart_if_master
2110
set_stop_if_backup
2211

2312
if [ "$ACTION" == "NOTIFY_BACKUP" ]; then
24-
disable_cron
13+
update_cron "disable" "send-heartbeat send-backup send-inventory"
2514
elif [ "$ACTION" == "NOTIFY_MASTER" ]; then
26-
enable_cron
15+
update_cron "enable" "send-heartbeat send-backup send-inventory"
2716
fi
2817

2918
add_sync_file /etc/config/ns-plug

packages/ns-ha/files/ns.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/sh
2+
3+
# Function to update crontab entries based on action (disable or enable)
4+
update_cron() {
5+
local action="$1"
6+
local commands="$2"
7+
local sed_command
8+
9+
# Determine the appropriate sed command based on the action
10+
if [ "$action" = "disable" ]; then
11+
sed_command='s/^\([^#]\)/#\1/'
12+
elif [ "$action" = "enable" ]; then
13+
sed_command='s/^#//'
14+
else
15+
echo "Invalid action: $action"
16+
return 1
17+
fi
18+
19+
# Create a temporary file to store modified crontab
20+
temp_crontab=$(mktemp)
21+
crontab -l > "$temp_crontab"
22+
23+
# Loop through each command and apply the sed transformation
24+
for cmd in $commands; do
25+
sed -i "/$cmd/$sed_command" "$temp_crontab"
26+
done
27+
28+
# Update the crontab with the modified content
29+
crontab "$temp_crontab"
30+
rm "$temp_crontab"
31+
}

0 commit comments

Comments
 (0)