Class: OpenC3::GemModel

Inherits:
Object show all
Extended by:
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, _extract_target_command_names, _extract_target_command_parameter_names, _extract_target_packet_item_names, _extract_target_packet_names, _get_and_set_cmd, _get_item, _limits_group, _set_tlm_process_args, _tlm_process_args, _validate_tlm_type, build_cmd, 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_cmd, disable_limits, disable_limits_group, disconnect_interface, disconnect_router, enable_cmd, enable_limits, enable_limits_group, get_all_cmd_names, get_all_cmds, get_all_interface_info, get_all_router_info, get_all_settings, get_all_target_info, get_all_tlm, get_all_tlm_names, get_cmd, get_cmd_buffer, get_cmd_cnt, get_cmd_cnts, get_cmd_hazardous, get_cmd_time, get_cmd_value, 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_param, get_router, get_router_names, get_setting, get_settings, get_target, get_target_interfaces, get_target_names, get_tlm, 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, log_and_raise_needed_errors: true) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/openc3/models/gem_model.rb', line 105

def self.destroy(name, log_and_raise_needed_errors: true)
  gem_name, version = self.extract_name_and_version(name)
  plugin_gem_names = PluginModel.gem_names
  if plugin_gem_names.include?(name)
    if log_and_raise_needed_errors
      message = "Gem file #{name} can't be uninstalled because needed by installed plugin"
      Logger.error message
      raise message
    end
  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

.destroy_all_other_versions(name) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/openc3/models/gem_model.rb', line 131

def self.destroy_all_other_versions(name)
  keep_gem_name, keep_gem_version = GemModel.extract_name_and_version(name)
  GemModel.names.each do |gem_full_name|
    gem_name, gem_version = GemModel.extract_name_and_version(gem_full_name)
    if gem_name == keep_gem_name and gem_version != keep_gem_version
      GemModel.destroy(gem_full_name, log_and_raise_needed_errors: false)
    end
  end
end

.extract_name_and_version(name) ⇒ Object



124
125
126
127
128
129
# File 'lib/openc3/models/gem_model.rb', line 124

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



50
51
52
53
54
55
56
# File 'lib/openc3/models/gem_model.rb', line 50

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

.install(name_or_path, scope:) ⇒ Object



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

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
45
46
47
48
# File 'lib/openc3/models/gem_model.rb', line 41

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

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/openc3/models/gem_model.rb', line 58

def self.put(gem_file_path, gem_install: true, scope:)
  if File.file?(gem_file_path)
    gem_filename = File.basename(gem_file_path)
    # Put into cosmoscache folder that we control
    FileUtils.mkdir_p("#{ENV['GEM_HOME']}/cosmoscache") unless Dir.exist?("#{ENV['GEM_HOME']}/cosmoscache")
    FileUtils.cp(gem_file_path, "#{ENV['GEM_HOME']}/cosmoscache/#{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