Class: OpenC3::MicroserviceModel
- Defined in:
- lib/openc3/models/microservice_model.rb
Constant Summary collapse
- PRIMARY_KEY =
'openc3_microservices'
Instance Attribute Summary collapse
-
#cmd ⇒ Object
Returns the value of attribute cmd.
-
#container ⇒ Object
Returns the value of attribute container.
-
#env ⇒ Object
Returns the value of attribute env.
-
#folder_name ⇒ Object
Returns the value of attribute folder_name.
-
#needs_dependencies ⇒ Object
Returns the value of attribute needs_dependencies.
-
#options ⇒ Object
Returns the value of attribute options.
-
#ports ⇒ Object
Returns the value of attribute ports.
-
#target_names ⇒ Object
Returns the value of attribute target_names.
-
#topics ⇒ Object
Returns the value of attribute topics.
-
#work_dir ⇒ Object
Returns the value of attribute work_dir.
Attributes inherited from Model
#name, #plugin, #scope, #updated_at
Class Method Summary collapse
- .all(scope: nil) ⇒ Object
-
.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.
-
.handle_config(parser, keyword, parameters, plugin: nil, needs_dependencies: false, scope:) ⇒ Object
Called by the PluginModel to allow this class to validate it’s top-level keyword: “MICROSERVICE”.
- .names(scope: nil) ⇒ Object
Instance Method Summary collapse
- #as_config ⇒ Object
- #as_json(*a) ⇒ Object
- #deploy(gem_path, variables, validate_only: false) ⇒ Object
- #handle_config(parser, keyword, parameters) ⇒ Object
-
#initialize(name:, folder_name: nil, cmd: [], work_dir: '.', ports: [], env: {}, topics: [], target_names: [], options: [], container: nil, updated_at: nil, plugin: nil, needs_dependencies: false, scope:) ⇒ MicroserviceModel
constructor
Create a microservice model to be deployed to S3.
- #undeploy ⇒ Object
Methods inherited from Model
#create, #destroy, #destroyed?, filter, find_all_by_plugin, from_json, get_all_models, get_model, set, store, #update
Constructor Details
#initialize(name:, folder_name: nil, cmd: [], work_dir: '.', ports: [], env: {}, topics: [], target_names: [], options: [], container: nil, updated_at: nil, plugin: nil, needs_dependencies: false, scope:) ⇒ MicroserviceModel
Create a microservice model to be deployed to S3
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 |
# File 'lib/openc3/models/microservice_model.rb', line 80 def initialize( name:, folder_name: nil, cmd: [], work_dir: '.', ports: [], env: {}, topics: [], target_names: [], options: [], container: nil, updated_at: nil, plugin: nil, needs_dependencies: false, scope: ) parts = name.split("__") if parts.length != 3 raise "name '#{name}' must be formatted as SCOPE__TYPE__NAME" end if parts[0] != scope raise "name '#{name}' scope '#{parts[0]}' doesn't match scope parameter '#{scope}'" end super(PRIMARY_KEY, name: name, updated_at: updated_at, plugin: plugin, scope: scope) @folder_name = folder_name @cmd = cmd @work_dir = work_dir @ports = ports @env = env @topics = topics @target_names = target_names @options = @container = container @needs_dependencies = needs_dependencies end |
Instance Attribute Details
#cmd ⇒ Object
Returns the value of attribute cmd.
28 29 30 |
# File 'lib/openc3/models/microservice_model.rb', line 28 def cmd @cmd end |
#container ⇒ Object
Returns the value of attribute container.
29 30 31 |
# File 'lib/openc3/models/microservice_model.rb', line 29 def container @container end |
#env ⇒ Object
Returns the value of attribute env.
30 31 32 |
# File 'lib/openc3/models/microservice_model.rb', line 30 def env @env end |
#folder_name ⇒ Object
Returns the value of attribute folder_name.
31 32 33 |
# File 'lib/openc3/models/microservice_model.rb', line 31 def folder_name @folder_name end |
#needs_dependencies ⇒ Object
Returns the value of attribute needs_dependencies.
32 33 34 |
# File 'lib/openc3/models/microservice_model.rb', line 32 def needs_dependencies @needs_dependencies end |
#options ⇒ Object
Returns the value of attribute options.
33 34 35 |
# File 'lib/openc3/models/microservice_model.rb', line 33 def @options end |
#ports ⇒ Object
Returns the value of attribute ports.
37 38 39 |
# File 'lib/openc3/models/microservice_model.rb', line 37 def ports @ports end |
#target_names ⇒ Object
Returns the value of attribute target_names.
34 35 36 |
# File 'lib/openc3/models/microservice_model.rb', line 34 def target_names @target_names end |
#topics ⇒ Object
Returns the value of attribute topics.
35 36 37 |
# File 'lib/openc3/models/microservice_model.rb', line 35 def topics @topics end |
#work_dir ⇒ Object
Returns the value of attribute work_dir.
36 37 38 |
# File 'lib/openc3/models/microservice_model.rb', line 36 def work_dir @work_dir end |
Class Method Details
.all(scope: nil) ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/openc3/models/microservice_model.rb', line 56 def self.all(scope: nil) scoped = {} unscoped = super(PRIMARY_KEY) unscoped.each do |name, json| if !scope or name.split("__")[0] == scope scoped[name] = json end end scoped 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
41 42 43 |
# File 'lib/openc3/models/microservice_model.rb', line 41 def self.get(name:, scope: nil) super(PRIMARY_KEY, name: name) end |
.handle_config(parser, keyword, parameters, plugin: nil, needs_dependencies: false, scope:) ⇒ Object
Called by the PluginModel to allow this class to validate it’s top-level keyword: “MICROSERVICE”
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/openc3/models/microservice_model.rb', line 68 def self.handle_config(parser, keyword, parameters, plugin: nil, needs_dependencies: false, scope:) case keyword when 'MICROSERVICE' parser.verify_num_parameters(2, 2, "#{keyword} <Folder Name> <Name>") # Create name by adding scope and type 'USER' to indicate where this microservice came from return self.new(folder_name: parameters[0], name: "#{scope}__USER__#{parameters[1].upcase}", plugin: plugin, needs_dependencies: needs_dependencies, scope: scope) else raise ConfigParser::Error.new(parser, "Unknown keyword and parameters for Microservice: #{keyword} #{parameters.join(" ")}") end end |
.names(scope: nil) ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/openc3/models/microservice_model.rb', line 45 def self.names(scope: nil) scoped = [] unscoped = super(PRIMARY_KEY) unscoped.each do |name| if !scope or name.split("__")[0] == scope scoped << name end end scoped end |
Instance Method Details
#as_config ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/openc3/models/microservice_model.rb', line 135 def as_config result = "MICROSERVICE #{@folder_name ? @folder_name : 'nil'} #{@name.split("__")[-1]}\n" result << " CMD #{@cmd.join(' ')}\n" result << " WORK_DIR \"#{@work_dir}\"\n" @ports.each do |port| result << " PORT #{port}\n" end @topics.each do |topic_name| result << " TOPIC #{topic_name}\n" end @target_names.each do |target_name| result << " TARGET_NAME #{target_name}\n" end @env.each do |key, value| result << " ENV #{key} \"#{value}\"\n" end @options.each do |option| result << " OPTION #{option.join(" ")}\n" end result << " CONTAINER #{@container}\n" if @container != 'openc3-base' result end |
#as_json(*a) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/openc3/models/microservice_model.rb', line 117 def as_json(*a) { 'name' => @name, 'folder_name' => @folder_name, 'cmd' => @cmd, 'work_dir' => @work_dir, 'ports' => @ports, 'env' => @env, 'topics' => @topics, 'target_names' => @target_names, 'options' => @options, 'container' => @container, 'updated_at' => @updated_at, 'plugin' => @plugin, 'needs_dependencies' => @needs_dependencies, } end |
#deploy(gem_path, variables, validate_only: false) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/openc3/models/microservice_model.rb', line 206 def deploy(gem_path, variables, validate_only: false) return unless @folder_name variables["microservice_name"] = @name start_path = "/microservices/#{@folder_name}/" Dir.glob(gem_path + start_path + "**/*") do |filename| next if filename == '.' or filename == '..' or File.directory?(filename) path = filename.split(gem_path)[-1] key = "#{@scope}/microservices/#{@name}/" + path.split(start_path)[-1] # Load microservice files 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 Aws::S3::Client.new.put_object(bucket: 'config', key: key, body: data) ConfigTopic.write({ kind: 'created', type: 'microservice', name: @name, plugin: @plugin }, scope: @scope) end end end |
#handle_config(parser, keyword, parameters) ⇒ Object
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 |
# File 'lib/openc3/models/microservice_model.rb', line 158 def handle_config(parser, keyword, parameters) case keyword when 'ENV' parser.verify_num_parameters(2, 2, "#{keyword} <Key> <Value>") @env[parameters[0]] = parameters[1] when 'WORK_DIR' parser.verify_num_parameters(1, 1, "#{keyword} <Dir>") @work_dir = parameters[0] when 'PORT' usage = "PORT <Number> <Protocol (Optional)" parser.verify_num_parameters(1, 2, usage) begin @ports << [Integer(parameters[0])] rescue => err # In case Integer fails raise ConfigParser::Error.new(parser, "Port must be an integer: #{parameters[0]}", usage) end protocol = ConfigParser.handle_nil(parameters[1]) if protocol # Per https://kubernetes.io/docs/concepts/services-networking/service/#protocol-support if %w(TCP UDP SCTP HTTP SCTP).include?(protocol.upcase) @ports[-1] << protocol.upcase else raise ConfigParser::Error.new(parser, "Unknown port protocol: #{parameters[1]}", usage) end else @ports[-1] << 'TCP' end when 'TOPIC' parser.verify_num_parameters(1, 1, "#{keyword} <Topic Name>") @topics << parameters[0] when 'TARGET_NAME' parser.verify_num_parameters(1, 1, "#{keyword} <Target Name>") @target_names << parameters[0] when 'CMD' parser.verify_num_parameters(1, nil, "#{keyword} <Args>") @cmd = parameters.dup when 'OPTION' parser.verify_num_parameters(2, nil, "#{keyword} <Option Name> <Option Values>") @options << parameters.dup when 'CONTAINER' parser.verify_num_parameters(1, 1, "#{keyword} <Container Image Name>") @container = parameters[0] else raise ConfigParser::Error.new(parser, "Unknown keyword and parameters for Microservice: #{keyword} #{parameters.join(" ")}") end return nil end |
#undeploy ⇒ Object
229 230 231 232 233 234 235 236 |
# File 'lib/openc3/models/microservice_model.rb', line 229 def undeploy rubys3_client = Aws::S3::Client.new prefix = "#{@scope}/microservices/#{@name}/" rubys3_client.list_objects(bucket: 'config', prefix: prefix).contents.each do |object| rubys3_client.delete_object(bucket: 'config', key: object.key) end ConfigTopic.write({ kind: 'deleted', type: 'microservice', name: @name, plugin: @plugin }, scope: @scope) end |