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

_cal_to_epoch, _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, commit_timeline_activity, config_tool_names, connect_interface, connect_router, count_timeline_activities, create_timeline, create_timeline_activity, delete_config, delete_limits_set, delete_timeline, delete_timeline_activity, 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_tlm, get_all_tlm_item_names, 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_timeline, get_timeline_activities, get_timeline_activity, get_tlm, get_tlm_available, get_tlm_buffer, get_tlm_cnt, get_tlm_cnts, get_tlm_packet, get_tlm_values, inject_tlm, interface_cmd, interface_details, interface_protocol_cmd, interface_target_disable, interface_target_enable, limits_enabled?, list_configs, list_settings, list_timelines, load_config, map_target_to_interface, map_target_to_router, normalize_tlm, offline_access_needed, override_tlm, router_cmd, router_details, router_protocol_cmd, router_target_disable, router_target_enable, save_config, send_raw, set_limits, set_limits_set, set_offline_access, set_setting, set_state_color, set_timeline_color, set_timeline_execute, 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_with_units, unmap_target_from_interface, unmap_target_from_router, update_news, update_plugin_store, update_timeline_activity

Methods included from CmdLog

#_build_cmd_output_string

Class Method Details

.destroy(name, log_and_raise_needed_errors: true) ⇒ Object



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

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 => e
      Logger.error "Gem file #{name} error uninstalling\n#{e.formatted}"
      raise e
    end
  end
end

.destroy_all_other_versions(name) ⇒ Object



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

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



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

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

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

.install(name_or_path, scope:) ⇒ Object



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

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.fetch('RUBYGEMS_URL', RubygemsUrl::DEFAULT)
  end
  # The url comes from a user-writable setting or ENV so validate it before
  # using it as a Gem source
  rubygems_url = RubygemsUrl.validate(rubygems_url) if rubygems_url
  Gem.sources = [rubygems_url] if rubygems_url
  Gem.done_installing_hooks.clear
  begin
    # Look for local gems only first, this avoids lengthy timeouts when checking rubygems in airgap env
    Gem.install(gem_file_path, "> 0.pre", build_args: ['--no-document, --without development,test'], prerelease: true, domain: :local)
  rescue Gem::Exception => e
    # If there is a failure look for both local and remote gems
    Gem.install(gem_file_path, "> 0.pre", build_args: ['--no-document, --without development,test'], prerelease: true, domain: :both)
  end
rescue => e
  message = "Gem file #{gem_file_path} error installing to #{ENV.fetch('GEM_HOME', nil)}\n#{e.formatted}"
  Logger.error message
  raise e
end

.namesObject



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

def self.names
  if Dir.exist?("#{ENV.fetch('GEM_HOME', nil)}/gems")
    result = Pathname.new("#{ENV.fetch('GEM_HOME', nil)}/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



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

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.fetch('GEM_HOME', nil)}/cosmoscache") unless Dir.exist?("#{ENV.fetch('GEM_HOME', nil)}/cosmoscache")
    FileUtils.cp(gem_file_path, "#{ENV.fetch('GEM_HOME', nil)}/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