Add managed accessory directories

This commit is contained in:
David Heinemeier Hansson
2023-01-23 13:36:47 +01:00
parent eceafbedf4
commit 03488bc67a
6 changed files with 78 additions and 17 deletions

View File

@@ -6,6 +6,7 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base
if name == "all" if name == "all"
MRSK.accessory_names.each { |accessory_name| boot(accessory_name) } MRSK.accessory_names.each { |accessory_name| boot(accessory_name) }
else else
directories(name)
upload(name) upload(name)
accessory = MRSK.accessory(name) accessory = MRSK.accessory(name)
@@ -19,6 +20,7 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base
on(accessory.host) do on(accessory.host) do
accessory.files.each do |(local, remote)| accessory.files.each do |(local, remote)|
accessory.ensure_local_file_present(local) accessory.ensure_local_file_present(local)
execute *accessory.make_directory_for(remote) execute *accessory.make_directory_for(remote)
upload! local, remote upload! local, remote
execute :chmod, "755", remote execute :chmod, "755", remote
@@ -26,6 +28,16 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base
end end
end end
desc "directories [NAME]", "Create accessory directories on host"
def directories(name)
accessory = MRSK.accessory(name)
on(accessory.host) do
accessory.directories.keys.each do |host_path|
execute *accessory.make_directory(host_path)
end
end
end
desc "reboot [NAME]", "Reboot accessory on host (stop container, remove container, start new container)" desc "reboot [NAME]", "Reboot accessory on host (stop container, remove container, start new container)"
def reboot(name) def reboot(name)
stop(name) stop(name)
@@ -114,7 +126,7 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base
stop(name) stop(name)
remove_container(name) remove_container(name)
remove_image(name) remove_image(name)
remove_files(name) remove_service_directory(name)
end end
end end
@@ -130,9 +142,9 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base
on(accessory.host) { execute *accessory.remove_image } on(accessory.host) { execute *accessory.remove_image }
end end
desc "remove_files [NAME]", "Remove accessory directory used for uploaded files from host" desc "remove_service_directory [NAME]", "Remove accessory directory used for uploaded files and data directories from host"
def remove_files(name) def remove_service_directory(name)
accessory = MRSK.accessory(name) accessory = MRSK.accessory(name)
on(accessory.host) { execute *accessory.remove_files } on(accessory.host) { execute *accessory.remove_service_directory }
end end
end end

View File

@@ -2,7 +2,7 @@ require "mrsk/commands/base"
class Mrsk::Commands::Accessory < Mrsk::Commands::Base class Mrsk::Commands::Accessory < Mrsk::Commands::Base
attr_reader :accessory_config attr_reader :accessory_config
delegate :service_name, :image, :host, :port, :files, :env_args, :volume_args, :label_args, to: :accessory_config delegate :service_name, :image, :host, :port, :files, :directories, :env_args, :volume_args, :label_args, to: :accessory_config
def initialize(config, name:) def initialize(config, name:)
super(config) super(config)
@@ -76,10 +76,14 @@ class Mrsk::Commands::Accessory < Mrsk::Commands::Base
end end
def make_directory_for(remote_file) def make_directory_for(remote_file)
[ :mkdir, "-p", Pathname.new(remote_file).dirname.to_s ] make_directory Pathname.new(remote_file).dirname.to_s
end end
def remove_files def make_directory(path)
[ :mkdir, "-p", path ]
end
def remove_service_directory
[ :rm, "-rf", service_name ] [ :rm, "-rf", service_name ]
end end

View File

@@ -50,8 +50,15 @@ class Mrsk::Configuration::Assessory
end || {} end || {}
end end
def directories
specifics["directories"]&.to_h do |host_to_container_mapping|
host_relative_path, container_path = host_to_container_mapping.split(":")
[ expand_host_path(host_relative_path), container_path ]
end || {}
end
def volumes def volumes
(specifics["volumes"] || []) + remote_files_as_volumes specific_volumes + remote_files_as_volumes + remote_directories_as_volumes
end end
def volume_args def volume_args
@@ -73,18 +80,37 @@ class Mrsk::Configuration::Assessory
end end
end end
def read_dynamic_file(local_file)
StringIO.new(ERB.new(IO.read(local_file)).result)
end
def expand_remote_file(remote_file) def expand_remote_file(remote_file)
service_name + remote_file service_name + remote_file
end end
def specific_volumes
specifics["volumes"] || []
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(remote_file)}:#{remote_file}" "#{service_data_directory + remote_file}:#{remote_file}"
end || [] end || []
end end
def read_dynamic_file(local_file) def remote_directories_as_volumes
StringIO.new(ERB.new(IO.read(local_file)).result) specifics["directories"]&.collect do |host_to_container_mapping|
host_relative_path, container_path = host_to_container_mapping.split(":")
[ expand_host_path(host_relative_path), container_path ].join(":")
end || []
end
def expand_host_path(host_relative_path)
"#{service_data_directory}/#{host_relative_path}"
end
def service_data_directory
"$PWD/#{service_name}"
end end
end end

View File

@@ -14,9 +14,21 @@ class CliAccessoryTest < ActiveSupport::TestCase
assert_match "test/fixtures/files/my.cnf app-mysql/etc/mysql/my.cnf", command assert_match "test/fixtures/files/my.cnf app-mysql/etc/mysql/my.cnf", command
end end
test "directories" do
command = stdouted { Mrsk::Cli::Accessory.start(["directories", "mysql", "-c", "test/fixtures/deploy_with_accessories.yml"]) }
assert_match "mkdir -p $PWD/app-mysql/data", command
end
test "remove service direcotry" do
command = stdouted { Mrsk::Cli::Accessory.start(["remove_service_directory", "mysql", "-c", "test/fixtures/deploy_with_accessories.yml"]) }
assert_match "rm -rf app-mysql", command
end
test "boot" do test "boot" do
command = stdouted { Mrsk::Cli::Accessory.start(["boot", "mysql", "-c", "test/fixtures/deploy_with_accessories.yml"]) } command = stdouted { Mrsk::Cli::Accessory.start(["boot", "mysql", "-c", "test/fixtures/deploy_with_accessories.yml"]) }
assert_match "Running docker run --name app-mysql -d --restart unless-stopped -p 3306:3306 -e [REDACTED] -e MYSQL_ROOT_HOST=% --volume /var/lib/mysql:/var/lib/mysql --volume $PWD/app-mysql/etc/mysql/my.cnf:/etc/mysql/my.cnf --label service=app-mysql mysql:5.7 on 1.1.1.3", command assert_match "Running docker run --name app-mysql -d --restart unless-stopped -p 3306:3306 -e [REDACTED] -e MYSQL_ROOT_HOST=% --volume $PWD/app-mysql/etc/mysql/my.cnf:/etc/mysql/my.cnf --volume $PWD/app-mysql/data:/var/lib/mysql --label service=app-mysql mysql:5.7 on 1.1.1.3", command
end end
end end

View File

@@ -23,6 +23,9 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase
"files" => [ "files" => [
"config/mysql/my.cnf:/etc/mysql/my.cnf", "config/mysql/my.cnf:/etc/mysql/my.cnf",
"db/structure.sql:/docker-entrypoint-initdb.d/structure.sql" "db/structure.sql:/docker-entrypoint-initdb.d/structure.sql"
],
"directories" => [
"data:/var/lib/mysql"
] ]
}, },
"redis" => { "redis" => {
@@ -87,7 +90,7 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase
end end
test "volume args" do test "volume args" do
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", "--volume", "$PWD/app-mysql/data:/var/lib/mysql"], @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
@@ -97,4 +100,8 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase
assert_equal "This was dynamically expanded", @config.accessory(:mysql).files.keys[2].read assert_equal "This was dynamically expanded", @config.accessory(:mysql).files.keys[2].read
end end
test "directories" do
assert_equal({"$PWD/app-mysql/data"=>"/var/lib/mysql"}, @config.accessory(:mysql).directories)
end
end end

View File

@@ -17,13 +17,13 @@ accessories:
MYSQL_ROOT_HOST: '%' MYSQL_ROOT_HOST: '%'
secret: secret:
- MYSQL_ROOT_PASSWORD - MYSQL_ROOT_PASSWORD
volumes:
- /var/lib/mysql:/var/lib/mysql
files: files:
- test/fixtures/files/my.cnf:/etc/mysql/my.cnf - test/fixtures/files/my.cnf:/etc/mysql/my.cnf
directories:
- data:/var/lib/mysql
redis: redis:
image: redis:latest image: redis:latest
host: 1.1.1.4 host: 1.1.1.4
port: 6379 port: 6379
volumes: directories:
- /var/lib/redis:/data - data:/data