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.



181
182
183
184
# File 'lib/jkr/plan.rb', line 181

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

Class Method Details

.load_plan(plan) ⇒ Object



186
187
188
189
190
191
# File 'lib/jkr/plan.rb', line 186

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



427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/jkr/plan.rb', line 427

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



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

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



329
330
331
# File 'lib/jkr/plan.rb', line 329

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

#def_cleanup(&proc) ⇒ Object



326
327
328
# File 'lib/jkr/plan.rb', line 326

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

#def_parameters(&proc) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/jkr/plan.rb', line 253

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



323
324
325
# File 'lib/jkr/plan.rb', line 323

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

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



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

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



245
246
247
# File 'lib/jkr/plan.rb', line 245

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

#drop_cachesObject



423
424
425
# File 'lib/jkr/plan.rb', line 423

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

#exec_time_estimate(&proc) ⇒ Object



319
320
321
# File 'lib/jkr/plan.rb', line 319

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

#extend(base_plan_name) ⇒ Object



231
232
233
234
235
236
237
238
239
# File 'lib/jkr/plan.rb', line 231

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



358
359
360
# File 'lib/jkr/plan.rb', line 358

def jkr_env
  self.plan.jkr_env
end

#load_script(name) ⇒ Object



227
228
229
# File 'lib/jkr/plan.rb', line 227

def load_script(name)
  use_script(name, false)
end

#notify_im_kayac(username, message) ⇒ Object



388
389
390
391
# File 'lib/jkr/plan.rb', line 388

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



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

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

#parameter(arg = nil) ⇒ Object



333
334
335
336
337
338
339
340
341
# File 'lib/jkr/plan.rb', line 333

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



194
195
196
# File 'lib/jkr/plan.rb', line 194

def plan
  @plan
end

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

utility functions



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

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 Also known as: system_, sh



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

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

#sh_(*args) ⇒ Object



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

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

#short_desc(short_desc) ⇒ Object



249
250
251
# File 'lib/jkr/plan.rb', line 249

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

#su_sh(*args) ⇒ Object



412
413
414
415
416
# File 'lib/jkr/plan.rb', line 412

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



418
419
420
421
# File 'lib/jkr/plan.rb', line 418

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

#super_analysis(plan) ⇒ Object



310
311
312
313
314
315
316
317
# File 'lib/jkr/plan.rb', line 310

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



302
303
304
305
306
307
308
# File 'lib/jkr/plan.rb', line 302

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



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

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



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

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



241
242
243
# File 'lib/jkr/plan.rb', line 241

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

#use_script(name, use = true) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/jkr/plan.rb', line 198

def use_script(name, use = true)
  # 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
    if use
      @plan.used_scripts.push(path)
    end
  else
    raise RuntimeError.new("Cannot use script: #{name}")
  end
end

#variable(arg = nil) ⇒ Object



362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/jkr/plan.rb', line 362

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