-
Notifications
You must be signed in to change notification settings - Fork 2
/
dsl.rb
62 lines (50 loc) · 2.11 KB
/
dsl.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
module Capistrano
module Itamae
module DSL
require "bundler"
DEFAULT_RECIPE = "default.rb"
# Run `itamae ssh`
# @param recipe_files [String, Array<String>]
# @param options [String] itamae ssh options
# @param environment [Hash] environment variables. (passed to `with`)
def itamae_ssh(recipe_files = DEFAULT_RECIPE, options: nil, environment: {})
recipe_paths = Array(recipe_files).map { |file| itamae_cookbooks_path.join(file) }
itamae_options = [options, itamae_ssh_default_options].compact
# NOTE: store server (`host` is changed to localhost in `run_locally`)
server = host
run_locally do
Bundler.with_original_env do
with environment do
execute(*generate_itamae_ssh_command(server, recipe_paths, itamae_options))
end
end
end
end
private
def generate_itamae_ssh_command(server, recipe_paths, itamae_options)
command = [:bundle, :exec, itamae_bin_name, :ssh]
command += recipe_paths
command += itamae_options unless itamae_options.empty?
command += ssh_options(server)
command
end
# via. https://github.com/unosk/capistano-with-terraform-and-itamae/blob/feb0d7d90f5c88412af9728586e598742dd36761/lib/capistrano/tasks/itamae.rake#L38-L51
def ssh_options(server)
ssh_options = fetch(:ssh_options, {}).dup
ssh_options.merge!(server.ssh_options) if server.ssh_options
ssh_options[:user] ||= server.user
ssh_options[:port] ||= server.port
ssh_options[:keys] ||= server.keys
ssh_options[:key] ||= ssh_options[:keys].first if ssh_options[:keys] && !ssh_options[:keys].empty?
options = []
options << "--host #{server.hostname}"
options << "--user #{ssh_options[:user]}" if ssh_options[:user]
options << "--port #{ssh_options[:port]}" if ssh_options[:port]
options << "--key #{ssh_options[:key]}" if ssh_options[:key]
options << "--dry-run" if dry_run?
options
end
end
end
end
include Capistrano::Itamae::DSL