Class: AutoConsul::Local::FileSystemState

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

Constant Summary collapse

VALID_MODES =
{
  'agent' => 'agent',
  'server' => 'server',
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ FileSystemState

Returns a new instance of FileSystemState.



6
7
8
9
10
11
12
# File 'lib/auto-consul/local.rb', line 6

def initialize path
  unless File.directory? path
    FileUtils.mkdir_p path
  end

  @path = path
end

Class Method Details

.determine_mode(mode_file) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/auto-consul/local.rb', line 41

def self.determine_mode mode_file
  if File.file? mode_file
    value = File.open(mode_file, 'r') {|f| f.read }
    VALID_MODES[value]
  else
    nil
  end
end

Instance Method Details

#agent?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/auto-consul/local.rb', line 61

def agent?
  mode == 'agent'
end

#data_pathObject



65
66
67
68
69
70
71
# File 'lib/auto-consul/local.rb', line 65

def data_path
  if not (m = mode).nil?
    File.join(path, mode)
  else
    nil
  end
end

#modeObject



50
51
52
53
54
55
# File 'lib/auto-consul/local.rb', line 50

def mode
  if @mode.nil?
    @mode = self.class.determine_mode mode_path
  end
  @mode
end

#mode_pathObject



18
19
20
# File 'lib/auto-consul/local.rb', line 18

def mode_path
  File.join(path, 'mode')
end

#pathObject



14
15
16
# File 'lib/auto-consul/local.rb', line 14

def path
  @path
end

#server?Boolean

Returns:

  • (Boolean)


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

def server?
  mode == 'server'
end

#set_agent!Object



26
27
28
# File 'lib/auto-consul/local.rb', line 26

def set_agent!
  set_mode 'agent'
end

#set_mode(mode) ⇒ Object



30
31
32
33
34
# File 'lib/auto-consul/local.rb', line 30

def set_mode mode
  File.open(mode_path, 'w') do |f|
    f.write mode
  end
end

#set_server!Object



22
23
24
# File 'lib/auto-consul/local.rb', line 22

def set_server!
  set_mode 'server'
end