2018-10-22 07:00:50 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-24 19:06:46 +00:00
|
|
|
require 'securerandom'
|
|
|
|
|
2012-05-26 10:37:49 +00:00
|
|
|
module Gitlab
|
2020-03-17 21:09:16 +00:00
|
|
|
# This class is an artifact of a time when common repository operations were
|
|
|
|
# performed by calling out to scripts in the gitlab-shell project. Now, these
|
|
|
|
# operations are all performed by Gitaly, and are mostly accessible through
|
|
|
|
# the Repository class. Prefer using a Repository to functionality here.
|
|
|
|
#
|
|
|
|
# Legacy code relating to namespaces still relies on Gitlab::Shell; it can be
|
|
|
|
# converted to a module once https://gitlab.com/groups/gitlab-org/-/epics/2320
|
|
|
|
# is completed. https://gitlab.com/gitlab-org/gitlab/-/issues/25095 tracks it.
|
2013-02-04 13:07:56 +00:00
|
|
|
class Shell
|
2017-03-01 11:00:37 +00:00
|
|
|
Error = Class.new(StandardError)
|
2012-09-07 05:16:29 +00:00
|
|
|
|
2014-11-05 16:14:22 +00:00
|
|
|
class << self
|
2020-01-27 12:08:35 +00:00
|
|
|
# Retrieve GitLab Shell secret token
|
|
|
|
#
|
|
|
|
# @return [String] secret token
|
2016-09-29 16:46:54 +00:00
|
|
|
def secret_token
|
|
|
|
@secret_token ||= begin
|
|
|
|
File.read(Gitlab.config.gitlab_shell.secret_file).chomp
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-27 12:08:35 +00:00
|
|
|
# Ensure gitlab shell has a secret token stored in the secret_file
|
|
|
|
# if that was never generated, generate a new one
|
2016-09-29 16:46:54 +00:00
|
|
|
def ensure_secret_token!
|
|
|
|
return if File.exist?(File.join(Gitlab.config.gitlab_shell.path, '.gitlab_shell_secret'))
|
|
|
|
|
|
|
|
generate_and_link_secret_token
|
|
|
|
end
|
|
|
|
|
2020-01-27 12:08:35 +00:00
|
|
|
# Returns required GitLab shell version
|
|
|
|
#
|
|
|
|
# @return [String] version from the manifest file
|
2014-11-05 16:14:22 +00:00
|
|
|
def version_required
|
2017-06-21 13:48:12 +00:00
|
|
|
@version_required ||= File.read(Rails.root
|
|
|
|
.join('GITLAB_SHELL_VERSION')).strip
|
2014-11-05 16:14:22 +00:00
|
|
|
end
|
2016-09-16 09:43:05 +00:00
|
|
|
|
2020-03-16 12:09:12 +00:00
|
|
|
# Return GitLab shell version
|
|
|
|
#
|
|
|
|
# @return [String] version
|
|
|
|
def version
|
|
|
|
@version ||= File.read(gitlab_shell_version_file).chomp if File.readable?(gitlab_shell_version_file)
|
|
|
|
end
|
|
|
|
|
2016-09-29 16:46:54 +00:00
|
|
|
private
|
|
|
|
|
2020-03-16 12:09:12 +00:00
|
|
|
def gitlab_shell_path
|
|
|
|
File.expand_path(Gitlab.config.gitlab_shell.path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def gitlab_shell_version_file
|
|
|
|
File.join(gitlab_shell_path, 'VERSION')
|
|
|
|
end
|
|
|
|
|
2016-09-29 16:46:54 +00:00
|
|
|
# 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 08:04:58 +00:00
|
|
|
@secret_token = SecureRandom.hex(16)
|
|
|
|
File.write(secret_file, @secret_token)
|
2016-09-29 16:46:54 +00: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 16:14:22 +00:00
|
|
|
end
|
|
|
|
|
2020-01-27 12:08:35 +00:00
|
|
|
# Move or rename a repository
|
2013-03-12 10:37:53 +00:00
|
|
|
#
|
2020-01-27 12:08:35 +00:00
|
|
|
# @example Move/rename a repository
|
2016-06-22 21:04:51 +00:00
|
|
|
# mv_repository("/path/to/storage", "gitlab/gitlab-ci", "randx/gitlab-ci-new")
|
2020-01-27 12:08:35 +00:00
|
|
|
#
|
|
|
|
# @param [String] storage project's storage path
|
|
|
|
# @param [String] disk_path current project path on disk
|
|
|
|
# @param [String] new_disk_path new project path on disk
|
|
|
|
# @return [Boolean] whether repository could be moved/renamed on disk
|
2020-03-17 21:09:16 +00:00
|
|
|
#
|
|
|
|
# @deprecated
|
2020-01-27 12:08:35 +00:00
|
|
|
def mv_repository(storage, disk_path, new_disk_path)
|
|
|
|
return false if disk_path.empty? || new_disk_path.empty?
|
2018-01-05 12:29:04 +00:00
|
|
|
|
2020-01-27 12:08:35 +00:00
|
|
|
Gitlab::Git::Repository.new(storage, "#{disk_path}.git", nil, nil).rename("#{new_disk_path}.git")
|
2019-10-02 15:06:12 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
rescue => e
|
2020-01-27 12:08:35 +00:00
|
|
|
Gitlab::ErrorTracking.track_exception(e, path: disk_path, new_path: new_disk_path, storage: storage)
|
2019-10-02 15:06:12 +00:00
|
|
|
|
|
|
|
false
|
2013-03-12 10:37:53 +00:00
|
|
|
end
|
|
|
|
|
2018-01-05 12:29:04 +00: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 15:22:45 +00:00
|
|
|
#
|
2020-01-27 12:08:35 +00:00
|
|
|
# @example Remove a repository
|
2016-06-22 21:04:51 +00:00
|
|
|
# remove_repository("/path/to/storage", "gitlab/gitlab-ci")
|
2020-01-27 12:08:35 +00:00
|
|
|
#
|
|
|
|
# @param [String] storage project's storage path
|
|
|
|
# @param [String] disk_path current project path on disk
|
2020-03-17 21:09:16 +00:00
|
|
|
#
|
|
|
|
# @deprecated
|
2020-01-27 12:08:35 +00:00
|
|
|
def remove_repository(storage, disk_path)
|
|
|
|
return false if disk_path.empty?
|
2018-01-05 12:29:04 +00:00
|
|
|
|
2020-01-27 12:08:35 +00:00
|
|
|
Gitlab::Git::Repository.new(storage, "#{disk_path}.git", nil, nil).remove
|
2019-10-02 15:06:12 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
rescue => e
|
2020-01-27 12:08:35 +00:00
|
|
|
Rails.logger.warn("Repository does not exist: #{e} at: #{disk_path}.git") # rubocop:disable Gitlab/RailsLogger
|
|
|
|
Gitlab::ErrorTracking.track_exception(e, path: disk_path, storage: storage)
|
2019-10-02 15:06:12 +00:00
|
|
|
|
2018-01-05 12:29:04 +00:00
|
|
|
false
|
2012-03-05 22:26:40 +00:00
|
|
|
end
|
|
|
|
|
2013-03-21 20:11:08 +00:00
|
|
|
# Add empty directory for storing repositories
|
|
|
|
#
|
2020-01-27 12:08:35 +00:00
|
|
|
# @example Add new namespace directory
|
2018-04-13 10:57:19 +00:00
|
|
|
# add_namespace("default", "gitlab")
|
2013-03-21 20:11:08 +00:00
|
|
|
#
|
2020-01-27 12:08:35 +00:00
|
|
|
# @param [String] storage project's storage path
|
|
|
|
# @param [String] name namespace name
|
2020-03-17 21:09:16 +00:00
|
|
|
#
|
|
|
|
# @deprecated
|
2016-06-22 21:04:51 +00:00
|
|
|
def add_namespace(storage, name)
|
2019-02-22 14:51:27 +00:00
|
|
|
Gitlab::GitalyClient.allow_n_plus_1_calls do
|
|
|
|
Gitlab::GitalyClient::NamespaceService.new(storage).add(name)
|
|
|
|
end
|
2017-09-12 10:26:59 +00:00
|
|
|
rescue GRPC::InvalidArgument => e
|
|
|
|
raise ArgumentError, e.message
|
2013-03-21 20:11:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Remove directory from repositories storage
|
|
|
|
# Every repository inside this directory will be removed too
|
|
|
|
#
|
2020-01-27 12:08:35 +00:00
|
|
|
# @example Remove namespace directory
|
2018-04-13 10:57:19 +00:00
|
|
|
# rm_namespace("default", "gitlab")
|
2013-03-21 20:11:08 +00:00
|
|
|
#
|
2020-01-27 12:08:35 +00:00
|
|
|
# @param [String] storage project's storage path
|
|
|
|
# @param [String] name namespace name
|
2020-03-17 21:09:16 +00:00
|
|
|
#
|
|
|
|
# @deprecated
|
2016-06-22 21:04:51 +00:00
|
|
|
def rm_namespace(storage, name)
|
2018-05-02 09:22:15 +00:00
|
|
|
Gitlab::GitalyClient::NamespaceService.new(storage).remove(name)
|
2017-09-12 10:26:59 +00:00
|
|
|
rescue GRPC::InvalidArgument => e
|
|
|
|
raise ArgumentError, e.message
|
2013-03-21 20:11:08 +00:00
|
|
|
end
|
2018-01-05 12:29:04 +00:00
|
|
|
alias_method :rm_directory, :rm_namespace
|
2013-03-21 20:11:08 +00:00
|
|
|
|
|
|
|
# Move namespace directory inside repositories storage
|
|
|
|
#
|
2020-01-27 12:08:35 +00:00
|
|
|
# @example Move/rename a namespace directory
|
2016-06-22 21:04:51 +00:00
|
|
|
# mv_namespace("/path/to/storage", "gitlab", "gitlabhq")
|
2013-03-21 20:11:08 +00:00
|
|
|
#
|
2020-01-27 12:08:35 +00:00
|
|
|
# @param [String] storage project's storage path
|
|
|
|
# @param [String] old_name current namespace name
|
|
|
|
# @param [String] new_name new namespace name
|
2020-03-17 21:09:16 +00:00
|
|
|
#
|
|
|
|
# @deprecated
|
2016-06-22 21:04:51 +00:00
|
|
|
def mv_namespace(storage, old_name, new_name)
|
2018-05-02 09:22:15 +00:00
|
|
|
Gitlab::GitalyClient::NamespaceService.new(storage).rename(old_name, new_name)
|
2019-01-03 21:03:35 +00:00
|
|
|
rescue GRPC::InvalidArgument => e
|
2019-12-16 12:07:43 +00:00
|
|
|
Gitlab::ErrorTracking.track_exception(e, old_name: old_name, new_name: new_name, storage: storage)
|
2019-01-03 21:03:35 +00:00
|
|
|
|
2017-10-05 13:01:26 +00:00
|
|
|
false
|
2013-03-21 20:11:08 +00:00
|
|
|
end
|
|
|
|
|
2020-01-27 12:08:35 +00:00
|
|
|
# Check if repository exists on disk
|
|
|
|
#
|
|
|
|
# @example Check if repository exists
|
|
|
|
# repository_exists?('default', 'gitlab-org/gitlab.git')
|
|
|
|
#
|
|
|
|
# @return [Boolean] whether repository exists or not
|
|
|
|
# @param [String] storage project's storage path
|
|
|
|
# @param [Object] dir_name repository dir name
|
2020-03-17 21:09:16 +00:00
|
|
|
#
|
|
|
|
# @deprecated
|
2019-10-09 09:06:19 +00:00
|
|
|
def repository_exists?(storage, dir_name)
|
|
|
|
Gitlab::Git::Repository.new(storage, dir_name, nil, nil).exists?
|
|
|
|
rescue GRPC::Internal
|
|
|
|
false
|
|
|
|
end
|
2011-12-03 23:44:59 +00:00
|
|
|
end
|
|
|
|
end
|