Class: IronWorkerNG::CLI
- Inherits:
-
Object
- Object
- IronWorkerNG::CLI
- Defined in:
- lib/iron_worker_ng/cli.rb
Constant Summary collapse
- LOG_ENTRY =
' '- LOG_GROUP =
'------> '
Instance Method Summary collapse
- #beta? ⇒ Boolean
- #client ⇒ Object
- #config=(config) ⇒ Object
- #display_table(t) ⇒ Object
- #env=(env) ⇒ Object
- #get_code_by_name(name) ⇒ Object
- #getlog(task_id, params, options) ⇒ Object
- #http_proxy=(http_proxy) ⇒ Object
- #info_code(name, params, options) ⇒ Object
- #info_schedule(schedule_id, params, options) ⇒ Object
- #info_task(task_id, params, options) ⇒ Object
-
#initialize ⇒ CLI
constructor
A new instance of CLI.
- #install(name, params, options) ⇒ Object
- #log(msg) ⇒ Object
- #log_group(msg) ⇒ Object
- #parse_time(s) ⇒ Object
- #patch(name, params, options) ⇒ Object
- #pause(name, params, options) ⇒ Object
- #project_id=(project_id) ⇒ Object
- #queue(name, params, options) ⇒ Object
- #resume(name, params, options) ⇒ Object
- #retry(task_id, params, options) ⇒ Object
- #run(name, params, options) ⇒ Object
- #schedule(name, params, options) ⇒ Object
- #stacks_list ⇒ Object
- #token=(token) ⇒ Object
- #update_schedule(schedule_id, params) ⇒ Object
- #upload(name, params, options) ⇒ Object
- #webhook(name, params, options) ⇒ Object
Constructor Details
#initialize ⇒ CLI
Returns a new instance of CLI.
6 7 8 9 10 11 12 13 14 |
# File 'lib/iron_worker_ng/cli.rb', line 6 def initialize @client = nil @config = nil @env = nil @project_id = nil @token = nil @http_proxy = nil end |
Instance Method Details
#beta? ⇒ Boolean
16 17 18 19 20 |
# File 'lib/iron_worker_ng/cli.rb', line 16 def beta? beta = ENV['IRON_BETA'] beta == '1' || beta == 1 || beta == 'true' || beta == true || beta == 'beta' end |
#client ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/iron_worker_ng/cli.rb', line 59 def client if @client.nil? log_group "Creating client" @client = IronWorkerNG::Client.new(:config => @config, :env => @env, :project_id => @project_id, :token => @token, :http_proxy => @http_proxy ) project = client.projects.get log "Project '#{project.name}' with id='#{project._id}'" end @client end |
#config=(config) ⇒ Object
22 23 24 25 26 |
# File 'lib/iron_worker_ng/cli.rb', line 22 def config=(config) @client = nil @config = config end |
#display_table(t) ⇒ Object
391 392 393 394 395 |
# File 'lib/iron_worker_ng/cli.rb', line 391 def display_table(t) t.each do |r| log sprintf('%-16s %s', r[0], r[1]) end end |
#env=(env) ⇒ Object
28 29 30 31 32 |
# File 'lib/iron_worker_ng/cli.rb', line 28 def env=(env) @client = nil @env = env end |
#get_code_by_name(name) ⇒ Object
369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/iron_worker_ng/cli.rb', line 369 def get_code_by_name(name) client codes = client.codes.list({:all => true}) code = codes.find { |code| code.name == name } unless code log "Code package with name='#{name}' not found" exit 1 end code end |
#getlog(task_id, params, options) ⇒ Object
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 |
# File 'lib/iron_worker_ng/cli.rb', line 193 def getlog(task_id, params, ) client wait = [:wait] || ['wait'] log_group "Getting log for task with id='#{task_id}'" log = '' if wait begin log = client.tasks.log(task_id) rescue end else log = client.tasks.log(task_id) end print log if wait client.tasks.wait_for(task_id) do |task| if task.status == 'running' begin next_log = client.tasks.log(task_id) print next_log[log.length .. - 1] log = next_log rescue end end end begin next_log = client.tasks.log(task_id) print next_log[log.length .. - 1] rescue end end end |
#http_proxy=(http_proxy) ⇒ Object
46 47 48 49 |
# File 'lib/iron_worker_ng/cli.rb', line 46 def http_proxy=(http_proxy) @client = nil @http_proxy = http_proxy end |
#info_code(name, params, options) ⇒ Object
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 |
# File 'lib/iron_worker_ng/cli.rb', line 266 def info_code(name, params, ) client log_group 'Getting code package info' codes = client.codes.list({:all => true}) code = codes.find { |code| code.name == name } unless code log "Code package with name='#{name}' not found" exit 1 end data = [] data << ['id', code._id] data << ['name', code.name] data << ['revision', code.rev] data << ['uploaded', parse_time(code.latest_change) || '-'] data << ['max concurrency', code.max_concurrency || '-'] data << ['retries', code.retries || '-'] data << ['retries delay', code.retries_delay || '-'] data << ['info', "https://hud.iron.io/tq/projects/#{client.api.project_id}/code/#{code._id}"] data << ['tasks info', "https://hud.iron.io/tq/projects/#{client.api.project_id}/jobs/#{code._id}/activity"] display_table(data) end |
#info_schedule(schedule_id, params, options) ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/iron_worker_ng/cli.rb', line 316 def info_schedule(schedule_id, params, ) client log_group 'Getting schedule info' schedule = client.schedules.get(schedule_id) data = [] data << ['id', schedule._id] data << ['code package', schedule.code_name] data << ['status', schedule.status] data << ['label', schedule.label] if schedule.label data << ['created', parse_time(schedule.created_at) || '-'] data << ['next start', parse_time(schedule.next_start) || '-'] data << ['run count', schedule.run_count || '-'] data << ['payload', schedule.payload] data << ['info', "https://hud.iron.io/tq/projects/#{client.api.project_id}/scheduled_jobs/#{schedule._id}"] display_table(data) end |
#info_task(task_id, params, options) ⇒ Object
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/iron_worker_ng/cli.rb', line 293 def info_task(task_id, params, ) client log_group 'Getting task info' task = client.tasks.get(task_id) data = [] data << ['id', task._id] data << ['code package', task.code_name] data << ['code revision', task.code_rev] data << ['status', task.status] data << ['label', task.label] if task.label data << ['priority', task.priority || 2] data << ['queued', parse_time(task.created_at) || '-'] data << ['started', parse_time(task.start_time) || '-'] data << ['finished', parse_time(task.end_time) || '-'] data << ['payload', task.payload] data << ['info', "https://hud.iron.io/tq/projects/#{client.api.project_id}/jobs/#{task._id}"] display_table(data) end |
#install(name, params, options) ⇒ Object
249 250 251 252 253 254 255 |
# File 'lib/iron_worker_ng/cli.rb', line 249 def install(name, params, ) log_group "Installing dependencies for code package with name='#{name}'" code = IronWorkerNG::Code::Base.new(name) code.install end |
#log(msg) ⇒ Object
51 52 53 |
# File 'lib/iron_worker_ng/cli.rb', line 51 def log(msg) IronCore::Logger.info 'IronWorkerNG', msg end |
#log_group(msg) ⇒ Object
55 56 57 |
# File 'lib/iron_worker_ng/cli.rb', line 55 def log_group(msg) IronCore::Logger.info 'IronWorkerNG', LOG_GROUP + msg end |
#parse_time(s) ⇒ Object
383 384 385 386 387 388 389 |
# File 'lib/iron_worker_ng/cli.rb', line 383 def parse_time(s) t = Time.parse(s) return nil if t == Time.utc(1) t end |
#patch(name, params, options) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/iron_worker_ng/cli.rb', line 125 def patch(name, params, ) client log_group "Patching code package '#{name}'" code_id = client.codes.patch(name, )._id code_info = client.codes.get(code_id) log "Patched code package uploaded with id='#{code_id}' and revision='#{code_info.rev}'" log "Check 'https://hud.iron.io/tq/projects/#{client.api.project_id}/code/#{code_id}' for more info" end |
#pause(name, params, options) ⇒ Object
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/iron_worker_ng/cli.rb', line 337 def pause(name, params, ) code = get_code_by_name(name) log "Pausing task queue for code '#{code._id}'" response = client.codes.pause_task_queue(code._id, ) if response.msg == 'Paused' log "Task queue and schedules for #{name} paused successfully" elsif response.msg == 'Already paused' log "Task queue and schedules for #{name} are already paused" else log 'Something went wrong' end log "Check 'https://hud.iron.io/tq/projects/#{client.api.project_id}/code/#{code._id}' for more info" end |
#project_id=(project_id) ⇒ Object
34 35 36 37 38 |
# File 'lib/iron_worker_ng/cli.rb', line 34 def project_id=(project_id) @client = nil @project_id = project_id end |
#queue(name, params, options) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/iron_worker_ng/cli.rb', line 137 def queue(name, params, ) client log_group "Queueing task" id = client.tasks.create(name, params[:payload] || params['payload'], )._id log "Code package '#{name}' queued with id='#{id}'" log "Check 'https://hud.iron.io/tq/projects/#{client.api.project_id}/jobs/#{id}' for more info" if [:wait] == true getlog(id, {}, {:wait => true}) end end |
#resume(name, params, options) ⇒ Object
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/iron_worker_ng/cli.rb', line 353 def resume(name, params, ) code = get_code_by_name(name) log "Resuming task queue for code '#{code._id}'" response = client.codes.resume_task_queue(code._id, ) if response.msg == 'Resumed' log "Task queue and schedules for #{name} resumed successfully" elsif response.msg == 'Already resumed' log "Task queue and schedules for #{name} are already resumed" else log 'Something went wrong' end log "Check 'https://hud.iron.io/tq/projects/#{client.api.project_id}/code/#{code._id}' for more info" end |
#retry(task_id, params, options) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/iron_worker_ng/cli.rb', line 163 def retry(task_id, params, ) client log_group "Retrying task with id='#{task_id}'" retry_task_id = client.tasks.retry(task_id, )._id log "Task retried with id='#{retry_task_id}'" log "Check 'https://hud.iron.io/tq/projects/#{client.api.project_id}/jobs/#{retry_task_id}' for more info" if [:wait] == true getlog(retry_task_id, {}, {:wait => true}) end end |
#run(name, params, options) ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/iron_worker_ng/cli.rb', line 233 def run(name, params, ) log_group "Creating code package" code = IronWorkerNG::Code::Base.new(name) log "Code package name is '#{code.name}'" log_group "Running '#{code.name}'" if [:worker_config] log "Loading worker_config at #{[:worker_config]}" c = IO.read([:worker_config]) [:config] = c end code.run(params[:payload] || params['payload'], [:config] || ['config']) end |
#schedule(name, params, options) ⇒ Object
152 153 154 155 156 157 158 159 160 161 |
# File 'lib/iron_worker_ng/cli.rb', line 152 def schedule(name, params, ) client log_group "Scheduling task" id = client.schedules.create(name, params[:payload] || params['payload'], )._id log "Code package '#{name}' scheduled with id='#{id}'" log "Check 'https://hud.iron.io/tq/projects/#{client.api.project_id}/scheduled_jobs/#{id}' for more info" end |
#stacks_list ⇒ Object
75 76 77 78 79 |
# File 'lib/iron_worker_ng/cli.rb', line 75 def stacks_list client log_group "List of available stacks" puts client.stacks_list.map{ |stack| "* #{stack}" }.join("\n") end |
#token=(token) ⇒ Object
40 41 42 43 44 |
# File 'lib/iron_worker_ng/cli.rb', line 40 def token=(token) @client = nil @token = token end |
#update_schedule(schedule_id, params) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/iron_worker_ng/cli.rb', line 178 def update_schedule(schedule_id, params) client log_group "Updating scheduled task with id=#{schedule_id}" response = client.schedules.update(schedule_id, JSON.parse(params[:schedule], :symbolize_names => true)) if response.msg == 'Updated' log 'Scheduled task updated successfully' log "Check 'https://hud.iron.io/tq/projects/#{client.api.project_id}/scheduled_jobs/#{schedule_id}' for more info" else log 'Something went wrong' end end |
#upload(name, params, options) ⇒ Object
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 |
# File 'lib/iron_worker_ng/cli.rb', line 81 def upload(name, params, ) client log_group "Creating code package" code = IronWorkerNG::Code::Base.new(:name => name, :env => @env) code.name(params[:name]) unless params[:name].nil? code.full_remote_build = params[:full_remote_build] unless params[:full_remote_build].nil? log "Code package name is '#{code.name}'" log "Max concurrency set to '#{[:max_concurrency]}'" unless [:max_concurrency].nil? log "Default priority set to '#{[:default_priority]}'" unless [:default_priority].nil? log "Retries set to '#{[:retries]}'" unless [:retries].nil? log "Retries delay set to '#{[:retries_delay]}'" unless [:retries_delay].nil? log "Host set to '#{[:host]}'" unless [:host].nil? if [:worker_config] log "Loading worker_config at #{[:worker_config]}" c = IO.read([:worker_config]) [:config] = c end if code.remote_build_command || code.full_remote_build log_group "Uploading and building code package '#{code.name}'" else log_group "Uploading code package '#{code.name}'" end if (params[:async] || params['async']) && code.remote_build_command builder_task_id = client.codes.create_async(code, ) log 'Code package is building' log "Check 'https://hud.iron.io/tq/projects/#{client.api.project_id}/tasks/#{builder_task_id}' for more info" else code_id = client.codes.create(code, )._id code_info = client.codes.get(code_id) log "Code package uploaded with id='#{code_id}' and revision='#{code_info.rev}'" log "Check 'https://hud.iron.io/tq/projects/#{client.api.project_id}/code/#{code_id}' for more info" end end |
#webhook(name, params, options) ⇒ Object
257 258 259 260 261 262 263 264 |
# File 'lib/iron_worker_ng/cli.rb', line 257 def webhook(name, params, ) client log_group 'Generating code package webhook' log 'You can invoke your worker by POSTing to the following URL' log client.api.url("projects/#{client.api.project_id}/tasks/webhook?code_name=#{name}&oauth=#{client.api.token}") end |