Commit ecae936d344b7d29f1898f81ac283d444c086f0d

Authored by Dmitriy Zaporozhets
2 parents 66a484c9 090ed21e

Merge branch 'batch_key_import' into 'master'

Batch Key Import
@@ -21,7 +21,7 @@ v 6.7.0 @@ -21,7 +21,7 @@ v 6.7.0
21 - Reuse the GitLab LDAP connection within each request 21 - Reuse the GitLab LDAP connection within each request
22 - Changed markdown new line behaviour to conform to markdown standards 22 - Changed markdown new line behaviour to conform to markdown standards
23 - Fix global search 23 - Fix global search
24 - 24 + - Faster authorized_keys rebuilding in `rake gitlab:shell:setup` (requires gitlab-shell 1.8.5)
25 25
26 v 6.6.2 26 v 6.6.2
27 - Fix 500 error on branch/tag create or remove via UI 27 - Fix 500 error on branch/tag create or remove via UI
lib/gitlab/backend/shell.rb
@@ -2,6 +2,12 @@ module Gitlab @@ -2,6 +2,12 @@ module Gitlab
2 class Shell 2 class Shell
3 class AccessDenied < StandardError; end 3 class AccessDenied < StandardError; end
4 4
  5 + class KeyAdder < Struct.new(:io)
  6 + def add_key(id, key)
  7 + io.puts("#{id}\t#{key.strip}")
  8 + end
  9 + end
  10 +
5 # Init new repository 11 # Init new repository
6 # 12 #
7 # name - project path with namespace 13 # name - project path with namespace
@@ -130,6 +136,16 @@ module Gitlab @@ -130,6 +136,16 @@ module Gitlab
130 system "#{gitlab_shell_path}/bin/gitlab-keys", "add-key", key_id, key_content 136 system "#{gitlab_shell_path}/bin/gitlab-keys", "add-key", key_id, key_content
131 end 137 end
132 138
  139 + # Batch-add keys to authorized_keys
  140 + #
  141 + # Ex.
  142 + # batch_add_keys { |adder| adder.add_key("key-42", "sha-rsa ...") }
  143 + def batch_add_keys(&block)
  144 + IO.popen(%W(#{gitlab_shell_path}/bin/gitlab-keys batch-add-keys), 'w') do |io|
  145 + block.call(KeyAdder.new(io))
  146 + end
  147 + end
  148 +
133 # Remove ssh key from gitlab shell 149 # Remove ssh key from gitlab shell
134 # 150 #
135 # Ex. 151 # Ex.
lib/tasks/gitlab/check.rake
@@ -727,7 +727,7 @@ namespace :gitlab do @@ -727,7 +727,7 @@ namespace :gitlab do
727 end 727 end
728 728
729 def check_gitlab_shell 729 def check_gitlab_shell
730 - required_version = Gitlab::VersionInfo.new(1, 8, 4) 730 + required_version = Gitlab::VersionInfo.new(1, 8, 5)
731 current_version = Gitlab::VersionInfo.parse(gitlab_shell_version) 731 current_version = Gitlab::VersionInfo.parse(gitlab_shell_version)
732 732
733 print "GitLab Shell version >= #{required_version} ? ... " 733 print "GitLab Shell version >= #{required_version} ? ... "
lib/tasks/gitlab/shell.rake
@@ -34,14 +34,18 @@ namespace :gitlab do @@ -34,14 +34,18 @@ namespace :gitlab do
34 34
35 Gitlab::Shell.new.remove_all_keys 35 Gitlab::Shell.new.remove_all_keys
36 36
37 - Key.find_each(batch_size: 1000) do |key|  
38 - if Gitlab::Shell.new.add_key(key.shell_id, key.key) 37 + Gitlab::Shell.new.batch_add_keys do |adder|
  38 + Key.find_each(batch_size: 1000) do |key|
  39 + adder.add_key(key.shell_id, key.key)
39 print '.' 40 print '.'
40 - else  
41 - print 'F'  
42 end 41 end
43 end 42 end
44 43
  44 + unless $?.success?
  45 + puts "Failed to add keys...".red
  46 + exit 1
  47 + end
  48 +
45 rescue Gitlab::TaskAbortedByUserError 49 rescue Gitlab::TaskAbortedByUserError
46 puts "Quitting...".red 50 puts "Quitting...".red
47 exit 1 51 exit 1