Class: Gitlab::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/shell.rb

Overview

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 gitlab.com/groups/gitlab-org/-/epics/2320 is completed. gitlab.com/gitlab-org/gitlab/-/issues/25095 tracks it.

Constant Summary collapse

Error =
Class.new(StandardError)
PERMITTED_ACTIONS =
%w[
  mv_repository remove_repository add_namespace rm_namespace mv_namespace
  repository_exists?
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ensure_secret_token!Object

Ensure gitlab shell has a secret token stored in the secret_file if that was never generated, generate a new one



32
33
34
35
36
# File 'lib/gitlab/shell.rb', line 32

def ensure_secret_token!
  return if File.exist?(File.join(Gitlab.config.gitlab_shell.path, '.gitlab_shell_secret'))

  generate_and_link_secret_token
end

.secret_tokenString

Retrieve GitLab Shell secret token

Returns:

  • (String)

    secret token



26
27
28
# File 'lib/gitlab/shell.rb', line 26

def secret_token
  @secret_token ||= File.read(Gitlab.config.gitlab_shell.secret_file).chomp
end

.versionString

Return GitLab shell version

Returns:

  • (String)

    version



49
50
51
# File 'lib/gitlab/shell.rb', line 49

def version
  @version ||= File.read(gitlab_shell_version_file).chomp if File.readable?(gitlab_shell_version_file)
end

.version_requiredString

Returns required GitLab shell version

Returns:

  • (String)

    version from the manifest file



41
42
43
44
# File 'lib/gitlab/shell.rb', line 41

def version_required
  @version_required ||= File.read(Rails.root
                                  .join('GITLAB_SHELL_VERSION')).strip
end

Instance Method Details

#add_namespace(storage, name) ⇒ Object

Deprecated.

Add empty directory for storing repositories

Examples:

Add new namespace directory

add_namespace("default", "gitlab")

Parameters:

  • storage (String)

    project’s storage path

  • name (String)

    namespace name



139
140
141
142
143
144
145
# File 'lib/gitlab/shell.rb', line 139

def add_namespace(storage, name)
  Gitlab::GitalyClient.allow_n_plus_1_calls do
    Gitlab::GitalyClient::NamespaceService.new(storage).add(name)
  end
rescue GRPC::InvalidArgument => e
  raise ArgumentError, e.message
end

#mv_namespace(storage, old_name, new_name) ⇒ Object

Deprecated.

Move namespace directory inside repositories storage

Examples:

Move/rename a namespace directory

mv_namespace("/path/to/storage", "gitlab", "gitlabhq")

Parameters:

  • storage (String)

    project’s storage path

  • old_name (String)

    current namespace name

  • new_name (String)

    new namespace name



174
175
176
177
178
179
180
# File 'lib/gitlab/shell.rb', line 174

def mv_namespace(storage, old_name, new_name)
  Gitlab::GitalyClient::NamespaceService.new(storage).rename(old_name, new_name)
rescue GRPC::InvalidArgument => e
  Gitlab::ErrorTracking.track_exception(e, old_name: old_name, new_name: new_name, storage: storage)

  false
end

#mv_repository(storage, disk_path, new_disk_path) ⇒ Boolean

Deprecated.

Move or rename a repository

Examples:

Move/rename a repository

mv_repository("/path/to/storage", "gitlab/gitlab-ci", "randx/gitlab-ci-new")

Parameters:

  • storage (String)

    project’s storage path

  • disk_path (String)

    current project path on disk

  • new_disk_path (String)

    new project path on disk

Returns:

  • (Boolean)

    whether repository could be moved/renamed on disk



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/gitlab/shell.rb', line 94

def mv_repository(storage, disk_path, new_disk_path)
  return false if disk_path.empty? || new_disk_path.empty?

  Gitlab::Git::Repository.new(storage, "#{disk_path}.git", nil, nil).rename("#{new_disk_path}.git")

  true
rescue StandardError => e
  Gitlab::ErrorTracking.track_exception(e, path: disk_path, new_path: new_disk_path, storage: storage)

  false
end

#remove_repository(storage, disk_path) ⇒ Object

Deprecated.

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.

Examples:

Remove a repository

remove_repository("/path/to/storage", "gitlab/gitlab-ci")

Parameters:

  • storage (String)

    project’s storage path

  • disk_path (String)

    current project path on disk



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/gitlab/shell.rb', line 117

def remove_repository(storage, disk_path)
  return false if disk_path.empty?

  Gitlab::Git::Repository.new(storage, "#{disk_path}.git", nil, nil).remove

  true
rescue StandardError => e
  Gitlab::AppLogger.warn("Repository does not exist: #{e} at: #{disk_path}.git")
  Gitlab::ErrorTracking.track_exception(e, path: disk_path, storage: storage)

  false
end

#repository_exists?(storage, dir_name) ⇒ Boolean

Deprecated.

Check if repository exists on disk

Examples:

Check if repository exists

repository_exists?('default', 'gitlab-org/gitlab.git')

Parameters:

  • storage (String)

    project’s storage path

  • dir_name (Object)

    repository dir name

Returns:

  • (Boolean)

    whether repository exists or not



192
193
194
195
196
# File 'lib/gitlab/shell.rb', line 192

def repository_exists?(storage, dir_name)
  Gitlab::Git::Repository.new(storage, dir_name, nil, nil).exists?
rescue GRPC::Internal
  false
end

#rm_namespace(storage, name) ⇒ Object Also known as: rm_directory

Deprecated.

Remove directory from repositories storage Every repository inside this directory will be removed too

Examples:

Remove namespace directory

rm_namespace("default", "gitlab")

Parameters:

  • storage (String)

    project’s storage path

  • name (String)

    namespace name



157
158
159
160
161
# File 'lib/gitlab/shell.rb', line 157

def rm_namespace(storage, name)
  Gitlab::GitalyClient::NamespaceService.new(storage).remove(name)
rescue GRPC::InvalidArgument => e
  raise ArgumentError, e.message
end