From 932c2675a7524fd0542c06edb09599232479915f Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 12 Nov 2013 16:42:35 +1100 Subject: [PATCH] work in progress, admin page for upgrades (provides source lives in git) --- app/controllers/admin/upgrade_controller.rb | 20 ++++++ app/views/admin/upgrade/.index.html.erb.swn | Bin 0 -> 12288 bytes app/views/admin/upgrade/_git_status.html.erb | 10 +++ app/views/admin/upgrade/index.html.erb | 18 +++++ config/routes.rb | 6 +- lib/upgrade/git_repo.rb | 69 +++++++++++++++++++ 6 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 app/controllers/admin/upgrade_controller.rb create mode 100644 app/views/admin/upgrade/.index.html.erb.swn create mode 100644 app/views/admin/upgrade/_git_status.html.erb create mode 100644 app/views/admin/upgrade/index.html.erb create mode 100644 lib/upgrade/git_repo.rb diff --git a/app/controllers/admin/upgrade_controller.rb b/app/controllers/admin/upgrade_controller.rb new file mode 100644 index 00000000000..096265108e7 --- /dev/null +++ b/app/controllers/admin/upgrade_controller.rb @@ -0,0 +1,20 @@ +# EXPERIMENTAL: front end for upgrading your instance using the web UI + +class Admin::UpgradeController < Admin::AdminController + + before_filter :ensure_admin + skip_before_filter :check_xhr + + layout 'no_js' + + def index + require_dependency 'upgrade/git_repo' + @main_repo = Upgrade::GitRepo.new(Rails.root) + end + + protected + + def ensure_admin + raise Discourse::InvalidAccess.new unless current_user.admin? + end +end diff --git a/app/views/admin/upgrade/.index.html.erb.swn b/app/views/admin/upgrade/.index.html.erb.swn new file mode 100644 index 0000000000000000000000000000000000000000..c3303d86e70ce3624ffcd7dfd4209456fea126f2 GIT binary patch literal 12288 zcmeI&&uSDg90%}UJ=Z_f7l=yfw%AEq6=fKg9;6~DNTq_OlAVdWf%y}Xj1{aEAHYk& z7qDKucvKL40FUBRc<>$kWoK7GD2o@N@*Vh+Op?vy_qi=IgSCfu?uqNkRpRR$QSZ~s z?f2JK=<7U@S5+LHd9{PK4(-fkbifJ61pd0>=up;qmH?({yEHeJ!rJbn${Xzp!)ca!fz~0uX=z1Rwwb z2tWV=|DQmxbM%7CIyEV6Vbae1(N1pBKmY;|fB*y_009U<00Izz00ba#0tHk-w0?%@ zH7CCRpZ@>7*(cXX=uW(4=S{N>9Q!BT$Mf*&z!bRDt5Z>&eQa^3EMo9)OxSC zWlEhC#?`9QJzKUzQzur1(em>1=|j6mP3Gb{o)D|udAj59A#Y7TcTc*7KG7;4iJ}qv p-N1g_E{jEt%N;-Icd?d-|F)jZN||~$);2zB7WZJuUnl7#e*&+*qZa@G literal 0 HcmV?d00001 diff --git a/app/views/admin/upgrade/_git_status.html.erb b/app/views/admin/upgrade/_git_status.html.erb new file mode 100644 index 00000000000..eb615ddb1ee --- /dev/null +++ b/app/views/admin/upgrade/_git_status.html.erb @@ -0,0 +1,10 @@ +<% if repo.valid? %> + Current version: <%= repo.latest_local_commit %> (<%= time_ago_in_words repo.latest_local_commit_date %> ago), + Remote version: <%= repo.latest_origin_commit %> (<%= time_ago_in_words repo.latest_origin_commit_date %> ago) + <% if repo.commits_behind > 0 %> + commits behind: <%= repo.commits_behind %> + + <% end %> +<% else %> + Not under source control. +<% end %> diff --git a/app/views/admin/upgrade/index.html.erb b/app/views/admin/upgrade/index.html.erb new file mode 100644 index 00000000000..5c232590228 --- /dev/null +++ b/app/views/admin/upgrade/index.html.erb @@ -0,0 +1,18 @@ +

Discourse

+

+<%= render partial: 'git_status', locals: {repo: @main_repo} %> +

+ +

Plugins

+
    + <% Discourse.plugins.each do |plugin| %> +
  • + <%= plugin.name %> - + <%= render partial: 'git_status', locals: {repo: Upgrade::GitRepo.new(File.dirname(plugin.path))} %> +
  • + <% end %> +
+ +

Processes

+ +

Log

diff --git a/config/routes.rb b/config/routes.rb index 3b578152cc9..48db18e57c9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -21,6 +21,7 @@ Discourse::Application.routes.draw do namespace :admin, constraints: StaffConstraint.new do get '' => 'admin#index' + resources :site_settings, constraints: AdminConstraint.new get 'reports/:type' => 'reports#show' @@ -96,7 +97,10 @@ Discourse::Application.routes.draw do delete 'key' => 'api#revoke_key' end end - end + + get 'upgrade' => 'upgrade#index' + + end # admin namespace get 'email_preferences' => 'email#preferences_redirect', :as => 'email_preferences_redirect' get 'email/unsubscribe/:key' => 'email#unsubscribe', as: 'email_unsubscribe' diff --git a/lib/upgrade/git_repo.rb b/lib/upgrade/git_repo.rb new file mode 100644 index 00000000000..c4499185f23 --- /dev/null +++ b/lib/upgrade/git_repo.rb @@ -0,0 +1,69 @@ +module Upgrade; end + +# like Grit just very very minimal +class Upgrade::GitRepo + attr_reader :path + + def initialize(path) + @path = path + @memoize = {} + end + + def valid? + File.directory?("#{path}/.git") + end + + def latest_local_commit + run "rev-parse --short HEAD" + end + + def latest_origin_commit + run "rev-parse --short #{tracking_branch}" + end + + def latest_origin_commit_date + commit_date(latest_origin_commit) + end + + def latest_local_commit_date + commit_date(latest_local_commit) + end + + def commits_behind + run("rev-list --count #{tracking_branch}..HEAD").to_i + end + + def url + url = run "config --get remote.origin.url" + if url =~ /^git/ + # hack so it works with git urls + url = "https://github.com/#{url.split(":")[1]}" + end + end + + protected + + def commit_date(commit) + unix_timestamp = run('show -s --format="%ct" ' << commit).to_i + Time.at(unix_timestamp).to_datetime + end + + def tracking_branch + run "for-each-ref --format='%(upstream:short)' $(git symbolic-ref -q HEAD)" + end + + def ensure_updated + @updated ||= Thread.new do + # this is a very slow operation, make it async + `cd #{path} && git remote update` + end + end + + def run(cmd) + ensure_updated + @memoize[cmd] ||= `cd #{path} && git #{cmd}`.strip + rescue => e + p e + end + +end