Bring accessory execution in line with app
This commit is contained in:
@@ -33,6 +33,7 @@ class Mrsk::Commands::Accessory < Mrsk::Commands::Base
|
|||||||
docker :ps, *service_filter
|
docker :ps, *service_filter
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def logs(since: nil, lines: nil, grep: nil)
|
def logs(since: nil, lines: nil, grep: nil)
|
||||||
pipe \
|
pipe \
|
||||||
docker(:logs, service_name, (" --since #{since}" if since), (" -n #{lines}" if lines), "-t", "2>&1"),
|
docker(:logs, service_name, (" --since #{since}" if since), (" -n #{lines}" if lines), "-t", "2>&1"),
|
||||||
@@ -46,14 +47,15 @@ class Mrsk::Commands::Accessory < Mrsk::Commands::Base
|
|||||||
).join(" ")
|
).join(" ")
|
||||||
end
|
end
|
||||||
|
|
||||||
def exec(*command, interactive: false)
|
|
||||||
|
def execute_in_existing_container(*command, interactive: false)
|
||||||
docker :exec,
|
docker :exec,
|
||||||
("-it" if interactive),
|
("-it" if interactive),
|
||||||
service_name,
|
service_name,
|
||||||
*command
|
*command
|
||||||
end
|
end
|
||||||
|
|
||||||
def run_exec(*command, interactive: false)
|
def execute_in_new_container(*command, interactive: false)
|
||||||
docker :run,
|
docker :run,
|
||||||
("-it" if interactive),
|
("-it" if interactive),
|
||||||
"--rm",
|
"--rm",
|
||||||
@@ -63,17 +65,18 @@ class Mrsk::Commands::Accessory < Mrsk::Commands::Base
|
|||||||
*command
|
*command
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def execute_in_existing_container_over_ssh(*command)
|
||||||
|
run_over_ssh execute_in_existing_container(*command, interactive: true).join(" "), host: host
|
||||||
|
end
|
||||||
|
|
||||||
|
def execute_in_new_container_over_ssh(*command)
|
||||||
|
run_over_ssh execute_in_new_container(*command, interactive: true).join(" "), host: host
|
||||||
|
end
|
||||||
|
|
||||||
def run_over_ssh(command)
|
def run_over_ssh(command)
|
||||||
super command, host: host
|
super command, host: host
|
||||||
end
|
end
|
||||||
|
|
||||||
def exec_over_ssh(*command)
|
|
||||||
run_over_ssh run_exec(*command, interactive: true).join(" ")
|
|
||||||
end
|
|
||||||
|
|
||||||
def bash
|
|
||||||
exec_over_ssh "bash"
|
|
||||||
end
|
|
||||||
|
|
||||||
def ensure_local_file_present(local_file)
|
def ensure_local_file_present(local_file)
|
||||||
if !local_file.is_a?(StringIO) && !Pathname.new(local_file).exist?
|
if !local_file.is_a?(StringIO) && !Pathname.new(local_file).exist?
|
||||||
|
|||||||
@@ -41,18 +41,20 @@ class CommandsAccessoryTest < ActiveSupport::TestCase
|
|||||||
@config = Mrsk::Configuration.new(@config)
|
@config = Mrsk::Configuration.new(@config)
|
||||||
@mysql = Mrsk::Commands::Accessory.new(@config, name: :mysql)
|
@mysql = Mrsk::Commands::Accessory.new(@config, name: :mysql)
|
||||||
@redis = Mrsk::Commands::Accessory.new(@config, name: :redis)
|
@redis = Mrsk::Commands::Accessory.new(@config, name: :redis)
|
||||||
|
|
||||||
|
ENV["MYSQL_ROOT_PASSWORD"] = "secret123"
|
||||||
|
end
|
||||||
|
|
||||||
|
teardown do
|
||||||
|
ENV.delete("MYSQL_ROOT_PASSWORD")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "run" do
|
test "run" do
|
||||||
ENV["MYSQL_ROOT_PASSWORD"] = "secret123"
|
|
||||||
|
|
||||||
assert_equal \
|
assert_equal \
|
||||||
[:docker, :run, "--name", "app-mysql", "-d", "--restart", "unless-stopped", "-p", "3306:3306", "-e", "MYSQL_ROOT_PASSWORD=secret123", "-e", "MYSQL_ROOT_HOST=%", "--label", "service=app-mysql", "mysql:8.0"], @mysql.run
|
[:docker, :run, "--name", "app-mysql", "-d", "--restart", "unless-stopped", "-p", "3306:3306", "-e", "MYSQL_ROOT_PASSWORD=secret123", "-e", "MYSQL_ROOT_HOST=%", "--label", "service=app-mysql", "mysql:8.0"], @mysql.run
|
||||||
|
|
||||||
assert_equal \
|
assert_equal \
|
||||||
[:docker, :run, "--name", "app-redis", "-d", "--restart", "unless-stopped", "-p", "6379:6379", "-e", "SOMETHING=else", "--volume", "/var/lib/redis:/data", "--label", "service=app-redis", "--label", "cache=true", "redis:latest"], @redis.run
|
[:docker, :run, "--name", "app-redis", "-d", "--restart", "unless-stopped", "-p", "6379:6379", "-e", "SOMETHING=else", "--volume", "/var/lib/redis:/data", "--label", "service=app-redis", "--label", "cache=true", "redis:latest"], @redis.run
|
||||||
ensure
|
|
||||||
ENV["MYSQL_ROOT_PASSWORD"] = nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "start" do
|
test "start" do
|
||||||
@@ -67,6 +69,35 @@ class CommandsAccessoryTest < ActiveSupport::TestCase
|
|||||||
assert_equal [:docker, :ps, "--filter", "label=service=app-mysql"], @mysql.info
|
assert_equal [:docker, :ps, "--filter", "label=service=app-mysql"], @mysql.info
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
test "execute in new container" do
|
||||||
|
assert_equal \
|
||||||
|
[ :docker, :run, "--rm", "-e", "MYSQL_ROOT_PASSWORD=secret123", "-e", "MYSQL_ROOT_HOST=%", "mysql:8.0", "mysql", "-u", "root" ],
|
||||||
|
@mysql.execute_in_new_container("mysql", "-u", "root")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "execute in existing container" do
|
||||||
|
assert_equal \
|
||||||
|
[ :docker, :exec, "app-mysql", "mysql", "-u", "root" ],
|
||||||
|
@mysql.execute_in_existing_container("mysql", "-u", "root")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "execute in new container over ssh" do
|
||||||
|
@mysql.stub(:run_over_ssh, ->(cmd, host:) { cmd }) do
|
||||||
|
assert_match %r|docker run -it --rm -e MYSQL_ROOT_PASSWORD=secret123 -e MYSQL_ROOT_HOST=% mysql:8.0 mysql -u root|,
|
||||||
|
@mysql.execute_in_new_container_over_ssh("mysql", "-u", "root")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "execute in existing container over ssh" do
|
||||||
|
@mysql.stub(:run_over_ssh, ->(cmd, host:) { cmd }) do
|
||||||
|
assert_match %r|docker exec -it app-mysql mysql -u root|,
|
||||||
|
@mysql.execute_in_existing_container_over_ssh("mysql", "-u", "root")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
test "logs" do
|
test "logs" do
|
||||||
assert_equal [:docker, :logs, "app-mysql", "-t", "2>&1"], @mysql.logs
|
assert_equal [:docker, :logs, "app-mysql", "-t", "2>&1"], @mysql.logs
|
||||||
assert_equal [:docker, :logs, "app-mysql", " --since 5m", " -n 100", "-t", "2>&1", "|", "grep 'thing'"], @mysql.logs(since: "5m", lines: 100, grep: "thing")
|
assert_equal [:docker, :logs, "app-mysql", " --since 5m", " -n 100", "-t", "2>&1", "|", "grep 'thing'"], @mysql.logs(since: "5m", lines: 100, grep: "thing")
|
||||||
|
|||||||
Reference in New Issue
Block a user