Module: OpenC3::LocalMode
- Defined in:
- lib/openc3/utilities/local_mode.rb
Constant Summary collapse
- OPENC3_LOCAL_MODE_PATH =
ENV['OPENC3_LOCAL_MODE_PATH'] || "/plugins"
- DEFAULT_PLUGINS =
[ 'openc3-cosmos-tool-admin', 'openc3-cosmos-tool-autonomic', 'openc3-cosmos-tool-calendar', 'openc3-cosmos-tool-cmdsender', 'openc3-cosmos-tool-cmdtlmserver', 'openc3-cosmos-tool-dataextractor', 'openc3-cosmos-tool-dataviewer', 'openc3-cosmos-tool-handbooks', 'openc3-cosmos-tool-limitsmonitor', 'openc3-cosmos-tool-packetviewer', 'openc3-cosmos-tool-scriptrunner', 'openc3-cosmos-tool-tablemanager', 'openc3-cosmos-tool-tlmgrapher', 'openc3-cosmos-tool-tlmviewer', 'openc3-cosmos-enterprise-tool-admin', 'openc3-enterprise-tool-base', 'openc3-tool-base', ]
Class Method Summary collapse
- .analyze_local_mode(plugin_name:, scope:) ⇒ Object
-
.build_local_catalog(scope:) ⇒ Object
Returns equivalent names and sizes to remote catalog => size.
-
.build_remote_catalog(bucket, scope:) ⇒ Object
Returns keys and sizes from remote catalog => size.
- .delete_local(key) ⇒ Object
- .delete_modified(target_name, scope:) ⇒ Object
- .delete_remote(bucket, key) ⇒ Object
-
.local_init ⇒ Object
Install plugins from local plugins folder Can only be used from openc3cli because calls top_level load_plugin.
- .local_target_files(scope:, path_matchers:, include_temp: false) ⇒ Object
- .modified_files(target_name, scope:) ⇒ Object
- .modified_targets(scope:) ⇒ Object
- .open_local_file(path, scope:) ⇒ Object
- .put_target_file(path, io_or_string, scope:) ⇒ Object
- .remove_local_plugin(plugin_name, scope:) ⇒ Object
- .scan_local_mode ⇒ Object
- .scan_plugin_dir(path) ⇒ Object
- .sync_local_to_remote(bucket, key) ⇒ Object
-
.sync_remote_to_local(bucket, key) ⇒ Object
Helper methods.
- .sync_targets_modified ⇒ Object
- .sync_with_bucket(bucket, scope:) ⇒ Object
-
.update_local_plugin(plugin_file_path, plugin_hash, old_plugin_name: nil, scope:) ⇒ Object
If old_plugin_name then this is an online upgrade.
- .update_local_plugin_files(full_folder_path, plugin_file_path, plugin_hash, gem_name) ⇒ Object
- .zip_target(target_name, zip, scope:) ⇒ Object
Class Method Details
.analyze_local_mode(plugin_name:, scope:) ⇒ Object
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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/openc3/utilities/local_mode.rb', line 126 def self.analyze_local_mode(plugin_name:, scope:) if ENV['OPENC3_LOCAL_MODE'] and Dir.exist?(OPENC3_LOCAL_MODE_PATH) # We already know a plugin with this name doesn't exist in the models # Now need to determine if there is a highly likely candidate that has been # updated, so that we don't do an erroneous extra plugin install gem_name = plugin_name.split("__")[0].split('-')[0..-2].join('-') local_plugins = scan_local_mode() scope_plugins = local_plugins[scope] if scope_plugins # Scan models for same gem found_models = {} models = OpenC3::PluginModel.all(scope: scope) models.each do |name, details| model_gem_name = name.split("__")[0].split('-')[0..-2].join('-') found_models[name] = details if gem_name == model_gem_name end # Definitely new install if no found models return nil if found_models.length == 0 # Scan local for same gem and try to match pairs found_local_plugins = {} scope_plugins.each do |folder_path, details| gems = details[:gems] plugin_instance = details[:plugin_instance] next unless plugin_instance next if gems.length != 1 local_gem_name = File.basename(gems[0]).split('-')[0..-2].join('-') if gem_name == local_gem_name # Gems match - Do the names match? data = File.read(plugin_instance) json = JSON.parse(data, :allow_nan => true, :create_additions => true) found = false found_models.each do |name, model_details| if json["name"] == name # Matched pair found = true break end end if found # names match # Remove from the list because we have a matched set # (local plugin_instance name and plugin model name) found_models.delete(json["name"]) else # Found a local plugin with the right gem, but a different name found_local_plugins[folder_path] = details end end end # At this point we only have unmatched plugins in found_models # Not a local mode install if no found local plugins return nil if found_local_plugins.length == 0 # If we have any unmatched models, assume this should match the first # (found_models are existing installed plugins with the same gem but # a different name) found_models.each do |name, model_details| puts "Choosing #{name} for update from local plugins" return model_details end end end return nil end |
.build_local_catalog(scope:) ⇒ Object
Returns equivalent names and sizes to remote catalog => size
446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'lib/openc3/utilities/local_mode.rb', line 446 def self.build_local_catalog(scope:) local_catalog = {} local_folder_path = "#{OPENC3_LOCAL_MODE_PATH}/#{scope}/targets_modified" prefix_length = "#{OPENC3_LOCAL_MODE_PATH}/".length FileUtils.mkdir_p(local_folder_path) Dir.glob(local_folder_path + "/**/*").each do |filename| next if File.directory?(filename) mod_filename = filename[prefix_length..-1] local_catalog[mod_filename] = File.size(filename) end return local_catalog end |
.build_remote_catalog(bucket, scope:) ⇒ Object
Returns keys and sizes from remote catalog => size
461 462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/openc3/utilities/local_mode.rb', line 461 def self.build_remote_catalog(bucket, scope:) remote_catalog = {} prefix = "#{scope}/targets_modified" resp = bucket.list_objects( bucket: ENV['OPENC3_CONFIG_BUCKET'], prefix: prefix, ) resp.each do |item| remote_catalog[item.key] = item.size end return remote_catalog end |
.delete_local(key) ⇒ Object
434 435 436 437 438 |
# File 'lib/openc3/utilities/local_mode.rb', line 434 def self.delete_local(key) local_path = "#{OPENC3_LOCAL_MODE_PATH}/#{key}" File.delete(local_path) if File.exist?(local_path) nil end |
.delete_modified(target_name, scope:) ⇒ Object
360 361 362 363 |
# File 'lib/openc3/utilities/local_mode.rb', line 360 def self.delete_modified(target_name, scope:) full_path = "#{OPENC3_LOCAL_MODE_PATH}/#{scope}/targets_modified/#{target_name}" FileUtils.rm_rf(full_path) end |
.delete_remote(bucket, key) ⇒ Object
440 441 442 |
# File 'lib/openc3/utilities/local_mode.rb', line 440 def self.delete_remote(bucket, key) bucket.delete_object(bucket: ENV['OPENC3_CONFIG_BUCKET'], key: key) end |
.local_init ⇒ Object
Install plugins from local plugins folder Can only be used from openc3cli because calls top_level load_plugin
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 |
# File 'lib/openc3/utilities/local_mode.rb', line 52 def self.local_init if ENV['OPENC3_LOCAL_MODE'] and Dir.exist?(OPENC3_LOCAL_MODE_PATH) puts "Local init running: #{OPENC3_LOCAL_MODE_PATH} exists" Dir.each_child(OPENC3_LOCAL_MODE_PATH).each do |scope_dir| next unless File.directory?("#{OPENC3_LOCAL_MODE_PATH}/#{scope_dir}") puts "Local init found scope: #{scope_dir}" Dir.each_child("#{OPENC3_LOCAL_MODE_PATH}/#{scope_dir}") do |plugin_dir| full_folder_path = "#{OPENC3_LOCAL_MODE_PATH}/#{scope_dir}/#{plugin_dir}" next if plugin_dir == "targets_modified" or not File.directory?(full_folder_path) puts "Local init found plugin_dir: #{full_folder_path}" gems, plugin_instance = scan_plugin_dir(full_folder_path) if gems.length > 1 puts "Local plugin folder contains more than one gem - skipping: #{full_folder_path}" next end if gems.length == 1 and plugin_instance # If one gem file and plugin_instance.json - Install instance load_plugin(gems[0], scope: scope_dir.upcase, plugin_hash_file: plugin_instance) elsif gems.length == 1 # Else If just gem - Install with default settings load_plugin(gems[0], scope: scope_dir.upcase) else puts "Local plugin folder contains no gem file - skipping: #{full_folder_path}" end end end sync_targets_modified() puts "Local init complete" else puts "Local init canceled: Local mode not enabled or #{OPENC3_LOCAL_MODE_PATH} does not exist" end end |
.local_target_files(scope:, path_matchers:, include_temp: false) ⇒ Object
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/openc3/utilities/local_mode.rb', line 393 def self.local_target_files(scope:, path_matchers:, include_temp: false) files = [] local_catalog = build_local_catalog(scope: scope) local_catalog.each do |key, size| split_key = key.split('/') # DEFAULT/targets_modified/__TEMP__/YYYY_MM_DD_HH_MM_SS_mmm_temp.rb # See target_file.rb TEMP_FOLDER if split_key[2] === '__TEMP__' files << split_key[2..-1].join('/') if include_temp next end if path_matchers found = false path_matchers.each do |path| if split_key.include?(path) found = true break end end next unless found end files << split_key[2..-1].join('/') end return files.sort end |
.modified_files(target_name, scope:) ⇒ Object
346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/openc3/utilities/local_mode.rb', line 346 def self.modified_files(target_name, scope:) modified = [] local_catalog = build_local_catalog(scope: scope) local_catalog.each do |key, size| split_key = key.split('/') # scope/targets_modified/target_name/* local_target_name = split_key[2] if target_name == local_target_name modified << split_key[3..-1].join('/') end end # Paths do not include target name return modified.sort end |
.modified_targets(scope:) ⇒ Object
333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/openc3/utilities/local_mode.rb', line 333 def self.modified_targets(scope:) targets = {} local_catalog = build_local_catalog(scope: scope) local_catalog.each do |key, size| split_key = key.split('/') # scope/targets_modified/target_name/* target_name = split_key[2] if target_name targets[target_name] = true end end return targets.keys.sort end |
.open_local_file(path, scope:) ⇒ Object
386 387 388 389 390 391 |
# File 'lib/openc3/utilities/local_mode.rb', line 386 def self.open_local_file(path, scope:) full_path = "#{OPENC3_LOCAL_MODE_PATH}/#{scope}/targets_modified/#{path}" return File.open(full_path, 'rb') rescue Errno::ENOENT nil end |
.put_target_file(path, io_or_string, scope:) ⇒ Object
373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/openc3/utilities/local_mode.rb', line 373 def self.put_target_file(path, io_or_string, scope:) full_folder_path = "#{OPENC3_LOCAL_MODE_PATH}/#{path}" FileUtils.mkdir_p(File.dirname(full_folder_path)) File.open(full_folder_path, 'wb') do |file| if String === io_or_string data = io_or_string else data = io_or_string.read end file.write(data) end end |
.remove_local_plugin(plugin_name, scope:) ⇒ Object
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/openc3/utilities/local_mode.rb', line 298 def self.remove_local_plugin(plugin_name, scope:) local_plugins = scan_local_mode() scope_local_plugins = local_plugins[scope] if scope_local_plugins scope_local_plugins.each do |full_folder_path, details| gems = details[:gems] plugin_instance = details[:plugin_instance] if gems.length == 1 and plugin_instance data = File.read(plugin_instance) json = JSON.parse(data, :allow_nan => true, :create_additions => true) instance_name = json['name'] if plugin_name == instance_name puts "Removing local plugin files: #{full_folder_path}" File.delete(gems[0]) File.delete(plugin_instance) if Dir.empty?(File.dirname(gems[0])) FileUtils.rm_rf(File.dirname(gems[0])) end break end end end end end |
.scan_local_mode ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/openc3/utilities/local_mode.rb', line 105 def self.scan_local_mode if ENV['OPENC3_LOCAL_MODE'] and Dir.exist?(OPENC3_LOCAL_MODE_PATH) local_plugins = {} Dir.each_child(OPENC3_LOCAL_MODE_PATH) do |scope_dir| full_scope_dir = "#{OPENC3_LOCAL_MODE_PATH}/#{scope_dir}" next unless File.directory?(full_scope_dir) local_plugins[scope_dir] ||= {} Dir.each_child("#{OPENC3_LOCAL_MODE_PATH}/#{scope_dir}") do |plugin_dir| full_folder_path = "#{OPENC3_LOCAL_MODE_PATH}/#{scope_dir}/#{plugin_dir}" next if plugin_dir == "targets_modified" or not File.directory?(full_folder_path) gems, plugin_instance = scan_plugin_dir(full_folder_path) local_plugins[scope_dir][full_folder_path] = {gems: gems, plugin_instance: plugin_instance} end end return local_plugins end return {} end |
.scan_plugin_dir(path) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/openc3/utilities/local_mode.rb', line 87 def self.scan_plugin_dir(path) gems = [] plugin_instance = nil Dir.each_child(path) do |filename| full_path = "#{path}/#{filename}" if not File.directory?(full_path) if File.extname(filename) == '.gem' gems << full_path elsif filename == 'plugin_instance.json' plugin_instance = full_path end end end return gems, plugin_instance end |
.sync_local_to_remote(bucket, key) ⇒ Object
427 428 429 430 431 432 |
# File 'lib/openc3/utilities/local_mode.rb', line 427 def self.sync_local_to_remote(bucket, key) local_path = "#{OPENC3_LOCAL_MODE_PATH}/#{key}" File.open(local_path, 'rb') do |read_file| bucket.put_object(bucket: ENV['OPENC3_CONFIG_BUCKET'], key: key, body: read_file) end end |
.sync_remote_to_local(bucket, key) ⇒ Object
Helper methods
421 422 423 424 425 |
# File 'lib/openc3/utilities/local_mode.rb', line 421 def self.sync_remote_to_local(bucket, key) local_path = "#{OPENC3_LOCAL_MODE_PATH}/#{key}" FileUtils.mkdir_p(File.dirname(local_path)) bucket.get_object(bucket: ENV['OPENC3_CONFIG_BUCKET'], key: key, path: local_path) end |
.sync_targets_modified ⇒ Object
323 324 325 326 327 328 329 330 331 |
# File 'lib/openc3/utilities/local_mode.rb', line 323 def self.sync_targets_modified if ENV['OPENC3_LOCAL_MODE'] and Dir.exist?(OPENC3_LOCAL_MODE_PATH) bucket = Bucket.getClient() scopes = ScopeModel.names() scopes.each do |scope| sync_with_bucket(bucket, scope: scope) end end end |
.sync_with_bucket(bucket, scope:) ⇒ Object
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 |
# File 'lib/openc3/utilities/local_mode.rb', line 474 def self.sync_with_bucket(bucket, scope:) # Build catalogs local_catalog = build_local_catalog(scope: scope) remote_catalog = build_remote_catalog(bucket, scope: scope) # Find and Handle Differences local_catalog.each do |key, size| remote_size = remote_catalog[key] if remote_size # Both files exist if ENV['OPENC3_LOCAL_MODE_SECONDARY'] sync_remote_to_local(bucket, key) if size != remote_size or ENV['OPENC3_LOCAL_MODE_FORCE_SYNC'] else sync_local_to_remote(bucket, key) if size != remote_size or ENV['OPENC3_LOCAL_MODE_FORCE_SYNC'] end else # Remote is missing local file if ENV['OPENC3_LOCAL_MODE_SECONDARY'] and ENV['OPENC3_LOCAL_MODE_SYNC_REMOVE'] delete_local(key) else # Go ahead and copy up to get in sync sync_local_to_remote(bucket, key) end end end remote_catalog.each do |key, size| local_size = local_catalog[key] if local_size # Both files exist - Handled earlier else # Local is missing remote file if not ENV['OPENC3_LOCAL_MODE_SECONDARY'] and ENV['OPENC3_LOCAL_MODE_SYNC_REMOVE'] delete_remote(bucket, key) else # Go ahead and copy down to get in sync sync_remote_to_local(bucket, key) end end end end |
.update_local_plugin(plugin_file_path, plugin_hash, old_plugin_name: nil, scope:) ⇒ Object
If old_plugin_name then this is an online upgrade
200 201 202 203 204 205 206 207 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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/openc3/utilities/local_mode.rb', line 200 def self.update_local_plugin(plugin_file_path, plugin_hash, old_plugin_name: nil, scope:) if ENV['OPENC3_LOCAL_MODE'] and Dir.exist?(OPENC3_LOCAL_MODE_PATH) variables = plugin_hash['variables'] if variables PluginModel::RESERVED_VARIABLE_NAMES.each do |name| variables.delete(name) end end if plugin_file_path =~ Regexp.new("^#{OPENC3_LOCAL_MODE_PATH}/#{scope}/") # From local init - Always just update the exact one File.open(File.join(File.dirname(plugin_file_path), 'plugin_instance.json'), 'wb') do |file| file.write(JSON.pretty_generate(plugin_hash, :allow_nan => true)) end else # From online install / update # Or init install of container plugin # Try to find an existing local folder for this plugin found = false gem_name = File.basename(plugin_file_path).split('-')[0..-2].join('-') FileUtils.mkdir_p("#{OPENC3_LOCAL_MODE_PATH}/#{scope}") Dir.each_child("#{OPENC3_LOCAL_MODE_PATH}/#{scope}") do |plugin_dir| full_folder_path = "#{OPENC3_LOCAL_MODE_PATH}/#{scope}/#{plugin_dir}" next if plugin_dir == "targets_modified" or not File.directory?(full_folder_path) gems, plugin_instance = scan_plugin_dir(full_folder_path) next if gems.length > 1 if gems.length == 1 found_gem_name = File.basename(gems[0]).split('-')[0..-2].join('-') if found_gem_name == gem_name # Same gem at least - Now see if same instance if plugin_instance if old_plugin_name # And we're updating a plugin data = File.read(plugin_instance) json = JSON.parse(data, :allow_nan => true, :create_additions => true) if json["name"] == old_plugin_name # Found plugin to update found = true update_local_plugin_files(full_folder_path, plugin_file_path, plugin_hash, gem_name) end else # New install of same plugin - Leave it alone end else # No exiting instance.json, but we found the same gem # This shouldn't happen without users using this wrong # We will update found = true update_local_plugin_files(full_folder_path, plugin_file_path, plugin_hash, gem_name) end end end end unless found # Then we will make a local version # Create a folder for this plugin and add gem and plugin_instance.json folder_name = gem_name count = 1 while File.exist?("#{OPENC3_LOCAL_MODE_PATH}/#{scope}/#{folder_name}") folder_name = gem_name + "-" + count.to_s count += 1 end full_folder_path = "#{OPENC3_LOCAL_MODE_PATH}/#{scope}/#{folder_name}" update_local_plugin_files(full_folder_path, plugin_file_path, plugin_hash, gem_name) end end end end |
.update_local_plugin_files(full_folder_path, plugin_file_path, plugin_hash, gem_name) ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/openc3/utilities/local_mode.rb', line 273 def self.update_local_plugin_files(full_folder_path, plugin_file_path, plugin_hash, gem_name) return if DEFAULT_PLUGINS.include?(gem_name) puts "Updating local plugin files: #{full_folder_path}" FileUtils.mkdir_p(full_folder_path) gems, plugin_instance = scan_plugin_dir(full_folder_path) gems.each do |gem| File.delete(gem) end temp_dir = Dir.mktmpdir begin unless File.exists?(plugin_file_path) plugin_file_path = OpenC3::GemModel.get(plugin_file_path) end File.open(File.join(full_folder_path, File.basename(plugin_file_path)), 'wb') do |file| data = File.read(plugin_file_path) file.write(data) end File.open(File.join(full_folder_path, 'plugin_instance.json'), 'wb') do |file| file.write(JSON.pretty_generate(plugin_hash, :allow_nan => true)) end ensure FileUtils.remove_entry(temp_dir) if temp_dir and File.exist?(temp_dir) end end |
.zip_target(target_name, zip, scope:) ⇒ Object
365 366 367 368 369 370 371 |
# File 'lib/openc3/utilities/local_mode.rb', line 365 def self.zip_target(target_name, zip, scope:) modified = modified_files(target_name, scope: scope) modified.each do |file_path| full_path = "#{OPENC3_LOCAL_MODE_PATH}/#{scope}/targets_modified/#{target_name}/#{file_path}" zip.add(file_path, full_path) end end |