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)

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


29
30
31
32
33
# File 'lib/gitlab/shell.rb', line 29

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


21
22
23
24
25
# File 'lib/gitlab/shell.rb', line 21

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

.versionString

Return GitLab shell version

Returns:

  • (String)

    version


46
47
48
# File 'lib/gitlab/shell.rb', line 46

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


38
39
40
41
# File 'lib/gitlab/shell.rb', line 38

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


134
135
136
137
138
139
140
# File 'lib/gitlab/shell.rb', line 134

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


169
170
171
172
173
174
175
# File 'lib/gitlab/shell.rb', line 169

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


89
90
91
92
93
94
95
96
97
98
99
# File 'lib/gitlab/shell.rb', line 89

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 => 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


112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/gitlab/shell.rb', line 112

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 => 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


187
188
189
190
191
# File 'lib/gitlab/shell.rb', line 187

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


152
153
154
155
156
# File 'lib/gitlab/shell.rb', line 152

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