Class: IronWorkerNG::Client
- Inherits:
-
Object
- Object
- IronWorkerNG::Client
- Defined in:
- lib/iron_worker_ng/client.rb
Instance Attribute Summary collapse
-
#api ⇒ Object
readonly
Returns the value of attribute api.
Instance Method Summary collapse
- #codes_create(code, options = {}) ⇒ Object
- #codes_create_async(code, options = {}) ⇒ Object
- #codes_delete(code_id) ⇒ Object
- #codes_download(code_id, options = {}) ⇒ Object
- #codes_get(code_id) ⇒ Object
- #codes_list(options = {}) ⇒ Object
- #codes_patch(name, options = {}) ⇒ Object
- #codes_revisions(code_id, options = {}) ⇒ Object
-
#initialize(options = {}, &block) ⇒ Client
constructor
A new instance of Client.
- #method_missing(name, *args, &block) ⇒ Object
- #options ⇒ Object
- #params_for_legacy(code_name, params) ⇒ Object
- #project_id ⇒ Object
- #projects_get ⇒ Object
- #schedules_cancel(schedule_id) ⇒ Object
- #schedules_create(code_name, params = {}, options = {}) ⇒ Object
- #schedules_create_legacy(code_name, params = {}, options = {}) ⇒ Object
- #schedules_get(schedule_id) ⇒ Object
- #schedules_list(options = {}) ⇒ Object
- #tasks_cancel(task_id) ⇒ Object
- #tasks_cancel_all(code_id) ⇒ Object
- #tasks_create(code_name, params = {}, options = {}) ⇒ Object
- #tasks_create_legacy(code_name, params = {}, options = {}) ⇒ Object
- #tasks_get(task_id) ⇒ Object
- #tasks_list(options = {}) ⇒ Object
- #tasks_log(task_id) ⇒ Object
- #tasks_retry(task_id, options = {}) ⇒ Object
- #tasks_set_progress(task_id, options = {}) ⇒ Object
- #tasks_wait_for(task_id, options = {}, &block) ⇒ Object
- #token ⇒ Object
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( = {}, &block) @api = IronWorkerNG::APIClient.new() 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
48 49 50 51 52 53 54 |
# File 'lib/iron_worker_ng/client.rb', line 48 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
#api ⇒ Object (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
#codes_create(code, options = {}) ⇒ Object
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 |
# File 'lib/iron_worker_ng/client.rb', line 90 def codes_create(code, = {}) IronCore::Logger.debug 'IronWorkerNG', "Calling codes.create with code='#{code.to_s}' and options='#{.to_s}'" if [:config] && [:config].is_a?(Hash) = .dup [:config] = [:config].to_json end 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', ) 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', ) builder_task = tasks.create(builder_code_name, :code_name => code.name, :client_options => @api..to_json, :codes_create_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) res['_id'] = res['id'] OpenStruct.new(res) end |
#codes_create_async(code, options = {}) ⇒ Object
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 |
# File 'lib/iron_worker_ng/client.rb', line 128 def codes_create_async(code, = {}) IronCore::Logger.debug 'IronWorkerNG', "Calling codes.create_async with code='#{code.to_s}' and options='#{.to_s}'" if [:config] && [:config].is_a?(Hash) = .dup [:config] = [:config].to_json end 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', ) 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', ) builder_task = tasks.create(builder_code_name, :code_name => code.name, :client_options => @api..to_json, :codes_create_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
245 246 247 248 249 250 251 |
# File 'lib/iron_worker_ng/client.rb', line 245 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
259 260 261 262 263 |
# File 'lib/iron_worker_ng/client.rb', line 259 def codes_download(code_id, = {}) IronCore::Logger.debug 'IronWorkerNG', "Calling codes.download with code_id='#{code_id}' and options='#{.to_s}'" @api.codes_download(code_id, ) end |
#codes_get(code_id) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/iron_worker_ng/client.rb', line 82 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
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 |
# File 'lib/iron_worker_ng/client.rb', line 56 def codes_list( = {}) IronCore::Logger.debug 'IronWorkerNG', "Calling codes.list with options='#{.to_s}'" all = [:all] || ['all'] if all result = [] page = [:page] || ['page'] || 0 per_page = [:per_page] || ['per_page'] || 100 while true next_codes = codes_list(.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()['codes'].map { |c| OpenStruct.new(c.merge('_id' => c['id'])) } end end |
#codes_patch(name, options = {}) ⇒ Object
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 |
# File 'lib/iron_worker_ng/client.rb', line 158 def codes_patch(name, = {}) IronCore::Logger.debug 'IronWorkerNG', "Calling codes.patch with name='#{name}' and options='#{.to_s}'" code = codes.list(:all => true).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_file = Dir['patch/*'].first system("cd code && patch -fp1 <../\#{patch_file} && cd ..") || exit(1) 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) patcher_code.file([:patch], 'patch') 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..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_revisions(code_id, options = {}) ⇒ Object
253 254 255 256 257 |
# File 'lib/iron_worker_ng/client.rb', line 253 def codes_revisions(code_id, = {}) IronCore::Logger.debug 'IronWorkerNG', "Calling codes.revisions with code_id='#{code_id}' and options='#{.to_s}'" @api.codes_revisions(code_id, )['revisions'].map { |c| OpenStruct.new(c.merge('_id' => c['id'])) } end |
#options ⇒ Object
36 37 38 |
# File 'lib/iron_worker_ng/client.rb', line 36 def @api. end |
#params_for_legacy(code_name, params) ⇒ Object
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'lib/iron_worker_ng/client.rb', line 410 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_id ⇒ Object
44 45 46 |
# File 'lib/iron_worker_ng/client.rb', line 44 def project_id @api.project_id end |
#projects_get ⇒ Object
401 402 403 404 405 406 407 408 |
# File 'lib/iron_worker_ng/client.rb', line 401 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) ⇒ Object
393 394 395 396 397 398 399 |
# File 'lib/iron_worker_ng/client.rb', line 393 def schedules_cancel(schedule_id) IronCore::Logger.debug 'IronWorkerNG', "Calling schedules.cancel with schedule_id='#{schedule_id}" @api.schedules_cancel(schedule_id) true end |
#schedules_create(code_name, params = {}, options = {}) ⇒ Object
373 374 375 376 377 378 379 380 381 |
# File 'lib/iron_worker_ng/client.rb', line 373 def schedules_create(code_name, params = {}, = {}) IronCore::Logger.debug 'IronWorkerNG', "Calling schedules.create with code_name='#{code_name}', params='#{params.to_s}' and options='#{.to_s}'" res = @api.schedules_create(code_name, params.is_a?(String) ? params : params.to_json, ) s = res['schedules'][0] s['_id'] = s['id'] OpenStruct.new(s) end |
#schedules_create_legacy(code_name, params = {}, options = {}) ⇒ Object
383 384 385 386 387 388 389 390 391 |
# File 'lib/iron_worker_ng/client.rb', line 383 def schedules_create_legacy(code_name, params = {}, = {}) IronCore::Logger.debug 'IronWorkerNG', "Calling schedules.create_legacy with code_name='#{code_name}', params='#{params.to_s}' and options='#{.to_s}'" res = @api.schedules_create(code_name, params_for_legacy(code_name, params), ) s = res['schedules'][0] s['_id'] = s['id'] OpenStruct.new(s) end |
#schedules_get(schedule_id) ⇒ Object
365 366 367 368 369 370 371 |
# File 'lib/iron_worker_ng/client.rb', line 365 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
359 360 361 362 363 |
# File 'lib/iron_worker_ng/client.rb', line 359 def schedules_list( = {}) IronCore::Logger.debug 'IronWorkerNG', "Calling schedules.list with options='#{.to_s}'" @api.schedules_list()['schedules'].map { |s| OpenStruct.new(s) } end |
#tasks_cancel(task_id) ⇒ Object
299 300 301 302 303 304 305 |
# File 'lib/iron_worker_ng/client.rb', line 299 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
307 308 309 310 311 312 313 |
# File 'lib/iron_worker_ng/client.rb', line 307 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
279 280 281 282 283 284 285 286 287 |
# File 'lib/iron_worker_ng/client.rb', line 279 def tasks_create(code_name, params = {}, = {}) IronCore::Logger.debug 'IronWorkerNG', "Calling tasks.create with code_name='#{code_name}', params='#{params.to_s}' and options='#{.to_s}'" res = @api.tasks_create(code_name, params.is_a?(String) ? params : params.to_json, ) t = res['tasks'][0] t['_id'] = t['id'] OpenStruct.new(t) end |
#tasks_create_legacy(code_name, params = {}, options = {}) ⇒ Object
289 290 291 292 293 294 295 296 297 |
# File 'lib/iron_worker_ng/client.rb', line 289 def tasks_create_legacy(code_name, params = {}, = {}) IronCore::Logger.debug 'IronWorkerNG', "Calling tasks.create_legacy with code_name='#{code_name}', params='#{params.to_s}' and options='#{.to_s}'" res = @api.tasks_create(code_name, params_for_legacy(code_name, params), ) t = res['tasks'][0] t['_id'] = t['id'] OpenStruct.new(t) end |
#tasks_get(task_id) ⇒ Object
271 272 273 274 275 276 277 |
# File 'lib/iron_worker_ng/client.rb', line 271 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
265 266 267 268 269 |
# File 'lib/iron_worker_ng/client.rb', line 265 def tasks_list( = {}) IronCore::Logger.debug 'IronWorkerNG', "Calling tasks.list with options='#{.to_s}'" @api.tasks_list()['tasks'].map { |t| OpenStruct.new(t.merge('_id' => t['id'])) } end |
#tasks_log(task_id) ⇒ Object
315 316 317 318 319 320 321 322 323 |
# File 'lib/iron_worker_ng/client.rb', line 315 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
349 350 351 352 353 354 355 356 357 |
# File 'lib/iron_worker_ng/client.rb', line 349 def tasks_retry(task_id, = {}) IronCore::Logger.debug 'IronWorkerNG', "Calling tasks.retry with task_id='#{task_id}' and options='#{.to_s}'" res = @api.tasks_retry(task_id, ) t = res['tasks'][0] t['_id'] = t['id'] OpenStruct.new(t) end |
#tasks_set_progress(task_id, options = {}) ⇒ Object
325 326 327 328 329 330 331 |
# File 'lib/iron_worker_ng/client.rb', line 325 def tasks_set_progress(task_id, = {}) IronCore::Logger.debug 'IronWorkerNG', "Calling tasks.set_progress with task_id='#{task_id}' and options='#{.to_s}'" @api.tasks_set_progress(task_id, ) true end |
#tasks_wait_for(task_id, options = {}, &block) ⇒ Object
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/iron_worker_ng/client.rb', line 333 def tasks_wait_for(task_id, = {}, &block) IronCore::Logger.debug 'IronWorkerNG', "Calling tasks.wait_for with task_id='#{task_id}' and options='#{.to_s}'" [:sleep] ||= ['sleep'] || 5 task = tasks_get(task_id) while task.status == 'queued' || task.status == 'running' block.call(task) unless block.nil? sleep [:sleep] task = tasks_get(task_id) end task end |
#token ⇒ Object
40 41 42 |
# File 'lib/iron_worker_ng/client.rb', line 40 def token @api.token end |