chrony/chrony.helper

77 lines
2.0 KiB
Plaintext
Raw Normal View History

2011-06-24 13:36:06 +00:00
#!/bin/bash
chronyc=/usr/bin/chronyc
dhclient_servers=/var/lib/dhclient/chrony.servers.*
dhclient_added_servers=/var/lib/dhclient/chrony.added_servers
2011-06-24 13:36:06 +00:00
service_name=chronyd.service
chrony_command() {
2013-06-21 14:51:21 +00:00
$chronyc -a -m "$1"
2011-06-24 13:36:06 +00:00
}
update_dhclient_added_servers() {
new_servers=$(echo "$1" | sort -u)
old_servers=$(cat $dhclient_added_servers 2> /dev/null)
[ "$old_servers" = "$new_servers" ] && return 0
[ -n "$new_servers" ] && echo "$new_servers" > $dhclient_added_servers ||
rm -f $dhclient_added_servers
}
2011-06-24 13:36:06 +00:00
add_dhclient_servers() {
shopt -s nullglob
servers_files=($dhclient_servers)
shopt -u nullglob
(( ${#servers_files[*]} )) || return 0
added_servers=$(
cat $dhclient_added_servers 2> /dev/null
cat ${servers_files[*]} |
while read server serverargs; do
chrony_command "add server $server $serverargs" &> /dev/null &&
echo "$server"
done)
update_dhclient_added_servers "$added_servers"
}
remove_dhclient_servers() {
[ -f $dhclient_added_servers ] || return 0
all_servers=$(
cat $dhclient_servers 2> /dev/null |
while read server serverargs; do
echo "$server"
done | sort -u)
echo "$all_servers" | comm -23 $dhclient_added_servers - |
while read server; do
chrony_command "delete $server" &> /dev/null
done
added_servers=$(echo "$all_servers" | comm -12 $dhclient_added_servers -)
update_dhclient_added_servers "$added_servers"
2011-06-24 13:36:06 +00:00
}
is_running() {
systemctl is-active -q $service_name
2011-06-24 13:36:06 +00:00
}
case "$1" in
add-dhclient-servers)
add_dhclient_servers
;;
remove-dhclient-servers)
remove_dhclient_servers
;;
is-running)
is_running
;;
2011-06-24 13:36:06 +00:00
command)
is_running && chrony_command "$2"
;;
forced-command)
chrony_command "$2"
;;
2011-06-24 13:36:06 +00:00
*)
2013-06-21 14:51:21 +00:00
echo $"Usage: $0 {add-dhclient-servers|remove-dhclient-servers|is-running|command|forced-command}"
2011-06-24 13:36:06 +00:00
exit 2
esac
exit $?