From b1be711fd53c99eed40be465293b1189cb2a89c8 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Tue, 21 Apr 2015 17:19:00 +0800 Subject: [PATCH] module update stuff added to kernel --- .../admin/module_store_controller.rb | 43 +++++++----- app/helpers/admin/module_store_helper.rb | 66 +++++++++++++++++++ .../module_store/_installed_module.html.erb | 12 ++++ .../module_store/_modules_to_update.html.erb | 47 +++++++++++++ app/views/admin/module_store/index.html.erb | 53 +++++++++++---- app/views/admin/sites/update_manager.erb | 7 +- config/routes.rb | 4 +- 7 files changed, 194 insertions(+), 38 deletions(-) create mode 100644 app/helpers/admin/module_store_helper.rb create mode 100644 app/views/admin/module_store/_installed_module.html.erb create mode 100644 app/views/admin/module_store/_modules_to_update.html.erb diff --git a/app/controllers/admin/module_store_controller.rb b/app/controllers/admin/module_store_controller.rb index 7385bbd..fbf4ba3 100644 --- a/app/controllers/admin/module_store_controller.rb +++ b/app/controllers/admin/module_store_controller.rb @@ -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? diff --git a/app/helpers/admin/module_store_helper.rb b/app/helpers/admin/module_store_helper.rb new file mode 100644 index 0000000..6f573da --- /dev/null +++ b/app/helpers/admin/module_store_helper.rb @@ -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 \ No newline at end of file diff --git a/app/views/admin/module_store/_installed_module.html.erb b/app/views/admin/module_store/_installed_module.html.erb new file mode 100644 index 0000000..504bc73 --- /dev/null +++ b/app/views/admin/module_store/_installed_module.html.erb @@ -0,0 +1,12 @@ + + + <%= installed_module["name"] %> + + + <% if installed_module["update_available"] %> + " data-module-type="<%= installed_module["type"] %>">Update + <% else %> + Up to Date + <% end %> + + \ No newline at end of file diff --git a/app/views/admin/module_store/_modules_to_update.html.erb b/app/views/admin/module_store/_modules_to_update.html.erb new file mode 100644 index 0000000..209f41f --- /dev/null +++ b/app/views/admin/module_store/_modules_to_update.html.erb @@ -0,0 +1,47 @@ + + + + + + + + + <%= render :partial => "installed_module", :collection => @built_in_extensions %> + +
TitleUpdate <%= (@extensions_to_update_count == 1 ? "(#{@extensions_to_update_count} update available)" : (@extensions_to_update_count > 1 ? "(#{@extensions_to_update_count} updates available)" : "")) %> ">Update All
+ + \ No newline at end of file diff --git a/app/views/admin/module_store/index.html.erb b/app/views/admin/module_store/index.html.erb index a053f70..d0d8b6d 100644 --- a/app/views/admin/module_store/index.html.erb +++ b/app/views/admin/module_store/index.html.erb @@ -9,6 +9,15 @@ <%= javascript_include_tag 'lib/footable-0.1' %> <%= javascript_include_tag 'lib/all-list' %> <%= javascript_include_tag 'lib/retina' %> + <%= flash[:notice] rescue nil%> @@ -39,7 +48,7 @@
@@ -57,6 +71,12 @@
<%= render :partial => "admin/store/store", :locals => {:section => "apps"} %>
+
+
+ + Fetching module update info. +
+
@@ -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('
Fetching module update info.
'); + } +}) + diff --git a/app/views/admin/sites/update_manager.erb b/app/views/admin/sites/update_manager.erb index a486f6d..678c441 100644 --- a/app/views/admin/sites/update_manager.erb +++ b/app/views/admin/sites/update_manager.erb @@ -126,8 +126,6 @@
<% if @store_permissions["permission_granted"] %> - - <% else %> Please register here. <% end %> @@ -173,9 +171,6 @@
- <% if @store_permissions["permission_granted"] %> - - <% end %>
@@ -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); diff --git a/config/routes.rb b/config/routes.rb index 789ef5c..121d006 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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