2012-10-25 18:56:24 -04:00
|
|
|
module Gitlab
|
|
|
|
module Satellite
|
2015-04-09 13:57:10 -04:00
|
|
|
autoload :DeleteFileAction, 'gitlab/satellite/files/delete_file_action'
|
|
|
|
autoload :EditFileAction, 'gitlab/satellite/files/edit_file_action'
|
|
|
|
autoload :FileAction, 'gitlab/satellite/files/file_action'
|
|
|
|
autoload :NewFileAction, 'gitlab/satellite/files/new_file_action'
|
|
|
|
|
2015-02-22 18:01:49 -05:00
|
|
|
class CheckoutFailed < StandardError; end
|
|
|
|
class CommitFailed < StandardError; end
|
|
|
|
class PushFailed < StandardError; end
|
|
|
|
|
2012-10-25 18:56:24 -04:00
|
|
|
class Satellite
|
2013-01-28 10:22:45 -05:00
|
|
|
include Gitlab::Popen
|
|
|
|
|
2012-10-25 18:56:24 -04:00
|
|
|
PARKING_BRANCH = "__parking_branch"
|
|
|
|
|
|
|
|
attr_accessor :project
|
|
|
|
|
|
|
|
def initialize(project)
|
|
|
|
@project = project
|
|
|
|
end
|
|
|
|
|
2014-09-25 18:07:40 -04:00
|
|
|
def log(message)
|
2013-01-29 05:12:24 -05:00
|
|
|
Gitlab::Satellite::Logger.error(message)
|
|
|
|
end
|
|
|
|
|
2012-10-25 19:32:33 -04:00
|
|
|
def clear_and_update!
|
2014-04-09 11:13:44 -04:00
|
|
|
project.ensure_satellite_exists
|
2012-10-25 19:44:20 -04:00
|
|
|
|
2013-04-25 10:15:33 -04:00
|
|
|
@repo = nil
|
2012-10-25 19:32:33 -04:00
|
|
|
clear_working_dir!
|
2013-03-17 10:50:20 -04:00
|
|
|
delete_heads!
|
2013-04-25 10:15:33 -04:00
|
|
|
remove_remotes!
|
2012-10-25 19:32:33 -04:00
|
|
|
update_from_source!
|
2012-10-25 18:56:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2014-02-25 06:00:33 -05:00
|
|
|
output, status = popen(%W(git clone -- #{project.repository.path_to_repo} #{path}),
|
2013-01-28 10:22:45 -05:00
|
|
|
Gitlab.config.satellites.path)
|
|
|
|
|
2013-02-04 09:19:37 -05:00
|
|
|
log("PID: #{project.id}: git clone #{project.repository.path_to_repo} #{path}")
|
2013-01-29 05:12:24 -05:00
|
|
|
log("PID: #{project.id}: -> #{output}")
|
|
|
|
|
2013-01-28 10:22:45 -05:00
|
|
|
if status.zero?
|
2012-12-22 16:45:22 -05:00
|
|
|
true
|
|
|
|
else
|
2013-01-29 05:12:24 -05:00
|
|
|
log("Failed to create satellite for #{project.name_with_namespace}")
|
2012-12-22 16:45:22 -05:00
|
|
|
false
|
|
|
|
end
|
2012-10-25 18:56:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def exists?
|
|
|
|
File.exists? path
|
|
|
|
end
|
|
|
|
|
2012-10-25 20:32:17 -04:00
|
|
|
# * Locks the satellite
|
|
|
|
# * Changes the current directory to the satellite's working dir
|
|
|
|
# * Yields
|
2012-10-25 19:44:20 -04:00
|
|
|
def lock
|
2014-04-09 11:13:44 -04:00
|
|
|
project.ensure_satellite_exists
|
2012-10-25 19:44:20 -04:00
|
|
|
|
|
|
|
File.open(lock_file, "w+") do |f|
|
2013-04-25 10:15:33 -04:00
|
|
|
begin
|
|
|
|
f.flock File::LOCK_EX
|
2014-07-14 06:15:48 -04:00
|
|
|
yield
|
2013-04-25 10:15:33 -04:00
|
|
|
ensure
|
|
|
|
f.flock File::LOCK_UN
|
2012-10-25 20:32:17 -04:00
|
|
|
end
|
2012-10-25 19:44:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def lock_file
|
2013-06-26 11:35:32 -04:00
|
|
|
create_locks_dir unless File.exists?(lock_files_dir)
|
|
|
|
File.join(lock_files_dir, "satellite_#{project.id}.lock")
|
2012-10-25 19:44:20 -04:00
|
|
|
end
|
|
|
|
|
2012-10-25 18:56:24 -04:00
|
|
|
def path
|
2013-01-16 06:30:31 -05:00
|
|
|
File.join(Gitlab.config.satellites.path, project.path_with_namespace)
|
2012-10-25 18:56:24 -04:00
|
|
|
end
|
2012-10-25 19:32:33 -04:00
|
|
|
|
2012-10-25 19:44:20 -04:00
|
|
|
def repo
|
2014-04-09 11:13:44 -04:00
|
|
|
project.ensure_satellite_exists
|
2012-10-25 19:44:20 -04:00
|
|
|
|
|
|
|
@repo ||= Grit::Repo.new(path)
|
|
|
|
end
|
2013-01-28 10:22:45 -05:00
|
|
|
|
|
|
|
def destroy
|
|
|
|
FileUtils.rm_rf(path)
|
|
|
|
end
|
2012-10-25 19:44:20 -04:00
|
|
|
|
2012-10-25 19:32:33 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
# Clear the working directory
|
|
|
|
def clear_working_dir!
|
|
|
|
repo.git.reset(hard: true)
|
2014-05-05 07:21:00 -04:00
|
|
|
repo.git.clean(f: true, d: true, x: true)
|
2012-10-25 19:32:33 -04:00
|
|
|
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!
|
2012-10-25 19:44:20 -04:00
|
|
|
heads = repo.heads.map(&:name)
|
2012-10-25 19:32:33 -04:00
|
|
|
|
|
|
|
# update or create the parking branch
|
2015-03-25 08:05:06 -04:00
|
|
|
repo.git.checkout(default_options({ B: true }), PARKING_BRANCH)
|
2012-10-25 19:32:33 -04:00
|
|
|
|
|
|
|
# remove the parking branch from the list of heads ...
|
|
|
|
heads.delete(PARKING_BRANCH)
|
|
|
|
# ... and delete all others
|
2015-02-02 23:36:54 -05:00
|
|
|
heads.each { |head| repo.git.branch(default_options({ D: true }), head) }
|
2013-04-25 10:15:33 -04:00
|
|
|
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)}
|
2012-10-25 19:32:33 -04:00
|
|
|
end
|
|
|
|
|
2013-11-19 08:41:11 -05:00
|
|
|
# Updates the satellite from bare repo
|
2012-10-25 19:32:33 -04:00
|
|
|
#
|
|
|
|
# Note: this will only update remote branches (i.e. origin/*)
|
|
|
|
def update_from_source!
|
2014-08-22 08:32:04 -04:00
|
|
|
repo.git.remote(default_options, 'set-url', :origin, project.repository.path_to_repo)
|
2013-04-25 10:15:33 -04:00
|
|
|
repo.git.fetch(default_options, :origin)
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_options(options = {})
|
2015-02-02 23:36:54 -05:00
|
|
|
{ raise: true, timeout: true }.merge(options)
|
2012-10-25 19:32:33 -04:00
|
|
|
end
|
2013-06-26 11:35:32 -04:00
|
|
|
|
2013-07-29 06:46:00 -04:00
|
|
|
# Create directory for storing
|
2013-06-26 11:35:32 -04:00
|
|
|
# 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
|
2012-10-25 18:56:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|