Method: OpenC3::PluginModel.install_phase2
- Defined in:
- lib/openc3/models/plugin_model.rb
.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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# 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 # Attempt to remove all older versions of this same plugin before install to prevent version conflicts # Especially on downgrades # Leave the same version if it already exists OpenC3::GemModel.destroy_all_other_versions(File.basename(gem_file_path)) # 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) if pypi_url pypi_url += '/simple' end rescue => e Logger.error("Failed to retrieve pypi_url: #{e.formatted}") ensure if pypi_url.nil? # If Redis isn't running try the ENV, then simply pypi.org/simple pypi_url = ENV['PYPI_URL'] if pypi_url pypi_url += '/simple' end pypi_url ||= 'https://pypi.org/simple' end end unless validate_only Logger.info "Installing python packages from requirements.txt with pypi_url=#{pypi_url}" puts `/openc3/bin/pipinstall --user --no-warn-script-location -i #{pypi_url} -r #{File.join(gem_path, 'requirements.txt')}` end 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' begin if current_model current_model.create unless validate_only current_model.deploy(gem_path, variables, validate_only: validate_only) end # If something goes wrong in create, or more likely in deploy, # we want to clear the current_model and try to instantiate the next # Otherwise we're stuck constantly iterating on the last model ensure current_model = nil current_model = OpenC3.const_get((keyword.capitalize + 'Model').intern).handle_config(parser, keyword, params, plugin: plugin_model.name, needs_dependencies: needs_dependencies, scope: scope) end 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 => e # Install failed - need to cleanup plugin_model.destroy unless validate_only raise e 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 |