Skip to content

Commit

Permalink
progress for create host.
Browse files Browse the repository at this point in the history
  • Loading branch information
abenari committed May 15, 2012
1 parent 370a7ac commit 0a9e867
Show file tree
Hide file tree
Showing 17 changed files with 207 additions and 45 deletions.
2 changes: 2 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ def process_success hash = {}
hash[:success_redirect] ||= eval("#{controller_name}_url")
hash[:json_code] = :created if action_name == "create"

return render :json => {:redirect => hash[:success_redirect]} if hash[:redirect_xhr]

respond_to do |format|
format.html do
notice hash[:success_msg]
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/hosts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def create
@host.managed = true
forward_request_url
if @host.save
process_success :success_redirect => @host
process_success :success_redirect => host_path(@host), :redirect_xhr => request.xhr?
else
load_vars_for_ajax
offer_to_overwrite_conflicts
Expand All @@ -108,7 +108,7 @@ def edit
def update
forward_request_url
if @host.update_attributes(params[:host])
process_success :success_redirect => @host
process_success :success_redirect => host_path(@host), :redirect_xhr => request.xhr?
else
load_vars_for_ajax
offer_to_overwrite_conflicts
Expand Down
16 changes: 16 additions & 0 deletions app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class TasksController < ApplicationController
skip_before_filter :session_expiry, :update_activity_time, :only => [:show]

def show
id = params[:id]
queue = Rails.cache.fetch(id)
respond_to do |format|
format.html {
@tasks = queue.nil? ? [] : JSON(queue)
return render :partial => 'list' if ajax?
}
format.json { render :json => queue }
end
end

end
22 changes: 22 additions & 0 deletions app/helpers/tasks_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module TasksHelper

def task_status status
icon = case status
when "completed"
"check"
when "running"
"refresh"
when "failed"
"remove"
when "rollbacked"
"fast-backward"
when "pending"
"th"
when "canceled"
"ban-circle"
else
"th"
end
icon_text(icon, status)
end
end
10 changes: 9 additions & 1 deletion app/models/host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,15 @@ def overwrite=(value)

# if certname does not exist, use hostname instead
def certname
super || name
read_attribute(:certname) || name
end

def queuename
@queuename ||= Foreman.uuid
end

def queuename=(value)
@queuename = value
end

private
Expand Down
6 changes: 6 additions & 0 deletions app/models/orchestration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ def process q
break unless q.failed.empty?

task.status = "running"

Rails.cache.write(queuename, q.to_json, :expires_in => 5.minutes)
begin
task.status = execute({:action => task.action}) ? "completed" : "failed"

Expand All @@ -104,15 +106,19 @@ def process q
end
end

Rails.cache.write(queuename, q.to_json, :expires_in => 5.minutes)
# if we have no failures - we are done
return true if q.failed.empty? and q.pending.empty? and q.conflict.empty? and orchestration_errors?

logger.warn "Rolling back due to a problem: #{q.failed + q.conflict}"
q.pending.each{ |task| task.status = "canceled" }

# handle errors
# we try to undo all completed operations and trigger a DB rollback
(q.completed + q.running).sort.reverse_each do |task|
begin
task.status = "rollbacked"
Rails.cache.write(queuename, q.to_json, :expires_in => 5.minutes)
execute({:action => task.action, :rollback => true})
rescue => e
# if the operation failed, we can just report upon it
Expand Down
4 changes: 3 additions & 1 deletion app/views/hosts/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<%= javascript 'host_edit', 'compute_resource', 'lookup_keys'%>
<%= render "conflicts" if @host.errors[:conflict].any? %>
<%= form_for @host do |f| %>
<%= render "progress" %>
<%= form_for @host, :html => {:'data-submit' => 'progress_bar'} do |f| %>
<%= base_errors_for @host %>

<ul class="nav nav-tabs" data-tabs="tabs">
Expand Down Expand Up @@ -48,6 +49,7 @@
</div>

<%= f.hidden_field :managed %>
<%= f.hidden_field :queuename %>
<%= render('unattended', :f => f) if SETTINGS[:unattended] and @host.managed -%>

Expand Down
16 changes: 16 additions & 0 deletions app/views/hosts/_progress.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div id="host-progress-modal" class="modal fade hide" >
<div class="modal-header">
<a href="#" class="close" data-dismiss="modal">&times</a>
<h3><%= action_name %> In Progress</h3>
</div>
<div class="modal-body">
<p id="tasks_progress"><%= action_name %> in progress.</p>
<div class="progress progress-striped active" style="width: 400px;">
<div class="bar" style="width: 0%;"></div>
</div>
</div>

<div class="modal-footer">
<a href="#" class="btn" data-dismiss="modal">OK</a>
</div>
</div>
5 changes: 5 additions & 0 deletions app/views/tasks/_list.rhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div id="tasks_progress">
<% @tasks.each do |task| %>
<p><%= task_status(task['status']) %><%= " - #{task['name']}" %></p>
<% end%>
</div>
1 change: 1 addition & 0 deletions app/views/tasks/show.rhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render 'list' %>
3 changes: 2 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class Application < Rails::Application
config.session_store :active_record_store

# enables in memory cache store with ttl
config.cache_store = TimedCachedStore.new
#config.cache_store = TimedCachedStore.new
config.cache_store = :file_store, '/tmp'
end
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,6 @@
# match for all unattended scripts
match 'unattended/(:action/(:id(.format)))', :controller => 'unattended'

resources :tasks, :only => [:show]

end
6 changes: 5 additions & 1 deletion lib/orchestration/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Orchestration
class Queue

attr_reader :items
STATUS = %w[ pending running failed completed rollbacked conflict ]
STATUS = %w[ pending running failed completed rollbacked conflict canceled]

def initialize
@items = []
Expand Down Expand Up @@ -39,6 +39,10 @@ def clear
@items = [] && true
end

def to_json
all.to_json
end

STATUS.each do |s|
define_method s do
all.delete_if {|t| t.status != s}.sort
Expand Down
67 changes: 32 additions & 35 deletions public/javascripts/application.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
$(function() {
onContentLoad();
});

function onContentLoad(){
$('.flash.error').hide().each(function(index, item) {
if ($('.alert-message.alert-error.base').length == 0) {
if ($('#host-conflicts-modal').length == 0) {
$.jnotify($(item).text(), { type: "error", sticky: true });
}
}
});
if ($('.alert-message.alert-error.base').length == 0) {
if ($('#host-conflicts-modal').length == 0) {
$.jnotify($(item).text(), { type: "error", sticky: true });
}
}
});

$('.flash.warning').hide().each(function(index, item) {
$.jnotify($(item).text(), { type: "warning", sticky: true });
});
$('.flash.warning').hide().each(function(index, item) {
$.jnotify($(item).text(), { type: "warning", sticky: true });
});

$('.flash.notice').hide().each(function(index, item) {
$.jnotify($(item).text(), { type: "success", sticky: false });
});

// adds buttons classes to all links
$("#title_action a").addClass("btn");
$("#title_action a[href*='new']").addClass("btn-success");

// highlight tabs with errors
$(".tab-content").find(".control-group.error").each(function() {
var id = $(this).parentsUntil(".tab-content").last().attr("id");
$("a[href=#"+id+"]").addClass("tab-error");
})

//set the tooltips
$('a[rel="popover"]').popover();
$('[rel="twipsy"]').tooltip();
}

$('.flash.notice').hide().each(function(index, item) {
$.jnotify($(item).text(), { type: "success", sticky: false });
});
});

function remove_fields(link) {
$(link).prev("input[type=hidden]").val("1");
Expand Down Expand Up @@ -122,12 +141,6 @@ $(document).ready(function() {
});
});

// adds buttons classes to all links
$(function(){
$("#title_action a").addClass("btn");
$("#title_action a[href*='new']").addClass("btn-success");
});

$(function() {
if ($("#login-form").size() > 0) {
$("#login_login").focus();
Expand Down Expand Up @@ -211,22 +224,6 @@ $(function() {

});


// highlight tabs with errors
$(function(){
$(".tab-content").find(".control-group.error").each(function() {
// find each tab id
var id = $(this).parentsUntil(".tab-content").last().attr("id");
// now add a class to that tab
$("a[href=#"+id+"]").addClass("tab-error");
})
});

$(function () {
$('a[rel="popover"]').popover();
$('[rel="twipsy"]').tooltip();
});

function filter_by_level(item){
var level = $(item).val();

Expand Down
76 changes: 72 additions & 4 deletions public/javascripts/host_edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,71 @@ function computeResourceSelected(item){
url: url,
data:'compute_resource_id=' + compute,
success: function(result){
$('#compute_resource').html(result);
$('#compute_resource').html(result);
}
})
}
}

var stop_pooling;

function submit_host(form){
var url = form.attr("action");
stop_pooling = false;
$("body").css("cursor", "progress");
animate_progress();

$.ajax({
type:'POST',
url: url,
data: form.serialize(),
success: function(response){
if(response.redirect){
window.location.replace(response.redirect);
}
else{
$("#host-progress-modal").modal('hide');
$('#content').replaceWith($("#content", response));
onHostEditLoad();
onContentLoad();
}
},
complete: function(response){
stop_pooling = true;
$("body").css("cursor", "auto");
$("#host-progress-modal").modal('hide');
}
});
return false;
}

function animate_progress(){
if (stop_pooling == true) return;
setTimeout(function() {
var task_id = $('#host_queuename').val();
$.get('/tasks/' + task_id, function (response){
update_progress(response);
animate_progress();
})
}, 1600);
}

function update_progress(data){
var all = $('p',data).size();
if (all == 0 || stop_pooling == true) return;
var done = $('.icon-check',data).size();
$("#host-progress-modal").modal();
if($('.icon-remove',data).size() > 0) {
$('.progress').removeClass('progress-success');
$('.progress').addClass('progress-danger');
}else{
$('.progress').removeClass('progress-danger');
$('.progress').addClass('progress-success');
}
$('.bar').width(done/all *400);
$('#tasks_progress').replaceWith(data);
}

function add_puppet_class(item){
var id = $(item).attr('data-class-id');
var type = $(item).attr('data-type');
Expand Down Expand Up @@ -233,8 +292,17 @@ function use_image_selected(element){
}

$(function () {
onHostEditLoad();
});

function onHostEditLoad(){
$("#host-conflicts-modal").modal({show: "true", backdrop: "static"});
$('#host-conflicts-modal').click(function(){
$('#host-conflicts-modal').modal('hide');
$('#host-conflicts-modal').click(function(){
$('#host-conflicts-modal').modal('hide');
});
var $form = $("[data-submit='progress_bar']");
$form.on('submit', function(){
submit_host($form);
return false;
});
});
}
8 changes: 8 additions & 0 deletions test/functional/tasks_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'test_helper'

class TasksControllerTest < ActionController::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
4 changes: 4 additions & 0 deletions test/unit/helpers/tasks_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'test_helper'

class TasksHelperTest < ActionView::TestCase
end

0 comments on commit 0a9e867

Please sign in to comment.