Skip to content

Commit 5eb9c5c

Browse files
feat(get-facts): node creation_date
Extract the node creation date by looking at the birth date of some files, created at NS8 installation time. Using system's "stat" command because it provides better filesystem birth date compatibility than Python 3.11.
1 parent f479091 commit 5eb9c5c

File tree

1 file changed

+16
-1
lines changed
  • core/imageroot/var/lib/nethserver/node/actions/get-facts

1 file changed

+16
-1
lines changed

core/imageroot/var/lib/nethserver/node/actions/get-facts/50get

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,27 @@ import subprocess
1717
import agent.volumes
1818
import agent.facts
1919
import ansible_runner
20-
20+
from datetime import datetime, timezone
2121

2222
def _run(cmd):
2323
proc = subprocess.run(cmd, shell=True, check=True,
2424
capture_output=True, text=True)
2525
return proc.stdout.rstrip().lstrip()
2626

27+
def _node_creation_date():
28+
# Read birth dates of files created during NS8 installation.
29+
try:
30+
output = subprocess.check_output(["stat", r"--printf='%Y\n%W\n'",
31+
"/etc/nethserver/wg0.pub",
32+
"/var/lib/nethserver/node/state/agent.env"
33+
],
34+
text=True,
35+
)
36+
timestamps = [int(x) for x in filter(str.isnumeric, output.split())]
37+
creation_time = min(timestamps) # Consider the older value
38+
except subprocess.CalledProcessError:
39+
creation_time = 0
40+
return datetime.fromtimestamp(creation_time, tz=timezone.utc).isoformat()
2741

2842
def _get_core_version():
2943
with open('/etc/nethserver/core.env', encoding='utf-8') as file:
@@ -94,6 +108,7 @@ leader_id = rdb.hget("cluster/environment", "NODE_ID")
94108
json.dump(
95109
{
96110
os.environ['AGENT_ID'].removeprefix('node/'): {
111+
'creation_date': _node_creation_date(),
97112
'cluster_leader': leader_id == os.getenv("NODE_ID"),
98113
'fqdn': agent.facts.pseudo_domain(facts['ansible_fqdn']),
99114
'default_ipv4': agent.facts.pseudo_ip(facts.get('ansible_default_ipv4', {}).get('address', '')),

0 commit comments

Comments
 (0)