Class: ConfCtl::Generation::HostList

Inherits:
Object
  • Object
show all
Defined in:
lib/confctl/generation/host_list.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ HostList

Returns a new instance of HostList.

Parameters:

  • host (String)


49
50
51
52
# File 'lib/confctl/generation/host_list.rb', line 49

def initialize(host)
  @host = host
  @generations = []
end

Instance Attribute Details

#hostString (readonly)

Returns:

  • (String)


46
47
48
# File 'lib/confctl/generation/host_list.rb', line 46

def host
  @host
end

Class Method Details

.fetch(mc, profile:) ⇒ Generation::HostList

Parameters:

Returns:



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/confctl/generation/host_list.rb', line 6

def self.fetch(mc, profile:)
  out, = mc.bash_script(<<~END)
    realpath #{profile}

    for generation in `ls -d -1 #{profile}-*-link` ; do
      echo "$generation;$(readlink $generation);$(stat --format=%Y $generation)"
    done
  END

  list = new(mc.machine.name)
  lines = out.strip.split("\n")
  current_path = lines.shift
  id_rx = /^#{Regexp.escape(profile)}-(\d+)-link$/

  lines.each do |line|
    link, path, created_at = line.split(';')

    if id_rx =~ link
      id = ::Regexp.last_match(1).to_i
    else
      warn "Invalid profile generation link '#{link}'"
      next
    end

    list << Generation::Host.new(
      mc.machine.name,
      profile,
      id,
      path,
      Time.at(created_at.to_i),
      current: path == current_path,
      mc:
    )
  end

  list.sort
  list
end

Instance Method Details

#<<(generation) ⇒ Object

Parameters:



55
56
57
# File 'lib/confctl/generation/host_list.rb', line 55

def <<(generation)
  generations << generation
end

#countInteger

Returns:

  • (Integer)


68
69
70
# File 'lib/confctl/generation/host_list.rb', line 68

def count
  generations.length
end

#currentGeneration::Host

Returns:



73
74
75
# File 'lib/confctl/generation/host_list.rb', line 73

def current
  generations.detect(&:current)
end

#eachObject



63
64
65
# File 'lib/confctl/generation/host_list.rb', line 63

def each(&)
  generations.each(&)
end

#sortObject



59
60
61
# File 'lib/confctl/generation/host_list.rb', line 59

def sort
  generations.sort! { |a, b| a.id <=> b.id }
end