From 0c3a044f81406372295b4975aab604ee0a4a6da6 Mon Sep 17 00:00:00 2001 From: Pavel Chipiga Date: Sun, 30 Oct 2011 18:46:32 +0200 Subject: [PATCH] Fix REXML Exception when using Unicorn or Passenger with XmlRPC. Add XML RPC events logging. Refactor. Improve after_rollback callback. Refs #2162 --- Gemfile | 2 +- Gemfile.lock | 9 ++++-- app/controllers/rpc_controller.rb | 47 ++++++++++++++++------------- app/models/event_log.rb | 2 +- app/models/project.rb | 2 +- app/models/project_to_repository.rb | 2 +- bin/xml-client-demo.rb | 6 ++-- config/locales/event_log.ru.yml | 3 ++ db/schema.rb | 7 ++--- 9 files changed, 46 insertions(+), 34 deletions(-) diff --git a/Gemfile b/Gemfile index 859535768..dcea2c372 100644 --- a/Gemfile +++ b/Gemfile @@ -29,7 +29,7 @@ gem 'unicorn' # XML-RPC support # gem 'actionwebservice' #, :git => 'git://github.com/ywen/actionwebservice.git' -gem "rails-xmlrpc" +gem "rails-xmlrpc", :git => 'git://github.com/chipiga/rails-xmlrpc.git' group :production do gem "airbrake" diff --git a/Gemfile.lock b/Gemfile.lock index 308de2191..2e7293c13 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,3 +1,9 @@ +GIT + remote: git://github.com/chipiga/rails-xmlrpc.git + revision: 3cf79e062c65447809209724a0cb8d1ea5af2e57 + specs: + rails-xmlrpc (0.3.1) + GIT remote: git@github.com:warpc/gitolito.git revision: 8608b9bdfd33e961d9f9d71dd594e62780c074f6 @@ -189,7 +195,6 @@ GEM activesupport (= 3.0.10) bundler (~> 1.0) railties (= 3.0.10) - rails-xmlrpc (0.3.2) rails3-generators (0.17.4) railties (>= 3.0.0) railties (3.0.10) @@ -280,7 +285,7 @@ DEPENDENCIES paperclip (~> 2.3) pg (~> 0.11.0) rails (= 3.0.10) - rails-xmlrpc + rails-xmlrpc! rails3-generators rspec-rails (~> 2.7.0) ruby-debug diff --git a/app/controllers/rpc_controller.rb b/app/controllers/rpc_controller.rb index d7c3ffd7f..3ee0e8984 100644 --- a/app/controllers/rpc_controller.rb +++ b/app/controllers/rpc_controller.rb @@ -3,7 +3,8 @@ class RpcController < ApplicationController before_filter :authenticate_user! before_filter :check_global_access - + before_filter lambda { EventLog.current_controller = self }, :only => :xe_index # should be after auth callback + ## Usage example: # # require 'xmlrpc/client' @@ -11,27 +12,31 @@ class RpcController < ApplicationController # client.call("project_versions", 1) def platforms + ActiveSupport::Notifications.instrument("event_log.observer", :message => 'список платформ') return Platform.select('id, unixname').where("platform_type = ?", 'main').map(&:attributes) end - - def user_projects - current_user.projects.map{|pr| { :id => pr.id, :unixname => pr.unixname } } - end - - def project_versions id - pr = Project.findby_id(id) - return nil if pr.blank? - pr.project_versions.collect { |tag| [tag.name.gsub(/^\w+\./, ""), tag.name] }.select { |pv| pv[1] =~ /^v\./ } - end - - def build_status id - BuildList.find_by_id(id).try(:status) - end - - def build_packet project_id, repo_id - # TODO: build packet - end - - + def user_projects + ActiveSupport::Notifications.instrument("event_log.observer", :message => 'список пользовательских проектов') + current_user.projects.map{|p| { :id => p.id, :unixname => p.unixname } } + end + + def project_versions id + p = Project.find_by_id(id) + ActiveSupport::Notifications.instrument("event_log.observer", :object => p, :message => "список версий") + return nil if p.blank? + p.project_versions.collect {|tag| [tag.name.gsub(/^\w+\./, ""), tag.name]}.select {|pv| pv[1] =~ /^v\./} + end + + def build_status id + bl = BuildList.find_by_id(id) + ActiveSupport::Notifications.instrument("event_log.observer", :object => bl, :message => 'статус сборки') + bl.try(:status) || 'not found' + end + + def build_packet project_id, repo_id + # p = Project.find_by_id(project_id); r = Repository.find_by_id(repo_id) + ActiveSupport::Notifications.instrument("event_log.observer", :message => 'сборка пакета') + 'unknown' # TODO: build packet + end end diff --git a/app/models/event_log.rb b/app/models/event_log.rb index feda49658..e69226f6c 100644 --- a/app/models/event_log.rb +++ b/app/models/event_log.rb @@ -18,9 +18,9 @@ class EventLog < ActiveRecord::Base create(attributes) do |el| el.user = current_controller.current_user el.ip = current_controller.request.remote_ip - el.protocol = 'web' # TODO pass protocol through controller or calculate by name el.controller = current_controller.class.to_s el.action = current_controller.action_name + el.protocol = (el.controller == 'RpcController' ? 'api' : 'web') end end diff --git a/app/models/project.rb b/app/models/project.rb index 744154c2a..7dece9c00 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -36,7 +36,7 @@ class Project < ActiveRecord::Base after_create :create_git_repo before_update :update_git_repo after_destroy :destroy_git_repo - after_rollback lambda { destroy_git_repo rescue true } + after_rollback lambda { destroy_git_repo rescue true if new_record? } def project_versions #tags.collect { |tag| [tag.name, tag.name.gsub(/^\w+\./, "")] }.select { |pv| pv[0] =~ /^v\./ } diff --git a/app/models/project_to_repository.rb b/app/models/project_to_repository.rb index eb8bcc4d1..f8caddff8 100644 --- a/app/models/project_to_repository.rb +++ b/app/models/project_to_repository.rb @@ -11,7 +11,7 @@ class ProjectToRepository < ActiveRecord::Base after_create lambda { project.xml_rpc_create(repository) } after_destroy lambda { project.xml_rpc_destroy(repository) } - after_rollback lambda { project.xml_rpc_destroy(repository) rescue true } + after_rollback lambda { project.xml_rpc_destroy(repository) rescue true if new_record? } #def path # build_path(project.unixname) diff --git a/bin/xml-client-demo.rb b/bin/xml-client-demo.rb index f4f4ec056..f5f634177 100644 --- a/bin/xml-client-demo.rb +++ b/bin/xml-client-demo.rb @@ -2,13 +2,13 @@ require 'rubygems' require 'xmlrpc/client' -require 'ap' # awesome_print +require 'awesome_print' # Please correctly fill following vars @host = 'localhost' @port = 3000 -@user = 'user@email' -@password = '' +@user = 'pchipiga@ya.ru' +@password = '123456' puts 'PLATFORMS' client = XMLRPC::Client.new(@host, '/api/xmlrpc', @port, nil, nil, @user, @password, false, 900) diff --git a/config/locales/event_log.ru.yml b/config/locales/event_log.ru.yml index 4c90558c7..dda6cc2ef 100644 --- a/config/locales/event_log.ru.yml +++ b/config/locales/event_log.ru.yml @@ -10,6 +10,7 @@ ru: 'devise/sessions_controller': 'Аутентификация пользователей' 'devise/passwords_controller': 'Восстановление пароля' 'users/omniauth_callbacks_controller': 'Внешняя аутентификация пользователей' + rpc_controller: XML RPC actions: 'devise/sessions_controller': create: 'вход' @@ -22,6 +23,8 @@ ru: build_lists_controller: cancel: 'сборка отменена' publish: 'сборка опубликована' + rpc_controller: + xe_index: запрос create: 'создано' update: 'обновлено' destroy: 'удалено' diff --git a/db/schema.rb b/db/schema.rb index 7cfc9f335..0ab0ec49d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -271,11 +271,10 @@ ActiveRecord::Schema.define(:version => 20111029150934) do create_table "users", :force => true do |t| t.string "name" - t.string "email", :default => "", :null => false - t.string "encrypted_password", :limit => 128, :default => "", :null => false - t.string "password_salt", :default => "", :null => false + t.string "email", :default => "", :null => false + t.string "encrypted_password", :limit => 128, :default => "", :null => false t.string "reset_password_token" - t.string "remember_token" + t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.datetime "created_at" t.datetime "updated_at"