Class: Storazzo::Storazzo::RicDiskConfig

Inherits:
Object
  • Object
show all
Includes:
Singleton, Colors, Common
Defined in:
lib/storazzo/ric_disk_config.rb

Direct Known Subclasses

RicDiskSampleConfig

Constant Summary collapse

DefaultConfigLocation =
File.expand_path '~/.storazzo.yaml'
DEFAULT_CONFIG_LOCATIONS =
[
  File.expand_path('~/.storazzo.yaml'), # HOME
  File.expand_path('./.storazzo.yaml') # LOCAL DIR
].freeze
DefaultGemLocationForTests =
"#{File.expand_path('../..', __dir__)}/etc/storazzo_config.sample.yaml"

Constants included from Colors

Colors::PREPEND_ME

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Colors

#azure, #blue, #deb2, #gray, #green, #orange, #pgreen, #pred, #purple, #pwhite, #pyellow, #red, #white, #yellow

Methods included from Common

#bug, #deb, #err, #fatal, #if_deb?, #linux?, #mac?, #ppp, #pverbose, #warn

Instance Attribute Details

#configObject

Returns the value of attribute config.



44
45
46
# File 'lib/storazzo/ric_disk_config.rb', line 44

def config
  @config
end

#config_fileObject

Returns the value of attribute config_file.



44
45
46
# File 'lib/storazzo/ric_disk_config.rb', line 44

def config_file
  @config_file
end

#load_calledObject

Returns the value of attribute load_called.



44
45
46
# File 'lib/storazzo/ric_disk_config.rb', line 44

def load_called
  @load_called
end

#project_idObject

doesnt work :/ alias :project_id, :config_project_id



134
135
136
# File 'lib/storazzo/ric_disk_config.rb', line 134

def project_id
  @project_id
end

Class Method Details

.gem_default_config_pathObject



184
185
186
# File 'lib/storazzo/ric_disk_config.rb', line 184

def self.gem_default_config_path
  "#{Storazzo.root}/etc/storazzo_config.sample.yaml"
end

.get_configObject



264
265
266
267
# File 'lib/storazzo/ric_disk_config.rb', line 264

def self.get_config
  instance.load unless instance.load_called
  instance.get_config
end

.safe_instanceObject



257
258
259
260
261
262
# File 'lib/storazzo/ric_disk_config.rb', line 257

def self.safe_instance
  puts 'This is a safe instance :)'
  my_config = instance
  my_config.load
  my_config
end

Instance Method Details

#already_loaded?Boolean

Returns:

  • (Boolean)


148
149
150
151
# File 'lib/storazzo/ric_disk_config.rb', line 148

def already_loaded?
  # return
  load_called == true
end

#config_default_folderObject



124
125
126
127
# File 'lib/storazzo/ric_disk_config.rb', line 124

def config_default_folder
  # self.
  @config['Config']['DefaultFolder'] # rescue "Unknown config_default_folder: #{$!}"
end

#config_hashObject



253
254
255
# File 'lib/storazzo/ric_disk_config.rb', line 253

def config_hash
  config['Config']
end

#config_project_idObject



129
130
131
# File 'lib/storazzo/ric_disk_config.rb', line 129

def config_project_id
  @config['Config']['Backends']['GoogleCloudStorage']['ProjectId']
end

#config_verObject



117
118
119
120
121
122
# File 'lib/storazzo/ric_disk_config.rb', line 117

def config_ver
  raise 'I cant compute Version since I cant compute @config. Are you sure you didnt instance this Singleton without calling load?' if @config.nil?

  @config['apiVersion'] # rescue :StillUnknown
  # config['ConfigVersion']
end

#gcs_enabled?Boolean

This should return true if and only if the user has configured the YAML to use GCS. For now we can just say TRUE since gsutil ls returns without project_id. However, we might have a flag enabled in storazzo config in next versions.

Returns:

  • (Boolean)


141
142
143
144
145
146
# File 'lib/storazzo/ric_disk_config.rb', line 141

def gcs_enabled?
  # true
  defined?(get_config['Config']['Backends']['GoogleCloudStorage'])
  # get_config['Config']['Backends']['GoogleCloudStorage DOESNT EXIST']
  # defined? Backends:     GoogleCloudStorage
end

#get_bucket_pathsObject



210
211
212
213
214
# File 'lib/storazzo/ric_disk_config.rb', line 210

def get_bucket_paths
  get_config['Config']['Backends']['GoogleCloudStorage']['BucketPaths'].map do |complex_gcs_struct|
    complex_gcs_struct['path']
  end
end

#get_config(opts = {}) ⇒ Object



178
179
180
181
182
# File 'lib/storazzo/ric_disk_config.rb', line 178

def get_config(opts = {})
  return load(opts) if @config.nil?

  @config
end

#get_local_foldersObject

returns all folders from file which are Directories This method is FLAKY! Sometimes gives error.

LocalFolderTest#test_show_all_shouldnt_fail_and_should_return_a_non_empty_array:

TypeError: no implicit conversion of Hash into String

/Users/ricc/git/storazzo/lib/storazzo/ric_disk_config.rb:38:in `expand_path'
/Users/ricc/git/storazzo/lib/storazzo/ric_disk_config.rb:38:in `block in load'
/Users/ricc/git/storazzo/lib/storazzo/ric_disk_config.rb:37:in `each'
/Users/ricc/git/storazzo/lib/storazzo/ric_disk_config.rb:37:in `load'
/Users/ricc/git/storazzo/lib/storazzo/ric_disk_config.rb:83:in `get_config'
/Users/ricc/git/storazzo/lib/storazzo/ric_disk_config.rb:95:in `get_local_folders'


200
201
202
203
204
205
206
207
208
# File 'lib/storazzo/ric_disk_config.rb', line 200

def get_local_folders
  config = get_config
  # puts config['Config']['AdditionalMountDirs']
  config['Config']['AdditionalMountDirs'].map do |folder|
    File.expand_path(folder)
  rescue StandardError
    folder
  end.filter { |f| File.directory?(f) }
end

#iterate_through_file_list_for_disks(files_list = [], opts = {}) ⇒ Object

UGLY CODE, copipasted from binary for ARGV, ex autosbrodola



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
246
247
248
249
250
251
# File 'lib/storazzo/ric_disk_config.rb', line 217

def iterate_through_file_list_for_disks(files_list = [], opts = {})
  verbose = opts.fetch :verbose, false
  raise "[iterate_through_file_list_for_disks] Wrong input, I need an array here: #{files_list} " unless files_list.is_a?(Array)

  # I decided i wont accept an emopty list, this is not how you use the gem, you lazy XXX!
  # if files_list == [] # or files_list.nil?  # empty -> ALL
  #     deb "iterate_through_file_list_for_disks(): no args provided"
  #     dirs = RicDisk.find_active_dirs()
  #     puts "DEB find_active_dirs: #{green dirs}"
  #     dirs.each {|dir|
  #         RicDisk.write_config_yaml_to_disk(dir)
  #         RicDisk.calculate_stats_files(dir) # dir is inutile
  #     } # TODO refactor in option sbrodola_afterwards=true. :)
  # else
  pverbose verbose,
           'iterate_through_file_list_for_disks(): I consider files_list as a list of directories to parse :)'

  # dirs = RicDisk.find_active_dirs()
  files_list.each do |dir|
    dir = File.expand_path(dir)
    if File.directory?(dir)
      # if dirs.include?(dir)
      pverbose verbose, "iterate_through_file_list_for_disks() Legit dir: #{green dir}"
      rd = RicDisk.new(Storazzo::Media::AbstractRicDisk.DirFactory(dir))
      pverbose verbose, "RicDisk from Factory (woohoo): #{rd}"
      rd.write_config_yaml_to_disk(dir)
      # RicDisk.write_config_yaml_to_disk(dir)
      # RicDisk.calculate_stats_files (CLASS) => will become OBJECT compute_stats_files
      rd.compute_stats_files # dir is inutile # TODO
    else
      raise("Doesnt seem a dir to me, quitting: #{dir}")
    end
  end
  # end
end

#load(config_path = nil, opts = {}) ⇒ Object

Load from the first valid config.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
# File 'lib/storazzo/ric_disk_config.rb', line 47

def load(config_path = nil, opts = {})
  verbose = opts.fetch :verbose, false

  if already_loaded? # and not self.config.nil?
    pverbose verbose, "[#{self.class}] Config.Load: already loaded"
    return config
  end
  pverbose verbose, 'Storazzo::RicDiskConfig.load(): BEGIN'
  # trying default location
  raise 'DefaultConfigLocation is not a string' unless DefaultConfigLocation.is_a?(String)

  possible_locations = DEFAULT_CONFIG_LOCATIONS #  [ default_config_locations .. , "./.storazzo.yaml"]
  deb "[Config.load] Possible Locations: #{possible_locations}"
  if config_path.is_a?(String)
    # possible_locations = [config_path] + possible_locations # .append()
    possible_locations.unshift(config_path) # append to front
    # OR: possible_locations.instert(0, config_path)
    pverbose verbose, "[LOAD] possible_locations: #{possible_locations}"
  end
  pverbose verbose, "Searching these paths in order: #{possible_locations}"
  # bug "This is not always an array of sTRINGS."
  raise 'possible_locations is not an array' unless possible_locations.is_a?(Array)

  possible_locations.each do |possible_path|
    # ASSERT is a string
    raise 'possible_path is not a string' unless possible_path.is_a?(String)

    deb "before buggy expand_path paz: '#{possible_path}''"
    paz = begin
      File.expand_path(possible_path)
    rescue StandardError
      possible_path
    end
    raise "Not a string: #{paz}" unless paz.is_a?(String)

    next unless File.exist?(paz)

    @config_file = paz
    @config = YAML.safe_load(File.read(paz)) # YAML.load(File.read("file_path"))

    unless begin
      @config['kind'] == 'StorazzoConfig'
    rescue StandardError
      false
    end
      puts white "RicDiskConfig.load(): Sorry this is wrong Config File. Kind=#{begin
        @config['kind']
      rescue StandardError
        $ERROR_INFO
      end}"
      next
    end
    #
    # pp @config if verbose
    config_ver = @config['apiVersion']
    # puts @config[:ConfigVersion]
    deb("OK. Storazzo::RicDiskConfig v'#{config_ver}' parsed correctly")
    @load_called = true
    return config
  end
  @load_called = true
  # only get here if nothing is found
  raise "No config found across these locations: #{possible_locations}. Consider copying and editing: #{RicDiskConfig.gem_default_config_path}"
end

#load_sample_versionObject

Obsolete, call another class instead.



113
114
115
# File 'lib/storazzo/ric_disk_config.rb', line 113

def load_sample_version
  raise 'DEPRECATED! USE SampleRicDiskConfig.load() instead!'
end

#to_sObject



153
154
155
156
157
158
159
160
161
162
# File 'lib/storazzo/ric_disk_config.rb', line 153

def to_s
  size = begin
    File.size(@config_file)
  rescue StandardError
    -1
  end
  # puts yellow "DEB: #{@config["apiVersion"]}"
  # "RicDiskConfig(ver=#{config_ver}, file=#{config_file}), #{white(size)} bytes" # - config_default_folder=#{self.config_default_folder}"
  "POLY_#{self.class}_(ver=#{config_ver}, file=#{config_file}), #{white(size)} bytes" # - config_default_folder=#{self.config_default_folder}"
end

#to_verbose_sObject



164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/storazzo/ric_disk_config.rb', line 164

def to_verbose_s
  h = {}
  h[:description] =
    'This is a Verbose Hash describing a RicDiskConfig or its child RicDiskSampleConfig to understand why it keeps failing..'
  h[:to_s] = to_s
  h[:class] = self.class
  h[:file] = __FILE__
  h[:id] = object_id
  h[:get_bucket_paths] = get_bucket_paths
  h[:get_local_folders] = get_local_folders
  h[:config_project_id] = config_project_id
  h
end