Class: Concourse
- Inherits:
-
Object
show all
- Includes:
- Util, Rake::DSL
- Defined in:
- lib/concourse.rb,
lib/concourse/util.rb,
lib/concourse/version.rb,
lib/concourse/pipeline.rb
Defined Under Namespace
Modules: Util
Classes: Pipeline
Constant Summary
collapse
- RUBIES =
these numbers/names align with public docker image names
{
mri: %w[2.4 2.5 2.6 2.7 3.0-rc], jruby: %w[9.2], rbx: %w[latest], windows: %w[2.3 2.4 2.5 2.6], truffle: %w[stable nightly] }
- DEFAULT_DIRECTORY =
"concourse"
- DEFAULT_FLY_TARGET =
"default"
- DEFAULT_SECRETS =
"private.yml"
- CONCOURSE_DOCKER_COMPOSE =
"docker-compose.yml"
- VERSION =
"0.38.0"
Constants included
from Util
Util::GITATTRIBUTES_FILE, Util::GITIGNORE_FILE
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Util
#docker_compose, #docker_compose_path, #each_job, #each_task, #ensure_in_gitignore, #erbify_file, #find_task, #fly, #note, #running, #sh
Constructor Details
#initialize(project_name, options = {}, &block) ⇒ Concourse
Returns a new instance of Concourse.
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/concourse.rb', line 57
def initialize(project_name, options = {}, &block)
@project_name = project_name
@directory = options[:directory] || DEFAULT_DIRECTORY
@fly_target = options[:fly_target] || DEFAULT_FLY_TARGET
@format = options.has_key?(:format) ? options[:format] : false
@fly_args = options.keys.grep(/^fly_args_/).inject({}) do |hash, key|
fly_command = key.to_s.gsub(/^fly_args_/, "").gsub("_", "-")
hash[fly_command] = options[key]
hash
end
base_secrets_filename = options[:secrets_filename] || DEFAULT_SECRETS
@secrets_filename = File.join(@directory, base_secrets_filename)
@pipelines = []
if block
block.call(self)
create_tasks!
else
add_pipeline(@project_name, (options[:pipeline_erb_filename] || "#{project_name}.yml"))
end
end
|
Instance Attribute Details
#directory ⇒ Object
Returns the value of attribute directory.
26
27
28
|
# File 'lib/concourse.rb', line 26
def directory
@directory
end
|
#fly_args ⇒ Object
Returns the value of attribute fly_args.
29
30
31
|
# File 'lib/concourse.rb', line 29
def fly_args
@fly_args
end
|
#fly_target ⇒ Object
Returns the value of attribute fly_target.
28
29
30
|
# File 'lib/concourse.rb', line 28
def fly_target
@fly_target
end
|
Returns the value of attribute format.
31
32
33
|
# File 'lib/concourse.rb', line 31
def format
@format
end
|
#pipelines ⇒ Object
Returns the value of attribute pipelines.
27
28
29
|
# File 'lib/concourse.rb', line 27
def pipelines
@pipelines
end
|
#project_name ⇒ Object
Returns the value of attribute project_name.
25
26
27
|
# File 'lib/concourse.rb', line 25
def project_name
@project_name
end
|
#secrets_filename ⇒ Object
Returns the value of attribute secrets_filename.
30
31
32
|
# File 'lib/concourse.rb', line 30
def secrets_filename
@secrets_filename
end
|
Class Method Details
.default_execute_args(task) ⇒ Object
41
42
43
44
45
46
47
|
# File 'lib/concourse.rb', line 41
def self.default_execute_args(task)
args = []
task["config"]["inputs"].each do |input|
args << "--input=#{input["name"]}=."
end
args.join(" ")
end
|
.production_rubies ⇒ Object
49
50
51
|
# File 'lib/concourse.rb', line 49
def self.production_rubies
RUBIES[:mri].reject { |r| r =~ /rc/ }
end
|
.rc_rubies ⇒ Object
53
54
55
|
# File 'lib/concourse.rb', line 53
def self.rc_rubies
RUBIES[:mri].select { |r| r =~ /rc/ }
end
|
.url_for(fly_target) ⇒ Object
35
36
37
38
39
|
# File 'lib/concourse.rb', line 35
def self.url_for(fly_target)
matching_line = `fly targets`.split("\n").grep(/^#{fly_target}/).first
raise "invalid fly target #{fly_target}" unless matching_line
matching_line.split(/ +/)[1]
end
|
Instance Method Details
#add_pipeline(name, erb_filename) ⇒ Object
81
82
83
|
# File 'lib/concourse.rb', line 81
def add_pipeline(name, erb_filename)
@pipelines << Concourse::Pipeline.new(name, @directory, erb_filename)
end
|
#create_tasks! ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
# File 'lib/concourse.rb', line 131
def create_tasks!
unless Dir.exist? directory
mkdir_p directory
end
pipelines.each do |pipeline|
unless File.exist? pipeline.erb_filename
warn "WARNING: concourse template #{pipeline.erb_filename.inspect} does not exist, run `rake concourse:init`"
end
end
namespace :concourse do
desc "bootstrap a concourse config"
task :init do
rake_init
end
desc "generate and validate all pipeline files"
task "generate" => pipeline_subcommands("generate")
pipelines.each do |pipeline|
desc "generate and validate the #{pipeline.name} pipeline file"
task "generate:#{pipeline.name}" do
rake_pipeline_generate pipeline
end
end
desc "upload all pipeline files"
task "set" => pipeline_subcommands("set")
pipelines.each do |pipeline|
desc "upload the #{pipeline.name} pipeline file"
task "set:#{pipeline.name}" => "generate:#{pipeline.name}" do
options = [
"-p '#{pipeline.name}'",
"-c '#{pipeline.filename}'",
]
if File.exist? secrets_filename
note "using #{secrets_filename} to resolve template vars in #{pipeline.filename}"
options << "-l '#{secrets_filename}'"
end
fly "set-pipeline", options.join(" ")
end
end
%w[expose hide pause unpause destroy].each do |command|
desc "#{command} all pipelines"
task command => pipeline_subcommands(command)
pipelines.each do |pipeline|
desc "#{command} the #{pipeline.name} pipeline"
task "#{command}:#{pipeline.name}" do
fly "#{command}-pipeline", "-p #{pipeline.name}"
end
end
end
desc "list all available tasks from all pipelines"
task "tasks" => "generate" do
tasks = []
pipelines.each do |pipeline|
each_task(pipeline) do |job, task|
tasks << "#{job["name"]}/#{task["task"]}"
end
end
note "Available Concourse tasks for #{project_name} are:"
tasks.sort.each { |task| puts " * #{task}" }
end
desc "fly execute the specified task"
task "task", [:job_task, :fly_execute_args] => "generate" do |t, args|
job_task = args[:job_task]
unless job_task
raise "ERROR: must specify a task name, like `rake #{t.name}[taskname]`"
end
concourse_task = find_task(job_task)
raise "ERROR: could not find task `#{job_task}`" unless concourse_task
fly_execute_args = args[:fly_execute_args] || Concourse.default_execute_args(concourse_task)
if File.exist? secrets_filename
note "using #{secrets_filename} to resolve template vars"
fly_execute_args += " -l '#{secrets_filename}'"
end
puts concourse_task.to_yaml
Tempfile.create("concourse-task") do |f|
f.write concourse_task["config"].to_yaml
f.close
Bundler.with_unbundled_env do
fly "execute", [fly_execute_args, "-c #{f.path}"].compact.join(" ")
end
end
end
desc "abort all running builds for this concourse team"
task "abort-builds" do |t, args|
`fly -t #{fly_target} builds`.each_line do |line|
pipeline_job, build_id, status = *line.split(/\s+/)[1, 3]
next unless status == "started"
fly "abort-build", "-j #{pipeline_job} -b #{build_id}"
end
end
desc "prune any stalled workers"
task "prune-stalled-workers" do
`fly -t #{fly_target} workers | fgrep stalled`.each_line do |line|
worker_id = line.split.first
fly "prune-worker", "-w #{worker_id}"
end
end
desc "set fly target to the local docker-compose cluster"
task "local" do
rake_concourse_local
end
namespace "local" do
desc "start up a docker-compose cluster for local CI"
task "up" do
rake_concourse_local_up
end
desc "shut down the docker-compose cluster"
task "down" do
rake_concourse_local_down
end
end
end
end
|
#ensure_docker_compose_file ⇒ Object
105
106
107
108
109
110
111
112
|
# File 'lib/concourse.rb', line 105
def ensure_docker_compose_file
return if File.exist?(docker_compose_path)
note "fetching docker compose file ..."
File.open(docker_compose_path, "w") do |f|
f.write URI.open("https://concourse-ci.org/docker-compose.yml").read
sh "docker pull concourse/concourse"
end
end
|
#pipeline_subcommands(command) ⇒ Object
85
86
87
|
# File 'lib/concourse.rb', line 85
def pipeline_subcommands(command)
pipelines.collect { |p| "#{command}:#{p.name}" }
end
|
#rake_concourse_local ⇒ Object
114
115
116
117
118
|
# File 'lib/concourse.rb', line 114
def rake_concourse_local
ensure_docker_compose_file
@fly_target = "local"
fly "login", "-u test -p test -c http://127.0.0.1:8080"
end
|
#rake_concourse_local_down ⇒ Object
126
127
128
129
|
# File 'lib/concourse.rb', line 126
def rake_concourse_local_down
ensure_docker_compose_file
docker_compose "down"
end
|
#rake_concourse_local_up ⇒ Object
120
121
122
123
124
|
# File 'lib/concourse.rb', line 120
def rake_concourse_local_up
ensure_docker_compose_file
docker_compose "up -d"
docker_compose "ps"
end
|
#rake_init ⇒ Object
89
90
91
92
93
94
95
|
# File 'lib/concourse.rb', line 89
def rake_init
FileUtils.mkdir_p File.join(directory, "tasks")
pipelines.each do |pipeline|
FileUtils.touch pipeline.erb_filename
end
ensure_in_gitignore secrets_filename
end
|
#rake_pipeline_generate(pipeline) ⇒ Object
97
98
99
100
101
102
103
|
# File 'lib/concourse.rb', line 97
def rake_pipeline_generate(pipeline)
File.open pipeline.filename, "w" do |f|
f.write erbify_file(pipeline.erb_filename, working_directory: directory)
end
fly "validate-pipeline", "-c #{pipeline.filename}"
fly "format-pipeline", "-c #{pipeline.filename} -w" if format
end
|