foreman-mco 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. checksums.yaml +15 -0
  2. data/Rakefile +40 -0
  3. data/app/assets/javascripts/foreman_mco/application.js +22 -0
  4. data/app/assets/javascripts/foreman_mco/mco_hosts_extensions.js +49 -0
  5. data/app/assets/stylesheets/foreman-mco/application.css +13 -0
  6. data/app/controllers/foreman_mco/command_histories_controller.rb +26 -0
  7. data/app/controllers/foreman_mco/command_statuses_controller.rb +49 -0
  8. data/app/controllers/foreman_mco/commands_controller.rb +48 -0
  9. data/app/controllers/foreman_mco/detailed_command_histories_controller.rb +27 -0
  10. data/app/helpers/foreman_mco/application_helper.rb +4 -0
  11. data/app/helpers/foreman_mco/command_histories_helper.rb +6 -0
  12. data/app/models/concerns/foreman_mco/hosts_helper.rb +25 -0
  13. data/app/models/foreman_mco/command/base.rb +24 -0
  14. data/app/models/foreman_mco/command/filter.rb +15 -0
  15. data/app/models/foreman_mco/command/package_commands.rb +33 -0
  16. data/app/models/foreman_mco/command/ping.rb +11 -0
  17. data/app/models/foreman_mco/command/puppet_commands.rb +31 -0
  18. data/app/models/foreman_mco/command/service_commands.rb +43 -0
  19. data/app/models/foreman_mco/command_status.rb +27 -0
  20. data/app/models/foreman_mco/commands.rb +4 -0
  21. data/app/models/foreman_mco/host_command_status.rb +12 -0
  22. data/app/models/services/foreman_mco/mco_proxy_api.rb +57 -0
  23. data/app/models/setting/mco.rb +15 -0
  24. data/app/overrides/enable_mco_actions_on_hosts.rb +9 -0
  25. data/app/views/foreman_mco/command_histories/index.html.erb +46 -0
  26. data/app/views/foreman_mco/commands/install_package.html.erb +9 -0
  27. data/app/views/foreman_mco/commands/ping.html.erb +6 -0
  28. data/app/views/foreman_mco/commands/puppet_disable.html.erb +8 -0
  29. data/app/views/foreman_mco/commands/puppet_enable.html.erb +8 -0
  30. data/app/views/foreman_mco/commands/puppet_runonce.html.erb +8 -0
  31. data/app/views/foreman_mco/commands/service_status.html.erb +9 -0
  32. data/app/views/foreman_mco/commands/start_service.html.erb +9 -0
  33. data/app/views/foreman_mco/commands/stop_service.html.erb +9 -0
  34. data/app/views/foreman_mco/commands/uninstall_package.html.erb +9 -0
  35. data/app/views/foreman_mco/detailed_command_histories/index.html.erb +26 -0
  36. data/app/views/layouts/foreman-mco/application.html.erb +14 -0
  37. data/config/routes.rb +24 -0
  38. data/db/migrate/20131030154603_create_command_statuses.rb +15 -0
  39. data/db/migrate/20131031113547_add_mcollective_proxy_feature.rb +8 -0
  40. data/db/migrate/20131125123644_create_host_command_statuses.rb +17 -0
  41. data/db/migrate/20131209192923_add_discovered_agents.rb +20 -0
  42. data/lib/foreman-mco.rb +4 -0
  43. data/lib/foreman-mco/engine.rb +60 -0
  44. data/lib/foreman-mco/version.rb +3 -0
  45. data/lib/tasks/foreman-mco_tasks.rake +4 -0
  46. data/test/dummy/README.rdoc +261 -0
  47. data/test/dummy/Rakefile +7 -0
  48. data/test/dummy/app/assets/javascripts/application.js +15 -0
  49. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  50. data/test/dummy/app/controllers/application_controller.rb +3 -0
  51. data/test/dummy/app/helpers/application_helper.rb +2 -0
  52. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  53. data/test/dummy/config.ru +4 -0
  54. data/test/dummy/config/application.rb +59 -0
  55. data/test/dummy/config/boot.rb +10 -0
  56. data/test/dummy/config/database.yml +25 -0
  57. data/test/dummy/config/environment.rb +5 -0
  58. data/test/dummy/config/environments/development.rb +37 -0
  59. data/test/dummy/config/environments/production.rb +67 -0
  60. data/test/dummy/config/environments/test.rb +37 -0
  61. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  62. data/test/dummy/config/initializers/inflections.rb +15 -0
  63. data/test/dummy/config/initializers/mime_types.rb +5 -0
  64. data/test/dummy/config/initializers/secret_token.rb +7 -0
  65. data/test/dummy/config/initializers/session_store.rb +8 -0
  66. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  67. data/test/dummy/config/locales/en.yml +5 -0
  68. data/test/dummy/config/routes.rb +4 -0
  69. data/test/dummy/public/404.html +26 -0
  70. data/test/dummy/public/422.html +26 -0
  71. data/test/dummy/public/500.html +25 -0
  72. data/test/dummy/public/favicon.ico +0 -0
  73. data/test/dummy/script/rails +6 -0
  74. data/test/foreman-mco_test.rb +7 -0
  75. data/test/integration/navigation_test.rb +10 -0
  76. data/test/test_helper.rb +15 -0
  77. metadata +181 -0
@@ -0,0 +1,43 @@
1
+ module ForemanMco::Command
2
+ class ServiceBase < Base
3
+ include ::ActiveModel::Validations
4
+
5
+ attr_reader :service_name
6
+ validates :service_name, :presence => true
7
+
8
+ def initialize(attrs)
9
+ super
10
+ @service_name = attrs[:service_name]
11
+ end
12
+ end
13
+
14
+ class ServiceStatus < ServiceBase
15
+ def remote_call
16
+ mco_proxy.service_status(service_name, filters)
17
+ end
18
+
19
+ def to_s
20
+ "service status #{service_name}"
21
+ end
22
+ end
23
+
24
+ class StartService < ServiceBase
25
+ def remote_call
26
+ mco_proxy.start_service(service_name, filters)
27
+ end
28
+
29
+ def to_s
30
+ "service start #{service_name}"
31
+ end
32
+ end
33
+
34
+ class StopService < ServiceBase
35
+ def remote_call
36
+ mco_proxy.stop_service(service_name, filters)
37
+ end
38
+
39
+ def to_s
40
+ "service stop #{service_name}"
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,27 @@
1
+ class ForemanMco::CommandStatus < ActiveRecord::Base
2
+ include ::Taxonomix
3
+
4
+ PENDING = "pending"
5
+ SUCCESS = "success"
6
+ WARNING = "warning"
7
+ FAILURE = "failure"
8
+
9
+ TYPES = [PENDING, SUCCESS, WARNING, FAILURE]
10
+
11
+ has_many :host_command_statuses
12
+ before_validation :set_default_status
13
+
14
+ validates :command, :jid, :presence => true
15
+ validates :status, :inclusion => { :in => TYPES }
16
+
17
+ default_scope lambda { order('foreman_mco_command_statuses.updated_at') }
18
+
19
+ scoped_search :on => :command, :complete_value => :true
20
+ scoped_search :on => :status, :complete_value => :true
21
+ scoped_search :on => :updated_at
22
+ scoped_search :on => :created_at
23
+
24
+ def set_default_status
25
+ self.status = PENDING if new_record?
26
+ end
27
+ end
@@ -0,0 +1,4 @@
1
+ require 'foreman_mco/command/package_commands'
2
+ require 'foreman_mco/command/service_commands'
3
+ require 'foreman_mco/command/puppet_commands'
4
+ require 'foreman_mco/command/ping'
@@ -0,0 +1,12 @@
1
+ module ForemanMco
2
+ class HostCommandStatus < ActiveRecord::Base
3
+ belongs_to :command_status
4
+
5
+ default_scope lambda { order('foreman_mco_host_command_statuses.created_at') }
6
+
7
+ scoped_search :on => :host, :complete_value => :true
8
+ scoped_search :on => :status_code, :complete_value => :true
9
+ scoped_search :on => :status_message, :complete_value => :true
10
+ scoped_search :on => :created_at
11
+ end
12
+ end
@@ -0,0 +1,57 @@
1
+ class ForemanMco::McoProxyApi < ::ProxyAPI::Resource
2
+ def initialize args
3
+ @url = args[:url] + "/mcollective"
4
+ super args
5
+ end
6
+
7
+ def test
8
+ parse(resource["test/test_command"].post(:name => "blah", 'params' => {:filters => filters}))
9
+ end
10
+
11
+ def install_package(package_name, filters)
12
+ parse(resource["packages/#{package_name}"].post({}, 'params' => {:filters => filters}))
13
+ end
14
+
15
+ def uninstall_package(package_name, filters)
16
+ parse(resource["packages/#{package_name}"].delete('params' => {:filters => filters}))
17
+ end
18
+
19
+ def service_status(service_name, filters)
20
+ parse(resource[ "services/#{service_name}"].get('params' => {:filters => filters}))
21
+ end
22
+
23
+ def start_service(service_name, filters)
24
+ parse(resource["services/#{service_name}/start"].post({}, 'params' => {:filters => filters}))
25
+ end
26
+
27
+ def stop_service(service_name, filters)
28
+ parse(resource["services/#{service_name}/stop"].post({}, 'params' => {:filters => filters}))
29
+ end
30
+
31
+ def puppet_runonce(filters)
32
+ parse(resource["puppet/runonce"].post({}, 'params' => {:filters => filters}))
33
+ end
34
+
35
+ def puppet_enable(filters)
36
+ parse(resource["puppet/enable"].post({}, 'params' => {:filters => filters}))
37
+ end
38
+
39
+ def puppet_disable(filters)
40
+ parse(resource["puppet/disable"].post({}, 'params' => {:filters => filters}))
41
+ end
42
+
43
+ def ping(filters)
44
+ parse(resource["ping"].get('params' => {:filters => filters}))
45
+ end
46
+
47
+ def parse(response)
48
+ if response and response.code >= 200 and response.code < 300
49
+ response.body.present? ? (JSON.parse(response.body) rescue response.body) : true
50
+ else
51
+ false
52
+ end
53
+ rescue => e
54
+ logger.warn "Failed to parse response: #{response} -> #{e}"
55
+ false
56
+ end
57
+ end
@@ -0,0 +1,15 @@
1
+ class Setting::Mco < ::Setting
2
+ BLANK_ATTRS << "use_mco"
3
+
4
+ def self.load_defaults
5
+ return unless ActiveRecord::Base.connection.table_exists?('settings')
6
+ return unless super
7
+
8
+ Setting.transaction do
9
+ [
10
+ self.set('use_mco', "Use MCO to execute remote commands", false),
11
+ ].compact.each { |s| self.create s.update(:category => "Setting::Mco") }
12
+ end
13
+ true
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ Deface::Override.new(:virtual_path => "hosts/index",
2
+ :name => "add_mco_hosts_actions",
3
+ :replace => "erb[silent]:contains('title_actions')",
4
+ :text => '<% title_actions mco_multiple_actions_select, multiple_actions_select, (link_to_if_authorized _("New Host"), hash_for_new_host_path) %>')
5
+
6
+ Deface::Override.new(:virtual_path => "hosts/_list",
7
+ :name => "support_mco_hosts_checkboxes",
8
+ :insert_before => 'table',
9
+ :text => '<%= javascript "foreman_mco/mco_hosts_extensions" %>')
@@ -0,0 +1,46 @@
1
+ <%= javascript "foreman_mco/mcollective_history" %>
2
+ <% title _("MCollective History") %>
3
+
4
+ <% if any_commands_run? %>
5
+ <table class="table table-bordered table-striped">
6
+ <tr>
7
+ <th><%= sort :command, :as => s_("Agent/Command") %></th>
8
+ <th><%= sort :created_at, :as => _("Start Time") %></th>
9
+ <th><%= sort :updated_at, :as => _("Last Updated") %></th>
10
+ <th><%= sort :status, :as => _("Status") %></th>
11
+ <th><%= _('Result') %></th>
12
+ </tr>
13
+ <% for command_status in @command_statuses %>
14
+ <tr>
15
+ <td><%= link_to_if(!command_status.result.empty?, command_status.command, command_history_detailed_command_histories_path(command_status)) do
16
+ command_status.command
17
+ end
18
+ %></td>
19
+ <td><%= time_ago_in_words(command_status.created_at.getlocal) %></td>
20
+ <td><%= time_ago_in_words(command_status.updated_at.getlocal) %></td>
21
+ <td><%= command_status.status %></td>
22
+ <td><%= command_status.result %></td>
23
+ </tr>
24
+ <% end %>
25
+ </table>
26
+
27
+ <div id="confirmation-modal" class="modal hide fade">
28
+ <div class="modal-header">
29
+ <a href="#" class="close" data-dismiss="modal">×</a>
30
+ <h3><%= _('Please Confirm') %></h3>
31
+ </div>
32
+ <div class="modal-body"></div>
33
+ <div class="modal-footer">
34
+ <a href="#" class="btn btn-primary"><%= _('Submit') %></a>
35
+ <a href="#" class="btn secondary"><%= _('Cancel') %></a>
36
+ </div>
37
+ </div>
38
+ <% else %>
39
+ <div class="alert">
40
+ <button type="button" class="close" data-dismiss="alert">&times;</button>
41
+ <strong>No commands have been run yet!</strong> Select a command from the "Select Action" dropdown above to run your first command.
42
+ </div></strong>
43
+ <% end %>
44
+
45
+ <%= page_entries_info @command_statuses %>
46
+ <%= will_paginate @command_statuses %>
@@ -0,0 +1,9 @@
1
+ <%= render 'hosts/selected_hosts', :hosts => @hosts %>
2
+
3
+ <%= form_for :command, :url => submit_command_commands_path() do |f| %>
4
+ <%= f.hidden_field :filters, :value => @filters %>
5
+ <%= f.hidden_field :command, :value => "install_package" %>
6
+ <%= f.text_field :package_name %>
7
+ <span class="label label-warning"><%= _('Warning') %></span>
8
+ <%= _('This might take a while, as install runs on all servers') %>
9
+ <% end %>
@@ -0,0 +1,6 @@
1
+ <%= form_for :command, :url => submit_command_commands_path() do |f| %>
2
+ <%= f.hidden_field :filters, :value => @filters %>
3
+ <%= f.hidden_field :command, :value => "ping" %>
4
+ <span class="label label-warning"><%= _('Warning') %></span>
5
+ <%= _('This might take a while, as command runs on all servers') %>
6
+ <% end %>
@@ -0,0 +1,8 @@
1
+ <%= render 'hosts/selected_hosts', :hosts => @hosts %>
2
+
3
+ <%= form_for :command, :url => submit_command_commands_path() do |f| %>
4
+ <%= f.hidden_field :filters, :value => @filters %>
5
+ <%= f.hidden_field :command, :value => "puppet_disable" %>
6
+ <span class="label label-warning"><%= _('Warning') %></span>
7
+ <%= _('This might take a while, as install runs on all servers') %>
8
+ <% end %>
@@ -0,0 +1,8 @@
1
+ <%= render 'hosts/selected_hosts', :hosts => @hosts %>
2
+
3
+ <%= form_for :command, :url => submit_command_commands_path() do |f| %>
4
+ <%= f.hidden_field :filters, :value => @filters %>
5
+ <%= f.hidden_field :command, :value => "puppet_enable" %>
6
+ <span class="label label-warning"><%= _('Warning') %></span>
7
+ <%= _('This might take a while, as install runs on all servers') %>
8
+ <% end %>
@@ -0,0 +1,8 @@
1
+ <%= render 'hosts/selected_hosts', :hosts => @hosts %>
2
+
3
+ <%= form_for :command, :url => submit_command_commands_path() do |f| %>
4
+ <%= f.hidden_field :filters, :value => @filters %>
5
+ <%= f.hidden_field :command, :value => "puppet_run_once" %>
6
+ <span class="label label-warning"><%= _('Warning') %></span>
7
+ <%= _('This might take a while, as install runs on all servers') %>
8
+ <% end %>
@@ -0,0 +1,9 @@
1
+ <%= render 'hosts/selected_hosts', :hosts => @hosts %>
2
+
3
+ <%= form_for :command, :url => submit_command_commands_path() do |f| %>
4
+ <%= f.hidden_field :filters, :value => @filters %>
5
+ <%= f.hidden_field :command, :value => "service_status" %>
6
+ <%= f.text_field :service_name %>
7
+ <span class="label label-warning"><%= _('Warning') %></span>
8
+ <%= _('This might take a while, as command runs on all servers') %>
9
+ <% end %>
@@ -0,0 +1,9 @@
1
+ <%= render 'hosts/selected_hosts', :hosts => @hosts %>
2
+
3
+ <%= form_for :command, :url => submit_command_commands_path() do |f| %>
4
+ <%= f.hidden_field :filters, :value => @filters %>
5
+ <%= f.hidden_field :command, :value => "start_service" %>
6
+ <%= f.text_field :service_name %>
7
+ <span class="label label-warning"><%= _('Warning') %></span>
8
+ <%= _('This might take a while, as command runs on all servers') %>
9
+ <% end %>
@@ -0,0 +1,9 @@
1
+ <%= render 'hosts/selected_hosts', :hosts => @hosts %>
2
+
3
+ <%= form_for :command, :url => submit_command_commands_path() do |f| %>
4
+ <%= f.hidden_field :filters, :value => @filters %>
5
+ <%= f.hidden_field :command, :value => "stop_service" %>
6
+ <%= f.text_field :service_name %>
7
+ <span class="label label-warning"><%= _('Warning') %></span>
8
+ <%= _('This might take a while, as command runs on all servers') %>
9
+ <% end %>
@@ -0,0 +1,9 @@
1
+ <%= render 'hosts/selected_hosts', :hosts => @hosts %>
2
+
3
+ <%= form_for :command, :url => submit_command_commands_path() do |f| %>
4
+ <%= f.hidden_field :filters, :value => @filters %>
5
+ <%= f.hidden_field :command, :value => "uninstall_package" %>
6
+ <%= f.text_field :package_name %>
7
+ <span class="label label-warning"><%= _('Warning') %></span>
8
+ <%= _('This might take a while, as install runs on all servers') %>
9
+ <% end %>
@@ -0,0 +1,26 @@
1
+ <% title _("Results") %>
2
+
3
+ <% title_actions %>
4
+
5
+ <table class="table table-bordered table-striped">
6
+ <tr>
7
+ <th><%= sort :host, :as => s_("Host") %></th>
8
+ <th><%= sort :status_code, :as => s_("Status Code") %></th>
9
+ <th><%= sort :status_message, :as => _("Status Message") %></th>
10
+ <th><%= sort :created_at, :as => _("Timestamp") %></th>
11
+ <th><%= _('Result') %></th>
12
+ </tr>
13
+ <% for detail in @details %>
14
+ <tr>
15
+ <td><%= link_to_if(@hosts.has_key?(detail.host), detail.host, @hosts.has_key?(detail.host) ? hash_for_host_path(:id => @hosts[detail.host].id) : {})
16
+ %></td>
17
+ <td><%= detail.status_code %></td>
18
+ <td><%= detail.status_message %></td>
19
+ <td><%= time_ago_in_words(detail.created_at.getlocal) %></td>
20
+ <td><%= detail.result %></td>
21
+ </tr>
22
+ <% end %>
23
+ </table>
24
+
25
+ <%= page_entries_info @details %>
26
+ <%= will_paginate @details %>
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>ForemanMco</title>
5
+ <%= stylesheet_link_tag "foreman-mco/application", :media => "all" %>
6
+ <%= javascript_include_tag "foreman-mco/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,24 @@
1
+ Rails.application.routes.draw do
2
+ scope :module => :foreman_mco do
3
+ resource :commands, :only => [] do
4
+ get :install_package, :on => :collection
5
+ get :uninstall_package, :on => :collection
6
+ get :service_status, :on => :collection
7
+ get :start_service, :on => :collection
8
+ get :stop_service, :on => :collection
9
+ get :puppet_runonce, :on => :collection
10
+ get :puppet_enable, :on => :collection
11
+ get :puppet_disable, :on => :collection
12
+ get :ping, :on => :collection
13
+ post :submit_command, :on => :collection
14
+ end
15
+
16
+ resources :command_histories, :only => [:index] do
17
+ resources :detailed_command_histories, :only => :index do
18
+ get :auto_complete_search, :on => :collection
19
+ end
20
+ get :auto_complete_search, :on => :collection
21
+ end
22
+ resources :command_statuses, :only => [:update]
23
+ end
24
+ end
@@ -0,0 +1,15 @@
1
+ class CreateCommandStatuses < ActiveRecord::Migration
2
+ def up
3
+ create_table :foreman_mco_command_statuses do |t|
4
+ t.column :jid, :string, :null => false
5
+ t.column :command, :text, :null => false
6
+ t.column :status, :string, :null => false
7
+ t.column :result, :text
8
+ t.timestamps
9
+ end
10
+ end
11
+
12
+ def down
13
+ drop_table :foreman_mco_command_statuses
14
+ end
15
+ end
@@ -0,0 +1,8 @@
1
+ class AddMcollectiveProxyFeature < ActiveRecord::Migration
2
+ def up
3
+ Feature.create(:name => "MCollective")
4
+ end
5
+
6
+ def down
7
+ end
8
+ end
@@ -0,0 +1,17 @@
1
+ class CreateHostCommandStatuses < ActiveRecord::Migration
2
+ def up
3
+ create_table :foreman_mco_host_command_statuses do |t|
4
+ t.references :command_status, :null => false
5
+ t.column :host, :string, :null => false
6
+ t.column :agent, :string, :null => false
7
+ t.column :action, :string, :null => false
8
+ t.column :status_code, :string, :null => false
9
+ t.column :status_message, :text, :null => false
10
+ t.column :result, :text
11
+ t.timestamps
12
+ end
13
+ end
14
+
15
+ def down
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ class AddDiscoveredAgents < ActiveRecord::Migration
2
+ def up
3
+ create_table :foreman_mco_discovered_agents do |t|
4
+ t.column :id, :integer, :null => false
5
+ t.column :name, :string, :null => false
6
+ t.column :type, :string, :null => false
7
+ end
8
+
9
+ create_table :foreman_mco_discovered_agent_fields do |t|
10
+ t.column :id, :integer, :null => false
11
+ t.column :name, :string, :null => false
12
+ t.column :discovered_agents_id, :integer, :null => false
13
+ end
14
+ end
15
+
16
+ def down
17
+ drop_table :foreman_mco_discovered_agents
18
+ drop_table :foreman_mco_discovered_agent_fields
19
+ end
20
+ end