Module: Cast
- Defined in:
- lib/cast.rb
Constant Summary collapse
- VERSION =
'0.1.8'- DEFAULTGROUPS =
'~/.cast.yml'- @@mux =
Mutex.new
- @@pids =
[]
- @@groups =
nil
Class Method Summary collapse
- .expand_groups(cmdgroups, groups = @@groups) ⇒ Object
- .load_groups(groupfile) ⇒ Object
- .local(cmd, options = {}) ⇒ Object
- .log(msg, source = 'cast', stream = $stdout) ⇒ Object
- .remote(host, cmd, ssh = 'ssh') ⇒ Object
- .run(hosts, cmd, serial = false, delay = nil, ssh = 'ssh', strict = false) ⇒ Object
Class Method Details
.expand_groups(cmdgroups, groups = @@groups) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/cast.rb', line 41 def self. cmdgroups, groups = @@groups hosts = [] cmdgroups.each do |group| if groups.include? group hosts += groups[group] else hosts << group end end return hosts.uniq end |
.load_groups(groupfile) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/cast.rb', line 25 def self.load_groups groupfile file = File. groupfile exists = File.exists? file @@groups ||= {} if !exists raise "could not find #{groupfile}" if groupfile != DEFAULTGROUPS log "no group file found at #{groupfile}" else log "loading groups from #{groupfile}" @@groups = YAML.load_file file end return @@groups end |
.local(cmd, options = {}) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/cast.rb', line 82 def self.local cmd, = {} = {} if == nil prefix = [:prefix] r = nil blk = Proc.new do |stdin, stdout, stderr, wait_thr| @@pids << wait_thr.pid stdin.close while wait_thr.status streams = [] streams << stdout if stdout streams << stderr if stderr break unless streams.size > 0 ios = IO.select streams ios.first.each do |stream| if stream == stdout line = stream.gets if line == nil stdout.close stdout = nil next end log line, prefix elsif stream == stderr line = stream.gets if line == nil stderr.close stderr = nil next end log line, prefix, $stderr else raise "unrecognized stream #{stream}" end end end r = wait_thr.value end if [:clean_bundler_env] Bundler.with_clean_env do Open3.popen3(cmd, &blk) end else Open3.popen3(cmd, &blk) end if [:ensure] unless r.exitstatus == 0 raise "command failed: #{cmd}" end end return r.exitstatus end |
.log(msg, source = 'cast', stream = $stdout) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/cast.rb', line 18 def self.log msg, source = 'cast', stream = $stdout @@mux.synchronize do prefix = "[#{source}] " unless source == nil stream.puts "#{prefix}#{msg}" end end |
.remote(host, cmd, ssh = 'ssh') ⇒ Object
76 77 78 79 80 |
# File 'lib/cast.rb', line 76 def self.remote host, cmd, ssh = 'ssh' fullcmd = "#{ssh} #{host} '#{cmd}'" log "running #{fullcmd}" return local fullcmd, {:prefix => host} end |
.run(hosts, cmd, serial = false, delay = nil, ssh = 'ssh', strict = false) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/cast.rb', line 54 def self.run hosts, cmd, serial = false, delay = nil, ssh = 'ssh', strict = false success = true if serial or delay hosts.each_with_index do |host, i| exitCode = remote host, cmd, ssh if strict success &&= (exitCode == 0) end if delay and i < hosts.size - 1 log "delay for #{delay} seconds" sleep delay end end else exitCodes = hosts.pmap { |host| remote host, cmd, ssh } if strict success = (exitCodes.reduce(:+) == 0) end end return success end |