This repository has been archived by the owner on Dec 18, 2019. It is now read-only.
/
dsh.rb
63 lines (57 loc) · 2.79 KB
/
dsh.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
63
module Deployinator
module Helpers
module DshHelpers
def dsh_fanout
@dsh_fanout || 30
end
def ignore_failure_command
" || test 0 -eq 0"
end
def group_option_for_dsh(groups)
groups = [groups] unless groups.is_a?(Array)
groups.map {|group| "-g #{group} "}.join("")
end
def run_dsh(groups, cmd, only_stdout=true, timing_metric=nil, log_errors=true, ignore_failure=false, secrets=[], &block)
dsh_groups = group_option_for_dsh(groups)
ignore_failure = ignore_failure ? ignore_failure_command : ""
cmd_return = run_cmd(%Q{ssh #{Deployinator.default_user}@#{Deployinator.deploy_host} dsh #{dsh_groups} -r ssh -F #{dsh_fanout} "#{cmd}"#{ignore_failure}}, timing_metric, log_errors, secrets, &block)
if only_stdout
cmd_return[:stdout]
else
cmd_return
end
end
# run dsh against a given host or array of hosts
def run_dsh_hosts(hosts, cmd, extra_opts='', only_stdout=true, timing_metric=nil, log_errors=true, ignore_failure=false, secrets=[], &block)
hosts = [hosts] unless hosts.is_a?(Array)
ignore_failure = ignore_failure ? ignore_failure_command : ""
if extra_opts.length > 0
run_cmd %Q{ssh #{Deployinator.default_user}@#{Deployinator.deploy_host} 'dsh -m #{hosts.join(',')} -r ssh -F #{dsh_fanout} #{extra_opts} -- "#{cmd}"#{ignore_failure}'}, timing_metric, log_errors, secrets, &block
else
run_cmd %Q{ssh #{Deployinator.default_user}@#{Deployinator.deploy_host} 'dsh -m #{hosts.join(',')} -r ssh -F #{dsh_fanout} -- "#{cmd}" #{ignore_failure}'}, timing_metric, log_errors, secrets, &block
end
end
def run_dsh_extra(groups, cmd, extra_opts, only_stdout=true, timing_metric=nil, log_errors=true, ignore_failure=false, secrets=[], &block)
dsh_groups = group_option_for_dsh(groups)
ignore_failure = ignore_failure ? ignore_failure_command : ""
cmd_return = run_cmd(%Q{ssh #{Deployinator.default_user}@#{Deployinator.deploy_host} dsh #{dsh_groups} -r ssh #{extra_opts} -F #{dsh_fanout} "#{cmd}"#{ignore_failure} }, timing_metric, log_errors, secrets, &block)
if only_stdout
cmd_return[:stdout]
else
cmd_return
end
end
def hosts_for(group)
@hosts_for ||= {}
@hosts_for[group] ||= begin
dsh_file = "/home/#{Deployinator.default_user}/.dsh/group/#{group}"
hosts = `ssh #{Deployinator.default_user}@#{Deployinator.deploy_host} cat #{dsh_file}`.chomp
if $?.nil? || $?.exitstatus != 0
raise "DSH hosts file at #{Deployinator.deploy_host}:#{dsh_file} is likely missing!"
end
hosts.split("\n").delete_if { |x| x.lstrip[0..0] == "#" }
end
end
end
end
end