Compare commits

..

30 Commits

Author SHA1 Message Date
David Heinemeier Hansson
0111fcc4e4 Bump version for 0.3.0 2023-01-27 16:19:31 +01:00
David Heinemeier Hansson
407e1cc028 Protect accessory cli from missing accessory 2023-01-27 16:12:18 +01:00
David Heinemeier Hansson
f58e5e0935 Better error reporting and failure capture for build push 2023-01-27 15:56:07 +01:00
David Heinemeier Hansson
03fdb9a9ac Chain builder setup for better resiliency
Context may already exist while buildx does not
2023-01-27 15:41:28 +01:00
David Heinemeier Hansson
a5ebb30de2 Include accessories in main details 2023-01-27 15:20:27 +01:00
David Heinemeier Hansson
ec18a2a1c4 Tolerable error reporting 2023-01-27 15:04:27 +01:00
David Heinemeier Hansson
9af09256d9 Nicer output 2023-01-26 22:17:02 +01:00
David Heinemeier Hansson
29a8a52cef Execute over SSH too 2023-01-26 16:17:00 +01:00
David Heinemeier Hansson
de0a3f8ee8 Only catch what we can carry 2023-01-26 16:16:47 +01:00
David Heinemeier Hansson
08cac72475 Allow skipping master key 2023-01-24 13:19:12 +01:00
David Heinemeier Hansson
200f12a4a1 Single setup command 2023-01-23 14:13:17 +01:00
David Heinemeier Hansson
f0d88a5ffe Bootstrap accessory hosts too 2023-01-23 14:13:10 +01:00
David Heinemeier Hansson
d6a6f000f9 Inspect accessories too 2023-01-23 14:12:50 +01:00
David Heinemeier Hansson
15495fb48c Allow partial overwrites 2023-01-23 14:12:43 +01:00
David Heinemeier Hansson
05f84cdbef Makes it easier to resume remove 2023-01-23 14:12:27 +01:00
David Heinemeier Hansson
03488bc67a Add managed accessory directories 2023-01-23 13:36:47 +01:00
David Heinemeier Hansson
eceafbedf4 Better explaining variables 2023-01-23 12:50:44 +01:00
David Heinemeier Hansson
e1d518216a Add dynamic file expansion 2023-01-23 12:45:49 +01:00
David Heinemeier Hansson
52d10394f7 Ensure uploads are readable 2023-01-23 12:45:36 +01:00
David Heinemeier Hansson
ddf52da132 Add exec and bash commands to accessories 2023-01-23 12:45:20 +01:00
David Heinemeier Hansson
747e0fd4c2 Fix tests 2023-01-23 10:58:31 +01:00
David Heinemeier Hansson
6177673870 Get details on all accessories 2023-01-23 10:39:22 +01:00
David Heinemeier Hansson
78e50f23cd All boot/remove for all accessories 2023-01-23 10:38:03 +01:00
David Heinemeier Hansson
699f271e6e No need for protecting against re-invocation 2023-01-23 10:37:49 +01:00
David Heinemeier Hansson
148c43fe29 Extract make_directory_for 2023-01-23 10:37:19 +01:00
David Heinemeier Hansson
cd44014069 Commands should do all the actual work 2023-01-23 10:35:22 +01:00
David Heinemeier Hansson
1bcc65bc56 Must use absolute path 2023-01-23 10:04:55 +01:00
David Heinemeier Hansson
62cc986c54 Cleanup files directory too 2023-01-23 10:04:46 +01:00
David Heinemeier Hansson
7b1ffbfd6d Unify docs 2023-01-23 10:04:36 +01:00
David Heinemeier Hansson
8af7e48a90 Add file mapping to accessories 2023-01-23 09:43:57 +01:00
23 changed files with 409 additions and 82 deletions

View File

@@ -1,7 +1,7 @@
PATH
remote: .
specs:
mrsk (0.2.0)
mrsk (0.3.0)
activesupport (>= 7.0)
sshkit (~> 1.21)
thor (~> 1.2)

View File

@@ -244,6 +244,14 @@ ARG RUBY_VERSION
FROM ruby:$RUBY_VERSION-slim as base
```
### Using without RAILS_MASTER_KEY
If you're using MRSK with older Rails apps that predate RAILS_MASTER_KEY, or with a non-Rails app, you can skip the default usage and reference:
```yaml
skip_master_key: true
```
### Using accessories for database, cache, search services
You can manage your accessory services via MRSK as well. The services will build off public images, and will not be automatically updated when you deploy:

View File

@@ -1,5 +1,13 @@
#!/usr/bin/env ruby
# Prevent failures from being reported twice.
Thread.report_on_exception = false
require "mrsk/cli"
Mrsk::Cli::Main.start(ARGV)
begin
Mrsk::Cli::Main.start(ARGV)
rescue SSHKit::Runner::ExecuteError => e
puts " \e[31mERROR (#{e.cause.class}): #{e.cause.message}\e[0m"
puts e.cause.backtrace if ENV["VERBOSE"]
end

View File

@@ -1,41 +1,104 @@
require "mrsk/cli/base"
class Mrsk::Cli::Accessory < Mrsk::Cli::Base
desc "boot [NAME]", "Boot accessory service on host"
desc "boot [NAME]", "Boot accessory service on host (use NAME=all to boot all accessories)"
def boot(name)
accessory = MRSK.accessory(name)
on(accessory.host) { execute *accessory.run }
if name == "all"
MRSK.accessory_names.each { |accessory_name| boot(accessory_name) }
else
with_accessory(name) do |accessory|
directories(name)
upload(name)
on(accessory.host) { execute *accessory.run }
end
end
end
desc "upload [NAME]", "Upload accessory files to host"
def upload(name)
with_accessory(name) do |accessory|
on(accessory.host) do
accessory.files.each do |(local, remote)|
accessory.ensure_local_file_present(local)
execute *accessory.make_directory_for(remote)
upload! local, remote
execute :chmod, "755", remote
end
end
end
end
desc "directories [NAME]", "Create accessory directories on host"
def directories(name)
with_accessory(name) do |accessory|
on(accessory.host) do
accessory.directories.keys.each do |host_path|
execute *accessory.make_directory(host_path)
end
end
end
end
desc "reboot [NAME]", "Reboot accessory on host (stop container, remove container, start new container)"
def reboot(name)
invoke :stop, [ name ]
invoke :remove_container, [ name ]
invoke :boot, [ name ]
with_accessory(name) do |accessory|
stop(name)
remove_container(name)
boot(name)
end
end
desc "start [NAME]", "Start existing accessory on host"
def start(name)
accessory = MRSK.accessory(name)
on(accessory.host) { execute *accessory.start }
with_accessory(name) do |accessory|
on(accessory.host) { execute *accessory.start }
end
end
desc "stop [NAME]", "Stop accessory on host"
def stop(name)
accessory = MRSK.accessory(name)
on(accessory.host) { execute *accessory.stop }
with_accessory(name) do |accessory|
on(accessory.host) { execute *accessory.stop, raise_on_non_zero_exit: false }
end
end
desc "restart [NAME]", "Restart accessory on host"
def restart(name)
invoke :stop, [ name ]
invoke :start, [ name ]
with_accessory(name) do
stop(name)
start(name)
end
end
desc "details [NAME]", "Display details about accessory on host"
desc "details [NAME]", "Display details about accessory on host (use NAME=all to boot all accessories)"
def details(name)
accessory = MRSK.accessory(name)
on(accessory.host) { puts capture_with_info(*accessory.info) }
if name == "all"
MRSK.accessory_names.each { |accessory_name| details(accessory_name) }
else
with_accessory(name) do |accessory|
on(accessory.host) { puts capture_with_info(*accessory.info) }
end
end
end
desc "exec [NAME] [CMD]", "Execute a custom command on accessory host"
option :run, type: :boolean, default: false, desc: "Start a new container to run the command rather than reusing existing"
def exec(name, cmd)
with_accessory(name) do |accessory|
runner = options[:run] ? :run_exec : :exec
on(accessory.host) { |host| puts_by_host host, capture_with_info(*accessory.send(runner, cmd)) }
end
end
desc "bash [NAME]", "Start a bash session on primary host (or specific host set by --hosts)"
def bash(name)
with_accessory(name) do |accessory|
run_locally do
info "Launching bash session on #{accessory.host}"
exec accessory.bash(host: accessory.host)
end
end
end
desc "logs [NAME]", "Show log lines from accessory on host"
@@ -44,42 +107,75 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base
option :grep, aliases: "-g", desc: "Show lines with grep match only (use this to fetch specific requests by id)"
option :follow, aliases: "-f", desc: "Follow logs on primary server (or specific host set by --hosts)"
def logs(name)
accessory = MRSK.accessory(name)
with_accessory(name) do |accessory|
grep = options[:grep]
grep = options[:grep]
if options[:follow]
run_locally do
info "Following logs on #{accessory.host}..."
info accessory.follow_logs(grep: grep)
exec accessory.follow_logs(grep: grep)
end
else
since = options[:since]
lines = options[:lines]
if options[:follow]
run_locally do
info "Following logs on #{accessory.host}..."
info accessory.follow_logs(grep: grep)
exec accessory.follow_logs(grep: grep)
end
else
since = options[:since]
lines = options[:lines]
on(accessory.host) do
puts capture_with_info(*accessory.logs(since: since, lines: lines, grep: grep))
on(accessory.host) do
puts capture_with_info(*accessory.logs(since: since, lines: lines, grep: grep))
end
end
end
end
desc "remove [NAME]", "Remove accessory container and image from host"
desc "remove [NAME]", "Remove accessory container and image from host (use NAME=all to boot all accessories)"
def remove(name)
invoke :stop, [ name ]
invoke :remove_container, [ name ]
invoke :remove_image, [ name ]
if name == "all"
MRSK.accessory_names.each { |accessory_name| remove(accessory_name) }
else
with_accessory(name) do
stop(name)
remove_container(name)
remove_image(name)
remove_service_directory(name)
end
end
end
desc "remove_container [NAME]", "Remove accessory container from host"
def remove_container(name)
accessory = MRSK.accessory(name)
on(accessory.host) { execute *accessory.remove_container }
with_accessory(name) do |accessory|
on(accessory.host) { execute *accessory.remove_container }
end
end
desc "remove_container [NAME]", "Remove accessory image from servers"
desc "remove_container [NAME]", "Remove accessory image from host"
def remove_image(name)
accessory = MRSK.accessory(name)
on(accessory.host) { execute *accessory.remove_image }
with_accessory(name) do |accessory|
on(accessory.host) { execute *accessory.remove_image }
end
end
desc "remove_service_directory [NAME]", "Remove accessory directory used for uploaded files and data directories from host"
def remove_service_directory(name)
with_accessory(name) do |accessory|
on(accessory.host) { execute *accessory.remove_service_directory }
end
end
private
def with_accessory(name)
if accessory = MRSK.accessory(name)
yield accessory
else
error_on_missing_accessory(name)
end
end
def error_on_missing_accessory(name)
options = MRSK.accessory_names.presence
error \
"No accessory by the name of '#{name}'" +
(options ? " (options: #{options.to_sentence})" : "")
end
end

View File

@@ -40,10 +40,24 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
end
desc "exec [CMD]", "Execute a custom command on servers"
option :run, type: :boolean, default: false, desc: "Start a new container to run the command rather than reusing existing"
option :method, aliases: "-m", default: "exec", desc: "Execution method: [exec] perform inside app container / [run] perform in new container / [ssh] perform over ssh"
def exec(cmd)
runner = options[:run] ? :run_exec : :exec
on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.send(runner, cmd)) }
runner = \
case options[:method]
when "exec" then "exec"
when "run" then "run_exec"
when "ssh" then "exec_over_ssh"
else raise "Unknown method: #{options[:method]}"
end.inquiry
if runner.exec_over_ssh?
run_locally do
info "Launching command on #{MRSK.primary_host}"
exec MRSK.app.exec_over_ssh(cmd, host: MRSK.primary_host)
end
else
on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.send(runner, cmd)) }
end
end
desc "console", "Start Rails Console on primary host (or specific host set by --hosts)"

View File

@@ -10,14 +10,21 @@ class Mrsk::Cli::Build < Mrsk::Cli::Base
desc "push", "Build locally and push app image to registry"
def push
verbose = options[:verbose]
cli = self
run_locally do
begin
MRSK.verbosity(:debug) { execute *MRSK.builder.push }
rescue SSHKit::Command::Failed => e
error "Missing compatible builder, so creating a new one first"
execute *MRSK.builder.create
MRSK.verbosity(:debug) { execute *MRSK.builder.push }
if e.message =~ /(no builder)|(no such file or directory)/
error "Missing compatible builder, so creating a new one first"
if cli.create
MRSK.verbosity(:debug) { execute *MRSK.builder.push }
end
else
raise
end
end
end
end
@@ -30,8 +37,17 @@ class Mrsk::Cli::Build < Mrsk::Cli::Base
desc "create", "Create a local build setup"
def create
run_locally do
debug "Using builder: #{MRSK.builder.name}"
execute *MRSK.builder.create
begin
debug "Using builder: #{MRSK.builder.name}"
execute *MRSK.builder.create
rescue SSHKit::Command::Failed => e
if e.message =~ /stderr=(.*)/
error "Couldn't create remote builder: #{$1}"
false
else
raise
end
end
end
end

View File

@@ -9,6 +9,15 @@ require "mrsk/cli/server"
require "mrsk/cli/traefik"
class Mrsk::Cli::Main < Mrsk::Cli::Base
desc "setup", "Setup all accessories and deploy the app to servers"
def setup
print_runtime do
invoke "mrsk:cli:server:bootstrap"
invoke "mrsk:cli:accessory:boot", [ "all" ]
deploy
end
end
desc "deploy", "Deploy the app to servers"
def deploy
print_runtime do
@@ -43,12 +52,13 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base
def details
invoke "mrsk:cli:traefik:details"
invoke "mrsk:cli:app:details"
invoke "mrsk:cli:accessory:details", [ "all" ]
end
desc "config", "Show combined config"
def config
run_locally do
pp MRSK.config.to_h
puts MRSK.config.to_h.to_yaml
end
end

View File

@@ -3,6 +3,6 @@ require "mrsk/cli/base"
class Mrsk::Cli::Server < Mrsk::Cli::Base
desc "bootstrap", "Ensure Docker is installed on the servers"
def bootstrap
on(MRSK.hosts) { execute "which docker || (apt-get update -y && apt-get install docker.io -y)" }
on(MRSK.hosts + MRSK.accessory_hosts) { execute "which docker || (apt-get update -y && apt-get install docker.io -y)" }
end
end

View File

@@ -48,6 +48,10 @@ class Mrsk::Commander
specific_hosts || config.accessories.collect(&:host)
end
def accessory_names
config.accessories&.collect(&:name) || []
end
def app
@app ||= Mrsk::Commands::App.new(config)
@@ -70,7 +74,7 @@ class Mrsk::Commander
end
def accessory(name)
(@accessories ||= {})[name] ||= Mrsk::Commands::Accessory.new(config, name: name)
config.accessories.detect { |a| a.name == name }
end

View File

@@ -2,7 +2,7 @@ require "mrsk/commands/base"
class Mrsk::Commands::Accessory < Mrsk::Commands::Base
attr_reader :accessory_config
delegate :service_name, :image, :host, :port, :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:)
super(config)
@@ -46,6 +46,47 @@ class Mrsk::Commands::Accessory < Mrsk::Commands::Base
).join(" "), host: host
end
def exec(*command, interactive: false)
docker :exec,
("-it" if interactive),
*env_args,
*volume_args,
service_name,
*command
end
def run_exec(*command, interactive: false)
docker :run,
("-it" if interactive),
"--rm",
*env_args,
*volume_args,
image,
*command
end
def bash(host:)
exec_over_ssh "bash", host: host
end
def ensure_local_file_present(local_file)
if !local_file.is_a?(StringIO) && !Pathname.new(local_file).exist?
raise "Missing file: #{local_file}"
end
end
def make_directory_for(remote_file)
make_directory Pathname.new(remote_file).dirname.to_s
end
def make_directory(path)
[ :mkdir, "-p", path ]
end
def remove_service_directory
[ :rm, "-rf", service_name ]
end
def remove_container
docker :container, :prune, "-f", *service_filter
end
@@ -55,6 +96,10 @@ class Mrsk::Commands::Accessory < Mrsk::Commands::Base
end
private
def exec_over_ssh(*command, host:)
run_over_ssh run_exec(*command, interactive: true).join(" "), host: host
end
def service_filter
[ "--filter", "label=service=#{service_name}" ]
end

View File

@@ -60,6 +60,10 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
*command
end
def exec_over_ssh(*command, host:)
run_over_ssh run_exec(*command, interactive: true).join(" "), host: host
end
def follow_logs(host:, grep: nil)
run_over_ssh pipe(
current_container_id,
@@ -89,15 +93,15 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
end
private
def exec_over_ssh(*command, host:)
run_over_ssh run_exec(*command, interactive: true).join(" "), host: host
end
def service_filter
[ "--filter", "label=service=#{config.service}" ]
end
def rails_master_key_arg
[ "-e", redact("RAILS_MASTER_KEY=#{config.master_key}") ]
if master_key = config.master_key
[ "-e", redact("RAILS_MASTER_KEY=#{master_key}") ]
else
[]
end
end
end

View File

@@ -16,6 +16,10 @@ module Mrsk::Commands
.tap { |commands| commands.pop } # Remove trailing combiner
end
def chain(*commands)
combine *commands, by: ";"
end
def pipe(*commands)
combine *commands, by: "|"
end

View File

@@ -2,13 +2,13 @@ require "mrsk/commands/builder/native"
class Mrsk::Commands::Builder::Native::Remote < Mrsk::Commands::Builder::Native
def create
combine \
chain \
create_context,
create_buildx
end
def remove
combine \
chain \
remove_context,
remove_buildx
end
@@ -25,7 +25,7 @@ class Mrsk::Commands::Builder::Native::Remote < Mrsk::Commands::Builder::Native
end
def info
combine \
chain \
docker(:context, :ls),
docker(:buildx, :ls)
end

View File

@@ -15,7 +15,7 @@ class Mrsk::Configuration
def create_from(base_config_file, destination: nil, version: "missing")
new(load_config_file(base_config_file).tap do |config|
if destination
config.merge! \
config.deep_merge! \
load_config_file destination_config_file(base_config_file, destination)
end
end, version: version)
@@ -52,7 +52,7 @@ class Mrsk::Configuration
end
def accessories
@accessories ||= raw_config.accessories.keys.collect { |name| Mrsk::Configuration::Assessory.new(name, config: self) }
@accessories ||= raw_config.accessories&.keys&.collect { |name| Mrsk::Configuration::Assessory.new(name, config: self) } || []
end
def accessory(name)
@@ -115,7 +115,9 @@ class Mrsk::Configuration
end
def master_key
ENV["RAILS_MASTER_KEY"] || File.read(Pathname.new(File.expand_path("config/master.key")))
unless raw_config.skip_master_key
ENV["RAILS_MASTER_KEY"] || File.read(Pathname.new(File.expand_path("config/master.key")))
end
end
def to_h
@@ -130,7 +132,8 @@ class Mrsk::Configuration
env_args: env_args,
volume_args: volume_args,
ssh_options: ssh_options,
builder: raw_config.builder
builder: raw_config.builder,
accessories: raw_config.accessories
}.compact
end

View File

@@ -43,8 +43,22 @@ class Mrsk::Configuration::Assessory
argumentize_env_with_secrets env
end
def files
specifics["files"]&.to_h do |local_to_remote_mapping|
local_file, remote_file = local_to_remote_mapping.split(":")
[ expand_local_file(local_file), expand_remote_file(remote_file) ]
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
specifics["volumes"] || []
specific_volumes + remote_files_as_volumes + remote_directories_as_volumes
end
def volume_args
@@ -57,4 +71,46 @@ class Mrsk::Configuration::Assessory
def default_labels
{ "service" => service_name }
end
def expand_local_file(local_file)
if local_file.end_with?("erb")
read_dynamic_file(local_file)
else
Pathname.new(File.expand_path(local_file)).to_s
end
end
def read_dynamic_file(local_file)
StringIO.new(ERB.new(IO.read(local_file)).result)
end
def expand_remote_file(remote_file)
service_name + remote_file
end
def specific_volumes
specifics["volumes"] || []
end
def remote_files_as_volumes
specifics["files"]&.collect do |local_to_remote_mapping|
_, remote_file = local_to_remote_mapping.split(":")
"#{service_data_directory + remote_file}:#{remote_file}"
end || []
end
def remote_directories_as_volumes
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

View File

@@ -1,3 +1,3 @@
module Mrsk
VERSION = "0.2.0"
VERSION = "0.3.0"
end

View File

@@ -5,13 +5,30 @@ require "mrsk/cli"
class CliAccessoryTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Stream
test "boot" do
ENV["MYSQL_ROOT_PASSWORD"] = "secret123"
setup { ENV["MYSQL_ROOT_PASSWORD"] = "secret123" }
teardown { ENV["MYSQL_ROOT_PASSWORD"] = nil }
test "upload" do
command = stdouted { Mrsk::Cli::Accessory.start(["upload", "mysql", "-c", "test/fixtures/deploy_with_accessories.yml"]) }
assert_match "test/fixtures/files/my.cnf app-mysql/etc/mysql/my.cnf", command
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
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 --label service=app-mysql mysql:5.7 on 1.1.1.3", command
ensure
ENV["MYSQL_ROOT_PASSWORD"] = nil
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

View File

@@ -2,14 +2,18 @@ require "test_helper"
require "mrsk/configuration"
require "mrsk/commands/app"
ENV["RAILS_MASTER_KEY"] = "456"
class CommandsAppTest < ActiveSupport::TestCase
setup do
ENV["RAILS_MASTER_KEY"] = "456"
@config = { service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" }, servers: [ "1.1.1.1" ] }
@app = Mrsk::Commands::App.new Mrsk::Configuration.new(@config)
end
teardown do
ENV["RAILS_MASTER_KEY"] = nil
end
test "run" do
assert_equal \
[:docker, :run, "-d", "--restart unless-stopped", "--name", "app-missing", "-e", "RAILS_MASTER_KEY=456", "--label", "service=app", "--label", "role=web", "--label", "traefik.http.routers.app.rule='PathPrefix(`/`)'", "--label", "traefik.http.services.app.loadbalancer.healthcheck.path=/up", "--label", "traefik.http.services.app.loadbalancer.healthcheck.interval=1s", "--label", "traefik.http.middlewares.app.retry.attempts=3", "--label", "traefik.http.middlewares.app.retry.initialinterval=500ms", "dhh/app:missing"], @app.run
@@ -27,4 +31,11 @@ class CommandsAppTest < ActiveSupport::TestCase
[ :docker, :run, "--rm", "-e", "RAILS_MASTER_KEY=456", "dhh/app:missing", "bin/rails", "db:setup" ],
@app.run_exec("bin/rails", "db:setup")
end
test "run without master key" do
ENV["RAILS_MASTER_KEY"] = nil
@app = Mrsk::Commands::App.new Mrsk::Configuration.new(@config.tap { |c| c[:skip_master_key] = true })
assert @app.run.exclude?("RAILS_MASTER_KEY=456")
end
end

View File

@@ -18,8 +18,15 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase
},
"secret" => [
"MYSQL_ROOT_PASSWORD"
]
}
],
},
"files" => [
"config/mysql/my.cnf:/etc/mysql/my.cnf",
"db/structure.sql:/docker-entrypoint-initdb.d/structure.sql"
],
"directories" => [
"data:/var/lib/mysql"
]
},
"redis" => {
"image" => "redis:latest",
@@ -83,7 +90,18 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase
end
test "volume args" do
assert_equal [], @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
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
test "directories" do
assert_equal({"$PWD/app-mysql/data"=>"/var/lib/mysql"}, @config.accessory(:mysql).directories)
end
end

View File

@@ -1,10 +1,10 @@
require "test_helper"
require "mrsk/configuration"
ENV["RAILS_MASTER_KEY"] = "456"
class ConfigurationTest < ActiveSupport::TestCase
setup do
ENV["RAILS_MASTER_KEY"] = "456"
@deploy = {
service: "app", image: "dhh/app",
registry: { "username" => "dhh", "password" => "secret" },
@@ -21,6 +21,10 @@ class ConfigurationTest < ActiveSupport::TestCase
@config_with_roles = Mrsk::Configuration.new(@deploy_with_roles)
end
teardown do
ENV["RAILS_MASTER_KEY"] = nil
end
test "ensure valid keys" do
assert_raise(ArgumentError) do
Mrsk::Configuration.new(@deploy.tap { _1.delete(:service) })
@@ -134,6 +138,11 @@ class ConfigurationTest < ActiveSupport::TestCase
assert_equal "456", @config.master_key
end
test "skip master key" do
config = Mrsk::Configuration.new(@deploy.tap { |c| c[:skip_master_key] = true })
assert_nil @config.master_key
end
test "volume_args" do
assert_equal ["--volume", "/local/path:/container/path"], @config.volume_args
end

View File

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

1
test/fixtures/files/my.cnf vendored Normal file
View File

@@ -0,0 +1 @@
# MySQL Config

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

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