This repository has been archived by the owner on May 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 119
/
queue.rb
96 lines (79 loc) · 2.74 KB
/
queue.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
class Job
# Encapsulates logic for figuring out which queue a given job needs to go
# into.
#
# Queue names for Job::Test instances are configured in `Travis.config` and
# are determined based on the repository slug (e.g. 'rails/rails' has its own
# queue) or the language given in the configuration (`.travis.yml`) and
# default to 'builds.linux'.
class Queue
SUDO_AND_SETUID_EXECUTABLES = %w(
sudo
ping
)
SUDO_DETECTION_REGEXP = /^[^#]*\b(#{SUDO_AND_SETUID_EXECUTABLES.join('|')})\b/
CUSTOM_STAGES = %w(
before_install
install
before_script
script
before_cache
after_success
after_failure
after_script
).map(&:to_sym)
class << self
def for(job)
queues.detect(lambda { default }) { |queue| queue.matches?(job) }
end
def queues
@queues ||= Array(Travis.config.queues).compact.map do |queue|
Queue.new(queue[:queue], queue.reject { |key, value| key == :queue })
end
end
def default
@default ||= Queue.new(Travis.config.default_queue, {})
end
def sudo_detected?(config)
config.values_at(*CUSTOM_STAGES).compact.flatten.any? do |s|
SUDO_DETECTION_REGEXP =~ s.to_s
end
end
end
attr_reader :name, :attrs
def initialize(name, attrs)
@name = name
@attrs = attrs
end
def matches?(job)
matchers = matchers_for(job)
unknown_matchers = @attrs.keys - matchers.keys
if unknown_matchers.length > 0
warn "unknown matchers used for queue #{name}: #{unknown_matchers.join(", ")}"
end
known_matchers = @attrs.keys & matchers.keys
known_matchers.length > 0 && known_matchers.all? { |key| matchers[key.to_sym] === @attrs[key] }
end
private
def matchers_for(job)
{
:slug => "#{job.repository.try(:owner_name)}/#{job.repository.try(:name)}",
:owner => job.repository.try(:owner_name),
:os => job.config[:os],
:language => Array(job.config[:language]).flatten.compact.first,
:sudo => job.config.fetch(:sudo) { !repo_is_default_docker?(job) },
:dist => job.config[:dist],
:osx_image => job.config[:osx_image],
}
end
def repo_is_default_docker?(job)
return true if Travis::Github::Education.education_queue?(job.repository.try(:owner))
return false unless Travis::Features.feature_active?(:docker_default_queue)
!self.class.sudo_detected?(job.config) && repo_created_after_docker_cutoff?(job.repository)
end
def repo_created_after_docker_cutoff?(repository)
return true if repository.created_at.nil?
repository.created_at > Time.parse(Travis.config.docker_default_queue_cutoff)
end
end
end