-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexport.rb
353 lines (301 loc) · 12.4 KB
/
export.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# RUNNING THE SCRIPT:
# ruby export.rb -c "<<PATH/CONFIG_FILE.rb>>"
# ruby export.rb -c "config/foo-web-server.rb"
#
# Example Config File Values (See Readme for additional details)
#
#
=begin yml config file example
---
core:
# server_url: https://<SPACE>.kinops.io OR https://<SERVER_NAME>.com/kinetic/<SPACE_SLUG>
server_url: https://web-server.com
space_slug: <SPACE_SLUG>
space_name: <SPACE_NAME>
service_user_username: <USER_NAME>
service_user_password: <PASSWORD>
options:
SUBMISSIONS_TO_EXPORT:
- datastore: true
formSlug: <FORM_SLUG>
REMOVE_DATA_PROPERTIES:
- createdAt
- createdBy
- updatedAt
- updatedBy
- closedAt
- closedBy
- submittedAt
- submittedBy
- id
- authStrategy
- key
- handle
task:
# server_url: https://<SPACE>.kinops.io/app/components/task OR https://<SERVER_NAME>.com/kinetic-task
server_url: https://web-server.com
service_user_username: <USER_NAME>
service_user_password: <PASSWORD>
http_options:
log_level: info
log_output: stderr
=end
require 'logger'
require 'json'
require 'optparse'
require 'kinetic_sdk'
template_name = "platform-template"
logger = Logger.new(STDERR)
logger.level = Logger::INFO
logger.formatter = proc do |severity, datetime, progname, msg|
date_format = datetime.utc.strftime("%Y-%m-%dT%H:%M:%S.%LZ")
"[#{date_format}] #{severity}: #{msg}\n"
end
# Determine the Present Working Directory
pwd = File.expand_path(File.dirname(__FILE__))
ARGV << '-h' if ARGV.empty?
# The options specified on the command line will be collected in *options*.
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: example.rb [options]"
opts.on("-c", "--c CONFIG_FILE", "The Configuration file to use") do |config|
options["CONFIG_FILE"] = config
end
# No argument, shows at tail. This will print an options summary.
# Try it and see!
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end.parse!
# determine the directory paths
platform_template_path = File.dirname(File.expand_path(__FILE__))
core_path = File.join(platform_template_path, "core")
task_path = File.join(platform_template_path, "task")
# ------------------------------------------------------------------------------
# methods
# ------------------------------------------------------------------------------
# Removes discussion id attribute from a given model
def remove_discussion_id_attribute(model)
if !model.is_a?(Array)
if model.has_key?("attributes")
scrubbed = model["attributes"].select do |attribute|
attribute["name"] != "Discussion Id"
end
end
model["attributes"] = scrubbed
end
return model
end
# ------------------------------------------------------------------------------
# constants
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# setup
# ------------------------------------------------------------------------------
logger.info "Installing gems for the \"#{template_name}\" template."
Dir.chdir(platform_template_path) { system("bundle", "install") }
vars = {}
file = "#{platform_template_path}/#{options['CONFIG_FILE']}"
# Check if configuration file exists
logger.info "Validating configuration file."
begin
if File.exist?(file) != true
raise "The file \"#{options['CONFIG_FILE']}\" does not exist."
end
rescue => error
logger.info error
exit
end
# Read the config file specified in the command line into the variable "vars"
begin
vars.merge!( YAML.load(File.read(file)) )
rescue => error
logger.info "Error loading YAML configuration"
logger.info error
exit
end
logger.info "Configuration file passed validation."
# Set http_options based on values provided in the config file.
http_options = (vars["http_options"] || {}).each_with_object({}) do |(k,v),result|
result[k.to_sym] = v
end
# Set variables based on values provided in the config file.
SUBMISSIONS_TO_EXPORT = vars["options"]["SUBMISSIONS_TO_EXPORT"]
REMOVE_DATA_PROPERTIES = vars["options"]["REMOVE_DATA_PROPERTIES"]
# Output the yml file config
logger.info "Output of Configuration File: \r #{JSON.pretty_generate(vars)}"
logger.info "Setting up the SDK"
space_sdk = KineticSdk::Core.new({
space_server_url: vars["core"]["server_url"],
space_slug: vars["core"]["space_slug"],
username: vars["core"]["service_user_username"],
password: vars["core"]["service_user_password"],
options: http_options.merge({ export_directory: "#{core_path}" })
})
task_sdk = KineticSdk::Task.new({
app_server_url: "#{vars["task"]["server_url"]}",
username: vars["task"]["service_user_username"],
password: vars["task"]["service_user_password"],
options: http_options.merge({ export_directory: "#{task_path}" })
})
# ------------------------------------------------------------------------------
# Validate connection and Credentials to Server
# ------------------------------------------------------------------------------
# Validate Core Connection
begin
logger.info "Validating connection to Core \"#{space_sdk.api_url}\""
response = space_sdk.me()
if response.status == 0
raise response.message
elsif response.status.to_s.match(/4\d{2}/)
raise response.content['error']
end
rescue => error
logger.info error
exit
end
# Validate Task Connection
begin
logger.info "Validating connection to Task \"#{task_sdk.api_url}\""
response = task_sdk.environment()
if response.status == 0
raise response.message
elsif response.status.to_s.match(/4\d{2}/)
raise response.content['error']
end
rescue => error
logger.info error
exit
end
logger.info "Validating connection to Cors and Task was Successful"
# ------------------------------------------------------------------------------
# core
# ------------------------------------------------------------------------------
logger.info "Removing files and folders from the existing \"#{template_name}\" template."
FileUtils.rm_rf Dir.glob("#{core_path}/*")
logger.info "Setting up the Core SDK"
# fetch export from core service and write to export directory
logger.info "Exporting the core components for the \"#{template_name}\" template."
logger.info " exporting with api: #{space_sdk.api_url}"
logger.info " - exporting configuration data (Kapps,forms, etc)"
space_sdk.export_space
# cleanup properties that should not be committed with export
# bridge keys
Dir["#{core_path}/space/bridges/*.json"].each do |filename|
bridge = JSON.parse(File.read(filename))
if bridge.has_key?("key")
bridge.delete("key")
File.open(filename, 'w') { |file| file.write(JSON.pretty_generate(bridge)) }
end
end
# cleanup space
filename = "#{core_path}/space.json"
space = JSON.parse(File.read(filename))
# filestore key
if space.has_key?("filestore") && space["filestore"].has_key?("key")
space["filestore"].delete("key")
end
# platform components
if space.has_key?("platformComponents")
if space["platformComponents"].has_key?("task")
space["platformComponents"].delete("task")
end
(space["platformComponents"]["agents"] || []).each_with_index do |agent,idx|
space["platformComponents"]["agents"][idx]["url"] = ""
end
end
# rewrite the space file
File.open(filename, 'w') { |file| file.write(JSON.pretty_generate(space)) }
# cleanup discussion ids
Dir["#{core_path}/**/*.json"].each do |filename|
model = remove_discussion_id_attribute(JSON.parse(File.read(filename)))
File.open(filename, 'w') { |file| file.write(JSON.pretty_generate(model)) }
end
# export submissions
logger.info "Exporting and writing submission data"
(SUBMISSIONS_TO_EXPORT || []).delete_if{ |item| item["kappSlug"].nil?}.each do |item|
is_datastore = item["datastore"] || false
logger.info "Exporting - #{is_datastore ? 'datastore' : 'kapp'} form #{item['formSlug']}"
# build directory to write files to
submission_path = is_datastore ?
"#{core_path}/space/datastore/forms/#{item['formSlug']}" :
"#{core_path}/space/kapps/#{item['kappSlug']}/forms/#{item['formSlug']}"
# get attachment fields from form definition
attachment_form = is_datastore ?
space_sdk.find_datastore_form(item['formSlug'], {"include" => "fields.details"}) :
space_sdk.find_form(item['kappSlug'], item['formSlug'], {"include" => "fields.details"})
# get attachment fields from form definition
attachement_files = attachment_form.status == 200 ? attachment_form.content['form']['fields'].select{ | file | file['dataType'] == "file" }.map { | field | field['name'] } : {}
# set base url for attachments
attachment_base_url = is_datastore ?
"#{space_sdk.api_url.gsub("/app/api/v1", "")}/app/datastore" :
"#{space_sdk.api_url.gsub("/app/api/v1", "")}"
# create folder to write submission data to
FileUtils.mkdir_p(submission_path, :mode => 0700)
# build params to pass to the retrieve_form_submissions method
params = {"include" => "details,children,origin,parent,values", "limit" => 1000, "direction" => "ASC"}
# open the submissions file in write mode
file = File.open("#{submission_path}/submissions.ndjson", 'w');
# ensure the file is empty
file.truncate(0)
response = nil
begin
# get submissions from datastore form or form
response = is_datastore ?
space_sdk.find_all_form_datastore_submissions(item['formSlug'], params).content :
space_sdk.find_form_submissions(item['kappSlug'], item['formSlug'], params).content
if response.has_key?("submissions")
# iterate over each submission
(response["submissions"] || []).each do |submission|
# write each attachment to a a dir
submission['values'].select{ |field, value| attachement_files.include?(field)}.each{ |field,value|
submission_id = submission['id']
# define the dir to contain the attahment
download_dir = "#{submission_path}/#{submission_id}/#{field}"
# evaluate fields with multiple attachments
value.map.with_index{ | attachment, index |
# create folder to write attachment
FileUtils.mkdir_p(download_dir, :mode => 0700)
# dir and file name to write attachment
download_path = "#{download_dir}/#{File.join(".", attachment['name'])}"
# url to retrieve the attachment
url = "#{attachment_base_url}/submissions/#{submission_id}/files/#{ERB::Util.url_encode(field)}/#{index}/#{ERB::Util.url_encode(attachment['name'])}"
# retrieve and write attachment
space_sdk.stream_download_to_file(download_path, url, {}, space_sdk.default_headers)
# add the "path" key to indicate the attachment's location
attachment['path'] = "/#{submission_id}/#{field}/#{attachment['name']}"
}
}
# append each submission (removing the submission unwanted attributes)
file.puts(JSON.generate(submission.delete_if { |key, value| REMOVE_DATA_PROPERTIES.member?(key)}))
end
end
params['pageToken'] = response['nextPageToken']
# get next page of submissions if there are more
end while !response.nil? && !response['nextPageToken'].nil?
# close the submissions file
file.close()
end
logger.info " - submission data export complete"
# ------------------------------------------------------------------------------
# task
# ------------------------------------------------------------------------------
logger.info "Removing files and folders from the existing \"#{template_name}\" template."
FileUtils.rm_rf Dir.glob("#{task_path}/*")
logger.info "Exporting the task components for the \"#{template_name}\" template."
logger.info " exporting with api: #{task_sdk.api_url}"
# export all sources, trees, routines, handlers,
# groups, policy rules, categories, and access keys
task_sdk.export_sources()
task_sdk.export_trees()
task_sdk.export_routines()
task_sdk.export_handlers()
task_sdk.export_groups()
task_sdk.export_policy_rules()
task_sdk.export_categories()
task_sdk.export_access_keys()
# ------------------------------------------------------------------------------
# complete
# ------------------------------------------------------------------------------
logger.info "Finished exporting the \"#{template_name}\" template."