Permalink
Browse files

rake app, use redis store project data, support cname

  • Loading branch information...
1 parent 843c3ad commit a8f3a154604a4aaf23ce7ae2aab5f266f5898617 @tka tka committed Feb 2, 2013
View
@@ -3,3 +3,5 @@ source "https://rubygems.org"
gem "sinatra"
gem "sinatra-contrib"
+gem "rack-contrib"
+gem "redis"
@@ -4,10 +4,13 @@ GEM
backports (2.7.1)
eventmachine (1.0.0)
rack (1.5.1)
+ rack-contrib (1.1.0)
+ rack (>= 0.9.1)
rack-protection (1.3.2)
rack
rack-test (0.6.2)
rack (>= 1.0)
+ redis (3.0.2)
sinatra (1.3.4)
rack (~> 1.4)
rack-protection (~> 1.3)
@@ -25,5 +28,7 @@ PLATFORMS
ruby
DEPENDENCIES
+ rack-contrib
+ redis
sinatra
sinatra-contrib
View
@@ -2,5 +2,152 @@ require 'bundler'
Bundler.require
-require './web_hosting.rb'
-run WebHosting
+require 'rack'
+require 'rack/mime'
+require 'rack/contrib'
+require 'redis'
+require 'yaml'
+require 'json'
+
+
+class TheHoldApp
+ def initialize
+ @redis = Redis.new
+ @base_path = "user_sites"
+ @cname_domain = "localhost"
+ end
+
+ def call(env)
+ req = Rack::Request.new(env)
+
+ site_key = "site-" + env["HTTP_HOST"].split(/:/).first
+ site = @redis.hgetall(site_key)
+ current_project_path = File.join(@base_path, site["login"], site["project"], "current")
+
+ return not_found if !( site["login"] && site["project"] && env["PATH_INFO"] != '/AUTH' )
+ return upload_file(req.params) if env["PATH_INFO"] == '/upload'
+
+ if site["auth_yaml"]
+ auth_yaml = YAML.load( site["auth_yaml"] )
+ if req.post? && req.params["login"] && req.params["password"]
+ if auth_yaml[ req.params["login"] ] && auth_yaml[ req.params["login"] ] == req.params["password"]
+ env["rack.session"]["password"]= req.params["password"]
+ env["rack.session"]["login"]= req.params["login"]
+ end
+ end
+ if auth_yaml[ env["rack.session"]["login"] ] == env["rack.session"]["password"]
+ return login(env)
+ end
+ end
+
+ path_info = env["PATH_INFO"][-1] == '/' ? "#{env["PATH_INFO"]}index.html" : env["PATH_INFO"]
+
+ if File.extname(path_info) == ""
+ path_info += "/index.html" if File.directory?( File.join( File.dirname(__FILE__), current_project_path, path_info ) )
+ end
+
+ redirect_url = File.join( "/", current_project_path, path_info )
+
+ mime_type = Rack::Mime.mime_type(File.extname(redirect_url), "text/html")
+ [200, {"Cache-Control" => "no-cache, no-store", 'Content-Type' => mime_type, 'X-Accel-Redirect' => redirect_url }, []]
+ end
+
+
+ def upload_file(params)
+ user_token_key = "user-#{params["login"]}"
+ user_token = @redis.get(user_token_key)
+ return forbidden unless user_token && user_token == params["token"]
+
+ Dir.chdir( File.dirname(__FILE__) )
+ tempfile_path = params["patch_file"][:tempfile].path
+ project_folder = File.join( @base_path, params["login"], params["project"])
+ project_folder = File.expand_path(project_folder)
+
+ FileUtils.mkdir_p(project_folder)
+ Dir.chdir(project_folder )
+
+ to_folder = File.expand_path( Time.now.strftime("%Y%m%d%H%M%S") )
+ FileUtils.mkdir(to_folder)
+ FileUtils.cp(tempfile_path, File.join(to_folder, "_patch.zip"))
+ Dir.chdir(to_folder)
+ %x{unzip _patch.zip}
+
+ to_json_file = 'manifest.json'
+ to_json_data = open(to_json_file,'r'){|f| f.read}
+ to = JSON.load( to_json_data )
+
+ to.each do |filename, md5|
+ if !File.exists?(filename)
+ FileUtils.mkdir_p(File.dirname(filename))
+ form_filename = File.expand_path( File.join("../current", filename) )
+ to_filename = File.expand_path( filename )
+ if form_filename.index(project_folder) && to_filename.index(project_folder)
+ next if !File.exists?(form_filename)
+ link_cmd = "ln -P '#{form}/#{filename}' '#{filename}'"
+ %x{#{link_cmd}}
+ end
+ end
+ end
+ FileUtils.rm("_patch.zip")
+ Dir.chdir( project_folder )
+ File.unlink("current") if File.exists?("current")
+ File.symlink(to_folder, "current")
+
+ project_hostname = "#{params["project"]}.#{params["login"]}.#{@cname_domain}"
+ @redis.hmset("site-#{project_hostname}", :login, params["login"], :project, params["project"] );
+
+ if params["cname"]
+ cname = params["cname"]
+ begin
+ dns = Resolv::DNS.new
+ target_name = dns.getresources(cname, Resolv::DNS::Resource::IN::CNAME).first.try(:name)
+ if target_name && target_name.to_s == project_hostname
+ @redis.hmset("site-#{cname}", :login, params["login"], :project, params["project"] );
+ end
+ end
+ end
+
+ auth_yaml_file = File.join( "current", "AUTH")
+ if File.exists?( auth_yaml_file ) && File.size(auth_yaml) < 8192
+ auth_yaml = YAML.load_file( auth_yaml_file )
+ valid_yaml = {}
+ auth_yaml.each{|id, pw| valid_yaml[id] = pw if id.is_a?(String) && pw.is_a?(String)}
+ @redis.hset("site-#{cname}", "auth_yaml", YAML.dump(auth_yaml) );
+ else
+ @redis.hdel("site-#{cname}", "auth_yaml" );
+ end
+
+ [200, {"Content-Type" => "text/plain"}, ["ok"]]
+ end
+
+ def not_found
+ [404, {'Content-Type' => 'text/plain' }, ["Not Fonud"]]
+ end
+
+ def forbidden
+ [403, {'Content-Type' => 'text/plain' }, ["Forbidden"]]
+ end
+
+ def login(env)
+ [200, { "Content-Type" => "text/html" }, [<<EOL
+ <html>
+ <body>
+ <h1>Login</h1>
+ <form action="#{env["PATH_INFO"]}" method="post">
+ login:<input type="text" name="login">
+
+ password:<input type="password" name="password">
+ <input type="submit">
+ </form>
+ </body>
+ </html>
+EOL
+ ]]
+ end
+
+end
+
+use Rack::Session::Cookie, :secret => '12345'
+
+run TheHoldApp.new
+
@@ -1,82 +0,0 @@
-require "sinatra/base"
-require "sinatra/reloader"
-require "sinatra/config_file"
-require "json"
-
-
-class WebHosting < Sinatra::Base
- configure :development do
- register Sinatra::Reloader
- end
-
- register Sinatra::ConfigFile
- config_file 'config.yml'
-
- get "*" do
- request.host
- end
-
- post "/upload" do
-
- halt 401, "forbidden" if params[:key] != "1234567890"
- # raise "PLEASE REPLACE THE KEY"
- Dir.chdir( File.dirname(__FILE__) )
- tempfile_path = params[:patch_file][:tempfile].path
- project_folder = File.join( settings.base_folder, params[:login], params[:project])
- project_folder = File.expand_path(project_folder)
-
- FileUtils.mkdir_p(project_folder)
- Dir.chdir(project_folder )
-
- to_folder = File.expand_path( Time.now.strftime("%Y%m%d%H%M%S") )
- FileUtils.mkdir(to_folder)
- FileUtils.cp(tempfile_path, File.join(to_folder, "_patch.zip"))
- Dir.chdir(to_folder)
- %x{unzip _patch.zip}
-
- to_json_file = 'manifest.json'
- to_json_data = open(to_json_file,'r'){|f| f.read}
- to = JSON.load( to_json_data )
-
- to.each do |filename, md5|
- if !File.exists?(filename)
- FileUtils.mkdir_p(File.dirname(filename))
- form_filename = File.expand_path( File.join("../current", filename) )
- to_filename = File.expand_path( filename )
- if form_filename.index(project_folder) && to_filename.index(project_folder)
- next if !File.exists?(form_filename)
- link_cmd = "ln -P '#{form}/#{filename}' '#{filename}'"
- %x{#{link_cmd}}
- end
- end
- end
- FileUtils.rm("_patch.zip")
- Dir.chdir( project_folder )
- File.unlink("current") if File.exists?("current")
- File.symlink(to_folder, "current")
-
- cname_filepath = File.join('current', 'CNAME')
- if File.file?(cname_filepath)
- cname = open(cname_filepath, 'r'){|f| f.gets.strip}
- puts cname
- begin
- dns = Resolv::DNS.new
- target_name = dns.getresources(cname, Resolv::DNS::Resource::IN::CNAME).first.try(:name)
- allow_cname = "#{params[:project]}.#{params[:login]}.#{settings.cname_domain}"
- if target_name && target_name.to_s == allow_cname
- Dir.chdir(File.dirname(__FILE__))
- target_cname_folder = File.join(settings.cname_folder, cname)
- puts target_cname_folder
- puts File.exists?( target_cname_folder).inspect
- puts File.symlink?( target_cname_folder).inspect
-
- File.unlink( target_cname_folder ) if File.exists?( target_cname_folder) || File.symlink?(target_cname_folder)
- File.symlink(to_folder, target_cname_folder)
- end
- rescue
- end
- end
- "ok"
- end
-
-end
View
@@ -1,5 +1,5 @@
module CompileVersion
- REVISION = 'f0863ce247'
- COMPILE_TIME = '201302011138'
+ REVISION = '843c3ad6bf'
+ COMPILE_TIME = '201302020730'
UPDATE_URL = ''
end
View
@@ -58,13 +58,15 @@ def self.upload_patch(to_folder, options)
req = Net::HTTP::Post::Multipart.new url.path,
"patch_file" => UploadIO.new(tempfile_io, "application/zip", "patch_file.zip"),
"login" => options[:login],
- "key" => options[:key],
+ "token" => options[:token],
"project" => options[:project]
- res = Net::HTTP.start(url.host, url.port) do |http|
+ respone = Net::HTTP.start(url.host, url.port) do |http|
http.request(req)
end
tempfile_io.close
File.unlink(tempfile)
+ puts respone.inspect
+ respone
end
end
View
@@ -505,9 +505,14 @@ def deploy_project_handler
App.try do
options = Compass.configuration.the_hold_options
temp_build_folder = File.join(Dir.tmpdir, "fireapp", rand.to_s)
- TheHoldUploader.upload_patch(build_project(temp_build_folder, {:headless => true}), options)
- host=URI(options[:host]).host
- Swt::Program.launch("http://#{options[:project]}.#{options[:login]}.#{host}")
+ respone = TheHoldUploader.upload_patch(build_project(temp_build_folder, {:headless => true}), options)
+ if respone.code == "200"
+ host=URI(options[:host]).host
+ Swt::Program.launch("http://#{options[:project]}.#{options[:login]}.#{host}")
+ App.alert("done")
+ else
+ App.alert(respone.body)
+ end
end
end
end

0 comments on commit a8f3a15

Please sign in to comment.