initial commit for docker manager

This commit is contained in:
Sam 2013-11-13 17:43:35 +11:00
commit 2bb95a06a9
8 changed files with 147 additions and 0 deletions

View File

View File

@ -0,0 +1,10 @@
module DockerManager
class AdminController < DockerManager::ApplicationController
layout nil
def index
require_dependency 'docker_manager/git_repo'
@main_repo = DockerManager::GitRepo.new(Rails.root)
end
end
end

View File

@ -0,0 +1,15 @@
module DockerManager
class ApplicationController < ActionController::Base
include CurrentUser
before_filter :ensure_admin
protected
def ensure_admin
raise Discourse::InvalidAccess.new unless current_user.admin?
end
end
end

View File

@ -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: <a href="<%= repo.url %>"><%= repo.latest_origin_commit %></a> (<%= time_ago_in_words repo.latest_origin_commit_date %> ago)
<% if repo.commits_behind > 0 %>
commits behind: <%= repo.commits_behind %>
<button action="">upgrade</button>
<% end %>
<% else %>
Not under source control.
<% end %>

View File

@ -0,0 +1,22 @@
<html>
<body>
<h2>Discourse</h2>
<p>
<%= render partial: 'git_status', locals: {repo: @main_repo} %>
</p>
<h2>Plugins</h2>
<ul>
<% Discourse.plugins.each do |plugin| %>
<li>
<%= plugin.name %> -
<%= render partial: 'git_status', locals: {repo: DockerManager::GitRepo.new(File.dirname(plugin.path))} %>
</li>
<% end %>
</ul>
<h2>Processes</h2>
<h2>Log</h2>
</body>
</html>

3
config/routes.rb Normal file
View File

@ -0,0 +1,3 @@
DockerManager::Engine.routes.draw do
get "admin/docker" => "admin#index"
end

View File

@ -0,0 +1,67 @@
# like Grit just very very minimal
class DockerManager::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

20
plugin.rb Normal file
View File

@ -0,0 +1,20 @@
# name: docker_manager
# about: Docker manager for Discourse image
# version: 0.1
# authors: Sam Saffron
module ::DockerManager
class Engine < ::Rails::Engine
engine_name "docker_manager"
isolate_namespace DockerManager
end
end
Rails.configuration.assets.precompile += ['docker_manager.js']
after_initialize do
Discourse::Application.routes.append do
mount ::DockerManager::Engine, at: "/"
end
end