Module: ManageIQ::ApplianceConsole::Utilities

Defined in:
lib/manageiq/appliance_console/utilities.rb

Class Method Summary collapse

Class Method Details

.bail_if_db_connections(message) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/manageiq/appliance_console/utilities.rb', line 47

def self.bail_if_db_connections(message)
  say("Checking for connections to the database...\n\n")
  if (conns = ManageIQ::ApplianceConsole::Utilities.db_connections - 1) > 0
    say("Warning: There are #{conns} existing connections to the database #{message}.\n\n")
    press_any_key
    raise MiqSignalError
  end
end

.db_connectionsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/manageiq/appliance_console/utilities.rb', line 29

def self.db_connections
  # TODO: this is impossible to test right now because we need to shell out and run our rails app which isn't a direct dependency here.
  # We have the settings from the database_configuration, so we should pass them here and simplify this and make it testable.
  # Basically, we're doing a lot of work to run this 1 query:
  #   psql -U username -h host postgres -c "select count(*) from pg_stat_activity where datname = 'vmdb_production';"
  # We shouldn't need to subtract our 1 "connection" in "bail_if_db_connections" if we connect to the postgres db.
  code   = [
    "database ||= ActiveRecord::Base.configurations.configs_for(:env_name => Rails.env).first.database",
    "conn = ActiveRecord::Base.connection",
    "exit conn.client_connections.count { |c| c['database'] == database }"
  ]
  result = AwesomeSpawn.run("bin/rails runner",
                            :params => [code.join("; ")],
                            :chdir  => ManageIQ::ApplianceConsole::RAILS_ROOT
                           )
  Integer(result.exit_status)
end

.db_regionObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/manageiq/appliance_console/utilities.rb', line 56

def self.db_region
  result = AwesomeSpawn.run(
    "bin/rails runner",
    :params => ["puts ApplicationRecord.my_region_number"],
    :chdir  => ManageIQ::ApplianceConsole::RAILS_ROOT
  )

  if result.failure?
    logger = ManageIQ::ApplianceConsole.logger
    logger.error "db_region: Failed to detect region_number"
    logger.error "Output: #{result.output.inspect}" unless result.output.blank?
    logger.error "Error:  #{result.error.inspect}"  unless result.error.blank?
    return
  end

  result.output.strip
end

.disk_usage(file = nil) ⇒ Object



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
# File 'lib/manageiq/appliance_console/utilities.rb', line 86

def self.disk_usage(file = nil)
  file_arg = file
  file_arg = "-l" if file.nil? || file == ""

  unless file_arg == "-l" || File.exist?(file)
    raise "file #{file} does not exist"
  end

  # Collect bytes
  result = AwesomeSpawn.run!("df", :params => ["-T", "-P", file_arg]).output.lines.each_with_object([]) do |line, array|
    lArray = line.strip.split(" ")
    next if lArray.length != 7
    fsname, type, total, used, free, used_percentage, mount_point = lArray
    next unless total =~ /[0-9]+/
    next if array.detect { |hh| hh[:filesystem] == fsname }

    array << {
      :filesystem         => fsname,
      :type               => type,
      :total_bytes        => total.to_i * 1024,
      :used_bytes         => used.to_i * 1024,
      :available_bytes    => free.to_i * 1024,
      :used_bytes_percent => used_percentage.chomp("%").to_i,
      :mount_point        => mount_point,
    }
  end

  # Collect inodes
  AwesomeSpawn.run!("df", :params => ["-T", "-P", "-i", file_arg]).output.lines.each do |line|
    lArray = line.strip.split(" ")
    next if lArray.length != 7
    fsname, _type, total, used, free, used_percentage, _mount_point = lArray
    next unless total =~ /[0-9]+/
    h = result.detect { |hh| hh[:filesystem] == fsname }
    next if h.nil?

    h[:total_inodes]        = total.to_i
    h[:used_inodes]         = used.to_i
    h[:available_inodes]    = free.to_i
    h[:used_inodes_percent] = used_percentage.chomp("%").to_i
  end
  result
end

.pg_statusObject



74
75
76
# File 'lib/manageiq/appliance_console/utilities.rb', line 74

def self.pg_status
  LinuxAdmin::Service.new(PostgresAdmin.service_name).running? ? "running" : "not running"
end

.rake(task, params, env = {}) ⇒ Object



9
10
11
# File 'lib/manageiq/appliance_console/utilities.rb', line 9

def self.rake(task, params, env = {})
  rake_run(task, params, env).success?
end

.rake_run(task, params, env = {}) ⇒ Object



13
14
15
16
17
# File 'lib/manageiq/appliance_console/utilities.rb', line 13

def self.rake_run(task, params, env = {})
  result = AwesomeSpawn.run("rake #{task}", :chdir => ManageIQ::ApplianceConsole::RAILS_ROOT, :params => params, :env => env)
  ManageIQ::ApplianceConsole.logger.error(result.error) if result.failure?
  result
end

.rake_run!(task, params, env = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/manageiq/appliance_console/utilities.rb', line 19

def self.rake_run!(task, params, env = {})
  result = rake_run(task, params, env)
  if result.failure?
    parsed_errors = result.error.split("\n").select { |line| line.match?(/^error: /i) }.join(', ')
    raise parsed_errors
  end

  result
end

.test_networkObject



78
79
80
81
82
83
84
# File 'lib/manageiq/appliance_console/utilities.rb', line 78

def self.test_network
  require 'net/ping'
  say("Test Network Configuration\n\n")
  while (h = ask_for_ip_or_hostname_or_none("hostname, ip address, or none to continue").presence)
    say("  " + h + ': ' + (Net::Ping::External.new(h).ping ? 'Success!' : 'Failure, Check network settings and IP address or hostname provided.'))
  end
end