2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-06-19 06:17:54 -04:00
|
|
|
# Gitaly note: SSH key operations are not part of Gitaly so will never be migrated.
|
2017-07-13 11:58:45 -04:00
|
|
|
|
2016-06-24 15:06:46 -04:00
|
|
|
require 'securerandom'
|
|
|
|
|
2012-05-26 06:37:49 -04:00
|
|
|
module Gitlab
|
2013-02-04 08:07:56 -05:00
|
|
|
class Shell
|
2017-07-03 14:28:29 -04:00
|
|
|
GITLAB_SHELL_ENV_VARS = %w(GIT_TERMINAL_PROMPT).freeze
|
|
|
|
|
2017-03-01 06:00:37 -05:00
|
|
|
Error = Class.new(StandardError)
|
2012-09-07 01:16:29 -04:00
|
|
|
|
2014-11-05 11:14:22 -05:00
|
|
|
class << self
|
2016-09-29 12:46:54 -04:00
|
|
|
def secret_token
|
|
|
|
@secret_token ||= begin
|
|
|
|
File.read(Gitlab.config.gitlab_shell.secret_file).chomp
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_secret_token!
|
|
|
|
return if File.exist?(File.join(Gitlab.config.gitlab_shell.path, '.gitlab_shell_secret'))
|
|
|
|
|
|
|
|
generate_and_link_secret_token
|
|
|
|
end
|
|
|
|
|
2014-11-05 11:14:22 -05:00
|
|
|
def version_required
|
2017-06-21 09:48:12 -04:00
|
|
|
@version_required ||= File.read(Rails.root
|
|
|
|
.join('GITLAB_SHELL_VERSION')).strip
|
2014-11-05 11:14:22 -05:00
|
|
|
end
|
2016-09-16 05:43:05 -04:00
|
|
|
|
2016-09-29 12:46:54 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
# Create (if necessary) and link the secret token file
|
|
|
|
def generate_and_link_secret_token
|
|
|
|
secret_file = Gitlab.config.gitlab_shell.secret_file
|
|
|
|
shell_path = Gitlab.config.gitlab_shell.path
|
|
|
|
|
|
|
|
unless File.size?(secret_file)
|
|
|
|
# Generate a new token of 16 random hexadecimal characters and store it in secret_file.
|
2016-10-13 04:04:58 -04:00
|
|
|
@secret_token = SecureRandom.hex(16)
|
|
|
|
File.write(secret_file, @secret_token)
|
2016-09-29 12:46:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
link_path = File.join(shell_path, '.gitlab_shell_secret')
|
|
|
|
if File.exist?(shell_path) && !File.exist?(link_path)
|
|
|
|
FileUtils.symlink(secret_file, link_path)
|
|
|
|
end
|
|
|
|
end
|
2014-11-05 11:14:22 -05:00
|
|
|
end
|
|
|
|
|
2019-01-18 15:54:26 -05:00
|
|
|
# Convenience methods for initializing a new repository with a Project model.
|
2019-01-17 16:50:18 -05:00
|
|
|
def create_project_repository(project)
|
|
|
|
create_repository(project.repository_storage, project.disk_path, project.full_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_wiki_repository(project)
|
|
|
|
create_repository(project.repository_storage, project.wiki.disk_path, project.wiki.full_path)
|
|
|
|
end
|
|
|
|
|
2013-02-04 08:07:56 -05:00
|
|
|
# Init new repository
|
2013-01-28 14:02:10 -05:00
|
|
|
#
|
2018-04-13 06:57:19 -04:00
|
|
|
# storage - the shard key
|
2019-01-17 14:17:14 -05:00
|
|
|
# disk_path - project disk path
|
2019-01-24 14:37:26 -05:00
|
|
|
# gl_project_path - project name
|
2013-01-28 10:39:02 -05:00
|
|
|
#
|
|
|
|
# Ex.
|
2018-12-23 02:34:35 -05:00
|
|
|
# create_repository("default", "path/to/gitlab-ci", "gitlab/gitlab-ci")
|
2013-01-28 10:39:02 -05:00
|
|
|
#
|
2019-01-24 14:37:26 -05:00
|
|
|
def create_repository(storage, disk_path, gl_project_path)
|
2019-01-17 14:17:14 -05:00
|
|
|
relative_path = disk_path.dup
|
2017-09-28 13:07:22 -04:00
|
|
|
relative_path << '.git' unless relative_path.end_with?('.git')
|
|
|
|
|
2019-01-17 14:21:29 -05:00
|
|
|
# During creation of a repository, gl_repository may not be known
|
|
|
|
# because that depends on a yet-to-be assigned project ID in the
|
|
|
|
# database (e.g. project-1234), so for now it is blank.
|
2019-01-24 14:37:26 -05:00
|
|
|
repository = Gitlab::Git::Repository.new(storage, relative_path, '', gl_project_path)
|
2018-07-06 08:44:54 -04:00
|
|
|
wrapped_gitaly_errors { repository.gitaly_repository_client.create_repository }
|
|
|
|
|
|
|
|
true
|
2018-03-14 05:33:41 -04:00
|
|
|
rescue => err # Once the Rugged codes gets removes this can be improved
|
2019-07-10 15:26:47 -04:00
|
|
|
Rails.logger.error("Failed to add repository #{storage}/#{disk_path}: #{err}") # rubocop:disable Gitlab/RailsLogger
|
2017-08-14 07:29:47 -04:00
|
|
|
false
|
2012-11-21 00:54:05 -05:00
|
|
|
end
|
|
|
|
|
2019-01-17 03:35:40 -05:00
|
|
|
def import_wiki_repository(project, wiki_formatter)
|
|
|
|
import_repository(project.repository_storage, wiki_formatter.disk_path, wiki_formatter.import_url, project.wiki.full_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_project_repository(project)
|
|
|
|
import_repository(project.repository_storage, project.disk_path, project.import_url, project.full_path)
|
|
|
|
end
|
|
|
|
|
2013-02-11 12:41:02 -05:00
|
|
|
# Import repository
|
|
|
|
#
|
2018-03-26 14:21:49 -04:00
|
|
|
# storage - project's storage name
|
2017-12-11 12:52:07 -05:00
|
|
|
# name - project disk path
|
|
|
|
# url - URL to import from
|
2013-02-11 12:41:02 -05:00
|
|
|
#
|
|
|
|
# Ex.
|
2018-03-26 14:21:49 -04:00
|
|
|
# import_repository("nfs-file06", "gitlab/gitlab-ci", "https://gitlab.com/gitlab-org/gitlab-test.git")
|
2013-02-11 12:41:02 -05:00
|
|
|
#
|
2019-01-24 14:37:26 -05:00
|
|
|
def import_repository(storage, name, url, gl_project_path)
|
2018-01-04 12:05:49 -05:00
|
|
|
if url.start_with?('.', '/')
|
2018-01-04 10:34:37 -05:00
|
|
|
raise Error.new("don't use disk paths with import_repository: #{url.inspect}")
|
|
|
|
end
|
|
|
|
|
2018-06-05 11:58:28 -04:00
|
|
|
relative_path = "#{name}.git"
|
2019-01-24 14:37:26 -05:00
|
|
|
cmd = GitalyGitlabProjects.new(storage, relative_path, gl_project_path)
|
2017-12-11 12:52:07 -05:00
|
|
|
|
2018-06-05 11:58:28 -04:00
|
|
|
success = cmd.import_project(url, git_timeout)
|
2017-12-11 12:52:07 -05:00
|
|
|
raise Error, cmd.output unless success
|
|
|
|
|
|
|
|
success
|
2013-02-11 12:41:02 -05:00
|
|
|
end
|
|
|
|
|
2018-01-05 07:29:04 -05:00
|
|
|
# Move repository reroutes to mv_directory which is an alias for
|
|
|
|
# mv_namespace. Given the underlying implementation is a move action,
|
|
|
|
# indescriminate of what the folders might be.
|
|
|
|
#
|
2016-06-22 17:04:51 -04:00
|
|
|
# storage - project's storage path
|
2017-12-11 12:52:07 -05:00
|
|
|
# path - project disk path
|
|
|
|
# new_path - new project disk path
|
2013-03-12 06:37:53 -04:00
|
|
|
#
|
|
|
|
# Ex.
|
2016-06-22 17:04:51 -04:00
|
|
|
# mv_repository("/path/to/storage", "gitlab/gitlab-ci", "randx/gitlab-ci-new")
|
|
|
|
def mv_repository(storage, path, new_path)
|
2018-01-05 07:29:04 -05:00
|
|
|
return false if path.empty? || new_path.empty?
|
|
|
|
|
|
|
|
!!mv_directory(storage, "#{path}.git", "#{new_path}.git")
|
2013-03-12 06:37:53 -04:00
|
|
|
end
|
|
|
|
|
2017-11-30 16:34:31 -05:00
|
|
|
# Fork repository to new path
|
2019-01-17 01:40:23 -05:00
|
|
|
# source_project - forked-from Project
|
|
|
|
# target_project - forked-to Project
|
|
|
|
def fork_repository(source_project, target_project)
|
|
|
|
forked_from_relative_path = "#{source_project.disk_path}.git"
|
|
|
|
fork_args = [target_project.repository_storage, "#{target_project.disk_path}.git", target_project.full_path]
|
|
|
|
|
|
|
|
GitalyGitlabProjects.new(source_project.repository_storage, forked_from_relative_path, source_project.full_path).fork_repository(*fork_args)
|
2013-03-19 11:37:50 -04:00
|
|
|
end
|
|
|
|
|
2018-01-05 07:29:04 -05:00
|
|
|
# Removes a repository from file system, using rm_diretory which is an alias
|
|
|
|
# for rm_namespace. Given the underlying implementation removes the name
|
|
|
|
# passed as second argument on the passed storage.
|
2013-01-28 10:22:45 -05:00
|
|
|
#
|
2016-06-22 17:04:51 -04:00
|
|
|
# storage - project's storage path
|
2017-12-11 12:52:07 -05:00
|
|
|
# name - project disk path
|
2013-01-28 10:22:45 -05:00
|
|
|
#
|
|
|
|
# Ex.
|
2016-06-22 17:04:51 -04:00
|
|
|
# remove_repository("/path/to/storage", "gitlab/gitlab-ci")
|
|
|
|
def remove_repository(storage, name)
|
2018-01-05 07:29:04 -05:00
|
|
|
return false if name.empty?
|
|
|
|
|
|
|
|
!!rm_directory(storage, "#{name}.git")
|
|
|
|
rescue ArgumentError => e
|
2019-07-10 15:26:47 -04:00
|
|
|
Rails.logger.warn("Repository does not exist: #{e} at: #{name}.git") # rubocop:disable Gitlab/RailsLogger
|
2018-01-05 07:29:04 -05:00
|
|
|
false
|
2012-03-05 17:26:40 -05:00
|
|
|
end
|
|
|
|
|
2019-03-19 07:16:21 -04:00
|
|
|
# Add new key to authorized_keys
|
2013-02-04 07:28:10 -05:00
|
|
|
#
|
2013-02-04 08:07:56 -05:00
|
|
|
# Ex.
|
2013-02-05 04:12:15 -05:00
|
|
|
# add_key("key-42", "sha-rsa ...")
|
2013-02-04 08:07:56 -05:00
|
|
|
#
|
2013-02-05 04:12:15 -05:00
|
|
|
def add_key(key_id, key_content)
|
2017-05-30 19:24:45 -04:00
|
|
|
return unless self.authorized_keys_enabled?
|
|
|
|
|
2019-03-19 07:16:21 -04:00
|
|
|
if shell_out_for_gitlab_keys?
|
|
|
|
gitlab_shell_fast_execute([
|
|
|
|
gitlab_shell_keys_path,
|
|
|
|
'add-key',
|
|
|
|
key_id,
|
|
|
|
strip_key(key_content)
|
|
|
|
])
|
|
|
|
else
|
|
|
|
gitlab_authorized_keys.add_key(key_id, key_content)
|
|
|
|
end
|
2013-02-04 08:07:56 -05:00
|
|
|
end
|
|
|
|
|
2014-03-13 13:32:30 -04:00
|
|
|
# Batch-add keys to authorized_keys
|
|
|
|
#
|
|
|
|
# Ex.
|
2019-03-19 07:16:21 -04:00
|
|
|
# batch_add_keys(Key.all)
|
|
|
|
def batch_add_keys(keys)
|
2017-05-30 19:24:45 -04:00
|
|
|
return unless self.authorized_keys_enabled?
|
|
|
|
|
2019-03-19 07:16:21 -04:00
|
|
|
if shell_out_for_gitlab_keys?
|
|
|
|
begin
|
|
|
|
IO.popen("#{gitlab_shell_keys_path} batch-add-keys", 'w') do |io|
|
|
|
|
add_keys_to_io(keys, io)
|
|
|
|
end
|
|
|
|
|
|
|
|
$?.success?
|
|
|
|
rescue Error
|
|
|
|
false
|
|
|
|
end
|
|
|
|
else
|
|
|
|
gitlab_authorized_keys.batch_add_keys(keys)
|
2014-03-13 13:32:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-19 07:16:21 -04:00
|
|
|
# Remove ssh key from authorized_keys
|
2013-01-28 10:39:02 -05:00
|
|
|
#
|
|
|
|
# Ex.
|
2019-03-19 07:16:21 -04:00
|
|
|
# remove_key("key-342")
|
2013-01-28 10:39:02 -05:00
|
|
|
#
|
2019-03-19 07:16:21 -04:00
|
|
|
def remove_key(id, _ = nil)
|
2017-05-30 19:24:45 -04:00
|
|
|
return unless self.authorized_keys_enabled?
|
|
|
|
|
2019-03-19 07:16:21 -04:00
|
|
|
if shell_out_for_gitlab_keys?
|
|
|
|
gitlab_shell_fast_execute([gitlab_shell_keys_path, 'rm-key', id])
|
|
|
|
else
|
|
|
|
gitlab_authorized_keys.rm_key(id)
|
|
|
|
end
|
2013-01-28 10:39:02 -05:00
|
|
|
end
|
|
|
|
|
2013-07-18 06:55:01 -04:00
|
|
|
# Remove all ssh keys from gitlab shell
|
|
|
|
#
|
|
|
|
# Ex.
|
2013-07-29 06:46:00 -04:00
|
|
|
# remove_all_keys
|
2013-07-18 06:55:01 -04:00
|
|
|
#
|
|
|
|
def remove_all_keys
|
2017-05-30 19:24:45 -04:00
|
|
|
return unless self.authorized_keys_enabled?
|
|
|
|
|
2019-03-19 07:16:21 -04:00
|
|
|
if shell_out_for_gitlab_keys?
|
|
|
|
gitlab_shell_fast_execute([gitlab_shell_keys_path, 'clear'])
|
|
|
|
else
|
|
|
|
gitlab_authorized_keys.clear
|
|
|
|
end
|
2013-07-18 06:55:01 -04:00
|
|
|
end
|
|
|
|
|
2017-06-26 17:40:08 -04:00
|
|
|
# Remove ssh keys from gitlab shell that are not in the DB
|
|
|
|
#
|
|
|
|
# Ex.
|
|
|
|
# remove_keys_not_found_in_db
|
|
|
|
#
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-06-26 17:40:08 -04:00
|
|
|
def remove_keys_not_found_in_db
|
|
|
|
return unless self.authorized_keys_enabled?
|
|
|
|
|
2019-07-10 15:26:47 -04:00
|
|
|
Rails.logger.info("Removing keys not found in DB") # rubocop:disable Gitlab/RailsLogger
|
2017-06-26 17:40:08 -04:00
|
|
|
|
|
|
|
batch_read_key_ids do |ids_in_file|
|
|
|
|
ids_in_file.uniq!
|
|
|
|
keys_in_db = Key.where(id: ids_in_file)
|
|
|
|
|
|
|
|
next unless ids_in_file.size > keys_in_db.count # optimization
|
|
|
|
|
|
|
|
ids_to_remove = ids_in_file - keys_in_db.pluck(:id)
|
|
|
|
ids_to_remove.each do |id|
|
2019-07-10 15:26:47 -04:00
|
|
|
Rails.logger.info("Removing key-#{id} not found in DB") # rubocop:disable Gitlab/RailsLogger
|
2017-06-26 17:40:08 -04:00
|
|
|
remove_key("key-#{id}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-06-26 17:40:08 -04:00
|
|
|
|
2013-03-21 16:11:08 -04:00
|
|
|
# Add empty directory for storing repositories
|
|
|
|
#
|
|
|
|
# Ex.
|
2018-04-13 06:57:19 -04:00
|
|
|
# add_namespace("default", "gitlab")
|
2013-03-21 16:11:08 -04:00
|
|
|
#
|
2016-06-22 17:04:51 -04:00
|
|
|
def add_namespace(storage, name)
|
2019-02-22 09:51:27 -05:00
|
|
|
# https://gitlab.com/gitlab-org/gitlab-ce/issues/58012
|
|
|
|
Gitlab::GitalyClient.allow_n_plus_1_calls do
|
|
|
|
Gitlab::GitalyClient::NamespaceService.new(storage).add(name)
|
|
|
|
end
|
2017-09-12 06:26:59 -04:00
|
|
|
rescue GRPC::InvalidArgument => e
|
|
|
|
raise ArgumentError, e.message
|
2013-03-21 16:11:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Remove directory from repositories storage
|
|
|
|
# Every repository inside this directory will be removed too
|
|
|
|
#
|
|
|
|
# Ex.
|
2018-04-13 06:57:19 -04:00
|
|
|
# rm_namespace("default", "gitlab")
|
2013-03-21 16:11:08 -04:00
|
|
|
#
|
2016-06-22 17:04:51 -04:00
|
|
|
def rm_namespace(storage, name)
|
2018-05-02 05:22:15 -04:00
|
|
|
Gitlab::GitalyClient::NamespaceService.new(storage).remove(name)
|
2017-09-12 06:26:59 -04:00
|
|
|
rescue GRPC::InvalidArgument => e
|
|
|
|
raise ArgumentError, e.message
|
2013-03-21 16:11:08 -04:00
|
|
|
end
|
2018-01-05 07:29:04 -05:00
|
|
|
alias_method :rm_directory, :rm_namespace
|
2013-03-21 16:11:08 -04:00
|
|
|
|
|
|
|
# Move namespace directory inside repositories storage
|
|
|
|
#
|
|
|
|
# Ex.
|
2016-06-22 17:04:51 -04:00
|
|
|
# mv_namespace("/path/to/storage", "gitlab", "gitlabhq")
|
2013-03-21 16:11:08 -04:00
|
|
|
#
|
2016-06-22 17:04:51 -04:00
|
|
|
def mv_namespace(storage, old_name, new_name)
|
2018-05-02 05:22:15 -04:00
|
|
|
Gitlab::GitalyClient::NamespaceService.new(storage).rename(old_name, new_name)
|
2019-01-03 16:03:35 -05:00
|
|
|
rescue GRPC::InvalidArgument => e
|
|
|
|
Gitlab::Sentry.track_acceptable_exception(e, extra: { old_name: old_name, new_name: new_name, storage: storage })
|
|
|
|
|
2017-10-05 09:01:26 -04:00
|
|
|
false
|
2013-03-21 16:11:08 -04:00
|
|
|
end
|
2019-01-03 16:03:35 -05:00
|
|
|
alias_method :mv_directory, :mv_namespace # Note: ShellWorker uses this alias
|
2013-03-21 16:11:08 -04:00
|
|
|
|
2014-09-25 18:07:40 -04:00
|
|
|
def url_to_repo(path)
|
2013-02-11 12:16:59 -05:00
|
|
|
Gitlab.config.gitlab_shell.ssh_path_prefix + "#{path}.git"
|
2012-08-28 17:04:06 -04:00
|
|
|
end
|
2013-03-12 06:37:53 -04:00
|
|
|
|
2013-11-20 06:41:41 -05:00
|
|
|
# Return GitLab shell version
|
|
|
|
def version
|
2013-12-09 12:32:37 -05:00
|
|
|
gitlab_shell_version_file = "#{gitlab_shell_path}/VERSION"
|
2013-11-20 06:41:41 -05:00
|
|
|
|
|
|
|
if File.readable?(gitlab_shell_version_file)
|
2015-04-06 08:02:49 -04:00
|
|
|
File.read(gitlab_shell_version_file).chomp
|
2013-11-20 06:41:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-03 05:50:08 -04:00
|
|
|
# Check if such directory exists in repositories.
|
|
|
|
#
|
|
|
|
# Usage:
|
2016-06-22 17:04:51 -04:00
|
|
|
# exists?(storage, 'gitlab')
|
|
|
|
# exists?(storage, 'gitlab/cookies.git')
|
2015-06-03 05:50:08 -04:00
|
|
|
#
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-06-22 17:04:51 -04:00
|
|
|
def exists?(storage, dir_name)
|
2018-05-02 05:22:15 -04:00
|
|
|
Gitlab::GitalyClient::NamespaceService.new(storage).exists?(dir_name)
|
2015-06-03 05:50:08 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2015-06-03 05:50:08 -04:00
|
|
|
|
2019-02-27 05:58:21 -05:00
|
|
|
def hooks_path
|
|
|
|
File.join(gitlab_shell_path, 'hooks')
|
|
|
|
end
|
|
|
|
|
2013-03-21 16:11:08 -04:00
|
|
|
protected
|
|
|
|
|
2013-12-09 12:32:37 -05:00
|
|
|
def gitlab_shell_path
|
2017-08-14 07:29:47 -04:00
|
|
|
File.expand_path(Gitlab.config.gitlab_shell.path)
|
|
|
|
end
|
|
|
|
|
2013-02-14 18:10:18 -05:00
|
|
|
def gitlab_shell_user_home
|
|
|
|
File.expand_path("~#{Gitlab.config.gitlab_shell.ssh_user}")
|
|
|
|
end
|
|
|
|
|
2016-06-22 17:04:51 -04:00
|
|
|
def full_path(storage, dir_name)
|
2013-03-21 16:11:08 -04:00
|
|
|
raise ArgumentError.new("Directory name can't be blank") if dir_name.blank?
|
|
|
|
|
2018-04-13 06:57:19 -04:00
|
|
|
File.join(Gitlab.config.repositories.storages[storage].legacy_disk_path, dir_name)
|
2013-03-21 16:11:08 -04:00
|
|
|
end
|
|
|
|
|
2014-10-31 08:00:50 -04:00
|
|
|
def gitlab_shell_projects_path
|
|
|
|
File.join(gitlab_shell_path, 'bin', 'gitlab-projects')
|
|
|
|
end
|
|
|
|
|
|
|
|
def gitlab_shell_keys_path
|
|
|
|
File.join(gitlab_shell_path, 'bin', 'gitlab-keys')
|
|
|
|
end
|
2017-07-03 14:28:29 -04:00
|
|
|
|
2017-09-01 11:20:49 -04:00
|
|
|
def authorized_keys_enabled?
|
|
|
|
# Return true if nil to ensure the authorized_keys methods work while
|
|
|
|
# fixing the authorized_keys file during migration.
|
|
|
|
return true if Gitlab::CurrentSettings.current_application_settings.authorized_keys_enabled.nil?
|
|
|
|
|
|
|
|
Gitlab::CurrentSettings.current_application_settings.authorized_keys_enabled
|
|
|
|
end
|
|
|
|
|
2017-07-03 14:28:29 -04:00
|
|
|
private
|
|
|
|
|
2019-03-19 07:16:21 -04:00
|
|
|
def shell_out_for_gitlab_keys?
|
|
|
|
Gitlab.config.gitlab_shell.authorized_keys_file.blank?
|
|
|
|
end
|
|
|
|
|
2017-07-03 14:28:29 -04:00
|
|
|
def gitlab_shell_fast_execute(cmd)
|
|
|
|
output, status = gitlab_shell_fast_execute_helper(cmd)
|
|
|
|
|
|
|
|
return true if status.zero?
|
|
|
|
|
2019-07-10 15:26:47 -04:00
|
|
|
Rails.logger.error("gitlab-shell failed with error #{status}: #{output}") # rubocop:disable Gitlab/RailsLogger
|
2017-07-03 14:28:29 -04:00
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2017-08-03 08:19:11 -04:00
|
|
|
def gitlab_shell_fast_execute_raise_error(cmd, vars = {})
|
|
|
|
output, status = gitlab_shell_fast_execute_helper(cmd, vars)
|
2017-07-03 14:28:29 -04:00
|
|
|
|
|
|
|
raise Error, output unless status.zero?
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2017-07-03 14:28:29 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2017-08-03 08:19:11 -04:00
|
|
|
def gitlab_shell_fast_execute_helper(cmd, vars = {})
|
|
|
|
vars.merge!(ENV.to_h.slice(*GITLAB_SHELL_ENV_VARS))
|
2017-07-03 14:28:29 -04:00
|
|
|
|
|
|
|
# Don't pass along the entire parent environment to prevent gitlab-shell
|
|
|
|
# from wasting I/O by searching through GEM_PATH
|
|
|
|
Bundler.with_original_env { Popen.popen(cmd, nil, vars) }
|
|
|
|
end
|
2017-08-10 10:08:48 -04:00
|
|
|
|
2017-12-11 12:52:07 -05:00
|
|
|
def git_timeout
|
|
|
|
Gitlab.config.gitlab_shell.git_timeout
|
|
|
|
end
|
|
|
|
|
2018-07-06 08:44:54 -04:00
|
|
|
def wrapped_gitaly_errors
|
|
|
|
yield
|
2017-08-10 10:08:48 -04:00
|
|
|
rescue GRPC::NotFound, GRPC::BadStatus => e
|
|
|
|
# Old Popen code returns [Error, output] to the caller, so we
|
|
|
|
# need to do the same here...
|
|
|
|
raise Error, e
|
|
|
|
end
|
2018-06-05 11:58:28 -04:00
|
|
|
|
2019-03-19 07:16:21 -04:00
|
|
|
def gitlab_authorized_keys
|
|
|
|
@gitlab_authorized_keys ||= Gitlab::AuthorizedKeys.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def batch_read_key_ids(batch_size: 100, &block)
|
|
|
|
return unless self.authorized_keys_enabled?
|
|
|
|
|
|
|
|
if shell_out_for_gitlab_keys?
|
|
|
|
IO.popen("#{gitlab_shell_keys_path} list-key-ids") do |key_id_stream|
|
|
|
|
key_id_stream.lazy.each_slice(batch_size) do |lines|
|
|
|
|
yield(lines.map { |l| l.chomp.to_i })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
gitlab_authorized_keys.list_key_ids.lazy.each_slice(batch_size) do |key_ids|
|
|
|
|
yield(key_ids)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def strip_key(key)
|
|
|
|
key.split(/[ ]+/)[0, 2].join(' ')
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_keys_to_io(keys, io)
|
|
|
|
keys.each do |k|
|
|
|
|
key = strip_key(k.key)
|
|
|
|
|
|
|
|
raise Error.new("Invalid key: #{key.inspect}") if key.include?("\t") || key.include?("\n")
|
|
|
|
|
|
|
|
io.puts("#{k.shell_id}\t#{key}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-05 11:58:28 -04:00
|
|
|
class GitalyGitlabProjects
|
2019-01-24 14:37:26 -05:00
|
|
|
attr_reader :shard_name, :repository_relative_path, :output, :gl_project_path
|
2018-06-05 11:58:28 -04:00
|
|
|
|
2019-01-24 14:37:26 -05:00
|
|
|
def initialize(shard_name, repository_relative_path, gl_project_path)
|
2018-06-05 11:58:28 -04:00
|
|
|
@shard_name = shard_name
|
|
|
|
@repository_relative_path = repository_relative_path
|
|
|
|
@output = ''
|
2019-01-24 14:37:26 -05:00
|
|
|
@gl_project_path = gl_project_path
|
2018-06-05 11:58:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def import_project(source, _timeout)
|
2019-01-24 14:37:26 -05:00
|
|
|
raw_repository = Gitlab::Git::Repository.new(shard_name, repository_relative_path, nil, gl_project_path)
|
2018-06-05 11:58:28 -04:00
|
|
|
|
|
|
|
Gitlab::GitalyClient::RepositoryService.new(raw_repository).import_repository(source)
|
|
|
|
true
|
|
|
|
rescue GRPC::BadStatus => e
|
|
|
|
@output = e.message
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2018-12-23 02:34:35 -05:00
|
|
|
def fork_repository(new_shard_name, new_repository_relative_path, new_project_name)
|
|
|
|
target_repository = Gitlab::Git::Repository.new(new_shard_name, new_repository_relative_path, nil, new_project_name)
|
2019-01-24 14:37:26 -05:00
|
|
|
raw_repository = Gitlab::Git::Repository.new(shard_name, repository_relative_path, nil, gl_project_path)
|
2018-06-05 11:58:28 -04:00
|
|
|
|
|
|
|
Gitlab::GitalyClient::RepositoryService.new(target_repository).fork_repository(raw_repository)
|
|
|
|
rescue GRPC::BadStatus => e
|
|
|
|
logger.error "fork-repository failed: #{e.message}"
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def logger
|
2019-07-10 15:26:47 -04:00
|
|
|
Rails.logger # rubocop:disable Gitlab/RailsLogger
|
2018-06-05 11:58:28 -04:00
|
|
|
end
|
|
|
|
end
|
2011-12-03 18:44:59 -05:00
|
|
|
end
|
|
|
|
end
|