2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-24 15:06:46 -04:00
|
|
|
require 'securerandom'
|
|
|
|
|
2012-05-26 06:37:49 -04:00
|
|
|
module Gitlab
|
2020-03-17 17:09:16 -04: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 08:07:56 -05:00
|
|
|
class Shell
|
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
|
2020-01-27 07:08:35 -05:00
|
|
|
# Retrieve GitLab Shell secret token
|
|
|
|
#
|
|
|
|
# @return [String] secret token
|
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
|
|
|
|
|
2020-01-27 07:08:35 -05: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 12:46:54 -04: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 07:08:35 -05:00
|
|
|
# Returns required GitLab shell version
|
|
|
|
#
|
|
|
|
# @return [String] version from the manifest file
|
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
|
|
|
|
2020-03-16 08:09:12 -04: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 12:46:54 -04:00
|
|
|
private
|
|
|
|
|
2020-03-16 08:09:12 -04: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 12:46:54 -04: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 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)
|
2022-09-01 20:11:46 -04:00
|
|
|
# It could happen that link_path is a broken symbolic link.
|
|
|
|
# In that case !File.exist?(link_path) is true, but we still want to overwrite the (broken) symbolic link.
|
|
|
|
FileUtils.ln_sf(secret_file, link_path)
|
2016-09-29 12:46:54 -04:00
|
|
|
end
|
|
|
|
end
|
2014-11-05 11:14:22 -05:00
|
|
|
end
|
|
|
|
|
2020-01-27 07:08:35 -05:00
|
|
|
# Move or rename a repository
|
2013-03-12 06:37:53 -04:00
|
|
|
#
|
2020-01-27 07:08:35 -05:00
|
|
|
# @example Move/rename a repository
|
2016-06-22 17:04:51 -04:00
|
|
|
# mv_repository("/path/to/storage", "gitlab/gitlab-ci", "randx/gitlab-ci-new")
|
2020-01-27 07:08:35 -05: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 17:09:16 -04:00
|
|
|
#
|
|
|
|
# @deprecated
|
2020-01-27 07:08:35 -05:00
|
|
|
def mv_repository(storage, disk_path, new_disk_path)
|
|
|
|
return false if disk_path.empty? || new_disk_path.empty?
|
2018-01-05 07:29:04 -05:00
|
|
|
|
2020-01-27 07:08:35 -05:00
|
|
|
Gitlab::Git::Repository.new(storage, "#{disk_path}.git", nil, nil).rename("#{new_disk_path}.git")
|
2019-10-02 11:06:12 -04:00
|
|
|
|
|
|
|
true
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError => e
|
2020-01-27 07:08:35 -05:00
|
|
|
Gitlab::ErrorTracking.track_exception(e, path: disk_path, new_path: new_disk_path, storage: storage)
|
2019-10-02 11:06:12 -04:00
|
|
|
|
|
|
|
false
|
2013-03-12 06:37:53 -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
|
|
|
#
|
2020-01-27 07:08:35 -05:00
|
|
|
# @example Remove a repository
|
2016-06-22 17:04:51 -04:00
|
|
|
# remove_repository("/path/to/storage", "gitlab/gitlab-ci")
|
2020-01-27 07:08:35 -05:00
|
|
|
#
|
|
|
|
# @param [String] storage project's storage path
|
|
|
|
# @param [String] disk_path current project path on disk
|
2020-03-17 17:09:16 -04:00
|
|
|
#
|
|
|
|
# @deprecated
|
2020-01-27 07:08:35 -05:00
|
|
|
def remove_repository(storage, disk_path)
|
|
|
|
return false if disk_path.empty?
|
2018-01-05 07:29:04 -05:00
|
|
|
|
2020-01-27 07:08:35 -05:00
|
|
|
Gitlab::Git::Repository.new(storage, "#{disk_path}.git", nil, nil).remove
|
2019-10-02 11:06:12 -04:00
|
|
|
|
|
|
|
true
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError => e
|
2020-09-11 08:08:50 -04:00
|
|
|
Gitlab::AppLogger.warn("Repository does not exist: #{e} at: #{disk_path}.git")
|
2020-01-27 07:08:35 -05:00
|
|
|
Gitlab::ErrorTracking.track_exception(e, path: disk_path, storage: storage)
|
2019-10-02 11:06:12 -04:00
|
|
|
|
2018-01-05 07:29:04 -05:00
|
|
|
false
|
2012-03-05 17:26:40 -05:00
|
|
|
end
|
|
|
|
|
2013-03-21 16:11:08 -04:00
|
|
|
# Add empty directory for storing repositories
|
|
|
|
#
|
2020-01-27 07:08:35 -05:00
|
|
|
# @example Add new namespace directory
|
2018-04-13 06:57:19 -04:00
|
|
|
# add_namespace("default", "gitlab")
|
2013-03-21 16:11:08 -04:00
|
|
|
#
|
2020-01-27 07:08:35 -05:00
|
|
|
# @param [String] storage project's storage path
|
|
|
|
# @param [String] name namespace name
|
2020-03-17 17:09:16 -04:00
|
|
|
#
|
|
|
|
# @deprecated
|
2016-06-22 17:04:51 -04:00
|
|
|
def add_namespace(storage, name)
|
2019-02-22 09:51:27 -05:00
|
|
|
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
|
|
|
|
#
|
2020-01-27 07:08:35 -05:00
|
|
|
# @example Remove namespace directory
|
2018-04-13 06:57:19 -04:00
|
|
|
# rm_namespace("default", "gitlab")
|
2013-03-21 16:11:08 -04:00
|
|
|
#
|
2020-01-27 07:08:35 -05:00
|
|
|
# @param [String] storage project's storage path
|
|
|
|
# @param [String] name namespace name
|
2020-03-17 17:09:16 -04:00
|
|
|
#
|
|
|
|
# @deprecated
|
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
|
|
|
|
#
|
2020-01-27 07:08:35 -05:00
|
|
|
# @example Move/rename a namespace directory
|
2016-06-22 17:04:51 -04:00
|
|
|
# mv_namespace("/path/to/storage", "gitlab", "gitlabhq")
|
2013-03-21 16:11:08 -04:00
|
|
|
#
|
2020-01-27 07:08:35 -05: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 17:09:16 -04:00
|
|
|
#
|
|
|
|
# @deprecated
|
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
|
2019-12-16 07:07:43 -05:00
|
|
|
Gitlab::ErrorTracking.track_exception(e, old_name: old_name, new_name: new_name, storage: storage)
|
2019-01-03 16:03:35 -05:00
|
|
|
|
2017-10-05 09:01:26 -04:00
|
|
|
false
|
2013-03-21 16:11:08 -04:00
|
|
|
end
|
|
|
|
|
2020-01-27 07:08:35 -05: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 17:09:16 -04:00
|
|
|
#
|
|
|
|
# @deprecated
|
2019-10-09 05:06:19 -04: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 18:44:59 -05:00
|
|
|
end
|
|
|
|
end
|