Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0111fcc4e4 | ||
|
|
407e1cc028 | ||
|
|
f58e5e0935 | ||
|
|
03fdb9a9ac | ||
|
|
a5ebb30de2 | ||
|
|
ec18a2a1c4 | ||
|
|
9af09256d9 | ||
|
|
29a8a52cef | ||
|
|
de0a3f8ee8 | ||
|
|
08cac72475 | ||
|
|
200f12a4a1 | ||
|
|
f0d88a5ffe | ||
|
|
d6a6f000f9 | ||
|
|
15495fb48c | ||
|
|
05f84cdbef | ||
|
|
03488bc67a | ||
|
|
eceafbedf4 | ||
|
|
e1d518216a | ||
|
|
52d10394f7 | ||
|
|
ddf52da132 | ||
|
|
747e0fd4c2 | ||
|
|
6177673870 | ||
|
|
78e50f23cd | ||
|
|
699f271e6e | ||
|
|
148c43fe29 | ||
|
|
cd44014069 | ||
|
|
1bcc65bc56 | ||
|
|
62cc986c54 | ||
|
|
7b1ffbfd6d | ||
|
|
8af7e48a90 |
@@ -1,7 +1,7 @@
|
|||||||
PATH
|
PATH
|
||||||
remote: .
|
remote: .
|
||||||
specs:
|
specs:
|
||||||
mrsk (0.2.0)
|
mrsk (0.3.0)
|
||||||
activesupport (>= 7.0)
|
activesupport (>= 7.0)
|
||||||
sshkit (~> 1.21)
|
sshkit (~> 1.21)
|
||||||
thor (~> 1.2)
|
thor (~> 1.2)
|
||||||
|
|||||||
@@ -244,6 +244,14 @@ ARG RUBY_VERSION
|
|||||||
FROM ruby:$RUBY_VERSION-slim as base
|
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
|
### 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:
|
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:
|
||||||
|
|||||||
8
bin/mrsk
8
bin/mrsk
@@ -1,5 +1,13 @@
|
|||||||
#!/usr/bin/env ruby
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
|
# Prevent failures from being reported twice.
|
||||||
|
Thread.report_on_exception = false
|
||||||
|
|
||||||
require "mrsk/cli"
|
require "mrsk/cli"
|
||||||
|
|
||||||
|
begin
|
||||||
Mrsk::Cli::Main.start(ARGV)
|
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
|
||||||
|
|||||||
@@ -1,42 +1,105 @@
|
|||||||
require "mrsk/cli/base"
|
require "mrsk/cli/base"
|
||||||
|
|
||||||
class Mrsk::Cli::Accessory < 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)
|
def boot(name)
|
||||||
accessory = MRSK.accessory(name)
|
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 }
|
on(accessory.host) { execute *accessory.run }
|
||||||
end
|
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)"
|
desc "reboot [NAME]", "Reboot accessory on host (stop container, remove container, start new container)"
|
||||||
def reboot(name)
|
def reboot(name)
|
||||||
invoke :stop, [ name ]
|
with_accessory(name) do |accessory|
|
||||||
invoke :remove_container, [ name ]
|
stop(name)
|
||||||
invoke :boot, [ name ]
|
remove_container(name)
|
||||||
|
boot(name)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "start [NAME]", "Start existing accessory on host"
|
desc "start [NAME]", "Start existing accessory on host"
|
||||||
def start(name)
|
def start(name)
|
||||||
accessory = MRSK.accessory(name)
|
with_accessory(name) do |accessory|
|
||||||
on(accessory.host) { execute *accessory.start }
|
on(accessory.host) { execute *accessory.start }
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
desc "stop [NAME]", "Stop accessory on host"
|
desc "stop [NAME]", "Stop accessory on host"
|
||||||
def stop(name)
|
def stop(name)
|
||||||
accessory = MRSK.accessory(name)
|
with_accessory(name) do |accessory|
|
||||||
on(accessory.host) { execute *accessory.stop }
|
on(accessory.host) { execute *accessory.stop, raise_on_non_zero_exit: false }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "restart [NAME]", "Restart accessory on host"
|
desc "restart [NAME]", "Restart accessory on host"
|
||||||
def restart(name)
|
def restart(name)
|
||||||
invoke :stop, [ name ]
|
with_accessory(name) do
|
||||||
invoke :start, [ name ]
|
stop(name)
|
||||||
|
start(name)
|
||||||
|
end
|
||||||
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)
|
def details(name)
|
||||||
accessory = MRSK.accessory(name)
|
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) }
|
on(accessory.host) { puts capture_with_info(*accessory.info) }
|
||||||
end
|
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"
|
desc "logs [NAME]", "Show log lines from accessory on host"
|
||||||
option :since, aliases: "-s", desc: "Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)"
|
option :since, aliases: "-s", desc: "Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)"
|
||||||
@@ -44,8 +107,7 @@ 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 :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)"
|
option :follow, aliases: "-f", desc: "Follow logs on primary server (or specific host set by --hosts)"
|
||||||
def logs(name)
|
def logs(name)
|
||||||
accessory = MRSK.accessory(name)
|
with_accessory(name) do |accessory|
|
||||||
|
|
||||||
grep = options[:grep]
|
grep = options[:grep]
|
||||||
|
|
||||||
if options[:follow]
|
if options[:follow]
|
||||||
@@ -63,23 +125,57 @@ class Mrsk::Cli::Accessory < Mrsk::Cli::Base
|
|||||||
end
|
end
|
||||||
end
|
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)
|
def remove(name)
|
||||||
invoke :stop, [ name ]
|
if name == "all"
|
||||||
invoke :remove_container, [ name ]
|
MRSK.accessory_names.each { |accessory_name| remove(accessory_name) }
|
||||||
invoke :remove_image, [ name ]
|
else
|
||||||
|
with_accessory(name) do
|
||||||
|
stop(name)
|
||||||
|
remove_container(name)
|
||||||
|
remove_image(name)
|
||||||
|
remove_service_directory(name)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "remove_container [NAME]", "Remove accessory container from host"
|
desc "remove_container [NAME]", "Remove accessory container from host"
|
||||||
def remove_container(name)
|
def remove_container(name)
|
||||||
accessory = MRSK.accessory(name)
|
with_accessory(name) do |accessory|
|
||||||
on(accessory.host) { execute *accessory.remove_container }
|
on(accessory.host) { execute *accessory.remove_container }
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
desc "remove_container [NAME]", "Remove accessory image from servers"
|
desc "remove_container [NAME]", "Remove accessory image from host"
|
||||||
def remove_image(name)
|
def remove_image(name)
|
||||||
accessory = MRSK.accessory(name)
|
with_accessory(name) do |accessory|
|
||||||
on(accessory.host) { execute *accessory.remove_image }
|
on(accessory.host) { execute *accessory.remove_image }
|
||||||
end
|
end
|
||||||
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
|
||||||
|
|||||||
@@ -40,11 +40,25 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
desc "exec [CMD]", "Execute a custom command on servers"
|
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)
|
def exec(cmd)
|
||||||
runner = options[:run] ? :run_exec : :exec
|
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)) }
|
on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.send(runner, cmd)) }
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
desc "console", "Start Rails Console on primary host (or specific host set by --hosts)"
|
desc "console", "Start Rails Console on primary host (or specific host set by --hosts)"
|
||||||
def console
|
def console
|
||||||
|
|||||||
@@ -10,15 +10,22 @@ class Mrsk::Cli::Build < Mrsk::Cli::Base
|
|||||||
desc "push", "Build locally and push app image to registry"
|
desc "push", "Build locally and push app image to registry"
|
||||||
def push
|
def push
|
||||||
verbose = options[:verbose]
|
verbose = options[:verbose]
|
||||||
|
cli = self
|
||||||
|
|
||||||
run_locally do
|
run_locally do
|
||||||
begin
|
begin
|
||||||
MRSK.verbosity(:debug) { execute *MRSK.builder.push }
|
MRSK.verbosity(:debug) { execute *MRSK.builder.push }
|
||||||
rescue SSHKit::Command::Failed => e
|
rescue SSHKit::Command::Failed => e
|
||||||
|
if e.message =~ /(no builder)|(no such file or directory)/
|
||||||
error "Missing compatible builder, so creating a new one first"
|
error "Missing compatible builder, so creating a new one first"
|
||||||
execute *MRSK.builder.create
|
|
||||||
|
if cli.create
|
||||||
MRSK.verbosity(:debug) { execute *MRSK.builder.push }
|
MRSK.verbosity(:debug) { execute *MRSK.builder.push }
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
raise
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -30,8 +37,17 @@ class Mrsk::Cli::Build < Mrsk::Cli::Base
|
|||||||
desc "create", "Create a local build setup"
|
desc "create", "Create a local build setup"
|
||||||
def create
|
def create
|
||||||
run_locally do
|
run_locally do
|
||||||
|
begin
|
||||||
debug "Using builder: #{MRSK.builder.name}"
|
debug "Using builder: #{MRSK.builder.name}"
|
||||||
execute *MRSK.builder.create
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,15 @@ require "mrsk/cli/server"
|
|||||||
require "mrsk/cli/traefik"
|
require "mrsk/cli/traefik"
|
||||||
|
|
||||||
class Mrsk::Cli::Main < Mrsk::Cli::Base
|
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"
|
desc "deploy", "Deploy the app to servers"
|
||||||
def deploy
|
def deploy
|
||||||
print_runtime do
|
print_runtime do
|
||||||
@@ -43,12 +52,13 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base
|
|||||||
def details
|
def details
|
||||||
invoke "mrsk:cli:traefik:details"
|
invoke "mrsk:cli:traefik:details"
|
||||||
invoke "mrsk:cli:app:details"
|
invoke "mrsk:cli:app:details"
|
||||||
|
invoke "mrsk:cli:accessory:details", [ "all" ]
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "config", "Show combined config"
|
desc "config", "Show combined config"
|
||||||
def config
|
def config
|
||||||
run_locally do
|
run_locally do
|
||||||
pp MRSK.config.to_h
|
puts MRSK.config.to_h.to_yaml
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,6 @@ require "mrsk/cli/base"
|
|||||||
class Mrsk::Cli::Server < Mrsk::Cli::Base
|
class Mrsk::Cli::Server < Mrsk::Cli::Base
|
||||||
desc "bootstrap", "Ensure Docker is installed on the servers"
|
desc "bootstrap", "Ensure Docker is installed on the servers"
|
||||||
def bootstrap
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -48,6 +48,10 @@ class Mrsk::Commander
|
|||||||
specific_hosts || config.accessories.collect(&:host)
|
specific_hosts || config.accessories.collect(&:host)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def accessory_names
|
||||||
|
config.accessories&.collect(&:name) || []
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
def app
|
def app
|
||||||
@app ||= Mrsk::Commands::App.new(config)
|
@app ||= Mrsk::Commands::App.new(config)
|
||||||
@@ -70,7 +74,7 @@ class Mrsk::Commander
|
|||||||
end
|
end
|
||||||
|
|
||||||
def accessory(name)
|
def accessory(name)
|
||||||
(@accessories ||= {})[name] ||= Mrsk::Commands::Accessory.new(config, name: name)
|
config.accessories.detect { |a| a.name == name }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -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, :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)
|
||||||
@@ -46,6 +46,47 @@ class Mrsk::Commands::Accessory < Mrsk::Commands::Base
|
|||||||
).join(" "), host: host
|
).join(" "), host: host
|
||||||
end
|
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
|
def remove_container
|
||||||
docker :container, :prune, "-f", *service_filter
|
docker :container, :prune, "-f", *service_filter
|
||||||
end
|
end
|
||||||
@@ -55,6 +96,10 @@ class Mrsk::Commands::Accessory < Mrsk::Commands::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
def exec_over_ssh(*command, host:)
|
||||||
|
run_over_ssh run_exec(*command, interactive: true).join(" "), host: host
|
||||||
|
end
|
||||||
|
|
||||||
def service_filter
|
def service_filter
|
||||||
[ "--filter", "label=service=#{service_name}" ]
|
[ "--filter", "label=service=#{service_name}" ]
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -60,6 +60,10 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
|||||||
*command
|
*command
|
||||||
end
|
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)
|
def follow_logs(host:, grep: nil)
|
||||||
run_over_ssh pipe(
|
run_over_ssh pipe(
|
||||||
current_container_id,
|
current_container_id,
|
||||||
@@ -89,15 +93,15 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def exec_over_ssh(*command, host:)
|
|
||||||
run_over_ssh run_exec(*command, interactive: true).join(" "), host: host
|
|
||||||
end
|
|
||||||
|
|
||||||
def service_filter
|
def service_filter
|
||||||
[ "--filter", "label=service=#{config.service}" ]
|
[ "--filter", "label=service=#{config.service}" ]
|
||||||
end
|
end
|
||||||
|
|
||||||
def rails_master_key_arg
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -16,6 +16,10 @@ module Mrsk::Commands
|
|||||||
.tap { |commands| commands.pop } # Remove trailing combiner
|
.tap { |commands| commands.pop } # Remove trailing combiner
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def chain(*commands)
|
||||||
|
combine *commands, by: ";"
|
||||||
|
end
|
||||||
|
|
||||||
def pipe(*commands)
|
def pipe(*commands)
|
||||||
combine *commands, by: "|"
|
combine *commands, by: "|"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2,13 +2,13 @@ require "mrsk/commands/builder/native"
|
|||||||
|
|
||||||
class Mrsk::Commands::Builder::Native::Remote < Mrsk::Commands::Builder::Native
|
class Mrsk::Commands::Builder::Native::Remote < Mrsk::Commands::Builder::Native
|
||||||
def create
|
def create
|
||||||
combine \
|
chain \
|
||||||
create_context,
|
create_context,
|
||||||
create_buildx
|
create_buildx
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove
|
def remove
|
||||||
combine \
|
chain \
|
||||||
remove_context,
|
remove_context,
|
||||||
remove_buildx
|
remove_buildx
|
||||||
end
|
end
|
||||||
@@ -25,7 +25,7 @@ class Mrsk::Commands::Builder::Native::Remote < Mrsk::Commands::Builder::Native
|
|||||||
end
|
end
|
||||||
|
|
||||||
def info
|
def info
|
||||||
combine \
|
chain \
|
||||||
docker(:context, :ls),
|
docker(:context, :ls),
|
||||||
docker(:buildx, :ls)
|
docker(:buildx, :ls)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ class Mrsk::Configuration
|
|||||||
def create_from(base_config_file, destination: nil, version: "missing")
|
def create_from(base_config_file, destination: nil, version: "missing")
|
||||||
new(load_config_file(base_config_file).tap do |config|
|
new(load_config_file(base_config_file).tap do |config|
|
||||||
if destination
|
if destination
|
||||||
config.merge! \
|
config.deep_merge! \
|
||||||
load_config_file destination_config_file(base_config_file, destination)
|
load_config_file destination_config_file(base_config_file, destination)
|
||||||
end
|
end
|
||||||
end, version: version)
|
end, version: version)
|
||||||
@@ -52,7 +52,7 @@ class Mrsk::Configuration
|
|||||||
end
|
end
|
||||||
|
|
||||||
def accessories
|
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
|
end
|
||||||
|
|
||||||
def accessory(name)
|
def accessory(name)
|
||||||
@@ -115,8 +115,10 @@ class Mrsk::Configuration
|
|||||||
end
|
end
|
||||||
|
|
||||||
def master_key
|
def master_key
|
||||||
|
unless raw_config.skip_master_key
|
||||||
ENV["RAILS_MASTER_KEY"] || File.read(Pathname.new(File.expand_path("config/master.key")))
|
ENV["RAILS_MASTER_KEY"] || File.read(Pathname.new(File.expand_path("config/master.key")))
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def to_h
|
def to_h
|
||||||
{
|
{
|
||||||
@@ -130,7 +132,8 @@ class Mrsk::Configuration
|
|||||||
env_args: env_args,
|
env_args: env_args,
|
||||||
volume_args: volume_args,
|
volume_args: volume_args,
|
||||||
ssh_options: ssh_options,
|
ssh_options: ssh_options,
|
||||||
builder: raw_config.builder
|
builder: raw_config.builder,
|
||||||
|
accessories: raw_config.accessories
|
||||||
}.compact
|
}.compact
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -43,8 +43,22 @@ class Mrsk::Configuration::Assessory
|
|||||||
argumentize_env_with_secrets env
|
argumentize_env_with_secrets env
|
||||||
end
|
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
|
def volumes
|
||||||
specifics["volumes"] || []
|
specific_volumes + remote_files_as_volumes + remote_directories_as_volumes
|
||||||
end
|
end
|
||||||
|
|
||||||
def volume_args
|
def volume_args
|
||||||
@@ -57,4 +71,46 @@ class Mrsk::Configuration::Assessory
|
|||||||
def default_labels
|
def default_labels
|
||||||
{ "service" => service_name }
|
{ "service" => service_name }
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
module Mrsk
|
module Mrsk
|
||||||
VERSION = "0.2.0"
|
VERSION = "0.3.0"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -5,13 +5,30 @@ require "mrsk/cli"
|
|||||||
class CliAccessoryTest < ActiveSupport::TestCase
|
class CliAccessoryTest < ActiveSupport::TestCase
|
||||||
include ActiveSupport::Testing::Stream
|
include ActiveSupport::Testing::Stream
|
||||||
|
|
||||||
test "boot" do
|
setup { ENV["MYSQL_ROOT_PASSWORD"] = "secret123" }
|
||||||
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"]) }
|
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
|
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
|
||||||
ensure
|
|
||||||
ENV["MYSQL_ROOT_PASSWORD"] = nil
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2,14 +2,18 @@ require "test_helper"
|
|||||||
require "mrsk/configuration"
|
require "mrsk/configuration"
|
||||||
require "mrsk/commands/app"
|
require "mrsk/commands/app"
|
||||||
|
|
||||||
ENV["RAILS_MASTER_KEY"] = "456"
|
|
||||||
|
|
||||||
class CommandsAppTest < ActiveSupport::TestCase
|
class CommandsAppTest < ActiveSupport::TestCase
|
||||||
setup do
|
setup do
|
||||||
|
ENV["RAILS_MASTER_KEY"] = "456"
|
||||||
|
|
||||||
@config = { service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" }, servers: [ "1.1.1.1" ] }
|
@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)
|
@app = Mrsk::Commands::App.new Mrsk::Configuration.new(@config)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
teardown do
|
||||||
|
ENV["RAILS_MASTER_KEY"] = nil
|
||||||
|
end
|
||||||
|
|
||||||
test "run" do
|
test "run" do
|
||||||
assert_equal \
|
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
|
[: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" ],
|
[ :docker, :run, "--rm", "-e", "RAILS_MASTER_KEY=456", "dhh/app:missing", "bin/rails", "db:setup" ],
|
||||||
@app.run_exec("bin/rails", "db:setup")
|
@app.run_exec("bin/rails", "db:setup")
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
@@ -18,8 +18,15 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase
|
|||||||
},
|
},
|
||||||
"secret" => [
|
"secret" => [
|
||||||
"MYSQL_ROOT_PASSWORD"
|
"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" => {
|
"redis" => {
|
||||||
"image" => "redis:latest",
|
"image" => "redis:latest",
|
||||||
@@ -83,7 +90,18 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "volume args" do
|
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
|
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
|
||||||
|
|
||||||
|
test "directories" do
|
||||||
|
assert_equal({"$PWD/app-mysql/data"=>"/var/lib/mysql"}, @config.accessory(:mysql).directories)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
require "test_helper"
|
require "test_helper"
|
||||||
require "mrsk/configuration"
|
require "mrsk/configuration"
|
||||||
|
|
||||||
ENV["RAILS_MASTER_KEY"] = "456"
|
|
||||||
|
|
||||||
class ConfigurationTest < ActiveSupport::TestCase
|
class ConfigurationTest < ActiveSupport::TestCase
|
||||||
setup do
|
setup do
|
||||||
|
ENV["RAILS_MASTER_KEY"] = "456"
|
||||||
|
|
||||||
@deploy = {
|
@deploy = {
|
||||||
service: "app", image: "dhh/app",
|
service: "app", image: "dhh/app",
|
||||||
registry: { "username" => "dhh", "password" => "secret" },
|
registry: { "username" => "dhh", "password" => "secret" },
|
||||||
@@ -21,6 +21,10 @@ class ConfigurationTest < ActiveSupport::TestCase
|
|||||||
@config_with_roles = Mrsk::Configuration.new(@deploy_with_roles)
|
@config_with_roles = Mrsk::Configuration.new(@deploy_with_roles)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
teardown do
|
||||||
|
ENV["RAILS_MASTER_KEY"] = nil
|
||||||
|
end
|
||||||
|
|
||||||
test "ensure valid keys" do
|
test "ensure valid keys" do
|
||||||
assert_raise(ArgumentError) do
|
assert_raise(ArgumentError) do
|
||||||
Mrsk::Configuration.new(@deploy.tap { _1.delete(:service) })
|
Mrsk::Configuration.new(@deploy.tap { _1.delete(:service) })
|
||||||
@@ -134,6 +138,11 @@ class ConfigurationTest < ActiveSupport::TestCase
|
|||||||
assert_equal "456", @config.master_key
|
assert_equal "456", @config.master_key
|
||||||
end
|
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
|
test "volume_args" do
|
||||||
assert_equal ["--volume", "/local/path:/container/path"], @config.volume_args
|
assert_equal ["--volume", "/local/path:/container/path"], @config.volume_args
|
||||||
end
|
end
|
||||||
|
|||||||
10
test/fixtures/deploy_with_accessories.yml
vendored
10
test/fixtures/deploy_with_accessories.yml
vendored
@@ -17,11 +17,13 @@ accessories:
|
|||||||
MYSQL_ROOT_HOST: '%'
|
MYSQL_ROOT_HOST: '%'
|
||||||
secret:
|
secret:
|
||||||
- MYSQL_ROOT_PASSWORD
|
- MYSQL_ROOT_PASSWORD
|
||||||
volumes:
|
files:
|
||||||
- /var/lib/mysql:/var/lib/mysql
|
- 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
|
||||||
|
|||||||
1
test/fixtures/files/my.cnf
vendored
Normal file
1
test/fixtures/files/my.cnf
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
# MySQL Config
|
||||||
1
test/fixtures/files/structure.sql.erb
vendored
Normal file
1
test/fixtures/files/structure.sql.erb
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<%= "This was dynamically expanded" %>
|
||||||
Reference in New Issue
Block a user