storazzo 0.3.7 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -0
- data/VERSION +1 -1
- data/bin/stats-with-md5 +428 -0
- data/lib/storazzo/media/gcs_bucket.rb +4 -2
- data/lib/storazzo/ric_disk.rb +111 -47
- data/lib/storazzo/ric_disk_config.rb +32 -21
- data/lib/storazzo/ric_disk_sample_config.rb +1 -1
- data/lib/storazzo/ric_disk_ugly.rb +245 -245
- data/storazzo.gemspec +4 -1
- data/test/test_gcs_bucket.rb +26 -15
- data/test/test_local_folder.rb +67 -15
- data/test/test_storazzo.rb +9 -9
- metadata +4 -2
data/lib/storazzo/ric_disk.rb
CHANGED
@@ -16,8 +16,13 @@ module Storazzo
|
|
16
16
|
|
17
17
|
# in order of finding, so the first will be the one we actually READ and use. I could looknat the date but cmon...
|
18
18
|
# These are the files I do accept.
|
19
|
-
ConfigFiles = %W{ ricdisk.yaml .ricdisk
|
20
|
-
|
19
|
+
ConfigFiles = %W{ ricdisk.yaml .ricdisk storazzo.yaml }
|
20
|
+
DefaultConfigFile = "storazzo.yaml" # .ricdisk }
|
21
|
+
RicdiskVersion = '2.1'
|
22
|
+
RicdiskHistory = [
|
23
|
+
'2022-07-29 2.1 Added timestamp',
|
24
|
+
'2022-07-28 2.0 Added tags, siz, unique_hash, computation_hostname, wr, ...',
|
25
|
+
]
|
21
26
|
DefaultGemfileTestDiskFolder = Storazzo.root + "/var/test/disks/" # was: @@default_gemfile_test_disks_folder
|
22
27
|
# Immutable
|
23
28
|
DefaultMediaFolders = %w{
|
@@ -26,7 +31,7 @@ module Storazzo
|
|
26
31
|
}.append(DefaultGemfileTestDiskFolder ).append("/media/#{ENV["USER"]}/" )
|
27
32
|
|
28
33
|
# # todo substitute with protobuf..
|
29
|
-
attr_accessor :name, :description, :ricdisk_file
|
34
|
+
attr_accessor :name, :description, :ricdisk_file, :ricdisk_file_full, :local_mountpoint, :wr, :path,
|
30
35
|
:ricdisk_file_empty, :size, :active_dirs, :ricdisk_version,
|
31
36
|
:unique_hash # new 202207
|
32
37
|
|
@@ -38,7 +43,6 @@ module Storazzo
|
|
38
43
|
|
39
44
|
def initialize(path, opts={})
|
40
45
|
deb "RicDisk initialize.. path=#{path}"
|
41
|
-
# @local_mountpoint = path
|
42
46
|
@local_mountpoint = File.expand_path(path)
|
43
47
|
@description = "This is an automated RicDisk description from v.#{RicdiskVersion}. Created on #{Time.now}'"
|
44
48
|
@ricdisk_version = RicdiskVersion
|
@@ -52,6 +56,7 @@ module Storazzo
|
|
52
56
|
@size = `du -s '#{path}'`.split(/\s/)[0] # self.size
|
53
57
|
@unique_hash = "MD5::" + Digest::MD5.hexdigest(File.expand_path(path)) # hash = Digest::MD5.hexdigest(File.expand_path(get_local_mountpoint))
|
54
58
|
@computation_hostname = Socket.gethostname
|
59
|
+
@created_at = Time.now
|
55
60
|
|
56
61
|
@ricdisk_file_empty = ricdisk_file_empty?
|
57
62
|
|
@@ -97,8 +102,10 @@ module Storazzo
|
|
97
102
|
def writeable?()
|
98
103
|
return @wr unless @wr.nil?
|
99
104
|
# Otherwise I can do an EXPENSIVE calculation
|
100
|
-
puts "TODO(ricc): Do expensive calculation if this FS is writeable: #{path}"
|
101
|
-
|
105
|
+
puts yellow("TODO(ricc): Do expensive calculation if this FS is writeable: #{path}")
|
106
|
+
#@wr = File.writable?(File.expand_path(@ricdisk_file)) # rescue false
|
107
|
+
raise "for some reason an important info (ricdisk_file='#{absolute_path}') is missing!" if ricdisk_file.nil?
|
108
|
+
@wr = File.writable?(absolute_path) # rescue false
|
102
109
|
return @wr
|
103
110
|
#:boh_todo_fix_me_and_compute
|
104
111
|
#false
|
@@ -123,9 +130,9 @@ module Storazzo
|
|
123
130
|
end
|
124
131
|
|
125
132
|
def absolute_path
|
126
|
-
|
133
|
+
#@local_mountpoint + "/" + @ricdisk_file
|
134
|
+
"#{local_mountpoint}/#{ricdisk_file}"
|
127
135
|
end
|
128
|
-
|
129
136
|
|
130
137
|
def self.find_active_dirs(base_dirs=nil, also_mountpoints=true)
|
131
138
|
if base_dirs.nil?
|
@@ -166,15 +173,32 @@ module Storazzo
|
|
166
173
|
deb "[CACHE HIT] ricdisk_file (didnt have to recompute it - yay!)"
|
167
174
|
return @ricdisk_file
|
168
175
|
end
|
169
|
-
|
176
|
+
deb "[compute_ricdisk_file] RICC_WARNING This requires cmputation I wanna do it almost once"
|
170
177
|
ConfigFiles.each do |papable_config_filename|
|
171
178
|
#return ".ricdisk.yaml" if File.exist?("#{path}/.ricdisk.yaml") #and File.empty?( "#{path}/.ricdisk.yaml")
|
172
179
|
#return ".ricdisk" if File.exist?("#{path}/.ricdisk") # and File.empty?( "#{path}/.ricdisk")
|
173
180
|
return papable_config_filename if File.exist?("#{path}/#{papable_config_filename}") # and File.empty?( "#{path}/.ricdisk")
|
174
181
|
end
|
175
|
-
|
182
|
+
deb "File not found! Neither #{ConfigFiles} exist.."
|
183
|
+
# return nil
|
184
|
+
return DefaultConfigFile
|
176
185
|
end
|
177
186
|
|
187
|
+
|
188
|
+
# def self.compute_ricdisk_file_by_path_once(path)
|
189
|
+
# # unless @ricdisk_file.nil?
|
190
|
+
# # deb "[CACHE HIT] ricdisk_file (didnt have to recompute it - yay!)"
|
191
|
+
# # return @ricdisk_file
|
192
|
+
# # end
|
193
|
+
# warn "RICC_WARNING This requires cmputation I wanna do it almost once"
|
194
|
+
# ConfigFiles.each do |papable_config_filename|
|
195
|
+
# #return ".ricdisk.yaml" if File.exist?("#{path}/.ricdisk.yaml") #and File.empty?( "#{path}/.ricdisk.yaml")
|
196
|
+
# #return ".ricdisk" if File.exist?("#{path}/.ricdisk") # and File.empty?( "#{path}/.ricdisk")
|
197
|
+
# return papable_config_filename if File.exist?("#{path}/#{papable_config_filename}") # and File.empty?( "#{path}/.ricdisk")
|
198
|
+
# end
|
199
|
+
# return nil
|
200
|
+
# end
|
201
|
+
|
178
202
|
|
179
203
|
# # new
|
180
204
|
# def self.get_ricdisk_file_obsolete(path)
|
@@ -198,13 +222,13 @@ module Storazzo
|
|
198
222
|
end
|
199
223
|
|
200
224
|
# maybe move to a RiccFile class? Maybe even INHERIT from FILE?
|
201
|
-
def
|
225
|
+
def obsolescence_seconds(file_path)
|
202
226
|
creation_time = File.stat(file_path).ctime
|
203
227
|
deb("[obsolescence_seconds] File #{file_path}: #{creation_time} - #{(Time.now - creation_time)} seconds ago")
|
204
228
|
(Time.now - creation_time).to_i
|
205
229
|
end
|
206
230
|
# maybe move to a RiccFile class? Maybe even INHERIT from FILE?
|
207
|
-
def
|
231
|
+
def obsolescence_days(file_path)
|
208
232
|
return obsolescence_seconds(file_path) / 86400
|
209
233
|
end
|
210
234
|
|
@@ -234,11 +258,16 @@ module Storazzo
|
|
234
258
|
# puts(yellow disk_info.to_yaml)
|
235
259
|
# end
|
236
260
|
if disk_info.is_a?(RicDisk)
|
237
|
-
|
238
|
-
if File.empty?(disk_info.absolute_path) and (disk_info.wr)
|
261
|
+
puts yellow("DEB disk_info.class: #{disk_info.class}")
|
262
|
+
if File.empty?(disk_info.absolute_path) # and (disk_info.wr)
|
239
263
|
puts(green("yay, we can now write the file '#{disk_info.absolute_path}' (which is R/W, I just checked!) with proper YAML content.."))
|
240
|
-
|
241
|
-
|
264
|
+
if disk_info.wr
|
265
|
+
ret = File.write(disk_info.absolute_path, disk_info.obj_to_yaml)
|
266
|
+
puts green("Written file! ret=#{ret}")
|
267
|
+
else
|
268
|
+
raise "TODO_IMPLEMENT: write in proper place in config dir"
|
269
|
+
puts red("TODO implement me")
|
270
|
+
end
|
242
271
|
else
|
243
272
|
puts(red("Something not right here: either file is NOT empty or disk is NOT writeable.. #{File.empty?(disk_info.absolute_path)}"))
|
244
273
|
puts("File size: #{File.size(disk_info.absolute_path)}")
|
@@ -246,30 +275,63 @@ module Storazzo
|
|
246
275
|
puts(disk_info.obj_to_hash)
|
247
276
|
puts(disk_info.obj_to_yaml)
|
248
277
|
end
|
249
|
-
else
|
278
|
+
else # not a RicDisk..
|
250
279
|
puts "[write_config_yaml_to_disk] No DiskInfo found across #{ConfigFiles}. I leave this function empty-handed."
|
251
280
|
end
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
281
|
+
|
282
|
+
#disk_info.absolute_path
|
283
|
+
#if File.exists?( "#{subdir}/.ricdisk") and ! File.empty?( "#{subdir}/.ricdisk")
|
284
|
+
# if File.exists?(disk_info.absolute_path) and ! File.empty?(disk_info.absolute_path)
|
285
|
+
# puts("Config File found with old-style name: '#{subdir}/.ricdisk' ! Please move it to .ricdisk.yaml!")
|
286
|
+
# puts(white `cat "#{disk_info.absolute_path}"`)
|
287
|
+
# else
|
288
|
+
# puts "WRITING NOW. [I BELIEVE THIS IS DUPE CODE - see a few lines above!] disk_info.obj_to_yaml .. to #{compute_ricdisk_file}"
|
289
|
+
# File.open(ricdisk_file_full, 'w').write(disk_info.obj_to_yaml)
|
290
|
+
# end
|
259
291
|
end
|
260
292
|
|
261
293
|
# TODO obsolete this as i should NOT be calling it from clas, but from method.
|
262
294
|
def self.ok_dir?(subdir)
|
263
295
|
File.exists?( "#{subdir}/.ricdisk") or File.exists?( "#{subdir}/.ricdisk.yaml")
|
264
296
|
end
|
297
|
+
|
298
|
+
def compute_stats_files(opts={})
|
299
|
+
puts azure("[compute_stats_files] TODO implement natively. Now I'm being lazy")
|
300
|
+
#Storazzo::RicDisk.calculate_stats_files(path, opts)
|
301
|
+
opts_upload_to_gcs = opts.fetch :upload_to_gcs, true
|
302
|
+
dir = path
|
303
|
+
|
304
|
+
full_file_path = "#{dir}/#{$stats_file}"
|
305
|
+
#return "This refactor is for another day"
|
306
|
+
|
307
|
+
puts("compute_stats_files(#{white dir}): #{white full_file_path}")
|
308
|
+
puts "TEST1 DIR EXISTS: #{dir} -> #{File.directory? dir}"
|
309
|
+
Dir.chdir(dir)
|
310
|
+
if File.exists?(full_file_path) and ($opts[:force] == false)
|
311
|
+
puts "File '#{$stats_file}' exists already." # - now should see if its too old, like more than 1 week old"
|
312
|
+
# TODO check for file time...
|
313
|
+
print "Lines found: #{yellow `wc -l "#{full_file_path}" `.chomp }. File obsolescence (days): #{yellow obsolescence_days(full_file_path)}."
|
314
|
+
if obsolescence_days(full_file_path) > 7
|
315
|
+
puts("*** ACHTUNG *** FIle is pretty old. You might consider rotating: #{yellow "mv #{full_file_path} #{full_file_path}_old"}. Or invoke with --force")
|
316
|
+
end
|
317
|
+
upload_to_gcs(full_file_path) if opts_upload_to_gcs
|
318
|
+
else
|
319
|
+
puts "Crunching data stats from '#{dir}' into '#{$stats_file}' ... please bear with me.. [maybe file didnt exist, maybe $opts[:force] is true]"
|
320
|
+
command = "find . -print0 | xargs -0 stats-with-md5 --no-color | tee '#{full_file_path}'"
|
321
|
+
puts("[#{`pwd`.chomp}] Executing: #{azure command}")
|
322
|
+
ret = backquote_execute(command)
|
323
|
+
puts "Done. #{ret.split("\n").count} files processed."
|
324
|
+
end
|
325
|
+
end
|
265
326
|
|
266
327
|
|
267
328
|
|
268
329
|
# Create RDS file.
|
269
330
|
def self.calculate_stats_files(dir, opts={})
|
270
|
-
opts_upload_to_gcs = opts.fetch :upload_to_gcs, true
|
331
|
+
opts_upload_to_gcs = opts.fetch :upload_to_gcs, true
|
332
|
+
|
271
333
|
full_file_path = "#{dir}/#{$stats_file}"
|
272
|
-
return "This
|
334
|
+
return "This refactor is for another day"
|
273
335
|
|
274
336
|
puts("calculate_stats_files(#{white dir}): #{white full_file_path}")
|
275
337
|
puts "TEST1 DIR EXISTS: #{dir} -> #{File.directory? dir}"
|
@@ -284,7 +346,7 @@ module Storazzo
|
|
284
346
|
upload_to_gcs(full_file_path) if opts_upload_to_gcs
|
285
347
|
else
|
286
348
|
puts "Crunching data stats from '#{dir}' into '#{$stats_file}' ... please bear with me.. [maybe file didnt exist, maybe $opts[:force] is true]"
|
287
|
-
command = "find . -print0 | xargs -0 stats-with-md5
|
349
|
+
command = "find . -print0 | xargs -0 stats-with-md5 --no-color | tee '#{full_file_path}'"
|
288
350
|
puts("[#{`pwd`.chomp}] Executing: #{azure command}")
|
289
351
|
ret = backquote_execute command
|
290
352
|
puts "Done. #{ret.split("\n").count} files processed."
|
@@ -394,25 +456,27 @@ end #/Module
|
|
394
456
|
|
395
457
|
|
396
458
|
|
397
|
-
|
398
|
-
|
399
|
-
#
|
400
|
-
#
|
401
|
-
|
459
|
+
def backquote_execute(cmd, opts={})
|
460
|
+
dryrun = opts.fetch :dryrun, false
|
461
|
+
# executed a command wrapped by dryrun though
|
462
|
+
return "DRYRUN backquote_execute(#{cmd})" if dryrun # $opts[:dryrun]
|
463
|
+
`#{cmd}`
|
464
|
+
end
|
402
465
|
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
#
|
413
|
-
#
|
414
|
-
#
|
415
|
-
#
|
416
|
-
#
|
417
|
-
|
466
|
+
def upload_to_gcs(file, opts={})
|
467
|
+
deb("upload_to_gcs(#{file}). TODO(ricc) after breafast upload to GCS : #{file}")
|
468
|
+
mount_name = file.split('/')[-2]
|
469
|
+
filename = "#{mount_name}-#{File.basename file}"
|
470
|
+
hostname = Socket.gethostname[/^[^.]+/]
|
471
|
+
command = "gsutil cp '#{file}' gs://#{$gcs_bucket}/backup/ricdisk-magic/#{ hostname }-#{filename}"
|
472
|
+
deb("Command: #{command}")
|
473
|
+
puts azure("GCS upload disabled until I know if it works :) command='#{command}'")
|
474
|
+
ret = backquote_execute(command, :dryrun => true)
|
475
|
+
# if $opts[:debug] do
|
476
|
+
# puts "+ Current list of files:"
|
477
|
+
# ret = backquote_execute("gsutil ls -al gs://#{$gcs_bucket}/backup/ricdisk-magic/")
|
478
|
+
# puts ret
|
479
|
+
# end
|
480
|
+
ret
|
481
|
+
end
|
418
482
|
|
@@ -41,7 +41,7 @@ public
|
|
41
41
|
verbose = opts.fetch :verbose, false
|
42
42
|
|
43
43
|
if already_loaded? # and not self.config.nil?
|
44
|
-
puts "[#{self.class}] load: already loaded"
|
44
|
+
puts "[#{self.class}] VERBOSE load: already loaded" if verbose
|
45
45
|
return self.config
|
46
46
|
end
|
47
47
|
|
@@ -116,6 +116,18 @@ public
|
|
116
116
|
"POLY_#{self.class}_(ver=#{config_ver}, file=#{config_file}), #{white(size)} bytes" # - config_default_folder=#{self.config_default_folder}"
|
117
117
|
end
|
118
118
|
|
119
|
+
def to_verbose_s
|
120
|
+
h = {}
|
121
|
+
h[:description] = "This is a Verbose Hash describing a RicDiskConfig or its child RicDiskSampleConfig to understand why it keeps failing.."
|
122
|
+
h[:to_s] = self.to_s
|
123
|
+
h[:class] = self.class
|
124
|
+
h[:file] = __FILE__
|
125
|
+
h[:id] = self.object_id
|
126
|
+
h[:get_bucket_paths] = self.get_bucket_paths()
|
127
|
+
h[:get_local_folders] = self.get_local_folders()
|
128
|
+
return h
|
129
|
+
end
|
130
|
+
|
119
131
|
def get_config(opts={})
|
120
132
|
return load(opts) if @config.nil?
|
121
133
|
@config
|
@@ -160,27 +172,26 @@ public
|
|
160
172
|
# RicDisk.write_config_yaml_to_disk(dir)
|
161
173
|
# RicDisk.calculate_stats_files(dir) # dir is inutile
|
162
174
|
# } # TODO refactor in option sbrodola_afterwards=true. :)
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
#if
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
# deb "Figghiu ri buttana: doesnt exist #{red dir}"
|
182
|
-
end
|
175
|
+
# else
|
176
|
+
raise "Wrong input, I need an array here: #{files_list} " unless files_list.is_a?(Array)
|
177
|
+
puts "iterate_through_file_list_for_disks(): I consider files_list as a list of directories to parse :)" if verbose
|
178
|
+
|
179
|
+
#dirs = RicDisk.find_active_dirs()
|
180
|
+
files_list.each do |dir|
|
181
|
+
dir = File.expand_path(dir)
|
182
|
+
if File.directory?(dir)
|
183
|
+
#if dirs.include?(dir)
|
184
|
+
puts "iterate_through_file_list_for_disks() Legit dir: #{green dir}" if verbose
|
185
|
+
rd = RicDisk.new(dir)
|
186
|
+
puts "RicDisk: #{rd}"
|
187
|
+
rd.write_config_yaml_to_disk(dir)
|
188
|
+
#RicDisk.write_config_yaml_to_disk(dir)
|
189
|
+
#RicDisk.calculate_stats_files (CLASS) => will become OBJECT compute_stats_files
|
190
|
+
rd.compute_stats_files() # dir is inutile # TODO
|
191
|
+
else
|
192
|
+
raise("Doesnt seem a dir to me, quitting: #{dir}")
|
183
193
|
end
|
194
|
+
end
|
184
195
|
#end
|
185
196
|
end #/iterate_through_file_list_for_disks
|
186
197
|
|
@@ -21,7 +21,7 @@ module Storazzo
|
|
21
21
|
|
22
22
|
public
|
23
23
|
def load # _sample_version
|
24
|
-
|
24
|
+
deb("[RicDiskSampleConfig] Wheew 1! We're NOT destroying the world here. We're actually instancing a second Singleton which is a child of the mother, and this time doing things nicely and Rubily.")
|
25
25
|
# super.load DefaultGemLocationForTests #super.load(DefaultGemLocationForTests, :verbose => true )
|
26
26
|
super(DefaultGemLocationForTests, :verbose => false )
|
27
27
|
end
|