BigW Consortium Gitlab

shell.rb 7.31 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 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
    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',
Douwe Maan committed
86
                                    storage, "#{name}.git", url, '800'])
87 88
      raise Error, output unless status.zero?
      true
89 90
    end

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

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

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

132
    # Add new key to gitlab-shell
133
    #
134
    # Ex.
135
    #   add_key("key-42", "sha-rsa ...")
136
    #
137
    def add_key(key_id, key_content)
138
      Gitlab::Utils.system_silent([gitlab_shell_keys_path,
139
                                   'add-key', key_id, self.class.strip_key(key_content)])
140 141
    end

142 143 144 145 146 147
    # 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|
148
        yield(KeyAdder.new(io))
149 150 151
      end
    end

152
    # Remove ssh key from gitlab shell
153 154
    #
    # Ex.
155
    #   remove_key("key-342", "sha-rsa ...")
156
    #
157
    def remove_key(key_id, key_content)
158 159
      Gitlab::Utils.system_silent([gitlab_shell_keys_path,
                                   'rm-key', key_id, key_content])
160 161
    end

162 163 164
    # Remove all ssh keys from gitlab shell
    #
    # Ex.
Johannes Schleifenbaum committed
165
    #   remove_all_keys
166 167
    #
    def remove_all_keys
168
      Gitlab::Utils.system_silent([gitlab_shell_keys_path, 'clear'])
169 170
    end

171 172 173
    # Add empty directory for storing repositories
    #
    # Ex.
174
    #   add_namespace("/path/to/storage", "gitlab")
175
    #
176
    def add_namespace(storage, name)
177
      FileUtils.mkdir_p(full_path(storage, name), mode: 0770) unless exists?(storage, name)
178 179 180 181 182 183
    end

    # Remove directory from repositories storage
    # Every repository inside this directory will be removed too
    #
    # Ex.
184
    #   rm_namespace("/path/to/storage", "gitlab")
185
    #
186 187
    def rm_namespace(storage, name)
      FileUtils.rm_r(full_path(storage, name), force: true)
188 189 190 191 192
    end

    # Move namespace directory inside repositories storage
    #
    # Ex.
193
    #   mv_namespace("/path/to/storage", "gitlab", "gitlabhq")
194
    #
195 196
    def mv_namespace(storage, old_name, new_name)
      return false if exists?(storage, new_name) || !exists?(storage, old_name)
197

198
      FileUtils.mv(full_path(storage, old_name), full_path(storage, new_name))
199 200
    end

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

205 206
    # Return GitLab shell version
    def version
207
      gitlab_shell_version_file = "#{gitlab_shell_path}/VERSION"
208 209

      if File.readable?(gitlab_shell_version_file)
210
        File.read(gitlab_shell_version_file).chomp
211 212 213
      end
    end

214 215 216
    # Check if such directory exists in repositories.
    #
    # Usage:
217 218
    #   exists?(storage, 'gitlab')
    #   exists?(storage, 'gitlab/cookies.git')
219
    #
220 221
    def exists?(storage, dir_name)
      File.exist?(full_path(storage, dir_name))
222 223
    end

224 225
    protected

226 227 228 229
    def gitlab_shell_path
      Gitlab.config.gitlab_shell.path
    end

230 231 232 233
    def gitlab_shell_user_home
      File.expand_path("~#{Gitlab.config.gitlab_shell.ssh_user}")
    end

234
    def full_path(storage, dir_name)
235 236
      raise ArgumentError.new("Directory name can't be blank") if dir_name.blank?

237
      File.join(storage, dir_name)
238 239
    end

240 241 242 243 244 245 246
    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
247 248
  end
end