2012-10-25 18:10:40 -04:00
|
|
|
module Gitlab
|
|
|
|
module Satellite
|
|
|
|
class Action
|
2014-07-15 14:27:35 -04:00
|
|
|
DEFAULT_OPTIONS = { git_timeout: Gitlab.config.satellites.timeout.seconds }
|
2012-10-25 18:10:40 -04:00
|
|
|
|
2012-10-25 18:24:02 -04:00
|
|
|
attr_accessor :options, :project, :user
|
2012-10-25 18:10:40 -04:00
|
|
|
|
2012-10-25 18:26:47 -04:00
|
|
|
def initialize(user, project, options = {})
|
|
|
|
@options = DEFAULT_OPTIONS.merge(options)
|
2012-10-25 18:10:40 -04:00
|
|
|
@project = project
|
2012-10-25 18:24:02 -04:00
|
|
|
@user = user
|
2012-10-25 18:10:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
# * Sets a 30s timeout for Git
|
|
|
|
# * Locks the satellite repo
|
|
|
|
# * Yields the prepared satellite repo
|
|
|
|
def in_locked_and_timed_satellite
|
2013-02-14 08:17:43 -05:00
|
|
|
Gitlab::ShellEnv.set_env(user)
|
|
|
|
|
2012-10-25 18:10:40 -04:00
|
|
|
Grit::Git.with_timeout(options[:git_timeout]) do
|
2012-10-25 19:44:20 -04:00
|
|
|
project.satellite.lock do
|
|
|
|
return yield project.satellite.repo
|
2012-10-25 18:10:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue Errno::ENOMEM => ex
|
2013-06-06 17:22:36 -04:00
|
|
|
return handle_exception(ex)
|
2012-10-25 18:10:40 -04:00
|
|
|
rescue Grit::Git::GitTimeout => ex
|
2013-06-06 17:22:36 -04:00
|
|
|
return handle_exception(ex)
|
2013-02-14 08:17:43 -05:00
|
|
|
ensure
|
|
|
|
Gitlab::ShellEnv.reset_env
|
2012-10-25 18:10:40 -04:00
|
|
|
end
|
|
|
|
|
2013-04-25 10:15:33 -04:00
|
|
|
# * Recreates the satellite
|
2012-10-25 18:24:02 -04:00
|
|
|
# * Sets up Git variables for the user
|
|
|
|
#
|
|
|
|
# Note: use this within #in_locked_and_timed_satellite
|
|
|
|
def prepare_satellite!(repo)
|
2012-10-25 19:32:33 -04:00
|
|
|
project.satellite.clear_and_update!
|
2012-10-25 18:24:02 -04:00
|
|
|
|
2015-06-23 08:30:21 -04:00
|
|
|
if user
|
|
|
|
repo.config['user.name'] = user.name
|
|
|
|
repo.config['user.email'] = user.email
|
|
|
|
end
|
2013-04-25 10:15:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def default_options(options = {})
|
2015-02-02 23:36:54 -05:00
|
|
|
{ raise: true, timeout: true }.merge(options)
|
2012-10-25 18:24:02 -04:00
|
|
|
end
|
2013-06-06 17:22:36 -04:00
|
|
|
|
|
|
|
def handle_exception(exception)
|
|
|
|
Gitlab::GitLogger.error(exception.message)
|
|
|
|
false
|
|
|
|
end
|
2012-10-25 18:10:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|