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

3
module Gitlab
4
  class Shell
5
    class Error < StandardError; end
6

Gabriel Mazetto's avatar
Gabriel Mazetto committed
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 33 34 35
      def version_required
        @version_required ||= File.read(Rails.root.
                                        join('GITLAB_SHELL_VERSION')).strip
      end
36 37 38 39

      def strip_key(key)
        key.split(/ /)[0, 2].join(' ')
      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 83 84
    def import_repository(storage, name, url)
      output, status = Popen::popen([gitlab_shell_projects_path, 'import-project',
                                     storage, "#{name}.git", url, '900'])
85 86
      raise Error, output unless status.zero?
      true
87 88
    end

89
    # Move repository
90
    # storage - project's storage path
91 92 93 94
    # path - project path with namespace
    # new_path - new project path with namespace
    #
    # Ex.
95
    #   mv_repository("/path/to/storage", "gitlab/gitlab-ci", "randx/gitlab-ci-new")
96
    #
97
    def mv_repository(storage, path, new_path)
98
      Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'mv-project',
99
                                   storage, "#{path}.git", "#{new_path}.git"])
100 101
    end

102
    # Fork repository to new namespace
103
    # forked_from_storage - forked-from project's storage path
104
    # path - project path with namespace
105
    # forked_to_storage - forked-to project's storage path
106 107 108
    # fork_namespace - namespace for forked project
    #
    # Ex.
109
    #  fork_repository("/path/to/forked_from/storage", "gitlab/gitlab-ci", "/path/to/forked_to/storage", "randx")
110
    #
111
    def fork_repository(forked_from_storage, path, forked_to_storage, fork_namespace)
112
      Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'fork-project',
113 114
                                   forked_from_storage, "#{path}.git", forked_to_storage,
                                   fork_namespace])
115 116
    end

117
    # Remove repository from file system
118
    #
119
    # storage - project's storage path
120
    # name - project path with namespace
121 122
    #
    # Ex.
123
    #   remove_repository("/path/to/storage", "gitlab/gitlab-ci")
124
    #
125
    def remove_repository(storage, name)
126
      Gitlab::Utils.system_silent([gitlab_shell_projects_path,
127
                                   'rm-project', storage, "#{name}.git"])
128 129
    end

130 131
    # Gc repository
    #
132
    # storage - project storage path
133 134 135
    # path - project path with namespace
    #
    # Ex.
136
    #   gc("/path/to/storage", "gitlab/gitlab-ci")
137
    #
138
    def gc(storage, path)
139
      Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'gc',
140
                                   storage, "#{path}.git"])
141 142
    end

143
    # Add new key to gitlab-shell
144
    #
145
    # Ex.
146
    #   add_key("key-42", "sha-rsa ...")
147
    #
148
    def add_key(key_id, key_content)
149
      Gitlab::Utils.system_silent([gitlab_shell_keys_path,
150
                                   'add-key', key_id, self.class.strip_key(key_content)])
151 152
    end

153 154 155 156 157 158 159 160 161 162
    # 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|
        block.call(KeyAdder.new(io))
      end
    end

163
    # Remove ssh key from gitlab shell
164 165
    #
    # Ex.
166
    #   remove_key("key-342", "sha-rsa ...")
167
    #
168
    def remove_key(key_id, key_content)
169 170
      Gitlab::Utils.system_silent([gitlab_shell_keys_path,
                                   'rm-key', key_id, key_content])
171 172
    end

173 174 175
    # Remove all ssh keys from gitlab shell
    #
    # Ex.
Johannes Schleifenbaum's avatar
Johannes Schleifenbaum committed
176
    #   remove_all_keys
177 178
    #
    def remove_all_keys
179
      Gitlab::Utils.system_silent([gitlab_shell_keys_path, 'clear'])
180 181
    end

182 183 184
    # Add empty directory for storing repositories
    #
    # Ex.
185
    #   add_namespace("/path/to/storage", "gitlab")
186
    #
187 188
    def add_namespace(storage, name)
      FileUtils.mkdir(full_path(storage, name), mode: 0770) unless exists?(storage, name)
189 190 191 192 193 194
    end

    # Remove directory from repositories storage
    # Every repository inside this directory will be removed too
    #
    # Ex.
195
    #   rm_namespace("/path/to/storage", "gitlab")
196
    #
197 198
    def rm_namespace(storage, name)
      FileUtils.rm_r(full_path(storage, name), force: true)
199 200 201 202 203
    end

    # Move namespace directory inside repositories storage
    #
    # Ex.
204
    #   mv_namespace("/path/to/storage", "gitlab", "gitlabhq")
205
    #
206 207
    def mv_namespace(storage, old_name, new_name)
      return false if exists?(storage, new_name) || !exists?(storage, old_name)
208

209
      FileUtils.mv(full_path(storage, old_name), full_path(storage, new_name))
210 211
    end

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

216 217
    # Return GitLab shell version
    def version
218
      gitlab_shell_version_file = "#{gitlab_shell_path}/VERSION"
219 220

      if File.readable?(gitlab_shell_version_file)
221
        File.read(gitlab_shell_version_file).chomp
222 223 224
      end
    end

225 226 227
    # Check if such directory exists in repositories.
    #
    # Usage:
228 229
    #   exists?(storage, 'gitlab')
    #   exists?(storage, 'gitlab/cookies.git')
230
    #
231 232
    def exists?(storage, dir_name)
      File.exist?(full_path(storage, dir_name))
233 234
    end

235 236
    protected

237 238 239 240
    def gitlab_shell_path
      Gitlab.config.gitlab_shell.path
    end

241 242 243 244
    def gitlab_shell_user_home
      File.expand_path("~#{Gitlab.config.gitlab_shell.ssh_user}")
    end

245
    def full_path(storage, dir_name)
246 247
      raise ArgumentError.new("Directory name can't be blank") if dir_name.blank?

248
      File.join(storage, dir_name)
249 250
    end

251 252 253 254 255 256 257
    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
258 259
  end
end