Sha256: 69affc5118a41b2ee62d4acff0c1a7e5b0783e4fb56bf895790d531755a45315

Contents?: true

Size: 1.87 KB

Versions: 33

Compression:

Stored size: 1.87 KB

Contents

require "thor"
require "yaml"
require 'cloudstack_client'

module CloudstackNagios 
  class Base < Thor
    include Thor::Actions
    include CloudstackNagios::Helper
    
    attr_reader :config

    # catch control-c and exit
    trap("SIGINT") {
      puts " bye"
      exit!
    }

    # exit with return code 1 in case of a error
    def self.exit_on_failure?
      true
    end

    no_commands do  
      def client(opts = {})
        @config ||= load_configuration
        @client ||= CloudstackClient::Connection.new(
          @config[:url],
          @config[:api_key],
          @config[:secret_key],
          opts.merge({debug: options[:debug]})
        )
      end

      def load_configuration(config_file = options[:config], env = options[:environment])
        unless File.exists?(config_file)
          say "Configuration file #{config_file} not found.", :red
          say "Please run \'cs-nagios setup\' to create one."
          exit 1
        end
        begin
          config = YAML::load(IO.read(config_file))
        rescue
          error "Can't load configuration from file #{config_file}."
          exit 1
        end
        if env
          config = config[env]
          unless config
            error "Can't find environment #{env} in configuration file."
            exit 1
          end
        end
        config
      end

      def find_project(project_name = options[:project])
        return nil unless project_name
        unless project = client.get_project(project_name)
          say "Project '#{options[:project]}' not found", :red
          exit 1
        end
        project
      end

      def filter_by(objects, tag_name, tag)
        objects.select {|r| r[tag_name].downcase == tag.downcase}
      end

      def sshoptions(ssh_key)
       {
         timeout: 5,
         keys: [ssh_key],
         auth_methods: %w(publickey)
       }
      end

    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
cloudstack-nagios-0.14.0 lib/cloudstack-nagios/base.rb
cloudstack-nagios-0.13.0 lib/cloudstack-nagios/base.rb
cloudstack-nagios-0.12.3 lib/cloudstack-nagios/base.rb
cloudstack-nagios-0.12.2 lib/cloudstack-nagios/base.rb
cloudstack-nagios-0.12.1 lib/cloudstack-nagios/base.rb
cloudstack-nagios-0.12.0 lib/cloudstack-nagios/base.rb
cloudstack-nagios-0.11.4 lib/cloudstack-nagios/base.rb
cloudstack-nagios-0.11.3 lib/cloudstack-nagios/base.rb
cloudstack-nagios-0.11.2 lib/cloudstack-nagios/base.rb
cloudstack-nagios-0.11.1 lib/cloudstack-nagios/base.rb
cloudstack-nagios-0.11.0 lib/cloudstack-nagios/base.rb
cloudstack-nagios-0.10.3 lib/cloudstack-nagios/base.rb
cloudstack-nagios-0.10.2 lib/cloudstack-nagios/base.rb
cloudstack-nagios-0.10.1 lib/cloudstack-nagios/base.rb
cloudstack-nagios-0.10.0 lib/cloudstack-nagios/base.rb
cloudstack-nagios-0.9.1 lib/cloudstack-nagios/base.rb
cloudstack-nagios-0.9.0 lib/cloudstack-nagios/base.rb
cloudstack-nagios-0.8.2 lib/cloudstack-nagios/base.rb
cloudstack-nagios-0.8.1 lib/cloudstack-nagios/base.rb
cloudstack-nagios-0.8.0 lib/cloudstack-nagios/base.rb