Class: Construqt::Flavour::Ciscian::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/construqt/flavour/ciscian/ciscian.rb

Defined Under Namespace

Classes: Lines

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ Result

Returns a new instance of Result.



23
24
25
26
27
28
29
30
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 23

def initialize(host)
  @host = host
  @sections = {}
  throw "ciscian flavour can only be created with dialect" unless host.dialect
  require_relative "dialect_#{host.dialect}.rb"
  throw "cannot load dialect class #{host.dialect}" unless Ciscian.dialects[host.dialect]
  self.dialect=Ciscian.dialects[host.dialect].new(self)
end

Instance Attribute Details

#dialectObject

Returns the value of attribute dialect.



22
23
24
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 22

def dialect
  @dialect
end

#hostObject

Returns the value of attribute host.



22
23
24
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 22

def host
  @host
end

#sectionsObject

Returns the value of attribute sections.



22
23
24
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 22

def sections
  @sections
end

Class Method Details

.compare(nu, old) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 115

def self.compare(nu, old)
  result = Result.new(nu.host)

  nu_root=NestedSection.new("root")
  nu_root.sections.merge!(nu.sections)
  other_root=NestedSection.new("root")
  other_root.sections.merge!(old.sections)

  deltas=NestedSection.compare(nu_root, other_root)
  throw "illegal state" if deltas.length != 1
  result.sections = deltas[0].sections unless deltas[0].nil?
  result
end

.normalize_section_key(key) ⇒ Object



65
66
67
68
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 65

def self.normalize_section_key(key)
  matchdata=key.match(/^\s*(no\s+|)(.*)/)
  matchdata[2]
end

.starts_with_no(key) ⇒ Object



70
71
72
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 70

def self.starts_with_no(key)
  return key.match(/^\s*no\s+/)
end

Instance Method Details

#add(section, clazz = SingleValueVerb) {|| ... } ⇒ Object

Yields:

  • ()


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 99

def add(section, clazz=SingleValueVerb)
  throw "section is nil" unless section
  section = Lines::Line.new(section, -1) unless section.kind_of?(Lines::Line)
  section_key=Result.normalize_section_key(section.to_s)

  @sections[section_key] ||= clazz.new(section_key)
  if Result.starts_with_no(section.to_s)
    @sections[section_key].no
  else
    @sections[section_key].yes
  end

  yield(@sections[section_key]) if block_given?
  @sections[section_key]
end

#commitObject



91
92
93
94
95
96
97
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 91

def commit
  self.dialect.commit
  Util.write_str(self.serialize().join("\n"), File.join(@host.name, "#{@host.fname||self.dialect.class.name}.cfg"))
  external=@host.id.interfaces.first.address
  external_ip=external.first_ipv4.nil? ? external.first_ipv6.to_s : external.first_ipv4.to_s
  DeployTemplate.write_template(@host, self.dialect.class.name, external_ip, "root", @host.password||@host.region.hosts.default_password)
end

#parse(lines) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 56

def parse(lines)
  lines = Lines.new(lines)
  while line = lines.shift
    parse_line(line, lines, self, self)
  end

  self
end

#parse_line(line, lines, section, result) ⇒ Object



74
75
76
77
78
79
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 74

def parse_line(line, lines, section, result)
  return if self.dialect.parse_line(line, lines, section, result)
  [NestedSection, SingleValueVerb].find do |clazz|
    clazz.parse_line(line, lines, section, result)
  end
end

#serializeObject



81
82
83
84
85
86
87
88
89
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 81

def serialize
  block=[]
  section_keys = self.dialect.sort_section_keys(@sections.keys)
  section_keys.each do |key|
    section = @sections[key]
    block += section.serialize
  end
  block
end