Class: OsCtl::Cli::Event

Inherits:
Command show all
Defined in:
lib/osctl/cli/event.rb

Instance Attribute Summary

Attributes inherited from Command

#args, #gopts, #opts

Instance Method Summary collapse

Methods inherited from Command

#cli_opt, #format_output, #initialize, #osctld_call, #osctld_fmt, #osctld_open, #osctld_resp, #require_args!, run

Methods included from Utils::Humanize

#break_interval, #format_long_duration, #format_percent, #format_short_duration, #humanize_data, #humanize_time_ns, #parse_data

Constructor Details

This class inherits a constructor from OsCtl::Cli::Command

Instance Method Details

#monitorObject



7
8
9
10
11
12
13
# File 'lib/osctl/cli/event.rb', line 7

def monitor
  c = osctld_open
  ret = c.cmd_data!(:event_subscribe)
  return if ret != 'subscribed'

  monitor_loop(c)
end

#monitor_ctObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/osctl/cli/event.rb', line 15

def monitor_ct
  c = osctld_open

  cmd_opts = {type: 'state', opts: {}}
  cmd_opts[:opts][:id] = args if args.any?

  ret = c.cmd_data!(:event_subscribe, cmd_opts)
  return if ret != 'subscribed'

  monitor_loop(c)
end

#monitor_loop(c) ⇒ Object (protected)



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/osctl/cli/event.rb', line 60

def monitor_loop(c)
  loop do
    resp = c.response!
    return if resp.data.nil?

    if block_given?
      return if yield(resp.data) == :stop
      next
    end

    if gopts[:json]
      puts resp.data.to_json

    elsif %w(management state).include?(resp.data[:type])
      send(:"print_#{resp.data[:type]}", resp.data[:opts])

    else
      p resp.data
    end

    STDOUT.flush
  end
end


84
85
86
87
# File 'lib/osctl/cli/event.rb', line 84

def print_management(opts)
  puts "management id=#{opts[:id]} state=#{opts[:state]} "+
       "command=#{opts[:cmd]} opts=#{PP.pp(opts[:opts], '')}"
end


89
90
91
# File 'lib/osctl/cli/event.rb', line 89

def print_state(opts)
  puts "state #{opts[:pool]} #{opts[:id]} #{opts[:state]}"
end

#wait_ctObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/osctl/cli/event.rb', line 27

def wait_ct
  require_args!('id', 'state')
  c = osctld_open

  pool = gopts[:pool]

  if args[0].index(':')
    pool, id = args[0].split(':')

  else
    id = args[0]
  end

  cmd_opts = {type: 'state', opts: {id: id}}
  cmd_opts[:opts][:pool] = pool if pool
  states = args[1..-1]

  # First, subscribe for events
  ret = c.cmd_data!(:event_subscribe, cmd_opts)
  return if ret != 'subscribed'

  # Then check the current state using another connection, exit if we're
  # in awaited state
  ct = osctld_call(:ct_show, id: args[0], pool: gopts[:pool])
  return if states.include?(ct[:state])

  # Wait for chosen state
  monitor_loop(c) do |event|
    :stop if states.include?(event[:opts][:state])
  end
end