Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ task :install => [
:macos,
:vscode,
:cfg,
:sandbox,
]
task :shell => [
:sh,
Expand All @@ -36,7 +37,8 @@ task :check => [
:test_zsh,
:shellcheck,
:test_bats,
:test_cfg
:test_cfg,
:test_sandbox
]

task :shellcheck do
Expand Down Expand Up @@ -308,6 +310,10 @@ task :cfg => :bash do
sh 'install -m 755 ruby/bin/cfg "$HOME/bin/cfg"'
end

task :sandbox => :cfg do
sh 'install -m 755 ruby/bin/sandbox "$HOME/bin/sandbox"'
end

task :test_cfg do
begin
require 'rspec/core/rake_task'
Expand All @@ -325,3 +331,21 @@ task :test_cfg do
end
Rake::Task[:_run_cfg_specs].invoke
end

task :test_sandbox do
begin
require 'rspec/core/rake_task'
rescue LoadError
puts "Warning: rspec not installed, skipping sandbox tests"
next
end
test_files = Dir.glob("ruby/lib/sandbox/spec/*_spec.rb")
if test_files.empty?
puts "No sandbox spec files found"
next
end
RSpec::Core::RakeTask.new(:_run_sandbox_specs) do |t|
t.pattern = test_files
end
Rake::Task[:_run_sandbox_specs].invoke
end
6 changes: 6 additions & 0 deletions ruby/bin/sandbox
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

$LOAD_PATH.unshift("#{Dir.home}/.local/lib/dotfiles")
require 'sandbox'
Sandbox::CLI.run(ARGV)
22 changes: 22 additions & 0 deletions ruby/lib/sandbox.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Sandbox
class Error < StandardError; end
end

require_relative 'sandbox/paths'
require_relative 'sandbox/name'
require_relative 'sandbox/ssh_alias'
require_relative 'sandbox/command_runner'
require_relative 'sandbox/spinner'
require_relative 'sandbox/connection_info'
require_relative 'sandbox/backend_interface'
require_relative 'sandbox/backends/bash_backend'
require_relative 'sandbox/backends/container'
require_relative 'sandbox/backends/kvm'
require_relative 'sandbox/backends/hcloud'
require_relative 'sandbox/backends/proxy'
require_relative 'sandbox/ai_bootstrapper'
require_relative 'sandbox/proxy_cli'
require_relative 'sandbox/app'
require_relative 'sandbox/cli'
75 changes: 75 additions & 0 deletions ruby/lib/sandbox/ai_bootstrapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# frozen_string_literal: true

require 'shellwords'

module Sandbox
class AiBootstrapper
def initialize(runner:, backend_name:, proxy: false)
@runner = runner
@backend_name = backend_name
@proxy = proxy
end

def validate_agent!(agent)
run_script("ensure_know_agent #{Shellwords.escape(agent)}")
end

def bootstrap!(sandbox_name, agent)
script = <<~BASH
ensure_know_agent #{Shellwords.escape(agent)}
bootstrap_ai #{Shellwords.escape(sandbox_name)} #{Shellwords.escape(agent)}
BASH
run_script(script)
end

private

def run_script(body)
script = +"set -euo pipefail\n"
script << "BACKEND=#{Shellwords.escape(@backend_name)}\n"
script << "PROXY=#{Shellwords.escape(@proxy ? 'true' : 'false')}\n"
script << "source #{Shellwords.escape(common_path)}\n"
script << "source #{Shellwords.escape(proxy_path)}\n"
script << "source #{Shellwords.escape(ai_bootstrap_path)}\n"
script << "source #{Shellwords.escape(container_path)}\n"
script << "source #{Shellwords.escape(kvm_path)}\n"
script << "source #{Shellwords.escape(hcloud_path)}\n"
script << <<~BASH
backend_get_ssh_port() {
local name="$1"
case "$BACKEND" in
container) get_container_ssh_port "$name" ;;
kvm) get_kvm_ssh_port "$name" ;;
hcloud) get_hcloud_ssh_port "$name" ;;
esac
}
BASH
script << body
@runner.run(['bash', '-lc', script])
end

def common_path
File.join(Dir.home, '.config/lib/bash/sandbox/common')
end

def proxy_path
File.join(Dir.home, '.config/lib/bash/sandbox/proxy-backend')
end

def ai_bootstrap_path
File.join(Dir.home, '.config/lib/bash/sandbox/ai-bootstrap')
end

def container_path
File.join(Dir.home, '.config/lib/bash/sandbox/container-backend')
end

def kvm_path
File.join(Dir.home, '.config/lib/bash/sandbox/kvm-backend')
end

def hcloud_path
File.join(Dir.home, '.config/lib/bash/sandbox/hcloud-backend')
end
end
end
Loading