-
Notifications
You must be signed in to change notification settings - Fork 109
/
command_creator.rb
43 lines (37 loc) · 1.17 KB
/
command_creator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module ForemanAnsibleCore
# Creates the actual command to be passed to foreman_tasks_core to run
class CommandCreator
attr_reader :command
def initialize(inventory_file, playbook_file, options = {})
@options = options
@command = [{ 'JSON_INVENTORY_FILE' => inventory_file }]
@command << 'ansible-playbook'
@command = command_options(@command)
@command << playbook_file
end
private
def command_options(command)
command.concat(['-i', json_inventory_script])
command.concat([setup_verbosity]) if verbose?
command.concat(['-T', @options[:timeout]]) unless @options[:timeout].nil?
command
end
def json_inventory_script
File.expand_path('../../bin/json_inventory.sh', File.dirname(__FILE__))
end
def setup_verbosity
verbosity_level = @options[:verbosity_level].to_i
verbosity = '-'
verbosity_level.times do
verbosity += 'v'
end
verbosity
end
def verbose?
verbosity_level = @options[:verbosity_level]
# rubocop:disable Rails/Present
!verbosity_level.nil? && !verbosity_level.empty? &&
verbosity_level.to_i > 0
end
end
end