CI: fix hostname self-heal logic
This commit is contained in:
parent
5bd54fc595
commit
c3aa8a7979
1 changed files with 14 additions and 27 deletions
|
|
@ -87,33 +87,20 @@ jobs:
|
|||
exit 1
|
||||
fi
|
||||
|
||||
HOSTNAME_SHORT=\"$(hostname)\"
|
||||
if ! getent hosts \"$HOSTNAME_SHORT\" >/dev/null 2>&1; then
|
||||
sudo -n /usr/bin/python3 - <<'PY'
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
hostname = os.popen("hostname").read().strip()
|
||||
hosts_path = Path("/etc/hosts")
|
||||
|
||||
text = hosts_path.read_text(encoding="utf-8") if hosts_path.exists() else ""
|
||||
lines = text.splitlines()
|
||||
|
||||
def has_hostname() -> bool:
|
||||
for line in lines:
|
||||
s = line.strip()
|
||||
if not s or s.startswith("#"):
|
||||
continue
|
||||
if hostname in s.split():
|
||||
return True
|
||||
return False
|
||||
|
||||
if not has_hostname():
|
||||
if text and not text.endswith("\n"):
|
||||
text += "\n"
|
||||
text += f"127.0.1.1 {hostname}\n"
|
||||
hosts_path.write_text(text, encoding="utf-8")
|
||||
PY
|
||||
if ! getent hosts "$(hostname)" >/dev/null 2>&1; then
|
||||
sudo -n /usr/bin/python3 -c 'import os
|
||||
from pathlib import Path
|
||||
hn = os.popen("hostname").read().strip()
|
||||
p = Path("/etc/hosts")
|
||||
t = p.read_text(encoding="utf-8") if p.exists() else ""
|
||||
lines = t.splitlines()
|
||||
ok = any((ln.strip() and not ln.lstrip().startswith("#") and hn in ln.split()) for ln in lines)
|
||||
if not ok:
|
||||
if t and not t.endswith("\n"):
|
||||
t += "\n"
|
||||
t += f"127.0.1.1 {hn}\n"
|
||||
p.write_text(t, encoding="utf-8")
|
||||
'
|
||||
fi
|
||||
|
||||
sudo -n mkdir -p /opt/infra-controller /etc/infra-controller /var/lib/infra-controller /var/log/infra-controller
|
||||
|
|
|
|||
Loading…
Reference in a new issue