satellite.rb 3.81 KB
Newer Older
1 2
module Gitlab
  module Satellite
3 4 5 6
    class CheckoutFailed < StandardError; end
    class CommitFailed < StandardError; end
    class PushFailed < StandardError; end

7
    class Satellite
8 9
      include Gitlab::Popen

10 11 12 13 14 15 16 17
      PARKING_BRANCH = "__parking_branch"

      attr_accessor :project

      def initialize(project)
        @project = project
      end

18
      def log(message)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
19 20 21
        Gitlab::Satellite::Logger.error(message)
      end

22
      def clear_and_update!
23
        project.ensure_satellite_exists
24

25
        @repo = nil
26
        clear_working_dir!
27
        delete_heads!
28
        remove_remotes!
29
        update_from_source!
30 31 32
      end

      def create
33
        output, status = popen(%W(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}")
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
37 38
        log("PID: #{project.id}: -> #{output}")

39
        if status.zero?
40 41
          true
        else
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
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

Riyad Preukschas's avatar
Riyad Preukschas committed
51 52 53
      # * Locks the satellite
      # * Changes the current directory to the satellite's working dir
      # * Yields
54
      def lock
55
        project.ensure_satellite_exists
56 57

        File.open(lock_file, "w+") do |f|
58 59
          begin
            f.flock File::LOCK_EX
60
            yield
61 62
          ensure
            f.flock File::LOCK_UN
Riyad Preukschas's avatar
Riyad Preukschas committed
63
          end
64 65 66 67
        end
      end

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

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

76
      def repo
77
        project.ensure_satellite_exists
78 79 80

        @repo ||= Grit::Repo.new(path)
      end
81 82 83 84

      def destroy
        FileUtils.rm_rf(path)
      end
85

86 87 88 89 90
      private

      # Clear the working directory
      def clear_working_dir!
        repo.git.reset(hard: true)
91
        repo.git.clean(f: true, d: true, x: true)
92 93 94 95 96 97 98
      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!
99
        heads = repo.heads.map(&:name)
100 101 102 103 104

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

        # remove the parking branch from the list of heads ...
        heads.delete(PARKING_BRANCH)
        # ... and delete all others
111
        heads.each { |head| repo.git.branch(default_options({ D: true }), head) }
112 113 114 115 116 117 118 119 120 121
      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)}
122 123
      end

124
      # Updates the satellite from bare repo
125 126 127
      #
      # Note: this will only update remote branches (i.e. origin/*)
      def update_from_source!
128
        repo.git.remote(default_options, 'set-url', :origin, project.repository.path_to_repo)
129 130 131 132
        repo.git.fetch(default_options, :origin)
      end

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

Johannes Schleifenbaum's avatar
Johannes Schleifenbaum committed
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