Class: OpenC3::PluginModel
- Includes:
- Api
- Defined in:
- lib/openc3/models/plugin_model.rb
Overview
Represents a OpenC3 plugin that can consist of targets, interfaces, routers microservices and tools. The PluginModel installs all these pieces as well as destroys them all when the plugin is removed.
Constant Summary collapse
- PRIMARY_KEY =
'openc3_plugins'
- RESERVED_VARIABLE_NAMES =
Reserved VARIABLE names. See local_mode.rb: update_local_plugin()
['target_name', 'microservice_name', 'scope']
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
Instance Attribute Summary collapse
-
#needs_dependencies ⇒ Object
Returns the value of attribute needs_dependencies.
-
#plugin_txt_lines ⇒ Object
Returns the value of attribute plugin_txt_lines.
-
#variables ⇒ Object
Returns the value of attribute variables.
Attributes inherited from Model
#name, #plugin, #scope, #updated_at
Class Method Summary collapse
- .all(scope: nil) ⇒ Object
-
.gem_names ⇒ Object
Get list of plugin gem names across all scopes to prevent uninstall of gems from GemModel.
-
.get(name:, scope: nil) ⇒ Object
NOTE: The following three class methods are used by the ModelController and are reimplemented to enable various Model class methods to work.
-
.install_phase1(gem_file_path, existing_variables: nil, existing_plugin_txt_lines: nil, process_existing: false, scope:, validate_only: false) ⇒ Object
Called by the PluginsController to parse the plugin variables Doesn’t actaully create the plugin during the phase.
-
.install_phase2(plugin_hash, scope:, gem_file_path: nil, validate_only: false) ⇒ Object
Called by the PluginsController to create the plugin Because this uses ERB it must be run in a seperate process from the API to prevent corruption and single require problems in the current proces.
- .names(scope: nil) ⇒ Object
Instance Method Summary collapse
- #as_json(*a) ⇒ Object
- #create(update: false, force: false) ⇒ Object
-
#initialize(name:, variables: {}, plugin_txt_lines: [], needs_dependencies: false, updated_at: nil, scope:) ⇒ PluginModel
constructor
A new instance of PluginModel.
-
#restore ⇒ Object
Reinstall.
-
#undeploy ⇒ Object
Undeploy all models associated with this plugin.
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
Methods inherited from Model
#check_disable_erb, #deploy, #destroy, #destroyed?, filter, find_all_by_plugin, from_json, get_all_models, get_model, handle_config, set, store, #update
Constructor Details
#initialize(name:, variables: {}, plugin_txt_lines: [], needs_dependencies: false, updated_at: nil, scope:) ⇒ PluginModel
Returns a new instance of PluginModel.
268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/openc3/models/plugin_model.rb', line 268 def initialize( name:, variables: {}, plugin_txt_lines: [], needs_dependencies: false, updated_at: nil, scope: ) super("#{scope}__#{PRIMARY_KEY}", name: name, updated_at: updated_at, scope: scope) @variables = variables @plugin_txt_lines = plugin_txt_lines @needs_dependencies = ConfigParser.handle_true_false(needs_dependencies) end |
Instance Attribute Details
#needs_dependencies ⇒ Object
Returns the value of attribute needs_dependencies.
55 56 57 |
# File 'lib/openc3/models/plugin_model.rb', line 55 def needs_dependencies @needs_dependencies end |
#plugin_txt_lines ⇒ Object
Returns the value of attribute plugin_txt_lines.
54 55 56 |
# File 'lib/openc3/models/plugin_model.rb', line 54 def plugin_txt_lines @plugin_txt_lines end |
#variables ⇒ Object
Returns the value of attribute variables.
53 54 55 |
# File 'lib/openc3/models/plugin_model.rb', line 53 def variables @variables end |
Class Method Details
.all(scope: nil) ⇒ Object
67 68 69 |
# File 'lib/openc3/models/plugin_model.rb', line 67 def self.all(scope: nil) super("#{scope}__#{PRIMARY_KEY}") end |
.gem_names ⇒ Object
Get list of plugin gem names across all scopes to prevent uninstall of gems from GemModel
331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/openc3/models/plugin_model.rb', line 331 def self.gem_names result = [] scopes = ScopeModel.names scopes.each do |scope| plugin_names = self.names(scope: scope) plugin_names.each do |plugin_name| gem_name = plugin_name.split("__")[0] result << gem_name unless result.include?(gem_name) end end return result.sort end |
.get(name:, scope: nil) ⇒ Object
NOTE: The following three class methods are used by the ModelController and are reimplemented to enable various Model class methods to work
59 60 61 |
# File 'lib/openc3/models/plugin_model.rb', line 59 def self.get(name:, scope: nil) super("#{scope}__#{PRIMARY_KEY}", name: name) end |
.install_phase1(gem_file_path, existing_variables: nil, existing_plugin_txt_lines: nil, process_existing: false, scope:, validate_only: false) ⇒ Object
Called by the PluginsController to parse the plugin variables Doesn’t actaully create the plugin during the phase
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/openc3/models/plugin_model.rb', line 73 def self.install_phase1(gem_file_path, existing_variables: nil, existing_plugin_txt_lines: nil, process_existing: false, scope:, validate_only: false) gem_name = File.basename(gem_file_path).split("__")[0] temp_dir = Dir.mktmpdir tf = nil begin if File.exist?(gem_file_path) # Load gem to internal gem server OpenC3::GemModel.put(gem_file_path, gem_install: false, scope: scope) unless validate_only else gem_file_path = OpenC3::GemModel.get(gem_name) end # Extract gem and process plugin.txt to determine what VARIABLEs need to be filled in pkg = Gem::Package.new(gem_file_path) if existing_plugin_txt_lines and process_existing # This is only used in openc3cli load when everything is known plugin_txt_lines = existing_plugin_txt_lines file_data = existing_plugin_txt_lines.join("\n") tf = Tempfile.new("plugin.txt") tf.write(file_data) tf.close plugin_txt_path = tf.path else # Otherwise we always process the new and return both pkg.extract_files(temp_dir) plugin_txt_path = File.join(temp_dir, 'plugin.txt') plugin_text = File.read(plugin_txt_path) plugin_txt_lines = [] plugin_text.each_line do |line| plugin_txt_lines << line.chomp end end parser = OpenC3::ConfigParser.new("https://openc3.com") # Phase 1 Gather Variables variables = {} parser.parse_file(plugin_txt_path, false, true, false) do |keyword, params| case keyword when 'VARIABLE' usage = "#{keyword} <Variable Name> <Default Value>" parser.verify_num_parameters(2, nil, usage) variable_name = params[0] if RESERVED_VARIABLE_NAMES.include?(variable_name) raise "VARIABLE name '#{variable_name}' is reserved" end value = params[1..-1].join(" ") variables[variable_name] = value if existing_variables && existing_variables.key?(variable_name) variables[variable_name] = existing_variables[variable_name] end end end model = PluginModel.new(name: gem_name, variables: variables, plugin_txt_lines: plugin_txt_lines, scope: scope) result = model.as_json(:allow_nan => true) result['existing_plugin_txt_lines'] = existing_plugin_txt_lines if existing_plugin_txt_lines and not process_existing and existing_plugin_txt_lines != result['plugin_txt_lines'] return result ensure FileUtils.remove_entry(temp_dir) if temp_dir and File.exist?(temp_dir) tf.unlink if tf end end |
.install_phase2(plugin_hash, scope:, gem_file_path: nil, validate_only: false) ⇒ Object
Called by the PluginsController to create the plugin Because this uses ERB it must be run in a seperate process from the API to prevent corruption and single require problems in the current proces
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 198 199 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 |
# File 'lib/openc3/models/plugin_model.rb', line 145 def self.install_phase2(plugin_hash, scope:, gem_file_path: nil, validate_only: false) # Register plugin to aid in uninstall if install fails plugin_hash.delete("existing_plugin_txt_lines") plugin_model = PluginModel.new(**(plugin_hash.transform_keys(&:to_sym)), scope: scope) plugin_model.create unless validate_only temp_dir = Dir.mktmpdir begin tf = nil # Get the gem from local gem server if it hasn't been passed unless gem_file_path gem_name = plugin_hash['name'].split("__")[0] gem_file_path = OpenC3::GemModel.get(gem_name) end # Actually install the gem now (slow) OpenC3::GemModel.install(gem_file_path, scope: scope) unless validate_only # Extract gem contents gem_path = File.join(temp_dir, "gem") FileUtils.mkdir_p(gem_path) pkg = Gem::Package.new(gem_file_path) pkg.extract_files(gem_path) Dir[File.join(gem_path, '**/screens/*.txt')].each do |filename| if File.basename(filename) != File.basename(filename).downcase raise "Invalid screen filename: #{filename}. Screen filenames must be lowercase." end end needs_dependencies = pkg.spec.runtime_dependencies.length > 0 needs_dependencies = true if Dir.exist?(File.join(gem_path, 'lib')) # Handle python requirements.txt if File.exist?(File.join(gem_path, 'requirements.txt')) begin pypi_url = get_setting('pypi_url', scope: scope) rescue # If Redis isn't running try the ENV, then simply pypi.org/simple pypi_url = ENV['PYPI_URL'] pypi_url ||= 'https://pypi.org/simple' end Logger.info "Installing python packages from requirements.txt" puts `pip install --user -i #{pypi_url} -r #{File.join(gem_path, 'requirements.txt')}` needs_dependencies = true end # If needs_dependencies hasn't already been set we need to scan the plugin.txt # to see if they've explicitly set the NEEDS_DEPENDENCIES keyword unless needs_dependencies if plugin_hash['plugin_txt_lines'].join("\n").include?('NEEDS_DEPENDENCIES') needs_dependencies = true end end if needs_dependencies plugin_model.needs_dependencies = true plugin_model.update unless validate_only end # Temporarily add all lib folders from the gem to the end of the load path load_dirs = [] begin Dir.glob("#{gem_path}/**/*").each do |load_dir| if File.directory?(load_dir) and File.basename(load_dir) == 'lib' load_dirs << load_dir $LOAD_PATH << load_dir end end # Process plugin.txt file file_data = plugin_hash['plugin_txt_lines'].join("\n") tf = Tempfile.new("plugin.txt") tf.write(file_data) tf.close plugin_txt_path = tf.path variables = plugin_hash['variables'] variables ||= {} variables['scope'] = scope if File.exist?(plugin_txt_path) parser = OpenC3::ConfigParser.new("https://openc3.com") current_model = nil parser.parse_file(plugin_txt_path, false, true, true, variables) do |keyword, params| case keyword when 'VARIABLE', 'NEEDS_DEPENDENCIES' # Ignore during phase 2 when 'TARGET', 'INTERFACE', 'ROUTER', 'MICROSERVICE', 'TOOL', 'WIDGET' if current_model current_model.create unless validate_only current_model.deploy(gem_path, variables, validate_only: validate_only) current_model = nil end current_model = OpenC3.const_get((keyword.capitalize + 'Model').intern).handle_config(parser, keyword, params, plugin: plugin_model.name, needs_dependencies: needs_dependencies, scope: scope) else if current_model current_model.handle_config(parser, keyword, params) else raise "Invalid keyword '#{keyword}' in plugin.txt" end end end if current_model current_model.create unless validate_only current_model.deploy(gem_path, variables, validate_only: validate_only) current_model = nil end end ensure load_dirs.each do |load_dir| $LOAD_PATH.delete(load_dir) end end rescue => err # Install failed - need to cleanup plugin_model.destroy unless validate_only raise err ensure FileUtils.remove_entry(temp_dir) if temp_dir and File.exist?(temp_dir) tf.unlink if tf end return plugin_model.as_json(:allow_nan => true) end |
.names(scope: nil) ⇒ Object
63 64 65 |
# File 'lib/openc3/models/plugin_model.rb', line 63 def self.names(scope: nil) super("#{scope}__#{PRIMARY_KEY}") end |
Instance Method Details
#as_json(*a) ⇒ Object
287 288 289 290 291 292 293 294 295 |
# File 'lib/openc3/models/plugin_model.rb', line 287 def as_json(*a) { 'name' => @name, 'variables' => @variables, 'plugin_txt_lines' => @plugin_txt_lines, 'needs_dependencies' => @needs_dependencies, 'updated_at' => @updated_at } end |
#create(update: false, force: false) ⇒ Object
282 283 284 285 |
# File 'lib/openc3/models/plugin_model.rb', line 282 def create(update: false, force: false) @name = @name + "__#{Time.now.utc.strftime("%Y%m%d%H%M%S")}" if not update and not @name.index("__") super(update: update, force: force) end |
#restore ⇒ Object
Reinstall
323 324 325 326 327 328 |
# File 'lib/openc3/models/plugin_model.rb', line 323 def restore plugin_hash = self.as_json(:allow_nan => true) plugin_hash['name'] = plugin_hash['name'].split("__")[0] OpenC3::PluginModel.install_phase2(plugin_hash, scope: @scope) @destroyed = false end |
#undeploy ⇒ Object
Undeploy all models associated with this plugin
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/openc3/models/plugin_model.rb', line 298 def undeploy microservice_count = 0 microservices = MicroserviceModel.find_all_by_plugin(plugin: @name, scope: @scope) microservices.each do |name, model_instance| model_instance.destroy microservice_count += 1 end # Wait for the operator to wake up and remove the microservice processes sleep 15 if microservice_count > 0 # Cycle time 5s times 2 plus 5s wait for soft stop and then hard stop # Remove all the other models now that the processes have stopped # Save TargetModel for last as it has the most to cleanup [InterfaceModel, RouterModel, ToolModel, WidgetModel, TargetModel].each do |model| model.find_all_by_plugin(plugin: @name, scope: @scope).each do |name, model_instance| model_instance.destroy end end # Cleanup Redis stuff that might have been left by microservices microservices.each do |name, model_instance| model_instance.cleanup end rescue Exception => error Logger.error("Error undeploying plugin model #{@name} in scope #{@scope} due to #{error}") end |