Skip to content

Commit

Permalink
added update-server.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
frsyuki committed Aug 25, 2014
1 parent a0bfad0 commit b9aeefa
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 49 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ gem 'httpclient', '~> 2.4.0'
gem 'nokogiri', '~> 1.6.3.1'
gem 'git', '~> 1.2.8'
gem 'parallel', '~> 1.2.4'
gem 'puma', '~> 2.9.0'
gem 'sinatra', '~> 1.4.5'
gem 'sinatra-contrib', '~> 1.4.2'
24 changes: 24 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,41 @@ GEM
remote: https://rubygems.org/
specs:
addressable (2.3.6)
backports (3.6.0)
faraday (0.9.0)
multipart-post (>= 1.2, < 3)
git (1.2.8)
httpclient (2.4.0)
mini_portile (0.6.0)
multi_json (1.10.1)
multipart-post (2.0.0)
nokogiri (1.6.3.1)
mini_portile (= 0.6.0)
octokit (3.3.1)
sawyer (~> 0.5.3)
parallel (1.2.4)
puma (2.9.0)
rack (>= 1.1, < 2.0)
rack (1.5.2)
rack-protection (1.5.3)
rack
rack-test (0.6.2)
rack (>= 1.0)
sawyer (0.5.5)
addressable (~> 2.3.5)
faraday (~> 0.8, < 0.10)
sinatra (1.4.5)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
sinatra-contrib (1.4.2)
backports (>= 2.0)
multi_json
rack-protection
rack-test
sinatra (~> 1.4.0)
tilt (~> 1.3)
tilt (1.4.1)

PLATFORMS
ruby
Expand All @@ -27,3 +48,6 @@ DEPENDENCIES
nokogiri (~> 1.6.3.1)
octokit (~> 3.3.1)
parallel (~> 1.2.4)
puma (~> 2.9.0)
sinatra (~> 1.4.5)
sinatra-contrib (~> 1.4.2)
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: bundle exec ruby update-server.rb
105 changes: 56 additions & 49 deletions update-index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,64 +124,71 @@ def octokit_search_repos(keyword, &block)
end
end

github_token = ENV['GITHUB_TOKEN']
website_repo = "git@github.com:msgpack/website.git"
repo_dir = File.expand_path("tmp/website")

log = Logger.new(STDOUT)
Faraday.default_adapter = :httpclient

retry_count = 0
begin
if Dir.exists?(repo_dir)
log.info "Using cached local git repository..."
git = Git.open(repo_dir)
else
log.info "Cloning remote git repository..."
FileUtils.mkdir_p(repo_dir)
git = Git.clone(website_repo, File.basename(repo_dir),
path: File.dirname(repo_dir))
end
def update_index(log)
github_token = ENV['GITHUB_TOKEN']
website_repo = "git@github.com:msgpack/website.git"
repo_dir = File.expand_path("tmp/website")

Faraday.default_adapter = :httpclient

log.info "Merging the latest files..."
log.info git.branch("gh-pages").checkout
log.info git.remote("origin").fetch
log.info git.remote("origin").merge("gh-pages")
prev_commit = git.object('HEAD').sha
retry_count = 0
begin
File.open('index.html.erb') do |f|
f.flock(File::LOCK_EX)
end

log.info git.remote("origin").merge("master")
if Dir.exists?(repo_dir)
log.info "Using cached local git repository..."
git = Git.open(repo_dir)
else
log.info "Cloning remote git repository..."
FileUtils.mkdir_p(repo_dir)
git = Git.clone(website_repo, File.basename(repo_dir),
path: File.dirname(repo_dir))
end

up = IndexHtmlRenderer.new(log, github_token)
html = up.render(File.join(".", "index.html.erb"))
orig = File.read(File.join(repo_dir, "index.html")) rescue ""
log.info "Merging the latest files..."
log.info git.branch("gh-pages").checkout
log.info git.remote("origin").fetch
log.info git.remote("origin").merge("gh-pages")
prev_commit = git.object('HEAD').sha

if html != orig
File.write(File.join(repo_dir, "index.html"), html)
log.info git.remote("origin").merge("master")

git.config("user.name", "msgpck.org updator on heroku")
git.config("user.email", "frsyuki@users.sourceforge.jp")
up = IndexHtmlRenderer.new(log, github_token)
html = up.render(File.join(".", "index.html.erb"))
orig = File.read(File.join(repo_dir, "index.html")) rescue ""

git.add(File.join(repo_dir, "index.html"))
git.commit("updated index.html")
end
if html != orig
File.write(File.join(repo_dir, "index.html"), html)

next_commit = git.object('HEAD').sha
if prev_commit == next_commit
log.info "Not changed."
else
log.info "Pushing changes to remote repository..."
log.info git.push("origin", "gh-pages")
end
git.config("user.name", "msgpck.org updator on heroku")
git.config("user.email", "frsyuki@users.sourceforge.jp")

log.info "Done."
git.add(File.join(repo_dir, "index.html"))
git.commit("updated index.html")
end

next_commit = git.object('HEAD').sha
if prev_commit == next_commit
log.info "Not changed."
else
log.info "Pushing changes to remote repository..."
log.info git.push("origin", "gh-pages")
end

rescue
raise if retry_count >= 1
log.info "Done."

# delete repo_dir and retry
FileUtils.rm_rf repo_dir
FileUtils.mkdir_p File.dirname(repo_dir)
retry_count += 1
retry
rescue
raise if retry_count >= 1

# delete repo_dir and retry
FileUtils.rm_rf repo_dir
FileUtils.mkdir_p File.dirname(repo_dir)
retry_count += 1
retry
end
end

update_index(Logger.new(STDOUT)) if __FILE__ == $0

13 changes: 13 additions & 0 deletions update-server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'sinatra'
require 'sinatra/streaming'
require_relative 'update-index'

set :server, 'puma'
set :port, (ENV['PORT'] || 8580).to_i

get '/update' do
headers 'Content-Type' => 'text/plain'
stream do |out|
update_index(Logger.new(out))
end
end

0 comments on commit b9aeefa

Please sign in to comment.