Add role concern with specialized cmds for job running

This commit is contained in:
David Heinemeier Hansson
2023-01-10 17:27:56 +01:00
parent 1cee87d440
commit e8beb362d0
8 changed files with 273 additions and 62 deletions

View File

@@ -9,15 +9,18 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
docker :pull, config.absolute_image docker :pull, config.absolute_image
end end
def run def run(role: :web)
role = config.role(role)
docker :run, docker :run,
"-d", "-d",
"--restart unless-stopped", "--restart unless-stopped",
"--name", config.service_with_version, "--name", config.service_with_version,
"-e", redact("RAILS_MASTER_KEY=#{config.master_key}"), "-e", redact("RAILS_MASTER_KEY=#{config.master_key}"),
*config.envs, *config.env_args,
*config.labels, *role.label_args,
config.absolute_image config.absolute_image,
role.cmd
end end
def start def start
@@ -39,7 +42,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
def exec(*command) def exec(*command)
docker :exec, docker :exec,
"-e", redact("RAILS_MASTER_KEY=#{config.master_key}"), "-e", redact("RAILS_MASTER_KEY=#{config.master_key}"),
*config.envs, *config.env_args,
config.service_with_version, config.service_with_version,
*command *command
end end

View File

@@ -1,14 +1,21 @@
require "active_support/ordered_options" require "active_support/ordered_options"
require "active_support/core_ext/string/inquiry"
require "erb" require "erb"
class Mrsk::Configuration class Mrsk::Configuration
delegate :service, :image, :env, :registry, :ssh_user, to: :config, allow_nil: true delegate :service, :image, :servers, :env, :registry, :ssh_user, to: :config, allow_nil: true
def self.load_file(file) class << self
if file.exist? def load_file(file)
new YAML.load(ERB.new(IO.read(file)).result).symbolize_keys if file.exist?
else new YAML.load(ERB.new(IO.read(file)).result).symbolize_keys
raise "Configuration file not found in #{file}" else
raise "Configuration file not found in #{file}"
end
end
def argumentize(argument, attributes)
attributes.flat_map { |k, v| [ argument, "#{k}=#{v}" ] }
end end
end end
@@ -17,11 +24,39 @@ class Mrsk::Configuration
ensure_required_keys_present ensure_required_keys_present
end end
def roles
@roles ||= role_names.collect { |role_name| Role.new(role_name, config: self) }
end
def role(name)
roles.detect { |r| r.name == name.to_s }
end
def hosts def hosts
ENV["HOSTS"] || config.servers hosts =
case
when ENV["HOSTS"]
ENV["HOSTS"].split(",")
when ENV["ROLES"]
role_names = ENV["ROLES"].split(",")
roles.select { |r| role_names.include?(r.name) }.flat_map(&:hosts)
else
roles.flat_map(&:hosts)
end
if hosts.any?
hosts
else
raise ArgumentError, "No hosts found"
end
end end
def primary_host
role(:web).hosts.first
end end
def version def version
@version ||= ENV["VERSION"] || `git rev-parse HEAD`.strip @version ||= ENV["VERSION"] || `git rev-parse HEAD`.strip
end end
@@ -38,18 +73,9 @@ class Mrsk::Configuration
"#{service}-#{version}" "#{service}-#{version}"
end end
def envs
parameterize "-e", env if env.present?
end
def labels def env_args
parameterize "--label", \ self.class.argumentize "-e", config.env if config.env.present?
"service" => service,
"traefik.http.routers.#{service}.rule" => "'PathPrefix(`/`)'",
"traefik.http.services.#{service}.loadbalancer.healthcheck.path" => "/up",
"traefik.http.services.#{service}.loadbalancer.healthcheck.interval" => "1s",
"traefik.http.middlewares.#{service}.retry.attempts" => "3",
"traefik.http.middlewares.#{service}.retry.initialinterval" => "500ms"
end end
def ssh_options def ssh_options
@@ -60,6 +86,7 @@ class Mrsk::Configuration
ENV["RAILS_MASTER_KEY"] || File.read(Rails.root.join("config/master.key")) ENV["RAILS_MASTER_KEY"] || File.read(Rails.root.join("config/master.key"))
end end
private private
attr_accessor :config attr_accessor :config
@@ -73,7 +100,9 @@ class Mrsk::Configuration
end end
end end
def parameterize(param, hash) def role_names
hash.flat_map { |k, v| [ param, "#{k}=#{v}" ] } config.servers.is_a?(Array) ? [ "web" ] : config.servers.keys.sort
end end
end end
require "mrsk/configuration/role"

View File

@@ -0,0 +1,63 @@
class Mrsk::Configuration::Role
delegate :argumentize, to: Mrsk::Configuration
attr_accessor :name
def initialize(name, config:)
@name, @config = name.inquiry, config
end
def hosts
@hosts ||= extract_hosts_from_config
end
def label_args
argumentize "--label", labels
end
def cmd
specializations["cmd"]
end
private
attr_accessor :config
def extract_hosts_from_config
if config.servers.is_a?(Array)
config.servers
else
servers = config.servers[name]
servers.is_a?(Array) ? servers : servers["hosts"]
end
end
def labels
if name.web?
default_labels.merge(traefik_labels)
else
default_labels
end
end
def default_labels
{ "service" => config.service, "role" => name }
end
def traefik_labels
{
"traefik.http.routers.#{config.service}.rule" => "'PathPrefix(`/`)'",
"traefik.http.services.#{config.service}.loadbalancer.healthcheck.path" => "/up",
"traefik.http.services.#{config.service}.loadbalancer.healthcheck.interval" => "1s",
"traefik.http.middlewares.#{config.service}.retry.attempts" => "3",
"traefik.http.middlewares.#{config.service}.retry.initialinterval" => "500ms"
}
end
def specializations
if config.servers.is_a?(Array) || config.servers[name].is_a?(Array)
{ }
else
config.servers[name].without("hosts")
end
end
end

View File

@@ -19,15 +19,17 @@ namespace :mrsk do
desc "Run app on servers (or start them if they've already been run)" desc "Run app on servers (or start them if they've already been run)"
task :run do task :run do
on(MRSK_CONFIG.hosts) do |host| MRSK_CONFIG.roles.each do |role|
begin on(MRSK_CONFIG.role(role).hosts) do |host|
execute *app.run begin
rescue SSHKit::Command::Failed => e execute *app.run(role: role)
if e.message =~ /already in use/ rescue SSHKit::Command::Failed => e
puts "Container with same version already deployed on #{host}, starting that instead" if e.message =~ /already in use/
execute *app.start, host: host puts "Container with same version already deployed on #{host}, starting that instead"
else execute *app.start, host: host
raise else
raise
end
end end
end end
end end
@@ -64,13 +66,13 @@ namespace :mrsk do
desc "Execute a custom task on the first defined server" desc "Execute a custom task on the first defined server"
task :once do task :once do
on(MRSK_CONFIG.hosts.first) { |host| puts capture(*app.exec(ENV["CMD"])) } on(MRSK_CONFIG.primary_host) { |host| puts capture(*app.exec(ENV["CMD"])) }
end end
namespace :once do namespace :once do
desc "Execute Rails command on the first defined server, like CMD='runner \"puts %(Hello World)\"" desc "Execute Rails command on the first defined server, like CMD='runner \"puts %(Hello World)\""
task :rails do task :rails do
on(MRSK_CONFIG.hosts.first) { puts capture(*app.exec("bin/rails", ENV["CMD"])) } on(MRSK_CONFIG.primary_host) { puts capture(*app.exec("bin/rails", ENV["CMD"])) }
end end
end end
end end

View File

@@ -6,7 +6,7 @@ namespace :mrsk do
namespace :registry do namespace :registry do
desc "Login to the registry locally and remotely" desc "Login to the registry locally and remotely"
task :login do task :login do
run_locally { execute *registry.login } run_locally { execute *registry.login }
on(MRSK_CONFIG.hosts) { execute *registry.login } on(MRSK_CONFIG.hosts) { execute *registry.login }
end end

View File

@@ -7,12 +7,12 @@ ENV["RAILS_MASTER_KEY"] = "456"
class AppCommandTest < ActiveSupport::TestCase class AppCommandTest < ActiveSupport::TestCase
setup do setup do
@config = { service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" } } @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
test "run" do test "run" do
assert_equal \ assert_equal \
[:docker, :run, "-d", "--restart unless-stopped", "--name", "app-123", "-e", "RAILS_MASTER_KEY=456", "--label", "service=app", "--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:123"], @app.run [:docker, :run, "-d", "--restart unless-stopped", "--name", "app-123", "-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:123"], @app.run
end end
end end

View File

@@ -0,0 +1,45 @@
require "test_helper"
require "mrsk/configuration"
ENV["VERSION"] = "123"
class ConfigurationRoleTest < ActiveSupport::TestCase
setup do
@deploy = {
service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" },
servers: [ "1.1.1.1", "1.1.1.2" ]
}
@config = Mrsk::Configuration.new(@deploy)
@deploy_with_roles = @deploy.dup.merge({
servers: {
"web" => [ "1.1.1.1", "1.1.1.2" ],
"workers" => {
"hosts" => [ "1.1.1.3", "1.1.1.4" ],
"cmd" => "bin/jobs"
}
}
})
@config_with_roles = Mrsk::Configuration.new(@deploy_with_roles)
end
test "hosts" do
assert_equal [ "1.1.1.1", "1.1.1.2" ], @config.role(:web).hosts
assert_equal [ "1.1.1.3", "1.1.1.4" ], @config_with_roles.role(:workers).hosts
end
test "label args" do
assert_equal [ "--label", "service=app", "--label", "role=workers" ], @config_with_roles.role(:workers).label_args
end
test "special label args for web" do
assert_equal [ "--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"], @config.role(:web).label_args
end
test "cmd" do
assert_nil @config.role(:web).cmd
assert_equal "bin/jobs", @config_with_roles.role(:workers).cmd
end
end

View File

@@ -2,47 +2,116 @@ require "test_helper"
require "mrsk/configuration" require "mrsk/configuration"
ENV["VERSION"] = "123" ENV["VERSION"] = "123"
ENV["RAILS_MASTER_KEY"] = "456"
class ConfigurationTest < ActiveSupport::TestCase class ConfigurationTest < ActiveSupport::TestCase
setup do setup do
@config = { service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" } } @deploy = {
service: "app", image: "dhh/app",
registry: { "username" => "dhh", "password" => "secret" },
env: { "REDIS_URL" => "redis://x/y" },
servers: [ "1.1.1.1", "1.1.1.2" ]
}
@config = Mrsk::Configuration.new(@deploy)
@deploy_with_roles = @deploy.dup.merge({
servers: { "web" => [ "1.1.1.1", "1.1.1.2" ], "workers" => { "hosts" => [ "1.1.1.3", "1.1.1.4" ] } } })
@config_with_roles = Mrsk::Configuration.new(@deploy_with_roles)
end end
test "ensure valid keys" do test "ensure valid keys" do
assert_raise(ArgumentError) do assert_raise(ArgumentError) do
Mrsk::Configuration.new(@config.tap { _1.delete(:service) }) Mrsk::Configuration.new(@deploy.tap { _1.delete(:service) })
Mrsk::Configuration.new(@config.tap { _1.delete(:image) }) Mrsk::Configuration.new(@deploy.tap { _1.delete(:image) })
Mrsk::Configuration.new(@config.tap { _1.delete(:registry) }) Mrsk::Configuration.new(@deploy.tap { _1.delete(:registry) })
Mrsk::Configuration.new(@config.tap { _1[:registry].delete("username") }) Mrsk::Configuration.new(@deploy.tap { _1[:registry].delete("username") })
Mrsk::Configuration.new(@config.tap { _1[:registry].delete("password") }) Mrsk::Configuration.new(@deploy.tap { _1[:registry].delete("password") })
end end
end end
test "repository" do test "roles" do
configuration = Mrsk::Configuration.new(@config) assert_equal %w[ web ], @config.roles.collect(&:name)
assert_equal "dhh/app", configuration.repository assert_equal %w[ web workers ], @config_with_roles.roles.collect(&:name)
end
configuration = Mrsk::Configuration.new(@config.tap { |c| c[:registry].merge!({ "server" => "ghcr.io" }) }) test "role" do
assert_equal "ghcr.io/dhh/app", configuration.repository assert_equal "web", @config.role(:web).name
assert_equal "workers", @config_with_roles.role(:workers).name
assert_nil @config.role(:missing)
end
test "hosts" do
assert_equal [ "1.1.1.1", "1.1.1.2"], @config.hosts
assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4" ], @config_with_roles.hosts
end
test "hosts from ENV" do
ENV["HOSTS"] = "1.1.1.5,1.1.1.6"
assert_equal [ "1.1.1.5", "1.1.1.6"], @config.hosts
ensure
ENV["HOSTS"] = nil
end
test "hosts from ENV roles" do
ENV["ROLES"] = "web,workers"
assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4" ], @config_with_roles.hosts
ENV["ROLES"] = "workers"
assert_equal [ "1.1.1.3", "1.1.1.4" ], @config_with_roles.hosts
ensure
ENV["ROLES"] = nil
end
test "primary host" do
assert_equal "1.1.1.1", @config.primary_host
assert_equal "1.1.1.1", @config_with_roles.primary_host
end
test "version" do
assert_equal "123", @config.version
end
test "repository" do
assert_equal "dhh/app", @config.repository
config = Mrsk::Configuration.new(@deploy.tap { |c| c[:registry].merge!({ "server" => "ghcr.io" }) })
assert_equal "ghcr.io/dhh/app", config.repository
end end
test "absolute image" do test "absolute image" do
configuration = Mrsk::Configuration.new(@config) assert_equal "dhh/app:123", @config.absolute_image
assert_equal "dhh/app:123", configuration.absolute_image
configuration = Mrsk::Configuration.new(@config.tap { |c| c[:registry].merge!({ "server" => "ghcr.io" }) }) config = Mrsk::Configuration.new(@deploy.tap { |c| c[:registry].merge!({ "server" => "ghcr.io" }) })
assert_equal "ghcr.io/dhh/app:123", configuration.absolute_image assert_equal "ghcr.io/dhh/app:123", config.absolute_image
end end
test "service with version" do
assert_equal "app-123", @config.service_with_version
end
test "env args" do
assert_equal [ "-e", "REDIS_URL=redis://x/y" ], @config.env_args
end
test "ssh options" do
assert_equal "root", @config.ssh_options[:user]
config = Mrsk::Configuration.new(@deploy.tap { |c| c[:ssh_user] = "app" })
assert_equal "app", @config.ssh_options[:user]
end
test "master key" do
assert_equal "456", @config.master_key
end
test "erb evaluation of yml config" do test "erb evaluation of yml config" do
configuration = Mrsk::Configuration.load_file Pathname.new(File.expand_path("fixtures/deploy.erb.yml", __dir__)) config = Mrsk::Configuration.load_file Pathname.new(File.expand_path("fixtures/deploy.erb.yml", __dir__))
assert_equal "my-user", configuration.registry["username"] assert_equal "my-user", config.registry["username"]
end
test "labels" do
configuration = Mrsk::Configuration.new(@config)
assert_equal ["--label", "service=app", "--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"],
configuration.labels
end end
end end