BigW Consortium Gitlab

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

3
module Gitlab
4
  class Shell
5
    class Error < StandardError; end
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 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
    # Add new key to gitlab-shell
131
    #
132
    # Ex.
133
    #   add_key("key-42", "sha-rsa ...")
134
    #
135
    def add_key(key_id, key_content)
136
      Gitlab::Utils.system_silent([gitlab_shell_keys_path,
137
                                   'add-key', key_id, self.class.strip_key(key_content)])
138 139
    end

140 141 142 143 144 145 146 147 148 149
    # 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

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

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

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

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

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

196
      FileUtils.mv(full_path(storage, old_name), full_path(storage, new_name))
197 198
    end

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

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

      if File.readable?(gitlab_shell_version_file)
208
        File.read(gitlab_shell_version_file).chomp
209 210 211
      end
    end

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

222 223
    protected

224 225 226 227
    def gitlab_shell_path
      Gitlab.config.gitlab_shell.path
    end

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

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

235
      File.join(storage, dir_name)
236 237
    end

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