Code clean up, removed access to api via standart login

This commit is contained in:
Wedge 2016-03-22 20:02:49 +03:00
parent 9c47ba64ba
commit d5180f73d7
7 changed files with 41 additions and 223 deletions

View File

@ -1,6 +1,8 @@
class Api::V1::BaseController < ApplicationController class Api::V1::BaseController < ApplicationController
include PaginateHelper include PaginateHelper
respond_to :json respond_to :json
skip_before_action :verify_authenticity_token
before_action :check_auth
helper_method :member_path helper_method :member_path
@ -13,6 +15,14 @@ class Api::V1::BaseController < ApplicationController
protected protected
def check_auth
authenticate_or_request_with_http_basic do |token,pw|
if user = User.find_by_authentication_token(token)
sign_in user, false
end
end
end
def set_csv_file_headers(file_name) def set_csv_file_headers(file_name)
headers['Content-Type'] = 'text/csv' headers['Content-Type'] = 'text/csv'
headers['Content-disposition'] = "attachment; filename=\"#{file_name}.csv\"" headers['Content-disposition'] = "attachment; filename=\"#{file_name}.csv\""

View File

@ -53,36 +53,6 @@ class ApplicationController < ActionController::Base
end end
end end
# For this example, we are simply using token authentication
# via parameters. However, anyone could use Rails's token
# authentication features to get the token from a header.
def authenticate_user!
if user = find_user_by_token
# Notice we are passing store false, so the user is not
# actually stored in the session and a token is needed
# for every request. If you want the token to work as a
# sign in token, you can simply remove store: false.
sign_in user, store: false
else
super
end
end
def authenticate_user
if user = find_user_by_token
sign_in user, store: false
end
end
def find_user_by_token
user_token = params[:authentication_token].presence
if user_token.blank? && request.authorization.present?
token, pass = *ActionController::HttpAuthentication::Basic::user_name_and_password(request)
user_token = token if pass.blank?
end
user = user_token && User.find_by_authentication_token(user_token.to_s)
end
def render_500(e) def render_500(e)
#check for exceptions Airbrake ignores by default and exclude them from manual Airbrake notification #check for exceptions Airbrake ignores by default and exclude them from manual Airbrake notification
if Rails.env.production? && !AIRBRAKE_IGNORE.include?(e.class) if Rails.env.production? && !AIRBRAKE_IGNORE.include?(e.class)

View File

@ -1,17 +1,9 @@
class Projects::BaseController < ApplicationController class Projects::BaseController < ApplicationController
prepend_before_action :authenticate_user_and_find_project prepend_before_action :find_project
protected protected
def find_collaborators def find_project
search = "%#{params[:search_user]}%"
@users = @project.collaborators.where("users.uname ILIKE ?", search)
@users |= @project.owner.members.where("users.uname ILIKE ?", search) if @project.owner.is_a?(Group)
@users = @users.sort_by(&:uname).first(10)
end
def authenticate_user_and_find_project
authenticate_user
return if params[:name_with_owner].blank? return if params[:name_with_owner].blank?
authorize @project = Project.find_by_owner_and_name!(params[:name_with_owner]), :show? authorize @project = Project.find_by_owner_and_name!(params[:name_with_owner]), :show?
end end

View File

@ -0,0 +1,22 @@
class RemoveOutdatedItemsJob
@queue = :low
def self.perform
log_file = Rails.root.join("log", "remove_outdated.log").to_s
counter_bl = 0
BuildList.outdated.find_each(batch_size: 100) do |bl|
bl.destroy && (counter_bl += 1) if bl.id != bl.last_published.first.try(:id)
end
counter_mb = 0
MassBuild.outdated.find_each do |mb|
mb.destroy && (counter_mb += 1) if mb.build_lists.count == 0
end
counter_pbl = ProductBuildList.outdated.count
ProductBuildList.outdated.destroy_all
File.open(log_file, "w") do |f|
f.puts "Build Lists deleted: #{counter_bl}"
f.puts "Mass Builds deleted: #{counter_mb}"
f.puts "Product Build Lists deleted: #{counter_pbl}"
end
end
end

View File

@ -39,3 +39,9 @@ clear_builder_on_stale_buildlists:
class: 'BuildLists::ClearBuilderOnStaleBuildListsJob' class: 'BuildLists::ClearBuilderOnStaleBuildListsJob'
queue: low queue: low
description: 'Clear builder on build lists which are still pending' description: 'Clear builder on build lists which are still pending'
remove_outdated_items:
cron: '0 3 * * *'
class: 'RemoveOutdatedItemsJob'
queue: low
description: 'Removes outdated mass builds, product buid lists and build lists'

View File

@ -6,22 +6,6 @@
# runner 'Download.parse_and_remove_nginx_log' # runner 'Download.parse_and_remove_nginx_log'
#end #end
every :day, at: '4:10 am' do
rake 'product_build_list:clear:outdated', output: 'log/product_build_list_clear.log'
end
every :day, at: '4:00 am' do
rake 'import:sync:all', output: 'log/sync.log'
end
every :day, at: '3:50 am' do
rake 'buildlist:clear:outdated', output: 'log/build_list_clear.log'
end
every :day, at: '3:30 am' do
rake 'pull_requests:clear', output: 'log/pull_requests_clear.log'
end
every :day, at: '3:00 am' do every :day, at: '3:00 am' do
rake 'activity_feeds:clear', output: 'log/activity_feeds.log' rake 'activity_feeds:clear', output: 'log/activity_feeds.log'
end end

View File

@ -1,166 +0,0 @@
require 'highline/import'
require 'open-uri'
namespace :import do
desc "Load projects"
task projects: :environment do
source = ENV['SOURCE'] || 'http://dl.dropbox.com/u/984976/package_list.txt'
owner = Group.find_by uname: "npp_team"
platform = Platform.find_by name: "RosaNPP" # RosaNPP
repo = platform.repositories.first rescue nil
say "START import projects from '#{source}' for '#{owner.uname}'.#{repo ? " To repo '#{platform.name}/#{repo.name}'." : ''}"
ask 'Press enter to continue'
open(source).readlines.each do |name|
name.chomp!; name.strip! #; name.downcase!
# name = name.match(/^([a-z\d_\-\+\.]+?)-(\d[a-z\d\-\.]+)\.src\.rpm$/)[1] # parse
print "Import '#{name}'..."
p = Project.find_or_create_by_name_and_owner_type_and_owner_id(name, owner.class.to_s, owner.id)
print p.persisted? ? "Ok!" : "Fail!"
if repo
print " Add to repo '#{platform.name}/#{repo.name}'."
repo.projects << p rescue print ' Fail!'
end
puts
end
say 'DONE'
end
# bundle exec rake import:srpm RAILS_ENV=production BASE=/share/platforms/naulinux5x_personal/tmp/SRPMS LIST=https://dl.dropbox.com/u/984976/nauschool5x.srpms.txt OWNER=naulinux PLATFORM=naulinux REPO=main CLEAR=true HIDDEN=true > log/srpm_naulinux.log &
desc 'Import SRPMs as projects'
task srpm: :environment do
base = ENV['BASE'] || '/share/alt_repos/rsync'
list = ENV['LIST'] #|| 'https://dl.dropbox.com/u/984976/alt_import.txt'
mask = ENV['MASK'] || '*.src.rpm'
hidden = ENV['HIDDEN'] == 'true' ? true : false
owner = User.find_by(uname: ENV['OWNER']) || Group.find_by!(uname: ENV['OWNER'] || 'altlinux')
platform = Platform.find_by!(name: ENV['PLATFORM'] || 'altlinux5')
repo = platform.repositories.find_by!(name: ENV['REPO'] || 'main')
clear = ENV['CLEAR'] == 'true' ? true : false
say "START import projects from '#{base}' using '#{list || mask}' for '#{owner.uname}' to repo '#{platform.name}/#{repo.name}'."
repo.project_to_repositories.clear if clear
(list ? open(list).readlines.map{|n| File.join base, n.chomp.strip} : Dir[File.join base, mask]).each do |srpm_file|
print "Processing '#{srpm_file}'... "
if name = `rpm -q --qf '[%{Name}]' -p #{srpm_file}` and $?.success? and name.present?
if clear # simply add
project = Project.find_or_create_by_name_and_owner_type_and_owner_id(name, owner.class.to_s, owner.id)
repo.projects << project rescue nil
else # check if project already added
if project = repo.projects.find_by(name: name) || repo.projects.by_name(name).first # fallback to speedup
print "Found project '#{project.name_with_owner}' in '#{platform.name}/#{repo.name}'."
elsif scoped = Project.where(owner_id: owner.id, owner_type: owner.class) and
project = scoped.find_by(name: name) || scoped.by_name(name).first
begin
repo.projects << project rescue nil
rescue Exception => e
print "Add project '#{project.name_with_owner}' to '#{platform.name}/#{repo.name}' FAILED: #{e.message}."
else
print "Add project '#{project.name_with_owner}' to '#{platform.name}/#{repo.name}' OK."
end
else
description = `rpm -q --qf '[%{Description}]' -p #{srpm_file}`.scrub('')
project = Project.create!(name: name, description: description) {|p| p.owner = owner}
repo.projects << project rescue nil
print "Create project #{project.name_with_owner} in #{platform.name}/#{repo.name} OK."
end
end
project.update_attributes(visibility: 'hidden') if hidden
project.import_srpm(srpm_file, platform.name)
print " Code import complete!"
else
print 'RPM Error!'
end
puts
end
say 'DONE'
end
namespace :sync do
desc "Sync all repos"
task all: :environment do
# system("bundle exec rake import:sync:run RELEASE=official/2011 PLATFORM=mandriva2011 REPOSITORY=main")
# system("bundle exec rake import:sync:run RELEASE=official/2011 PLATFORM=mandriva2011 REPOSITORY=contrib")
# system("bundle exec rake import:sync:run RELEASE=official/2011 PLATFORM=mandriva2011 REPOSITORY=non-free")
#system("bundle exec rake import:sync:run RELEASE=devel/cooker PLATFORM=cooker REPOSITORY=main")
#system("bundle exec rake import:sync:run RELEASE=devel/cooker PLATFORM=cooker REPOSITORY=contrib")
#system("bundle exec rake import:sync:run RELEASE=devel/cooker PLATFORM=cooker REPOSITORY=non-free")
system("bundle exec rake import:sync:run SOURCE=rsync://mirror.yandex.ru/fedora-epel/6/SRPMS/ DESTINATION=#{File.join(APP_CONFIG['root_path'], 'mirror.yandex.ru', 'fedora-epel', '6', 'SRPMS')} PLATFORM=server_personal REPOSITORY=main OWNER=server BRANCH=import")
system("bundle exec rake import:sync:run SOURCE=rsync://rh-mirror.redhat.com/redhat/linux/enterprise/6Server/en/os/SRPMS/ DESTINATION=#{File.join(APP_CONFIG['root_path'], 'rh-mirror.redhat.com', 'redhat', 'linux', 'enterprise', '6Server', 'en', 'os', 'SRPMS')} PLATFORM=server_personal REPOSITORY=main OWNER=server BRANCH=import")
end
task run: [:rsync, :parse]
desc "Rsync with mirror.yandex.ru"
task rsync: :environment do
release = ENV['RELEASE'] || 'official/2011'
repository = ENV['REPOSITORY'] || 'main'
source = ENV['SOURCE'] || "rsync://mirror.yandex.ru/mandriva/#{release}/SRPMS/#{repository}/"
destination = ENV['DESTINATION'] || File.join(APP_CONFIG['root_path'], 'mirror.yandex.ru', 'mandriva', release, 'SRPMS', repository)
say "START rsync projects (*.src.rpm) from '#{source}' to '#{destination}' (#{Time.now.utc})"
if system "rsync -rtv --delete --exclude='backports/*' --exclude='testing/*' #{source} #{destination}" # --include='*.src.rpm'
say 'Rsync ok!'
else
say 'Rsync failed!'
end
say "DONE (#{Time.now.utc})"
end
desc "Parse repository for changes"
task parse: :environment do
release = ENV['RELEASE'] || 'official/2011'
platform = Platform.find_by(name: ENV['PLATFORM'] || "mandriva2011")
repository = platform.repositories.find_by(name: ENV['REPOSITORY'] || 'main')
source = ENV['DESTINATION'] || File.join(APP_CONFIG['root_path'], 'mirror.yandex.ru', 'mandriva', release, 'SRPMS', repository.name, '{release,updates}')
owner = Group.find_or_create_by(uname: ENV['OWNER'] || 'import') {|g| g.name = g.uname; g.owner = User.first}
branch = ENV['BRANCH'] || "import_#{platform.name}"
say "START (#{Time.now.utc})"
Dir[File.join source, '*.src.rpm'].each do |srpm_file|
print "Processing '#{srpm_file}'... "
if name = `rpm -q --qf '[%{Name}]' -p #{srpm_file}` and $?.success? and name.present? and
version = `rpm -q --qf '[%{Version}-%{Release}]' -p #{srpm_file}` and $?.success? and version.present?
project_import = ProjectImport.find_by_name_and_platform_id(name, platform.id) || ProjectImport.by_name(name).where(platform_id: platform.id).first || ProjectImport.new(name: name, platform_id: platform.id)
if version != project_import.version.to_s and File.mtime(srpm_file) > project_import.file_mtime
unless project = project_import.project
if platform.personal? # search project through owner # used for testhat
project = Project.find_or_create_by_name_and_owner_type_and_owner_id(name, owner.class.to_s, owner.id)
print "Use project #{project.name_with_owner}. "
else # search project through repository
if project = repository.projects.find_by(name: name) || repository.projects.by_name(name).first # fallback to speedup
print "Found project #{project.name_with_owner} in #{platform.name}/#{repository.name}. "
elsif scoped = Project.where(owner_id: owner.id, owner_type: owner.class) and
project = scoped.find_by(name: name) || scoped.by_name(name).first
repository.projects << project
print "Add project #{project.name_with_owner} to #{platform.name}/#{repository.name}. "
else
description = `rpm -q --qf '[%{Description}]' -p #{srpm_file}`.scrub('')
project = Project.create!(name: name, description: description) {|p| p.owner = owner}
repository.projects << project
print "Create project #{project.name_with_owner} at #{platform.name}/#{repository.name}. "
end
end
end
project.import_srpm(srpm_file, branch)
print "New version (#{version}) for #{project.name_with_owner} successfully imported to branch #{branch}! "
project_import.project = project
# project_import.platform = platform
project_import.version = version
project_import.file_mtime = File.mtime(srpm_file)
project_import.save!
# TODO notify import.members
print 'Ok!'
else
print 'Not updated!'
end
else
print 'RPM Error!'
end
puts
end
say "DONE (#{Time.now.utc})"
end
end
end