Class: VagrantPlugins::OrbStack::Util::MachineNamer

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-orbstack/util/machine_namer.rb

Overview

Utility class for generating unique machine names with collision avoidance

Generates machine names in the format: vagrant-- where short-id is a 6-character random hex string. Implements collision detection and automatic retry with new IDs.

Examples:

Generate a unique machine name

machine = double('machine', name: 'web_server')
name = MachineNamer.generate(machine)
# => "vagrant-web-server-a3b2c1"

Collision handling

# If "vagrant-default-a3b2c1" exists, generates new ID automatically
name = MachineNamer.generate(machine)
# => "vagrant-default-d4e5f6" (different ID)

Constant Summary collapse

MAX_RETRIES =

Maximum number of retry attempts for collision avoidance

3
MAX_NAME_LENGTH =

Maximum machine name length (DNS hostname limit)

63

Class Method Summary collapse

Class Method Details

.generate(machine) ⇒ String

Generate a unique machine name with collision avoidance.

Creates a machine name in the format vagrant-- where:

  • name is sanitized from machine.name (lowercase, hyphens, alphanumeric)
  • id is a 6-character random hex string

If a collision is detected (name already exists in OrbStack), retries with a new random ID up to MAX_RETRIES times.

Parameters:

  • machine (Vagrant::Machine)

    The machine object with name attribute

Returns:

  • (String)

    A unique machine name

Raises:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/vagrant-orbstack/util/machine_namer.rb', line 49

def self.generate(machine)
  machine_name = machine.name.to_s
  sanitized = sanitize_name(machine_name)

  MAX_RETRIES.times do
    short_id = SecureRandom.hex(3)
    candidate = "vagrant-#{sanitized}-#{short_id}"

    return candidate unless check_collision?(candidate)
  end

  # All retries exhausted - raise error
  raise MachineNameCollisionError,
        "Failed to generate unique machine name after #{MAX_RETRIES} attempts (machine: #{machine_name})"
end