Skip to content

Commit 00e4390

Browse files
author
thehighnotes
committed
Improve node alias auto-detection during hub installation
- Match local machine against hub's hosts list by username or hostname before prompting - Preserve existing hub entry details while marking the node as 'local' - Add PATH export to shell aliases for consistent tool availability
1 parent e638920 commit 00e4390

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

docs/hub/getting-started.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ The node wizard will:
6363
1. Ask for the hub's IP address and username
6464
2. Establish SSH connectivity (generate keys, `ssh-copy-id`)
6565
3. Sync `config.json` and `projects.json` from the hub
66-
4. Create SSH stub scripts that forward hub commands to the hub machine
66+
4. Auto-detect this machine from the hub's hosts list (matches by username, then hostname) — only prompts for alias/name if no match is found
67+
5. Create SSH stub scripts that forward hub commands to the hub machine
6768

6869
**Usage:**
6970

tools/hub/install.py

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ def setup_shell_aliases():
295295

296296
aliases = f"""
297297
{marker}
298+
export PATH="$HOME/.local/bin:$PATH"
298299
alias repo='~/git'
299300
alias hub='~/hub'
300301
alias status='~/hub --status'
@@ -661,11 +662,9 @@ def setup_node():
661662
if not ask_yn("Continue without hub connection?", default=False):
662663
sys.exit(1)
663664

664-
# Step 3: Fetch config from hub
665+
# Step 3: Fetch config from hub and identify this node
665666
print()
666667
print_step(3, total_steps, "Sync from hub")
667-
local_key = ask("This node's alias", hostname)
668-
local_name = ask("Display name", hostname.capitalize())
669668

670669
hub_config = None
671670
hub_projects = None
@@ -699,12 +698,44 @@ def setup_node():
699698
print_warn("Could not fetch hub config — using defaults")
700699
hub_config = {}
701700

702-
# Build node config
701+
# Try to auto-detect this node from the hub's hosts list
703702
hosts = hub_config.get('hosts', {})
704-
hosts[local_key] = {
705-
'name': local_name, 'ip': '127.0.0.1',
706-
'user': user, 'roles': ['local'],
707-
}
703+
local_key = None
704+
local_name = None
705+
706+
if hosts:
707+
# Skip the hub's own local_host entry — we're looking for *this* machine
708+
hub_local = hub_config.get('hub', {}).get('local_host', '')
709+
candidates = {k: v for k, v in hosts.items() if k != hub_local}
710+
711+
# Match by username first (most reliable), then hostname
712+
for key, info in candidates.items():
713+
if info.get('user') == user:
714+
local_key = key
715+
local_name = info.get('name', key)
716+
break
717+
if not local_key:
718+
for key, info in candidates.items():
719+
if key == hostname or info.get('name', '').lower() == hostname.lower():
720+
local_key = key
721+
local_name = info.get('name', key)
722+
break
723+
724+
if local_key:
725+
print_ok(f"Matched hub host: {local_key}{local_name} ({hosts[local_key].get('ip', '?')}, {user})")
726+
# Preserve the hub's entry but mark as local
727+
hub_entry = hosts[local_key]
728+
hub_entry['roles'] = list(set(hub_entry.get('roles', [])) | {'local'})
729+
hub_entry['ip'] = '127.0.0.1'
730+
hub_entry['user'] = user
731+
else:
732+
print_warn("Could not auto-detect this machine in hub's hosts")
733+
local_key = ask("This node's alias", hostname)
734+
local_name = ask("Display name", hostname.capitalize())
735+
hosts[local_key] = {
736+
'name': local_name, 'ip': '127.0.0.1',
737+
'user': user, 'roles': ['local'],
738+
}
708739

709740
config = {
710741
'hub': {

0 commit comments

Comments
 (0)