Class: OpenC3::MicroserviceModel
- Includes:
- ConfigKeywordModel
- 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.
-
#db_shard ⇒ Object
Returns the value of attribute db_shard.
-
#disable_erb ⇒ Object
Returns the value of attribute disable_erb.
-
#enabled ⇒ Object
Returns the value of attribute enabled.
-
#env ⇒ Object
Returns the value of attribute env.
-
#folder_name ⇒ Object
Returns the value of attribute folder_name.
-
#ignore_changes ⇒ Object
Returns the value of attribute ignore_changes.
-
#needs_dependencies ⇒ Object
Returns the value of attribute needs_dependencies.
-
#options ⇒ Object
Returns the value of attribute options.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#ports ⇒ Object
Returns the value of attribute ports.
-
#prefix ⇒ Object
Returns the value of attribute prefix.
-
#secrets ⇒ Object
Returns the value of attribute secrets.
-
#shard ⇒ Object
Returns the value of attribute shard.
-
#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_json(*a) ⇒ Object
- #cleanup ⇒ 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: [], parent: nil, container: nil, updated_at: nil, plugin: nil, needs_dependencies: false, secrets: [], prefix: nil, disable_erb: nil, ignore_changes: nil, shard: 0, db_shard: 0, enabled: true, scope:) ⇒ MicroserviceModel
constructor
Create a microservice model to be deployed to bucket storage.
-
#runtime_python_env ⇒ Object
Resolve the Python environment variables that will be used at runtime.
- #undeploy ⇒ Object
Methods included from ConfigKeywordModel
Methods inherited from Model
#check_disable_erb, #create, #destroy, #destroyed?, #diff, filter, find_all_by_plugin, from_json, get_all_models, get_model, set, store, store_queued, #update
Constructor Details
#initialize(name:, folder_name: nil, cmd: [], work_dir: '.', ports: [], env: {}, topics: [], target_names: [], options: [], parent: nil, container: nil, updated_at: nil, plugin: nil, needs_dependencies: false, secrets: [], prefix: nil, disable_erb: nil, ignore_changes: nil, shard: 0, db_shard: 0, enabled: true, scope:) ⇒ MicroserviceModel
Create a microservice model to be deployed to bucket storage
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 141 142 143 144 145 |
# File 'lib/openc3/models/microservice_model.rb', line 92 def initialize( name:, folder_name: nil, cmd: [], work_dir: '.', ports: [], env: {}, topics: [], target_names: [], options: [], parent: nil, container: nil, updated_at: nil, plugin: nil, needs_dependencies: false, secrets: [], prefix: nil, disable_erb: nil, ignore_changes: nil, shard: 0, db_shard: 0, enabled: true, 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 = @parent = parent @container = container @needs_dependencies = needs_dependencies @secrets = secrets @prefix = prefix @disable_erb = disable_erb @ignore_changes = ignore_changes @shard = shard.to_i # to_i to handle nil @db_shard = db_shard.to_i # to_i to handle nil @enabled = enabled @enabled = true if @enabled.nil? @bucket = Bucket.getClient() end |
Instance Attribute Details
#cmd ⇒ Object
Returns the value of attribute cmd.
32 33 34 |
# File 'lib/openc3/models/microservice_model.rb', line 32 def cmd @cmd end |
#container ⇒ Object
Returns the value of attribute container.
33 34 35 |
# File 'lib/openc3/models/microservice_model.rb', line 33 def container @container end |
#db_shard ⇒ Object
Returns the value of attribute db_shard.
48 49 50 |
# File 'lib/openc3/models/microservice_model.rb', line 48 def db_shard @db_shard end |
#disable_erb ⇒ Object
Returns the value of attribute disable_erb.
45 46 47 |
# File 'lib/openc3/models/microservice_model.rb', line 45 def disable_erb @disable_erb end |
#enabled ⇒ Object
Returns the value of attribute enabled.
49 50 51 |
# File 'lib/openc3/models/microservice_model.rb', line 49 def enabled @enabled end |
#env ⇒ Object
Returns the value of attribute env.
34 35 36 |
# File 'lib/openc3/models/microservice_model.rb', line 34 def env @env end |
#folder_name ⇒ Object
Returns the value of attribute folder_name.
35 36 37 |
# File 'lib/openc3/models/microservice_model.rb', line 35 def folder_name @folder_name end |
#ignore_changes ⇒ Object
Returns the value of attribute ignore_changes.
46 47 48 |
# File 'lib/openc3/models/microservice_model.rb', line 46 def ignore_changes @ignore_changes end |
#needs_dependencies ⇒ Object
Returns the value of attribute needs_dependencies.
36 37 38 |
# File 'lib/openc3/models/microservice_model.rb', line 36 def needs_dependencies @needs_dependencies end |
#options ⇒ Object
Returns the value of attribute options.
37 38 39 |
# File 'lib/openc3/models/microservice_model.rb', line 37 def @options end |
#parent ⇒ Object
Returns the value of attribute parent.
42 43 44 |
# File 'lib/openc3/models/microservice_model.rb', line 42 def parent @parent end |
#ports ⇒ Object
Returns the value of attribute ports.
41 42 43 |
# File 'lib/openc3/models/microservice_model.rb', line 41 def ports @ports end |
#prefix ⇒ Object
Returns the value of attribute prefix.
44 45 46 |
# File 'lib/openc3/models/microservice_model.rb', line 44 def prefix @prefix end |
#secrets ⇒ Object
Returns the value of attribute secrets.
43 44 45 |
# File 'lib/openc3/models/microservice_model.rb', line 43 def secrets @secrets end |
#shard ⇒ Object
Returns the value of attribute shard.
47 48 49 |
# File 'lib/openc3/models/microservice_model.rb', line 47 def shard @shard end |
#target_names ⇒ Object
Returns the value of attribute target_names.
38 39 40 |
# File 'lib/openc3/models/microservice_model.rb', line 38 def target_names @target_names end |
#topics ⇒ Object
Returns the value of attribute topics.
39 40 41 |
# File 'lib/openc3/models/microservice_model.rb', line 39 def topics @topics end |
#work_dir ⇒ Object
Returns the value of attribute work_dir.
40 41 42 |
# File 'lib/openc3/models/microservice_model.rb', line 40 def work_dir @work_dir end |
Class Method Details
.all(scope: nil) ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/openc3/models/microservice_model.rb', line 68 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
53 54 55 |
# File 'lib/openc3/models/microservice_model.rb', line 53 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"
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/openc3/models/microservice_model.rb', line 80 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
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/openc3/models/microservice_model.rb', line 57 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_json(*a) ⇒ Object
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 |
# File 'lib/openc3/models/microservice_model.rb', line 167 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, 'parent' => @parent, 'container' => @container, 'updated_at' => @updated_at, 'plugin' => @plugin, 'needs_dependencies' => @needs_dependencies, 'secrets' => @secrets, 'prefix' => @prefix, 'disable_erb' => @disable_erb, 'ignore_changes' => @ignore_changes, 'shard' => @shard, 'db_shard' => @db_shard, 'enabled' => @enabled, } end |
#cleanup ⇒ Object
310 311 312 313 314 |
# File 'lib/openc3/models/microservice_model.rb', line 310 def cleanup # Cleanup metrics metric_model = MetricModel.new(name: @name, scope: @scope) metric_model.destroy end |
#deploy(gem_path, variables, validate_only: false) ⇒ Object
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 290 291 |
# File 'lib/openc3/models/microservice_model.rb', line 263 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") erb_disabled = check_disable_erb(filename) unless erb_disabled OpenC3.set_working_dir(File.dirname(filename)) do data = ERB.new(data.comment_erb(), trim_mode: "-").result(binding.set_variables(variables)) if data.is_printable? and File.basename(filename)[0] != '_' end end unless validate_only @bucket.put_object(bucket: ENV['OPENC3_CONFIG_BUCKET'], key: key, body: data) end end unless validate_only config = { kind: 'created', type: 'microservice', name: @name } config[:plugin] = @plugin if @plugin ConfigTopic.write(config, scope: @scope) end end |
#handle_config(parser, keyword, parameters) ⇒ Object
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 |
# File 'lib/openc3/models/microservice_model.rb', line 193 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' parse_port(parser, keyword, parameters) 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].upcase when 'CMD' parser.verify_num_parameters(1, nil, "#{keyword} <Args>") @cmd = parameters.dup # Automatically use UV venv Python for user microservices to ensure openc3 module is found # Replace 'python', 'python3', or 'python3.X' with the correct Python binary # Handles both split form: CMD python start.py -> ["python", "start.py"] # and quoted single-string form: CMD "python start.py" -> ["python start.py"] python_bin = ENV['OPENC3_PYTHON_BIN'] || '/openc3/python/.venv/bin/python' # Handle split form: CMD python start.py => @cmd = ["python", "start.py"] if @cmd[0] =~ /^python3?(\.\d+)*$/ @cmd[0] = python_bin # Handle quoted single-string form: CMD "python start.py" => @cmd = ["python start.py"] elsif @cmd[0] =~ /^python3?(\.\d+)*\s+/ _exe, rest = @cmd[0].split(/\s+/, 2) @cmd = [python_bin, rest] end 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] when 'SECRET' parser.verify_num_parameters(3, 4, "#{keyword} <Secret Type: ENV or FILE> <Secret Name> <Environment Variable Name or File Path> <Secret Store Name (Optional)>") validate_secret(parser, keyword, parameters) if ConfigParser.handle_nil(parameters[3]) @secrets << parameters.dup else @secrets << parameters[0..2] end when 'ROUTE_PREFIX' parser.verify_num_parameters(1, 1, "#{keyword} <Route Prefix>") @prefix = parameters[0] when 'DISABLE_ERB' # 0 to unlimited parameters @disable_erb ||= [] if parameters @disable_erb.concat(parameters) end when 'SHARD' parser.verify_num_parameters(1, 1, "#{keyword} <Shard Number Starting from 0>") @shard = Integer(parameters[0]) when 'DB_SHARD' parser.verify_num_parameters(1, 1, "#{keyword} <DB_Shard Number Starting from 0>") @db_shard = Integer(parameters[0]) when 'STOPPED' parser.verify_num_parameters(0, 0, "#{keyword}") @enabled = false else raise ConfigParser::Error.new(parser, "Unknown keyword and parameters for Microservice: #{keyword} #{parameters.join(" ")}") end return nil end |
#runtime_python_env ⇒ Object
Resolve the Python environment variables that will be used at runtime. This mirrors the logic in MicroserviceOperator#convert_microservice_to_process_definition.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/openc3/models/microservice_model.rb', line 149 def runtime_python_env python_bin = ENV['OPENC3_PYTHON_BIN'] || '/openc3/python/.venv/bin/python' result = { 'OPENC3_PYTHON_BIN' => python_bin } if @needs_dependencies && @plugin candidate = PythonVenv.plugin_venv_path(scope: @scope, plugin_name: @plugin) if candidate result['VIRTUAL_ENV'] = candidate result['PYTHONUSERBASE'] = candidate site_packages = Dir.glob("#{candidate}/lib/python*/site-packages").first result['PYTHONPATH'] = site_packages else result['VIRTUAL_ENV'] = nil result['PYTHONUSERBASE'] = PythonVenv::DEFAULT_PYTHONUSERBASE end end result end |
#undeploy ⇒ Object
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/openc3/models/microservice_model.rb', line 293 def undeploy prefix = "#{@scope}/microservices/#{@name}/" objects = @bucket.list_objects(bucket: ENV['OPENC3_CONFIG_BUCKET'], prefix: prefix) keys = objects.map(&:key) if keys.length > 0 # Batch delete in chunks of 1000 (S3 limit) keys.each_slice(1000) do |key_batch| @bucket.delete_objects(bucket: ENV['OPENC3_CONFIG_BUCKET'], keys: key_batch) end end config = { kind: 'deleted', type: 'microservice', name: @name } config[:plugin] = @plugin if @plugin ConfigTopic.write(config, scope: @scope) rescue Exception => error Logger.error("Error undeploying microservice model #{@name} in scope #{@scope} due to #{error}") end |