Class: Rundock::Runner

Inherits:
Object show all
Defined in:
lib/rundock/runner.rb

Constant Summary collapse

ScenarioNotFoundError =
Class.new(StandardError)
RUNDOCK_PLUGINS =
%w[operation hook]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Runner

Returns a new instance of Runner.



22
23
24
# File 'lib/rundock/runner.rb', line 22

def initialize(options)
  @options = options
end

Instance Attribute Details

#scenarioObject (readonly)

Returns the value of attribute scenario.



20
21
22
# File 'lib/rundock/runner.rb', line 20

def scenario
  @scenario
end

Class Method Details

.run(options) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/rundock/runner.rb', line 10

def run(options)
  Logger.debug 'Starting Rundock:'

  runner = self.new(options)
  runner.load_plugins
  runner.build(options)
  runner.run
end

Instance Method Details

#build(options) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rundock/runner.rb', line 30

def build(options)
  if options[:scenario] || options[:targetgroup]
    raise ScenarioNotFoundError, "'#{options[:scenario]}' scenario file is not found." if options[:scenario] &&
                                                                                          !FileTest.exist?(options[:scenario])
    raise ScenarioNotFoundError, "'#{options[:targetgroup]}' targetgroup file is not found." if options[:command] &&
                                                                                                options[:targetgroup] &&
                                                                                                !FileTest.exist?(options[:targetgroup])

    options[:scenario] = options[:targetgroup] if options[:command] && options[:targetgroup]

    # parse scenario
    if options[:scenario] =~ %r{^(http|https)://}
      # read from http/https
      open(options[:scenario]) do |f|
        @scenario = Rundock::Builder::ScenarioBuilder.new(options, f).build
      end
    else
      File.open(options[:scenario]) do |f|
        @scenario = Rundock::Builder::ScenarioBuilder.new(options, f).build
      end
    end
  else
    # do rundock ssh
    @scenario = Rundock::Builder::ScenarioBuilder.new(options, nil).build
  end
end

#load_pluginsObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rundock/runner.rb', line 57

def load_plugins
  # load from current lib dir(for development)
  Dir.glob('./lib/rundock/plugin/**/*.rb').each do |f|
    require f.gsub(/.rb$/, '')
  end

  # load from local project
  Dir.glob("#{__dir__}/plugin/**/*.rb").each do |f|
    require f.gsub(/.rb$/, '')
  end

  # load from installed gems
  gems = []
  Gem::Specification.each do |gem|
    gems << gem.name
  end
  gems.uniq!

  gems.each do |g|
    RUNDOCK_PLUGINS.each do |plugin|
      next if g !~ /^(rundock-plugin-#{plugin})-/
      next if Gem::Specification.find_by_name(g).nil?
      Logger.debug("Loading rundock plugin: #{g}")
      libdir = "#{Gem::Specification.find_by_name(g).full_gem_path}/lib/#{Regexp.last_match(1).tr('-', '/')}"
      Dir.glob("#{libdir}/*.rb").each do |f|
        require f.gsub(/.rb$/, '')
      end
    end
  end
end

#runObject



26
27
28
# File 'lib/rundock/runner.rb', line 26

def run
  @scenario.run
end