Class: Cosmos::ScopeModel

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

Constant Summary collapse

PRIMARY_KEY =
'cosmos_scopes'

Instance Attribute Summary

Attributes inherited from Model

#name, #plugin, #scope, #updated_at

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#as_config, #create, #destroy, filter, find_all_by_plugin, from_json, get_all_models, get_model, handle_config, set, store, #update

Constructor Details

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

Returns a new instance of ScopeModel.



43
44
45
# File 'lib/cosmos/models/scope_model.rb', line 43

def initialize(name:, updated_at: nil, scope: nil)
  super(PRIMARY_KEY, name: name, scope: name, updated_at: updated_at)
end

Class Method Details

.all(scope: nil) ⇒ Object



39
40
41
# File 'lib/cosmos/models/scope_model.rb', line 39

def self.all(scope: nil)
  super(PRIMARY_KEY)
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



31
32
33
# File 'lib/cosmos/models/scope_model.rb', line 31

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

.limits_set(scope:) ⇒ Object



147
148
149
# File 'lib/cosmos/models/scope_model.rb', line 147

def self.limits_set(scope:)
  Store.hget("#{scope}__cosmos_system", 'limits_set').intern
end

.names(scope: nil) ⇒ Object



35
36
37
# File 'lib/cosmos/models/scope_model.rb', line 35

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

Instance Method Details

#as_jsonObject



47
48
49
50
# File 'lib/cosmos/models/scope_model.rb', line 47

def as_json
  { 'name' => @name,
    'updated_at' => @updated_at }
end

#deploy(gem_path, variables) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/cosmos/models/scope_model.rb', line 52

def deploy(gem_path, variables)
  seed_database()

  # COSMOS Log Microservice
  microservice_name = "#{@scope}__COSMOS__LOG"
  microservice = MicroserviceModel.new(
    name: microservice_name,
    cmd: ["ruby", "text_log_microservice.rb", microservice_name],
    work_dir: '/cosmos/lib/cosmos/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}__cosmos_log_messages"],
    scope: @scope
  )
  microservice.create
  microservice.deploy(gem_path, variables)
  Logger.info "Configured microservice #{microservice_name}"

  # Notification Log Microservice
  microservice_name = "#{@scope}__NOTIFICATION__LOG"
  microservice = MicroserviceModel.new(
    name: microservice_name,
    cmd: ["ruby", "text_log_microservice.rb", microservice_name],
    work_dir: '/cosmos/lib/cosmos/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}__cosmos_notifications"],
    scope: @scope
  )
  microservice.create
  microservice.deploy(gem_path, variables)
  Logger.info "Configured microservice #{microservice_name}"

  # UNKNOWN CommandLog Microservice
  Topic.initialize_streams(["#{@scope}__COMMAND__{UNKNOWN}__UNKNOWN"])
  microservice_name = "#{@scope}__COMMANDLOG__UNKNOWN"
  microservice = MicroserviceModel.new(
    name: microservice_name,
    cmd: ["ruby", "log_microservice.rb", microservice_name],
    work_dir: '/cosmos/lib/cosmos/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: [],
    scope: @scope
  )
  microservice.create
  microservice.deploy(gem_path, variables)
  Logger.info "Configured microservice #{microservice_name}"

  # UNKNOWN PacketLog Microservice
  Topic.initialize_streams(["#{@scope}__TELEMETRY__{UNKNOWN}__UNKNOWN"])
  microservice_name = "#{@scope}__PACKETLOG__UNKNOWN"
  microservice = MicroserviceModel.new(
    name: microservice_name,
    cmd: ["ruby", "log_microservice.rb", microservice_name],
    work_dir: '/cosmos/lib/cosmos/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: [],
    scope: @scope
  )
  microservice.create
  microservice.deploy(gem_path, variables)
  Logger.info "Configured microservice #{microservice_name}"
end

#seed_databaseObject



142
143
144
145
# File 'lib/cosmos/models/scope_model.rb', line 142

def seed_database
  setting = SettingsModel.get(name: 'source_url')
  SettingsModel.set({ name: 'source_url', data: 'https://github.com/BallAerospace/COSMOS' }, scope: @scope) unless setting
end

#undeployObject



131
132
133
134
135
136
137
138
139
140
# File 'lib/cosmos/models/scope_model.rb', line 131

def undeploy
  model = MicroserviceModel.get_model(name: "#{@scope}__COSMOS__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
end