Class: Oxidized::Model

Inherits:
Object
  • Object
show all
Extended by:
DSLCommands, DSLSetup, Macros
Includes:
Config::Vars, SemanticLogger::Loggable
Defined in:
lib/oxidized/model/model.rb,
lib/oxidized/model/macros.rb,
lib/oxidized/model/outputs.rb,
lib/oxidized/model/dslsetup.rb,
lib/oxidized/model/dslcommands.rb

Direct Known Subclasses

ACMEPACKET, ACOS, ACSW, ADVA, AEN, ALTEONOS, AOS, AOS7, AOSW, ARBOS, ASA, AWPlus, AddPack, Adtran, Aireos, Airfiber, Airos, Alvarion, Aoscx, ApcAos, Apc_aos, AricentISS, ArubaInstant, AsterNOS, AsyncOS, AudioCodes, AudioCodesMP, AxOS, BDCOM, BR6910, Boss, C4CMTS, CNOS, Cambium, CambiumePMP, Casa, Catos, CiscoCE, CiscoNGA, CiscoSMA, CiscoSMB, CiscoVPN3k, ComnetMS, Comtrol, Comware, Coriant8600, CoriantGroove, CoriantTmos, Cumulus, DCNOS, DNOS, DataCom, Defacto, DellX, Dlink, DlinkNextGen, ECIapollo, EFOS, EOS, EatonNetwork, EdgeCOS, EdgeSwitch, Edgeos, Eltex, Enterasys, Enterasys800, Enterprise_SONiC, Exalink, F5OS, FSOS, FTOS, FabricOS, FastIron, FiberDriver, FireLinuxOS, Firebrick, FirewareOS, FortiGate, FortiOS, FortiWLC, FujitsuPY, GaiaOS, Garderos, GcomBNPS, GrandStream, GrandstreamHT8xx, H3C, HPEBladeSystem, HPMSM, Hatteras, Hios, Hirschmann, HpeMsa, IBOS, IOS, IOSXR, IPOS, ISAM, Icotera, Ingate, IronWare, Ivanti, JunOS, KornfeldOS, LANCOM, LenovoNOS, LinksysSRW, LinuxGeneric, ML66, MLNXOS, MasterOS, Mimosab11, Mtrlrfs, NDMS, NOS, NSXConfig, NSXDfw, NSXFirewall, NXOS, NecIX, NetScaler, Netgear, Netonix, Nodegrid, OS10, OS6, OcNOS, OneFinity, OneOS, OpenGear, OpenWrt, Openbsd, OpnSense, PanOS, PanOS_API, Perle, PfSense, Planet, PowerConnect, Procurve, PurityOS, QTECH, QuantaOS, RAISECOM, RGOS, Riverbed, RouterOS, SAOS, SAOS10, SGOS, SLXOS, SROS, SROSMD, ScreenOS, Siklu, SikluMHTG, SixWind, SmartAX, SmartByte, SmartCS, SonicOS, SpeedTouch, StoneOS, SwOS, TDRE, TELCO, TMOS, TNSR, TPLink, Trango, TrueNAS, UCS, UPLINKOLT, Unifiap, VOLTAIRE, VRP, Viptela, Voss, Vyatta, Vyos, WEOS, XOS, Yamaha, ZTEOLT, ZhoneOLT, Zy1308, ZyNOS, ZyNOSADSL, ZyNOSCLI, ZyNOSGS, ZyNOSMGS

Defined Under Namespace

Modules: DSLCommands, DSLSetup, Macros Classes: Outputs

Constant Summary collapse

METADATA_DEFAULT =

rubocop:disable Style/FormatStringToken

"%{comment}Fetched by Oxidized with model %{model} " \
"from host %{name} [%{ip}]\n".freeze

Constants included from Macros

Macros::VERBS

Instance Attribute Summary collapse

Attributes included from DSLCommands

#procs

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSLSetup

cfgs, input_sequence, inputs

Methods included from DSLCommands

cmds, post, pre

Methods included from Config::Vars

#vars

Instance Attribute Details

#inputObject

Returns the value of attribute input.



48
49
50
# File 'lib/oxidized/model/model.rb', line 48

def input
  @input
end

#nodeObject

Returns the value of attribute node.



48
49
50
# File 'lib/oxidized/model/model.rb', line 48

def node
  @node
end

Class Method Details

.inherited(klass) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/oxidized/model/model.rb', line 26

def inherited(klass)
  super
  if klass.superclass == Oxidized::Model
    klass.instance_variable_set('@cmd',     Hash.new { |h, k| h[k] = [] })
    klass.instance_variable_set('@cfg',     Hash.new { |h, k| h[k] = [] })
    klass.instance_variable_set('@procs',   Hash.new { |h, k| h[k] = [] })
    klass.instance_variable_set '@expect',  []
    klass.instance_variable_set '@comment', nil
    klass.instance_variable_set '@prompt',  nil
    klass.instance_variable_set '@metadata', {}
    klass.instance_variable_set '@inputs', nil

  else # we're subclassing some existing model, take its variables
    instance_variables.each do |var|
      iv = instance_variable_get(var)
      klass.instance_variable_set var, iv.dup
      @cmd[:cmd] = iv[:cmd].dup if var.to_s == "@cmd"
    end
  end
end

Instance Method Details

#cfgObject



125
126
127
# File 'lib/oxidized/model/model.rb', line 125

def cfg
  self.class.cfgs
end

#cmd(string, input: nil, &block) ⇒ Object

input specifies to run this command only with this input type if input is not specified, always run the command



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/oxidized/model/model.rb', line 52

def cmd(string, input: nil, &block)
  logger.debug "Executing #{string}"
  out = if input.nil? || input.include?(@input.to_sym)
          out = @input.cmd(string)
        else
          # Do not run this command
          return ''
        end
  return false unless out

  out = out.b unless Oxidized.config.input.utf8_encoded?
  self.class.cmds[:all].each do |all_block|
    out = instance_exec out, string, &all_block
  end
  if vars :remove_secret
    self.class.cmds[:secret].each do |all_block|
      out = instance_exec out, string, &all_block
    end
  end
  out = instance_exec out, &block if block
  process_cmd_output out, string
end

#comment(str) ⇒ Object



174
175
176
177
178
179
180
# File 'lib/oxidized/model/model.rb', line 174

def comment(str)
  data = String.new('')
  str.each_line do |line|
    data << self.class.comment << line
  end
  data
end

#expectObject



121
122
123
# File 'lib/oxidized/model/model.rb', line 121

def expect(...)
  self.class.expect(...)
end

#expects(data) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/oxidized/model/model.rb', line 133

def expects(data)
  self.class.expects.each do |re, cb|
    if data.match re
      data = cb.arity == 2 ? instance_exec([data, re], &cb) : instance_exec(data, &cb)
    end
  end
  data
end

#getObject

Get the commands from the model



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
# File 'lib/oxidized/model/model.rb', line 143

def get
  logger.debug 'Collecting commands\' outputs'
  outputs = Outputs.new
  self.class.cmds[:cmd].each do |data|
    command = data[:cmd]
    args = data[:args]
    block = data[:block]

    next if args.include?(:if) && !instance_exec(&args[:if])

    out = cmd command, input: args[:input], &block
    return false unless out

    outputs << out
  end
  procs = self.class.procs
  procs[:pre].each do |pre_proc|
    outputs.unshift process_cmd_output(instance_eval(&pre_proc), '')
  end
  procs[:post].each do |post_proc|
    outputs << process_cmd_output(instance_eval(&post_proc), '')
  end
  if vars("metadata") == true
     = (:top)
     = (:bottom)
    outputs.unshift  if 
    outputs <<  if 
  end
  outputs
end

#interpolate_string(template) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/oxidized/model/model.rb', line 93

def interpolate_string(template)
  return nil unless template

  time = Time.now
  template_variables = {
    model:   self.class.name,
    name:    node.name,
    ip:      node.ip,
    group:   node.group,
    comment: self.class.comment,
    year:    time.year,
    month:   "%02d" % time.month,
    day:     "%02d" % time.day,
    hour:    "%02d" % time.hour,
    minute:  "%02d" % time.min,
    second:  "%02d" % time.sec
  }
  template % template_variables
end

#metadata(position) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/oxidized/model/model.rb', line 75

def (position)
  return unless %i[top bottom].include? position

   = self.class.instance_variable_get(:@metadata)
  var_position = { top: "metadata_top", bottom: "metadata_bottom" }
  if [:top] || [:bottom]
    # the model defines metadata at :top ot :bottom, use the model
    value = [position]
    value.is_a?(Proc) ? instance_eval(&value) : interpolate_string(value)
  elsif vars("metadata_top") || vars("metadata_bottom")
    # vars defines metadata_top or metadata bottom, use the vars
    interpolate_string(vars(var_position[position]))
  elsif position == :top
    # default: use METADATA_DEFAULT for top
    interpolate_string(METADATA_DEFAULT)
  end
end

#outputObject



113
114
115
# File 'lib/oxidized/model/model.rb', line 113

def output
  @input.output
end

#promptObject



129
130
131
# File 'lib/oxidized/model/model.rb', line 129

def prompt
  self.class.prompt
end

#screenscrapeObject



200
201
202
# File 'lib/oxidized/model/model.rb', line 200

def screenscrape
  @input.class.to_s.match(/Telnet/) || vars(:ssh_no_exec)
end

#send(data) ⇒ Object



117
118
119
# File 'lib/oxidized/model/model.rb', line 117

def send(data)
  @input.send data
end

#significant_changes(config) ⇒ Object



204
205
206
207
208
209
# File 'lib/oxidized/model/model.rb', line 204

def significant_changes(config)
  self.class.cmds[:significant_changes].each do |block|
    config = instance_exec config, &block
  end
  config
end

#xmlcomment(str) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/oxidized/model/model.rb', line 182

def xmlcomment(str)
  # XML Comments start with <!-- and end with -->
  #
  # Because it's illegal for the first or last characters of a comment
  # to be a -, i.e. <!--- or ---> are illegal, and also to improve
  # readability, we add extra spaces after and before the beginning
  # and end of comment markers.
  #
  # Also, XML Comments must not contain --. So we put a space between
  # any double hyphens, by replacing any - that is followed by another -
  # with '- '
  data = String.new('')
  str.each_line do |_line|
    data << '<!-- ' << str.gsub(/-(?=-)/, '- ').chomp << " -->\n"
  end
  data
end