Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use YAML to store user-specified config #6

Merged
merged 1 commit into from Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions spec/tasks/lima_start_spec.rb
Expand Up @@ -130,8 +130,9 @@
end

context 'with config specified' do
config = { 'a': 'b' }
tmpfile = Tempfile.new(["lima_#{vm_name}", '.json'])
let(:opts) { super().merge(config: { 'a': 'b' }) }

tmpfile = Tempfile.new(["lima_#{vm_name}", '.yaml'])
before :each do
allow(Tempfile).to receive(:new).and_return(tmpfile)
end
Expand All @@ -140,7 +141,14 @@
tmpfile.unlink
end

it_behaves_like 'limactl start', { name: vm_name, config: config }, tmpfile.path
it 'invokes `limactl start` with correct parameters' do
allow(Open3).to receive(:capture3).with('limactl', 'validate', tmpfile.path).and_return(['stdout', 'stderr', 0])
allow(Open3).to receive(:capture3).with('limactl', 'start', "--name=#{opts[:name]}", "--timeout=#{opts[:timeout]}", tmpfile.path).and_return(lima_response)

task.set_opts(opts)
result = task.create
expect(result).to eq(success_result)
end
end
end

Expand Down
32 changes: 28 additions & 4 deletions tasks/start.rb
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'json'
require 'yaml'
require 'open3'
require 'tempfile'
require 'shellwords'
Expand Down Expand Up @@ -40,11 +40,18 @@ def create
elsif @url
cfg_url = @url
elsif @config
# Write @config to a temporary JSON file and pass it to limactl later
# Write @config to a temporary YAML file and pass it to limactl later
safe_name = Shellwords.escape(@name)
tmpfile = Tempfile.new(["lima_#{safe_name}", '.json'])
tmpfile.write(JSON.dump(@config))
tmpfile = Tempfile.new(["lima_#{safe_name}", '.yaml'])
# @config has symbolized keys by default. So .to_yaml will write keys as :symbols.
# Keys should be stringified to avoid this so Lima can parse the YAML properly.
tmpfile.write(stringify_keys_recursively(@config).to_yaml)
tmpfile.close

# Validate the config
_, stderr_str, status = Open3.capture3(@limactl, 'validate', tmpfile.path)
raise TaskHelper::Error.new(stderr_str, 'lima/validate-error', @config.to_yaml) unless status == 0

cfg_url = tmpfile.path
else
raise TaskHelper::Error.new('One of url/template/config parameters must be specified', 'lima/create-error')
Expand Down Expand Up @@ -81,6 +88,23 @@ def set_opts(opts = {})
@template = opts[:template]
@config = opts[:config]
end

private

# Stringify Hash keys recursively
def stringify_keys_recursively(hash)
stringified_hash = {}
hash.each do |k, v|
stringified_hash[k.to_s] = if v.is_a?(Hash)
stringify_keys_recursively(v)
elsif v.is_a?(Array)
v.map { |x| x.is_a?(Hash) ? stringify_keys_recursively(x) : x }
else
v
end
end
stringified_hash
end
end

LimaStart.run if $PROGRAM_NAME == __FILE__