Module: Cast
- Defined in:
- lib/cast.rb
Constant Summary collapse
- VERSION =
'0.1.5'
- DEFAULTGROUPS =
'~/.cast.yml'
- @@mux =
Mutex.new
- @@pids =
[]
- @@groups =
nil
Class Method Summary collapse
- .ensure_local(cmd, prefix = nil) ⇒ Object
- .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') ⇒ Object
Class Method Details
.ensure_local(cmd, prefix = nil) ⇒ Object
74 75 76 77 78 |
# File 'lib/cast.rb', line 74 def self.ensure_local cmd, prefix = nil r = local cmd, prefix raise "command failed: #{cmd}" unless r == 0 return r end |
.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
80 81 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 143 144 145 |
# File 'lib/cast.rb', line 80 def self.local cmd, = {} prefix = nil if .is_a? String prefix = # this is for backwards compatibility elsif [:prefix] prefix = [:prefix] end 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
68 69 70 71 72 |
# File 'lib/cast.rb', line 68 def self.remote host, cmd, ssh = 'ssh' fullcmd = "#{ssh} #{host} '#{cmd}'" log "running #{fullcmd}" local fullcmd, host end |
.run(hosts, cmd, serial = false, delay = nil, ssh = 'ssh') ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/cast.rb', line 54 def self.run hosts, cmd, serial = false, delay = nil, ssh = 'ssh' if serial or delay hosts.each_with_index do |host, i| remote host, cmd, ssh if delay and i < hosts.size - 1 log "delay for #{delay} seconds" sleep delay end end else hosts.peach { |host| remote host, cmd, ssh } end end |