BigW Consortium Gitlab

shell.rb 8.12 KB
Newer Older
1 2
require 'securerandom'

3
module Gitlab
4
  class Shell
5
    Error = Class.new(StandardError)
6

7
    KeyAdder = Struct.new(:io) do
8
      def add_key(id, key)
9 10 11 12 13 14
        key = Gitlab::Shell.strip_key(key)
        # Newline and tab are part of the 'protocol' used to transmit id+key to the other end
        if key.include?("\t") || key.include?("\n")
          raise Error.new("Invalid key: #{key.inspect}")
        end

15
        io.puts("#{id}\t#{key}")
16 17 18
      end
    end

19
    class << self
20 21 22 23 24 25 26 27 28 29 30 31
      def secret_token
        @secret_token ||= begin
          File.read(Gitlab.config.gitlab_shell.secret_file).chomp
        end
      end

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

        generate_and_link_secret_token
      end

32
      def version_required
33 34
        @version_required ||= File.read(Rails.root.
                                        join('GITLAB_SHELL_VERSION')).strip
35
      end
36 37

      def strip_key(key)
38
        key.split(/[ ]+/)[0, 2].join(' ')
39
      end
40 41 42 43 44 45 46 47 48 49

      private

      # 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.
50 51
          @secret_token = SecureRandom.hex(16)
          File.write(secret_file, @secret_token)
52 53 54 55 56 57 58
        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
59 60
    end

61
    # Init new repository
62
    #
63
    # storage - project's storage path
64
    # name - project path with namespace
65 66
    #
    # Ex.
67
    #   add_repository("/path/to/storage", "gitlab/gitlab-ci")
68
    #
69
    def add_repository(storage, name)
70
      Gitlab::Utils.system_silent([gitlab_shell_projects_path,
71
                                   'add-project', storage, "#{name}.git"])
72 73
    end

74 75
    # Import repository
    #
76
    # storage - project's storage path
77 78 79
    # name - project path with namespace
    #
    # Ex.
80
    #   import_repository("/path/to/storage", "gitlab/gitlab-ci", "https://github.com/randx/six.git")
81
    #
82
    def import_repository(storage, name, url)
83 84
      # Timeout should be less than 900 ideally, to prevent the memory killer
      # to silently kill the process without knowing we are timing out here.
85
      output, status = Popen.popen([gitlab_shell_projects_path, 'import-project',
86
                                    storage, "#{name}.git", url, "#{Gitlab.config.gitlab_shell.git_timeout}"])
87 88
      raise Error, output unless status.zero?
      true
89 90
    end

91 92 93 94 95
    # Fetch remote for repository
    #
    # name - project path with namespace
    # remote - remote name
    # forced - should we use --force flag?
96
    # no_tags - should we use --no-tags flag?
97 98 99 100 101
    #
    # Ex.
    #   fetch_remote("gitlab/gitlab-ci", "upstream")
    #
    def fetch_remote(storage, name, remote, forced: false, no_tags: false)
102
      args = [gitlab_shell_projects_path, 'fetch-remote', storage, "#{name}.git", remote, "#{Gitlab.config.gitlab_shell.git_timeout}"]
103 104 105 106 107 108 109 110
      args << '--force' if forced
      args << '--no-tags' if no_tags

      output, status = Popen.popen(args)
      raise Error, output unless status.zero?
      true
    end

111
    # Move repository
112
    # storage - project's storage path
113 114 115 116
    # path - project path with namespace
    # new_path - new project path with namespace
    #
    # Ex.
117
    #   mv_repository("/path/to/storage", "gitlab/gitlab-ci", "randx/gitlab-ci-new")
118
    #
119
    def mv_repository(storage, path, new_path)
120
      Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'mv-project',
121
                                   storage, "#{path}.git", "#{new_path}.git"])
122 123
    end

124
    # Fork repository to new namespace
125
    # forked_from_storage - forked-from project's storage path
126
    # path - project path with namespace
127
    # forked_to_storage - forked-to project's storage path
128 129 130
    # fork_namespace - namespace for forked project
    #
    # Ex.
131
    #  fork_repository("/path/to/forked_from/storage", "gitlab/gitlab-ci", "/path/to/forked_to/storage", "randx")
132
    #
133
    def fork_repository(forked_from_storage, path, forked_to_storage, fork_namespace)
134
      Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'fork-project',
135 136
                                   forked_from_storage, "#{path}.git", forked_to_storage,
                                   fork_namespace])
137 138
    end

139
    # Remove repository from file system
140
    #
141
    # storage - project's storage path
142
    # name - project path with namespace
143 144
    #
    # Ex.
145
    #   remove_repository("/path/to/storage", "gitlab/gitlab-ci")
146
    #
147
    def remove_repository(storage, name)
148
      Gitlab::Utils.system_silent([gitlab_shell_projects_path,
149
                                   'rm-project', storage, "#{name}.git"])
150 151
    end

152
    # Add new key to gitlab-shell
153
    #
154
    # Ex.
155
    #   add_key("key-42", "sha-rsa ...")
156
    #
157
    def add_key(key_id, key_content)
158
      Gitlab::Utils.system_silent([gitlab_shell_keys_path,
159
                                   'add-key', key_id, self.class.strip_key(key_content)])
160 161
    end

162 163 164 165 166 167
    # Batch-add keys to authorized_keys
    #
    # Ex.
    #   batch_add_keys { |adder| adder.add_key("key-42", "sha-rsa ...") }
    def batch_add_keys(&block)
      IO.popen(%W(#{gitlab_shell_path}/bin/gitlab-keys batch-add-keys), 'w') do |io|
168
        yield(KeyAdder.new(io))
169 170 171
      end
    end

172
    # Remove ssh key from gitlab shell
173 174
    #
    # Ex.
175
    #   remove_key("key-342", "sha-rsa ...")
176
    #
177
    def remove_key(key_id, key_content)
178 179
      Gitlab::Utils.system_silent([gitlab_shell_keys_path,
                                   'rm-key', key_id, key_content])
180 181
    end

182 183 184
    # Remove all ssh keys from gitlab shell
    #
    # Ex.
Johannes Schleifenbaum committed
185
    #   remove_all_keys
186 187
    #
    def remove_all_keys
188
      Gitlab::Utils.system_silent([gitlab_shell_keys_path, 'clear'])
189 190
    end

191 192 193
    # Add empty directory for storing repositories
    #
    # Ex.
194
    #   add_namespace("/path/to/storage", "gitlab")
195
    #
196
    def add_namespace(storage, name)
197 198 199 200
      path = full_path(storage, name)
      FileUtils.mkdir_p(path, mode: 0770) unless exists?(storage, name)
    rescue Errno::EEXIST => e
      Rails.logger.warn("Directory exists as a file: #{e} at: #{path}")
201 202 203 204 205 206
    end

    # Remove directory from repositories storage
    # Every repository inside this directory will be removed too
    #
    # Ex.
207
    #   rm_namespace("/path/to/storage", "gitlab")
208
    #
209 210
    def rm_namespace(storage, name)
      FileUtils.rm_r(full_path(storage, name), force: true)
211 212 213 214 215
    end

    # Move namespace directory inside repositories storage
    #
    # Ex.
216
    #   mv_namespace("/path/to/storage", "gitlab", "gitlabhq")
217
    #
218 219
    def mv_namespace(storage, old_name, new_name)
      return false if exists?(storage, new_name) || !exists?(storage, old_name)
220

221
      FileUtils.mv(full_path(storage, old_name), full_path(storage, new_name))
222 223
    end

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

228 229
    # Return GitLab shell version
    def version
230
      gitlab_shell_version_file = "#{gitlab_shell_path}/VERSION"
231 232

      if File.readable?(gitlab_shell_version_file)
233
        File.read(gitlab_shell_version_file).chomp
234 235 236
      end
    end

237 238 239
    # Check if such directory exists in repositories.
    #
    # Usage:
240 241
    #   exists?(storage, 'gitlab')
    #   exists?(storage, 'gitlab/cookies.git')
242
    #
243 244
    def exists?(storage, dir_name)
      File.exist?(full_path(storage, dir_name))
245 246
    end

247 248
    protected

249 250 251 252
    def gitlab_shell_path
      Gitlab.config.gitlab_shell.path
    end

253 254 255 256
    def gitlab_shell_user_home
      File.expand_path("~#{Gitlab.config.gitlab_shell.ssh_user}")
    end

257
    def full_path(storage, dir_name)
258 259
      raise ArgumentError.new("Directory name can't be blank") if dir_name.blank?

260
      File.join(storage, dir_name)
261 262
    end

263 264 265 266 267 268 269
    def gitlab_shell_projects_path
      File.join(gitlab_shell_path, 'bin', 'gitlab-projects')
    end

    def gitlab_shell_keys_path
      File.join(gitlab_shell_path, 'bin', 'gitlab-keys')
    end
270 271
  end
end