Skip to content

Relax root requirement - allow cluster-tool to run without sudo - #17

Open
danielerez wants to merge 1 commit into
osac-project:mainfrom
danielerez:relax-root-requirement
Open

Relax root requirement - allow cluster-tool to run without sudo#17
danielerez wants to merge 1 commit into
osac-project:mainfrom
danielerez:relax-root-requirement

Conversation

@danielerez

@danielerez danielerez commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR removes the requirement to run cluster-tool with sudo. Instead, the tool uses sudo
internally only for the specific operations that require elevated privileges (HAProxy and DNS
configuration). Users can now run commands normally and will be prompted for their password only
when needed.

Motivation

Previously, cluster-tool had several issues with permissions:

  • Required running entire commands with sudo ./cluster-tool ...
  • Users had to choose between "all sudo" or "no sudo"
  • chown operations failed without root
  • Used qemu:///session which lacks network bridge permissions
  • HAProxy and DNS configuration required root for entire invocation

This made the tool awkward to use and less secure (running everything as root when only small parts
needed it).

Changes

1. Sudo used internally for privileged operations

  • HAProxy functions now use sudo for /etc/haproxy/haproxy.cfg operations
  • DNS functions now use sudo for /etc/NetworkManager/dnsmasq.d/ operations
  • User is prompted for password automatically when needed
  • User can press Ctrl+C at sudo prompt to skip HAProxy/DNS configuration

2. Made file ownership changes non-fatal

  • All chown qemu:qemu operations use check=False
  • Files remain accessible to libvirt when user is in libvirt group

3. Use qemu:///system connection

  • Added virsh_cmd() helper using qemu:///system connection
  • Fixes "Operation not permitted" when creating network bridges
  • Works when user is in libvirt group

4. Added configure-access command

  • Configure HAProxy/DNS separately if skipped during boot
  • Usage: ./cluster-tool configure-access <clone-id>
  • Handles sudo prompts internally

5. Improved connect for non-root usage

  • Detects if running as root vs regular user
  • Checks for existing packages/services instead of requiring root
  • Graceful degradation with warnings

6. Robust cleanup

  • Network cleanup uses check=False
  • HAProxy/DNS cleanup doesn't fail entire operation

Prerequisites

On the server:

  1. User in libvirt group: sudo usermod -aG libvirt $USER (log out/in)
  2. Packages installed: libvirt, qemu-kvm, podman, pigz, haproxy, skopeo, zstd
  3. Services running: libvirtd, haproxy
  4. User has sudo privileges (for HAProxy/DNS configuration)

On the client (laptop):

  • Run ./cluster-tool setup client once (prompts for sudo password)

Usage

Normal workflow (sudo prompts automatically):

# Boot cluster - sudo password prompted for HAProxy/DNS
./cluster-tool boot --flavor my-flavor --name test
[sudo] password for user:   ← appears automatically
# Cluster boots, HAProxy and DNS configured

# Access cluster via domain names
export KUBECONFIG=~/.kube/test.kubeconfig
oc get nodes

Skip sudo by pressing Ctrl+C at prompt:

# Boot cluster
./cluster-tool boot --flavor my-flavor --name test
[sudo] password for user:   ← press Ctrl+C here
  WARNING: HAProxy configuration requires sudo
           You can configure later: ./cluster-tool configure-access test
# Cluster still boots successfully!

# Configure access later
./cluster-tool configure-access test
[sudo] password for user:   ← prompted again
  ✓ HAProxy configured
  ✓ DNS configured

Access via VM IP (no HAProxy/DNS needed):

# Boot cluster, skip sudo
./cluster-tool boot --flavor my-flavor --name test
[sudo] password for user:   ← press Ctrl+C
  VM IP:  192.168.X.10

# Access directly
ssh -i /path/to/flavor/crypto/cluster-tool.key core@192.168.X.10 \
  "sudo oc --kubeconfig=/etc/.../lb-ext.kubeconfig get nodes"

What Works Now

✅ ./cluster-tool boot (sudo prompts for HAProxy/DNS)
✅ ./cluster-tool connect (when packages installed)
✅ ./cluster-tool destroy (sudo prompts for HAProxy/DNS cleanup)
✅ ./cluster-tool configure-access (sudo prompts)
✅ ./cluster-tool snapshot
✅ ./cluster-tool pull
✅ ./cluster-tool push
✅ ./cluster-tool list
✅ ./cluster-tool flavors

Benefits

1. Cleaner UX: Run commands normally, not sudo ./cluster-tool ...
2. More secure: Only specific operations use elevated privileges
3. Flexible: Can skip sudo prompts by pressing Ctrl+C
4. Transparent: Clear when and why sudo is needed
5. No behavior change: Everything works exactly as before for users with sudo

Limitations

When you skip sudo prompts (press Ctrl+C):
- HAProxy: Can't update /etc/haproxy/haproxy.cfg
  - Impact: No domain name access to API/console
  - Workaround: Use configure-access command later, or access via VM IP
- DNS: Can't update /etc/NetworkManager/dnsmasq.d/
  - Impact: Cluster domains won't resolve
  - Workaround: Use VM IPs

The cluster itself boots perfectly - HAProxy/DNS are just convenience features.

Breaking Changes

None. Commands work exactly the same, just with better sudo handling.

New Command

- configure-access <clone-id> - Configure HAProxy and DNS separately (prompts for sudo)

This is a much better approach! The tool now handles sudo transparently and only when needed.

Remove the requirement to run cluster-tool with sudo. Instead, use sudo
internally only for operations that need elevated privileges (HAProxy
and DNS configuration). Users run commands normally and are prompted
for password only when needed.

Changes:

1. Use sudo internally for privileged operations
   - HAProxy read/write uses 'sudo cat/mv/chmod/systemctl'
   - DNS configuration uses 'sudo mv/chmod/nmcli'
   - User prompted for password automatically when needed
   - User can press Ctrl+C at sudo prompt to skip

2. Use qemu:///system connection for libvirt
   - Added virsh_cmd() helper that forces qemu:///system
   - Fixes "Operation not permitted" when creating network bridges
   - Works when user is in libvirt group

3. Make file ownership changes non-fatal
   - All 'chown qemu:qemu' operations use check=False
   - Files remain accessible when user is in libvirt group

4. Improve connect command for non-root usage
   - Detect if running as root vs regular user
   - Check for existing packages/services
   - Graceful degradation with warnings

5. Add configure-access command
   - Configure HAProxy and DNS separately after boot
   - Usage: ./cluster-tool configure-access <clone-id>
   - Useful when user skips sudo during boot

6. Better error messages and output
   - Show exact commands when sudo fails
   - Display VM IP when HAProxy not configured
   - Clear instructions for next steps

Prerequisites:
- User in libvirt group: sudo usermod -aG libvirt $USER
- Required packages installed
- User has sudo privileges (prompted when needed)

Usage:
  # Normal - sudo prompts automatically
  ./cluster-tool boot --flavor my-flavor --name test
  [sudo] password:  ← prompted automatically

  # Or skip sudo and configure later
  ./cluster-tool boot --flavor my-flavor --name test
  [sudo] password:  ← press Ctrl+C
  ./cluster-tool configure-access test

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@danielerez
danielerez force-pushed the relax-root-requirement branch from 04c0662 to 59a8d9d Compare June 9, 2026 20:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant