Class: OpenC3::ScopeModel

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

Constant Summary collapse

PRIMARY_KEY =
'openc3_scopes'

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

#destroyed?, filter, find_all_by_plugin, get_all_models, handle_config, set, store, #update

Constructor Details

#initialize(name:, updated_at: nil, scope: nil) ⇒ ScopeModel

Returns a new instance of ScopeModel.



65
66
67
68
# File 'lib/openc3/models/scope_model.rb', line 65

def initialize(name:, updated_at: nil, scope: nil)
  super(PRIMARY_KEY, name: name, scope: name, updated_at: updated_at)
  @children = []
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



34
35
36
# File 'lib/openc3/models/scope_model.rb', line 34

def children
  @children
end

Class Method Details

.all(scope: nil) ⇒ Object



46
47
48
# File 'lib/openc3/models/scope_model.rb', line 46

def self.all(scope: nil)
  super(PRIMARY_KEY)
end

.from_json(json, scope: nil) ⇒ Object



50
51
52
53
54
# File 'lib/openc3/models/scope_model.rb', line 50

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



38
39
40
# File 'lib/openc3/models/scope_model.rb', line 38

def self.get(name:, scope: nil)
  super(PRIMARY_KEY, name: name)
end

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



56
57
58
59
60
61
62
63
# File 'lib/openc3/models/scope_model.rb', line 56

def self.get_model(name:, scope: nil)
  json = get(name: name)
  if json
    return from_json(json)
  else
    return nil
  end
end

.names(scope: nil) ⇒ Object



42
43
44
# File 'lib/openc3/models/scope_model.rb', line 42

def self.names(scope: nil)
  super(PRIMARY_KEY)
end

Instance Method Details

#as_json(*a) ⇒ Object



90
91
92
93
# File 'lib/openc3/models/scope_model.rb', line 90

def as_json(*a)
  { 'name' => @name,
    'updated_at' => @updated_at }
end

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



70
71
72
73
74
75
# File 'lib/openc3/models/scope_model.rb', line 70

def create(update: false, force: false)
  # Ensure there are no "." in the scope name - prevents gems accidently becoming scope names
  raise "Invalid scope name: #{@name}" if @name !~ /^[a-zA-Z0-9_-]+$/
  @name = @name.upcase
  super(update: update, force: force)
end

#deploy(gem_path, variables) ⇒ Object



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

def deploy(gem_path, variables)
  seed_database()

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

  # Create UNKNOWN target for display of unknown data
  model = TargetModel.new(name: "UNKNOWN", scope: @scope)
  model.create
  # Not deployed - we only want raw packet logging for UNKNOWN
  # TODO: Cleanup support

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

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

  # Notification Log Microservice
  deploy_openc3_notifications_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)

  # Periodic Microservice
  deploy_periodic_microservice(gem_path, variables, @parent)

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

#deploy_openc3_log_messages_microservice(gem_path, variables, parent) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/openc3/models/scope_model.rb', line 95

def deploy_openc3_log_messages_microservice(gem_path, variables, parent)
  microservice_name = "#{@scope}__OPENC3__LOG"
  microservice = MicroserviceModel.new(
    name: microservice_name,
    cmd: ["ruby", "text_log_microservice.rb", microservice_name],
    work_dir: '/openc3/lib/openc3/microservices',
    options: [
      # The following options are optional (600 and 50_000_000 are the defaults)
      # ["CYCLE_TIME", "600"], # Keep at most 10 minutes per log
      # ["CYCLE_SIZE", "50_000_000"] # Keep at most ~50MB per log
    ],
    topics: ["#{@scope}__openc3_log_messages"],
    parent: parent,
    scope: @scope
  )
  microservice.create
  microservice.deploy(gem_path, variables)
  @children << microservice_name if parent
  Logger.info "Configured microservice #{microservice_name}"
end

#deploy_openc3_notifications_microservice(gem_path, variables, parent) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/openc3/models/scope_model.rb', line 116

def deploy_openc3_notifications_microservice(gem_path, variables, parent)
  microservice_name = "#{@scope}__NOTIFICATION__LOG"
  microservice = MicroserviceModel.new(
    name: microservice_name,
    cmd: ["ruby", "text_log_microservice.rb", microservice_name],
    work_dir: '/openc3/lib/openc3/microservices',
    options: [
      # The following options are optional (600 and 50_000_000 are the defaults)
      ["CYCLE_TIME", "3600"], # Keep at most 1 hour per log
    ],
    topics: ["#{@scope}__openc3_notifications"],
    parent: parent,
    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



180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/openc3/models/scope_model.rb', line 180

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,
    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



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

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: [],
    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



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

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,
    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



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/openc3/models/scope_model.rb', line 158

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,
    scope: @scope
  )
  microservice.create
  microservice.deploy(gem_path, variables)
  @children << microservice_name if parent
  Logger.info "Configured microservice #{microservice_name}"
end

#destroyObject



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/openc3/models/scope_model.rb', line 77

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



272
273
274
275
276
277
# File 'lib/openc3/models/scope_model.rb', line 272

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: 'https://rubygems.org' }, scope: @scope) unless setting
end

#undeployObject



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/openc3/models/scope_model.rb', line 247

def undeploy
  model = MicroserviceModel.get_model(name: "#{@scope}__SCOPEMULTI__#{@scope}", scope: @scope)
  model.destroy if model
  model = MicroserviceModel.get_model(name: "#{@scope}__OPENC3__LOG", scope: @scope)
  model.destroy if model
  model = MicroserviceModel.get_model(name: "#{@scope}__NOTIFICATION__LOG", scope: @scope)
  model.destroy if model
  model = MicroserviceModel.get_model(name: "#{@scope}__COMMANDLOG__UNKNOWN", scope: @scope)
  model.destroy if model
  model = MicroserviceModel.get_model(name: "#{@scope}__PACKETLOG__UNKNOWN", scope: @scope)
  model.destroy if model
  model = MicroserviceModel.get_model(name: "#{@scope}__PERIODIC__#{@scope}", scope: @scope)
  model.destroy if model
  model = MicroserviceModel.get_model(name: "#{@scope}__TRIGGER_GROUP__DEFAULT", scope: @scope)
  model.destroy if model

  # Delete the topics we created for the scope
  Topic.del("#{@scope}__COMMAND__{UNKNOWN}__UNKNOWN")
  Topic.del("#{@scope}__TELEMETRY__{UNKNOWN}__UNKNOWN")
  Topic.del("#{@scope}__openc3_targets")
  Topic.del("#{@scope}__CONFIG")
  Topic.del("#{@scope}__openc3_autonomic")
  Topic.del("#{@scope}__TRIGGER__GROUP")
end