Class: VagrantPlugins::OrbStack::Util::OrbStackCLI
- Inherits:
-
Object
- Object
- VagrantPlugins::OrbStack::Util::OrbStackCLI
- Defined in:
- lib/vagrant-orbstack/util/orbstack_cli.rb
Overview
Utility class for detecting and interacting with OrbStack CLI
This class provides a Ruby interface to the OrbStack command-line tool (orb).
All methods execute shell commands and return parsed results. The class handles
timeouts, error detection, and logging automatically.
Constant Summary collapse
- QUERY_TIMEOUT =
Default timeout for non-mutating query operations (list, info). Use this for fast read operations.
30- MUTATE_TIMEOUT =
Default timeout for state-changing operations (start, stop, delete). Use this for operations that modify machine state.
60- CREATE_TIMEOUT =
Default timeout for machine creation operations. Longer timeout accounts for distribution image downloads which can take 30-120 seconds on first use.
120
Class Method Summary collapse
-
.available? ⇒ Boolean
Check if orb command is available in PATH.
-
.create_machine(name, distribution:, timeout: CREATE_TIMEOUT) ⇒ Hash
Create a new OrbStack machine.
-
.delete_machine(name) ⇒ Boolean
Delete an OrbStack machine.
-
.list_machines ⇒ Array<Hash>, Array
List all OrbStack machines.
-
.machine_info(name) ⇒ Hash?
Get detailed information about a specific machine.
-
.running? ⇒ Boolean
Check if OrbStack is currently running.
-
.start_machine(name, timeout: MUTATE_TIMEOUT) ⇒ Hash
Start an OrbStack machine.
-
.stop_machine(name) ⇒ Boolean
Stop an OrbStack machine.
-
.version ⇒ String?
Get OrbStack version.
Class Method Details
.available? ⇒ Boolean
Check if orb command is available in PATH
55 56 57 58 |
# File 'lib/vagrant-orbstack/util/orbstack_cli.rb', line 55 def available? stdout, _stderr, success = execute_command('which orb') !stdout.empty? && success end |
.create_machine(name, distribution:, timeout: CREATE_TIMEOUT) ⇒ Hash
Create a new OrbStack machine
Executes orb create <distro> <name>. This operation can take 30-120 seconds
on first use if the distribution image needs to be downloaded. Subsequent
creations of the same distribution are much faster.
rubocop:disable Naming/PredicateMethod
156 157 158 159 160 |
# File 'lib/vagrant-orbstack/util/orbstack_cli.rb', line 156 def create_machine(name, distribution:, timeout: CREATE_TIMEOUT) _, stderr, success = execute_command("orb create #{distribution} #{name}", timeout: timeout) raise_unless_successful!('create', stderr, success) { id: name, status: 'running' } end |
.delete_machine(name) ⇒ Boolean
Delete an OrbStack machine
Executes orb delete <name>. This permanently removes the machine and
all its data. The operation cannot be undone.
174 175 176 177 178 |
# File 'lib/vagrant-orbstack/util/orbstack_cli.rb', line 174 def delete_machine(name) _, stderr, success = execute_command("orb delete #{name}", timeout: MUTATE_TIMEOUT) raise_unless_successful!('delete', stderr, success) true end |
.list_machines ⇒ Array<Hash>, Array
List all OrbStack machines
Executes orb list and parses the output into an array of machine hashes.
Each hash contains the machine name and current status.
rubocop:disable Metrics/MethodLength
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/vagrant-orbstack/util/orbstack_cli.rb', line 93 def list_machines stdout, _stderr, success = execute_command('orb list', timeout: QUERY_TIMEOUT) return [] unless success return [] if stdout.empty? # Parse machine list output (format: NAME STATUS DISTRO IP) machines = [] stdout.each_line do |line| parts = line.strip.split(/\s+/) next if parts.empty? machines << { name: parts[0], status: parts[1] } end machines end |
.machine_info(name) ⇒ Hash?
Get detailed information about a specific machine
Executes orb info <name> and parses the JSON output. Returns nil if
the machine doesn't exist or if JSON parsing fails.
127 128 129 130 131 132 133 134 135 |
# File 'lib/vagrant-orbstack/util/orbstack_cli.rb', line 127 def machine_info(name) stdout, _stderr, success = execute_command("orb info --format json #{name}", timeout: QUERY_TIMEOUT) return nil unless success JSON.parse(stdout) rescue JSON::ParserError => e @logger.warn("Failed to parse machine info JSON: #{e.}") nil end |
.running? ⇒ Boolean
Check if OrbStack is currently running
73 74 75 76 |
# File 'lib/vagrant-orbstack/util/orbstack_cli.rb', line 73 def running? stdout, _stderr, success = execute_command('orb status') success && stdout.match?(/running/i) end |
.start_machine(name, timeout: MUTATE_TIMEOUT) ⇒ Hash
Start an OrbStack machine
Executes orb start <name>. Starts a stopped machine. If the machine
is already running, this is a no-op.
193 194 195 196 197 |
# File 'lib/vagrant-orbstack/util/orbstack_cli.rb', line 193 def start_machine(name, timeout: MUTATE_TIMEOUT) _, stderr, success = execute_command("orb start #{name}", timeout: timeout) raise_unless_successful!('start', stderr, success) { id: name, status: 'running' } end |
.stop_machine(name) ⇒ Boolean
Stop an OrbStack machine
Executes orb stop <name>. Stops a running machine gracefully. If the
machine is already stopped, this is a no-op.
211 212 213 214 215 |
# File 'lib/vagrant-orbstack/util/orbstack_cli.rb', line 211 def stop_machine(name) _, stderr, success = execute_command("orb stop #{name}", timeout: MUTATE_TIMEOUT) raise_unless_successful!('stop', stderr, success) true end |
.version ⇒ String?
Get OrbStack version
62 63 64 65 66 67 68 69 |
# File 'lib/vagrant-orbstack/util/orbstack_cli.rb', line 62 def version stdout, _stderr, success = execute_command('orb --version') return nil unless success # Parse version from output like "orb version 1.2.3" or just "1.2.3" match = stdout.match(/(\d+\.\d+\.\d+)/) match ? match[1] : nil end |