hammer_cli_foreman_tasks 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,5 @@
1
+ This program and entire repository is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.
2
+
3
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
4
+
5
+ You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ Hammer CLI Foreman Tasks
2
+ ========================
3
+
4
+ Showing of the tasks (results and progress) in the Hammer CLI.
5
+
6
+ Allows waiting for async task after the task was triggered.
7
+
8
+ Usage:
9
+
10
+ ```ruby
11
+ class MyAsyncCommand < HammerCLIForemanTasks::AsyncCommand
12
+ action "run"
13
+ command_name "run"
14
+
15
+ success_message "Task started with id %{id}s"
16
+ failure_message "Could not run the task"
17
+
18
+ apipie_options
19
+ end
20
+ ```
21
+
22
+ Also, there is `HammerCLIForemanTasks::Helper` with helper methods, if
23
+ the `AsyncCommand` class doesn't fit for the case.
24
+
25
+ The `AsyncCommand` comes with `--async` option so that the command
26
+ doesn't wait for the task to finish.
27
+
28
+ There is a `task` command with `progress` action available, showing the
29
+ progress for action based on id.
30
+
31
+ Usage:
32
+
33
+ # wait for task to finish (showing the progress)
34
+ hammer task progress --id 1234-5678-7654-3210
@@ -0,0 +1,18 @@
1
+ module HammerCLIForemanTasks
2
+ class AsyncCommand < HammerCLIForeman::WriteCommand
3
+ include HammerCLIForemanTasks::Helper
4
+
5
+ option '--async', :flag, 'Do not wait for the task'
6
+
7
+ def execute
8
+ if option_async?
9
+ super
10
+ else
11
+ task_progress(send_request)
12
+ HammerCLI::EX_OK
13
+ end
14
+ end
15
+
16
+ apipie_options
17
+ end
18
+ end
@@ -0,0 +1,34 @@
1
+ module HammerCLIForemanTasks
2
+ module Helper
3
+ # render the progress of the task using polling to the task API
4
+ def task_progress(task_or_id)
5
+ task_id = task_or_id.is_a?(Hash) ? task_or_id['id'] : task_or_id
6
+ TaskProgress.new(task_id) { |id| load_task(id) }.tap do |task_progress|
7
+ task_progress.render
8
+ end
9
+ end
10
+
11
+ def load_task(id)
12
+ options = resource_config.merge(resource_config[:credentials].to_params)
13
+ client = ForemanApi::Base.new(options)
14
+ flatten_task(client.http_call(:get, "/foreman_tasks/api/tasks/#{id}").first)
15
+ end
16
+
17
+ def send_request
18
+ flatten_task(super)
19
+ end
20
+
21
+ # make sure to flatten the nested data, to use it easily in
22
+ # success_message, such as
23
+ #
24
+ # success_messasge "Repository is being synchronized in task %{id}s"
25
+ #
26
+ def flatten_task(task)
27
+ if task.key?('id')
28
+ return task
29
+ else
30
+ return task.values.first
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,22 @@
1
+ module HammerCLIForemanTasks
2
+ class Task < HammerCLI::AbstractCommand
3
+ class ProgressCommand < HammerCLIForeman::ReadCommand
4
+
5
+ include HammerCLIForemanTasks::Helper
6
+
7
+ command_name "progress"
8
+ desc "Show the progress of the task"
9
+ option '--id', "UUID", "UUID of the task", :required => true
10
+
11
+ def execute
12
+ task_progress(option_id)
13
+ HammerCLI::EX_OK
14
+ end
15
+
16
+ end
17
+
18
+ autoload_subcommands
19
+ end
20
+
21
+ HammerCLI::MainCommand.subcommand 'task', "Tasks related actions.", Task
22
+ end
@@ -0,0 +1,69 @@
1
+ require 'powerbar'
2
+ module HammerCLIForemanTasks
3
+ class TaskProgress
4
+ attr_accessor :interval, :task
5
+
6
+ def initialize(task_id, &block)
7
+ @update_block = block
8
+ @task_id = task_id
9
+ @interval = 2
10
+ end
11
+
12
+ def render
13
+ update_task
14
+ if task_pending?
15
+ render_progress
16
+ else
17
+ render_result
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def render_progress
24
+ progress_bar do |bar|
25
+ begin
26
+ while true
27
+ bar.show(:msg => "Task #{@task_id} progress", :done => @task['progress'].to_f, :total => 1)
28
+ if task_pending?
29
+ sleep interval
30
+ update_task
31
+ else
32
+ break
33
+ end
34
+ end
35
+ rescue Interrupt
36
+ # Inerrupting just means we stop rednering the progress bar
37
+ end
38
+ end
39
+ end
40
+
41
+ def render_result
42
+ puts "Task %{uuid}: %{result}" % { :uuid => @task_id, :result => @task['result'] }
43
+ unless @task['humanized']['output'].to_s.empty?
44
+ puts @task['humanized']['output']
45
+ end
46
+ end
47
+
48
+ def update_task
49
+ @task = @update_block.call(@task_id)
50
+ end
51
+
52
+ def task_pending?
53
+ !%w[paused stopped].include?(@task['state'])
54
+ end
55
+
56
+ def progress_bar
57
+ bar = PowerBar.new
58
+ @closed = false
59
+ bar.settings.tty.finite.template.main = '[${<bar>}] [${<percent>%}]'
60
+ bar.settings.tty.finite.template.padchar = ' '
61
+ bar.settings.tty.finite.template.barchar = '.'
62
+ bar.settings.tty.finite.output = Proc.new { |s| $stderr.print s }
63
+ yield bar
64
+ ensure
65
+ bar.close
66
+ render_result
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,3 @@
1
+ module HammerCLIForemanTasks
2
+ VERSION = '0.0.1' unless defined? VERSION
3
+ end
@@ -0,0 +1,6 @@
1
+ module HammerCLIForemanTasks
2
+ require 'hammer_cli_foreman_tasks/helper'
3
+ require 'hammer_cli_foreman_tasks/task_progress'
4
+ require 'hammer_cli_foreman_tasks/async_command'
5
+ require 'hammer_cli_foreman_tasks/task'
6
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hammer_cli_foreman_tasks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ivan Nečas
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-02-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: powerbar
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: hammer_cli_foreman
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: ! 'Contains the code for showing of the tasks (results and progress)
47
+ in the Hammer CLI.
48
+
49
+ '
50
+ email:
51
+ - inecas@redhat.com
52
+ executables: []
53
+ extensions: []
54
+ extra_rdoc_files: []
55
+ files:
56
+ - lib/hammer_cli_foreman_tasks.rb
57
+ - lib/hammer_cli_foreman_tasks/task_progress.rb
58
+ - lib/hammer_cli_foreman_tasks/version.rb
59
+ - lib/hammer_cli_foreman_tasks/async_command.rb
60
+ - lib/hammer_cli_foreman_tasks/helper.rb
61
+ - lib/hammer_cli_foreman_tasks/task.rb
62
+ - LICENSE
63
+ - README.md
64
+ homepage: https://github.com/iNecas/hammer-cli-foreman-tasks
65
+ licenses:
66
+ - GPL v3+
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ segments:
78
+ - 0
79
+ hash: 1902630174674946817
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ segments:
87
+ - 0
88
+ hash: 1902630174674946817
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 1.8.23
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Foreman CLI plugin for showing tasks information for resoruces and users
95
+ test_files: []