openc3 7.5.1 → 7.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +0 -0
  3. data/bin/openc3cli +82 -5
  4. data/data/config/_params.yaml +3 -0
  5. data/data/config/command_modifiers.yaml +5 -3
  6. data/data/config/default_plugins.yaml +1 -0
  7. data/data/config/interface_modifiers.yaml +17 -1
  8. data/data/config/item_modifiers.yaml +24 -1
  9. data/data/config/microservice.yaml +15 -1
  10. data/data/config/parameter_modifiers.yaml +4 -4
  11. data/data/config/plugins.yaml +511 -3
  12. data/data/config/settings.yaml +17 -0
  13. data/data/config/target.yaml +53 -0
  14. data/data/config/telemetry.yaml +58 -2
  15. data/data/config/widgets.yaml +30 -0
  16. data/lib/openc3/accessors/accessor.rb +14 -2
  17. data/lib/openc3/accessors/binary_accessor.rb +25 -5
  18. data/lib/openc3/api/CLAUDE.md +1 -0
  19. data/lib/openc3/api/api.rb +1 -0
  20. data/lib/openc3/api/cmd_api.rb +9 -1
  21. data/lib/openc3/api/interface_api.rb +7 -2
  22. data/lib/openc3/api/limits_api.rb +93 -41
  23. data/lib/openc3/api/router_api.rb +7 -2
  24. data/lib/openc3/api/settings_api.rb +4 -1
  25. data/lib/openc3/api/shared_resource_api.rb +107 -0
  26. data/lib/openc3/api/tlm_api.rb +10 -0
  27. data/lib/openc3/interfaces/http_client_interface.rb +84 -27
  28. data/lib/openc3/interfaces/protocols/preidentified_protocol.rb +6 -2
  29. data/lib/openc3/interfaces/tcpip_server_interface.rb +205 -125
  30. data/lib/openc3/io/json_api.rb +2 -10
  31. data/lib/openc3/io/json_api_object.rb +2 -10
  32. data/lib/openc3/logs/packet_log_writer.rb +8 -3
  33. data/lib/openc3/microservices/decom_common.rb +4 -1
  34. data/lib/openc3/microservices/decom_microservice.rb +52 -6
  35. data/lib/openc3/microservices/interface_decom_common.rb +13 -0
  36. data/lib/openc3/microservices/interface_microservice.rb +50 -11
  37. data/lib/openc3/microservices/limits_group_microservice.rb +513 -0
  38. data/lib/openc3/microservices/log_microservice.rb +22 -8
  39. data/lib/openc3/microservices/microservice.rb +8 -2
  40. data/lib/openc3/microservices/multi_microservice.rb +76 -22
  41. data/lib/openc3/microservices/queue_microservice.rb +29 -24
  42. data/lib/openc3/migrations/20260704000000_default_bridge.rb +1 -2
  43. data/lib/openc3/migrations/20261103000001_limits_group_backfill.rb +193 -0
  44. data/lib/openc3/models/bridge_model.rb +3 -3
  45. data/lib/openc3/models/cvt_model.rb +3 -1
  46. data/lib/openc3/models/host_interface_microservice_model.rb +4 -4
  47. data/lib/openc3/models/interface_model.rb +27 -7
  48. data/lib/openc3/models/microservice_group.rb +167 -0
  49. data/lib/openc3/models/microservice_model.rb +88 -2
  50. data/lib/openc3/models/panel_model.rb +109 -0
  51. data/lib/openc3/models/plugin_model.rb +1006 -26
  52. data/lib/openc3/models/plugin_store_model.rb +6 -1
  53. data/lib/openc3/models/protected_storage_model.rb +64 -0
  54. data/lib/openc3/models/queue_model.rb +19 -10
  55. data/lib/openc3/models/scope_model.rb +80 -3
  56. data/lib/openc3/models/setting_model.rb +2 -2
  57. data/lib/openc3/models/shared_resource_model.rb +381 -0
  58. data/lib/openc3/models/target_model.rb +324 -68
  59. data/lib/openc3/operators/microservice_operator.rb +76 -9
  60. data/lib/openc3/operators/operator.rb +29 -0
  61. data/lib/openc3/packets/commands.rb +16 -16
  62. data/lib/openc3/packets/limits.rb +2 -0
  63. data/lib/openc3/packets/packet.rb +75 -4
  64. data/lib/openc3/packets/packet_config.rb +152 -13
  65. data/lib/openc3/packets/packet_item.rb +35 -14
  66. data/lib/openc3/packets/packet_item_limits.rb +67 -6
  67. data/lib/openc3/packets/parsers/limits_parser.rb +69 -2
  68. data/lib/openc3/packets/parsers/packet_item_parser.rb +6 -0
  69. data/lib/openc3/packets/parsers/packet_parser.rb +45 -0
  70. data/lib/openc3/packets/parsers/state_parser.rb +1 -1
  71. data/lib/openc3/script/CLAUDE.md +1 -0
  72. data/lib/openc3/script/api_shared.rb +17 -9
  73. data/lib/openc3/script/calendar.rb +18 -6
  74. data/lib/openc3/script/commands.rb +11 -9
  75. data/lib/openc3/script/critical_cmd.rb +22 -2
  76. data/lib/openc3/script/metadata.rb +30 -4
  77. data/lib/openc3/script/plugins.rb +8 -0
  78. data/lib/openc3/script/script.rb +8 -24
  79. data/lib/openc3/script/web_socket_api.rb +23 -17
  80. data/lib/openc3/system/system.rb +9 -3
  81. data/lib/openc3/top_level.rb +17 -0
  82. data/lib/openc3/topics/command_topic.rb +7 -1
  83. data/lib/openc3/topics/decom_interface_topic.rb +9 -3
  84. data/lib/openc3/topics/discovery_topic.rb +84 -0
  85. data/lib/openc3/topics/limits_event_topic.rb +236 -17
  86. data/lib/openc3/topics/telemetry_decom_topic.rb +12 -3
  87. data/lib/openc3/topics/topic.rb +2 -1
  88. data/lib/openc3/utilities/authentication.rb +67 -16
  89. data/lib/openc3/utilities/cli_generator.rb +117 -25
  90. data/lib/openc3/utilities/client_ip_header_filter.rb +38 -0
  91. data/lib/openc3/utilities/config_overlay.rb +24 -1
  92. data/lib/openc3/utilities/cosmos_rails_formatter.rb +18 -10
  93. data/lib/openc3/utilities/hardware_discovery.rb +279 -0
  94. data/lib/openc3/utilities/limits_rollup.rb +81 -0
  95. data/lib/openc3/utilities/local_mode.rb +48 -5
  96. data/lib/openc3/utilities/questdb_client.rb +130 -22
  97. data/lib/openc3/utilities/reingest_job.rb +12 -3
  98. data/lib/openc3/utilities/running_script.rb +206 -49
  99. data/lib/openc3/utilities/script.rb +24 -12
  100. data/lib/openc3/utilities/script_env.rb +164 -0
  101. data/lib/openc3/utilities/script_message_signing.rb +133 -0
  102. data/lib/openc3/utilities/store_autoload.rb +32 -1
  103. data/lib/openc3/utilities/store_queued.rb +5 -1
  104. data/lib/openc3/version.rb +6 -6
  105. data/templates/command_validator/command_validator.py +9 -9
  106. data/templates/{tool_react/pnpm-workspace.yaml → panel/.prettierrc.cjs} +7 -5
  107. data/templates/panel/Rakefile +24 -0
  108. data/templates/panel/package.json +17 -0
  109. data/templates/{tool_angular → panel}/pnpm-workspace.yaml +5 -0
  110. data/templates/panel/src/Panel.js +52 -0
  111. data/templates/panel/src/Panel.vue +60 -0
  112. data/templates/panel/src/panelSdk.js +24 -0
  113. data/templates/panel/vite-Panel.config.js +43 -0
  114. data/templates/tool_vue/package.json +3 -3
  115. data/templates/widget/package.json +2 -2
  116. metadata +24 -56
  117. data/templates/tool_angular/.editorconfig +0 -16
  118. data/templates/tool_angular/.gitignore +0 -44
  119. data/templates/tool_angular/.vscode/extensions.json +0 -4
  120. data/templates/tool_angular/.vscode/launch.json +0 -20
  121. data/templates/tool_angular/.vscode/tasks.json +0 -42
  122. data/templates/tool_angular/angular.json +0 -111
  123. data/templates/tool_angular/extra-webpack.config.js +0 -8
  124. data/templates/tool_angular/package.json +0 -48
  125. data/templates/tool_angular/src/app/app-routing.module.ts +0 -15
  126. data/templates/tool_angular/src/app/app.component.html +0 -22
  127. data/templates/tool_angular/src/app/app.component.scss +0 -18
  128. data/templates/tool_angular/src/app/app.component.spec.ts +0 -29
  129. data/templates/tool_angular/src/app/app.component.ts +0 -52
  130. data/templates/tool_angular/src/app/app.module.ts +0 -30
  131. data/templates/tool_angular/src/app/custom-overlay-container.ts +0 -17
  132. data/templates/tool_angular/src/app/empty-route/empty-route.component.ts +0 -7
  133. data/templates/tool_angular/src/app/openc3-api.d.ts +0 -1
  134. data/templates/tool_angular/src/assets/.gitkeep +0 -0
  135. data/templates/tool_angular/src/environments/environment.prod.ts +0 -3
  136. data/templates/tool_angular/src/environments/environment.ts +0 -16
  137. data/templates/tool_angular/src/favicon.ico +0 -0
  138. data/templates/tool_angular/src/index.html +0 -13
  139. data/templates/tool_angular/src/main.single-spa.ts +0 -40
  140. data/templates/tool_angular/src/single-spa/asset-url.ts +0 -12
  141. data/templates/tool_angular/src/single-spa/single-spa-props.ts +0 -8
  142. data/templates/tool_angular/src/styles.scss +0 -1
  143. data/templates/tool_angular/tsconfig.app.json +0 -13
  144. data/templates/tool_angular/tsconfig.json +0 -33
  145. data/templates/tool_angular/tsconfig.spec.json +0 -14
  146. data/templates/tool_react/.eslintrc +0 -7
  147. data/templates/tool_react/.gitignore +0 -71
  148. data/templates/tool_react/.prettierignore +0 -8
  149. data/templates/tool_react/babel.config.json +0 -29
  150. data/templates/tool_react/jest.config.js +0 -12
  151. data/templates/tool_react/package.json +0 -51
  152. data/templates/tool_react/src/openc3-tool_name.js +0 -24
  153. data/templates/tool_react/src/root.component.js +0 -88
  154. data/templates/tool_react/src/root.component.test.js +0 -9
  155. data/templates/tool_react/webpack.config.js +0 -27
  156. data/templates/tool_svelte/.gitignore +0 -71
  157. data/templates/tool_svelte/.prettierignore +0 -8
  158. data/templates/tool_svelte/.prettierrc.js +0 -5
  159. data/templates/tool_svelte/babel.config.js +0 -12
  160. data/templates/tool_svelte/build/smui.css +0 -1
  161. data/templates/tool_svelte/jest.config.js +0 -9
  162. data/templates/tool_svelte/package.json +0 -49
  163. data/templates/tool_svelte/pnpm-workspace.yaml +0 -20
  164. data/templates/tool_svelte/rollup.config.js +0 -74
  165. data/templates/tool_svelte/src/App.svelte +0 -42
  166. data/templates/tool_svelte/src/App.test.js +0 -9
  167. data/templates/tool_svelte/src/theme/_smui-theme.scss +0 -25
  168. data/templates/tool_svelte/src/tool_name.js +0 -17
  169. data/templates/widget/LICENSE.md +0 -20
@@ -19,6 +19,7 @@ require 'openc3/microservices/microservice'
19
19
  require 'openc3/topics/topic'
20
20
  require 'openc3/logs/buffered_packet_log_writer'
21
21
  require 'openc3/config/config_parser'
22
+ require 'openc3/utilities/sleeper'
22
23
 
23
24
  module OpenC3
24
25
  class LogMicroservice < Microservice
@@ -60,18 +61,30 @@ module OpenC3
60
61
  def run
61
62
  setup_plws()
62
63
  setup_microservice_topic()
64
+ @sleeper = Sleeper.new
63
65
  while true
64
66
  break if @cancel_thread
65
67
 
66
- Topic.read_topics(@topics, db_shard: @db_shard) do |topic, msg_id, msg_hash, redis|
67
- break if @cancel_thread
68
- if topic == @microservice_topic
69
- microservice_cmd(topic, msg_id, msg_hash, redis)
70
- else
71
- log_data(topic, msg_id, msg_hash, redis)
68
+ begin
69
+ Topic.read_topics(@topics, db_shard: @db_shard) do |topic, msg_id, msg_hash, redis|
70
+ break if @cancel_thread
71
+ if topic == @microservice_topic
72
+ microservice_cmd(topic, msg_id, msg_hash, redis)
73
+ else
74
+ log_data(topic, msg_id, msg_hash, redis)
75
+ end
76
+ @count += 1
77
+ @metric.set(name: 'log_total', value: @count, type: 'counter')
72
78
  end
73
- @count += 1
74
- @metric.set(name: 'log_total', value: @count, type: 'counter')
79
+ rescue => e
80
+ # Keep logging after an error reading the topics (e.g. Redis briefly
81
+ # unavailable) like decom does. A dead thread would stop the process,
82
+ # which in a MULTI or MULTIGROUP stops every microservice in it.
83
+ @error = e
84
+ @error_count += 1
85
+ @metric.set(name: 'log_error_total', value: @error_count, type: 'counter')
86
+ @logger.error("#{@name} error reading topics: #{e.formatted}")
87
+ break if @sleeper.sleep(1) # Don't spin while the error persists
75
88
  end
76
89
  end
77
90
  end
@@ -121,6 +134,7 @@ module OpenC3
121
134
  end
122
135
 
123
136
  def shutdown
137
+ @sleeper&.cancel
124
138
  # Make sure all the existing logs are properly closed down
125
139
  threads = []
126
140
  @plws.each do |_target_name, plw_hash|
@@ -133,8 +133,7 @@ module OpenC3
133
133
  # Get configuration for any targets
134
134
  @target_names = @config["target_names"]
135
135
  @target_names ||= []
136
- # NOTE: setup_targets doesn't do anything if @target_names is empty
137
- System.setup_targets(@target_names, @temp_dir, scope: @scope, descriptions: load_descriptions?) unless is_plugin
136
+ setup_system() unless is_plugin
138
137
 
139
138
  # Use at_exit to shutdown cleanly no matter how we die
140
139
  at_exit do
@@ -238,6 +237,13 @@ module OpenC3
238
237
  true
239
238
  end
240
239
 
240
+ # Set up System for this microservice's targets (an empty System if it has
241
+ # none). System is one per process and the first setup decides which
242
+ # targets it has, so subclasses that set it up later override this.
243
+ def setup_system
244
+ System.setup_targets(@target_names, @temp_dir, scope: @scope, descriptions: load_descriptions?)
245
+ end
246
+
241
247
  def shutdown(state = 'STOPPED')
242
248
  return if @shutdown_complete
243
249
  begin
@@ -14,35 +14,89 @@
14
14
  require 'openc3/microservices/microservice'
15
15
  require 'openc3/topics/topic'
16
16
  require 'openc3/utilities/thread_manager'
17
+ require 'openc3/utilities/sleeper'
17
18
 
18
19
  module OpenC3
20
+ # Runs other microservices as threads in one process. They are given on the
21
+ # command line, or if none are given (as for a MICROSERVICE_GROUP's
22
+ # MULTIGROUP) they are the microservices that name this one as their parent.
19
23
  class MultiMicroservice < Microservice
24
+ def setup_system
25
+ # Intentionally empty. This microservice has no targets of its own, so
26
+ # leave System for run to set up for its children's targets.
27
+ end
28
+
29
+ def child_models
30
+ names = ARGV.dup
31
+ if names.empty?
32
+ names = MicroserviceModel.all(scope: @scope).values.select { |config| config['parent'] == @name }.map { |config| config['name'] }.sort
33
+ end
34
+ names.map { |name| MicroserviceModel.get_model(name: name, scope: @scope) }.compact.select(&:enabled)
35
+ end
36
+
20
37
  def run
21
- ARGV.each do |microservice_name|
22
- microservice_model = MicroserviceModel.get_model(name: microservice_name, scope: @scope)
23
- if microservice_model and microservice_model.enabled
24
- thread = Thread.new do
25
- cmd_line = microservice_model.cmd.join(' ')
26
- split_cmd_line = cmd_line.split(' ')
27
- filename = nil
28
- split_cmd_line.each do |item|
29
- if File.extname(item) == '.rb'
30
- filename = item
31
- break
32
- end
33
- end
34
- raise "Could not determine class filename from '#{cmd_line}'" unless filename
35
- OpenC3.set_working_dir(microservice_model.work_dir) do
36
- require File.join(microservice_model.work_dir, filename)
38
+ models = child_models()
39
+ # System is one per process and the first microservice to set it up
40
+ # decides which targets it has, so set it up for every child first
41
+ target_names = models.map(&:target_names).flatten.compact.uniq
42
+ System.setup_targets(target_names, @temp_dir, scope: @scope, descriptions: false)
43
+ self.class.ensure_pool_sizes(models)
44
+
45
+ models.each do |microservice_model|
46
+ thread = Thread.new do
47
+ cmd_line = microservice_model.cmd.join(' ')
48
+ split_cmd_line = cmd_line.split(' ')
49
+ filename = nil
50
+ split_cmd_line.each do |item|
51
+ if File.extname(item) == '.rb'
52
+ filename = item
53
+ break
37
54
  end
38
- klass = filename.filename_to_class_name.to_class
39
- klass.run(microservice_model.name)
40
55
  end
41
- ThreadManager.instance.register(thread)
56
+ raise "Could not determine class filename from '#{cmd_line}'" unless filename
57
+
58
+ OpenC3.set_working_dir(microservice_model.work_dir) do
59
+ require File.join(microservice_model.work_dir, filename)
60
+ end
61
+ klass = filename.filename_to_class_name.to_class
62
+ klass.run(microservice_model.name)
42
63
  end
64
+ ThreadManager.instance.register(thread)
65
+ end
66
+ if models.empty?
67
+ # Nothing to run until the operator restarts this with children
68
+ # Stop when the sleep is canceled too. shutdown cancels it before it
69
+ # sets @cancel_thread, and a canceled Sleeper can't sleep again.
70
+ @sleeper = Sleeper.new
71
+ until @cancel_thread
72
+ break if @sleeper.sleep(1)
73
+ end
74
+ else
75
+ ThreadManager.instance.monitor
76
+ ThreadManager.instance.shutdown
77
+ end
78
+ end
79
+
80
+ def shutdown
81
+ @sleeper&.cancel
82
+ super()
83
+ end
84
+
85
+ # Redis connections every child may use at once: one held while it reads
86
+ # its topics and one for other calls
87
+ CONNECTIONS_PER_CHILD = 2
88
+ DEFAULT_POOL_SIZE = 100
89
+
90
+ # The children share this process's Redis connection pools, which are sized
91
+ # for one microservice. Each child holds a connection while it reads its
92
+ # topics, so with too many children some can't get one in time and die.
93
+ def self.ensure_pool_sizes(models)
94
+ pool_size = DEFAULT_POOL_SIZE + (models.length * CONNECTIONS_PER_CHILD)
95
+ db_shards = ([0] + models.map { |model| model.db_shard.to_i }).uniq
96
+ db_shards.each do |db_shard|
97
+ Store.instance(db_shard: db_shard).ensure_pool_size(pool_size)
98
+ EphemeralStore.instance(db_shard: db_shard).ensure_pool_size(pool_size)
43
99
  end
44
- ThreadManager.instance.monitor
45
- ThreadManager.instance.shutdown
46
100
  end
47
101
  end
48
102
  end
@@ -50,4 +104,4 @@ if __FILE__ == $0
50
104
  OpenC3::MultiMicroservice.run
51
105
  OpenC3::ThreadManager.instance.shutdown
52
106
  OpenC3::ThreadManager.instance.join
53
- end
107
+ end
@@ -18,17 +18,6 @@ require 'openc3/utilities/authentication'
18
18
  require 'openc3/api/api'
19
19
 
20
20
  module OpenC3
21
- saved_verbose = $VERBOSE
22
- $VERBOSE = nil
23
- module Script
24
- private
25
- # Override the prompt_for_hazardous method to always return true since there is no user to prompt
26
- def prompt_for_hazardous(target_name, cmd_name, hazardous_description)
27
- return true
28
- end
29
- end
30
- $VERBOSE = saved_verbose
31
-
32
21
  # The queue processor runs in a single thread and processes commands via cmd_api.
33
22
  class QueueProcessor
34
23
  include Api # Provides access to api methods
@@ -58,14 +47,14 @@ module OpenC3
58
47
  def process_queued_commands
59
48
  while @state == 'RELEASE'
60
49
  begin
50
+ args = nil
61
51
  _queue_name, command_data, _timestamp = Store.bzpopmin("#{@scope}:#{@name}", timeout: 0.2)
62
52
  if command_data
63
53
  command = JSON.parse(command_data)
64
- # It's important to set queue: false here to avoid infinite recursion when
65
- # OPENC3_DEFAULT_QUEUE is set because commands would be re-queued to the default queue
66
- # NOTE: cmd() via script rescues hazardous errors and calls prompt_for_hazardous()
67
- # but we've overridden it to always return true and go straight to cmd_no_hazardous_check()
68
-
54
+ extra = command['extra']
55
+ if extra.is_a?(String)
56
+ extra = JSON.parse(extra, allow_nan: true, create_additions: true)
57
+ end
69
58
  # Support both new format (target_name, cmd_name, cmd_params) and legacy format (command string)
70
59
  if command['target_name'] && command['cmd_name']
71
60
  # New format: use 3-parameter cmd() method
@@ -74,20 +63,36 @@ module OpenC3
74
63
  else
75
64
  cmd_params = {}
76
65
  end
77
- validate = command.key?('validate') ? command['validate'] : true
78
- timeout = command['timeout']
79
- # Pass queue_username so Command History is attributed to the original
80
- # author rather than the queue microservice name
81
- cmd(command['target_name'], command['cmd_name'], cmd_params, queue: false, validate: validate, timeout: timeout, queue_username: command['username'], scope: @scope)
66
+ args = [command['target_name'], command['cmd_name'], cmd_params]
82
67
  elsif command['value']
83
68
  # Legacy format: use single string parameter for backwards compatibility
84
- validate = command.key?('validate') ? command['validate'] : true
85
- timeout = command['timeout']
86
- cmd(command['value'], queue: false, validate: validate, timeout: timeout, queue_username: command['username'], scope: @scope)
69
+ args = [command['value']]
87
70
  else
88
71
  @logger.error "QueueProcessor: Invalid command format, missing required fields"
89
72
  end
90
73
  end
74
+ if args
75
+ # It's important to set queue: false here to avoid infinite recursion when
76
+ # OPENC3_DEFAULT_QUEUE is set because commands would be re-queued to the default queue.
77
+ # Pass queue_username so Command History is attributed to the original
78
+ # author rather than the queue microservice name.
79
+ kwargs = {
80
+ queue: false,
81
+ validate: command.key?('validate') ? command['validate'] : true,
82
+ timeout: command['timeout'],
83
+ queue_username: command['username'],
84
+ extra: extra,
85
+ scope: @scope
86
+ }
87
+ begin
88
+ cmd(*args, **kwargs)
89
+ rescue HazardousError
90
+ # Releasing the queue approves its commands and there is no user to prompt,
91
+ # so resend hazardous commands without the hazardous check. This matches
92
+ # executing a single command in QueuesController#exec_command.
93
+ cmd_no_hazardous_check(*args, **kwargs)
94
+ end
95
+ end
91
96
  rescue StandardError => e
92
97
  @logger.error "QueueProcessor failed to process command from queue #{@name}\n#{e.message}"
93
98
  end
@@ -16,7 +16,7 @@ require 'openc3/models/bridge_model'
16
16
 
17
17
  module OpenC3
18
18
  # Ensure every existing scope has a DEFAULT bridge_microservice (the Iroh hub
19
- # that lets openc3-app run COSMOS interfaces on the host). New scopes get one
19
+ # that lets openc3-cosmos-app run COSMOS interfaces on the host). New scopes get one
20
20
  # via ScopeModel#deploy; this backfills any scope created before that.
21
21
  class DefaultBridge < Migration
22
22
  def self.run
@@ -26,7 +26,6 @@ module OpenC3
26
26
 
27
27
  microservice = BridgeModel.build_microservice(bridge_name: "DEFAULT", scope: scope, shard: scope_model.shard)
28
28
  microservice.create
29
- microservice.deploy
30
29
  Logger.info("Added DEFAULT bridge_microservice to scope #{scope}")
31
30
  end
32
31
  end
@@ -0,0 +1,193 @@
1
+ # Copyright 2026 OpenC3, Inc.
2
+ # All Rights Reserved.
3
+ #
4
+ # This program is distributed in the hope that it will be useful,
5
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
6
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7
+ # See LICENSE.md for more details.
8
+
9
+ # This file may also be used under the terms of a commercial license
10
+ # if purchased from OpenC3, Inc.
11
+
12
+ require 'openc3/utilities/migration'
13
+ require 'openc3/models/scope_model'
14
+ require 'openc3/models/target_model'
15
+ require 'openc3/models/microservice_model'
16
+ require 'openc3/topics/limits_event_topic'
17
+
18
+ module OpenC3
19
+ # Backfill limits groups as telemetry for scopes created before it existed:
20
+ # - Fail before changing anything if a target has a telemetry packet of its own
21
+ # named LIMITS_GROUP, which is now reserved (see check_limits_group_packets)
22
+ # - Store each limits group under the target which defines it (TARGET__GROUP)
23
+ # rather than just its name. Groups whose names can't become LIMITS_GROUP
24
+ # items are removed with a warning, like installing skips them.
25
+ # - Define the <TARGET> LIMITS_GROUP packet for each target with limits groups
26
+ # and add it to the target's TSDB microservice
27
+ # - Add the limits group microservice to the scope's SCOPEMULTI
28
+ # New scopes get the microservice via ScopeModel#deploy and installing a target
29
+ # stores its groups and defines its packet.
30
+ class LimitsGroupBackfill < Migration
31
+ def self.run
32
+ scopes = ScopeModel.get_all_models(scope: nil)
33
+ check_limits_group_packets(scopes.keys)
34
+ scopes.each do |scope, scope_model|
35
+ backfill_targets(scope)
36
+ add_microservice(scope, scope_model)
37
+ end
38
+ end
39
+
40
+ # LIMITS_GROUP is now a reserved telemetry packet name. Raising fails the
41
+ # upgrade before anything is changed and the migration runs again on the
42
+ # next start, so the targets can be fixed on the previous version first.
43
+ def self.check_limits_group_packets(scopes)
44
+ targets = []
45
+ scopes.each do |scope|
46
+ TargetModel.get_all_models(scope: scope).each do |target_name, model|
47
+ json = Store.instance(db_shard: model.db_shard).hget("#{scope}__openc3tlm__#{target_name}", PacketConfig::LIMITS_GROUP_PACKET)
48
+ next unless json
49
+
50
+ # A previous run which failed later on already stored the generated packet.
51
+ # config_name hashes the name, layout and states of every item.
52
+ groups = model.limits_groups || []
53
+ next if !groups.empty? and JSON.parse(json, allow_nan: true, create_additions: true)['config_name'] == PacketConfig.generate_limits_group_packet(target_name, groups).config_name
54
+
55
+ targets << "#{target_name} (scope #{scope})"
56
+ end
57
+ end
58
+ return if targets.empty?
59
+
60
+ raise "Telemetry packet name #{PacketConfig::LIMITS_GROUP_PACKET} is now reserved for the limits groups a target defines, but these targets have a telemetry packet named #{PacketConfig::LIMITS_GROUP_PACKET}: #{targets.join(', ')}. Go back to the previous COSMOS version, rename the packet in each target's telemetry definition, reinstall the plugins, then upgrade again."
61
+ end
62
+
63
+ # Every step can run again: the migration is only recorded once every scope
64
+ # is done so a failure (e.g. in add_microservice) reruns the whole thing.
65
+ def self.backfill_targets(scope)
66
+ models = TargetModel.get_all_models(scope: scope)
67
+ rekey_groups(scope, models)
68
+ stored = Store.hkeys("#{scope}__limits_groups").to_set
69
+ models.each do |target_name, model|
70
+ group_names = valid_group_names(model, stored)
71
+ next if group_names.empty?
72
+
73
+ # Storing the packet again on a rerun is harmless (see add_limits_group_packet)
74
+ add_limits_group_packet(model, PacketConfig.generate_limits_group_packet(target_name, group_names))
75
+ end
76
+ end
77
+
78
+ # Before groups became items in <TARGET> LIMITS_GROUP any name was allowed.
79
+ # Drop the ones which can't, and the ones rekey_groups couldn't give to a
80
+ # single target, which it removed from the store.
81
+ #
82
+ # @param stored [Set<String>] The group keys in the store
83
+ # @return [Array<String>] The target's group names which can be LIMITS_GROUP items
84
+ def self.valid_group_names(model, stored)
85
+ group_names = model.limits_groups || []
86
+ valid = group_names.select do |group|
87
+ !PacketConfig.limits_group_name_error(group) and stored.include?(LimitsEventTopic.limits_group_key(model.name, group))
88
+ end
89
+ if valid.length != group_names.length
90
+ model.limits_groups = valid
91
+ model.update
92
+ end
93
+ valid
94
+ end
95
+
96
+ # Store each group under the target which defines it. Names could contain
97
+ # '__' before groups were owned by a target so a legacy name can be a key
98
+ # written here (e.g. INST's group SYSTEM__TEMPS and SYSTEM's group TEMPS).
99
+ # The two can't be told apart afterwards so the new keys, the deletes and a
100
+ # marker are written together and a rerun skips a scope already done.
101
+ def self.rekey_groups(scope, models)
102
+ done_key = "#{scope}__limits_groups_rekeyed"
103
+ return if Store.get(done_key)
104
+
105
+ groups = Store.hgetall("#{scope}__limits_groups")
106
+ # Before groups were owned by a target they were stored by name alone
107
+ owners = Hash.new { |hash, group| hash[group] = [] }
108
+ models.each do |target_name, model|
109
+ (model.limits_groups || []).each do |group|
110
+ if groups[group]
111
+ owners[group] << target_name
112
+ else
113
+ # e.g. another target defined it too and uninstalling that one deleted it
114
+ Logger.warn("Removed LIMITS_GROUP #{group} of target #{target_name} in scope #{scope}: its definition isn't stored. Reinstall the target to use it.")
115
+ end
116
+ end
117
+ end
118
+ rekeyed = {}
119
+ legacy = Set.new
120
+ # A stored group no target defines would be read as TARGET__GROUP if its name
121
+ # contains '__' so remove it if it can't be a group. Others stay ownerless.
122
+ groups.each_key do |group|
123
+ next if owners.key?(group) or !(error = PacketConfig.limits_group_name_error(group))
124
+
125
+ Logger.warn("Removed LIMITS_GROUP #{group} in scope #{scope}: no target defines it and the name #{error}.")
126
+ legacy << group
127
+ end
128
+ owners.each do |group, target_names|
129
+ legacy << group
130
+ target_names.each do |target_name|
131
+ if (error = PacketConfig.limits_group_name_error(group))
132
+ Logger.warn("Removed LIMITS_GROUP #{group} of target #{target_name} in scope #{scope}: the name #{error}. Rename the group and reinstall the target to use it.")
133
+ elsif target_names.length > 1
134
+ # Each install overwrote the group so only the last one's items were kept
135
+ Logger.warn("Removed LIMITS_GROUP #{group} of target #{target_name} in scope #{scope}: targets #{target_names.sort.join(', ')} all define it but only one of their definitions was stored. Reinstall the target to use it.")
136
+ else
137
+ rekeyed[LimitsEventTopic.limits_group_key(target_name, group)] = groups[group]
138
+ end
139
+ end
140
+ end
141
+ legacy.subtract(rekeyed.keys)
142
+ Store.multi do |multi|
143
+ rekeyed.each { |key, items| multi.hset("#{scope}__limits_groups", key, items) }
144
+ multi.hdel("#{scope}__limits_groups", *legacy.to_a) unless legacy.empty?
145
+ multi.set(done_key, 'true')
146
+ end
147
+ end
148
+
149
+ # Installing a target adds this packet like any other
150
+ # (see PacketConfig#build_limits_group_packet)
151
+ def self.add_limits_group_packet(model, packet)
152
+ TargetModel.set_packet(model.name, packet.packet_name, packet, scope: model.scope)
153
+ # Seed the CVT like TargetModel#update_store_telemetry so the packet can be read
154
+ # before the limits group microservice publishes it. HSETNX never overwrites values.
155
+ json_hash = packet.sorted_items.to_h { |item| [item.name, nil] }
156
+ Store.instance(db_shard: model.db_shard).hsetnx("#{model.scope}__tlm__#{model.name}", packet.packet_name, JSON.generate(json_hash, allow_nan: true))
157
+ packet.sorted_items.each { |item| TargetModel.add_to_target_allitems_list(model.name, item.name, scope: model.scope) }
158
+ model.update_store_item_map
159
+ # Installing subscribes the TSDB microservice to every packet's DECOM topic.
160
+ # Like TargetModel#dynamic_update tell a running one and update its model.
161
+ tsdb = "#{model.scope}__TSDB__#{model.name}"
162
+ if MicroserviceModel.get_model(name: tsdb, scope: model.scope)
163
+ topics = ["#{model.scope}__DECOM__{#{model.name}}__#{packet.packet_name}"]
164
+ Topic.write_topic("MICROSERVICE__#{tsdb}", { 'command' => 'ADD_TOPICS', 'topics' => topics.to_json }, db_shard: model.db_shard)
165
+ model.add_topics_to_microservice(tsdb, topics)
166
+ end
167
+ end
168
+
169
+ def self.add_microservice(scope, scope_model)
170
+ parent = "#{scope}__SCOPEMULTI__#{scope}"
171
+ options = ScopeModel.limits_group_microservice_options(scope, scope_model.shard, parent)
172
+ created = false
173
+ unless MicroserviceModel.get_model(name: options[:name], scope: scope)
174
+ MicroserviceModel.new(**options).create
175
+ created = true
176
+ end
177
+ # SCOPEMULTI starts its children from its command line so add this one.
178
+ # Updating the model makes the operator restart SCOPEMULTI. Checked even
179
+ # when the child exists in case a previous run stopped before this.
180
+ multi = MicroserviceModel.get_model(name: parent, scope: scope)
181
+ if multi and !multi.cmd.include?(options[:name])
182
+ multi.cmd << options[:name]
183
+ multi.update
184
+ created = true
185
+ end
186
+ Logger.info("Added limits group microservice to scope #{scope}") if created
187
+ end
188
+ end
189
+ end
190
+
191
+ unless ENV['OPENC3_NO_MIGRATE']
192
+ OpenC3::LimitsGroupBackfill.run
193
+ end
@@ -20,9 +20,9 @@ require 'securerandom'
20
20
  module OpenC3
21
21
  # Stores the identity of a named Iroh bridge (the bridge_microservice hub).
22
22
  #
23
- # A bridge is the COSMOS-side Iroh server that openc3-app and host interfaces
23
+ # A bridge is the COSMOS-side Iroh server that openc3-cosmos-app and host interfaces
24
24
  # connect to. This model stores the bridge's Iroh public key (its EndpointId)
25
- # and most recent connection ticket, plus the authorized openc3-app control
25
+ # and most recent connection ticket, plus the authorized openc3-cosmos-app control
26
26
  # identity (app_public_key) recorded during enrollment. The bridge's own
27
27
  # private key lives in the secrets store, never here.
28
28
  #
@@ -118,7 +118,7 @@ module OpenC3
118
118
 
119
119
  # Generate a one-time manual-enrollment code, store it on this bridge, and
120
120
  # return a token (base64 JSON of bridge name + hub ticket + code) that a
121
- # remote openc3-app pastes to enroll. Requires the bridge to be up (ticket
121
+ # remote openc3-cosmos-app pastes to enroll. Requires the bridge to be up (ticket
122
122
  # present). The code is redeemed once over the api/enroll ALPN.
123
123
  def generate_enrollment_token
124
124
  raise "Bridge '#{@name}' has no ticket yet; is its bridge_microservice running?" if @ticket.nil? || @ticket.empty?
@@ -184,7 +184,9 @@ module OpenC3
184
184
  end
185
185
  # If we were able to find a value, try to get the limits state
186
186
  if item_result[0]
187
- if now - hash['RECEIVED_TIMESECONDS'] > stale_time
187
+ # LIMITS_GROUP packets are only published when a value changes so their
188
+ # age says nothing about whether they are current. Never mark them stale.
189
+ if packet_name != 'LIMITS_GROUP' and now - hash['RECEIVED_TIMESECONDS'] > stale_time
188
190
  item_result[1] = :STALE
189
191
  else
190
192
  # The last key is simply the name (RAW) so we can append __L
@@ -14,16 +14,16 @@
14
14
  require 'openc3/models/model'
15
15
 
16
16
  module OpenC3
17
- # Captures the information openc3-app needs to spawn a COSMOS interface on the
17
+ # Captures the information openc3-cosmos-app needs to spawn a COSMOS interface on the
18
18
  # host computer (outside Docker) so it can reach hardware such as serial ports
19
19
  # that aren't available inside a container. A host microservice performs raw
20
20
  # data transfer and basic functionality only; complex processing (protocols,
21
21
  # target definitions) stays on the Docker side of COSMOS. The host side speaks
22
- # Iroh back to openc3-app which forwards the data into COSMOS through the named
22
+ # Iroh back to openc3-cosmos-app which forwards the data into COSMOS through the named
23
23
  # bridge_microservice relay.
24
24
  #
25
25
  # This model is written by InterfaceModel#deploy for bridged interfaces (those
26
- # configured with the BRIDGE keyword) and read by openc3-app. It is deliberately
26
+ # configured with the BRIDGE keyword) and read by openc3-cosmos-app. It is deliberately
27
27
  # kept separate from MicroserviceModel so it is NOT run by the normal COSMOS
28
28
  # microservice operator.
29
29
  class HostInterfaceMicroserviceModel < Model
@@ -42,7 +42,7 @@ module OpenC3
42
42
  # The bridge (bridge_microservice relay) this host interface routes through.
43
43
  attr_accessor :bridge_name
44
44
  # The Iroh ALPN stream name (the interface name) used to route data back to
45
- # openc3-app and on to the matching COSMOS bridge_interface.
45
+ # openc3-cosmos-app and on to the matching COSMOS bridge_interface.
46
46
  attr_accessor :stream
47
47
 
48
48
  # NOTE: The following three class methods are reimplemented (like other
@@ -58,6 +58,7 @@ module OpenC3
58
58
  attr_accessor :shard
59
59
  attr_accessor :db_shard
60
60
  attr_accessor :bridge_name
61
+ attr_accessor :shared_resource
61
62
 
62
63
  # NOTE: The following three class methods are used by the ModelController
63
64
  # and are reimplemented to enable various Model class methods to work
@@ -137,6 +138,7 @@ module OpenC3
137
138
  shard: 0,
138
139
  db_shard: 0,
139
140
  bridge_name: nil,
141
+ shared_resource: nil,
140
142
  scope:
141
143
  )
142
144
  if self.class._get_type == 'INTERFACE'
@@ -197,6 +199,7 @@ module OpenC3
197
199
  @shard = shard.to_i # to_i to handle nil
198
200
  @db_shard = db_shard.to_i # to_i to handle nil
199
201
  @bridge_name = bridge_name&.to_s&.upcase
202
+ @shared_resource = shared_resource
200
203
  @secrets = secrets
201
204
  end
202
205
 
@@ -271,6 +274,7 @@ module OpenC3
271
274
  'shard' => @shard,
272
275
  'db_shard' => @db_shard,
273
276
  'bridge_name' => @bridge_name,
277
+ 'shared_resource' => @shared_resource,
274
278
  'updated_at' => @updated_at
275
279
  }
276
280
  end
@@ -438,8 +442,13 @@ module OpenC3
438
442
  @container = parameters[0]
439
443
 
440
444
  when 'ROUTE_PREFIX'
441
- parser.verify_num_parameters(1, 1, "#{keyword} <Route Prefix>")
442
- @prefix = parameters[0]
445
+ usage = "#{keyword} <Route Prefix>"
446
+ parser.verify_num_parameters(1, 1, usage)
447
+ begin
448
+ @prefix = MicroserviceModel.validate_route_prefix(parameters[0])
449
+ rescue RuntimeError => error
450
+ raise ConfigParser::Error.new(parser, error.message, usage)
451
+ end
443
452
 
444
453
  when 'SHARD'
445
454
  parser.verify_num_parameters(1, 1, "#{keyword} <Shard Number Starting from 0>")
@@ -452,11 +461,21 @@ module OpenC3
452
461
  when 'BRIDGE'
453
462
  if type == 'INTERFACE'
454
463
  parser.verify_num_parameters(1, 1, "#{keyword} <Bridge Name>")
464
+ raise parser.error("BRIDGE can not be used with SHARED_RESOURCE") if @shared_resource
455
465
  @bridge_name = parameters[0].to_s.upcase
456
466
  else
457
467
  raise ConfigParser::Error.new(parser, "BRIDGE not supported for Router: #{keyword} #{parameters.join(" ")}")
458
468
  end
459
469
 
470
+ when 'SHARED_RESOURCE'
471
+ parser.verify_num_parameters(1, 1, "#{keyword} <Shared Resource Name>")
472
+ raise parser.error("SHARED_RESOURCE can not be used with BRIDGE") if @bridge_name
473
+ begin
474
+ @shared_resource = SharedResourceModel.validate_name(parameters[0])
475
+ rescue => e
476
+ raise parser.error(e.message)
477
+ end
478
+
460
479
  else
461
480
  raise ConfigParser::Error.new(parser, "Unknown keyword and parameters for Interface/Router: #{keyword} #{parameters.join(" ")}")
462
481
 
@@ -469,9 +488,9 @@ module OpenC3
469
488
  def deploy(gem_path, variables, validate_only: false)
470
489
  type = self.class._get_type
471
490
  microservice_name = "#{@scope}__#{type}__#{@name}"
472
- # A bridged interface runs on the host (openc3-app) rather than in Docker.
491
+ # A bridged interface runs on the host (openc3-cosmos-app) rather than in Docker.
473
492
  # COSMOS runs a bridge_interface tunnel + a shared bridge_microservice relay
474
- # and hands the real interface's spawn info to openc3-app. See deploy_bridge.
493
+ # and hands the real interface's spawn info to openc3-cosmos-app. See deploy_bridge.
475
494
  return deploy_bridge(gem_path, variables, microservice_name, type, validate_only: validate_only) if @bridge_name
476
495
 
477
496
  microservice = MicroserviceModel.new(
@@ -488,6 +507,7 @@ module OpenC3
488
507
  prefix: @prefix,
489
508
  shard: @shard,
490
509
  db_shard: @db_shard,
510
+ shared_resource: @shared_resource,
491
511
  scope: @scope
492
512
  )
493
513
  unless validate_only
@@ -502,14 +522,14 @@ module OpenC3
502
522
 
503
523
  # Deploy a bridged interface. This splits the interface across the Docker
504
524
  # side of COSMOS (bridge_interface tunnel + protocols/target processing) and
505
- # the host side (openc3-app spawns the real interface for raw data transfer):
525
+ # the host side (openc3-cosmos-app spawns the real interface for raw data transfer):
506
526
  # 1. A COSMOS interface_microservice that builds a bridge_interface tunnel
507
527
  # (InterfaceModel#build substitutes BridgeInterface when bridge_name is
508
528
  # set) and connects to the named bridge_microservice relay.
509
529
  # 2. A shared bridge_microservice relay (one per bridge_name) that this
510
530
  # interface's stream (ALPN stream/<name>) is added to.
511
531
  # 3. A HostInterfaceMicroserviceModel holding the real interface's spawn info for
512
- # openc3-app to run the interface on the host.
532
+ # openc3-cosmos-app to run the interface on the host.
513
533
  def deploy_bridge(gem_path, variables, microservice_name, type, validate_only: false)
514
534
  # The COSMOS side runs the Python interface_microservice which builds a
515
535
  # BridgeInterface (see InterfaceModel#build). It keeps this interface's
@@ -562,7 +582,7 @@ module OpenC3
562
582
  relay
563
583
  end
564
584
 
565
- # Capture the real interface's spawn info for openc3-app. The host side does
585
+ # Capture the real interface's spawn info for openc3-cosmos-app. The host side does
566
586
  # raw data transfer only, so protocols and target definitions are omitted;
567
587
  # the connection options and secrets needed to open the device are included.
568
588
  def create_host_interface_microservice