Add dynamic file expansion

This commit is contained in:
David Heinemeier Hansson
2023-01-23 12:45:49 +01:00
parent 52d10394f7
commit e1d518216a
5 changed files with 29 additions and 10 deletions

View File

@@ -18,7 +18,8 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base
accessory = MRSK.accessory(name) accessory = MRSK.accessory(name)
on(accessory.host) do on(accessory.host) do
accessory.files.each do |(local, remote)| accessory.files.each do |(local, remote)|
execute *accessory.make_directory_for(local, remote) accessory.ensure_local_file_present(local)
execute *accessory.make_directory_for(remote)
upload! local, remote upload! local, remote
execute :chmod, "755", remote execute :chmod, "755", remote
end end

View File

@@ -46,10 +46,6 @@ class Mrsk::Commands::Accessory < Mrsk::Commands::Base
).join(" "), host: host ).join(" "), host: host
end end
def make_directory_for(local, remote)
if Pathname.new(local).exist?
[ :mkdir, "-p", Pathname.new(remote).dirname.to_s ]
else
def exec(*command, interactive: false) def exec(*command, interactive: false)
docker :exec, docker :exec,
("-it" if interactive), ("-it" if interactive),
@@ -73,10 +69,16 @@ class Mrsk::Commands::Accessory < Mrsk::Commands::Base
exec_over_ssh "bash", host: host exec_over_ssh "bash", host: host
end end
def ensure_local_file_present(local)
if !local.is_a?(StringIO) && !Pathname.new(local).exist?
raise "Missing file: #{local}" raise "Missing file: #{local}"
end end
end end
def make_directory_for(remote)
[ :mkdir, "-p", Pathname.new(remote).dirname.to_s ]
end
def remove_files def remove_files
[ :rm, "-rf", service_name ] [ :rm, "-rf", service_name ]
end end

View File

@@ -46,7 +46,7 @@ class Mrsk::Configuration::Assessory
def files def files
specifics["files"]&.to_h do |local_to_remote_mapping| specifics["files"]&.to_h do |local_to_remote_mapping|
local_file, remote_file = local_to_remote_mapping.split(":") local_file, remote_file = local_to_remote_mapping.split(":")
[ expand_local_file_path(local_file), expand_remote_file_path(remote_file) ] [ expand_local_file(local_file), expand_remote_file(remote_file) ]
end || {} end || {}
end end
@@ -65,18 +65,26 @@ class Mrsk::Configuration::Assessory
{ "service" => service_name } { "service" => service_name }
end end
def expand_local_file_path(local_file) def expand_local_file(local_file)
Pathname.new(File.expand_path(local_file)).to_s if local_file.end_with?("erb")
read_dynamic_file(local_file)
else
Pathname.new(File.expand_path(local_file)).to_s
end
end end
def expand_remote_file_path(remote_file) def expand_remote_file(remote_file)
service_name + remote_file service_name + remote_file
end end
def remote_files_as_volumes def remote_files_as_volumes
specifics["files"]&.collect do |local_to_remote_mapping| specifics["files"]&.collect do |local_to_remote_mapping|
_, remote_file = local_to_remote_mapping.split(":") _, remote_file = local_to_remote_mapping.split(":")
"$PWD/#{expand_remote_file_path(remote_file)}:#{remote_file}" "$PWD/#{expand_remote_file(remote_file)}:#{remote_file}"
end || [] end || []
end end
def read_dynamic_file(local_file)
StringIO.new(ERB.new(IO.read(local_file)).result)
end
end end

View File

@@ -90,4 +90,11 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase
assert_equal ["--volume", "$PWD/app-mysql/etc/mysql/my.cnf:/etc/mysql/my.cnf", "--volume", "$PWD/app-mysql/docker-entrypoint-initdb.d/structure.sql:/docker-entrypoint-initdb.d/structure.sql"], @config.accessory(:mysql).volume_args assert_equal ["--volume", "$PWD/app-mysql/etc/mysql/my.cnf:/etc/mysql/my.cnf", "--volume", "$PWD/app-mysql/docker-entrypoint-initdb.d/structure.sql:/docker-entrypoint-initdb.d/structure.sql"], @config.accessory(:mysql).volume_args
assert_equal ["--volume", "/var/lib/redis:/data"], @config.accessory(:redis).volume_args assert_equal ["--volume", "/var/lib/redis:/data"], @config.accessory(:redis).volume_args
end end
test "dynamic file expansion" do
@deploy[:accessories]["mysql"]["files"] << "test/fixtures/files/structure.sql.erb:/docker-entrypoint-initdb.d/structure.sql"
@config = Mrsk::Configuration.new(@deploy)
assert_equal "This was dynamically expanded", @config.accessory(:mysql).files.keys[2].read
end
end end

1
test/fixtures/files/structure.sql.erb vendored Normal file
View File

@@ -0,0 +1 @@
<%= "This was dynamically expanded" %>