Class: Oxidized::Model

Inherits:
Object
  • Object
show all
Includes:
Config::Vars
Defined in:
lib/oxidized/model/model.rb,
lib/oxidized/model/outputs.rb

Defined Under Namespace

Classes: Outputs

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Config::Vars

#vars

Instance Attribute Details

#inputObject

Returns the value of attribute input.



99
100
101
# File 'lib/oxidized/model/model.rb', line 99

def input
  @input
end

#nodeObject

Returns the value of attribute node.



99
100
101
# File 'lib/oxidized/model/model.rb', line 99

def node
  @node
end

Class Method Details

.cfg(*methods, **args, &block) ⇒ Object



27
28
29
30
31
# File 'lib/oxidized/model/model.rb', line 27

def cfg *methods, **args, &block
  [methods].flatten.each do |method|
    process_args_block(@cfg[method.to_s], args, block)
  end
end

.cfgsObject



33
34
35
# File 'lib/oxidized/model/model.rb', line 33

def cfgs
  @cfg
end

.cmd(_cmd = nil, **args, &block) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/oxidized/model/model.rb', line 37

def cmd _cmd = nil, **args, &block
  if _cmd.class == Symbol
    process_args_block(@cmd[_cmd], args, block)
  else
    process_args_block(@cmd[:cmd], args, [_cmd, block])
  end
  Oxidized.logger.debug "lib/oxidized/model/model.rb Added #{_cmd} to the commands list"
end

.cmdsObject



46
47
48
# File 'lib/oxidized/model/model.rb', line 46

def cmds
  @cmd
end

.comment(_comment = '# ') ⇒ Object



18
19
20
21
# File 'lib/oxidized/model/model.rb', line 18

def comment _comment = '# '
  return @comment if @comment
  @comment = block_given? ? yield : _comment
end

.expect(re, **args, &block) ⇒ Object



50
51
52
# File 'lib/oxidized/model/model.rb', line 50

def expect re, **args, &block
  process_args_block(@expect, args, [re, block])
end

.expectsObject



54
55
56
# File 'lib/oxidized/model/model.rb', line 54

def expects
  @expect
end

.inherited(klass) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/oxidized/model/model.rb', line 9

def inherited klass
  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
end

.post(**args) { ... } ⇒ void

This method returns an undefined value.

calls the block at the end of the model, adding the output of the block to the output string

Yields:

  • expects block which should return [String]

Author:

Since:

  • 0.0.39



76
77
78
# File 'lib/oxidized/model/model.rb', line 76

def post **args, &block
  process_args_block(@procs[:post], args, block)
end

.pre(**args) { ... } ⇒ void

This method returns an undefined value.

calls the block at the end of the model, prepending the output of the block to the output string

Yields:

  • expects block which should return [String]

Author:

Since:

  • 0.0.39



65
66
67
# File 'lib/oxidized/model/model.rb', line 65

def pre **args, &block
  process_args_block(@procs[:pre], args, block)
end

.procsHash

Returns hash proc procs :pre+:post to be prepended/postfixed to output.

Returns:

  • (Hash)

    hash proc procs :pre+:post to be prepended/postfixed to output

Author:

Since:

  • 0.0.39



83
84
85
# File 'lib/oxidized/model/model.rb', line 83

def procs
  @procs
end

.prompt(_prompt = nil) ⇒ Object



23
24
25
# File 'lib/oxidized/model/model.rb', line 23

def prompt _prompt = nil
  @prompt or @prompt = _prompt
end

Instance Method Details

#cfgObject



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

def cfg
  self.class.cfgs
end

#cmd(string, &block) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/oxidized/model/model.rb', line 101

def cmd string, &block
  Oxidized.logger.debug "lib/oxidized/model/model.rb Executing #{string}"
  out = @input.cmd(string)
  return false unless out
  out = out.b unless Oxidized.config.input.utf8_encoded?
  self.class.cmds[:all].each do |all_block|
    out = instance_exec Oxidized::String.new(out), string, &all_block
  end
  if vars :remove_secret
    self.class.cmds[:secret].each do |all_block|
      out = instance_exec Oxidized::String.new(out), string, &all_block
    end
  end
  out = instance_exec Oxidized::String.new(out), &block if block
  process_cmd_output out, string
end

#comment(_comment) ⇒ Object



169
170
171
172
173
174
175
# File 'lib/oxidized/model/model.rb', line 169

def comment _comment
  data = ''
  _comment.each_line do |line|
    data << self.class.comment << line
  end
  data
end

#expect(re, &block) ⇒ Object



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

def expect re, &block
  self.class.expect re, &block
end

#expects(data) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/oxidized/model/model.rb', line 138

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

#getObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/oxidized/model/model.rb', line 151

def get
  Oxidized.logger.debug 'lib/oxidized/model/model.rb Collecting commands\' outputs'
  outputs = Outputs.new
  procs = self.class.procs
  self.class.cmds[:cmd].each do |command, block|
    out = cmd command, &block
    return false unless out
    outputs << out
  end
  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
  outputs
end

#outputObject



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

def output
  @input.output
end

#promptObject



134
135
136
# File 'lib/oxidized/model/model.rb', line 134

def prompt
  self.class.prompt
end

#screenscrapeObject



177
178
179
# File 'lib/oxidized/model/model.rb', line 177

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

#send(data) ⇒ Object



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

def send data
  @input.send data
end