satellite.rb 3.7 KB
Newer Older
1
module Gitlab
2 3 4 5 6
  class SatelliteNotExistError < StandardError
    def initialize(msg = "Satellite doesn't exist")
      super
    end
  end
7

8 9
  module Satellite
    class Satellite
10 11
      include Gitlab::Popen

12 13 14 15 16 17 18 19
      PARKING_BRANCH = "__parking_branch"

      attr_accessor :project

      def initialize(project)
        @project = project
      end

D
Dmitriy Zaporozhets 已提交
20 21 22 23
      def log message
        Gitlab::Satellite::Logger.error(message)
      end

24
      def clear_and_update!
25
        project.ensure_satellite_exists
26

I
Izaak Alpert 已提交
27
        @repo = nil
28
        clear_working_dir!
M
murank 已提交
29
        delete_heads!
I
Izaak Alpert 已提交
30
        remove_remotes!
31
        update_from_source!
32 33 34
      end

      def create
35
        output, status = popen(%W(git clone -- #{project.repository.path_to_repo} #{path}),
36 37
                               Gitlab.config.satellites.path)

38
        log("PID: #{project.id}: git clone #{project.repository.path_to_repo} #{path}")
D
Dmitriy Zaporozhets 已提交
39 40
        log("PID: #{project.id}: -> #{output}")

41
        if status.zero?
42 43
          true
        else
D
Dmitriy Zaporozhets 已提交
44
          log("Failed to create satellite for #{project.name_with_namespace}")
45 46
          false
        end
47 48 49 50 51 52
      end

      def exists?
        File.exists? path
      end

R
Riyad Preukschas 已提交
53 54 55
      # * Locks the satellite
      # * Changes the current directory to the satellite's working dir
      # * Yields
56
      def lock
57
        project.ensure_satellite_exists
58 59

        File.open(lock_file, "w+") do |f|
I
Izaak Alpert 已提交
60 61 62 63 64
          begin
            f.flock File::LOCK_EX
            Dir.chdir(path) { return yield }
          ensure
            f.flock File::LOCK_UN
R
Riyad Preukschas 已提交
65
          end
66 67 68 69
        end
      end

      def lock_file
70 71
        create_locks_dir unless File.exists?(lock_files_dir)
        File.join(lock_files_dir, "satellite_#{project.id}.lock")
72 73
      end

74
      def path
75
        File.join(Gitlab.config.satellites.path, project.path_with_namespace)
76
      end
77

78
      def repo
79
        project.ensure_satellite_exists
80 81 82

        @repo ||= Grit::Repo.new(path)
      end
83 84 85 86

      def destroy
        FileUtils.rm_rf(path)
      end
87

88 89 90 91 92 93 94 95 96 97 98 99
      private

      # Clear the working directory
      def clear_working_dir!
        repo.git.reset(hard: true)
      end

      # Deletes all branches except the parking branch
      #
      # This ensures we have no name clashes or issues updating branches when
      # working with the satellite.
      def delete_heads!
100
        heads = repo.heads.map(&:name)
101 102 103 104 105

        # update or create the parking branch
        if heads.include? PARKING_BRANCH
          repo.git.checkout({}, PARKING_BRANCH)
        else
I
Izaak Alpert 已提交
106
          repo.git.checkout(default_options({b: true}), PARKING_BRANCH)
107 108 109 110 111
        end

        # remove the parking branch from the list of heads ...
        heads.delete(PARKING_BRANCH)
        # ... and delete all others
I
Izaak Alpert 已提交
112 113 114 115 116 117 118 119 120 121 122
        heads.each { |head| repo.git.branch(default_options({D: true}), head) }
      end

      # Deletes all remotes except origin
      #
      # This ensures we have no remote name clashes or issues updating branches when
      # working with the satellite.
      def remove_remotes!
        remotes = repo.git.remote.split(' ')
        remotes.delete('origin')
        remotes.each { |name| repo.git.remote(default_options,'rm', name)}
123 124
      end

D
Dmitriy Zaporozhets 已提交
125
      # Updates the satellite from bare repo
126 127 128
      #
      # Note: this will only update remote branches (i.e. origin/*)
      def update_from_source!
I
Izaak Alpert 已提交
129 130 131 132 133
        repo.git.fetch(default_options, :origin)
      end

      def default_options(options = {})
        {raise: true, timeout: true}.merge(options)
134
      end
135

J
Johannes Schleifenbaum 已提交
136
      # Create directory for storing
137 138 139 140 141 142 143 144
      # satellites lock files
      def create_locks_dir
        FileUtils.mkdir_p(lock_files_dir)
      end

      def lock_files_dir
        @lock_files_dir ||= File.join(Gitlab.config.satellites.path, "tmp")
      end
145 146 147
    end
  end
end