Class: Junos::Ez::FS::Provider

Inherits:
Provider::Parent show all
Defined in:
lib/junos-ez/utils/fs.rb,
lib/junos-ez/utils/fs.rb

Overview


PRIVATE METHODS

These are helper/private methods, or methods that are current work-in-progress/under-investigation


Instance Attribute Summary

Attributes inherited from Provider::Parent

#catalog, #has, #list, #name, #ndev, #parent, #properties, #providers, #should

Instance Method Summary collapse

Methods inherited from Provider::Parent

#[], #[]=, #activate!, #active?, #catalog!, #create, #create!, #create_from_hash!, #create_from_yaml!, #deactivate!, #delete!, #each, #exists?, #init_has, #initialize, #is_new?, #is_provider?, #list!, #name_decorated, #need_write?, #read!, #rename!, #reorder!, #select, #to_h, #to_h_expanded, #to_yaml, #with, #write!, #xml_at_edit, #xml_at_top, #xml_build_change, #xml_change__active, #xml_change__exist, #xml_change_admin, #xml_change_description, #xml_config_read!, #xml_element_newname, #xml_get_has_xml, #xml_on_create, #xml_on_delete

Constructor Details

This class inherits a constructor from Junos::Ez::Provider::Parent

Instance Method Details

#cat(filename) ⇒ Object


cat - is used to obtain the text contents of the file




188
189
190
191
192
193
194
# File 'lib/junos-ez/utils/fs.rb', line 188

def cat( filename )                   
  begin
    @ndev.rpc.file_show( :filename => filename ).text
  rescue => e
    raise IOError, e.rsp.xpath('rpc-error/error-message').text.strip
  end
end

#checksum(method, path) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/junos-ez/utils/fs.rb', line 69

def checksum( method, path )    
  got = case method
  when :md5
    @ndev.rpc.get_checksum_information( :path => path )
  when :sha256
    @ndev.rpc.get_sha256_checksum_information( :path => path )
  when :sha1
    @ndev.rpc.get_sha1_checksum_information( :path => path )
  end    
  
  f_chk = got.xpath('file-checksum')
  if (err = f_chk.xpath('rpc-error/error-message')[0])
    raise IOError, err.text.strip
  end    
  f_chk.xpath('checksum').text.strip    
end

#cleanup!Object


cleanup! will perform the ‘request system storage cleanup’ command and remove the files. If you want to check which files will be removed, use the cleanup? method first




253
254
255
256
257
258
259
260
# File 'lib/junos-ez/utils/fs.rb', line 253

def cleanup!
  got = @ndev.rpc.request_system_storage_cleanup
  gone_h = {}
  got.xpath('file-list/file').each do |file|
    _cleanup_file_to_h( file, gone_h )
  end
  gone_h
end

#cleanup?Boolean


‘cleanup?’ will return information on files that would be removed if cleanup! was executed


Returns:

  • (Boolean)


267
268
269
270
271
272
273
274
# File 'lib/junos-ez/utils/fs.rb', line 267

def cleanup?
  got = @ndev.rpc.request_system_storage_cleanup( :dry_run => true )
  dryrun_h = {}
  got.xpath('file-list/file').each do |file|
    _cleanup_file_to_h( file, dryrun_h )
  end
  dryrun_h    
end

#cp!(from_file, to_file, opts = {}) ⇒ Object


cp! - copies a file. The from_file and to_file can be URL parameters, yo!

opts will set the source address of the copy command, useful when URL contain SCP, HTTP




284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/junos-ez/utils/fs.rb', line 284

def cp!( from_file, to_file, opts = {} )
  args = { :source => from_file, :destination => to_file }
  args[:source_address] = opts[:source_address] if opts[:source_address]
  
  begin
    got = @ndev.rpc.file_copy( args )
  rescue => e
    raise IOError, e.rsp.xpath('rpc-error/error-message').text.strip
  else
    return true
  end
end

#cwd(directory) ⇒ Object


cwd - change the current working directory. This method will return the String of the new working directory or raise and IOError exception if the directory is invalid




51
52
53
54
55
56
57
58
59
# File 'lib/junos-ez/utils/fs.rb', line 51

def cwd( directory )
  begin
    got = @ndev.rpc.set_cli_working_directory( :directory => directory )
  rescue => e
    raise IOError, "invalid directory: #{directory}"
  else
    got.xpath('working-directory').text
  end    
end

#df(opts = {}) ⇒ Object


df - shows the system storage information

opts = [:text, :xml, :hash]

defaults :hash

opts = value to device size values,

valid only for :format == :hash



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/junos-ez/utils/fs.rb', line 208

def df( opts = {} )
      
  outf = {:format => 'text' } if opts[:format] == :text
  args = { :detail => true } if opts[:size_div]
  
  got = @ndev.rpc.get_system_storage( args, outf )
  
  return got.text if opts[:format] == :text
  return got if opts[:format] == :xml
  
  df_h = {}
  ### need to turn this into a Hash
  got.xpath('filesystem').each do |fs|
    fs_name = fs.xpath('filesystem-name').text.strip
    fs_h = {}
    df_h[fs_name] = fs_h
    
    fs_h[:mounted_on] = fs.xpath('mounted-on').text.strip        
    datum = fs.xpath('total-blocks')
    fs_h[:total_blocks] = datum.text.to_i
    fs_h[:total_size] = datum.attribute('format').value
    
    datum = fs.xpath('used-blocks')
    fs_h[:used_blocks] = datum.text.to_i
    fs_h[:used_size] = datum.attribute('format').value
    fs_h[:used_percent] = fs.xpath('used-percent').text.to_i
    
    datum = fs.xpath('available-blocks')
    fs_h[:avail_blocks] = datum.text.to_i
    fs_h[:avail_size] = datum.attribute('format').value
    if opts[:size_div]
      fs_h[:total_size] = fs_h[:total_size].to_i / opts[:size_div]
      fs_h[:used_size] = fs_h[:used_size].to_i / opts[:size_div]
      fs_h[:avail_size] = fs_h[:avail_size].to_i / opts[:size_div]
    end
  end
  df_h
end

#ls(*args) ⇒ Object


ls - provides directory listing of files/subdirs. if directory is nil, then the current working directory is used. The following options (opts) are supported

:format => [:text, :xml, :hash], default = :hash :recurse => true - recursive listing thru subdirs :detail => true - file details, on if :recurse




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
123
124
125
126
127
128
129
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
180
181
# File 'lib/junos-ez/utils/fs.rb', line 96

def ls( *args )
  
  directory = nil
  opts = {}
  
  case args.count
  when 1
    if args[0].kind_of? Hash
      opts = args[0]
    else
      directory = args[0]
    end
  when 2
    directory = args[0]
    opts = args[1]      
  end
  
  # args are the RPC arguments ...
  args = {}
  args[:path] = directory if directory
  args[:recursive] = true if opts[:recurse]
  args[:detail] = true if opts[:detail]      
  args.delete(:detail) if( args[:detail] and args[:recursive])
  
  # RPC output format, default is XML
  outf = { :format => 'text' } if opts[:format] == :text
  
  got = @ndev.rpc.file_list( args, outf )
  return nil unless got
  
  return got.text if opts[:format] == :text
  return got if opts[:format] == :xml
  
  # if we're here, then we need to conver the output 
  # to a Hash.  Joy!
  
  collect_detail = args[:detail] || args[:recursive]
  
  ls_hash = {}
  got.xpath('directory').each do |dir|
    
    dir_name = dir.xpath('directory-name').text.strip
    dir_hash = {}
    
    dir_hash[:fileblocks] = dir.xpath('total-file-blocks').text.to_i
    files_info = dir.xpath('file-information')
    
    dir_hash[:files] = {}       
    dir_hash[:dirs] = {}        # sub-directories
    
    files_info.each do |file|
      f_name = file.xpath('file-name').text.strip
      f_h = {}                                  
      
      if file.xpath('file-directory')[0]
        dir_hash[:dirs][f_name] = f_h
      else
        dir_hash[:files][f_name] = f_h    
      end
      
      next unless collect_detail
      
      f_h[:owner] = file.xpath('file-owner').text.strip
      f_h[:group] = file.xpath('file-group').text.strip
      f_h[:links] = file.xpath('file-links').text.to_i
      f_h[:size] = file.xpath('file-size').text.to_i
      
      xml_when_item(file.xpath('file-symlink-target')) { |i|
        f_h[:symlink] = i.text.strip
      }
      
      fp = file.xpath('file-permissions')[0]
      f_h[:permissions_text] = fp.attribute('format').value
      f_h[:permissions] = fp.text.to_i
      
      fd = file.xpath('file-date')[0]
      f_h[:date] = fd.attribute('format').value
      f_h[:date_epoc] = fd.text.to_i
      
    end # each directory file
    ls_hash[ dir_name ] = dir_hash        
  end # each directory
  
  return nil if ls_hash.empty?
  ls_hash
end

#mv!(from_path, to_path) ⇒ Object


‘mv’ - just like unix, moves/renames a file


Raises:

  • (IOError)


301
302
303
304
305
# File 'lib/junos-ez/utils/fs.rb', line 301

def mv!( from_path, to_path )
  got = @ndev.rpc.command( "file rename #{from_path} #{to_path}" )
  return true if got.nil?     # got no error
  raise IOError, got.text
end

#pwdObject


pwd - retrieve current working directory, return String




65
66
67
# File 'lib/junos-ez/utils/fs.rb', line 65

def pwd
  ndev.rpc.command("show cli directory").text.strip
end

#rm!(path) ⇒ Object


rm! - just like unix, removes files


Raises:

  • (IOError)


311
312
313
314
315
316
# File 'lib/junos-ez/utils/fs.rb', line 311

def rm!( path )
  got = @ndev.rpc.file_delete( :path => path )
  return true if got.nil?     # got no error
  # otherwise, there was an error, check output
  raise IOError, got.text
end