Class: Gitlab::Shell

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

Defined Under Namespace

Classes: Error, KeyAdder

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.version_requiredObject


13
14
15
16
# File 'lib/gitlab/backend/shell.rb', line 13

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

Instance Method Details

#add_key(key_id, key_content) ⇒ Object

Add new key to gitlab-shell

Ex.

add_key("key-42", "sha-rsa ...")

99
100
101
102
# File 'lib/gitlab/backend/shell.rb', line 99

def add_key(key_id, key_content)
  Gitlab::Utils.system_silent([gitlab_shell_keys_path,
                               'add-key', key_id, key_content])
end

#add_namespace(name) ⇒ Object

Add empty directory for storing repositories

Ex.

add_namespace("gitlab")

138
139
140
# File 'lib/gitlab/backend/shell.rb', line 138

def add_namespace(name)
  FileUtils.mkdir(full_path(name), mode: 0770) unless exists?(name)
end

#add_repository(name) ⇒ Object

Init new repository

name - project path with namespace

Ex.

add_repository("gitlab/gitlab-ci")

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

def add_repository(name)
  Gitlab::Utils.system_silent([gitlab_shell_projects_path,
                               'add-project', "#{name}.git"])
end

#batch_add_keys(&block) ⇒ Object

Batch-add keys to authorized_keys

Ex.

batch_add_keys { |adder| adder.add_key("key-42", "sha-rsa ...") }

108
109
110
111
112
# File 'lib/gitlab/backend/shell.rb', line 108

def batch_add_keys(&block)
  IO.popen(%W(#{gitlab_shell_path}/bin/gitlab-keys batch-add-keys), 'w') do |io|
    block.call(KeyAdder.new(io))
  end
end

#exists?(dir_name) ⇒ Boolean

Check if such directory exists in repositories.

Usage:

exists?('gitlab')
exists?('gitlab/cookies.git')

Returns:

  • (Boolean)

182
183
184
# File 'lib/gitlab/backend/shell.rb', line 182

def exists?(dir_name)
  File.exists?(full_path(dir_name))
end

#fork_repository(path, fork_namespace) ⇒ Object

Fork repository to new namespace

path - project path with namespace fork_namespace - namespace for forked project

Ex.

fork_repository("gitlab/gitlab-ci", "randx")

65
66
67
68
# File 'lib/gitlab/backend/shell.rb', line 65

def fork_repository(path, fork_namespace)
  Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'fork-project',
                               "#{path}.git", fork_namespace])
end

#gc(path) ⇒ Object

Gc repository

path - project path with namespace

Ex.

gc("gitlab/gitlab-ci")

89
90
91
92
# File 'lib/gitlab/backend/shell.rb', line 89

def gc(path)
  Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'gc',
                               "#{path}.git"])
end

#import_repository(name, url) ⇒ Object

Import repository

name - project path with namespace

Ex.

import_repository("gitlab/gitlab-ci", "https://github.com/randx/six.git")

Raises:


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

def import_repository(name, url)
  output, status = Popen::popen([gitlab_shell_projects_path, 'import-project', "#{name}.git", url, '900'])
  raise Error, output unless status.zero?
  true
end

#mv_namespace(old_name, new_name) ⇒ Object

Move namespace directory inside repositories storage

Ex.

mv_namespace("gitlab", "gitlabhq")

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

def mv_namespace(old_name, new_name)
  return false if exists?(new_name) || !exists?(old_name)

  FileUtils.mv(full_path(old_name), full_path(new_name))
end

#mv_repository(path, new_path) ⇒ Object

Move repository

path - project path with namespace new_path - new project path with namespace

Ex.

mv_repository("gitlab/gitlab-ci", "randx/gitlab-ci-new")

52
53
54
55
# File 'lib/gitlab/backend/shell.rb', line 52

def mv_repository(path, new_path)
  Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'mv-project',
                               "#{path}.git", "#{new_path}.git"])
end

#remove_all_keysObject

Remove all ssh keys from gitlab shell

Ex.

remove_all_keys

129
130
131
# File 'lib/gitlab/backend/shell.rb', line 129

def remove_all_keys
  Gitlab::Utils.system_silent([gitlab_shell_keys_path, 'clear'])
end

#remove_key(key_id, key_content) ⇒ Object

Remove ssh key from gitlab shell

Ex.

remove_key("key-342", "sha-rsa ...")

119
120
121
122
# File 'lib/gitlab/backend/shell.rb', line 119

def remove_key(key_id, key_content)
  Gitlab::Utils.system_silent([gitlab_shell_keys_path,
                               'rm-key', key_id, key_content])
end

#remove_repository(name) ⇒ Object

Remove repository from file system

name - project path with namespace

Ex.

remove_repository("gitlab/gitlab-ci")

77
78
79
80
# File 'lib/gitlab/backend/shell.rb', line 77

def remove_repository(name)
  Gitlab::Utils.system_silent([gitlab_shell_projects_path,
                               'rm-project', "#{name}.git"])
end

#rm_namespace(name) ⇒ Object

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

Ex.

rm_namespace("gitlab")

148
149
150
# File 'lib/gitlab/backend/shell.rb', line 148

def rm_namespace(name)
  FileUtils.rm_r(full_path(name), force: true)
end

#url_to_repo(path) ⇒ Object


163
164
165
# File 'lib/gitlab/backend/shell.rb', line 163

def url_to_repo(path)
  Gitlab.config.gitlab_shell.ssh_path_prefix + "#{path}.git"
end

#versionObject

Return GitLab shell version


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

def version
  gitlab_shell_version_file = "#{gitlab_shell_path}/VERSION"

  if File.readable?(gitlab_shell_version_file)
    File.read(gitlab_shell_version_file).chomp
  end
end