-
Notifications
You must be signed in to change notification settings - Fork 124
/
process.rb
358 lines (294 loc) · 11.9 KB
/
process.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
354
355
356
357
358
# encoding: UTF-8
#
# Copyright (c) 2010-2015 GoodData Corporation. All rights reserved.
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
require 'zip'
require 'uri'
require_relative '../helpers/global_helpers'
require_relative '../rest/resource'
require_relative 'execution_detail'
require_relative 'schedule'
APP_STORE_URL ||= 'https://github.com/gooddata/app_store'
module GoodData
class Process < Rest::Resource
attr_reader :data
alias_method :raw_data, :data
alias_method :json, :data
alias_method :to_hash, :data
class << self
def [](id, options = {})
project = options[:project]
c = client(options)
if id == :all && project
uri = "/gdc/projects/#{project.pid}/dataload/processes"
data = c.get(uri)
data['processes']['items'].map do |process_data|
c.create(Process, process_data, project: project)
end
elsif id == :all
uri = "/gdc/account/profile/#{c.user.obj_id}/dataload/processes"
data = c.get(uri)
pids = data['processes']['items'].map { |process_data| process_data['process']['links']['self'].match(%r{/gdc/projects/(\w*)/})[1] }.uniq
projects_lookup = pids.pmap { |pid| c.projects(pid) }.reduce({}) do |a, e|
a[e.pid] = e
a
end
data['processes']['items'].map do |process_data|
pid = process_data['process']['links']['self'].match(%r{/gdc/projects/(\w*)/})[1]
c.create(Process, process_data, project: projects_lookup[pid])
end
else
uri = "/gdc/projects/#{project.pid}/dataload/processes/#{id}"
c.create(Process, c.get(uri), project: project)
end
end
def all
Process[:all]
end
def with_deploy(dir, options = {}, &block)
_client, project = GoodData.get_client_and_project(options)
GoodData.with_project(project) do
params = options[:params].nil? ? [] : [options[:params]]
if block
begin
res = GoodData::Process.deploy(dir, options.merge(:files_to_exclude => params))
block.call(res)
rescue => e
puts e.inspect
ensure
res.delete if res
end
else
GoodData::Process.deploy(dir, options.merge(:files_to_exclude => params))
end
end
end
def upload_package(path, files_to_exclude, opts = { :client => GoodData.connection })
GoodData.get_client_and_project(opts)
zip_and_upload(path, files_to_exclude, opts)
end
# Deploy a new process or redeploy existing one.
#
# @param path [String] Path to ZIP archive or to a directory containing files that should be ZIPed
# @option options [String] :files_to_exclude
# @option options [String] :type ('GRAPH') Type of process - GRAPH or RUBY
# @option options [String] :name Readable name of the process
# @option options [String] :process_id ID of a process to be redeployed (do not set if you want to create a new process)
# @option options [Boolean] :verbose (false) Switch on verbose mode for detailed logging
def deploy(path, options = { :client => GoodData.client, :project => GoodData.project })
return deploy_brick(path, options) if path.to_s.start_with?(APP_STORE_URL)
return deploy_from_appstore(path.to_s, options) if (path.to_s =~ %r{\${.*}:(.*)\/(.*):\/}) == 0 # rubocop:disable Style/NumericPredicate
client, project = GoodData.get_client_and_project(options)
path = Pathname(path) || fail('Path is not specified')
files_to_exclude = options[:files_to_exclude].nil? ? [] : options[:files_to_exclude].map { |pname| Pathname(pname) }
process_id = options[:process_id]
type = options[:type] || 'GRAPH'
deploy_name = options[:name]
fail ArgumentError, 'options[:name] can not be nil or empty!' if deploy_name.nil? || deploy_name.empty?
verbose = options[:verbose] || false
puts HighLine.color("Deploying #{path}", HighLine::BOLD) if verbose
deployed_path = Process.upload_package(path, files_to_exclude, client: client, project: project)
data = {
:process => {
:name => deploy_name,
:path => "/uploads/#{File.basename(deployed_path)}",
:type => type
}
}
res = if process_id.nil?
client.post("/gdc/projects/#{project.pid}/dataload/processes", data)
else
client.put("/gdc/projects/#{project.pid}/dataload/processes/#{process_id}", data)
end
process = client.create(Process, res, project: project)
puts HighLine.color("Deploy DONE #{path}", HighLine::GREEN) if verbose
process
end
def deploy_brick(path, options = { :client => GoodData.client, :project => GoodData.project })
client, project = GoodData.get_client_and_project(options)
brick_uri_parts = URI(path).path.split('/')
ref = brick_uri_parts[4]
brick_name = brick_uri_parts.last
brick_path = brick_uri_parts[5..-1].join('/')
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
`git clone #{APP_STORE_URL}`
end
Dir.chdir(File.join(dir, 'app_store')) do
if ref
`git checkout #{ref}`
fail 'Wrong branch or tag specified!' if $CHILD_STATUS.to_i.nonzero?
end
opts = {
:client => client,
:project => project,
:name => brick_name,
:type => 'RUBY'
}
full_brick_path = File.join(dir, 'app_store', brick_path)
unless File.exist?(full_brick_path)
fail "Invalid brick name specified - '#{brick_name}'"
end
return deploy(full_brick_path, opts)
end
end
end
def deploy_from_appstore(path, options = { :client => GoodData.client, :project => GoodData.project })
client, project = GoodData.get_client_and_project(options)
deploy_name = options[:name]
fail ArgumentError, 'options[:name] can not be nil or empty!' if deploy_name.nil? || deploy_name.empty?
verbose = options[:verbose] || false
puts HighLine.color("Deploying #{path}", HighLine::BOLD) if verbose
process_id = options[:process_id]
data = {
process: {
name: deploy_name,
path: path,
type: 'RUBY'
}
}
res =
if process_id.nil?
client.post("/gdc/projects/#{project.pid}/dataload/processes", data)
else
client.put("/gdc/projects/#{project.pid}/dataload/processes/#{process_id}", data)
end
if res.keys.first == 'asyncTask'
res = JSON.parse(client.poll_on_code(res['asyncTask']['links']['poll'], options.merge(process: false)))
end
process = client.create(Process, res, project: project)
puts HighLine.color("Deploy DONE #{path}", HighLine::GREEN) if verbose
process
end
private
def with_zip(opts = {})
client = opts[:client]
Tempfile.open('deploy-graph-archive') do |temp|
zip_filename = temp.path
File.open(zip_filename, 'w') do |zip|
Zip::File.open(zip.path, Zip::File::CREATE) do |zipfile|
yield zipfile
end
end
client.upload_to_user_webdav(temp.path, opts)
temp.path
end
end
def zip_and_upload(path, files_to_exclude, opts = {})
client = opts[:client]
puts 'Creating package for upload'
if !path.directory? && (path.extname == '.grf' || path.extname == '.rb')
with_zip(opts) do |zipfile|
zipfile.add(File.basename(path), path)
end
elsif !path.directory?
# this branch expects a zipped file. Since the filename on webdav is by default
# equal to the filename of a local file. I happened often that the name clashed
# if ran in parallel. Create a randomized name to mitigate that
randomized_filename = (0...16).map { (65 + rand(26)).chr }.join
client.upload_to_user_webdav(path, { filename: randomized_filename }.merge(opts))
randomized_filename
else
with_zip(opts) do |zipfile|
files_to_upload = Dir[File.join(path, '**', '**')].reject { |f| files_to_exclude.include?(Pathname(path) + f) }
puts "Uploading #{files_to_upload.count} files."
files_to_upload.each do |file|
file_pathname = Pathname.new(file)
file_relative_pathname = file_pathname.relative_path_from(Pathname.new(path))
zipfile.add(file_relative_pathname, file)
end
end
end
end
# -----------------------------
end
def initialize(data)
@data = data
end
def delete
client.delete(uri)
end
# Redeploy existing process.
#
# @param path [String] Path to ZIP archive or to a directory containing files that should be ZIPed
# @option options [String] :files_to_exclude
# @option options [String] :process_id ('nobody') From address
# @option options [String] :type ('GRAPH') Type of process - GRAPH or RUBY
# @option options [String] :name Readable name of the process
# @option options [Boolean] :verbose (false) Switch on verbose mode for detailed logging
def deploy(path, options = {})
Process.deploy(path, { client: client, process_id: process_id, :project => project, :name => name, :type => type }.merge(options))
end
# Downloads the process from S3 in a zipped form.
#
# @return [IO] The stream of data that represents a zipped deployed process.
def download
link = links['source']
client.connection.refresh_token
client.get(link, process: false) { |_, _, result| RestClient.get(result.to_hash['location'].first) }
end
def process
data['process']
end
def name
process['name']
end
def type
process['type'].downcase.to_sym
end
def links
process['links']
end
def link
links['self']
end
alias_method :uri, :link
def obj_id
uri.split('/').last
end
alias_method :process_id, :obj_id
def executions_link
links['executions']
end
def graphs
process['graphs']
end
def executables
process['executables']
end
def path
process['path']
end
def schedules
project.schedules.select { |schedule| schedule.process_id == obj_id }
end
def create_schedule(cron, executable, options = {})
project.create_schedule(process_id, cron, executable, options.merge(client: client, project: project))
end
def execute(executable, options = {})
result = start_execution(executable, options)
begin
client.poll_on_code(result['executionTask']['links']['poll'], options)
rescue RestClient::RequestFailed => e
raise(e)
ensure
result = client.get(result['executionTask']['links']['detail'])
if result['executionDetail']['status'] == 'ERROR'
fail "Runing process failed. You can look at a log here #{result['executionDetail']['logFileName']}"
end
end
client.create(GoodData::ExecutionDetail, result, client: client, project: project)
end
def start_execution(executable, options = {})
params = options[:params] || {}
hidden_params = options[:hidden_params] || {}
client.post(executions_link,
:execution => {
:graph => executable.to_s,
:params => GoodData::Helpers.encode_public_params(params),
:hiddenParams => GoodData::Helpers.encode_hidden_params(hidden_params)
})
end
end
end