2 class SatelliteNotExistError < StandardError; end
8 PARKING_BRANCH = "__parking_branch"
10 attr_accessor :project
12 def initialize(project)
17 Gitlab::Satellite::Logger.error(message)
20 def raise_no_satellite
21 raise SatelliteNotExistError.new("Satellite doesn't exist")
25 raise_no_satellite unless exists?
36 output, status = popen("git clone #{project.repository.path_to_repo} #{path}",
37 Gitlab.config.satellites.path)
39 log("PID: #{project.id}: git clone #{project.repository.path_to_repo} #{path}")
40 log("PID: #{project.id}: -> #{output}")
45 log("Failed to create satellite for #{project.name_with_namespace}")
54 # * Locks the satellite
55 # * Changes the current directory to the satellite's working dir
58 raise_no_satellite unless exists?
60 File.open(lock_file, "w+") do |f|
63 Dir.chdir(path) { return yield }
71 create_locks_dir unless File.exists?(lock_files_dir)
72 File.join(lock_files_dir, "satellite_#{project.id}.lock")
76 File.join(Gitlab.config.satellites.path, project.path_with_namespace)
80 raise_no_satellite unless exists?
82 @repo ||= Grit::Repo.new(path)
91 # Clear the working directory
92 def clear_working_dir!
93 repo.git.reset(hard: true)
96 # Deletes all branches except the parking branch
98 # This ensures we have no name clashes or issues updating branches when
99 # working with the satellite.
101 heads = repo.heads.map(&:name)
103 # update or create the parking branch
104 if heads.include? PARKING_BRANCH
105 repo.git.checkout({}, PARKING_BRANCH)
107 repo.git.checkout(default_options({b: true}), PARKING_BRANCH)
110 # remove the parking branch from the list of heads ...
111 heads.delete(PARKING_BRANCH)
112 # ... and delete all others
113 heads.each { |head| repo.git.branch(default_options({D: true}), head) }
116 # Deletes all remotes except origin
118 # This ensures we have no remote name clashes or issues updating branches when
119 # working with the satellite.
121 remotes = repo.git.remote.split(' ')
122 remotes.delete('origin')
123 remotes.each { |name| repo.git.remote(default_options,'rm', name)}
126 # Updates the satellite from Gitolite
128 # Note: this will only update remote branches (i.e. origin/*)
129 def update_from_source!
130 repo.git.fetch(default_options, :origin)
133 def default_options(options = {})
134 {raise: true, timeout: true}.merge(options)
137 # Create directory for storing
138 # satellites lock files
140 FileUtils.mkdir_p(lock_files_dir)
144 @lock_files_dir ||= File.join(Gitlab.config.satellites.path, "tmp")