summaryrefslogtreecommitdiff
path: root/lib/gitlab_projects.rb
blob: 49a1d25646a11ac4fb28dfc40932087a225c0a53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
require 'fileutils'
require 'tempfile'
require 'timeout'
require 'open3'

require_relative 'gitlab_config'
require_relative 'gitlab_logger'
require_relative 'gitlab_metrics'

class GitlabProjects
  GLOBAL_HOOKS_DIRECTORY = File.join(ROOT_PATH, 'hooks')

  # Project name is a directory name for repository with .git at the end
  # It may be namespaced or not. Like repo.git or gitlab/repo.git
  attr_reader :project_name

  # Absolute path to directory where repositories stored
  # By default it is /home/git/repositories
  attr_reader :repos_path

  # Full path is an absolute path to the repository
  # Ex /home/git/repositories/test.git
  attr_reader :full_path

  def self.create_hooks(path)
    local_hooks_directory = File.join(path, 'hooks')
    real_local_hooks_directory = :not_found
    begin
      real_local_hooks_directory = File.realpath(local_hooks_directory)
    rescue Errno::ENOENT
      # real_local_hooks_directory == :not_found
    end

    if real_local_hooks_directory != File.realpath(GLOBAL_HOOKS_DIRECTORY)
      if File.exist?(local_hooks_directory)
        $logger.info "Moving existing hooks directory and symlinking global hooks directory for #{path}."
        FileUtils.mv(local_hooks_directory, "#{local_hooks_directory}.old.#{Time.now.to_i}")
      end
      FileUtils.ln_sf(GLOBAL_HOOKS_DIRECTORY, local_hooks_directory)
    else
      $logger.info "Hooks already exist for #{path}."
      true
    end
  end

  def initialize
    @command = ARGV.shift
    @repos_path = ARGV.shift
    @project_name = ARGV.shift
    @full_path = File.join(@repos_path, @project_name) unless @project_name.nil?
  end

  def exec
    GitlabMetrics.measure("command-#{@command}") do
      case @command
      when 'create-tag';
        create_tag
      when 'add-project';
        add_project
      when 'list-projects';
        puts list_projects
      when 'rm-project';
        rm_project
      when 'mv-project';
        mv_project
      when 'mv-storage';
        mv_storage
      when 'import-project';
        import_project
      when 'fork-project';
        fork_project
      when 'fetch-remote';
        fetch_remote
      when 'push-branches';
        push_branches
      when 'delete-remote-branches';
        delete_remote_branches
      when 'list-remote-tags';
        list_remote_tags
      when 'gc';
        gc
      else
        $logger.warn "Attempt to execute invalid gitlab-projects command #{@command.inspect}."
        puts 'not allowed'
        false
      end
    end
  end

  protected

  def list_remote_tags
    remote_name = ARGV.shift

    tag_list, exit_code, error = nil
    cmd = %W(git --git-dir=#{full_path} ls-remote --tags #{remote_name})

    Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thr|
      tag_list  = stdout.read
      error     = stderr.read
      exit_code = wait_thr.value.exitstatus
    end

    if exit_code.zero?
      puts tag_list
      true
    else
      puts error
      false
    end
  end

  def push_branches
    remote_name = ARGV.shift

    # timeout for push
    timeout = (ARGV.shift || 120).to_i

    $logger.info "Pushing branches from #{full_path} to remote #{remote_name}: #{ARGV}"
    cmd = %W(git --git-dir=#{full_path} push -- #{remote_name}).concat(ARGV)
    pid = Process.spawn(*cmd)

    begin
      Timeout.timeout(timeout) do
        Process.wait(pid)
      end

      $?.exitstatus.zero?
    rescue => exception
      $logger.error "Pushing branches to remote #{remote_name} failed due to: #{exception.message}."

      Process.kill('KILL', pid)
      Process.wait
      false
    end
  end

  def delete_remote_branches
    remote_name = ARGV.shift
    branches = ARGV.map { |branch_name| ":#{branch_name}" }

    $logger.info "Pushing deleted branches from #{full_path} to remote #{remote_name}: #{ARGV}"
    cmd = %W(git --git-dir=#{full_path} push -- #{remote_name}).concat(branches)
    pid = Process.spawn(*cmd)

    begin
      Process.wait(pid)

      $?.exitstatus.zero?
    rescue => exception
      $logger.error "Pushing deleted branches to remote #{remote_name} failed due to: #{exception.message}"

      Process.kill('KILL', pid)
      Process.wait
      false
    end
  end

  def create_tag
    tag_name = ARGV.shift
    ref = ARGV.shift || "HEAD"
    cmd = %W(git --git-dir=#{full_path} tag)
    if ARGV.size > 0
      msg = ARGV.shift
      cmd += %W(-a -m #{msg})
    end
    cmd += %W(-- #{tag_name} #{ref})
    system(*cmd)
  end

  def add_project
    $logger.info "Adding project #{@project_name} at <#{full_path}>."
    FileUtils.mkdir_p(full_path, mode: 0770)
    cmd = %W(git --git-dir=#{full_path} init --bare)
    system(*cmd) && self.class.create_hooks(full_path)
  end

  def list_projects
    $logger.info 'Listing projects'
    Dir.chdir(repos_path) do
      next Dir.glob('**/*.git')
    end
  end

  def rm_project
    $logger.info "Removing project #{@project_name} from <#{full_path}>."
    FileUtils.rm_rf(full_path)
  end

  def mask_password_in_url(url)
    result = URI(url)
    result.password = "*****" unless result.password.nil?
    result.user = "*****" unless result.user.nil? #it's needed for oauth access_token
    result
  rescue
    url
  end

  def fetch_remote
    @name = ARGV.shift

    # timeout for fetch
    timeout = (ARGV.shift || 120).to_i

    # fetch with --force ?
    forced = ARGV.include?('--force')

    # fetch with --tags or --no-tags
    tags_option = ARGV.include?('--no-tags') ? '--no-tags' : '--tags'

    $logger.info "Fetching remote #{@name} for project #{@project_name}."
    cmd = %W(git --git-dir=#{full_path} fetch #{@name} --prune --quiet)
    cmd << '--force' if forced
    cmd << tags_option

    setup_ssh_auth do |env|
      pid = Process.spawn(env, *cmd)

      begin
        _, status = Timeout.timeout(timeout) do
          Process.wait2(pid)
        end

        status.success?
      rescue => exception
        $logger.error "Fetching remote #{@name} for project #{@project_name} failed due to: #{exception.message}."

        Process.kill('KILL', pid)
        Process.wait
        false
      end
    end
  end

  def remove_origin_in_repo
    cmd = %W(git --git-dir=#{full_path} remote rm origin)
    pid = Process.spawn(*cmd)
    Process.wait(pid)
  end

  # Import project via git clone --bare
  # URL must be publicly cloneable
  def import_project
    # Skip import if repo already exists
    return false if File.exists?(full_path)

    @source = ARGV.shift
    masked_source = mask_password_in_url(@source)

    # timeout for clone
    timeout = (ARGV.shift || 120).to_i
    $logger.info "Importing project #{@project_name} from <#{masked_source}> to <#{full_path}>."
    cmd = %W(git clone --bare -- #{@source} #{full_path})

    pid = Process.spawn(*cmd)

    begin
      Timeout.timeout(timeout) do
        Process.wait(pid)
      end

      return false unless $?.exitstatus.zero?
    rescue Timeout::Error
      $logger.error "Importing project #{@project_name} from <#{masked_source}> failed due to timeout."

      Process.kill('KILL', pid)
      Process.wait
      FileUtils.rm_rf(full_path)
      return false
    end

    self.class.create_hooks(full_path)
    # The project was imported successfully.
    # Remove the origin URL since it may contain password.
    remove_origin_in_repo

    true
  end

  # Move repository from one directory to another
  #
  # Ex.
  #  gitlab.git -> gitlabhq.git
  #  gitlab/gitlab-ci.git -> randx/six.git
  #
  # Wont work if target namespace directory does not exist
  #
  def mv_project
    new_path = ARGV.shift

    unless new_path
      $logger.error "mv-project failed: no destination path provided."
      return false
    end

    new_full_path = File.join(repos_path, new_path)

    # verify that the source repo exists
    unless File.exists?(full_path)
      $logger.error "mv-project failed: source path <#{full_path}> does not exist."
      return false
    end

    # ...and that the target repo does not exist
    if File.exists?(new_full_path)
      $logger.error "mv-project failed: destination path <#{new_full_path}> already exists."
      return false
    end

    $logger.info "Moving project #{@project_name} from <#{full_path}> to <#{new_full_path}>."
    FileUtils.mv(full_path, new_full_path)
  end

  # Move repository from one storage path to another
  #
  # Wont work if target namespace directory does not exist in the new storage path
  #
  def mv_storage
    new_storage = ARGV.shift

    unless new_storage
      $logger.error "mv-storage failed: no destination storage path provided."
      return false
    end

    new_full_path = File.join(new_storage, project_name)

    # verify that the source repo exists
    unless File.exists?(full_path)
      $logger.error "mv-storage failed: source path <#{full_path}> does not exist."
      return false
    end

    # Make sure the destination directory exists
    FileUtils.mkdir_p(new_full_path)

    # Make sure the source path ends with a slash so that rsync copies the
    # contents of the directory, as opposed to copying the directory by name
    source_path = File.join(full_path, '')

    if wait_for_pushes
      $logger.info "Syncing project #{@project_name} from <#{full_path}> to <#{new_full_path}>."

      # Set a low IO priority with ionice to not choke the server on moves
      if rsync(source_path, new_full_path, 'ionice -c2 -n7 rsync')
        true
      else
        # If the command fails with `ionice` (maybe because we're on a OS X
        # development machine), try again without `ionice`.
        rsync(source_path, new_full_path)
      end
    else
      $logger.error "mv-storage failed: source path <#{full_path}> is waiting for pushes to finish."
      false
    end
  end

  def fork_project
    destination_repos_path = ARGV.shift

    unless destination_repos_path
      $logger.error "fork-project failed: no destination repository path provided."
      return false
    end

    new_namespace = ARGV.shift

    # destination namespace must be provided
    unless new_namespace
      $logger.error "fork-project failed: no destination namespace provided."
      return false
    end

    # destination namespace must exist
    namespaced_path = File.join(destination_repos_path, new_namespace)
    unless File.exists?(namespaced_path)
      $logger.error "fork-project failed: destination namespace <#{namespaced_path}> does not exist."
      return false
    end

    # a project of the same name cannot already be within the destination namespace
    full_destination_path = File.join(namespaced_path, project_name.split('/')[-1])
    if File.exists?(full_destination_path)
      $logger.error "fork-project failed: destination repository <#{full_destination_path}> already exists."
      return false
    end

    $logger.info "Forking project from <#{full_path}> to <#{full_destination_path}>."
    cmd = %W(git clone --bare -- #{full_path} #{full_destination_path})
    system(*cmd) && self.class.create_hooks(full_destination_path)
  end

  def gc
    $logger.info "Running git gc for <#{full_path}>."
    unless File.exists?(full_path)
      $logger.error "gc failed: destination path <#{full_path}> does not exist."
      return false
    end
    cmd = %W(git --git-dir=#{full_path} gc)
    system(*cmd)
  end

  def wait_for_pushes
    # Try for 30 seconds, polling every 10
    3.times do
      return true if gitlab_reference_counter.value == 0
      sleep 10
    end

    false
  end

  # Builds a small shell script that can be used to execute SSH with a set of
  # custom options.
  #
  # Options are expanded as `'-oKey="Value"'`, so SSH will correctly interpret
  # paths with spaces in them. We trust the user not to embed single or double
  # quotes in the key or value.
  def custom_ssh_script(options = {})
    args = options.map { |k, v| "'-o#{k}=\"#{v}\"'" }.join(' ')

    [
      "#!/bin/sh",
      "exec ssh #{args} \"$@\""
    ].join("\n")
  end

  # Known hosts data and private keys can be passed to gitlab-shell in the
  # environment. If present, this method puts them into temporary files, writes
  # a script that can substitute as `ssh`, setting the options to respect those
  # files, and yields: { "GIT_SSH" => "/tmp/myScript" }
  def setup_ssh_auth
    options = {}

    if ENV.key?('GITLAB_SHELL_SSH_KEY')
      key_file = Tempfile.new('gitlab-shell-key-file')
      key_file.chmod(0o400)
      key_file.write(ENV['GITLAB_SHELL_SSH_KEY'])
      key_file.close

      options['IdentityFile'] = key_file.path
      options['IdentitiesOnly'] = 'yes'
    end

    if ENV.key?('GITLAB_SHELL_KNOWN_HOSTS')
      known_hosts_file = Tempfile.new('gitlab-shell-known-hosts')
      known_hosts_file.chmod(0o400)
      known_hosts_file.write(ENV['GITLAB_SHELL_KNOWN_HOSTS'])
      known_hosts_file.close

      options['StrictHostKeyChecking'] = 'yes'
      options['UserKnownHostsFile'] = known_hosts_file.path
    end

    return yield({}) if options.empty?

    script = Tempfile.new('gitlab-shell-ssh-wrapper')
    script.chmod(0o755)
    script.write(custom_ssh_script(options))
    script.close

    yield('GIT_SSH' => script.path)
  ensure
    key_file.close! unless key_file.nil?
    known_hosts_file.close! unless known_hosts_file.nil?
    script.close! unless script.nil?
  end

  def gitlab_reference_counter
    @gitlab_reference_counter ||= begin
      # Defer loading because this pulls in gitlab_net, which takes 100-200 ms
      # to load
      require_relative 'gitlab_reference_counter'
      GitlabReferenceCounter.new(full_path)
    end
  end

  def rsync(src, dest, rsync_path = 'rsync')
    command = rsync_path.split + %W(-a --delete --rsync-path="#{rsync_path}" #{src} #{dest})
    system(*command)
  end
end