Class: IronWorkerNG::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/iron_worker_ng/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Client

Returns a new instance of Client.



28
29
30
31
32
33
34
# File 'lib/iron_worker_ng/client.rb', line 28

def initialize(options = {}, &block)
  @api = IronWorkerNG::APIClient.new(options)

  unless block.nil?
    instance_eval(&block)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/iron_worker_ng/client.rb', line 52

def method_missing(name, *args, &block)
  if args.length == 0
    IronWorkerNG::ClientProxyCaller.new(self, name)
  else
    super(name, *args, &block)
  end
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



26
27
28
# File 'lib/iron_worker_ng/client.rb', line 26

def api
  @api
end

Instance Method Details

#clusters_create(params = {}) ⇒ Object



467
468
469
470
471
# File 'lib/iron_worker_ng/client.rb', line 467

def clusters_create(params = {})
  IronCore::Logger.debug 'IronWorkerNG', "Calling clusters.create with params='#{params.to_s}'"
  res = @api.clusters_create(params)
  OpenStruct.new(res)
end

#clusters_credentials(id) ⇒ Object



461
462
463
464
465
# File 'lib/iron_worker_ng/client.rb', line 461

def clusters_credentials(id)
  IronCore::Logger.debug 'IronWorkerNG', "Calling clusters.credentials"
  res = @api.clusters_credentials(id)
  OpenStruct.new(res)
end

#clusters_delete(cluster_id) ⇒ Object



479
480
481
482
483
# File 'lib/iron_worker_ng/client.rb', line 479

def clusters_delete(cluster_id)
  IronCore::Logger.debug 'IronWorkerNG', "Calling clusters.delete with cluster_id='#{cluster_id}'"
  res = @api.clusters_delete(cluster_id)
  OpenStruct.new(res)
end

#clusters_get(id) ⇒ Object



454
455
456
457
458
459
# File 'lib/iron_worker_ng/client.rb', line 454

def clusters_get(id)
  IronCore::Logger.debug 'IronWorkerNG', "Calling clusters.get"
  res = @api.clusters_get(id)['cluster']
  res['_id'] = res['id']
  OpenStruct.new(res)
end

#clusters_internal_listObject



496
497
498
499
# File 'lib/iron_worker_ng/client.rb', line 496

def clusters_internal_list
  IronCore::Logger.debug 'IronWorkerNG', "Calling clusters.internal.list"
  clusters_list_base(internal: true)
end

#clusters_list(options = {}) ⇒ Object



449
450
451
452
# File 'lib/iron_worker_ng/client.rb', line 449

def clusters_list(options = {})
  IronCore::Logger.debug 'IronWorkerNG', "Calling clusters.list"
  clusters_list_base(options)
end

#clusters_share(cluster_id, params = {}) ⇒ Object



485
486
487
488
489
# File 'lib/iron_worker_ng/client.rb', line 485

def clusters_share(cluster_id, params = {})
  IronCore::Logger.debug 'IronWorkerNG', "Calling clusters.share with params='#{params.to_s}'"
  res = @api.clusters_share(cluster_id, params)
  OpenStruct.new(res)
end

#clusters_shared_listObject



491
492
493
494
# File 'lib/iron_worker_ng/client.rb', line 491

def clusters_shared_list
  IronCore::Logger.debug 'IronWorkerNG', "Calling clusters.shared.list"
  clusters_list_base(shared: true)
end

#clusters_unshare(cluster_id, user_id) ⇒ Object



501
502
503
504
505
# File 'lib/iron_worker_ng/client.rb', line 501

def clusters_unshare(cluster_id, user_id)
  IronCore::Logger.debug 'IronWorkerNG', "Calling clusters.unshare with cluster_id='#{cluster_id}', user_id='#{user_id}'"
  res = @api.clusters_unshare(cluster_id, user_id)
  OpenStruct.new(res)
end

#clusters_update(cluster_id, params = {}) ⇒ Object



473
474
475
476
477
# File 'lib/iron_worker_ng/client.rb', line 473

def clusters_update(cluster_id, params = {})
  IronCore::Logger.debug 'IronWorkerNG', "Calling clusters.update with params='#{params.to_s}'"
  res = @api.clusters_update(cluster_id, params)
  OpenStruct.new(res)
end

#codes_create(code, options = {}) ⇒ 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/iron_worker_ng/client.rb', line 99

def codes_create(code, options = {})
  IronCore::Logger.debug 'IronWorkerNG', "Calling codes.create with code='#{code.to_s}' and options='#{options.to_s}'"

  if options[:config] && options[:config].is_a?(Hash)
    options = options.dup
    options[:config] = options[:config].to_json
  end

  options.merge!(stack:code.stack) if code.stack

  container_file = code.create_container

  if code.zip_package
    res = nil
    IronWorkerNG::Fetcher.fetch_to_file(code.zip_package) do |file|
      res = @api.codes_create(code.name, file, 'sh', '__runner__.sh', options)
    end
  elsif code.remote_build_command.nil? && (not code.full_remote_build)
    res = @api.codes_create(code.name, container_file, 'sh', '__runner__.sh', options)
  else
    builder_code_name = code.name + (code.name[0 .. 0].upcase == code.name[0 .. 0] ? '::Builder' : '::builder')

    @api.codes_create(builder_code_name, container_file, 'sh', '__runner__.sh', options)

    builder_task = tasks.create(builder_code_name, :code_name => code.name, :client_options => @api.options.to_json, :codes_create_options => options.to_json)

    builder_task = tasks.wait_for(builder_task._id)

    if builder_task.status != 'complete'
      log = tasks.log(builder_task._id)

      File.unlink(container_file)

      IronCore::Logger.error 'IronWorkerNG', "Error while remote building worker\n" + log, IronCore::Error
    end

    res = JSON.parse(builder_task.msg)
  end

  File.unlink(container_file) if code.zip_package.nil?

  res['_id'] = res['id']
  OpenStruct.new(res)
end

#codes_create_async(code, options = {}) ⇒ Object



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
# File 'lib/iron_worker_ng/client.rb', line 144

def codes_create_async(code, options = {})
  IronCore::Logger.debug 'IronWorkerNG', "Calling codes.create_async with code='#{code.to_s}' and options='#{options.to_s}'"

  if options[:config] && options[:config].is_a?(Hash)
    options = options.dup
    options[:config] = options[:config].to_json
  end

  options.merge!(stack:code.stack) if code.stack

  container_file = code.create_container

  if code.remote_build_command.nil? && (not code.full_remote_build)
    res = @api.codes_create(code.name, container_file, 'sh', '__runner__.sh', options)
  else
    builder_code_name = code.name + (code.name[0 .. 0].upcase == code.name[0 .. 0] ? '::Builder' : '::builder')

    @api.codes_create(builder_code_name, container_file, 'sh', '__runner__.sh', options)

    builder_task = tasks.create(builder_code_name, :code_name => code.name, :client_options => @api.options.to_json, :codes_create_options => options.to_json)

    File.unlink(container_file)

    return builder_task._id
  end

  File.unlink(container_file)

  res['_id'] = res['id']
  OpenStruct.new(res)
end

#codes_delete(code_id) ⇒ Object



262
263
264
265
266
267
268
# File 'lib/iron_worker_ng/client.rb', line 262

def codes_delete(code_id)
  IronCore::Logger.debug 'IronWorkerNG', "Calling codes.delete with code_id='#{code_id}'"

  @api.codes_delete(code_id)

  true
end

#codes_download(code_id, options = {}) ⇒ Object



276
277
278
279
280
# File 'lib/iron_worker_ng/client.rb', line 276

def codes_download(code_id, options = {})
  IronCore::Logger.debug 'IronWorkerNG', "Calling codes.download with code_id='#{code_id}' and options='#{options.to_s}'"

  @api.codes_download(code_id, options)
end

#codes_get(code_id) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/iron_worker_ng/client.rb', line 91

def codes_get(code_id)
  IronCore::Logger.debug 'IronWorkerNG', "Calling codes.get with code_id='#{code_id}'"

  c = @api.codes_get(code_id)
  c['_id'] = c['id']
  OpenStruct.new(c)
end

#codes_list(options = {}) ⇒ Object



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
# File 'lib/iron_worker_ng/client.rb', line 65

def codes_list(options = {})
  IronCore::Logger.debug 'IronWorkerNG', "Calling codes.list with options='#{options.to_s}'"

  all = options[:all] || options['all']

  if all
    result = []

    page = options[:page] || options['page'] || 0
    per_page = options[:per_page] || options['per_page'] || 100

    while true
      next_codes = codes_list(options.merge({:page => page}).delete_if { |name| name == :all || name == 'all' })

      result += next_codes

      break if next_codes.length != per_page
      page += 1
    end

    result
  else
    @api.codes_list(options)['codes'].map { |c| OpenStruct.new(c.merge('_id' => c['id'])) }
  end
end

#codes_patch(name, options = {}) ⇒ Object



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
# File 'lib/iron_worker_ng/client.rb', line 176

def codes_patch(name, options = {})
  IronCore::Logger.debug 'IronWorkerNG', "Calling codes.patch with name='#{name}' and options='#{options.to_s}'"

  code = codes.list(per_page: 100).find { |c| c.name == name }

  if code.nil?
    IronCore::Logger.error 'IronWorkerNG', "Can't find code with name='#{name}' to patch", IronCore::Error
  end

  patcher_code_name = name + (name[0 .. 0].upcase == name[0 .. 0] ? '::Patcher' : '::patcher')

  exec_dir = ::Dir.tmpdir + '/' + ::Dir::Tmpname.make_tmpname('iron-worker-ng-', 'exec')
  exec_file_name = exec_dir + '/patchcer.rb'

  FileUtils.mkdir_p(exec_dir)

  exec_file = File.open(exec_file_name, 'w')
  exec_file.write <<EXEC_FILE
# #{IronWorkerNG.full_version}

File.open('.gemrc', 'w') do |gemrc|
  gemrc.puts('gem: --no-ri --no-rdoc')
end

`gem install iron_worker_ng`

require 'iron_worker_ng'

client = IronWorkerNG::Client.new(JSON.parse(params[:client_options]))

original_code = client.codes.get(params[:code_id])
original_code_data = client.codes.download(params[:code_id])

`mkdir code`
original_code_zip = File.open('code/code.zip', 'w')
original_code_zip.write(original_code_data)
original_code_zip.close
`cd code && unzip code.zip && rm code.zip && cd ..`

patch_params = JSON.parse(params[:patch])
patch_params.each {|k, v| system("cat patch/\#{k} > code/\#{v}")}

code_container = IronWorkerNG::Code::Container::Zip.new

Dir['code/*'].each do |entry|
  code_container.add(entry[5 .. -1], entry)
end

code_container.close

res = client.api.codes_create(original_code.name, code_container.name, 'sh', '__runner__.sh', :config => original_code.config)

res['_id'] = res['id']
res = OpenStruct.new(res)

client.tasks.set_progress(iron_task_id, :msg => res.marshal_dump.to_json)
EXEC_FILE
  exec_file.close

  patcher_code = IronWorkerNG::Code::Base.new
  patcher_code.runtime = :ruby
  patcher_code.name = patcher_code_name
  patcher_code.exec(exec_file_name)
  options[:patch].keys.each {|v| patcher_code.file(v, 'patch')}
  patch_params = Hash[options[:patch].map {|k,v| [File.basename(k), v]}]

  patcher_container_file = patcher_code.create_container

  @api.codes_create(patcher_code_name, patcher_container_file, 'sh', '__runner__.sh', {})

  FileUtils.rm_rf(exec_dir)
  File.unlink(patcher_container_file)

  patcher_task = tasks.create(patcher_code_name, :code_id => code._id, :client_options => @api.options.to_json, patch: patch_params.to_json)
  patcher_task = tasks.wait_for(patcher_task._id)

  if patcher_task.status != 'complete'
    log = tasks.log(patcher_task._id)
    IronCore::Logger.error 'IronWorkerNG', "Error while patching worker\n" + log, IronCore::Error
  end

  res = JSON.parse(patcher_task.msg)
  res['_id'] = res['id']
  OpenStruct.new(res)
end

#codes_pause_task_queue(code_id, options = {}) ⇒ Object



282
283
284
285
286
287
# File 'lib/iron_worker_ng/client.rb', line 282

def codes_pause_task_queue(code_id, options = {})
  IronCore::Logger.debug 'IronWorkerNG', "Calling codes.pause_task_queue with code_id='#{code_id}' and options='#{options.to_s}'"

  res = @api.codes_pause_task_queue(code_id, options)
  OpenStruct.new(res)
end

#codes_resume_task_queue(code_id, options = {}) ⇒ Object



289
290
291
292
293
294
# File 'lib/iron_worker_ng/client.rb', line 289

def codes_resume_task_queue(code_id, options = {})
  IronCore::Logger.debug 'IronWorkerNG', "Calling codes.resume_task_queue with code_id='#{code_id}' and options='#{options.to_s}'"

  res = @api.codes_resume_task_queue(code_id, options)
  OpenStruct.new(res)
end

#codes_revisions(code_id, options = {}) ⇒ Object



270
271
272
273
274
# File 'lib/iron_worker_ng/client.rb', line 270

def codes_revisions(code_id, options = {})
  IronCore::Logger.debug 'IronWorkerNG', "Calling codes.revisions with code_id='#{code_id}' and options='#{options.to_s}'"

  @api.codes_revisions(code_id, options)['revisions'].map { |c| OpenStruct.new(c.merge('_id' => c['id'])) }
end

#jwtObject



44
45
46
# File 'lib/iron_worker_ng/client.rb', line 44

def jwt
  @api.jwt
end

#optionsObject



36
37
38
# File 'lib/iron_worker_ng/client.rb', line 36

def options
  @api.options
end

#params_for_legacy(code_name, params = {}) ⇒ Object



507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/iron_worker_ng/client.rb', line 507

def params_for_legacy(code_name, params = {})
  if params.is_a?(String)
    params = JSON.parse(params)
  end

  attrs = {}

  params.keys.each do |k|
    attrs['@' + k.to_s] = params[k]
  end

  attrs = attrs.to_json

  {:class_name => code_name, :attr_encoded => Base64.encode64(attrs), :sw_config => {:project_id => project_id, :token => token}}.to_json
end

#project_idObject



48
49
50
# File 'lib/iron_worker_ng/client.rb', line 48

def project_id
  @api.project_id
end

#projects_getObject



440
441
442
443
444
445
446
447
# File 'lib/iron_worker_ng/client.rb', line 440

def projects_get
  IronCore::Logger.debug 'IronWorkerNG', "Calling projects.get"

  res = @api.projects_get

  res['_id'] = res['id']
  OpenStruct.new(res)
end

#schedules_cancel(schedule_id, options = {}) ⇒ Object



432
433
434
435
436
437
438
# File 'lib/iron_worker_ng/client.rb', line 432

def schedules_cancel(schedule_id, options = {})
  IronCore::Logger.debug 'IronWorkerNG', "Calling schedules.cancel with schedule_id='#{schedule_id}, options='#{options.to_s}'"

  @api.schedules_cancel(schedule_id, options)

  true
end

#schedules_create(code_name, params = {}, options = {}) ⇒ Object



404
405
406
407
408
409
410
411
412
# File 'lib/iron_worker_ng/client.rb', line 404

def schedules_create(code_name, params = {}, options = {})
  IronCore::Logger.debug 'IronWorkerNG', "Calling schedules.create with code_name='#{code_name}', params='#{params.to_s}' and options='#{options.to_s}'"

  res = @api.schedules_create(code_name, params.is_a?(String) ? params : params.to_json, options)

  s = res['schedules'][0]
  s['_id'] = s['id']
  OpenStruct.new(s)
end

#schedules_create_legacy(code_name, params = {}, options = {}) ⇒ Object



422
423
424
425
426
427
428
429
430
# File 'lib/iron_worker_ng/client.rb', line 422

def schedules_create_legacy(code_name, params = {}, options = {})
  IronCore::Logger.debug 'IronWorkerNG', "Calling schedules.create_legacy with code_name='#{code_name}', params='#{params.to_s}' and options='#{options.to_s}'"

  res = @api.schedules_create(code_name, params_for_legacy(code_name, params), options)

  s = res['schedules'][0]
  s['_id'] = s['id']
  OpenStruct.new(s)
end

#schedules_get(schedule_id) ⇒ Object



396
397
398
399
400
401
402
# File 'lib/iron_worker_ng/client.rb', line 396

def schedules_get(schedule_id)
  IronCore::Logger.debug 'IronWorkerNG', "Calling schedules.get with schedule_id='#{schedule_id}"

  s = @api.schedules_get(schedule_id)
  s['_id'] = s['id']
  OpenStruct.new(s)
end

#schedules_list(options = {}) ⇒ Object



390
391
392
393
394
# File 'lib/iron_worker_ng/client.rb', line 390

def schedules_list(options = {})
  IronCore::Logger.debug 'IronWorkerNG', "Calling schedules.list with options='#{options.to_s}'"

  @api.schedules_list(options)['schedules'].map { |s| OpenStruct.new(s.merge('_id' => s['id'])) }
end

#schedules_update(id, options = {}) ⇒ Object



414
415
416
417
418
419
420
# File 'lib/iron_worker_ng/client.rb', line 414

def schedules_update(id, options = {})
  IronCore::Logger.debug 'IronWorkerNG', "Calling schedules.update with id='#{id}', options='#{options.to_s}'"

  res = @api.schedules_update(id, options)

  OpenStruct.new(res)
end

#stacks_listObject



60
61
62
# File 'lib/iron_worker_ng/client.rb', line 60

def stacks_list
  @api.stacks_list
end

#tasks_cancel(task_id) ⇒ Object



330
331
332
333
334
335
336
# File 'lib/iron_worker_ng/client.rb', line 330

def tasks_cancel(task_id)
  IronCore::Logger.debug 'IronWorkerNG', "Calling tasks.cancel with task_id='#{task_id}'"

  @api.tasks_cancel(task_id)

  true
end

#tasks_cancel_all(code_id) ⇒ Object



338
339
340
341
342
343
344
# File 'lib/iron_worker_ng/client.rb', line 338

def tasks_cancel_all(code_id)
  IronCore::Logger.debug 'IronWorkerNG', "Calling tasks.cancel_all with code_id='#{code_id}'"

  @api.tasks_cancel_all(code_id)

  true
end

#tasks_create(code_name, params = {}, options = {}) ⇒ Object



310
311
312
313
314
315
316
317
318
# File 'lib/iron_worker_ng/client.rb', line 310

def tasks_create(code_name, params = {}, options = {})
  IronCore::Logger.debug 'IronWorkerNG', "Calling tasks.create with code_name='#{code_name}', params='#{params.to_s}' and options='#{options.to_s}'"

  res = @api.tasks_create(code_name, params.is_a?(String) ? params : params.to_json, options)

  t = res['tasks'][0]
  t['_id'] = t['id']
  OpenStruct.new(t)
end

#tasks_create_legacy(code_name, params = {}, options = {}) ⇒ Object



320
321
322
323
324
325
326
327
328
# File 'lib/iron_worker_ng/client.rb', line 320

def tasks_create_legacy(code_name, params = {}, options = {})
  IronCore::Logger.debug 'IronWorkerNG', "Calling tasks.create_legacy with code_name='#{code_name}', params='#{params.to_s}' and options='#{options.to_s}'"

  res = @api.tasks_create(code_name, params_for_legacy(code_name, params), options)

  t = res['tasks'][0]
  t['_id'] = t['id']
  OpenStruct.new(t)
end

#tasks_get(task_id) ⇒ Object



302
303
304
305
306
307
308
# File 'lib/iron_worker_ng/client.rb', line 302

def tasks_get(task_id)
  IronCore::Logger.debug 'IronWorkerNG', "Calling tasks.get with task_id='#{task_id}'"

  t = @api.tasks_get(task_id)
  t['_id'] = t['id']
  OpenStruct.new(t)
end

#tasks_list(options = {}) ⇒ Object



296
297
298
299
300
# File 'lib/iron_worker_ng/client.rb', line 296

def tasks_list(options = {})
  IronCore::Logger.debug 'IronWorkerNG', "Calling tasks.list with options='#{options.to_s}'"

  @api.tasks_list(options)['tasks'].map { |t| OpenStruct.new(t.merge('_id' => t['id'])) }
end

#tasks_log(task_id) ⇒ Object



346
347
348
349
350
351
352
353
354
# File 'lib/iron_worker_ng/client.rb', line 346

def tasks_log(task_id)
  IronCore::Logger.debug 'IronWorkerNG', "Calling tasks.log with task_id='#{task_id}'"

  if block_given?
    @api.tasks_log(task_id) { |chunk| yield(chunk) }
  else
    @api.tasks_log(task_id)
  end
end

#tasks_retry(task_id, options = {}) ⇒ Object



380
381
382
383
384
385
386
387
388
# File 'lib/iron_worker_ng/client.rb', line 380

def tasks_retry(task_id, options = {})
  IronCore::Logger.debug 'IronWorkerNG', "Calling tasks.retry with task_id='#{task_id}' and options='#{options.to_s}'"

  res = @api.tasks_retry(task_id, options)

  t = res['tasks'][0]
  t['_id'] = t['id']
  OpenStruct.new(t)
end

#tasks_set_progress(task_id, options = {}) ⇒ Object



356
357
358
359
360
361
362
# File 'lib/iron_worker_ng/client.rb', line 356

def tasks_set_progress(task_id, options = {})
  IronCore::Logger.debug 'IronWorkerNG', "Calling tasks.set_progress with task_id='#{task_id}' and options='#{options.to_s}'"

  @api.tasks_set_progress(task_id, options)

  true
end

#tasks_wait_for(task_id, options = {}, &block) ⇒ Object



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/iron_worker_ng/client.rb', line 364

def tasks_wait_for(task_id, options = {}, &block)
  IronCore::Logger.debug 'IronWorkerNG', "Calling tasks.wait_for with task_id='#{task_id}' and options='#{options.to_s}'"

  options[:sleep] ||= options['sleep'] || 5

  task = tasks_get(task_id)

  while task.status == 'queued' || task.status == 'running'
    block.call(task) unless block.nil?
    sleep options[:sleep]
    task = tasks_get(task_id)
  end

  task
end

#tokenObject



40
41
42
# File 'lib/iron_worker_ng/client.rb', line 40

def token
  @api.token
end