satellite.rb 2.8 KB
Newer Older
1
module Gitlab
2 3
  class SatelliteNotExistError < StandardError; end

4 5
  module Satellite
    class Satellite
6 7
      include Gitlab::Popen

8 9 10 11 12 13 14 15
      PARKING_BRANCH = "__parking_branch"

      attr_accessor :project

      def initialize(project)
        @project = project
      end

D
Dmitriy Zaporozhets 已提交
16 17 18 19
      def log message
        Gitlab::Satellite::Logger.error(message)
      end

20 21 22 23
      def raise_no_satellite
        raise SatelliteNotExistError.new("Satellite doesn't exist")
      end

24
      def clear_and_update!
25
        raise_no_satellite unless exists?
26

27
        clear_working_dir!
M
murank 已提交
28
        delete_heads!
29
        update_from_source!
30 31 32
      end

      def create
33
        output, status = popen("git clone #{project.repository.path_to_repo} #{path}",
34 35
                               Gitlab.config.satellites.path)

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

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

      def exists?
        File.exists? path
      end

R
Riyad Preukschas 已提交
51 52 53
      # * Locks the satellite
      # * Changes the current directory to the satellite's working dir
      # * Yields
54
      def lock
55
        raise_no_satellite unless exists?
56 57 58 59

        File.open(lock_file, "w+") do |f|
          f.flock(File::LOCK_EX)

R
Riyad Preukschas 已提交
60 61 62
          Dir.chdir(path) do
            return yield
          end
63 64 65 66
        end
      end

      def lock_file
D
Dmitriy Zaporozhets 已提交
67
        Rails.root.join("tmp", "satellite_#{project.id}.lock")
68 69
      end

70
      def path
71
        File.join(Gitlab.config.satellites.path, project.path_with_namespace)
72
      end
73

74
      def repo
75
        raise_no_satellite unless exists?
76 77 78

        @repo ||= Grit::Repo.new(path)
      end
79 80 81 82

      def destroy
        FileUtils.rm_rf(path)
      end
83

84 85 86 87 88 89 90 91 92 93 94 95
      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!
96
        heads = repo.heads.map(&:name)
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

        # update or create the parking branch
        if heads.include? PARKING_BRANCH
          repo.git.checkout({}, PARKING_BRANCH)
        else
          repo.git.checkout({b: true}, PARKING_BRANCH)
        end

        # remove the parking branch from the list of heads ...
        heads.delete(PARKING_BRANCH)
        # ... and delete all others
        heads.each { |head| repo.git.branch({D: true}, head) }
      end

      # Updates the satellite from Gitolite
      #
      # Note: this will only update remote branches (i.e. origin/*)
      def update_from_source!
115
        repo.git.fetch({timeout: true}, :origin)
116
      end
117 118 119
    end
  end
end