module update stuff added to kernel

This commit is contained in:
Harry Bomrah 2015-04-21 17:19:00 +08:00
parent 25c612ac9e
commit b1be711fd5
7 changed files with 194 additions and 38 deletions

View File

@ -1,9 +1,8 @@
class Admin::ModuleStoreController < OrbitAdminController
layout "structure"
include Admin::ModuleStoreHelper
def index
@extensions = []
@downloaded_extensions = []
# if current_site.site_token?
# if current_site.store_confirmation
# @extensions = get_extensions
@ -24,6 +23,11 @@ class Admin::ModuleStoreController < OrbitAdminController
@extension = get_extension(params[:id]) rescue nil
end
def modules_to_update
@built_in_extensions = get_built_in_extensions
render :partial => "modules_to_update"
end
def download
#get extension related values
extension = get_extension(params[:id]) rescue nil
@ -95,11 +99,28 @@ class Admin::ModuleStoreController < OrbitAdminController
end
def restart
Bundler.with_clean_env { `cd #{Rails.root} && bundle install` }
Bundler.with_clean_env { `cd #{Rails.root} && BUNDLE_GEMFILE=built_in_extensions.rb bundle update && bundle` }
Bundler.with_clean_env { `cd #{Rails.root} && BUNDLE_GEMFILE=downloaded_extensions.rb bundle update && bundle` }
@built_in_extensions = get_built_in_extensions
render :partial => "modules_to_update"
%x(kill -s USR2 `cat tmp/pids/unicorn.pid`)
sleep 5
end
def update_module
key = params["key"]
type = params["type"]
if type == "built_in"
Bundler.with_clean_env { `cd #{Rails.root} && BUNDLE_GEMFILE=built_in_extensions.rb bundle update #{key}` }
elsif type == "downloaded"
Bundler.with_clean_env { `cd #{Rails.root} && BUNDLE_GEMFILE=downloaded_extensions.rb bundle update #{key}` }
end
@built_in_extensions = get_built_in_extensions
render :partial => "modules_to_update"
%x(kill -s USR2 `cat tmp/pids/unicorn.pid`)
sleep 5
end
protected
def get_extensions
@ -133,21 +154,7 @@ class Admin::ModuleStoreController < OrbitAdminController
store_session.get_extension(id)
end
def get_downloaded_extension
downloaded_extensions = []
extensions = File.new("#{Rails.root}/downloaded_extensions.rb", "r")
while (extension = extensions.gets)
status = !extension.start_with?("# ")
extension = extension.split(',')
downloaded_extensions << {'name' => extension[0].split(/[\'\"]/)[1], 'repo' => extension[1].split(/[\'\"]/)[1], 'tag' => extension[2].split(/[\'\"]/)[1], 'status' => status}
end
extensions.close
downloaded_extensions.to_json
end
def toggle_item(module_key, active)
if !module_key.nil?

View File

@ -0,0 +1,66 @@
module Admin::ModuleStoreHelper
def get_built_in_extensions
built_in_extensions = []
extensions_to_update = get_extensions_to_update
@extensions_to_update_count = extensions_to_update.count
built_in_extensions = get_extension_from_file("built_in",extensions_to_update)
built_in_extensions.concat(get_extension_from_file("downloaded",extensions_to_update))
return built_in_extensions
end
def get_extension_from_file(type,extensions_to_update)
temp = []
extensions = File.new("#{Rails.root}/#{type}_extensions.rb", "r")
while (extension = extensions.gets)
is_extension = extension.start_with?("gem")
extension = extension.split(',')
if is_extension
key = extension[0].split(/[\'\"]/)[1]
ma = ModuleApp.find_by_key(key) rescue nil
if !ma.nil?
update_available = extensions_to_update.include?(key) ? true : false
temp << {'key' => key, 'name' => ma.title, 'repo' => extension[1].split(/[\'\"]/)[1], 'tag' => (extension[2].split(/[\'\"]/)[1] rescue ""), "update_available" => update_available, "type" => type}
end
end
end
return temp
end
def get_extensions_to_update
if !File.exists?("#{Rails.root}/built_in_extensions.rb.lock") || !File.exists?("#{Rails.root}/downloaded_extensions.rb.lock")
update_extension_file("built_in")
update_extension_file("downloaded")
return []
else
update_extensions = get_update_info_for_extensions("built_in")
update_extensions.concat(get_update_info_for_extensions("downloaded"))
return update_extensions
end
end
def update_extension_file(type)
Bundler.with_clean_env { `cd #{Rails.root} && BUNDLE_GEMFILE=#{type}_extensions.rb bundle update && bundle` }
end
def get_update_info_for_extensions(type)
buffer = Bundler.with_clean_env { `cd #{Rails.root} && BUNDLE_GEMFILE=#{type}_extensions.rb bundle outdated` }
buffer = buffer.split("\n")
if buffer.last == "Bundle up to date!"
return []
elsif buffer.first.start_with?("The git source")
update_extension_file(type)
return []
else
output = []
buffer.each do |b|
if b.start_with?(" * ")
temp = b.split(" ")
output << temp[1] if !temp[1].nil?
end
end
return output
end
end
end

View File

@ -0,0 +1,12 @@
<tr>
<td class="detail-row">
<span class="mt_title"><%= installed_module["name"] %></span>
</td>
<td class="active">
<% if installed_module["update_available"] %>
<a href="" class="update_module btn btn-primary" data-module-key="<%= installed_module["key"] %>" data-module-type="<%= installed_module["type"] %>">Update</button>
<% else %>
Up to Date
<% end %>
</td>
</tr>

View File

@ -0,0 +1,47 @@
<table class="table table-striped">
<thead>
<tr class="sort-header">
<th class="">Title</th>
<th class="">Update <%= (@extensions_to_update_count == 1 ? "(#{@extensions_to_update_count} update available)" : (@extensions_to_update_count > 1 ? "(#{@extensions_to_update_count} updates available)" : "")) %> <a href="" id="update_all_modules" class="btn btn-primary pull-right <%= @extensions_to_update_count > 1 ? "" : "disabled" %>">Update All</a></th>
</tr>
</thead>
<tbody class="sort-holder" id="tbody_designs">
<%= render :partial => "installed_module", :collection => @built_in_extensions %>
</tbody>
</table>
<script type="text/javascript">
$("a.update_module").on("click",function(){
if(!$(this).hasClass("disabled")){
$(this).text("Updating").addClass("disabled");
var key = $(this).data("module-key"),
type = $(this).data("module-type")
$.ajax({
url : "/admin/module_store/update_module",
data : {"key" : key, "type" : type},
dataType : "json",
type : "post"
}).always(function(obj){
if(obj.status == 200){
update_module_table.html(obj.responseText);
}
})
}
return false;
})
$("a#update_all_modules").on("click",function(){
if(!$(this).hasClass("disabled")){
$(this).text("Updating").addClass("disabled");
$.ajax({
url : "/admin/module_store/restart",
type : "post"
}).always(function(obj){
if(obj.status == 200){
update_module_table.html(obj.responseText);
}
})
}
return false;
})
</script>

View File

@ -9,6 +9,15 @@
<%= javascript_include_tag 'lib/footable-0.1' %>
<%= javascript_include_tag 'lib/all-list' %>
<%= javascript_include_tag 'lib/retina' %>
<style type="text/css">
.loading-image-wrapper {
text-align: center;
}
.loading-image-wrapper > span {
display: block;
padding: 20px 0;
}
</style>
</head>
<%= flash[:notice] rescue nil%>
@ -39,7 +48,7 @@
<ul>
<li class="active">
<a href="#installed_modules" data-toggle="tab">
<i class="icons-download"></i><%= t(:installed_modules) %>
<i class="icon-cloud-download"></i> <%= t(:installed_modules) %>
</a>
</li>
<li>
@ -47,6 +56,11 @@
<i class="icon-shopping-cart"></i><%= t(:module_store) %>
</a>
</li>
<li>
<a href="#update_module" data-toggle="tab">
<i class="icon-download-alt"></i> <%= t(:update_modules) %>
</a>
</li>
</ul>
</div>
<div class="pannel-body tab-content">
@ -57,6 +71,12 @@
<div class="tab-pane fade" id="module_store">
<%= render :partial => "admin/store/store", :locals => {:section => "apps"} %>
</div>
<div class="tab-pane fade" id="update_module">
<div class="loading-image-wrapper">
<img src="/assets/preloader.gif" />
<span>Fetching module update info.</span>
</div>
</div>
</div>
</div>
<div class="modal-backdrop"></div>
@ -129,11 +149,6 @@
$("#module_msg_title").html("<%= t(:change_applied) %>");
$("#module_msg_content").html("<%= t("update_manager_.restart_server") %>");
$.get("<%= admin_module_store_restart_server_path%>",function(){
$("#alert_wrap").delay(2000).fadeOut(300,function(){
$(".modal-backdrop").fadeOut();
});
});
});
});
}
@ -153,14 +168,26 @@
$("#module_msg_title").html("<%= t(:change_applied) %>");
$("#module_msg_content").html("<%= t("update_manager_.restart_server") %>");
$.get("<%= admin_module_store_restart_server_path%>",function(){
$("#alert_wrap").delay(2000).fadeOut(300,function(){
$(".modal-backdrop").fadeOut();
location.reload();
});
});
});
});
}
var update_module_table = $("#update_module");
$('a[data-toggle="tab"]').on('shown', function (e) {
var el = $(e.target),
prev_el = $(e.relatedTarget);
if(el.attr("href") == "#update_module"){
$.ajax({
url : "/admin/module_store/modules_to_update",
type : "get",
dataType : "html"
}).done(function(data){
update_module_table.html(data);
})
}else if(prev_el.attr("href") == "#update_module"){
update_module_table.html('<div class="loading-image-wrapper"><img src="/assets/preloader.gif" /><span>Fetching module update info.</span></div>');
}
})
</script>

View File

@ -126,8 +126,6 @@
<div style="height: 55px;">
<% if @store_permissions["permission_granted"] %>
<button id="update_btn" class="btn btn-primary pull-right" style="margin: 10px;"><i class="icon-inbox icon-white"></i> <%= t("update_manager_.system_update") %></button>
<button id="module_update_btn" class="btn btn-primary pull-right" style="margin-right: 10px;"><i class="icon-inbox icon-white"></i> <%= t("update_manager_.module_update") %></button>
<% else %>
<a href="/<%= I18n.locale.to_s %>/admin/designs" class="pull-right">Please register here.</a>
<% end %>
@ -173,9 +171,6 @@
</div>
<div style="padding: 10px 0; height: 65px;">
<button id="chech_update_btn" class="btn btn-small btn-inverse pull-right"><i class="icon-refresh icon-white"></i> <%= t("update_manager_.check_update") %></button>
<% if @store_permissions["permission_granted"] %>
<button id="module_update_btn" class="btn btn-primary btn-small pull-right" style="margin-right: 10px;"><i class="icon-inbox icon-white"></i> <%= t("update_manager_.module_update") %></button>
<% end %>
</div>
</div>
</div>
@ -269,7 +264,7 @@
check_updates();
get_update_history();
$("#module_update_btn").click(check_module_updates);
// $("#module_update_btn").click(check_module_updates);
$("#chech_update_btn").click(check_updates);

View File

@ -240,10 +240,12 @@ Orbit::Application.routes.draw do
get 'design_list' => 'designs#design_list'
get 'module_store' => 'module_store#index'
get 'module_store/show' => 'module_store#show'
post 'module_store/update_module' => 'module_store#update_module'
get 'module_store/download' => 'module_store#download'
get 'module_store/remove_module' => 'module_store#remove_module'
get 'module_store/toggle_module' => 'module_store#toggle_module'
get 'module_store/restart_server' => 'module_store#restart_server'
post 'module_store/restart' => 'module_store#restart'
get 'module_store/modules_to_update' => 'module_store#modules_to_update'
#store routes