Class: OpenC3::GemModel

Inherits:
Object show all
Includes:
Api
Defined in:
lib/openc3/models/gem_model.rb

Overview

This class acts like a Model but doesn’t inherit from Model because it doesn’t actual interact with the Store (Redis). Instead we implement names, get, put and destroy to allow interaction with gem files from the PluginModel and the GemsController.

Constant Summary

Constants included from Api

Api::DELAY_METRICS, Api::DURATION_METRICS, Api::SUBSCRIPTION_DELIMITER, Api::SUM_METRICS

Constants included from ApiShared

ApiShared::DEFAULT_TLM_POLLING_RATE

Constants included from Extract

Extract::SCANNING_REGULAR_EXPRESSION

Class Method Summary collapse

Methods included from Api

#_build_cmd_output_string, #_cmd_implementation, #_get_item, #_limits_group, #_set_tlm_process_args, #_tlm_process_args, #_validate_tlm_type, #build_command, #cmd, #cmd_no_checks, #cmd_no_hazardous_check, #cmd_no_range_check, #cmd_raw, #cmd_raw_no_checks, #cmd_raw_no_hazardous_check, #cmd_raw_no_range_check, #config_tool_names, #connect_interface, #connect_router, #delete_config, #disable_limits, #disable_limits_group, #disconnect_interface, #disconnect_router, #enable_limits, #enable_limits_group, #get_all_command_names, #get_all_commands, #get_all_interface_info, #get_all_router_info, #get_all_settings, #get_all_target_info, #get_all_telemetry, #get_all_telemetry_names, #get_cmd_buffer, #get_cmd_cnt, #get_cmd_cnts, #get_cmd_hazardous, #get_cmd_time, #get_cmd_value, #get_command, #get_interface, #get_interface_names, #get_item, #get_limits, #get_limits_events, #get_limits_groups, #get_limits_set, #get_limits_sets, #get_metrics, #get_out_of_limits, #get_overall_limits_state, #get_overrides, #get_packet_derived_items, #get_packets, #get_parameter, #get_router, #get_router_names, #get_setting, #get_settings, #get_target, #get_target_interfaces, #get_target_names, #get_telemetry, #get_tlm_buffer, #get_tlm_cnt, #get_tlm_cnts, #get_tlm_packet, #get_tlm_values, #inject_tlm, #interface_cmd, #interface_protocol_cmd, #limits_enabled?, #list_configs, #list_settings, #load_config, #map_target_to_interface, #normalize_tlm, #offline_access_needed, #override_tlm, #router_cmd, #router_protocol_cmd, #save_config, #send_raw, #set_limits, #set_limits_set, #set_offline_access, #set_setting, #set_tlm, #start_raw_logging_interface, #start_raw_logging_router, #stash_all, #stash_delete, #stash_get, #stash_keys, #stash_set, #stop_raw_logging_interface, #stop_raw_logging_router, #subscribe_packets, #tlm, #tlm_formatted, #tlm_raw, #tlm_variable, #tlm_with_units

Class Method Details

.destroy(name) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/openc3/models/gem_model.rb', line 98

def self.destroy(name)
  gem_name, version = self.extract_name_and_version(name)
  plugin_gem_names = PluginModel.gem_names
  if plugin_gem_names.include?(name)
    message = "Gem file #{name} can't be uninstalled because needed by installed plugin"
    Logger.error message
    raise message
  else
    begin
      Gem::Uninstaller.new(gem_name, {:version => version, :force => true}).uninstall
    rescue => err
      Logger.error "Gem file #{name} error uninstalling\n#{err.formatted}"
      raise err
    end
  end
end

.extract_name_and_version(name) ⇒ Object



115
116
117
118
119
120
# File 'lib/openc3/models/gem_model.rb', line 115

def self.extract_name_and_version(name)
  split_name = name.split('-')
  gem_name = split_name[0..-2].join('-')
  version = File.basename(split_name[-1], '.gem')
  return gem_name, version
end

.get(name) ⇒ Object



46
47
48
49
50
# File 'lib/openc3/models/gem_model.rb', line 46

def self.get(name)
  path = "#{ENV['GEM_HOME']}/cache/#{name}"
  return path if File.exist?(path)
  raise "Gem #{name} not found"
end

.install(name_or_path, scope:) ⇒ Object



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
# File 'lib/openc3/models/gem_model.rb', line 70

def self.install(name_or_path, scope:)
  if File.exist?(name_or_path)
    gem_file_path = name_or_path
  else
    gem_file_path = get(name_or_path)
  end
  begin
    rubygems_url = get_setting('rubygems_url', scope: scope)
  rescue
    # If Redis isn't running try the ENV, then simply rubygems.org
    rubygems_url = ENV['RUBYGEMS_URL']
    rubygems_url ||= 'https://rubygems.org'
  end
  Gem.sources = [rubygems_url] if rubygems_url
  Gem.done_installing_hooks.clear
  begin
    # Look for local gems only first, this avoids lengthly timeouts when checking rubygems in airgap env
    Gem.install(gem_file_path, "> 0.pre", build_args: ['--no-document'], prerelease: true, domain: :local)
  rescue Gem::Exception => err
    # If there is a failure look for both local and remote gems
    Gem.install(gem_file_path, "> 0.pre", build_args: ['--no-document'], prerelease: true, domain: :both)
  end
rescue => err
  message = "Gem file #{gem_file_path} error installing to #{ENV['GEM_HOME']}\n#{err.formatted}"
  Logger.error message
  raise err
end

.namesObject



41
42
43
44
# File 'lib/openc3/models/gem_model.rb', line 41

def self.names
  result = Pathname.new("#{ENV['GEM_HOME']}/gems").children.select { |c| c.directory? }.collect { |p| File.basename(p) + '.gem' }
  return result.sort
end

.put(gem_file_path, gem_install: true, scope:) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/openc3/models/gem_model.rb', line 52

def self.put(gem_file_path, gem_install: true, scope:)
  if File.file?(gem_file_path)
    gem_filename = File.basename(gem_file_path)
    FileUtils.mkdir_p("#{ENV['GEM_HOME']}/cache") unless Dir.exist?("#{ENV['GEM_HOME']}/cache")
    FileUtils.cp(gem_file_path, "#{ENV['GEM_HOME']}/cache/#{File.basename(gem_file_path)}")
    if gem_install
      Logger.info "Installing gem: #{gem_filename}"
      result = OpenC3::ProcessManager.instance.spawn(["ruby", "/openc3/bin/openc3cli", "geminstall", gem_filename, scope], "package_install", gem_filename, Time.now + 3600.0, scope: scope)
      return result.name
    end
  else
    message = "Gem file #{gem_file_path} does not exist!"
    Logger.error message
    raise message
  end
  return nil
end