Module: ClusterBomb::Bomb

Includes:
Logging, Roles
Included in:
Cli
Defined in:
lib/cluster_bomb/bomb.rb

Overview

Task Runner module Loads task files and runs tasks All tasks run within the context of the class implementing this module

Defined Under Namespace

Classes: Config, Task

Constant Summary

Constants included from Logging

Logging::DEFAULT_LOGFILENAME

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

log, log_disable, log_enable, log_init, puts, #puts

Methods included from Roles

#clear_role, #role, #servers, #valid_role?

Instance Attribute Details

#auto_reloadObject

Returns the value of attribute auto_reload.



38
39
40
# File 'lib/cluster_bomb/bomb.rb', line 38

def auto_reload
  @auto_reload
end

#configurationObject

Returns the value of attribute configuration.



38
39
40
# File 'lib/cluster_bomb/bomb.rb', line 38

def configuration
  @configuration
end

#envObject

Returns the value of attribute env.



38
39
40
# File 'lib/cluster_bomb/bomb.rb', line 38

def env
  @env
end

#interactiveObject

Returns the value of attribute interactive.



38
39
40
# File 'lib/cluster_bomb/bomb.rb', line 38

def interactive
  @interactive
end

#sudo_modeObject

Returns the value of attribute sudo_mode.



38
39
40
# File 'lib/cluster_bomb/bomb.rb', line 38

def sudo_mode
  @sudo_mode
end

#usernameObject

Returns the value of attribute username.



38
39
40
# File 'lib/cluster_bomb/bomb.rb', line 38

def username
  @username
end

Instance Method Details

#clearObject



249
250
251
# File 'lib/cluster_bomb/bomb.rb', line 249

def clear
  @cluster.clear!
end

#clear_env!Object



134
135
136
137
138
139
# File 'lib/cluster_bomb/bomb.rb', line 134

def clear_env!
  env.each_key do |k|
    self.instance_eval("undef #{k.to_s}; undef #{k.to_s}=")
  end
  self.env={}
end

#desc(str) ⇒ Object



64
65
66
# File 'lib/cluster_bomb/bomb.rb', line 64

def desc(str)
  @current_desription=str
end

#disconnect!Object



240
241
242
# File 'lib/cluster_bomb/bomb.rb', line 240

def disconnect!
  @cluster.disconnect!
end

#download(remote, local, options = {}, &task) ⇒ Object



174
175
176
# File 'lib/cluster_bomb/bomb.rb', line 174

def download(remote, local, options={}, &task)
  @cluster.download(remote, local, options={}, &task)
end

#ensure_var(name, value = nil) ⇒ Object



125
126
127
128
# File 'lib/cluster_bomb/bomb.rb', line 125

def ensure_var(name, value=nil)
  return if env.has_key? name.to_sym
  set(name, value)
end

#exec(name, options = {}) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/cluster_bomb/bomb.rb', line 152

def exec(name, options={}) 
  sudo_save = @sudo_mode
  # @cluster.reset!
  server_list=server_list_from_options(options)
  t = @tasks[name]
  raise "TASK NOT FOUND: #{name}" unless t
  @sudo_mode = true if t.sudo # Turn it on if sudo is true
  raise "Task not found: #{name}" if t.nil?      
  if self.auto_reload && t.updated?
    puts "Reloading #{t.filename}"
    reload(t.filename)
    t = @tasks[name]
  end
  raise "Task not found: #{name}" if t.nil?
  server_list = self.servers(t.roles) if server_list.empty?
  # puts "CONNECTED: #{@cluster.connected?}"
  @cluster.connect!(server_list) unless @cluster.connected? && (!options[:roles] && !options[:hosts])
  raise "Task #{name} not found" unless t
  t.proc.call
  @sudo_mode = sudo_save
end

#exists?(attrname) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/cluster_bomb/bomb.rb', line 130

def exists?(attrname)
  env.has_key? attrname.to_sym
end

#get_task(name) ⇒ Object



236
237
238
# File 'lib/cluster_bomb/bomb.rb', line 236

def get_task(name)
  @tasks[name]      
end

#group(str) ⇒ Object



60
61
62
# File 'lib/cluster_bomb/bomb.rb', line 60

def group(str)
  @current_group=str
end

#hostsObject



252
253
254
# File 'lib/cluster_bomb/bomb.rb', line 252

def hosts
  @cluster.hosts
end

#initializeObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cluster_bomb/bomb.rb', line 39

def initialize
  @sudo_mode = false
  @tasks||={}
  @cluster||=nil
  self.env={}   
  @reloading=false
  @current_load_file=nil
  @current_load_file_time=nil
  self.auto_reload=true
  @configuration = Config.new
  @configuration.load!
  @username = @configuration.username
  raise "Unable to get a default user name. Exiting..." unless @username
  @interactive=false
  super
end

#interactive?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/cluster_bomb/bomb.rb', line 56

def interactive?
  @interactive
end

#load(fn) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/cluster_bomb/bomb.rb', line 104

def load(fn)
  @current_group=fn.split('/').last
  s = File.read(fn)
  @current_load_file=fn
  @current_load_file_time=File.stat(fn).mtime
  self.load_str(s)
end

#load_str(str) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/cluster_bomb/bomb.rb', line 85

def load_str(str)
  begin
    self.instance_eval(str)
  rescue Exception => e
    puts "Exception while loading: #{@current_load_file}"
    raise e
  end
  ssh_options = @configuration.ssh_options(username)
  @cluster = Cluster.new(username,ssh_options) unless @cluster
  @current_load_file=nil
  @current_load_file_time=nil           
end

#reconnect!(roles) ⇒ Object

primarily for shell use to change roles



227
228
229
230
# File 'lib/cluster_bomb/bomb.rb', line 227

def reconnect!(roles)
  @cluster.reset!
  @cluster.connect!(self.servers(roles))
end

#reload(fn) ⇒ Object



98
99
100
101
102
# File 'lib/cluster_bomb/bomb.rb', line 98

def reload(fn)
  @reloading=true
  load(fn)
  @reloading=false
end

#role_listObject



78
79
80
81
82
83
84
# File 'lib/cluster_bomb/bomb.rb', line 78

def role_list
  ret=[]
  @roles.each do |k,v|
    ret << {:name=>k, :hostnames=>v}
  end
  ret
end

#run(command, options = {}, &task) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
# File 'lib/cluster_bomb/bomb.rb', line 204

def run(command, options={}, &task)
  server_list=server_list_from_options(options)
  if !server_list.empty?
    @cluster.reset!
    @cluster.connect!(server_list)
  end
  options[:sudo] =  @sudo_mode unless options.has_key? :sudo
  # use max_run_time environment variable if not passed in by caller
  options[:max_run_time] = self.configuration.max_run_time unless options[:max_run_time]
  @cluster.run(command, options, &task)
end

#server_list_from_options(options) ⇒ Object



216
217
218
219
220
221
222
223
224
# File 'lib/cluster_bomb/bomb.rb', line 216

def server_list_from_options(options)
  server_list=[]
  if options[:roles]
    server_list = self.servers(options[:roles])
  elsif options[:hosts]
    server_list=options[:hosts]
  end
  server_list
end

#set(name, value = nil) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/cluster_bomb/bomb.rb', line 112

def set(name, value=nil)
  self.env[name.to_sym]=value
  code=<<-EODEF
  def #{name}
    self.env[:#{name}]
  end
  def #{name}=(rhs)
    self.env[:#{name}]=rhs
  end      
  EODEF
  self.instance_eval(code)
end

#switch_user(user) ⇒ Object



141
142
143
144
145
146
147
148
149
150
# File 'lib/cluster_bomb/bomb.rb', line 141

def switch_user(user)
  if @configuration.has_ssh_options? user
    ssh_options = @configuration.ssh_options(user)
  else
    ssh_options={} # Use ssh agent or config file
  end
  @username = ssh_options[:user] || user
  @cluster.credentials(@username, ssh_options)
  @cluster.disconnect!
end

#task(name, options = {}, &task) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/cluster_bomb/bomb.rb', line 68

def task(name, options={}, &task)
  t = Task.new(name, @current_group, @current_load_file,@current_load_file_time, options )
  raise "task #{t.name} is already defined" if @tasks[t.name] && !@reloading
  @tasks[t.name]=t
  t.proc = task
  t.roles=options[:roles]
  t.description=@current_desription
  
  @current_description=''
end

#task_listObject



244
245
246
247
248
# File 'lib/cluster_bomb/bomb.rb', line 244

def task_list
  ret=[]
  @tasks.each{|k,v| ret << v }
  ret
end

#upload(local, remote, options = {}, &task) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/cluster_bomb/bomb.rb', line 178

def upload(local, remote, options={}, &task)
  files=[]
  
  server_list=[]
  if options[:roles]
    server_list = self.servers(options[:roles])
  elsif options[:hosts]
    server_list=options[:hosts]
  end
  if !server_list.empty?
    @cluster.reset!
    @cluster.connect!(server_list)
  end
        
  if local.index('*')
    files=Dir.glob(local)
  else
    files << local
  end
  files.each do |f|
    next if File.stat(f).directory?
    puts "Uploading: #{f}"
    @cluster.upload(f, remote, options, &task)
  end
end

#valid_task?(name) ⇒ Boolean

Returns:

  • (Boolean)


232
233
234
# File 'lib/cluster_bomb/bomb.rb', line 232

def valid_task?(name)
  @tasks[name] ? true : false
end