@@ -31,14 +31,14 @@ def initialize
3131 @model_class = OpenC3 ::PluginModel
3232 end
3333
34- def check_localhost_reachability ( gem_url , store_id )
34+ def check_localhost_reachability ( gem_url , store_plugin_id )
3535 uri = URI . parse ( gem_url )
3636 return gem_url unless [ 'localhost' , '127.0.0.1' ] . include? uri . host
3737
3838 api_key_setting = OpenC3 ::SettingModel . get ( name : 'store_api_key' , scope : 'DEFAULT' )
3939 api_key = api_key_setting [ 'data' ] if api_key_setting
4040
41- test_url = "http://#{ uri . host } :#{ uri . port } /api/v1.1/cosmos_plugins/#{ store_id } "
41+ test_url = "http://#{ uri . host } :#{ uri . port } /api/v1.1/cosmos_plugins/#{ store_plugin_id } "
4242 begin
4343 uri_obj = URI ( test_url )
4444 req = Net ::HTTP ::Get . new ( uri_obj )
@@ -79,19 +79,23 @@ def show
7979 store_plugins = JSON . parse ( store_plugins )
8080 if store_plugins . is_a? ( Array ) # as opposed to a Hash, which indicates an error
8181 plugins . each do |plugin_name , plugin |
82- if plugin [ 'store_id' ]
83- store_data = store_plugins . find { |store_plugin | store_plugin [ 'id' ] == plugin [ 'store_id' ] }
84- plugin . merge! ( store_data ) if store_data
82+ if plugin [ 'store_plugin_id' ]
83+ store_plugin = store_plugins . find { |store_plugin | store_plugin [ 'id' ] == plugin [ 'store_plugin_id' ] }
84+ store_version_id = plugin [ 'store_version_id' ] || store_plugin [ 'current_version_id' ]
85+ store_version = store_plugin [ 'versions' ] . find { |store_version | store_version [ 'id' ] == store_version_id }
86+ plugin . merge! ( store_version ) if store_version
8587 end
8688 end
8789 end
8890
8991 render json : plugins
9092 else
9193 plugin = @model_class . get ( name : params [ :id ] , scope : params [ :scope ] )
92- if plugin && plugin [ 'store_id' ]
93- store_data = OpenC3 ::PluginStoreModel . get_by_id ( plugin [ 'store_id' ] )
94- plugin . merge! ( store_data ) if store_data
94+ if plugin && plugin [ 'store_plugin_id' ]
95+ store_plugin = OpenC3 ::PluginStoreModel . get_by_id ( plugin [ 'store_plugin_id' ] )
96+ store_version_id = plugin [ 'store_version_id' ] || store_plugin [ 'current_version_id' ]
97+ store_version = store_plugin [ 'versions' ] . find { |store_version | store_version [ 'id' ] == store_version_id }
98+ plugin . merge! ( store_version ) if store_version
9599 end
96100
97101 render json : plugin
@@ -104,15 +108,20 @@ def show
104108 # Add a new plugin
105109 def create ( update = false )
106110 return unless authorization ( 'admin' )
107- file = if params [ :store_id ]
108- store_data = OpenC3 ::PluginStoreModel . get_by_id ( params [ :store_id ] )
109- if store_data . nil? || store_data [ 'gem_url' ] . nil?
110- render json : { status : 'error' , message : 'Unable to fetch requested plugin.' } , status : 500
111+ file = if params [ :store_plugin_id ]
112+ store_data = OpenC3 ::PluginStoreModel . get_by_id ( params [ :store_plugin_id ] )
113+ unless store_data . nil?
114+ store_version_id = params [ :store_version_id ] || store_data [ 'current_version_id' ]
115+ store_version_id = Integer ( store_version_id )
116+ store_version = store_data [ 'versions' ] . find { |version | version [ 'id' ] == store_version_id }
117+ end
118+ if store_version . nil? || store_version [ 'gem_url' ] . nil?
119+ render json : { status : 'error' , message : 'Unable to fetch requested plugin version.' } , status : 500
111120 return
112121 end
113122
114123 # Try to find the correct hostname (in case it's localhost and needs to be host.docker.internal)
115- adjusted_gem_url = check_localhost_reachability ( store_data [ 'gem_url' ] , params [ :store_id ] )
124+ adjusted_gem_url = check_localhost_reachability ( store_version [ 'gem_url' ] , params [ :store_plugin_id ] )
116125 if adjusted_gem_url . nil?
117126 render json : { status : 'error' , message : 'Gem could not be downloaded. Host is not reachable.' } , status : 500
118127 return
@@ -127,13 +136,13 @@ def create(update = false)
127136 tempfile = Down . download ( adjusted_gem_url )
128137 end
129138 checksum = Digest ::SHA256 . file ( tempfile . path ) . hexdigest . downcase
130- expected = store_data [ 'checksum' ] . downcase
139+ expected = store_version [ 'checksum' ] . downcase
131140 unless checksum == expected
132141 render json : { status : 'error' , message : "Checksum verification failed. Expected #{ expected } but got #{ checksum } " } , status : 500
133142 return
134143 end
135144
136- original_filename = File . basename ( store_data [ 'gem_filename' ] )
145+ original_filename = File . basename ( store_version [ 'gem_filename' ] )
137146 tempfile
138147 else
139148 params [ :plugin ]
@@ -149,9 +158,9 @@ def create(update = false)
149158 gem_file_path = temp_dir + '/' + original_filename
150159 FileUtils . cp ( tempfile . path , gem_file_path )
151160 if @existing_model
152- result = OpenC3 ::PluginModel . install_phase1 ( gem_file_path , existing_variables : @existing_model [ 'variables' ] , existing_plugin_txt_lines : @existing_model [ 'plugin_txt_lines' ] , store_id : params [ :store_id ] , scope : scope )
161+ result = OpenC3 ::PluginModel . install_phase1 ( gem_file_path , existing_variables : @existing_model [ 'variables' ] , existing_plugin_txt_lines : @existing_model [ 'plugin_txt_lines' ] , store_plugin_id : params [ :store_plugin_id ] , store_version_id : params [ :store_version_id ] , scope : scope )
153162 else
154- result = OpenC3 ::PluginModel . install_phase1 ( gem_file_path , store_id : params [ :store_id ] , scope : scope )
163+ result = OpenC3 ::PluginModel . install_phase1 ( gem_file_path , store_plugin_id : params [ :store_plugin_id ] , store_version_id : params [ :store_version_id ] , scope : scope )
155164 end
156165 render json : result
157166 rescue OpenC3 ::EmptyGemFileError => error
0 commit comments