Class: OpenC3::TargetFile

Inherits:
Object show all
Defined in:
lib/openc3/utilities/target_file.rb

Constant Summary collapse

DEFAULT_BUCKET_NAME =
'config'
TEMP_FOLDER =

Matches ScriptRunner.vue const TEMP_FOLDER

'__TEMP__'

Class Method Summary collapse

Class Method Details

.all(scope, path_matchers, include_temp: false) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
# File 'lib/openc3/utilities/target_file.rb', line 28

def self.all(scope, path_matchers, include_temp: false)
  result = []
  modified = []
  temp = []

  rubys3_client = Aws::S3::Client.new
  token = nil
  while true
    resp = rubys3_client.list_objects_v2({
      bucket: DEFAULT_BUCKET_NAME,
      prefix: "#{scope}/targets",
      max_keys: 1000,
      continuation_token: token
    })

    resp.contents.each do |object|
      split_key = object.key.split('/')
      # DEFAULT/targets_modified/__TEMP__/YYYY_MM_DD_HH_MM_SS_mmm_temp.rb
      if split_key[2] == TEMP_FOLDER
        temp << split_key[2..-1].join('/') if include_temp
        next
      end

      found = false
      path_matchers.each do |path|
        if split_key.include?(path)
          found = true
          break
        end
      end
      next unless found
      result_no_scope_or_target_folder = split_key[2..-1].join('/')
      if object.key.include?("#{scope}/targets_modified")
        modified << result_no_scope_or_target_folder
      else
        result << result_no_scope_or_target_folder
      end
    end
    break unless resp.is_truncated
    token = resp.next_continuation_token
  end

  # Add in local targets_modified if present
  if ENV['OPENC3_LOCAL_MODE']
    local_modified = OpenC3::LocalMode.local_target_files(scope: scope, path_matchers: path_matchers, include_temp: include_temp)
    local_modified.each do |filename|
      if include_temp and filename.include?(TEMP_FOLDER)
        temp << filename
      else
        modified << filename unless modified.include?(filename)
        result << filename unless result.include?(filename)
      end
    end
  end

  # Determine if there are any modified files and mark them with '*'
  result.map! do |file|
    if modified.include?(file)
      modified.delete(file)
      "#{file}*"
    else
      file
    end
  end

  # Concat any remaining modified files (new files not in original target)
  result.concat(modified)
  result.concat(temp.uniq)
  result.sort
end

.body(scope, name) ⇒ Object



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
# File 'lib/openc3/utilities/target_file.rb', line 125

def self.body(scope, name)
  name = name.split('*')[0] # Split '*' that indicates modified
  rubys3_client = Aws::S3::Client.new
  begin
    # First try opening a potentially modified version by looking for the modified target
    if ENV['OPENC3_LOCAL_MODE']
      local_file = OpenC3::LocalMode.open_local_file(name, scope: scope)
      return local_file.read if local_file
    end

    resp =
      rubys3_client.get_object(
        bucket: DEFAULT_BUCKET_NAME,
        key: "#{scope}/targets_modified/#{name}",
      )
  rescue Aws::S3::Errors::NoSuchKey
    # Now try the original
    resp =
      rubys3_client.get_object(
        bucket: DEFAULT_BUCKET_NAME,
        key: "#{scope}/targets/#{name}",
      )
  end
  if File.extname(name) == ".bin"
    resp.body.binmode
  end
  resp.body.read
end

.create(scope, name, text, content_type: 'text/plain') ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/openc3/utilities/target_file.rb', line 154

def self.create(scope, name, text, content_type: 'text/plain')
  return false unless text
  if ENV['OPENC3_LOCAL_MODE']
    OpenC3::LocalMode.put_target_file("#{scope}/targets_modified/#{name}", text, scope: scope)
  end
  OpenC3::S3Utilities.put_object_and_check(
    # Use targets_modified to save modifications
    # This keeps the original target clean (read-only)
    key: "#{scope}/targets_modified/#{name}",
    body: text,
    bucket: DEFAULT_BUCKET_NAME,
    content_type: content_type,
  )
  true
end

.delete_temp(scope) ⇒ Object



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
# File 'lib/openc3/utilities/target_file.rb', line 99

def self.delete_temp(scope)
  rubys3_client = Aws::S3::Client.new
  token = nil
  while true
    resp = rubys3_client.list_objects_v2({
      bucket: DEFAULT_BUCKET_NAME,
      prefix: "#{scope}/targets_modified/#{TEMP_FOLDER}",
      max_keys: 1000,
      continuation_token: token
    })

    resp.contents.each do |object|
      rubys3_client.delete_object(
        bucket: DEFAULT_BUCKET_NAME,
        key: object.key,
      )
      if ENV['OPENC3_LOCAL_MODE']
        OpenC3::LocalMode.delete_local(object.key)
      end
    end
    break unless resp.is_truncated
    token = resp.next_continuation_token
  end
  true
end

.destroy(scope, name) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/openc3/utilities/target_file.rb', line 170

def self.destroy(scope, name)
  rubys3_client = Aws::S3::Client.new

  if ENV['OPENC3_LOCAL_MODE']
    OpenC3::LocalMode.delete_local("#{scope}/targets_modified/#{name}")
  end

  # Only delete file from the modified target directory
  rubys3_client.delete_object(
    key: "#{scope}/targets_modified/#{name}",
    bucket: DEFAULT_BUCKET_NAME,
  )
  true
end