-
Notifications
You must be signed in to change notification settings - Fork 482
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Aliases are defined in the configuration file under the `aliases` key. The configuration is a map of alias name to command. When we run the command the we just do a literal replacement of the alias with the string. So if we have: ```yaml aliases: console: app exec -r console -i --reuse "rails console" ``` Then running `kamal console -r workers` will run the command ```sh $ kamal app exec -r console -i --reuse "rails console" -r workers ``` Because of the order Thor parses the arguments, this allows us to override the role from the alias command. There might be cases where we need to munge the command a bit more but that would involve getting into Thor command parsing internals, which are complicated and possibly subject to change. There's a chance that your aliases could conflict with future built-in commands, but there's not likely to be many of those and if it happens you'll get a validation error when you upgrade. Thanks to @dhnaranjo for the idea!
- Loading branch information
Showing
19 changed files
with
207 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
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 |
4 changes: 0 additions & 4 deletions
4
test/integration/docker/deployer/app/.kamal/hooks/pre-connect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
4 changes: 0 additions & 4 deletions
4
test/integration/docker/deployer/app_with_roles/.kamal/hooks/pre-connect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.