Class: BarkingIguana::Compound::TestStage

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Benchmark, Logging::Helper
Defined in:
lib/barking_iguana/compound/test_stage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#directoryObject

Returns the value of attribute directory.



6
7
8
# File 'lib/barking_iguana/compound/test_stage.rb', line 6

def directory
  @directory
end

#testObject

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

#actionsObject



13
14
15
# File 'lib/barking_iguana/compound/test_stage.rb', line 13

def actions
  i(setup converge verify).freeze
end

#ansible_verbosityObject



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_upObject



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

#convergeObject



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_nameObject



34
35
36
# File 'lib/barking_iguana/compound/test_stage.rb', line 34

def display_name
  test.name + ' stage ' + directory
end

#generate_inventoryObject



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.expand_path '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.expand_path 'original', d
      logger.debug { "#{name}: copying original inventory to #{original_file} from #{inventory_path}" }
      FileUtils.copy inventory_path, original_file
    end
  end
end

#generated_inventoryObject



67
68
69
# File 'lib/barking_iguana/compound/test_stage.rb', line 67

def generated_inventory
  @generated_inventory ||= generate_inventory
end

#inventory_pathObject



38
39
40
# File 'lib/barking_iguana/compound/test_stage.rb', line 38

def inventory_path
  @inventory_path ||= stage_file_with_fallback('inventory')
end

#nameObject



30
31
32
# File 'lib/barking_iguana/compound/test_stage.rb', line 30

def name
  directory
end

#original_inventoryObject



63
64
65
# File 'lib/barking_iguana/compound/test_stage.rb', line 63

def original_inventory
  @original_inventory ||= Ansible.inventory(inventory_path)
end

#playbookObject



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_loggerObject



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_pathObject



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_fileObject



153
154
155
# File 'lib/barking_iguana/compound/test_stage.rb', line 153

def results_file
  @results_file ||= File.expand_path('playbook.out', tmp_dir('results', 'ansible'))
end

#runObject



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

#setupObject



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_directoryObject



42
43
44
# File 'lib/barking_iguana/compound/test_stage.rb', line 42

def stage_directory
  File.expand_path 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.expand_path 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.expand_path 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.expand_path File.join(sub_path), @tmp_dir
  FileUtils.mkdir_p full_path
  full_path
end

#verifyObject



148
149
150
151
# File 'lib/barking_iguana/compound/test_stage.rb', line 148

def verify
  server_spec.run
  ansible_spec.run
end