From e0dffdb6ea30cf49168cfd41dd325dfebbc2aaeb Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Wed, 19 Sep 2012 13:02:22 +0200 Subject: [PATCH] appmarket: Rename server.rb to appmarket-server.rb --- market/bin/appmarket-server | 4 +- market/config.ru | 2 +- market/controllers/server.rb | 299 ----------------------------------- 3 files changed, 3 insertions(+), 302 deletions(-) delete mode 100644 market/controllers/server.rb diff --git a/market/bin/appmarket-server b/market/bin/appmarket-server index c1c42a4c4a..feb562cecb 100755 --- a/market/bin/appmarket-server +++ b/market/bin/appmarket-server @@ -18,14 +18,14 @@ if [ -z "$ONE_LOCATION" ]; then MARKET_PID=/var/run/one/appmarket-server.pid - MARKET_SERVER=/usr/lib/one/ruby/apptools/market/controllers/server.rb + MARKET_SERVER=/usr/lib/one/ruby/apptools/market/controllers/appmarket-server.rb MARKET_LOCK_FILE=/var/lock/one/.appmarket.lock MARKET_LOG=/var/log/one/appmarket-server.log MARKET_LOG_ERROR=/var/log/one/appmarket-server.error MARKET_ETC=/etc/one/appmarket-server.conf else MARKET_PID=$ONE_LOCATION/var/appmarket-server.pid - MARKET_SERVER=$ONE_LOCATION/lib/ruby/apptools/market/controllers/server.rb + MARKET_SERVER=$ONE_LOCATION/lib/ruby/apptools/market/controllers/appmarket-server.rb MARKET_LOCK_FILE=$ONE_LOCATION/var/.appmarket.lock MARKET_LOG=$ONE_LOCATION/var/appmarket-server.log MARKET_LOG_ERROR=$ONE_LOCATION/var/appmarket-server.error diff --git a/market/config.ru b/market/config.ru index d4e660fe17..b4e73551d6 100644 --- a/market/config.ru +++ b/market/config.ru @@ -1,4 +1,4 @@ $: << '.' -require 'controllers/server' +require 'controllers/appmarket-server' run Sinatra::Application \ No newline at end of file diff --git a/market/controllers/server.rb b/market/controllers/server.rb deleted file mode 100644 index 72e66fb123..0000000000 --- a/market/controllers/server.rb +++ /dev/null @@ -1,299 +0,0 @@ -require 'rubygems' -require 'sinatra' -require 'haml' -require 'redcarpet' - -ONE_LOCATION = ENV["ONE_LOCATION"] - -if !ONE_LOCATION - LOG_LOCATION = "/var/log/one" - VAR_LOCATION = "/var/lib/one" - ETC_LOCATION = "/etc/one" - RUBY_LIB_LOCATION = "/usr/lib/one/ruby" -else - VAR_LOCATION = ONE_LOCATION + "/var" - LOG_LOCATION = ONE_LOCATION + "/var" - ETC_LOCATION = ONE_LOCATION + "/etc" - RUBY_LIB_LOCATION = ONE_LOCATION+"/lib/ruby" -end - -APPMARKET_LOG = LOG_LOCATION + "/appmarket-server.log" -CONFIGURATION_FILE = ETC_LOCATION + "/appmarket-server.conf" - -$: << RUBY_LIB_LOCATION -$: << RUBY_LIB_LOCATION+'/cloud' -$: << RUBY_LIB_LOCATION+'/apptools/market' - -require 'models' -require 'session' -require 'parser' -require 'mailer' - -require 'pp' - -configure do - set :views, File.join(File.dirname(__FILE__), '..', 'views') - - if settings.respond_to? :public_folder - set :public_folder, File.join(File.dirname(__FILE__), '..', 'public') - else - set :public, File.join(File.dirname(__FILE__), '..', 'public') - end - - # Initialize DB with admin credentials - if User.list(nil).empty? - User.bootstrap(CONF['user']) - end - - set :bind, CONF[:host] - set :port, CONF[:port] - - set :config, CONF -end - -use Rack::Session::Pool, :key => 'appmarket' - -helpers do - def build_session - @session = Session.new(request.env) - - if params[:remember] == "true" - env['rack.session.options'][:expire_after] = 30*60*60*24 - end - - session[:ip] = request.ip - session[:instance] = @session - end -end - - -before do - if request.env['PATH_INFO'] == '/' - redirect '/appliance' - end - - if request.path != '/login' && request.path != '/logout' - if session[:ip] && session[:ip]==request.ip - @session = session[:instance] - else - build_session - end - - unless @session.authorize(request.env) - if request.env["HTTP_ACCEPT"] && request.env["HTTP_ACCEPT"].split(',').grep(/text\/html/).empty? - error 401 - else - redirect '/appliance' - end - end - end -end - -# -# Login -# - -post '/login' do - build_session - halt 401 if @session.anonymous? -end - -post '/logout' do - session.clear - redirect '/appliance' -end - -# -# User -# - -get '/user' do - if request.env["HTTP_ACCEPT"] && request.env["HTTP_ACCEPT"].split(',').grep(/text\/html/).empty? - Parser.generate_body({'sEcho' => "1", 'users' => User.list(@session)}) - else - haml :user_index - end -end - -get '/user/:id' do - begin - @user = User.show(@session, params[:id]) - rescue BSON::InvalidObjectId - error 404, $!.message - end - - if @user.nil? - error 404 - end - - if request.env["HTTP_ACCEPT"] && request.env["HTTP_ACCEPT"].split(',').grep(/text\/html/).empty? - Parser.generate_body(@user) - else - haml :user_show - end -end - -post '/user/:id/enable' do - begin - user = User.show(@session, params[:id]) - rescue BSON::InvalidObjectId - error 404, $!.message - end - - if user.nil? - error 404 - end - - begin - update_result = User.enable(@session, params[:id]) - rescue BSON::InvalidObjectId - error 404, $!.message - rescue Validator::ParseException - error 400, $!.message - end - - #if update_result != true - # status 404 - #end - - if setting.config['mail'] - Mailer.send_enable(user['email'], user['username']) - end -end - -post '/user' do - begin - object_id = User.create(@session, Parser.parse_body(request)) - rescue Validator::ParseException - error 400, $!.message - rescue Mongo::OperationFailure - error 400, "The username/organization already exists in the OpenNebula Marketplace" - end - - Parser.generate_body({"_id" => object_id}) -end - -put '/user/:id' do - begin - @user = User.update(@session, params[:id], Parser.parse_body(request)) - rescue BSON::InvalidObjectId - error 404, $!.message - rescue Validator::ParseException - error 400, $!.message - rescue - error 400, $!.message - end - - status 200 -end - -delete '/user/:id' do - begin - @user = User.delete(@session, params[:id]) - rescue BSON::InvalidObjectId - error 404, $!.message - end -end - -# -# Appliance -# - -get '/appliance' do - if request.env["HTTP_ACCEPT"] && request.env["HTTP_ACCEPT"].split(',').grep(/text\/html/).empty? - Parser.generate_body({'sEcho' => "1", 'appliances' => Appliance.list(@session)}) - else - haml :appliance_index - end -end - -get '/appliance/:id' do - begin - @app = Appliance.show(@session, params[:id]) - rescue BSON::InvalidObjectId - error 404, $!.message - end - - if @app.nil? - error 404 - end - - appliance_url = request.env['rack.url_scheme'] + - '://' + - request.env['HTTP_HOST'] + - request.env['REQUEST_URI'] - - @app['links'] = { - 'download' => { - 'href' => appliance_url + '/download' - } - } - - if request.env["HTTP_ACCEPT"] && request.env["HTTP_ACCEPT"].split(',').grep(/text\/html/).empty? - Parser.generate_body(@app) - else - render = Redcarpet::Render::HTML.new( - :filter_html => true, - :no_images => false, - :no_links => false, - :no_styles => true, - :safe_links_only => true, - :with_toc_data => true, - :hard_wrap => true, - :xhtml => true) - - @markdown = Redcarpet::Markdown.new(render, - :autolink => true, - :space_after_headers => true) - - haml :appliance_show - end -end - -post '/appliance' do - begin - object_id = Appliance.create(@session, Parser.parse_body(request)) - rescue Validator::ParseException - error 400, $!.message - rescue - error 400, $!.message - end - - Parser.generate_body({"_id" => object_id}) -end - -delete '/appliance/:id' do - begin - @app = Appliance.delete(@session, params[:id]) - rescue BSON::InvalidObjectId - error 404, $!.message - end -end - -put '/appliance/:id' do - begin - @app = Appliance.update(@session, params[:id], Parser.parse_body(request)) - rescue BSON::InvalidObjectId - error 404, $!.message - rescue Validator::ParseException - error 400, $!.message - rescue - error 400, $!.message - end - - status 200 -end - -get '/appliance/:id/download' do - begin - url = Appliance.file_url(@session, params[:id]) - rescue BSON::InvalidObjectId - error 404, $!.message - end - - if url.nil? - error 404 - end - - redirect url -end