Skip to content

Commit

Permalink
Update with first proper run of scripts/ntpServerConverter.py
Browse files Browse the repository at this point in the history
Signed-off-by: Jauder Ho <[email protected]>
  • Loading branch information
jauderho committed Jul 28, 2024
1 parent 24fb594 commit 9f2b568
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 45 deletions.
34 changes: 15 additions & 19 deletions chrony.conf
Original file line number Diff line number Diff line change
@@ -1,46 +1,43 @@
#
# NTS servers in chrony format
#
# All
server time.cloudflare.com nts iburst

# Cloudflare (Anycast)
pool time.cloudflare.com nts iburst maxsources 2

# NTP.br (Brazil)
# Brazil
server a.st1.ntp.br nts iburst
server b.st1.ntp.br nts iburst
server c.st1.ntp.br nts iburst
server d.st1.ntp.br nts iburst
server gps.ntp.br nts iburst

# Brazil
server brazil.time.system76.com nts iburst
server time.bolha.one nts iburst

# Finland
server ntp.miuku.net nts iburst

# Friedrich-Alexander-Universität / FAU (Germany)
# France
server paris.time.system76.com nts iburst

# Germany
server ntp3.fau.de nts iburst
server ntp3.ipv6.fau.de nts iburst

# Physikalisch-Technische Bundesanstalt / PTB (Germany)
server ptbtime1.ptb.de nts iburst
server ptbtime2.ptb.de nts iburst
server ptbtime3.ptb.de nts iburst
server ptbtime4.ptb.de nts iburst

# Germany
server www.jabber-germany.de nts iburst
server www.masters-of-cloud.de nts iburst
server ntp.nanosrvr.cloud nts iburst

# TimeNL (Netherlands)
# Netherlands
server ntppool1.time.nl nts iburst
server ntppool2.time.nl nts iburst

# Singapore
server ntpmon.dcs1.biz nts iburst

# Netnod (Sweden)
# Sweden
server nts.netnod.se nts iburst
server gbg1.nts.netnod.se nts iburst
server gbg2.nts.netnod.se nts iburst
Expand All @@ -59,15 +56,14 @@ server ntp.trifence.ch nts iburst
server ntp.zeitgitter.net nts iburst
server time.signorini.ch nts iburst

# System76 (US / France / Brazil)
server virginia.time.system76.com nts iburst
# US
server ohio.time.system76.com nts iburst
server oregon.time.system76.com nts iburst
server paris.time.system76.com nts iburst
server brazil.time.system76.com nts iburst

# US
server virginia.time.system76.com nts iburst
server stratum1.time.cifelli.xyz nts iburst
server time.cifelli.xyz nts iburst
server time.txryan.com nts iburst

# Known VM servers (may be less accurate)
server ntp.viarouge.net nts iburst
server time.xargs.org nts iburst
5 changes: 3 additions & 2 deletions nts-sources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,12 @@ servers:
- hostname: "[ntp.viarouge.net](http://ntp.viarouge.net)"
stratum: 2
location: France
owner: Vincent Viarouge
owner: Hubert Viarouge
vm: true

- hostname: time.xargs.org
stratum: 2
location: US
owner: Unknown
owner: Michael Driscoll
notes: IPv4 and IPv6
vm: true
84 changes: 60 additions & 24 deletions scripts/ntpServerConverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
import yaml
import argparse
import re
import os

def load_yaml(file_path):
with open(file_path, 'r') as file:
return yaml.safe_load(file)

def extract_hostname(hostname_field):
# Extract hostname from potential markdown link
match = re.search(r'\[([^\]]+)\]', hostname_field)
if match:
return match.group(1)
return hostname_field

def generate_markdown(data):
markdown = "|Hostname|Stratum|Location|Owner|Notes|\n|---|:---:|---|---|---|\n"
markdown = "## The List\n"
markdown += "|Hostname|Stratum|Location|Owner|Notes|\n|---|:---:|---|---|---|\n"
current_location = None
vm_servers = []
for server in data['servers']:
Expand All @@ -25,23 +26,22 @@ def generate_markdown(data):
continue
if server['location'] != current_location:
if current_location is not None:
markdown += "|||\n"
markdown += "||\n"
current_location = server['location']

hostname = extract_hostname(server['hostname'])
hostname = server['hostname']
stratum = server['stratum']
location = server['location']
owner = server['owner']
notes = server.get('notes', '')

markdown += f"|{hostname}|{stratum}|{location}|{owner}|{notes}|\n"

# Add VM servers in a separate table
if vm_servers:
markdown += "\nThe following servers are known to be virtualized and may be less accurate. YMMV.\n"
markdown += "\nThe following servers are known to be virtualized and may be less accurate. YMMV.\n\n"
markdown += "|Hostname|Stratum|Location|Owner|Notes|\n|---|:---:|---|---|---|\n"
for server in vm_servers:
hostname = extract_hostname(server['hostname'])
hostname = server['hostname']
stratum = server['stratum']
location = server['location']
owner = server['owner']
Expand All @@ -52,10 +52,9 @@ def generate_markdown(data):
return markdown

def generate_chrony_conf(data):
chrony_conf = "#\n# NTS servers in chrony format\n#\n\n"
chrony_conf = "#\n# NTS servers in chrony format\n#\n"
current_location = None
vm_servers = []
# Process non-VM servers
for server in data['servers']:
if server['vm']:
vm_servers.append(server)
Expand All @@ -67,36 +66,73 @@ def generate_chrony_conf(data):
current_location = server['location']

hostname = extract_hostname(server['hostname'])
chrony_conf += f"server {hostname} iburst nts\n"
chrony_conf += f"server {hostname} nts iburst\n"

# Add VM servers at the end
if vm_servers:
chrony_conf += "\n# Virtual Machine Servers (may be less accurate)\n"
chrony_conf += "\n# Known VM servers (may be less accurate)\n"
for server in vm_servers:
hostname = extract_hostname(server['hostname'])
chrony_conf += f"server {hostname} iburst nts\n"
chrony_conf += f"server {hostname} nts iburst\n"

return chrony_conf

def update_readme(readme_path, new_content):
with open(readme_path, 'r') as file:
content = file.read()

start = content.index("## The List")
end = content.index("## Star History", start)

# Keep the original content before "## The List" and after "## Star History"
updated_content = content[:start] + new_content + "\n" + content[end:]

with open(readme_path, 'w') as file:
file.write(updated_content)

def main():
parser = argparse.ArgumentParser(description="Convert NTP server data to Markdown or chrony.conf format")
parser.add_argument("input_file", help="Path to the input YAML file")
parser.add_argument("output_format", choices=["markdown", "chrony"], help="Output format (markdown or chrony)")
parser.add_argument("output_file", help="Path to the output file")
parser.add_argument("output_format", nargs='?', choices=["markdown", "chrony"], help="Output format (markdown or chrony)")
parser.add_argument("output_file", nargs='?', help="Path to the output file")

args = parser.parse_args()

data = load_yaml(args.input_file)

if args.output_format == "markdown":
output = generate_markdown(data)
else:
output = generate_chrony_conf(data)

with open(args.output_file, 'w') as file:
file.write(output)

print(f"Output written to {args.output_file}")
if args.output_format is None:
# Behavior 1: Update README.md and write new chrony.conf if they exist
readme_path = "README.md"
chrony_path = "chrony.conf"

if os.path.exists(readme_path):
markdown_content = generate_markdown(data)
update_readme(readme_path, markdown_content)
print(f"Updated {readme_path}")

chrony_content = generate_chrony_conf(data)
with open(chrony_path, 'w') as file:
file.write(chrony_content)
print(f"Written {chrony_path}")

elif args.output_format == "markdown":
# Behavior 2: Write markdown to specified file
if args.output_file is None:
parser.error("output_file is required when output_format is specified")

markdown_content = generate_markdown(data)
with open(args.output_file, 'w') as file:
file.write(markdown_content)
print(f"Markdown written to {args.output_file}")

elif args.output_format == "chrony":
# Behavior 3: Write chrony format to specified file
if args.output_file is None:
parser.error("output_file is required when output_format is specified")

chrony_content = generate_chrony_conf(data)
with open(args.output_file, 'w') as file:
file.write(chrony_content)
print(f"Chrony configuration written to {args.output_file}")

if __name__ == "__main__":
main()
Expand Down

0 comments on commit 9f2b568

Please sign in to comment.