Class: Construqt::Flavour::Mikrotik::Result
- Inherits:
-
Object
- Object
- Construqt::Flavour::Mikrotik::Result
- Defined in:
- lib/construqt/flavour/mikrotik/flavour_mikrotik_result.rb
Instance Method Summary collapse
- #add(block, digest, *path) ⇒ Object
- #add_remove_pre_condition(condition, *path) ⇒ Object
- #commit ⇒ Object
- #empty?(name) ⇒ Boolean
- #host ⇒ Object
-
#initialize(host) ⇒ Result
constructor
A new instance of Result.
- #prepare(default, cfg, enable = true) ⇒ Object
- #remove_condition(digests, key) ⇒ Object
- #render_mikrotik(default, cfg, *path) ⇒ Object
- #render_mikrotik_set_by_key(default, cfg, *path) ⇒ Object
- #render_mikrotik_set_direct(default, cfg, *path) ⇒ Object
Constructor Details
#initialize(host) ⇒ Result
Returns a new instance of Result.
5 6 7 8 9 |
# File 'lib/construqt/flavour/mikrotik/flavour_mikrotik_result.rb', line 5 def initialize(host) @remove_pre_condition = {} @host = host @result = {} end |
Instance Method Details
#add(block, digest, *path) ⇒ Object
103 104 105 106 107 108 |
# File 'lib/construqt/flavour/mikrotik/flavour_mikrotik_result.rb', line 103 def add(block, digest, *path) key = File.join(*path) @result[key] ||= [] @result[key] << OpenStruct.new(:digest => digest, :block => block, :path => path) @result[key] end |
#add_remove_pre_condition(condition, *path) ⇒ Object
110 111 112 |
# File 'lib/construqt/flavour/mikrotik/flavour_mikrotik_result.rb', line 110 def add_remove_pre_condition(condition, *path) @remove_pre_condition[path.join(' ')] = condition end |
#commit ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/construqt/flavour/mikrotik/flavour_mikrotik_result.rb', line 130 def commit sorted = {} @result.map do |path, blocks| key = blocks.first.path.join(' ') digests = blocks.select{|i| i.digest } sorted[key] = Util.write_str([ "/#{key}", blocks.map{|i|i.block}.join("\n"), remove_condition(digests, key), "" ].join("\n"), File.join(@host.name, "#{path}.rsc")) end all=[ "system identity", "system clock", "system script", "system scheduler", "user", "interface", "interface bonding", "interface bridge", "interface bridge port", "interface vlan", "interface vrrp", "interface gre6", "ipv6 address", "ipv6 route", "ip address", "ip dns", "ip route", "ip ipsec proposal", "ip ipsec peer", "ip ipsec policy", "routing filter", "routing bgp instance", "routing bgp peer", "tool graphing interface", "ip service" ].map do |path| if sorted[path] sorted[path] else Construqt.logger.warn "WARNING [#{path}] not found #{sorted.keys.join('-')}" unless sorted[path] "" end end.join("\n") Util.write_str(all, File.join(@host.name, "all.rsc")) end |
#empty?(name) ⇒ Boolean
15 16 17 |
# File 'lib/construqt/flavour/mikrotik/flavour_mikrotik_result.rb', line 15 def empty?(name) not @result[name] end |
#host ⇒ Object
11 12 13 |
# File 'lib/construqt/flavour/mikrotik/flavour_mikrotik_result.rb', line 11 def host @host end |
#prepare(default, cfg, enable = true) ⇒ Object
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/construqt/flavour/mikrotik/flavour_mikrotik_result.rb', line 19 def prepare(default, cfg, enable = true) if enable default['disabled'] = Schema.boolean.default(false) end result = {} cfg.each do |key, val| unless default[key] Construqt.logger.debug("skip cfg unknown key:#{key} val:#{val}") else result[key] = val end end keys = {} default.each do |key, val| if val.kind_of?(Schema) val.field_name = key throw "type must set of #{key}" unless val.type? throw "required key:#{key} not set" if val.required? and (result[key].nil? or result[key].to_s.empty?) result[key] = val.get_default if !val.get_default.nil? && result[key].nil? keys[key] = result[key] if val.key? else throw "default type has to be a schema #{val}" end end OpenStruct.new( :key => keys.keys.sort{|a,b| default[a].key_order <=> default[b].key_order } .map{|k| v=keys[k]; "#{k}=#{default[k].serialize(v)}"}.join(" && "), :result => result, :add_line => result.select{ |k,v| if default[k].kind_of?(Schema) && default[k].noset? false else !(v.to_s.empty?) end }.map{|k,v| "#{k}=#{default[k].serialize(v)}"}.sort.join(" ") ) end |
#remove_condition(digests, key) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/construqt/flavour/mikrotik/flavour_mikrotik_result.rb', line 114 def remove_condition(digests, key) condition = @remove_pre_condition[key] if condition condition = "(#{condition})" end if digests.nil? || digests.compact.empty? digest = nil else digest = "(!(#{digests.map{|i| "(#{i.digest})"}.join(" || ")}))" end term = [condition, digest].compact.join(" && ") term.empty? ? "" : "remove [ find #{term} ]" end |
#render_mikrotik(default, cfg, *path) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/construqt/flavour/mikrotik/flavour_mikrotik_result.rb', line 71 def render_mikrotik(default, cfg, *path) enable = !cfg['no_auto_disable'] # HACK cfg.delete("no_auto_disable") prepared = prepare(default, cfg, enable) ret = ["{"] ret << " :local found [find "+prepared.key+"]" ret << " :if ($found = \"\") do={" ret << " :put "+"/#{path.join(' ')} add #{prepared.add_line}".inspect ret << " add #{prepared.add_line}" ret << " } else={" #ret << " :put "+"/#{path.join(' ')} set #{prepared.add_line}".inspect ret << " :local record [get $found]" prepared.result.keys.sort.each do |key| next if prepared.result[key].nil? val = default[key].serialize(prepared.result[key]) next if val.to_s.empty? compare_val = default[key].serialize_compare(prepared.result[key]) if compare_val ret << " :if (($record->#{key.inspect})!=#{compare_val}) do={" ret << " :put "+"/#{path.join(' ')} set [find #{prepared.key} ] #{key}=#{val}".inspect ret << " set $found #{key}=#{val}" ret << " }" else ret << " set $found #{key}=#{val}" end end ret << " }" ret << "}" add(ret.join("\n"), prepared.key, *path) end |
#render_mikrotik_set_by_key(default, cfg, *path) ⇒ Object
66 67 68 69 |
# File 'lib/construqt/flavour/mikrotik/flavour_mikrotik_result.rb', line 66 def render_mikrotik_set_by_key(default, cfg, *path) prepared = prepare(default, cfg) add("set [ find #{prepared.key} ] #{prepared.add_line}", nil, *path) end |
#render_mikrotik_set_direct(default, cfg, *path) ⇒ Object
61 62 63 64 |
# File 'lib/construqt/flavour/mikrotik/flavour_mikrotik_result.rb', line 61 def render_mikrotik_set_direct(default, cfg, *path) prepared = prepare(default, cfg, false) add("set #{prepared.add_line}", nil, *path) end |