2021-02-05 07:09:31 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-07 03:06:39 -05:00
|
|
|
namespace :gitlab do
|
|
|
|
namespace :shell do
|
2020-01-23 01:08:32 -05:00
|
|
|
desc "GitLab | Shell | Install or upgrade gitlab-shell"
|
2018-01-24 03:12:33 -05:00
|
|
|
task :install, [:repo] => :gitlab_environment do |t, args|
|
2014-04-05 15:31:07 -04:00
|
|
|
warn_user_is_not_gitlab
|
|
|
|
|
2014-11-05 11:14:22 -05:00
|
|
|
default_version = Gitlab::Shell.version_required
|
2017-04-10 19:15:48 -04:00
|
|
|
args.with_defaults(repo: 'https://gitlab.com/gitlab-org/gitlab-shell.git')
|
2014-04-05 15:31:07 -04:00
|
|
|
|
2014-10-10 05:06:08 -04:00
|
|
|
gitlab_url = Gitlab.config.gitlab.url
|
2014-04-05 15:31:07 -04:00
|
|
|
# gitlab-shell requires a / at the end of the url
|
2014-10-18 16:36:00 -04:00
|
|
|
gitlab_url += '/' unless gitlab_url.end_with?('/')
|
2014-05-02 06:46:32 -04:00
|
|
|
target_dir = Gitlab.config.gitlab_shell.path
|
2014-04-05 15:31:07 -04:00
|
|
|
|
2020-05-14 11:08:14 -04:00
|
|
|
checkout_or_clone_version(version: default_version, repo: args.repo, target_dir: target_dir, clone_opts: %w[--depth 1])
|
2014-04-05 15:31:07 -04:00
|
|
|
|
|
|
|
# Make sure we're on the right tag
|
|
|
|
Dir.chdir(target_dir) do
|
|
|
|
config = {
|
2016-09-28 06:45:46 -04:00
|
|
|
user: Gitlab.config.gitlab.user,
|
2014-04-05 15:31:07 -04:00
|
|
|
gitlab_url: gitlab_url,
|
2016-09-28 06:45:46 -04:00
|
|
|
auth_file: File.join(user_home, ".ssh", "authorized_keys"),
|
2014-11-18 10:14:36 -05:00
|
|
|
log_level: "INFO",
|
2014-04-05 15:31:07 -04:00
|
|
|
audit_usernames: false
|
2014-05-02 06:46:32 -04:00
|
|
|
}.stringify_keys
|
2014-04-05 15:31:07 -04:00
|
|
|
|
|
|
|
# Generate config.yml based on existing gitlab settings
|
2022-08-17 05:11:44 -04:00
|
|
|
File.open("config.yml", "w+") { |f| f.puts config.to_yaml }
|
2014-04-05 15:31:07 -04:00
|
|
|
|
2017-05-02 11:44:40 -04:00
|
|
|
[
|
|
|
|
%w(bin/install) + repository_storage_paths_args,
|
2019-10-21 11:05:58 -04:00
|
|
|
%w(make build)
|
2017-05-02 11:44:40 -04:00
|
|
|
].each do |cmd|
|
|
|
|
unless Kernel.system(*cmd)
|
|
|
|
raise "command failed: #{cmd.join(' ')}"
|
|
|
|
end
|
|
|
|
end
|
2014-04-05 15:31:07 -04:00
|
|
|
end
|
|
|
|
|
2016-09-29 12:46:54 -04:00
|
|
|
Gitlab::Shell.ensure_secret_token!
|
2014-04-05 15:31:07 -04:00
|
|
|
end
|
|
|
|
|
2020-01-23 01:08:32 -05:00
|
|
|
desc "GitLab | Shell | Setup gitlab-shell"
|
2018-01-24 03:12:33 -05:00
|
|
|
task setup: :gitlab_environment do
|
2013-02-07 03:06:39 -05:00
|
|
|
setup
|
|
|
|
end
|
2013-02-09 05:30:49 -05:00
|
|
|
|
2020-01-23 01:08:32 -05:00
|
|
|
desc "GitLab | Shell | Build missing projects"
|
2018-01-24 03:12:33 -05:00
|
|
|
task build_missing_projects: :gitlab_environment do
|
2013-02-09 05:30:49 -05:00
|
|
|
Project.find_each(batch_size: 1000) do |project|
|
2014-11-05 11:51:08 -05:00
|
|
|
path_to_repo = project.repository.path_to_repo
|
2016-08-09 17:23:25 -04:00
|
|
|
if File.exist?(path_to_repo)
|
2013-02-09 05:30:49 -05:00
|
|
|
print '-'
|
|
|
|
else
|
2018-03-14 04:56:22 -04:00
|
|
|
if Gitlab::Shell.new.create_repository(project.repository_storage,
|
2017-07-21 20:37:22 -04:00
|
|
|
project.disk_path)
|
2013-02-09 05:30:49 -05:00
|
|
|
print '.'
|
|
|
|
else
|
|
|
|
print 'F'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-02-07 03:06:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def setup
|
|
|
|
warn_user_is_not_gitlab
|
|
|
|
|
2018-09-19 16:50:31 -04:00
|
|
|
ensure_write_to_authorized_keys_is_enabled
|
|
|
|
|
2013-05-29 15:19:23 -04:00
|
|
|
unless ENV['force'] == 'yes'
|
2018-09-19 16:50:31 -04:00
|
|
|
puts "This task will now rebuild the authorized_keys file."
|
|
|
|
puts "You will lose any data stored in the authorized_keys file."
|
2013-05-29 15:19:23 -04:00
|
|
|
ask_to_continue
|
|
|
|
puts ""
|
|
|
|
end
|
2013-02-07 03:06:39 -05:00
|
|
|
|
2020-03-12 08:09:17 -04:00
|
|
|
authorized_keys = Gitlab::AuthorizedKeys.new
|
|
|
|
|
|
|
|
authorized_keys.clear
|
2013-02-07 03:06:39 -05:00
|
|
|
|
2019-03-19 07:16:21 -04:00
|
|
|
Key.find_in_batches(batch_size: 1000) do |keys|
|
2020-03-12 08:09:17 -04:00
|
|
|
unless authorized_keys.batch_add_keys(keys)
|
2019-03-19 07:16:21 -04:00
|
|
|
puts "Failed to add keys...".color(:red)
|
|
|
|
exit 1
|
2013-02-07 03:06:39 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue Gitlab::TaskAbortedByUserError
|
2016-06-01 18:37:15 -04:00
|
|
|
puts "Quitting...".color(:red)
|
2013-02-07 03:06:39 -05:00
|
|
|
exit 1
|
|
|
|
end
|
2018-09-19 16:50:31 -04:00
|
|
|
|
|
|
|
def ensure_write_to_authorized_keys_is_enabled
|
2020-03-12 08:09:17 -04:00
|
|
|
return if Gitlab::CurrentSettings.authorized_keys_enabled?
|
2018-09-19 16:50:31 -04:00
|
|
|
|
|
|
|
puts authorized_keys_is_disabled_warning
|
|
|
|
|
|
|
|
unless ENV['force'] == 'yes'
|
|
|
|
puts 'Do you want to permanently enable the "Write to authorized_keys file" setting now?'
|
|
|
|
ask_to_continue
|
|
|
|
end
|
|
|
|
|
|
|
|
puts 'Enabling the "Write to authorized_keys file" setting...'
|
2020-03-12 08:09:17 -04:00
|
|
|
Gitlab::CurrentSettings.update!(authorized_keys_enabled: true)
|
2018-09-19 16:50:31 -04:00
|
|
|
|
|
|
|
puts 'Successfully enabled "Write to authorized_keys file"!'
|
|
|
|
puts ''
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorized_keys_is_disabled_warning
|
|
|
|
<<-MSG.strip_heredoc
|
|
|
|
WARNING
|
|
|
|
|
|
|
|
The "Write to authorized_keys file" setting is disabled, which prevents
|
|
|
|
the file from being rebuilt!
|
|
|
|
|
|
|
|
It should be enabled for most GitLab installations. Large installations
|
|
|
|
may wish to disable it as part of speeding up SSH operations.
|
|
|
|
|
|
|
|
See https://docs.gitlab.com/ee/administration/operations/fast_ssh_key_lookup.html
|
|
|
|
|
|
|
|
If you did not intentionally disable this option in Admin Area > Settings,
|
|
|
|
then you may have been affected by the 9.3.0 bug in which the new setting
|
|
|
|
was disabled by default.
|
|
|
|
|
2019-09-18 10:02:45 -04:00
|
|
|
https://gitlab.com/gitlab-org/gitlab/issues/2738
|
2018-09-19 16:50:31 -04:00
|
|
|
|
|
|
|
It was reverted in 9.3.1 and fixed in 9.3.3, however, if Settings were
|
|
|
|
saved while the setting was unchecked, then it is still disabled.
|
|
|
|
MSG
|
|
|
|
end
|
2013-02-07 03:06:39 -05:00
|
|
|
end
|