Permalink
Browse files

Log with Scrolls.

  • Loading branch information...
1 parent f71f556 commit eaa9b3ec7cd8d93f78e1228ceebdd70e9038369c @technomancy committed Nov 26, 2012
Showing with 35 additions and 14 deletions.
  1. +2 −1 Gemfile
  2. +2 −0 Gemfile.lock
  3. +21 −10 lib/sokoban.rb
  4. +10 −3 lib/sokoban/receiver.rb
View
@@ -7,4 +7,5 @@ gem "heroku-api"
gem "redis"
gem "uuid"
gem "json"
-gem "excon"
+gem "excon"
+gem "scrolls"
View
@@ -14,6 +14,7 @@ GEM
rack (>= 1.0)
servolux (~> 0.8.1)
redis (3.0.2)
+ scrolls (0.2.1)
servolux (0.8.1)
systemu (2.5.2)
uuid (2.3.6)
@@ -30,4 +31,5 @@ DEPENDENCIES
rack
rack-streaming-proxy
redis
+ scrolls
uuid
View
@@ -2,20 +2,25 @@
require "redis"
require "rack/streaming_proxy"
require "uuid"
+require "scrolls"
module Sokoban
class Error < StandardError
attr_accessor :status
end
class Proxy
+ include Scrolls
+
def initialize
- @uuid = UUID.new
- @redis = Redis.new(:url => ENV["REDIS_URL"])
+ @uuid = UUID.new # factory factory factory
+ @redis = Redis.new(:url => ENV["REDIS_URL"] || "redis://localhost:6379")
+ Scrolls.global_context(app: "sokoban", deploy: ENV["DEPLOY"] || "local")
+ log(at: "ready")
end
def proxy(env, base_url)
- req = Rack::Request.new(env)
+ req = Rack::Request.new(env)
url = base_url + env["PATH_INFO"]
url += "?" + env["QUERY_STRING"] unless env["QUERY_STRING"].empty?
@@ -35,7 +40,7 @@ def call(env)
app_name = env["REQUEST_PATH"][/^(.+?)\.git/, 1]
receiver_url = ensure_receiver(app_name, api_key(env))
- puts "call app_name=#{app_name} receiver=#{receiver_url}"
+ log(fn: "call", app_name: app_name, receiver: receiver_url)
proxy(env, receiver_url)
rescue Error => e
[e.status, {"Content-Type" => "text/plain"}, [e.message]]
@@ -45,22 +50,28 @@ def ensure_receiver(app_name, api_key)
JSON.parse(@redis.hget(app_name) || launch(app_name, api_key))
end
- def receiver_config
+ def receiver_config(reply_key)
# TODO: get release_url, repo get/put urls from core
{ "REDIS_URL" => ENV["REDIS_URL"],
- "REPLY_KEY" => "launched.#{@uuid.generate}",
+ "REPLY_KEY" => reply_key,
"REPO_GET_URL" => "http://p.hagelb.org/sokoban.bundle",
}
end
def launch(app_name, api_key)
- heroku = Heroku::API.new(:api_key => api_key)
- heroku.post_ps(app_name, command, { :ps_env => receiver_config })
- @redis.blpop.tap {|receiver| @redis.hset(app_name, receiver) }
+ reply_key = "launched.#{@uuid.generate}"
+ log(fn: "launch", app_name: app_name, reply_key: reply_key) do
+ heroku = Heroku::API.new(:api_key => api_key)
+ heroku.post_ps(app_name, "bundle exec bin/receiver",
+ { :ps_env => receiver_config(reply_key) })
+
+ log(fn: "launch", app_name: app_name, reply_key: reply_key, at: "wait")
+ @redis.blpop(reply_key).tap {|receiver| @redis.hset(app_name, receiver) }
+ end
end
def api_key(env)
- auth = Rack::Auth::Basic::Request.new(env)
+ auth = Rack::Auth::Basic::Request.new(env) # TODO: git never honors netrc
if auth.provided? && auth.basic? && auth.credentials
auth.credentials[1]
else
View
@@ -5,9 +5,11 @@
require 'redis'
require 'json'
require 'time'
+require 'scrolls'
module Sokoban
class Receiver
+ include Scrolls
ROUTES =
[["POST", :service_rpc, /(.*?)\/git-upload-pack$/, 'upload-pack'],
@@ -25,12 +27,16 @@ class Receiver
]
def initialize(repo_url)
+ Scrolls.global_context(app: "sokoban", receiver: true)
bundle = File.join("/tmp", "repo.bundle")
@repo_dir = File.join("/tmp", "repo.git")
- system("curl --retry 3 --max-time 90 #{repo_url} > #{bundle}")
- system("git bundle verify #{bundle}") or raise "Corrupt repo."
- system("git clone --bare #{bundle} #{@repo_dir}")
+ log(at: "clone") do
+ system("curl --retry 3 --max-time 90 #{repo_url} > #{bundle}")
+ system("git bundle verify #{bundle}") or raise "Corrupt repo."
+ system("git clone --bare #{bundle} #{@repo_dir}")
+ File.delete(bundle)
+ end
reply
end
@@ -63,6 +69,7 @@ def route(req_method, req_path)
def reply
host = UDPSocket.open { |s| s.connect("64.233.187.99", 1); s.addr.last }
reply = JSON.unparse({:host => host, :port => ENV["PORT"]})
+ log(fn: "reply", host: host, port: port)
Redis.new(:url => ENV["REDIS_URL"]).lpush(ENV["REPLY_KEY"], reply)
end

0 comments on commit eaa9b3e

Please sign in to comment.