Class: PuppetBox::PuppetBox

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

Constant Summary collapse

WORKING_DIR =
File.join(Dir.home, '.puppetbox')

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil, nodeset_file: nil, working_dir: nil) ⇒ PuppetBox

Returns a new instance of PuppetBox.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/puppetbox/puppetbox.rb', line 12

def initialize(logger:nil, nodeset_file: nil, working_dir: nil)
  # The results of all tests on all driver instances
  @result_set = ResultSet.new

  # A complete test suite of tests to run - includes driver instance, host
  # and classes
  @testsuite = {}
  @logger = Logger.new(logger).logger

  # the nodesets file contains a YAML representation of a hash containing
  # the node name, config options, driver to use etc - so external tools can
  # talk to use about nodes of particular name and puppetbox will sort out
  # the how and why of what this exactly should involve
  @nodeset = NodeSet.new(nodeset_file)

  @working_dir = working_dir || WORKING_DIR
end

Instance Method Details

#enqueue_test(node_name, run_from, puppet_class) ⇒ Object

Enqueue a test into the testsuite for



32
33
34
35
36
37
38
39
40
# File 'lib/puppetbox/puppetbox.rb', line 32

def enqueue_test(node_name, run_from, puppet_class)
  instantiate_driver(node_name, run_from)
  @testsuite[node_name]["classes"] << puppet_class
  #  get_driver_instance(driver_name, host)
  # node_name
  # puppet_class.name,
  # nodeset_yaml["HOSTS"][node.name]['config'],
  # @repo,
end

#instantiate_driver(node_name, run_from) ⇒ Object



48
49
50
51
52
53
54
55
56
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
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
# File 'lib/puppetbox/puppetbox.rb', line 48

def instantiate_driver(node_name, run_from)
  node    = @nodeset.get_node(node_name)
  config  = node["config"]
  driver  = node["driver"]
  if @testsuite.has_key?(node_name)
    @logger.debug("#{node_name} already registered")
  else
    @logger.debug("Creating new driver instance for #{node_name}")

    # for now just support the vagrant driver
    case driver
    when "vagrant"

      # For the moment, just use the checked out production environment inside
      # onceover's working directory.  The full path resolves to something like
      # .onceover/etc/puppetlabs/code/environments/production -- in the
      # directory your running onceover from
      #
      # we pass in our pre-configured logger instance for separation and to
      # reduce the amount of log output.  We only print puppet apply output for
      # failed runs, however, if user runs in onceover's --debug mode, then we
      # will print the customary ton of messages, including those from vagrant
      # itself.
      puts config["box"]
      di = Driver::Vagrant.new(
        node_name,
        run_from,
        config,
        # "#{repo.tempdir}/etc/puppetlabs/code/environments/production",
        logger: @logger,
        working_dir: @working_dir,
      )

      # immediately validate the configuration to allow us to fail-fast
      di.validate_config
    else
      raise "PuppetBox only supports driver: 'vagrant' at the moment (requested: #{driver})"
    end

    @testsuite[node_name] = {
      "instance" => di,
      "classes"  => [],
    }
  end

  # di
  # result = ::PuppetBox.run_puppet(di, puppet_class)

  # indent = "  "
  # if result.passed
  #   logger.info("#{indent}#{host}:#{puppet_class} --> PASSED")
  # else
  #   logger.error("#{indent}#{host}:#{puppet_class} --> FAILED")
  #   # since we stop running on failure, the error messages will be in the
  #   # last element of the result.messages array (tada!)
  #   messages = result.messages
  #   messages[-1].each { |line|
  #     # puts "XXXXXXX #{line}"
  #     logger.error "#{indent}#{host} - #{line}"
  #   }
  #   #   puts "size of result messages #{result.messages.size}"
  #   #   puts "size of result messages #{result.messages[0].size}"
  #   #   run.each { |message_arr|
  #   #     puts message_arr
  #   #     #message_arr.each { |line|
  #   #   #    puts line
  #   #   #  }
  #   #     # require 'pry'
  #   #     # binding.pry
  #   #     #puts "messages size"
  #   #     #puts messages.size
  #   #   #  messages.each { |message|
  #   #       # messages from the puppet run are avaiable in a nested array of run
  #   #       # and then lines so lets print each one out indended from the host so
  #   #       # we can see what's what
  #   #   #    logger.error("#{host}       #{message}")
  #   #   #  }
  #   #   }
  #   # }
  # end
  # result.passed
end

#passed?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/puppetbox/puppetbox.rb', line 141

def passed?
  @result_set.passed?
end

Print all results to STDOUT



133
134
135
# File 'lib/puppetbox/puppetbox.rb', line 133

def print_results
  Report::print(@result_set)
end

#result_setObject



137
138
139
# File 'lib/puppetbox/puppetbox.rb', line 137

def result_set
  @result_set
end

#run_puppet(driver_instance, puppet_classes, logger: nil, reset_after_run: true) ⇒ Object

Run puppet using driver_instance to execute



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/puppetbox/puppetbox.rb', line 146

def run_puppet(driver_instance, puppet_classes, logger:nil, reset_after_run:true)
  # use supplied logger in preference to the default puppetbox logger instance
  logger = logger || @logger
  logger.debug("#{driver_instance.node_name} running test for #{puppet_classes}")
  puppet_classes = Array(puppet_classes)

  if driver_instance.open
    logger.debug("#{driver_instance.node_name} started")
    if driver_instance.self_test
      logger.debug("#{driver_instance.node_name} self_test OK, running puppet")
      puppet_classes.each{ |puppet_class|
        if @result_set.class_size(driver_instance.node_name) > 0 and reset_after_run
          # purge and reboot the vm - this will save approximately 1 second
          # per class on the self-test which we now know will succeed
          driver_instance.reset
        end
        driver_instance.run_puppet_x2(puppet_class)
        @result_set.save(driver_instance.node_name, puppet_class, driver_instance.result)
      }
      logger.debug("#{driver_instance.node_name} test completed, closing instance")
    else
      raise "#{driver_instance.node_name} self test failed, unable to continue"
    end
  else
    raise "#{driver_instance.node_name} failed to start, unable to continue"
  end

  driver_instance.close
end

#run_testsuiteObject



42
43
44
45
46
# File 'lib/puppetbox/puppetbox.rb', line 42

def run_testsuite
  @testsuite.each { |id, tests|
    run_puppet(tests["instance"], tests["classes"], logger:@logger, reset_after_run:true)
  }
end