Class: OpenvasCli::VasTask

Inherits:
VasBase
  • Object
show all
Defined in:
lib/openvas-cli/vas_task.rb

Instance Attribute Summary collapse

Attributes inherited from VasBase

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from VasBase

#destroy, #destroy!, get_by_id, #initialize, #new_record?, #reset_changes, #save, #save!, #to_key, #to_param, #update_attributes

Methods included from ConnAddin

included

Methods included from XmlAddin

included

Constructor Details

This class inherits a constructor from OpenvasCli::VasBase

Instance Attribute Details

#commentObject

Returns the value of attribute comment.



7
8
9
# File 'lib/openvas-cli/vas_task.rb', line 7

def comment
  @comment
end

#config_idObject

Returns the value of attribute config_id.



11
12
13
# File 'lib/openvas-cli/vas_task.rb', line 11

def config_id
  @config_id
end

#last_report_idObject

Returns the value of attribute last_report_id.



10
11
12
# File 'lib/openvas-cli/vas_task.rb', line 10

def last_report_id
  @last_report_id
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/openvas-cli/vas_task.rb', line 6

def name
  @name
end

#schedule_idObject

Returns the value of attribute schedule_id.



13
14
15
# File 'lib/openvas-cli/vas_task.rb', line 13

def schedule_id
  @schedule_id
end

#statusObject

Returns the value of attribute status.



8
9
10
# File 'lib/openvas-cli/vas_task.rb', line 8

def status
  @status
end

#target_idObject

Returns the value of attribute target_id.



12
13
14
# File 'lib/openvas-cli/vas_task.rb', line 12

def target_id
  @target_id
end

#times_runObject

Returns the value of attribute times_run.



9
10
11
# File 'lib/openvas-cli/vas_task.rb', line 9

def times_run
  @times_run
end

Class Method Details

.from_xml_node(node) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/openvas-cli/vas_task.rb', line 203

def self.from_xml_node(node)
  t                = VasTask.new
  t.id             = extract_value_from("@id", node)
  t.name           = extract_value_from("name", node)
  t.comment        = extract_value_from("comment", node)
  t.status         = extract_value_from("status", node)
  if node.at_xpath("progress")
    t.progress = VasTaskProgress.from_xml_node(node.at_xpath("progress"))
  end
  t.times_run      = extract_value_from("report_count/finished", node).to_i
  t.last_report_id = extract_value_from("last_report/report/@id", node)
  t.config_id      = extract_value_from("config/@id", node)
  t.target_id      = extract_value_from("target/@id", node)
  node.xpath("reports/report").each { |xr|
    t.reports << VasReport.new({
      :id => extract_value_from("@id", xr),
      :started_at => extract_value_from("timestamp", xr)
    })
  }  
  
  t.reset_changes
  
  t
end

.get_all(options = {}) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/openvas-cli/vas_task.rb', line 184

def self.get_all(options = {})
  params = {:apply_overrides => 0, :sort_field => "name"}
  
  params[:task_id] = options[:id] if options[:id]

  req = Nokogiri::XML::Builder.new { |xml|
    xml.get_tasks(params)
  }
  
  tasks = connection.send_receive(req.doc)
  
  ret = []
  tasks.xpath('//task').each { |t|
    ret << from_xml_node(t)
  }
  
  ret
end

Instance Method Details

#configObject



57
58
59
# File 'lib/openvas-cli/vas_task.rb', line 57

def config
  @config ||= pull_my_config
end

#config=(val) ⇒ Object



61
62
63
64
# File 'lib/openvas-cli/vas_task.rb', line 61

def config=(val)
  @config = val
  config_id = val.id if val
end

#create_or_updateObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/openvas-cli/vas_task.rb', line 79

def create_or_update
      
  if schedule && schedule.changed?
    return unless schedule.save
    schedule_id = schedule.id
  end
  
  if config && config.changed?
    return unless config.save
    config_id = config.id
  end
   
  req = Nokogiri::XML::Builder.new { |xml|
    if @id
      xml.modify_task(:id => @id) {
        xml.name    { xml.text(@name) }    if name_changed?
        xml.comment { xml.text(@comment) } if comment_changed?
        xml.schedule(:id => @schedule_id)  if schedule_id && !schedule_id.blank? && schedule_id_changed?
      }
    else
      xml.create_task {
        xml.name    { xml.text(@name) }    if @name
        xml.comment { xml.text(@comment) } if @comment
        xml.config(:id => @config_id)
        xml.target(:id => @target_id)
        xml.schedule(:id => @schedule_id)  if @schedule_id && !@schedule_id.blank?
      }
    end
  }
  
  begin
    resp = VasTask.connection.send_receive(req.doc)
    @id = VasTask.extract_value_from("/create_task_response/@id", resp) unless @id
    reset_changes
    
    true
  rescue VasExceptions::CommandException => e
    errors[:command_failure] << e.message
    
    nil
  end
  
  
end

#delete_recordObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/openvas-cli/vas_task.rb', line 124

def delete_record
  req = Nokogiri::XML::Builder.new { |xml|
    xml.delete_task(:task_id => @id)
  }
  
  begin
    VasTask.connection.send_receive(req.doc)
    
    true
  rescue VasExceptions::CommandException => e
    errors[:command_failure] << e.message
    
    nil
  end
end

#pauseObject



168
169
170
171
172
173
174
# File 'lib/openvas-cli/vas_task.rb', line 168

def pause
  req = Nokogiri::XML::Builder.new { |xml|
    xml.pause_task(:task_id => @id)
  }
  
  VasTask.connection.send_receive(req.doc)
end

#progress(refresh = false) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/openvas-cli/vas_task.rb', line 152

def progress(refresh=false)
  if refresh == true || @progress == nil
    params = {:apply_overrides => 0, :sort_field => "name"}
    params[:task_id] = options[:task_id] if options[:task_id]
  
    req = Nokogiri::XML::Builder.new { |xml|
      xml.get_tasks
    }
    
    task = VasTask.connection.send_receive(req.doc)
  
    @progress = VasTaskProgress.from_xml_node task.at_xpath('/get_tasks_response/task/progress')
  end
  @progress
end

#progress=(val) ⇒ Object



148
149
150
# File 'lib/openvas-cli/vas_task.rb', line 148

def progress=(val)
  @progress = val
end

#reportsObject



75
76
77
# File 'lib/openvas-cli/vas_task.rb', line 75

def reports
  @reports ||= []
end

#scheduleObject



48
49
50
# File 'lib/openvas-cli/vas_task.rb', line 48

def schedule
  @schedule ||= pull_my_schedule
end

#schedule=(v) ⇒ Object



52
53
54
55
# File 'lib/openvas-cli/vas_task.rb', line 52

def schedule=(v)
  @schedule = v
  schedule_id = v ? v.id : nil 
end

#startObject



140
141
142
143
144
145
146
# File 'lib/openvas-cli/vas_task.rb', line 140

def start
  req = Nokogiri::XML::Builder.new { |xml|
    xml.resume_or_start_task(:task_id => @id)
  }
  
  VasTask.connection.send_receive(req.doc)
end

#stopObject



176
177
178
179
180
181
182
# File 'lib/openvas-cli/vas_task.rb', line 176

def stop
  req = Nokogiri::XML::Builder.new { |xml|
    xml.stop_task(:task_id => @id)
  }
  
  VasTask.connection.send_receive(req.doc)
end

#targetObject



66
67
68
# File 'lib/openvas-cli/vas_task.rb', line 66

def target
  @target ||= pull_my_target
end

#target=(val) ⇒ Object



70
71
72
73
# File 'lib/openvas-cli/vas_task.rb', line 70

def target=(val)
  @target = val
  target_id = val.id if val
end