Class: AutoConsul::RunState::CLIProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/auto-consul/run_state/cli.rb

Constant Summary collapse

AGENT_MASK =
0b1
SERVER_MASK =
0b10

Instance Method Summary collapse

Instance Method Details

#agent?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/auto-consul/run_state/cli.rb', line 49

def agent?
  (run_state & AGENT_MASK) > 0
end

#check_run_stateObject



6
7
8
9
10
11
12
13
14
15
# File 'lib/auto-consul/run_state/cli.rb', line 6

def check_run_state
  result = 0
  r, w = IO.pipe
  if system("consul info", :out => w)
    w.close
    result = flags_from_output r
    r.close
  end
  result
end

#flags_from_output(stream) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/auto-consul/run_state/cli.rb', line 17

def flags_from_output stream
  consul_block = false
  result = 0
  stream.each do |line|
    if line =~ /^consul:\s/
      consul_block = true
      result = AGENT_MASK
      break
    end
  end

  if consul_block
    stream.each do |line|
      # Exit condition from consul block
      break if line !~ /^\s+/

      if line =~ /^\s+server\s+=\s+true/
        result |= SERVER_MASK
        break
      end
    end
  end
  result
end

#run_stateObject



42
43
44
45
46
47
# File 'lib/auto-consul/run_state/cli.rb', line 42

def run_state
  if @run_state.nil?
    @run_state = check_run_state
  end
  @run_state
end

#running?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/auto-consul/run_state/cli.rb', line 57

def running?
  run_state > 0
end

#server?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/auto-consul/run_state/cli.rb', line 53

def server?
  (run_state & SERVER_MASK) > 0
end