Compare commits
41 Commits
v1.8.0
...
revert-905
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
459ba95bbf | ||
|
|
6adf3c117f | ||
|
|
9f0b10425c | ||
|
|
5f2384f123 | ||
|
|
eab7d3adc5 | ||
|
|
d2d0223c37 | ||
|
|
56268d724d | ||
|
|
cffb6c3d7e | ||
|
|
bd1726f305 | ||
|
|
7ddb122a22 | ||
|
|
98c951bbdb | ||
|
|
374c117b79 | ||
|
|
d6a5cf3c78 | ||
|
|
2aeabda455 | ||
|
|
c048c097ed | ||
|
|
ed148628fb | ||
|
|
d48080c772 | ||
|
|
3f64338929 | ||
|
|
0ab838bc25 | ||
|
|
b7382ceeaf | ||
|
|
69367fbc6b | ||
|
|
2515bd705c | ||
|
|
579e169be2 | ||
|
|
d6f5da92be | ||
|
|
9ccfe20b10 | ||
|
|
e871d347d5 | ||
|
|
b8af719bb7 | ||
|
|
f48987aa03 | ||
|
|
ef051eca1b | ||
|
|
173d44ee0a | ||
|
|
4e811372f8 | ||
|
|
ec4aa45852 | ||
|
|
5e11a64181 | ||
|
|
57d9ce177a | ||
|
|
b12de87388 | ||
|
|
8a98949634 | ||
|
|
0eb9f48082 | ||
|
|
9db6fc0704 | ||
|
|
27fede3caa | ||
|
|
29c723f7ec | ||
|
|
2755582c47 |
@@ -1,7 +1,7 @@
|
||||
# Use the official Ruby 3.2.0 Alpine image as the base image
|
||||
FROM ruby:3.2.0-alpine
|
||||
|
||||
# Install docker/buildx-bin
|
||||
# Install docker/buildx-bin
|
||||
COPY --from=docker/buildx-bin /buildx /usr/libexec/docker/cli-plugins/docker-buildx
|
||||
|
||||
# Set the working directory to /kamal
|
||||
@@ -14,7 +14,7 @@ COPY Gemfile Gemfile.lock kamal.gemspec ./
|
||||
COPY lib/kamal/version.rb /kamal/lib/kamal/version.rb
|
||||
|
||||
# Install system dependencies
|
||||
RUN apk add --no-cache --update build-base git docker openrc openssh-client-default \
|
||||
RUN apk add --no-cache build-base git docker openrc openssh-client-default \
|
||||
&& rc-update add docker boot \
|
||||
&& gem install bundler --version=2.4.3 \
|
||||
&& bundle install
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
PATH
|
||||
remote: .
|
||||
specs:
|
||||
kamal (1.8.0)
|
||||
kamal (2.0.0.alpha)
|
||||
activesupport (>= 7.0)
|
||||
base64 (~> 0.2)
|
||||
bcrypt_pbkdf (~> 1.0)
|
||||
@@ -10,7 +10,7 @@ PATH
|
||||
ed25519 (~> 1.2)
|
||||
net-ssh (~> 7.0)
|
||||
sshkit (>= 1.23.0, < 2.0)
|
||||
thor (~> 1.2)
|
||||
thor (~> 1.3)
|
||||
zeitwerk (~> 2.5)
|
||||
|
||||
GEM
|
||||
|
||||
20
bin/docs
20
bin/docs
@@ -17,6 +17,7 @@ end
|
||||
|
||||
DOCS = {
|
||||
"accessory" => "Accessories",
|
||||
"alias" => "Aliases",
|
||||
"boot" => "Booting",
|
||||
"builder" => "Builders",
|
||||
"configuration" => "Configuration overview",
|
||||
@@ -67,26 +68,27 @@ class DocWriter
|
||||
output.puts
|
||||
place = :new_section
|
||||
elsif line =~ /^ *#/
|
||||
generate_line(line, place: place)
|
||||
generate_line(line, heading: place == :new_section)
|
||||
place = :in_section
|
||||
else
|
||||
output.puts "```yaml"
|
||||
output.print line
|
||||
output.puts line
|
||||
place = :in_yaml
|
||||
end
|
||||
when :in_yaml
|
||||
when :in_yaml, :in_empty_line_yaml
|
||||
if line =~ /^ *#/
|
||||
output.puts "```"
|
||||
generate_line(line, place: :new_section)
|
||||
generate_line(line, heading: place == :in_empty_line_yaml)
|
||||
place = :in_section
|
||||
elsif line.empty?
|
||||
place = :in_empty_line_yaml
|
||||
else
|
||||
output.puts
|
||||
output.print line
|
||||
output.puts line
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
output.puts "\n```" if place == :in_yaml
|
||||
output.puts "```" if place == :in_yaml
|
||||
end
|
||||
|
||||
def generate_header
|
||||
@@ -98,7 +100,7 @@ class DocWriter
|
||||
output.puts
|
||||
end
|
||||
|
||||
def generate_line(line, place: :in_section)
|
||||
def generate_line(line, heading: false)
|
||||
line = line.gsub(/^ *#\s?/, "")
|
||||
|
||||
if line =~ /(.*)kamal docs ([a-z]*)(.*)/
|
||||
@@ -109,7 +111,7 @@ class DocWriter
|
||||
line = "#{$1}[#{titlify($2.split("/").last)}](#{$2})#{$3}"
|
||||
end
|
||||
|
||||
if place == :new_section
|
||||
if heading
|
||||
output.puts "## [#{line}](##{linkify(line)})"
|
||||
else
|
||||
output.puts line
|
||||
|
||||
@@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
|
||||
spec.add_dependency "activesupport", ">= 7.0"
|
||||
spec.add_dependency "sshkit", ">= 1.23.0", "< 2.0"
|
||||
spec.add_dependency "net-ssh", "~> 7.0"
|
||||
spec.add_dependency "thor", "~> 1.2"
|
||||
spec.add_dependency "thor", "~> 1.3"
|
||||
spec.add_dependency "dotenv", "~> 2.8"
|
||||
spec.add_dependency "zeitwerk", "~> 2.5"
|
||||
spec.add_dependency "ed25519", "~> 1.2"
|
||||
|
||||
9
lib/kamal/cli/alias/command.rb
Normal file
9
lib/kamal/cli/alias/command.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
class Kamal::Cli::Alias::Command < Thor::DynamicCommand
|
||||
def run(instance, args = [])
|
||||
if (_alias = KAMAL.config.aliases[name])
|
||||
Kamal::Cli::Main.start(Shellwords.split(_alias.command) + ARGV[1..-1])
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -71,11 +71,12 @@ class Kamal::Cli::App < Kamal::Cli::Base
|
||||
end
|
||||
end
|
||||
|
||||
desc "exec [CMD]", "Execute a custom command on servers within the app container (use --help to show options)"
|
||||
desc "exec [CMD...]", "Execute a custom command on servers within the app container (use --help to show options)"
|
||||
option :interactive, aliases: "-i", type: :boolean, default: false, desc: "Execute command over ssh for an interactive shell (use for console/bash)"
|
||||
option :reuse, type: :boolean, default: false, desc: "Reuse currently running container instead of starting a new one"
|
||||
option :env, aliases: "-e", type: :hash, desc: "Set environment variables for the command"
|
||||
def exec(cmd)
|
||||
def exec(*cmd)
|
||||
cmd = Kamal::Utils.join_commands(cmd)
|
||||
env = options[:env]
|
||||
case
|
||||
when options[:interactive] && options[:reuse]
|
||||
|
||||
@@ -6,7 +6,8 @@ module Kamal::Cli
|
||||
class Base < Thor
|
||||
include SSHKit::DSL
|
||||
|
||||
def self.exit_on_failure?() true end
|
||||
def self.exit_on_failure?() false end
|
||||
def self.dynamic_command_class() Kamal::Cli::Alias::Command end
|
||||
|
||||
class_option :verbose, type: :boolean, aliases: "-v", desc: "Detailed logging"
|
||||
class_option :quiet, type: :boolean, aliases: "-q", desc: "Minimal logging"
|
||||
@@ -22,8 +23,14 @@ module Kamal::Cli
|
||||
|
||||
class_option :skip_hooks, aliases: "-H", type: :boolean, default: false, desc: "Don't run hooks"
|
||||
|
||||
def initialize(*)
|
||||
super
|
||||
def initialize(args = [], local_options = {}, config = {})
|
||||
if config[:current_command].is_a?(Kamal::Cli::Alias::Command)
|
||||
# When Thor generates a dynamic command, it doesn't attempt to parse the arguments.
|
||||
# For our purposes, it means the arguments are passed in args rather than local_options.
|
||||
super([], args, config)
|
||||
else
|
||||
super
|
||||
end
|
||||
@original_env = ENV.to_h.dup
|
||||
load_env
|
||||
initialize_commander(options_with_subcommand_class_options)
|
||||
|
||||
@@ -140,7 +140,7 @@ class Kamal::Cli::Build < Kamal::Cli::Base
|
||||
mirror_hosts = Concurrent::Hash.new
|
||||
on(KAMAL.hosts) do |host|
|
||||
first_mirror = capture_with_info(*KAMAL.builder.first_mirror).strip.presence
|
||||
mirror_hosts[first_mirror] ||= host if first_mirror
|
||||
mirror_hosts[first_mirror] ||= host.to_s if first_mirror
|
||||
rescue SSHKit::Command::Failed => e
|
||||
raise unless e.message =~ /error calling index: reflect: slice index out of range/
|
||||
end
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
class Kamal::Cli::Server < Kamal::Cli::Base
|
||||
desc "exec", "Run a custom command on the server (use --help to show options)"
|
||||
option :interactive, type: :boolean, aliases: "-i", default: false, desc: "Run the command interactively (use for console/bash)"
|
||||
def exec(cmd)
|
||||
def exec(*cmd)
|
||||
cmd = Kamal::Utils.join_commands(cmd)
|
||||
hosts = KAMAL.hosts | KAMAL.accessory_hosts
|
||||
|
||||
case
|
||||
|
||||
@@ -27,7 +27,11 @@ class Kamal::Commander
|
||||
|
||||
def specific_primary!
|
||||
@specifics = nil
|
||||
self.specific_hosts = [ config.primary_host ]
|
||||
if specific_roles.present?
|
||||
self.specific_hosts = [ specific_roles.first.primary_host ]
|
||||
else
|
||||
self.specific_hosts = [ config.primary_host ]
|
||||
end
|
||||
end
|
||||
|
||||
def specific_roles=(role_names)
|
||||
@@ -113,6 +117,10 @@ class Kamal::Commander
|
||||
@traefik ||= Kamal::Commands::Traefik.new(config)
|
||||
end
|
||||
|
||||
def alias(name)
|
||||
config.aliases[name]
|
||||
end
|
||||
|
||||
|
||||
def with_verbosity(level)
|
||||
old_level = self.verbosity
|
||||
|
||||
@@ -6,7 +6,7 @@ module Kamal::Commands::Builder::Clone
|
||||
end
|
||||
|
||||
def clone
|
||||
git :clone, Kamal::Git.root, path: clone_directory
|
||||
git :clone, Kamal::Git.root, "--recurse-submodules", path: clone_directory
|
||||
end
|
||||
|
||||
def clone_reset_steps
|
||||
@@ -14,7 +14,8 @@ module Kamal::Commands::Builder::Clone
|
||||
git(:remote, "set-url", :origin, Kamal::Git.root, path: build_directory),
|
||||
git(:fetch, :origin, path: build_directory),
|
||||
git(:reset, "--hard", Kamal::Git.revision, path: build_directory),
|
||||
git(:clean, "-fdx", path: build_directory)
|
||||
git(:clean, "-fdx", path: build_directory),
|
||||
git(:submodule, :update, "--init", path: build_directory)
|
||||
]
|
||||
end
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ class Kamal::Configuration
|
||||
delegate :argumentize, :optionize, to: Kamal::Utils
|
||||
|
||||
attr_reader :destination, :raw_config
|
||||
attr_reader :accessories, :boot, :builder, :env, :healthcheck, :logging, :traefik, :servers, :ssh, :sshkit, :registry
|
||||
attr_reader :accessories, :aliases, :boot, :builder, :env, :healthcheck, :logging, :traefik, :servers, :ssh, :sshkit, :registry
|
||||
|
||||
include Validation
|
||||
|
||||
@@ -47,13 +47,14 @@ class Kamal::Configuration
|
||||
@destination = destination
|
||||
@declared_version = version
|
||||
|
||||
validate! raw_config, example: validation_yml.symbolize_keys, context: ""
|
||||
validate! raw_config, example: validation_yml.symbolize_keys, context: "", with: Kamal::Configuration::Validator::Configuration
|
||||
|
||||
# Eager load config to validate it, these are first as they have dependencies later on
|
||||
@servers = Servers.new(config: self)
|
||||
@registry = Registry.new(config: self)
|
||||
|
||||
@accessories = @raw_config.accessories&.keys&.collect { |name| Accessory.new(name, config: self) } || []
|
||||
@aliases = @raw_config.aliases&.keys&.to_h { |name| [ name, Alias.new(name, config: self) ] } || {}
|
||||
@boot = Boot.new(config: self)
|
||||
@builder = Builder.new(config: self)
|
||||
@env = Env.new(config: @raw_config.env || {})
|
||||
|
||||
15
lib/kamal/configuration/alias.rb
Normal file
15
lib/kamal/configuration/alias.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
class Kamal::Configuration::Alias
|
||||
include Kamal::Configuration::Validation
|
||||
|
||||
attr_reader :name, :command
|
||||
|
||||
def initialize(name, config:)
|
||||
@name, @command = name.inquiry, config.raw_config["aliases"][name]
|
||||
|
||||
validate! \
|
||||
command,
|
||||
example: validation_yml["aliases"]["uname"],
|
||||
context: "aliases/#{name}",
|
||||
with: Kamal::Configuration::Validator::Alias
|
||||
end
|
||||
end
|
||||
26
lib/kamal/configuration/docs/alias.yml
Normal file
26
lib/kamal/configuration/docs/alias.yml
Normal file
@@ -0,0 +1,26 @@
|
||||
# Aliases
|
||||
#
|
||||
# Aliases are shortcuts for Kamal commands.
|
||||
#
|
||||
# For example, for a Rails app, you might open a console with:
|
||||
#
|
||||
# ```shell
|
||||
# kamal app exec -i -r console "rails console"
|
||||
# ```
|
||||
#
|
||||
# By defining an alias, like this:
|
||||
aliases:
|
||||
console: app exec -r console -i "rails console"
|
||||
# You can now open the console with:
|
||||
# ```shell
|
||||
# kamal console
|
||||
# ```
|
||||
|
||||
# Configuring aliases
|
||||
#
|
||||
# Aliases are defined in the root config under the alias key
|
||||
#
|
||||
# Each alias is named and can only contain lowercase letters, numbers, dashes and underscores.
|
||||
|
||||
aliases:
|
||||
uname: app exec -p -q -r web "uname -a"
|
||||
@@ -2,13 +2,24 @@
|
||||
#
|
||||
# Configuration is read from the `config/deploy.yml`
|
||||
#
|
||||
|
||||
# Destinations
|
||||
#
|
||||
# When running commands, you can specify a destination with the `-d` flag,
|
||||
# e.g. `kamal deploy -d staging`
|
||||
#
|
||||
# In this case the configuration will also be read from `config/deploy.staging.yml`
|
||||
# and merged with the base configuration.
|
||||
|
||||
# Extensions
|
||||
#
|
||||
# The available configuration options are explained below.
|
||||
# Kamal will not accept unrecognized keys in the configuration file.
|
||||
#
|
||||
# However, you might want to declare a configuration block using YAML anchors
|
||||
# and aliases to avoid repetition.
|
||||
#
|
||||
# You can use prefix a configuration section with `x-` to indicate that it is an
|
||||
# extension. Kamal will ignore the extension and not raise an error.
|
||||
|
||||
# The service name
|
||||
# This is a required value. It is used as the container name prefix.
|
||||
@@ -155,3 +166,9 @@ healthcheck:
|
||||
# Docker logging configuration, see kamal docs logging
|
||||
logging:
|
||||
...
|
||||
|
||||
# Aliases
|
||||
#
|
||||
# Alias configuration, see kamal docs alias
|
||||
aliases:
|
||||
...
|
||||
|
||||
@@ -29,7 +29,7 @@ env:
|
||||
# To pass the secrets you should list them under the `secret` key. When you do this the
|
||||
# other variables need to be moved under the `clear` key.
|
||||
#
|
||||
# Unlike clear valies, secrets are not passed directly to the container,
|
||||
# Unlike clear values, secrets are not passed directly to the container,
|
||||
# but are stored in an env file on the host
|
||||
# The file is not updated when deploying, only when running `kamal envify` or `kamal env push`.
|
||||
env:
|
||||
|
||||
@@ -13,33 +13,34 @@ class Kamal::Configuration::Validator
|
||||
|
||||
private
|
||||
def validate_against_example!(validation_config, example)
|
||||
validate_type! validation_config, Hash
|
||||
validate_type! validation_config, example.class
|
||||
|
||||
if (unknown_keys = validation_config.keys - example.keys).any?
|
||||
unknown_keys_error unknown_keys
|
||||
end
|
||||
if example.class == Hash
|
||||
check_unknown_keys! validation_config, example
|
||||
|
||||
validation_config.each do |key, value|
|
||||
with_context(key) do
|
||||
example_value = example[key]
|
||||
validation_config.each do |key, value|
|
||||
next if extension?(key)
|
||||
with_context(key) do
|
||||
example_value = example[key]
|
||||
|
||||
if example_value == "..."
|
||||
validate_type! value, *(Array if key == :servers), Hash
|
||||
elsif key == "hosts"
|
||||
validate_servers! value
|
||||
elsif example_value.is_a?(Array)
|
||||
validate_array_of! value, example_value.first.class
|
||||
elsif example_value.is_a?(Hash)
|
||||
case key.to_s
|
||||
when "options", "args"
|
||||
validate_type! value, Hash
|
||||
when "labels"
|
||||
validate_hash_of! value, example_value.first[1].class
|
||||
if example_value == "..."
|
||||
validate_type! value, *(Array if key == :servers), Hash
|
||||
elsif key == "hosts"
|
||||
validate_servers! value
|
||||
elsif example_value.is_a?(Array)
|
||||
validate_array_of! value, example_value.first.class
|
||||
elsif example_value.is_a?(Hash)
|
||||
case key.to_s
|
||||
when "options", "args"
|
||||
validate_type! value, Hash
|
||||
when "labels"
|
||||
validate_hash_of! value, example_value.first[1].class
|
||||
else
|
||||
validate_against_example! value, example_value
|
||||
end
|
||||
else
|
||||
validate_against_example! value, example_value
|
||||
validate_type! value, example_value.class
|
||||
end
|
||||
else
|
||||
validate_type! value, example_value.class
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -137,4 +138,18 @@ class Kamal::Configuration::Validator
|
||||
ensure
|
||||
@context = old_context
|
||||
end
|
||||
|
||||
def allow_extensions?
|
||||
false
|
||||
end
|
||||
|
||||
def extension?(key)
|
||||
key.to_s.start_with?("x-")
|
||||
end
|
||||
|
||||
def check_unknown_keys!(config, example)
|
||||
unknown_keys = config.keys - example.keys
|
||||
unknown_keys.reject! { |key| extension?(key) } if allow_extensions?
|
||||
unknown_keys_error unknown_keys if unknown_keys.present?
|
||||
end
|
||||
end
|
||||
|
||||
15
lib/kamal/configuration/validator/alias.rb
Normal file
15
lib/kamal/configuration/validator/alias.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
class Kamal::Configuration::Validator::Alias < Kamal::Configuration::Validator
|
||||
def validate!
|
||||
super
|
||||
|
||||
name = context.delete_prefix("aliases/")
|
||||
|
||||
if name !~ /\A[a-z0-9_-]+\z/
|
||||
error "Invalid alias name: '#{name}'. Must only contain lowercase letters, alphanumeric, hyphens and underscores."
|
||||
end
|
||||
|
||||
if Kamal::Cli::Main.commands.include?(name)
|
||||
error "Alias '#{name}' conflicts with a built-in command."
|
||||
end
|
||||
end
|
||||
end
|
||||
6
lib/kamal/configuration/validator/configuration.rb
Normal file
6
lib/kamal/configuration/validator/configuration.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
class Kamal::Configuration::Validator::Configuration < Kamal::Configuration::Validator
|
||||
private
|
||||
def allow_extensions?
|
||||
true
|
||||
end
|
||||
end
|
||||
@@ -77,4 +77,8 @@ module Kamal::Utils
|
||||
def stable_sort!(elements, &block)
|
||||
elements.sort_by!.with_index { |element, index| [ block.call(element), index ] }
|
||||
end
|
||||
|
||||
def join_commands(commands)
|
||||
commands.map(&:strip).join(" ")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
module Kamal
|
||||
VERSION = "1.8.0"
|
||||
VERSION = "2.0.0.alpha"
|
||||
end
|
||||
|
||||
@@ -247,6 +247,12 @@ class CliAppTest < CliTestCase
|
||||
end
|
||||
end
|
||||
|
||||
test "exec separate arguments" do
|
||||
run_command("exec", "ruby", " -v").tap do |output|
|
||||
assert_match "docker run --rm --env-file .kamal/env/roles/app-web.env dhh/app:latest ruby -v", output
|
||||
end
|
||||
end
|
||||
|
||||
test "exec with reuse" do
|
||||
run_command("exec", "--reuse", "ruby -v").tap do |output|
|
||||
assert_match "sh -c 'docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting --filter ancestor=$(docker image ls --filter reference=dhh/app:latest --format '\\''{{.ID}}'\\'') ; docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting' | head -1 | while read line; do echo ${line#app-web-}; done", output # Get current version
|
||||
|
||||
@@ -42,7 +42,7 @@ class CliBuildTest < CliTestCase
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:docker, "--version", "&&", :docker, :buildx, "version")
|
||||
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:execute)
|
||||
.with(:git, "-C", "#{Dir.tmpdir}/kamal-clones/app-#{pwd_sha}", :clone, Dir.pwd)
|
||||
.with(:git, "-C", "#{Dir.tmpdir}/kamal-clones/app-#{pwd_sha}", :clone, Dir.pwd, "--recurse-submodules")
|
||||
.raises(SSHKit::Command::Failed.new("fatal: destination path 'kamal' already exists and is not an empty directory"))
|
||||
.then
|
||||
.returns(true)
|
||||
@@ -50,6 +50,7 @@ class CliBuildTest < CliTestCase
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:git, "-C", build_directory, :fetch, :origin)
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:git, "-C", build_directory, :reset, "--hard", Kamal::Git.revision)
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:git, "-C", build_directory, :clean, "-fdx")
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:git, "-C", build_directory, :submodule, :update, "--init")
|
||||
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:execute)
|
||||
.with(:docker, :buildx, :build, "--push", "--platform", "linux/amd64,linux/arm64", "--builder", "kamal-app-multiarch", "-t", "dhh/app:999", "-t", "dhh/app:latest", "--label", "service=\"app\"", "--file", "Dockerfile", ".")
|
||||
@@ -88,7 +89,7 @@ class CliBuildTest < CliTestCase
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:docker, "--version", "&&", :docker, :buildx, "version")
|
||||
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:execute)
|
||||
.with(:git, "-C", "#{Dir.tmpdir}/kamal-clones/app-#{pwd_sha}", :clone, Dir.pwd)
|
||||
.with(:git, "-C", "#{Dir.tmpdir}/kamal-clones/app-#{pwd_sha}", :clone, Dir.pwd, "--recurse-submodules")
|
||||
.raises(SSHKit::Command::Failed.new("fatal: destination path 'kamal' already exists and is not an empty directory"))
|
||||
.then
|
||||
.returns(true)
|
||||
@@ -185,7 +186,7 @@ class CliBuildTest < CliTestCase
|
||||
run_command("pull").tap do |output|
|
||||
assert_match /Pulling image on 1\.1\.1\.\d to seed the mirror\.\.\./, output
|
||||
assert_match "Pulling image on remaining hosts...", output
|
||||
assert_match /docker pull dhh\/app:999/, output
|
||||
assert_equal 4, output.scan(/docker pull dhh\/app:999/).size, output
|
||||
assert_match "docker inspect -f '{{ .Config.Labels.service }}' dhh/app:999 | grep -x app || (echo \"Image dhh/app:999 is missing the 'service' label\" && exit 1)", output
|
||||
end
|
||||
end
|
||||
@@ -199,7 +200,7 @@ class CliBuildTest < CliTestCase
|
||||
run_command("pull").tap do |output|
|
||||
assert_match /Pulling image on 1\.1\.1\.\d, 1\.1\.1\.\d to seed the mirrors\.\.\./, output
|
||||
assert_match "Pulling image on remaining hosts...", output
|
||||
assert_match /docker pull dhh\/app:999/, output
|
||||
assert_equal 4, output.scan(/docker pull dhh\/app:999/).size, output
|
||||
assert_match "docker inspect -f '{{ .Config.Labels.service }}' dhh/app:999 | grep -x app || (echo \"Image dhh/app:999 is missing the 'service' label\" && exit 1)", output
|
||||
end
|
||||
end
|
||||
|
||||
@@ -42,12 +42,13 @@ class CliTestCase < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
def assert_hook_ran(hook, output, version:, service_version:, hosts:, command:, subcommand: nil, runtime: false)
|
||||
performer = Kamal::Git.email.presence || `whoami`.chomp
|
||||
whoami = `whoami`.chomp
|
||||
performer = Kamal::Git.email.presence || whoami
|
||||
service = service_version.split("@").first
|
||||
|
||||
assert_match "Running the #{hook} hook...\n", output
|
||||
|
||||
expected = %r{Running\s/usr/bin/env\s\.kamal/hooks/#{hook}\sas\s#{performer}@localhost\n\s
|
||||
expected = %r{Running\s/usr/bin/env\s\.kamal/hooks/#{hook}\sas\s#{whoami}@localhost\n\s
|
||||
DEBUG\s\[[0-9a-f]*\]\sCommand:\s\(\sexport\s
|
||||
KAMAL_RECORDED_AT=\"\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\dZ\"\s
|
||||
KAMAL_PERFORMER=\"#{performer}\"\s
|
||||
@@ -62,4 +63,12 @@ class CliTestCase < ActiveSupport::TestCase
|
||||
|
||||
assert_match expected, output
|
||||
end
|
||||
|
||||
def with_argv(*argv)
|
||||
old_argv = ARGV
|
||||
ARGV.replace(*argv)
|
||||
yield
|
||||
ensure
|
||||
ARGV.replace(old_argv)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -537,9 +537,47 @@ class CliMainTest < CliTestCase
|
||||
assert_equal Kamal::VERSION, version
|
||||
end
|
||||
|
||||
test "run an alias for details" do
|
||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:traefik:details")
|
||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:app:details")
|
||||
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:accessory:details", [ "all" ])
|
||||
|
||||
run_command("info", config_file: "deploy_with_aliases")
|
||||
end
|
||||
|
||||
test "run an alias for a console" do
|
||||
run_command("console", config_file: "deploy_with_aliases").tap do |output|
|
||||
assert_match "docker exec app-console-999 bin/console on 1.1.1.5", output
|
||||
assert_match "App Host: 1.1.1.5", output
|
||||
end
|
||||
end
|
||||
|
||||
test "run an alias for a console overriding role" do
|
||||
run_command("console", "-r", "workers", config_file: "deploy_with_aliases").tap do |output|
|
||||
assert_match "docker exec app-workers-999 bin/console on 1.1.1.3", output
|
||||
assert_match "App Host: 1.1.1.3", output
|
||||
end
|
||||
end
|
||||
|
||||
test "run an alias for a console passing command" do
|
||||
run_command("exec", "bin/job", config_file: "deploy_with_aliases").tap do |output|
|
||||
assert_match "docker exec app-console-999 bin/job on 1.1.1.5", output
|
||||
assert_match "App Host: 1.1.1.5", output
|
||||
end
|
||||
end
|
||||
|
||||
test "append to command with an alias" do
|
||||
run_command("rails", "db:migrate:status", config_file: "deploy_with_aliases").tap do |output|
|
||||
assert_match "docker exec app-console-999 rails db:migrate:status on 1.1.1.5", output
|
||||
assert_match "App Host: 1.1.1.5", output
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def run_command(*command, config_file: "deploy_simple")
|
||||
stdouted { Kamal::Cli::Main.start([ *command, "-c", "test/fixtures/#{config_file}.yml" ]) }
|
||||
with_argv([ *command, "-c", "test/fixtures/#{config_file}.yml" ]) do
|
||||
stdouted { Kamal::Cli::Main.start }
|
||||
end
|
||||
end
|
||||
|
||||
def with_test_dotenv(**files)
|
||||
|
||||
@@ -3,8 +3,8 @@ require_relative "cli_test_case"
|
||||
class CliServerTest < CliTestCase
|
||||
test "running a command with exec" do
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:capture)
|
||||
.with("date", verbosity: 1)
|
||||
.returns("Today")
|
||||
.with("date", verbosity: 1)
|
||||
.returns("Today")
|
||||
|
||||
hosts = "1.1.1.1".."1.1.1.4"
|
||||
run_command("exec", "date").tap do |output|
|
||||
@@ -15,6 +15,20 @@ class CliServerTest < CliTestCase
|
||||
end
|
||||
end
|
||||
|
||||
test "running a command with exec multiple arguments" do
|
||||
SSHKit::Backend::Abstract.any_instance.stubs(:capture)
|
||||
.with("date -j", verbosity: 1)
|
||||
.returns("Today")
|
||||
|
||||
hosts = "1.1.1.1".."1.1.1.4"
|
||||
run_command("exec", "date", "-j").tap do |output|
|
||||
hosts.map do |host|
|
||||
assert_match "Running 'date -j' on #{hosts.to_a.join(', ')}...", output
|
||||
assert_match "App Host: #{host}\nToday", output
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
test "bootstrap already installed" do
|
||||
stub_setup
|
||||
SSHKit::Backend::Abstract.any_instance.expects(:execute).with(:docker, "-v", raise_on_non_zero_exit: false).returns(true).at_least_once
|
||||
|
||||
@@ -344,4 +344,12 @@ class ConfigurationTest < ActiveSupport::TestCase
|
||||
|
||||
assert_raises(Kamal::ConfigurationError) { Kamal::Configuration.new(@deploy_with_roles.merge(retain_containers: 0)) }
|
||||
end
|
||||
|
||||
test "extensions" do
|
||||
dest_config_file = Pathname.new(File.expand_path("fixtures/deploy_with_extensions.yml", __dir__))
|
||||
|
||||
config = Kamal::Configuration.create_from config_file: dest_config_file
|
||||
assert_equal config.role(:web_tokyo).running_traefik?, true
|
||||
assert_equal config.role(:web_chicago).running_traefik?, true
|
||||
end
|
||||
end
|
||||
|
||||
21
test/fixtures/deploy_with_aliases.yml
vendored
Normal file
21
test/fixtures/deploy_with_aliases.yml
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
service: app
|
||||
image: dhh/app
|
||||
servers:
|
||||
web:
|
||||
- 1.1.1.1
|
||||
- 1.1.1.2
|
||||
workers:
|
||||
hosts:
|
||||
- 1.1.1.3
|
||||
- 1.1.1.4
|
||||
console:
|
||||
hosts:
|
||||
- 1.1.1.5
|
||||
registry:
|
||||
username: user
|
||||
password: pw
|
||||
aliases:
|
||||
info: details
|
||||
console: app exec --reuse -p -r console "bin/console"
|
||||
exec: app exec --reuse -p -r console
|
||||
rails: app exec --reuse -p -r console rails
|
||||
24
test/fixtures/deploy_with_extensions.yml
vendored
Normal file
24
test/fixtures/deploy_with_extensions.yml
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
|
||||
x-web: &web
|
||||
traefik: true
|
||||
|
||||
service: app
|
||||
image: dhh/app
|
||||
servers:
|
||||
web_chicago:
|
||||
<<: *web
|
||||
hosts:
|
||||
- 1.1.1.1
|
||||
- 1.1.1.2
|
||||
web_tokyo:
|
||||
<<: *web
|
||||
hosts:
|
||||
- 1.1.1.3
|
||||
- 1.1.1.4
|
||||
env:
|
||||
REDIS_URL: redis://x/y
|
||||
registry:
|
||||
server: registry.digitalocean.com
|
||||
username: user
|
||||
password: pw
|
||||
primary_role: web_tokyo
|
||||
@@ -29,8 +29,6 @@ services:
|
||||
context: docker/registry
|
||||
environment:
|
||||
- REGISTRY_HTTP_ADDR=0.0.0.0:4443
|
||||
- REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt
|
||||
- REGISTRY_HTTP_TLS_KEY=/certs/domain.key
|
||||
volumes:
|
||||
- shared:/shared
|
||||
- registry:/var/lib/registry/
|
||||
|
||||
@@ -22,7 +22,6 @@ COPY app_with_roles/ app_with_roles/
|
||||
|
||||
RUN rm -rf /root/.ssh
|
||||
RUN ln -s /shared/ssh /root/.ssh
|
||||
RUN mkdir -p /etc/docker/certs.d/registry:4443 && ln -s /shared/certs/domain.crt /etc/docker/certs.d/registry:4443/ca.crt
|
||||
|
||||
RUN git config --global user.email "deployer@example.com"
|
||||
RUN git config --global user.name "Deployer"
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
#!/bin/sh
|
||||
|
||||
echo "About to lock..."
|
||||
if [ "$KAMAL_HOSTS" != "vm1,vm2" ]; then
|
||||
echo "Expected hosts to be 'vm1,vm2', got $KAMAL_HOSTS"
|
||||
exit 1
|
||||
fi
|
||||
mkdir -p /tmp/${TEST_ID} && touch /tmp/${TEST_ID}/pre-connect
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
#!/bin/sh
|
||||
|
||||
echo "About to lock..."
|
||||
if [ "$KAMAL_HOSTS" != "vm1,vm2,vm3" ]; then
|
||||
echo "Expected hosts to be 'vm1,vm2,vm3', got $KAMAL_HOSTS"
|
||||
exit 1
|
||||
fi
|
||||
mkdir -p /tmp/${TEST_ID} && touch /tmp/${TEST_ID}/pre-connect
|
||||
|
||||
@@ -37,3 +37,7 @@ accessories:
|
||||
- web
|
||||
stop_wait_time: 1
|
||||
readiness_delay: 0
|
||||
aliases:
|
||||
whome: version
|
||||
worker_hostname: app exec -r workers -q --reuse hostname
|
||||
uname: server exec -q -p uname
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#!/bin/bash
|
||||
|
||||
dockerd --max-concurrent-downloads 1 &
|
||||
dockerd --max-concurrent-downloads 1 --insecure-registry registry:4443 &
|
||||
|
||||
exec sleep infinity
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
#!/bin/sh
|
||||
|
||||
while [ ! -f /certs/domain.crt ]; do sleep 1; done
|
||||
|
||||
exec /entrypoint.sh /etc/docker/registry/config.yml
|
||||
|
||||
@@ -10,8 +10,6 @@ RUN mkdir ssh && \
|
||||
COPY registry-dns.conf .
|
||||
COPY boot.sh .
|
||||
|
||||
RUN mkdir certs && openssl req -newkey rsa:4096 -nodes -sha256 -keyout certs/domain.key -x509 -days 365 -out certs/domain.crt -subj '/CN=registry' -extensions EXT -config registry-dns.conf
|
||||
|
||||
HEALTHCHECK --interval=1s CMD pgrep sleep
|
||||
|
||||
CMD ["./boot.sh"]
|
||||
|
||||
@@ -5,7 +5,6 @@ WORKDIR /work
|
||||
RUN apt-get update --fix-missing && apt-get -y install openssh-client openssh-server docker.io
|
||||
|
||||
RUN mkdir /root/.ssh && ln -s /shared/ssh/id_rsa.pub /root/.ssh/authorized_keys
|
||||
RUN mkdir -p /etc/docker/certs.d/registry:4443 && ln -s /shared/certs/domain.crt /etc/docker/certs.d/registry:4443/ca.crt
|
||||
|
||||
RUN echo "HOST_TOKEN=abcd" >> /etc/environment
|
||||
|
||||
|
||||
@@ -4,6 +4,6 @@ while [ ! -f /root/.ssh/authorized_keys ]; do echo "Waiting for ssh keys"; sleep
|
||||
|
||||
service ssh restart
|
||||
|
||||
dockerd --max-concurrent-downloads 1 &
|
||||
dockerd --max-concurrent-downloads 1 --insecure-registry registry:4443 &
|
||||
|
||||
exec sleep infinity
|
||||
|
||||
@@ -82,6 +82,22 @@ class MainTest < IntegrationTest
|
||||
assert_equal({ "cmd"=>"wget -qO- http://localhost > /dev/null || exit 1", "interval"=>"1s", "max_attempts"=>3, "port"=>3000, "path"=>"/up", "cord"=>"/tmp/kamal-cord", "log_lines"=>50 }, config[:healthcheck])
|
||||
end
|
||||
|
||||
test "aliases" do
|
||||
@app = "app_with_roles"
|
||||
|
||||
kamal :envify
|
||||
kamal :deploy
|
||||
|
||||
output = kamal :whome, capture: true
|
||||
assert_equal Kamal::VERSION, output
|
||||
|
||||
output = kamal :worker_hostname, capture: true
|
||||
assert_match /App Host: vm3\nvm3-[0-9a-f]{12}$/, output
|
||||
|
||||
output = kamal :uname, "-o", capture: true
|
||||
assert_match "App Host: vm1\nGNU/Linux", output
|
||||
end
|
||||
|
||||
test "setup and remove" do
|
||||
# Check remove completes when nothing has been setup yet
|
||||
kamal :remove, "-y"
|
||||
|
||||
Reference in New Issue
Block a user