Class: CloudstackNagios::Cli

Inherits:
Base
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/cloudstack-nagios/cli.rb

Constant Summary

Constants included from Helper

Helper::RETURN_CODES

Instance Attribute Summary

Attributes inherited from Base

#config

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

exit_on_failure?

Methods included from Helper

#check_data, #cs_routers, #cs_system_vms, #exit_with_failure, #load_template, #storage_pools

Class Method Details

.start(given_args = ARGV, config = {}) ⇒ Object

rescue error globally



10
11
12
13
14
15
16
# File 'lib/cloudstack-nagios/cli.rb', line 10

def self.start(given_args=ARGV, config={})
  super
rescue => e
  error_class = e.class.name.split('::')
  puts "\e[31mERROR\e[0m: #{error_class.last} - #{e.message}"
  puts e.backtrace if ARGV.include? "--debug"
end

Instance Method Details

#environments(file = ) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cloudstack-nagios/cli.rb', line 74

def environments(file = options[:config])
  config = {}
  if File.exists? file
    begin
      config = YAML::load(IO.read(file))
    rescue
      error "Can't load configuration from file #{config_file}."
      exit 1
    end
    table = [%w(Name URL)]
    table << ["default", config[:url]]
    config.each_key do |key|
      table << [key, config[key][:url]] unless key.class == Symbol
    end
    print_table table
  end
end

#setup(file = options[:config]) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cloudstack-nagios/cli.rb', line 42

def setup(file = options[:config])
  config = {}
  unless options[:url]
    say "Configuring #{options[:environment] || 'default'} environment."
    say "What's the URL of your Cloudstack API?", :yellow
    say "Example: https://my-cloudstack-service/client/api/"
    config[:url] = ask("URL:", :magenta)
  end
  unless options[:api_key]
    config[:api_key] = ask("API Key:", :magenta)
  end
  unless options[:secret_key]
    config[:secret_key] = ask("Secret Key:", :magenta)
  end
  if options[:environment]
    config = {options[:environment] => config}
  end
  if File.exists? file
    begin
      old_config = YAML::load(IO.read(file))
    rescue
      error "Can't load configuration from file #{config_file}."
      exit 1
    end
    say "Warning: #{file} already exists.", :red
    exit unless yes?("Do you want to merge your settings? [y/N]", :red)
    config = old_config.merge(config)
  end
  File.open(file, 'w+') {|f| f.write(config.to_yaml) }
end

#versionObject



34
35
36
# File 'lib/cloudstack-nagios/cli.rb', line 34

def version
  say "cloudstack-nagios v#{CloudstackNagios::VERSION}"
end