Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pillar/device-steps.sh: fix global NTPSERVER var initialization #4053

Merged
merged 4 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions pkg/pillar/cmd/zedagent/handlentp.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,14 @@ func dialUnixWithChronyd(address string) (*chronyConn, error) {
&net.UnixAddr{Name: address, Net: "unixgram"},
)
if err != nil {
// Even there was an error, net.DialUnix() leaves trash behind.
// What a shame.
os.Remove(local)
return nil, err
}
if err := os.Chmod(local, 0600); err != nil {
conn.Close()
os.Remove(local)
return nil, err
}
return &chronyConn{Conn: conn, local: local}, nil
Expand All @@ -261,9 +265,6 @@ func dialUnixWithChronyd(address string) (*chronyConn, error) {
func getNTPSourcesInfo(ctx *zedagentContext) *info.ZInfoNTPSources {
conn, err := dialUnixWithChronyd(unixChronydPath)
if err != nil {
// Even there was an error, net.DialUnix() leaves trash behind.
// What a shame.
os.Remove(conn.local)
log.Errorf("getNTPSourcesInfo: can't connect to chronyd: %v", err)
return nil
}
Expand Down
27 changes: 19 additions & 8 deletions pkg/pillar/scripts/device-steps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ DEFAULT_NTPMODE=pool
NTPSERVERS=

# Return one line with all the NTP servers for all the ports
get_ntp_servers() {
get_ntp_servers_from_nim() {
if [ ! -f "$INPUTFILE" ]; then
return
fi
Expand All @@ -91,7 +91,8 @@ get_ntp_servers() {
.NtpServers | .[]' $INPUTFILE)

# Concat all in one string
ntp_all="$ntp_static $ntp_dynamic"
# shellcheck disable=SC3037
ntp_all=$(echo -e "$ntp_static\n$ntp_dynamic" | sort | uniq)

# Make the following: "$mode $ntp" and separate
# each with \n for further processing in a caller
Expand All @@ -101,6 +102,13 @@ get_ntp_servers() {
done
ntp_all="$list"

# shellcheck disable=SC3037
echo -n "$ntp_all"
}

get_ntp_servers() {
ntp_all=$(get_ntp_servers_from_nim)

# Fallback to default if nothing is configured
if [ -z "$ntp_all" ]; then
ntp_all="$DEFAULT_NTPMODE\n$DEFAULT_NTPSERVER\n"
Expand Down Expand Up @@ -139,19 +147,21 @@ wait_ntp_sync() {
# Populate NTP sources from the string argument, which represents
# \n separated list of servers
populate_ntp_sources() {
servers="$1"
ns="$1"
echo "$(date -Ins -u) Populate NTP with new sources"
# Creates string of servers in "add $mode $server iburst" format
args=$(format_chrony_args "$servers" "add" "iburst")
args=$(format_chrony_args "$ns" "add" "iburst")
echo "$(date -Ins -u) chronyc -m $args"
echo "$args" | xargs /usr/bin/chronyc -m
ret_code=$?
echo "$(date -Ins -u) chronyc: $ret_code"
# Init global variable
NTPSERVERS="$ns"
}

# Start NTP daemon
start_ntp_daemon() {
NTPSERVERS=$(get_ntp_servers)
ns=$(get_ntp_servers)
echo "$(date -Ins -u) Check for NTP config"
if [ -f /usr/sbin/chronyd ]; then
# Run chronyd to keep it in sync.
Expand All @@ -167,7 +177,7 @@ start_ntp_daemon() {
if [ "$ret_code" = "0" ]; then
# Give some time for chronyd to start
sleep 1
populate_ntp_sources "$NTPSERVERS"
populate_ntp_sources "$ns"
fi
else
echo "$(date -Ins -u) ERROR: no NTP (chrony) on EVE"
Expand All @@ -176,6 +186,7 @@ start_ntp_daemon() {

# Reload NTP sources
reload_ntp_sources() {
ns="$1"
echo "$(date -Ins -u) Remove all NTP sources from chronyd"
while true; do
# Get all sources, skip 2 lines header, take first line
Expand All @@ -192,7 +203,7 @@ reload_ntp_sources() {
break
fi
done
populate_ntp_sources "$NTPSERVERS"
populate_ntp_sources "$ns"
}

# If zedbox is already running we don't have to start it.
Expand Down Expand Up @@ -491,7 +502,7 @@ while true; do
if [ -n "$ns" ] && [ "$ns" != "$NTPSERVERS" ]; then
echo "$(date -Ins -u) NTP server changed from \"$NTPSERVERS\" to \"$ns\", reload NTP sources"
# Reload NTP sources
reload_ntp_sources
reload_ntp_sources "$ns"
fi
sleep 300
done
Loading