Ravn::Tactical::Operation::

DownloadMissionArchive class

Ravn-tactical Operation to download mission archive artifacts from a node.

Public Class Methods

new( mission_id: )

Override the constructor to mandate a mission_id.

   # File lib/ravn/tactical/operation/download_mission_archive.rb
25 def initialize( mission_id: )
26     super
27 end
param_validator()

Return a block for Roda::RodaPlugins::TypecastParams validation for this operation.

   # File lib/ravn/tactical/operation/download_mission_archive.rb
17 def self::param_validator
18     return ->( params ) do
19         params.uuid!( 'mission_id' )
20     end
21 end

Public Instance Methods

after_run( node, response )

Post-run hook – move the archive into the right spot on the Control.

    # File lib/ravn/tactical/operation/download_mission_archive.rb
 90 def after_run( node, response )
 91     if response.status == 200
 92         self.log.info "Reading archive from response: %d bytes" % [ response.body.bytesize ]
 93         self.extract_archive( node, response )
 94 
 95         self.mission.finish_archive if self.mission.all_nodes_archived?
 96 
 97         return true
 98     else
 99         self.log.error "Request to download archive for %s failed: %d" %
100             [ node.device_id, response.status ]
101 
102         return false
103     end
104 end
done_sending()

Streaming callback – called when the file is done being sent on the remote node.

   # File lib/ravn/tactical/operation/download_mission_archive.rb
82 def done_sending
83     self.log.info "Done streaming tar archive. Cleaning up the old archive file."
84     arch_directory = self.mission.archive_directory_path
85     arch_directory.rmtree
86 end
extract_archive( node, response )

Read the archive for the specified node from the given response and put it in the correct directory.

    # File lib/ravn/tactical/operation/download_mission_archive.rb
109 def extract_archive( node, response )
110     directory = self.mission.device_archive_directory_for( node.device_id )
111     Tempfile.create( 'mission-archive' ) do |tarball|
112         response.body.copy_to( tarball )
113         tarball.close
114 
115         self.log.info "Extracting tar archive into %s" % [ directory ]
116         self.log.debug "Parked the tarball at: %p" % [ tarball.path ]
117 
118         Archive::Tar::Minitar.unpack( tarball.path, directory.to_s ) do |action, fn, stats|
119             self.log.debug "%s: %s %p" % [ action, fn, stats ]
120         end
121     end
122 end
mission()

Fetch the mission associated with this operation by the :mission_id parameter.

   # File lib/ravn/tactical/operation/download_mission_archive.rb
39 def mission
40     return @mission ||= begin
41         Ravn::Tactical::Mission.fetch( self.mission_id ) or
42             raise "no such mission %p" % [ mission_id ]
43     end
44 end
mission_id()

Return the configured mission ID for this operation. Raises an exception if no valid mission ID parameter was specified.

   # File lib/ravn/tactical/operation/download_mission_archive.rb
32 def mission_id
33     return self.config[ :mission_id ]
34 end
run( context, request )

Generate the tarball containing mission archive files and return it.

   # File lib/ravn/tactical/operation/download_mission_archive.rb
48 def run( context, request )
49     mission = self.mission
50 
51     raise "Mission %s is not archived!" % [ mission.id ] unless mission.archived?
52     self.log.info "Downloading archive for mission %s." % [ mission.id ]
53     self.stream_files( mission, context )
54 end
stream_files( mission, context )

Stream a tar archive of the given mission‘s archived files via the given context.

   # File lib/ravn/tactical/operation/download_mission_archive.rb
58 def stream_files( mission, context )
59     files = mission.device_archive_files
60 
61     self.log.info "Streaming tar archive with %d file/s" % [ files.size ]
62     context.response[ 'Content-type' ] = 'archive/tar'
63     context.stream( callback: self.method(:done_sending) ) do |raw_out|
64         self.log.debug "Starting archive for %p" % [ raw_out ]
65         out = Archive::Tar::Minitar::Output.new( raw_out )
66 
67         Dir.chdir( mission.device_archive_directory_path ) do
68             files.each do |file|
69                 file = file.relative_path_from( mission.device_archive_directory_path )
70                 self.log.debug "  adding %s" % [ file ]
71                 Archive::Tar::Minitar.pack_file( file.to_s, out ) do |action, fn, stats|
72                     self.log.debug "%s: %s %p" % [ action, fn, stats ]
73                 end
74             end
75         end
76     end
77 end