Commit 8d6e72f3 authored by Michael Kozono's avatar Michael Kozono

Move batch-add-logic to background migration

Do the work synchronously to avoid multiple workers attempting to add batches of keys at the same time.

Also, make the delete portion wait until after adding is done.
parent 12d0a64d
...@@ -4,19 +4,6 @@ class GitlabShellWorker ...@@ -4,19 +4,6 @@ class GitlabShellWorker
include DedicatedSidekiqQueue include DedicatedSidekiqQueue
def perform(action, *arg) def perform(action, *arg)
if action.to_s == 'batch_add_keys_in_db_starting_from'
batch_add_keys_in_db_starting_from(arg.first)
else
gitlab_shell.send(action, *arg) gitlab_shell.send(action, *arg)
end end
end
# Not added to Gitlab::Shell because I don't expect this to be used again
def batch_add_keys_in_db_starting_from(start_id)
gitlab_shell.batch_add_keys do |adder|
Key.find_each(start: start_id, batch_size: 1000) do |key|
adder.add_key(key.shell_id, key.key)
end
end
end
end end
...@@ -3,6 +3,10 @@ module Gitlab ...@@ -3,6 +3,10 @@ module Gitlab
class UpdateAuthorizedKeysFileSince class UpdateAuthorizedKeysFileSince
class Key < ActiveRecord::Base class Key < ActiveRecord::Base
self.table_name = 'keys' self.table_name = 'keys'
def shell_id
"key-#{id}"
end
end end
def perform(cutoff_datetime) def perform(cutoff_datetime)
...@@ -14,13 +18,26 @@ module Gitlab ...@@ -14,13 +18,26 @@ module Gitlab
def add_keys_since(cutoff_datetime) def add_keys_since(cutoff_datetime)
start_key = Key.select(:id).where("created_at >= ?", cutoff_datetime).take start_key = Key.select(:id).where("created_at >= ?", cutoff_datetime).take
if start_key if start_key
GitlabShellWorker.perform_async(:batch_add_keys_in_db_starting_from, start_key.id) batch_add_keys_in_db_starting_from(start_key.id)
end end
end end
def remove_keys_not_found_in_db def remove_keys_not_found_in_db
GitlabShellWorker.perform_async(:remove_keys_not_found_in_db) GitlabShellWorker.perform_async(:remove_keys_not_found_in_db)
end end
# Not added to Gitlab::Shell because I don't expect this to be used again
def batch_add_keys_in_db_starting_from(start_id)
gitlab_shell.batch_add_keys do |adder|
Key.find_each(start: start_id, batch_size: 1000) do |key|
adder.add_key(key.shell_id, key.key)
end
end
end
def gitlab_shell
@gitlab_shell ||= Gitlab::Shell.new
end
end end
end end
end end
require 'spec_helper' require 'spec_helper'
describe Gitlab::BackgroundMigration::UpdateAuthorizedKeysFileSince do describe Gitlab::BackgroundMigration::UpdateAuthorizedKeysFileSince do
let(:background_migration) { described_class.new }
describe '#perform' do describe '#perform' do
let!(:cutoff_datetime) { DateTime.now } let!(:cutoff_datetime) { DateTime.now }
subject { described_class.new.perform(cutoff_datetime) } subject { background_migration.perform(cutoff_datetime) }
context 'when an SSH key was created after the cutoff datetime' do context 'when an SSH key was created after the cutoff datetime' do
before do before do
...@@ -19,9 +21,8 @@ describe Gitlab::BackgroundMigration::UpdateAuthorizedKeysFileSince do ...@@ -19,9 +21,8 @@ describe Gitlab::BackgroundMigration::UpdateAuthorizedKeysFileSince do
@key = create(:key) @key = create(:key)
end end
it 'queues a batch_add_keys_from call to GitlabShellWorker, including the start key ID' do it 'calls batch_add_keys_in_db_starting_from with the start key ID' do
expect(GitlabShellWorker).to receive(:perform_async).with(:batch_add_keys_in_db_starting_from, @key.id) expect(background_migration).to receive(:batch_add_keys_in_db_starting_from).with(@key.id)
allow(GitlabShellWorker).to receive(:perform_async).with(:remove_keys_not_found_in_db)
subject subject
end end
end end
...@@ -35,7 +36,7 @@ describe Gitlab::BackgroundMigration::UpdateAuthorizedKeysFileSince do ...@@ -35,7 +36,7 @@ describe Gitlab::BackgroundMigration::UpdateAuthorizedKeysFileSince do
describe '#add_keys_since' do describe '#add_keys_since' do
let!(:cutoff_datetime) { DateTime.now } let!(:cutoff_datetime) { DateTime.now }
subject { described_class.new.add_keys_since(cutoff_datetime) } subject { background_migration.add_keys_since(cutoff_datetime) }
before do before do
Timecop.freeze Timecop.freeze
...@@ -50,15 +51,15 @@ describe Gitlab::BackgroundMigration::UpdateAuthorizedKeysFileSince do ...@@ -50,15 +51,15 @@ describe Gitlab::BackgroundMigration::UpdateAuthorizedKeysFileSince do
@key = create(:key) @key = create(:key)
end end
it 'queues a batch_add_keys_from call to GitlabShellWorker, including the start key ID' do it 'calls batch_add_keys_in_db_starting_from with the start key ID' do
expect(GitlabShellWorker).to receive(:perform_async).with(:batch_add_keys_in_db_starting_from, @key.id) expect(background_migration).to receive(:batch_add_keys_in_db_starting_from).with(@key.id)
subject subject
end end
end end
context 'when an SSH key was not created after the cutoff datetime' do context 'when an SSH key was not created after the cutoff datetime' do
it 'does not use GitlabShellWorker' do it 'does not call batch_add_keys_in_db_starting_from' do
expect(GitlabShellWorker).not_to receive(:perform_async) expect(background_migration).not_to receive(:batch_add_keys_in_db_starting_from)
subject subject
end end
end end
...@@ -67,7 +68,32 @@ describe Gitlab::BackgroundMigration::UpdateAuthorizedKeysFileSince do ...@@ -67,7 +68,32 @@ describe Gitlab::BackgroundMigration::UpdateAuthorizedKeysFileSince do
describe '#remove_keys_not_found_in_db' do describe '#remove_keys_not_found_in_db' do
it 'queues a rm_keys_not_in_db call to GitlabShellWorker' do it 'queues a rm_keys_not_in_db call to GitlabShellWorker' do
expect(GitlabShellWorker).to receive(:perform_async).with(:remove_keys_not_found_in_db) expect(GitlabShellWorker).to receive(:perform_async).with(:remove_keys_not_found_in_db)
described_class.new.remove_keys_not_found_in_db background_migration.remove_keys_not_found_in_db
end
end
describe '#batch_add_keys_in_db_starting_from' do
context 'when there are many keys in the DB' do
before do
@keys = []
10.times do
@keys << create(:key)
end
end
it 'adds all the keys in the DB, starting from the given ID, to the authorized_keys file' do
Gitlab::Shell.new.remove_all_keys
background_migration.batch_add_keys_in_db_starting_from(@keys[3].id)
file = File.read(Rails.root.join('tmp/tests/.ssh/authorized_keys'))
expect(file.scan(/ssh-rsa/).count).to eq(7)
expect(file).to_not include(Gitlab::Shell.strip_key(@keys[0].key))
expect(file).to_not include(Gitlab::Shell.strip_key(@keys[2].key))
expect(file).to include(Gitlab::Shell.strip_key(@keys[3].key))
expect(file).to include(Gitlab::Shell.strip_key(@keys[9].key))
end
end end
end end
end end
...@@ -9,29 +9,4 @@ describe GitlabShellWorker do ...@@ -9,29 +9,4 @@ describe GitlabShellWorker do
worker.perform(:add_key, 'foo', 'bar') worker.perform(:add_key, 'foo', 'bar')
end end
end end
describe '#perform with batch_add_keys_in_db_starting_from' do
context 'when there are many keys in the DB' do
before do
@keys = []
10.times do
@keys << create(:key)
end
end
it 'adds all the keys in the DB, starting from the given ID, to the authorized_keys file' do
Gitlab::Shell.new.remove_all_keys
worker.perform(:batch_add_keys_in_db_starting_from, @keys[3].id)
file = File.read(Rails.root.join('tmp/tests/.ssh/authorized_keys'))
expect(file.scan(/ssh-rsa/).count).to eq(7)
expect(file).to_not include(Gitlab::Shell.strip_key(@keys[0].key))
expect(file).to_not include(Gitlab::Shell.strip_key(@keys[2].key))
expect(file).to include(Gitlab::Shell.strip_key(@keys[3].key))
expect(file).to include(Gitlab::Shell.strip_key(@keys[9].key))
end
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment