Class: Mackerel::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/mackerel/runner.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Runner

Returns a new instance of Runner.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mackerel/runner.rb', line 9

def initialize(args)

  args.unshift 'help' if args.empty?

  @args = args
  cmd = args.shift

  if Runner.method_defined?(cmd) and cmd != 'run'
    send(cmd, args)
  else
    puts "Error: `#{cmd}` command not found.\n\n"
    send('help', args)
    abort
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



7
8
9
# File 'lib/mackerel/runner.rb', line 7

def args
  @args
end

Class Method Details

.execute(args) ⇒ Object

Shortcut



26
27
28
# File 'lib/mackerel/runner.rb', line 26

def self.execute(args)
  new(args)
end

Instance Method Details

#help(args) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mackerel/runner.rb', line 30

def help(args)
  puts <<-EOS
#{$0}: Command line interface for Mackerel (https://mackerel.io/).
  Get host(s) information from hostname or service, role.
mkr host info [--name foo] [--service service] [--role role]

  Set status of a host
mkr host status --host-id foo --status working

  Retire a host
mkr host retire --host-id foo

  Authentication
API key must be set to the environment variable as follows.
  export MACKEREL_APIKEY=foobar

  EOS
end

#host(args) ⇒ Object

mkr host status –host-id foo



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/mackerel/runner.rb', line 50

def host(args)
  mc = Mackerel::Client.new(:mackerel_api_key => ENV['MACKEREL_APIKEY'])
  params = {}
  opt = OptionParser.new
  opt.on('--host-id HOSTID'){|v| params[:hostid] = v }

  cmd = args.shift
  case cmd
  when 'status'
      opt.on('--status STATUS'){|v| params[:status] = v }
      opt.parse!(args)
      if params[:status]
        begin
          res = mc.update_host_status(params[:hostid], params[:status])
          puts "Updated to '#{params[:status]}'"
        rescue => msg
          abort "Error: #{msg}"
        end
      else
        begin
          res = mc.get_hosts(:hostid => params[:hostid])[0]
          puts "#{res.status}"
        rescue => msg
          abort "Error: #{msg}"
        end

      end

  when 'retire'
    opt.parse!(args)
    begin
      res = mc.retire_host(params[:hostid])
    rescue => msg
      abort "Error: #{msg}"
    end

  when 'info'
    opt.on('--service SERVICE'){|v| params[:service] = v }
    opt.on('--role ROLE')      {|v| params[:roles] = v }
    opt.on('--name NAME')      {|v| params[:name] = v }
    opt.on('--host-id HOSTID') {|v| params[:hostId] = v }
    opt.parse!(args)
    begin
      if params[:hostid]
        res = [ mc.get_host(params[:hostId]) ]
      else
        res = mc.get_hosts(params)
      end
    rescue => msg
      abort "Error: #{msg}"
    end
    res.each do |res|
      puts <<-EOS
name: #{res.name}, status: #{res.status}, id: #{res.id}, roles: #{res.roles}
      EOS
    end
  else
    abort "Error: `#{command}` command not found for host."
  end
end