lib/openc3/models/widget_model.rb in openc3-5.0.11 vs lib/openc3/models/widget_model.rb in openc3-5.1.0
- old
+ new
@@ -14,24 +14,28 @@
# GNU Affero General Public License for more details.
# Modified by OpenC3, Inc.
# All changes Copyright 2022, OpenC3, Inc.
# All Rights Reserved
+#
+# This file may also be used under the terms of a commercial license
+# if purchased from OpenC3, Inc.
require 'openc3/top_level'
require 'openc3/models/model'
require 'openc3/models/scope_model'
-require 'openc3/utilities/s3'
+require 'openc3/utilities/bucket'
+require 'openc3/utilities/bucket_utilities'
module OpenC3
class WidgetModel < Model
PRIMARY_KEY = 'openc3_widgets'
attr_accessor :name
attr_accessor :full_name
attr_accessor :filename
- attr_accessor :s3_key
+ attr_accessor :bucket_key
attr_accessor :needs_dependencies
# NOTE: The following three class methods are used by the ModelController
# and are reimplemented to enable various Model class methods to work
def self.get(name:, scope: nil)
@@ -84,11 +88,11 @@
scope:
)
super("#{scope}__#{PRIMARY_KEY}", name: name, plugin: plugin, updated_at: updated_at, scope: scope)
@full_name = @name.capitalize + 'Widget'
@filename = @full_name + '.umd.min.js'
- @s3_key = 'widgets/' + @full_name + '/' + @filename
+ @bucket_key = 'widgets/' + @full_name + '/' + @filename
@needs_dependencies = needs_dependencies
end
def as_json(*a)
{
@@ -108,31 +112,34 @@
raise ConfigParser::Error.new(parser, "Unknown keyword and parameters for Widget: #{keyword} #{parameters.join(" ")}")
end
def deploy(gem_path, variables, validate_only: false)
# Ensure tools bucket exists
- OpenC3::S3Utilities.ensure_public_bucket('tools') unless validate_only
+ bucket = nil
+ unless validate_only
+ bucket = Bucket.getClient()
+ bucket.ensure_public(ENV['OPENC3_TOOLS_BUCKET'])
+ end
filename = gem_path + "/tools/widgets/" + @full_name + '/' + @filename
# Load widget file
data = File.read(filename, mode: "rb")
OpenC3.set_working_dir(File.dirname(filename)) do
data = ERB.new(data, trim_mode: "-").result(binding.set_variables(variables)) if data.is_printable?
end
unless validate_only
- cache_control = OpenC3::S3Utilities.get_cache_control(@filename)
+ cache_control = BucketUtilities.get_cache_control(@filename)
# TODO: support widgets that aren't just a single js file (and its associated map file)
- rubys3_client = Aws::S3::Client.new
- rubys3_client.put_object(bucket: 'tools', content_type: 'application/javascript', cache_control: cache_control, key: @s3_key, body: data)
+ bucket.put_object(bucket: ENV['OPENC3_TOOLS_BUCKET'], content_type: 'application/javascript', cache_control: cache_control, key: @bucket_key, body: data)
data = File.read(filename + '.map', mode: "rb")
- rubys3_client.put_object(bucket: 'tools', content_type: 'application/json', cache_control: cache_control, key: @s3_key + '.map', body: data)
+ bucket.put_object(bucket: ENV['OPENC3_TOOLS_BUCKET'], content_type: 'application/json', cache_control: cache_control, key: @bucket_key + '.map', body: data)
end
end
def undeploy
- rubys3_client = Aws::S3::Client.new
- rubys3_client.delete_object(bucket: 'tools', key: @s3_key)
- rubys3_client.delete_object(bucket: 'tools', key: @s3_key + '.map')
+ bucket = Bucket.getClient()
+ bucket.delete_object(bucket: ENV['OPENC3_TOOLS_BUCKET'], key: @bucket_key)
+ bucket.delete_object(bucket: ENV['OPENC3_TOOLS_BUCKET'], key: @bucket_key + '.map')
end
end
end