Skip to content

Commit

Permalink
first attemp to make installation working
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Nov 2, 2017
1 parent d0cfa16 commit b7b486e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 155 deletions.
20 changes: 5 additions & 15 deletions src/clients/ntp-client_finish.rb
Expand Up @@ -42,27 +42,17 @@ def main

# User config from installation time:
# fortunately so far we only have the server address(es)
@ss = NtpClient.GetUsedNtpServers
pools = NtpClient.ntp_conf.pools

# ntp.conf from the RPM
NtpClient.config_has_been_read = false
NtpClient.ProcessNtpConf
Builtins.y2milestone(
"ntp-client_finish NtpClient::Read::ntp_records %1",
NtpClient.ntp_records
)

# put users server(s) back
Builtins.foreach(@ss) do |server|
# if not present, it adds it,
# otherwise it preserves what is there. uff what an API
idx = NtpClient.findSyncRecord("server", server) # -1: new
NtpClient.selectSyncRecord(idx)
Ops.set(NtpClient.selected_record, "address", server)
Ops.set(NtpClient.selected_record, "type", "server")
# bnc#450418, add a recommended option
NtpClient.enableOptionInSyncRecord("iburst")
NtpClient.storeSyncRecord
NtpClient.ntp_conf.clear_pools

pools.each_pair do |server, options|
NtpClient.ntp_conf.add_pool(server, options)
end

NtpClient.write_only = true
Expand Down
9 changes: 6 additions & 3 deletions src/clients/ntp-client_proposal.rb
Expand Up @@ -304,7 +304,8 @@ def AskUser
ntp_server2 = Convert.to_string(
UI.QueryWidget(Id(:ntp_address), :Value)
)
AddSingleServer(ntp_server2)
NtpClient.ntp_conf.clear_pools
NtpClient.ntp_conf.add_pool(ntp_server2)
retval = Convert.to_boolean(WFM.CallFunction("ntp-client"))
ret = :next if retval
MakeProposal()
Expand All @@ -316,9 +317,11 @@ def WriteNtpSettings(ntp_servers, ntp_server, run_service)
ntp_servers = deep_copy(ntp_servers)
NtpClient.modified = true
if ntp_servers != []
Builtins.foreach(ntp_servers) { |server| AddSingleServer(server) }
ntp_servers.each do |server|
NtpClient.ntp_conf.add_pool(server)
end
else
AddSingleServer(ntp_server)
NtpClient.ntp_conf.add_pool(ntp_server)
end
NtpClient.run_service = run_service
if !run_service
Expand Down
28 changes: 27 additions & 1 deletion src/lib/cfa/chrony_conf.rb
Expand Up @@ -22,7 +22,9 @@ def load
end

# options hash with key value, where value can be nil for keyword options
def add_pool(address, options)
# or :default to use default options
def add_pool(address, options = :default)
options = default_pool_options if options == :default
# if there is already pool entry, place it after, if not, try use comment
existing_pools = pure_pools
if existing_pools.empty?
Expand All @@ -45,11 +47,35 @@ def add_pool(address, options)
data.add(key, value, placer)
end

def default_pool_options
{ "iburst" => nil }
end

# delete all pools defined
def clear_pools
data.delete(POOLS_MATCHER)
end

# returns copy of available pools
# TODO allow modify of specific pool
# hash with key server and value is options hash
def pools
pools_data = pure_pools.map { |p| p[:value] }
pools_map = pools_data.map do |entry|
case entry
when String
[entry, {}]
when AugeasTreeValue
options = Hash[entry.tree.data.map { |e| [e[:key], e[:value]] }]
[entry.value, options]
else
raise "invalid pool data #{entry.inspect}"
end
end

Hash[pools_map]
end

private

COLLECTION_KEYS = [
Expand Down
142 changes: 6 additions & 136 deletions src/modules/NtpClient.rb
Expand Up @@ -11,7 +11,7 @@
# Input and output routines.
require "yast"
require "yaml"
require "cfa/ntp_conf"
require "cfa/chrony_conf"
require "yast2/target_file" # required to cfa work on changed scr

module Yast
Expand Down Expand Up @@ -143,6 +143,11 @@ def main
@deleted_records = []
end

# CFA instance for reading/writing /etc/ntp.conf
def ntp_conf
@chrony_conf ||= CFA::ChronyConf.new
end

def add_to_deleted_records(records)
records.each do |record|
cfa = record["cfa_record"]
Expand Down Expand Up @@ -273,23 +278,9 @@ def read_ntp_conf
end

load_ntp_records

log.info("Raw ntp conf #{ntp_conf.raw}")
true
end

def load_ntp_records
@ntp_records = ntp_conf.records.map do |record|
{
"type" => record.type,
"address" => record.value,
"options" => record.raw_options,
"comment" => record.comment.to_s,
"cfa_record" => record
}
end
end

# Read and parse /etc/ntp.conf
# @return true on success
def ProcessNtpConf
Expand All @@ -302,102 +293,6 @@ def ProcessNtpConf

@config_has_been_read = true

fudge_records = Builtins.filter(@ntp_records) do |m|
Ops.get_string(m, "type", "") == "fudge"
end

fudge_map = Convert.convert(
Builtins.listmap(fudge_records) do |m|
key = Ops.get_string(m, "address", "")
{ key => m }
end,
from: "map <string, map>",
to: "map <string, map <string, any>>"
)

restrict_records = Builtins.filter(@ntp_records) do |m|
Ops.get_string(m, "type", "") == "restrict"
end

@restrict_map = Convert.convert(
Builtins.listmap(restrict_records) do |m|
key = Ops.get_string(m, "address", "")
value2 = {}
opts = Builtins.splitstring(
String.CutBlanks(Ops.get_string(m, "options", "")),
" \t"
)
if Ops.get(opts, 0, "") == "mask"
Ops.set(value2, "mask", Ops.get(opts, 1, ""))
Ops.set(opts, 0, "")
Ops.set(opts, 1, "")
else
Ops.set(value2, "mask", "")
end
Ops.set(
value2,
"options",
String.CutBlanks(Builtins.mergestring(opts, " "))
)
Ops.set(value2, "comment", Ops.get_string(m, "comment", ""))
value2["cfa_record"] = m["cfa_record"]
{ key => value2 }
end,
from: "map <string, map>",
to: "map <string, map <string, any>>"
)

@ntp_records = Builtins.filter(@ntp_records) do |m|
Ops.get_string(m, "type", "") != "fudge"
end

@ntp_records = Builtins.filter(@ntp_records) do |m|
Ops.get_string(m, "type", "") != "restrict"
end

@ntp_records = Convert.convert(
Builtins.maplist(@ntp_records) do |m|
if Builtins.haskey(fudge_map, Ops.get_string(m, "address", ""))
Ops.set(
m,
"fudge_options",
Ops.get_string(
fudge_map,
[Ops.get_string(m, "address", ""), "options"],
""
)
)
Ops.set(
m,
"fudge_comment",
Ops.get_string(
fudge_map,
[Ops.get_string(m, "address", ""), "comment"],
""
)
)
m["cfa_fudge_record"] = ntp_conf.records.find do |record|
record.type == "fudge" && record.value == m["address"]
end
end
m
end,
from: "list <map>",
to: "list <map <string, any>>"
)

# mark local clock to be local clock and not real servers
@ntp_records = Builtins.maplist(@ntp_records) do |p|
if Ops.get_string(p, "type", "") == "server" &&
Builtins.regexpmatch(
Ops.get_string(p, "address", ""),
"^127.127.[0-9]+.[0-9]+$"
)
Ops.set(p, "type", "__clock")
end
p
end

true
end

Expand Down Expand Up @@ -579,12 +474,6 @@ def Write
# write settings
return false if !go_next

# Restrict map records are written first to not mangle the config file
# (bsc#983486)
@ntp_records = restrict_map_records + @ntp_records

log.info "Writing settings #{@ntp_records}"

Report.Error(Message.CannotWriteSettingsTo("/etc/ntp.conf")) if !write_ntp_conf

write_and_update_policy
Expand Down Expand Up @@ -1165,18 +1054,6 @@ def update_cfa_record(record)
# Write current /etc/ntp.conf with @ntp_records
# @return [Boolean] true on success
def write_ntp_conf
records_for_write.each do |record|
unless record["cfa_record"]
ntp_conf.records << CFA::NtpConf::Record.record_class(record["type"]).new
record["cfa_record"] = ntp_conf.records.last
end

update_cfa_record(record)
log.info "new record #{record.inspect}"
end

ntp_conf.records.delete_if { |record| @deleted_records.include?(record) }

begin
ntp_conf.save
rescue StandardError => e
Expand Down Expand Up @@ -1388,13 +1265,6 @@ def pool_servers_for(known_countries)
def cache_server(server)
@ntp_servers[server["address"].to_s] = server
end

# CFA instance for reading/writing /etc/ntp.conf
def ntp_conf
@ntp_conf ||= CFA::NtpConf.new
end


end

NtpClient = NtpClientClass.new
Expand Down

0 comments on commit b7b486e

Please sign in to comment.