Class: ChefClass

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

Overview

Wrapper class to interact with chef

Instance Method Summary collapse

Constructor Details

#initialize(name, vm: nil, task: nil) ⇒ ChefClass

rubocop:disable Naming/AccessorMethodName



16
17
18
19
20
# File 'lib/miq_utilities/chef.rb', line 16

def initialize(name, vm: nil, task: nil)
  @logger = LoggingClass.new(name)
  @vm = vm unless vm.nil?
  @task = task unless task.nil?
end

Instance Method Details

#call_chef(cmd, timeout = 20, pre_cmd = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/miq_utilities/chef.rb', line 22

def call_chef(cmd, timeout = 20, pre_cmd = nil)
  # unset the variables for knife
  pre_cmd ||= 'unset GEM_HOME GEM_PATH IRBRC MY_RUBY_HOME'

  require 'linux_admin'
  require 'timeout'

  begin
    Timeout.timeout(timeout) do
      @logger.log(level: 'info', message: "Executing [#{cmd}] with timeout of #{timeout} seconds")
      result = LinuxAdmin::Common.run("#{pre_cmd};#{cmd}")
      @logger.log(level: 'info', message: "success?: #{result.success?}")
      @logger.log(level: 'info', message: "exit_status: #{result.exit_status}") unless result.exit_status.blank?
      @logger.log(level: 'info', message: "output: #{result.output}")
      @logger.log(level: 'info', message: "error: #{result.error}") unless result.error.blank?
      return result
    end
  rescue => timeout
    @logger.log(level: 'error', message: "Error executing chef: #{timeout.class} #{timeout} #{timeout.backtrace.join("\n")}")
    return false
  rescue => err
    raise("\n    Class:<#{self.class}>\n    Method:<#{__method__}>\n    Error:<#{err} - #{err.backtrace.join("\n")}>")
  end
end

#get_chef_environment_name(ws_values = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/miq_utilities/chef.rb', line 47

def get_chef_environment_name(ws_values = {})
  chef_environment = $evm.object['chef_environment']
  chef_environment ||= $evm.root['dialog_chef_environment']
  if @task
    env = @task.get_tags[:environment]
    region = @task.get_option(:cmdb)[:ProvisionCMDBDetails]['CountryCode'].downcase
    chef_environment = @task.get_tags[:chef_environment] || ws_values[:chef_environment] || @task.get_option(:chef_environment)
    chef_environment ||= case env
                         when 'production'
                           "#{region}_prod"
                         when 'quality_excellence'
                           "#{region}_qe"
                         when 'development'
                           "#{region}_dev"
                         else
                           '_default'
                         end
  end
  chef_environment ||= '_default'
  @logger.log(level: 'info', message: "chef_environment: #{chef_environment}")
  chef_environment
end

#get_chef_node_nameObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/miq_utilities/chef.rb', line 78

def get_chef_node_name
  chef_node_name = begin
    @vm.hostnames.first
  rescue
    nil
  end
  domain_suffix = get_domain_suffix
  chef_node_name ||= @task.get_option(:vm_target_hostname) if @task
  chef_node_name ||= @vm.name.to_s unless @vm.nil?
  chef_node_name = "#{chef_node_name}.#{domain_suffix}" unless domain_suffix.nil? || chef_node_name.include?(domain_suffix)

  @logger.log(level: 'info', message: "chef_node_name: #{chef_node_name}")
  chef_node_name.downcase
end

#get_chef_role(ws_values = {}) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/miq_utilities/chef.rb', line 93

def get_chef_role(ws_values = {})
  chef_role = $evm.object['chef_role']
  chef_role ||= $evm.root['dialog_chef_role']
  if @task
    chef_role = @task.get_tags[:chef_role] || ws_values[:chef_role] || @task.get_option(:chef_role)
  end

  chef_role ||= ''
  @logger.log(level: 'info', message: "chef_role: #{chef_role}")
  chef_role
end

#get_chef_typeObject



70
71
72
73
74
75
76
# File 'lib/miq_utilities/chef.rb', line 70

def get_chef_type
  chef_type = $evm.object['chef_type']
  chef_type ||= $evm.root['dialog_chef_type']
  chef_type ||= 'role'
  @logger.log(level: 'info', message: "chef_type: #{chef_type}")
  chef_type
end

#get_chef_version(chef_version = nil) ⇒ Object



114
115
116
117
118
# File 'lib/miq_utilities/chef.rb', line 114

def get_chef_version(chef_version = nil)
  chef_version ||= $evm.object['chef_version']
  @logger.log(level: 'info', message: "chef_version: #{chef_version}")
  chef_version
end

#get_domain_suffixObject



105
106
107
108
109
110
111
112
# File 'lib/miq_utilities/chef.rb', line 105

def get_domain_suffix
  domain_suffix   = nil
  domain_suffix ||= $evm.object['domain_suffix']
  domain_suffix ||= $evm.root['dialog_domain_suffix']
  domain_suffix ||= @task.get_tags[:domain_suffix] if @task
  @logger.log(level: 'info', message: "domain_suffix: #{domain_suffix}")
  domain_suffix
end