Class: Jkr::Plan::PlanLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/jkr/plan.rb

Defined Under Namespace

Classes: PlanParams

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plan) ⇒ PlanLoader

Returns a new instance of PlanLoader.



171
172
173
174
# File 'lib/jkr/plan.rb', line 171

def initialize(plan)
  @plan = plan
  @params = nil
end

Class Method Details

.load_plan(plan) ⇒ Object



176
177
178
179
180
181
# File 'lib/jkr/plan.rb', line 176

def self.load_plan(plan)
  plan_loader = self.new(plan)
  plan.src = File.open(plan.file_path, "r").read
  plan_loader.instance_eval(plan.src, plan.file_path, 1)
  plan
end

Instance Method Details

#checkout_git(dir, repo_url, branch) ⇒ Object



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/jkr/plan.rb', line 407

def checkout_git(dir, repo_url, branch)
  if ! File.directory?(dir)
    sh! "git clone #{repo_url} #{dir}"
  end

  Dir.chdir(dir) do
    if ! File.exists?(".git")
      sh! "git clone #{repo_url} ."
    end

    sh "git checkout -t origin/#{branch}"
    sh! "git pull"

    sh! "git rev-parse HEAD > #{@plan.resultset_dir + '/git-commit.log'}"
  end
end

#constant(arg = nil) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/jkr/plan.rb', line 327

def constant(arg = nil)
  if arg.is_a? Hash
    arg.keys.each do |const_name|
      if @params.vars.keys.include?(const_name)
        raise Jkr::ParameterError.new("#{const_name} is already defined as variable")
      end
    end

    # set param
    @params.params.merge!(arg)
  else
    raise ArgumentError.new
  end
end

#def_analysis(&proc) ⇒ Object



313
314
315
# File 'lib/jkr/plan.rb', line 313

def def_analysis(&proc)
  @plan.analysis = proc
end

#def_cleanup(&proc) ⇒ Object



310
311
312
# File 'lib/jkr/plan.rb', line 310

def def_cleanup(&proc)
  @plan.cleanup = proc
end

#def_parameters(&proc) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/jkr/plan.rb', line 237

def def_parameters(&proc)
  @params = PlanParams.new
  proc.call()

  consts = @params.params
  vars   = @params.vars

  if @plan.base_plan
    consts.keys.each do |const|
      if ! @plan.params.include?(const)
        raise Jkr::ParameterError.new("#{const} is not defined in base plan: #{@plan.base_plan.title}")
      end
    end

    vars.keys.each do |var|
      if ! @plan.vars.include?(var)
        raise Jkr::ParameterError.new("#{var} is not defined in base plan: #{@plan.base_plan.title}")
      end
    end
  end

  @plan.params.merge!(@params.params)
  @plan.vars.merge!(@params.vars)
end

#def_prep(&proc) ⇒ Object



307
308
309
# File 'lib/jkr/plan.rb', line 307

def def_prep(&proc)
  @plan.prep = proc
end

#def_routine(options = {}, &proc) ⇒ Object



262
263
264
265
266
267
# File 'lib/jkr/plan.rb', line 262

def def_routine(options = {}, &proc)
  if options[:nr_run]
    @plan.routine_nr_run = options[:nr_run]
  end
  @plan.routine = proc
end

#description(plan_desc) ⇒ Object



229
230
231
# File 'lib/jkr/plan.rb', line 229

def description(plan_desc)
  @plan.desc = plan_desc.to_s
end

#drop_cachesObject



403
404
405
# File 'lib/jkr/plan.rb', line 403

def drop_caches()
  su_sh('echo 1 > /proc/sys/vm/drop_caches')
end

#exec_time_estimate(&proc) ⇒ Object



303
304
305
# File 'lib/jkr/plan.rb', line 303

def exec_time_estimate(&proc)
  @plan.exec_time_estimate = proc
end

#extend(base_plan_name) ⇒ Object



215
216
217
218
219
220
221
222
223
# File 'lib/jkr/plan.rb', line 215

def extend(base_plan_name)
  base_plan = Plan.create_by_name(self.plan.jkr_env, base_plan_name.to_s,
                                  :plan_search_path => @plan.plan_search_path,
                                  :script_search_path => @plan.script_search_path)
  self.plan.base_plan = base_plan

  @plan.params.merge!(base_plan.params)
  @plan.vars.merge!(base_plan.vars)
end

#jkr_envObject



342
343
344
# File 'lib/jkr/plan.rb', line 342

def jkr_env
  self.plan.jkr_env
end

#notify_im_kayac(username, message) ⇒ Object



372
373
374
375
# File 'lib/jkr/plan.rb', line 372

def notify_im_kayac(username, message)
  Net::HTTP.post_form(URI.parse("http://im.kayac.com/api/post/#{username}"),
                      {'message'=>message})
end

#param_filter(&proc) ⇒ Object



360
361
362
# File 'lib/jkr/plan.rb', line 360

def param_filter(&proc)
  @plan.param_filters.push(proc)
end

#parameter(arg = nil) ⇒ Object



317
318
319
320
321
322
323
324
325
# File 'lib/jkr/plan.rb', line 317

def parameter(arg = nil)
  if arg.is_a? Hash
    # set param
    $stderr.puts("'parameter' is deprecated. use 'constant' instead.")
    constant(arg)
  else
    @params
  end
end

#planObject

Functions for describing plans in ‘.plan’ files below



184
185
186
# File 'lib/jkr/plan.rb', line 184

def plan
  @plan
end

#send_mail(subject, addrs, body, files = []) ⇒ Object

utility functions



365
366
367
368
369
370
# File 'lib/jkr/plan.rb', line 365

def send_mail(subject, addrs, body, files = [])
  attach_option = files.map{|file| "-a #{file}"}.join(" ")
  IO.popen("mutt #{addrs.join(' ')} -s #{subject.inspect} #{attach_option}", "w+") do |io|
    io.puts body
  end
end

#sh(*args) ⇒ Object



377
378
379
380
# File 'lib/jkr/plan.rb', line 377

def sh(*args)
  puts "sh: #{args.join(' ')}"
  return system(*args)
end

#sh!(*args) ⇒ Object Also known as: system_



382
383
384
385
386
387
388
# File 'lib/jkr/plan.rb', line 382

def sh!(*args)
  puts "sh!: #{args.join(' ')}"
  unless system(*args)
    raise RuntimeError.new(args.join(" "))
  end
  true
end

#short_desc(short_desc) ⇒ Object



233
234
235
# File 'lib/jkr/plan.rb', line 233

def short_desc(short_desc)
  @plan.short_desc = short_desc.gsub(/ /, '_').gsub(/\//, '!')
end

#su_sh(*args) ⇒ Object



392
393
394
395
396
# File 'lib/jkr/plan.rb', line 392

def su_sh(*args)
  puts "su_sh: #{args.join(' ')}"
  su_cmd = File.expand_path("../su_cmd", __FILE__)
  system_(su_cmd, args.join(' '))
end

#sudo_sh(*args) ⇒ Object



398
399
400
401
# File 'lib/jkr/plan.rb', line 398

def sudo_sh(*args)
  puts "sudo_sh: #{args.join(' ')}"
  system_((["sudo"] + args).join(' '))
end

#super_analysis(plan) ⇒ Object



294
295
296
297
298
299
300
301
# File 'lib/jkr/plan.rb', line 294

def super_analysis(plan)
  if @plan.base_plan == nil
    RuntimeError.new("No super plan.")
  else
    @plan.base_plan.resultset_dir = @plan.resultset_dir
    @plan.base_plan.do_analysis(plan)
  end
end

#super_cleanup(plan) ⇒ Object



286
287
288
289
290
291
292
# File 'lib/jkr/plan.rb', line 286

def super_cleanup(plan)
  if @plan.base_plan == nil
    RuntimeError.new("No super plan.")
  else
    @plan.base_plan.do_cleanup(plan)
  end
end

#super_prep(plan) ⇒ Object



278
279
280
281
282
283
284
# File 'lib/jkr/plan.rb', line 278

def super_prep(plan)
  if @plan.base_plan == nil
    RuntimeError.new("No super plan.")
  else
    @plan.base_plan.do_prep(plan)
  end
end

#super_routine(plan, params) ⇒ Object

call routine of super plan



270
271
272
273
274
275
276
# File 'lib/jkr/plan.rb', line 270

def super_routine(plan, params)
  if @plan.base_plan == nil
    RuntimeError.new("No super plan.")
  else
    @plan.base_plan.do_routine(plan, params)
  end
end

#title(plan_title) ⇒ Object



225
226
227
# File 'lib/jkr/plan.rb', line 225

def title(plan_title)
  @plan.title = plan_title.to_s
end

#use_script(name) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/jkr/plan.rb', line 188

def use_script(name)
  # find script file
  if name.is_a? Symbol
    name = name.to_s + ".rb"
  elsif ! (name =~ /\.rb$/)
    name += ".rb"
  end

  path = nil
  search_dirs = @plan.script_search_path
  while ! search_dirs.empty?
    dir = search_dirs.shift
    path = File.expand_path(name, dir)

    if File.exists?(path)
      break
    end
  end

  if path
    load path
    @plan.used_scripts.push(path)
  else
    raise RuntimeError.new("Cannot use script: #{name}")
  end
end

#variable(arg = nil) ⇒ Object



346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/jkr/plan.rb', line 346

def variable(arg = nil)
  if arg.is_a? Hash
    arg.keys.each do |var_name|
      if @params.params.keys.include?(var_name)
        raise Jkr::ParameterError.new("#{var_name} is already defined as constant")
      end
    end

    @params.vars.merge!(arg)
  else
    raise ArgumentError.new
  end
end