Class: VagrantPlugins::GlobalStatus::GlobalRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-global-status/global_registry.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(statefile) ⇒ GlobalRegistry

Returns a new instance of GlobalRegistry.



23
24
25
26
27
28
29
30
31
# File 'lib/vagrant-global-status/global_registry.rb', line 23

def initialize(statefile)
  @statefile = statefile
  if @statefile.file?
    @current_state = JSON.parse(@statefile.read(:encoding => Encoding::UTF_8))
    fix_current_status
  else
    @current_state = { 'environments' => {} }
  end
end

Class Method Details

.deregister(params) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/vagrant-global-status/global_registry.rb', line 15

def self.deregister(params)
  statefile = params.fetch(:home_path).join('machine-environments.json')
  new(statefile).deregister(
    params.fetch(:machine_name).to_s,
    params.fetch(:root_path).to_s
  )
end

.register(params) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/vagrant-global-status/global_registry.rb', line 6

def self.register(params)
  statefile = params.fetch(:home_path).join('machine-environments.json')
  new(statefile).register(
    params.fetch(:machine_name).to_s,
    params.fetch(:root_path).to_s,
    Time.now.to_i.to_s
  )
end

Instance Method Details

#deregister(machine_name, root_path) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/vagrant-global-status/global_registry.rb', line 53

def deregister(machine_name, root_path)
  @current_state['environments'][root_path] ||= { 'machines' => [] }

  global_environment = @current_state['environments'][root_path]
  global_environment['machines'].delete_if {|machine| machine["name"] = machine_name }

  write_statefile
end

#environmentsObject



33
34
35
36
37
38
39
# File 'lib/vagrant-global-status/global_registry.rb', line 33

def environments
  @environments ||= @current_state['environments'].each_with_object({}) do |(env, data), hash|
    hash[env] = GlobalEnvironment.new(env, data)
  end

  @environments.values
end

#fix_current_statusObject



62
63
64
65
66
67
68
69
# File 'lib/vagrant-global-status/global_registry.rb', line 62

def fix_current_status 
  @current_state['environments'].each_with_object({}) do |(env, data), hash|
    if not File.exist? env or not File.exist? env + "/Vagrantfile" 
      @current_state['environments'].delete(env)
    end
  end
  write_statefile
end

#register(machine_name, root_path, created_at) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/vagrant-global-status/global_registry.rb', line 41

def register(machine_name, root_path, created_at)
  @current_state['environments'][root_path] ||= { 'machines' => [] }

  global_environment = @current_state['environments'][root_path]
  machine = { 'name' => machine_name }
  unless global_environment['machines'].include?(machine)
    global_environment['machines'] << {'name' => machine_name, 'created_at' => created_at}
  end

  write_statefile
end