Class: OpenC3::ScopeModel

Inherits:
Model show all
Defined in:
lib/openc3/models/scope_model.rb

Constant Summary collapse

PRIMARY_KEY =
"openc3_scopes"
SYSTEM_HEALTH_DEFAULTS =

Default thresholds used by the System Health tool and the metrics microservices. Stored in the 'system_health' setting as a JSON string.

{
  "cpu" => {
    "redThreshold" => 90.0,
    "yellowThreshold" => 80.0,
    "snoozeMinutes" => 15,
    "lastTriggerTimeRed" => nil,  # timestamp or nil
    "lastTriggerTimeYellow" => nil,  # timestamp or nil
    "sustainedSeconds" => 15
  },
  "memory" => {
    "redThreshold" => 90.0,
    "yellowThreshold" => 80.0,
    "snoozeMinutes" => 15,
    "lastTriggerTimeRed" => nil,  # timestamp or nil
    "lastTriggerTimeYellow" => nil,  # timestamp or nil
    "sustainedSeconds" => 15
  },
  "disk" => {
    "redThreshold" => 90.0,
    "yellowThreshold" => 80.0,
    "snoozeMinutes" => 720,  # 12 hours
    "lastTriggerTimeRed" => nil,  # timestamp or nil
    "lastTriggerTimeYellow" => nil,  # timestamp or nil
    "sustainedSeconds" => 60
  },
  "global" => {
    "enableAlerts" => true
  }
}

Instance Attribute Summary collapse

Attributes inherited from Model

#name, #plugin, #scope, #updated_at

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#check_disable_erb, #destroyed?, #diff, filter, find_all_by_plugin, get_all_models, handle_config, set, store, store_queued, #update

Constructor Details

#initialize(name:, text_log_cycle_time: 600, text_log_cycle_size: 50_000_000, text_log_retain_time: nil, tool_log_retain_time: nil, cleanup_poll_time: 3600, command_authority: false, critical_commanding: "OFF", shard: 0, updated_at: nil) ⇒ ScopeModel

Returns a new instance of ScopeModel.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/openc3/models/scope_model.rb', line 122

def initialize(name:,
  text_log_cycle_time: 600,
  text_log_cycle_size: 50_000_000,
  text_log_retain_time: nil,
  tool_log_retain_time: nil,
  cleanup_poll_time: 3600,
  command_authority: false,
  critical_commanding: "OFF",
  shard: 0,
  updated_at: nil)
  super(
    PRIMARY_KEY,
    name: name,
    updated_at: updated_at,
    # This sets the @scope variable which is sort of redundant for the ScopeModel
    # (since its the same as @name) but every model has a @scope
    scope: name
  )
  @text_log_cycle_time = text_log_cycle_time
  @text_log_cycle_size = text_log_cycle_size
  @text_log_retain_time = text_log_retain_time
  @tool_log_retain_time = tool_log_retain_time
  @cleanup_poll_time = cleanup_poll_time
  @command_authority = command_authority
  @critical_commanding = critical_commanding.to_s.upcase
  @critical_commanding = "OFF" if @critical_commanding.length == 0
  unless ["OFF", "NORMAL", "ALL"].include?(@critical_commanding)
    raise "Invalid value for critical_commanding: #{@critical_commanding}"
  end
  @shard = shard.to_i # to_i to handle nil
  @children = []
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



80
81
82
# File 'lib/openc3/models/scope_model.rb', line 80

def children
  @children
end

#cleanup_poll_timeObject

Returns the value of attribute cleanup_poll_time.



85
86
87
# File 'lib/openc3/models/scope_model.rb', line 85

def cleanup_poll_time
  @cleanup_poll_time
end

#command_authorityObject

Returns the value of attribute command_authority.



86
87
88
# File 'lib/openc3/models/scope_model.rb', line 86

def command_authority
  @command_authority
end

#critical_commandingObject

Returns the value of attribute critical_commanding.



87
88
89
# File 'lib/openc3/models/scope_model.rb', line 87

def critical_commanding
  @critical_commanding
end

#shardObject

Returns the value of attribute shard.



88
89
90
# File 'lib/openc3/models/scope_model.rb', line 88

def shard
  @shard
end

#text_log_cycle_sizeObject

Returns the value of attribute text_log_cycle_size.



82
83
84
# File 'lib/openc3/models/scope_model.rb', line 82

def text_log_cycle_size
  @text_log_cycle_size
end

#text_log_cycle_timeObject

Returns the value of attribute text_log_cycle_time.



81
82
83
# File 'lib/openc3/models/scope_model.rb', line 81

def text_log_cycle_time
  @text_log_cycle_time
end

#text_log_retain_timeObject

Returns the value of attribute text_log_retain_time.



83
84
85
# File 'lib/openc3/models/scope_model.rb', line 83

def text_log_retain_time
  @text_log_retain_time
end

#tool_log_retain_timeObject

Returns the value of attribute tool_log_retain_time.



84
85
86
# File 'lib/openc3/models/scope_model.rb', line 84

def tool_log_retain_time
  @tool_log_retain_time
end

Class Method Details

.all(scope: nil) ⇒ Object

NOSONAR - scope: is part of the caller-facing signature



103
104
105
# File 'lib/openc3/models/scope_model.rb', line 103

def self.all(scope: nil) # NOSONAR - scope: is part of the caller-facing signature
  super(PRIMARY_KEY)
end

.from_json(json, scope: nil) ⇒ Object

NOSONAR - scope: is part of the caller-facing signature



107
108
109
110
111
# File 'lib/openc3/models/scope_model.rb', line 107

def self.from_json(json, scope: nil) # NOSONAR - scope: is part of the caller-facing signature
  json = JSON.parse(json, allow_nan: true, create_additions: true) if String === json
  raise "json data is nil" if json.nil?
  new(**json.transform_keys(&:to_sym))
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

The scope keyword is given to support the ModelController method signature even though it is not used



95
96
97
# File 'lib/openc3/models/scope_model.rb', line 95

def self.get(name:, scope: nil) # NOSONAR - scope: is part of the caller-facing signature
  super(PRIMARY_KEY, name: name)
end

.get_model(name:, scope: nil) ⇒ Object

NOSONAR - scope: is part of the caller-facing signature



113
114
115
116
117
118
119
120
# File 'lib/openc3/models/scope_model.rb', line 113

def self.get_model(name:, scope: nil) # NOSONAR - scope: is part of the caller-facing signature
  json = get(name: name)
  if json
    from_json(json)
  else
    nil
  end
end

.names(scope: nil) ⇒ Object

NOSONAR - scope: is part of the caller-facing signature



99
100
101
# File 'lib/openc3/models/scope_model.rb', line 99

def self.names(scope: nil) # NOSONAR - scope: is part of the caller-facing signature
  super(PRIMARY_KEY)
end

Instance Method Details

#as_json(*_a) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/openc3/models/scope_model.rb', line 206

def as_json(*_a)
  {"name" => @name,
   "updated_at" => @updated_at,
   "text_log_cycle_time" => @text_log_cycle_time,
   "text_log_cycle_size" => @text_log_cycle_size,
   "text_log_retain_time" => @text_log_retain_time,
   "tool_log_retain_time" => @tool_log_retain_time,
   "cleanup_poll_time" => @cleanup_poll_time,
   "command_authority" => @command_authority,
   "critical_commanding" => @critical_commanding,
   "shard" => @shard}
end

#create(update: false, force: false, queued: false) ⇒ Object



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
# File 'lib/openc3/models/scope_model.rb', line 155

def create(update: false, force: false, queued: false)
  # Ensure there are no "." in the scope name - prevents gems accidentally becoming scope names
  raise "Invalid scope name: #{@name}" if !/^[a-zA-Z0-9_-]+$/.match?(@name)
  # Double underscore is the COSMOS separator used to build microservice and topic names
  raise "Invalid scope name: #{@name} (double underscore not allowed)" if @name.include?("__")
  @name = @name.upcase
  @scope = @name # Ensure @scope matches @name
  # Ensure the various cycle and retain times are integers
  @text_log_cycle_time = @text_log_cycle_time.to_i
  @text_log_cycle_size = @text_log_cycle_size.to_i
  @text_log_retain_time = @text_log_retain_time.to_i if @text_log_retain_time
  @tool_log_retain_time = @tool_log_retain_time.to_i if @tool_log_retain_time
  @cleanup_poll_time = @cleanup_poll_time.to_i
  super

  if ENTERPRISE
    # If we're updating the scope and disabling command_authority
    # then we clear out all the existing values so it comes up fresh
    if update and @command_authority == false
      CmdAuthorityModel.names(scope: @name).each do |auth_name|
        model = CmdAuthorityModel.get_model(name: auth_name, scope: @name)
        model.destroy if model
      end
    end

    # If we're updating the scope and disabling critical_commanding
    # then we clear out all the pending critical commands
    if update and @critical_commanding == "OFF"
      CriticalCmdModel.names(scope: @name).each do |name|
        model = CriticalCmdModel.get_model(name: name, scope: @name)
        model.destroy if model
      end
    end
  end

  SystemEventsTopic.write(:scope, as_json)
end

#deploy(gem_path, variables) ⇒ Object



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/openc3/models/scope_model.rb', line 372

def deploy(gem_path, variables)
  seed_database

  if ENTERPRISE
    # Create DEFAULT trigger group model
    model = TriggerGroupModel.get(name: "DEFAULT", scope: @scope)
    unless model
      model = TriggerGroupModel.new(name: "DEFAULT", shard: @shard, scope: @scope)
      model.create
      model.deploy
    end
  end

  # Create UNKNOWN target for display of unknown data
  model = TargetModel.new(name: "UNKNOWN", shard: @shard, scope: @scope)
  model.create

  @parent = "#{@scope}__SCOPEMULTI__#{@scope}"

  # OpenC3 Log Microservice
  deploy_openc3_log_messages_microservice(gem_path, variables, @parent)

  # UNKNOWN CommandLog Microservice
  deploy_unknown_commandlog_microservice(gem_path, variables, @parent)

  # UNKNOWN PacketLog Microservice
  deploy_unknown_packetlog_microservice(gem_path, variables, @parent)

  # Only DEFAULT scope
  if @scope == "DEFAULT"
    # Periodic Microservice
    deploy_periodic_microservice(gem_path, variables, @parent)
  end

  # Scope Cleanup Microservice
  deploy_scopecleanup_microservice(gem_path, variables, @parent)

  if ENTERPRISE
    # Critical Cmd Microservice
    deploy_critical_cmd_microservice(gem_path, variables, @parent)
  end

  # DEFAULT bridge_microservice (Iroh hub for host interfaces)
  deploy_bridge_microservice(gem_path, variables)

  # Multi Microservice to parent other scope microservices
  deploy_scopemulti_microservice(gem_path, variables)
end

#deploy_bridge_microservice(gem_path, variables) ⇒ Object

Every scope gets a DEFAULT bridge_microservice: the Iroh hub that lets openc3-app run COSMOS interfaces on the host. It idles (no data streams, no enrolled app) until an interface is bridged / openc3-app enrolls.

It is a top-level microservice (spawned directly by the operator), NOT a child of SCOPEMULTI: that runs its Ruby children in-process, and the bridge is Python.



226
227
228
229
230
231
232
233
234
235
# File 'lib/openc3/models/scope_model.rb', line 226

def deploy_bridge_microservice(gem_path, variables)
  microservice = BridgeModel.build_microservice(
    bridge_name: "DEFAULT",
    scope: @scope,
    shard: @shard
  )
  microservice.create
  microservice.deploy(gem_path, variables)
  Logger.info "Configured microservice #{microservice.name}"
end

#deploy_critical_cmd_microservice(gem_path, variables, parent) ⇒ Object



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/openc3/models/scope_model.rb', line 341

def deploy_critical_cmd_microservice(gem_path, variables, parent)
  microservice_name = "#{@scope}__CRITICALCMD__#{@scope}"
  microservice = MicroserviceModel.new(
    name: microservice_name,
    cmd: ["ruby", "critical_cmd_microservice.rb", microservice_name],
    work_dir: "/openc3-enterprise/lib/openc3-enterprise/microservices",
    parent: parent,
    shard: @shard,
    scope: @scope
  )
  microservice.create
  microservice.deploy(gem_path, variables)
  @children << microservice_name if parent
  Logger.info "Configured microservice #{microservice_name}"
end

#deploy_openc3_log_messages_microservice(gem_path, variables, parent) ⇒ Object



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/scope_model.rb', line 237

def deploy_openc3_log_messages_microservice(gem_path, variables, parent)
  microservice_name = "#{@scope}__OPENC3__LOG"
  topics = ["#{@scope}__openc3_log_messages"]
  # Also log the NOSCOPE messages with this microservice for the DEFAULT scope
  if @scope == "DEFAULT"
    topics << "NOSCOPE__openc3_log_messages"
  end
  microservice = MicroserviceModel.new(
    name: microservice_name,
    cmd: ["ruby", "text_log_microservice.rb", microservice_name],
    work_dir: "/openc3/lib/openc3/microservices",
    options: [
      ["CYCLE_TIME", @text_log_cycle_time],
      ["CYCLE_SIZE", @text_log_cycle_size]
    ],
    topics: topics,
    parent: parent,
    shard: @shard,
    scope: @scope
  )
  microservice.create
  microservice.deploy(gem_path, variables)
  @children << microservice_name if parent
  Logger.info "Configured microservice #{microservice_name}"
end

#deploy_periodic_microservice(gem_path, variables, parent) ⇒ Object



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/openc3/models/scope_model.rb', line 309

def deploy_periodic_microservice(gem_path, variables, parent)
  microservice_name = "#{@scope}__PERIODIC__#{@scope}"
  microservice = MicroserviceModel.new(
    name: microservice_name,
    cmd: ["ruby", "periodic_microservice.rb", microservice_name],
    work_dir: "/openc3/lib/openc3/microservices",
    parent: parent,
    shard: @shard,
    scope: @scope
  )
  microservice.create
  microservice.deploy(gem_path, variables)
  @children << microservice_name if parent
  Logger.info "Configured microservice #{microservice_name}"
end

#deploy_scopecleanup_microservice(gem_path, variables, parent) ⇒ Object



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/openc3/models/scope_model.rb', line 325

def deploy_scopecleanup_microservice(gem_path, variables, parent)
  microservice_name = "#{@scope}__SCOPECLEANUP__#{@scope}"
  microservice = MicroserviceModel.new(
    name: microservice_name,
    cmd: ["ruby", "scope_cleanup_microservice.rb", microservice_name],
    work_dir: "/openc3/lib/openc3/microservices",
    parent: parent,
    shard: @shard,
    scope: @scope
  )
  microservice.create
  microservice.deploy(gem_path, variables)
  @children << microservice_name if parent
  Logger.info "Configured microservice #{microservice_name}"
end

#deploy_scopemulti_microservice(gem_path, variables) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/openc3/models/scope_model.rb', line 357

def deploy_scopemulti_microservice(gem_path, variables)
  microservice_name = "#{@scope}__SCOPEMULTI__#{@scope}"
  microservice = MicroserviceModel.new(
    name: microservice_name,
    cmd: ["ruby", "multi_microservice.rb", *@children],
    work_dir: "/openc3/lib/openc3/microservices",
    target_names: [],
    shard: @shard,
    scope: @scope
  )
  microservice.create
  microservice.deploy(gem_path, variables)
  Logger.info "Configured microservice #{microservice_name}"
end

#deploy_unknown_commandlog_microservice(gem_path, variables, parent) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/openc3/models/scope_model.rb', line 263

def deploy_unknown_commandlog_microservice(gem_path, variables, parent)
  microservice_name = "#{@scope}__COMMANDLOG__UNKNOWN"
  microservice = MicroserviceModel.new(
    name: microservice_name,
    cmd: ["ruby", "log_microservice.rb", microservice_name],
    work_dir: "/openc3/lib/openc3/microservices",
    options: [
      ["RAW_OR_DECOM", "RAW"],
      ["CMD_OR_TLM", "CMD"],
      ["CYCLE_TIME", "3600"] # Keep at most 1 hour per log
    ],
    topics: ["#{@scope}__COMMAND__{UNKNOWN}__UNKNOWN"],
    target_names: [],
    parent: parent,
    shard: @shard,
    scope: @scope
  )
  microservice.create
  microservice.deploy(gem_path, variables)
  @children << microservice_name if parent
  Logger.info "Configured microservice #{microservice_name}"
end

#deploy_unknown_packetlog_microservice(gem_path, variables, parent) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/openc3/models/scope_model.rb', line 286

def deploy_unknown_packetlog_microservice(gem_path, variables, parent)
  microservice_name = "#{@scope}__PACKETLOG__UNKNOWN"
  microservice = MicroserviceModel.new(
    name: microservice_name,
    cmd: ["ruby", "log_microservice.rb", microservice_name],
    work_dir: "/openc3/lib/openc3/microservices",
    options: [
      ["RAW_OR_DECOM", "RAW"],
      ["CMD_OR_TLM", "TLM"],
      ["CYCLE_TIME", "3600"] # Keep at most 1 hour per log
    ],
    topics: ["#{@scope}__TELEMETRY__{UNKNOWN}__UNKNOWN"],
    target_names: [],
    parent: parent,
    shard: @shard,
    scope: @scope
  )
  microservice.create
  microservice.deploy(gem_path, variables)
  @children << microservice_name if parent
  Logger.info "Configured microservice #{microservice_name}"
end

#destroyObject



193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/openc3/models/scope_model.rb', line 193

def destroy
  if @name != "DEFAULT"
    # Remove all the plugins for this scope
    plugins = PluginModel.get_all_models(scope: @name)
    plugins.each do |_plugin_name, plugin|
      plugin.destroy
    end
    super
  else
    raise "DEFAULT scope cannot be destroyed"
  end
end

#seed_databaseObject



452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/openc3/models/scope_model.rb', line 452

def seed_database
  setting = SettingModel.get(name: "source_url")
  SettingModel.set({name: "source_url", data: "https://github.com/OpenC3/cosmos"}, scope: @scope) unless setting
  setting = SettingModel.get(name: "rubygems_url")
  SettingModel.set({name: "rubygems_url", data: ENV["RUBYGEMS_URL"] || RubygemsUrl::DEFAULT}, scope: @scope) unless setting
  setting = SettingModel.get(name: "pypi_url")
  SettingModel.set({name: "pypi_url", data: ENV["PYPI_URL"] || "https://pypi.org"}, scope: @scope) unless setting
  # Default the news feed on, but only if nothing has set it yet. `openc3cli
  # initsettings` runs before the first plugin load creates this scope, so an
  # unconditional set here would discard OPENC3_SETTING_NEWS_FEED=false and
  # leave the seeded provenance record disagreeing with Redis forever.
  setting = SettingModel.get(name: "news_feed")
  SettingModel.set({name: "news_feed", data: true}, scope: @scope) unless setting

  setting = SettingModel.get(name: "system_health")
  # Settings are stored as JSON strings
  SettingModel.set({name: "system_health", data: JSON.generate(SYSTEM_HEALTH_DEFAULTS)}, scope: "DEFAULT") unless setting
end

#undeployObject



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/openc3/models/scope_model.rb', line 421

def undeploy
  # Delete UNKNOWN target
  target = TargetModel.get_model(name: "UNKNOWN", scope: @scope)
  target.destroy

  # Destroy all microservices in this scope
  microservices = MicroserviceModel.get_all_models(scope: @name)
  microservices.each do |_microservice_name, microservice|
    microservice.destroy
  end

  # Destroy all bridges in this scope
  bridges = BridgeModel.get_all_models(scope: @name)
  bridges.each do |_bridge_name, bridge|
    bridge.destroy
  end

  # Cleanup Topics
  if ENTERPRISE
    Topic.del("#{@scope}__openc3_autonomic")
    Topic.del("#{@scope}__TRIGGER__GROUP")
  end

  # Delete the topics we created for the scope
  db_shard = Store.db_shard_for_target('UNKNOWN', scope: @scope)
  Topic.del("#{@scope}__COMMAND__{UNKNOWN}__UNKNOWN", db_shard: db_shard)
  Topic.del("#{@scope}__TELEMETRY__{UNKNOWN}__UNKNOWN", db_shard: db_shard)
  Topic.del("#{@scope}__openc3_targets")
  Topic.del("#{@scope}__CONFIG")
end