Class: BarkingIguana::Compound::TestStage
- Inherits:
-
Object
- Object
- BarkingIguana::Compound::TestStage
- Extended by:
- Forwardable
- Includes:
- Benchmark, Logging::Helper
- Defined in:
- lib/barking_iguana/compound/test_stage.rb
Instance Attribute Summary collapse
-
#directory ⇒ Object
Returns the value of attribute directory.
-
#test ⇒ Object
Returns the value of attribute test.
Instance Method Summary collapse
- #actions ⇒ Object
- #ansible_verbosity ⇒ Object
- #clean_up ⇒ Object
- #converge ⇒ Object
- #display_name ⇒ Object
- #generate_inventory ⇒ Object
- #generated_inventory ⇒ Object
-
#initialize(test, directory) ⇒ TestStage
constructor
A new instance of TestStage.
- #inventory_path ⇒ Object
- #name ⇒ Object
- #original_inventory ⇒ Object
- #playbook ⇒ Object
- #playbook_logger ⇒ Object
- #playbook_path ⇒ Object
- #results_file ⇒ Object
- #run ⇒ Object
- #setup ⇒ Object
- #stage_directory ⇒ Object
- #stage_file_with_fallback(file_name) ⇒ Object
- #tmp_dir(*sub_path) ⇒ Object
- #verify ⇒ Object
Constructor Details
#initialize(test, directory) ⇒ TestStage
Returns a new instance of TestStage.
8 9 10 11 |
# File 'lib/barking_iguana/compound/test_stage.rb', line 8 def initialize test, directory self.test = test self.directory = directory end |
Instance Attribute Details
#directory ⇒ Object
Returns the value of attribute directory.
6 7 8 |
# File 'lib/barking_iguana/compound/test_stage.rb', line 6 def directory @directory end |
#test ⇒ Object
Returns the value of attribute test.
6 7 8 |
# File 'lib/barking_iguana/compound/test_stage.rb', line 6 def test @test end |
Instance Method Details
#actions ⇒ Object
13 14 15 |
# File 'lib/barking_iguana/compound/test_stage.rb', line 13 def actions i(setup converge verify).freeze end |
#ansible_verbosity ⇒ Object
101 102 103 104 |
# File 'lib/barking_iguana/compound/test_stage.rb', line 101 def ansible_verbosity return 2 unless ENV['ANSIBLE_VERBOSITY'] ENV['ANSIBLE_VERBOSITY'].to_i end |
#clean_up ⇒ Object
138 139 140 141 |
# File 'lib/barking_iguana/compound/test_stage.rb', line 138 def clean_up logger.debug { "Removing temporary directory for stage #{name} from #{tmp_dir}" } FileUtils.rm_r tmp_dir end |
#converge ⇒ Object
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/barking_iguana/compound/test_stage.rb', line 127 def converge unless File.exists? playbook_path logger.debug { "Not running anything because #{playbook_path.inspect} does not exist" } return end playbook.run ensure logger.debug { "Removing generated inventory from #{generated_inventory}" } FileUtils.rm_r generated_inventory end |
#display_name ⇒ Object
34 35 36 |
# File 'lib/barking_iguana/compound/test_stage.rb', line 34 def display_name test.name + ' stage ' + directory end |
#generate_inventory ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/barking_iguana/compound/test_stage.rb', line 79 def generate_inventory benchmark "#{name}: generating inventory for test stage" do tmp_dir('inventory').tap do |d| logger.debug { "#{name}: inventory directory is #{d.inspect}" } connection_file = File. 'connection', d Ansible::InventoryWriter.new(connection_file).tap do |i| benchmark "#{name}: generating connection inventory at #{connection_file}" do hosts.each do |host| h = test.host_manager.find_by_name host.inventory_name i.add_host h end logger.debug { "#{name}: writing connection inventory:\n#{i.to_s}" } i.write_file end end original_file = File. 'original', d logger.debug { "#{name}: copying original inventory to #{original_file} from #{inventory_path}" } FileUtils.copy inventory_path, original_file end end end |
#generated_inventory ⇒ Object
67 68 69 |
# File 'lib/barking_iguana/compound/test_stage.rb', line 67 def generated_inventory @generated_inventory ||= generate_inventory end |
#inventory_path ⇒ Object
38 39 40 |
# File 'lib/barking_iguana/compound/test_stage.rb', line 38 def inventory_path @inventory_path ||= stage_file_with_fallback('inventory') end |
#name ⇒ Object
30 31 32 |
# File 'lib/barking_iguana/compound/test_stage.rb', line 30 def name directory end |
#original_inventory ⇒ Object
63 64 65 |
# File 'lib/barking_iguana/compound/test_stage.rb', line 63 def original_inventory @original_inventory ||= Ansible.inventory(inventory_path) end |
#playbook ⇒ Object
106 107 108 |
# File 'lib/barking_iguana/compound/test_stage.rb', line 106 def playbook Ansible.playbook(playbook_path, run_from: control_directory).inventory(generated_inventory).stream_to(playbook_logger).verbosity(ansible_verbosity).diff end |
#playbook_logger ⇒ Object
110 111 112 |
# File 'lib/barking_iguana/compound/test_stage.rb', line 110 def playbook_logger @playbook_logger ||= BarkingIguana::ForkCalls.fork_to(logger, results_logger) end |
#playbook_path ⇒ Object
59 60 61 |
# File 'lib/barking_iguana/compound/test_stage.rb', line 59 def playbook_path @playbook_path ||= stage_file_with_fallback('playbook.yml') end |
#results_file ⇒ Object
153 154 155 |
# File 'lib/barking_iguana/compound/test_stage.rb', line 153 def results_file @results_file ||= File.('playbook.out', tmp_dir('results', 'ansible')) end |
#run ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/barking_iguana/compound/test_stage.rb', line 20 def run benchmark display_name do actions.each do |action| benchmark "#{display_name} action #{action}" do public_send action end end end end |
#setup ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/barking_iguana/compound/test_stage.rb', line 114 def setup desired_hosts = hosts.sort.map(&:name) logger.debug { "Desired hosts for #{display_name}: #{desired_hosts.join(', ')}" } active_hosts = host_manager.active.sort.map(&:name) logger.debug { "Active hosts for #{display_name}: #{active_hosts.join(', ')}" } hosts_to_launch = desired_hosts - active_hosts logger.debug { "Launch hosts for #{display_name}: #{hosts_to_launch.join(', ')}" } host_manager.launch *hosts_to_launch hosts_to_stop = active_hosts - desired_hosts logger.debug { "Stop hosts for #{display_name}: #{hosts_to_stop.join(', ')}" } host_manager.shutdown *hosts_to_stop end |
#stage_directory ⇒ Object
42 43 44 |
# File 'lib/barking_iguana/compound/test_stage.rb', line 42 def stage_directory File. directory, test.directory end |
#stage_file_with_fallback(file_name) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/barking_iguana/compound/test_stage.rb', line 46 def stage_file_with_fallback file_name logger.debug { "Searching for #{file_name.inspect}" } stage_file = File. file_name, stage_directory logger.debug { "Checking #{stage_file.inspect}" } if File.exists? stage_file logger.debug { "Found #{file_name.inspect} at #{stage_file.inspect}" } return stage_file end test_file = File. file_name, test.directory logger.debug { "Assuming it'll be at #{test_file.inspect}" } test_file end |
#tmp_dir(*sub_path) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/barking_iguana/compound/test_stage.rb', line 71 def tmp_dir *sub_path @tmp_dir ||= Dir.mktmpdir return @tmp_dir if sub_path.empty? full_path = File. File.join(sub_path), @tmp_dir FileUtils.mkdir_p full_path full_path end |
#verify ⇒ Object
148 149 150 151 |
# File 'lib/barking_iguana/compound/test_stage.rb', line 148 def verify server_spec.run ansible_spec.run end |