gitlab数据同步一致性任务

This commit is contained in:
siteen 2016-12-13 17:21:58 +08:00
parent a2b7778de1
commit 413efa8d02
4 changed files with 54 additions and 0 deletions

View File

@ -0,0 +1,3 @@
class DataException < ActiveRecord::Base
attr_accessible :container_id, :container_type, :message
end

View File

@ -0,0 +1,11 @@
class CreateDataExceptions < ActiveRecord::Migration
def change
create_table :data_exceptions do |t|
t.string :message
t.integer :container_id
t.string :container_type
t.timestamps
end
end
end

View File

@ -0,0 +1,35 @@
namespace :gitlab do
desc "make sure trustie'data consistent with gitlab's data"
task :sync_date => :environment do
projects = Project.where("gpid is not null")
s = Trustie::Gitlab::Sync.new
g = Gitlab.client
begin
projects = Project.where(:status => 1)
projects.each do |project|
# sync members and roles
if project.members.count != g.team_members(project.gpid).count
DataException.create(:message => "project's members is not true", :container_id => project.id, :container_type => "Project")
s.only_members(project)
end
end
users = User.where("gpid is not null and status =?", 1)
users.each do |user|
# sync username
g.edit_user(user.gid, :username => user.login) if user.login != g.user(user.gid).try(:username)
# sync email
g.edit_user(user.gid, :email => user.mail) if user.mail != g.user(user.gid).try(:email)
# sync password
options = {:encrypted_password=> user.hashed_password, :password_salt=> user.salt}
g.put("/users/ext/#{uid}", :body => options)
end
rescue Exception => e
DataException.create(:message => e.message)
puts e
end
end
end

View File

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe DataException, :type => :model do
pending "add some examples to (or delete) #{__FILE__}"
end