Class: OsCtl::Cli::Event

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

Instance Method Summary collapse

Methods inherited from Command

#cli_opt, #format_output, #osctld_call, #osctld_fmt, #osctld_open, #osctld_resp, run

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)



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

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


86
87
88
89
# File 'lib/osctl/cli/event.rb', line 86

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


91
92
93
# File 'lib/osctl/cli/event.rb', line 91

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', strict: false)
  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: } }
  cmd_opts[:opts][:pool] = pool if pool
  states = args[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