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], jruby: %w[9.2], rbx: %w[latest], windows: %w[2.3 2.4 2.5 2.6], }
- DEFAULT_DIRECTORY =
"concourse"
- DEFAULT_FLY_TARGET =
"default"
- DEFAULT_SECRETS =
"private.yml"
- CONCOURSE_DOCKER_COMPOSE =
"docker-compose.yml"
- VERSION =
"0.35.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.
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/concourse.rb', line 56
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.
25
26
27
|
# File 'lib/concourse.rb', line 25
def directory
@directory
end
|
#fly_args ⇒ Object
Returns the value of attribute fly_args.
28
29
30
|
# File 'lib/concourse.rb', line 28
def fly_args
@fly_args
end
|
#fly_target ⇒ Object
Returns the value of attribute fly_target.
27
28
29
|
# File 'lib/concourse.rb', line 27
def fly_target
@fly_target
end
|
Returns the value of attribute format.
30
31
32
|
# File 'lib/concourse.rb', line 30
def format
@format
end
|
#pipelines ⇒ Object
Returns the value of attribute pipelines.
26
27
28
|
# File 'lib/concourse.rb', line 26
def pipelines
@pipelines
end
|
#project_name ⇒ Object
Returns the value of attribute project_name.
24
25
26
|
# File 'lib/concourse.rb', line 24
def project_name
@project_name
end
|
#secrets_filename ⇒ Object
Returns the value of attribute secrets_filename.
29
30
31
|
# File 'lib/concourse.rb', line 29
def secrets_filename
@secrets_filename
end
|
Class Method Details
.default_execute_args(task) ⇒ Object
40
41
42
43
44
45
46
|
# File 'lib/concourse.rb', line 40
def self.default_execute_args(task)
args = []
task["config"]["inputs"].each do |input|
args << "--input=#{input["name"]}=."
end
args.join(" ")
end
|
.production_rubies ⇒ Object
48
49
50
|
# File 'lib/concourse.rb', line 48
def self.production_rubies
RUBIES[:mri].reject { |r| r =~ /rc/ }
end
|
.rc_rubies ⇒ Object
52
53
54
|
# File 'lib/concourse.rb', line 52
def self.rc_rubies
RUBIES[:mri].select { |r| r =~ /rc/ }
end
|
.url_for(fly_target) ⇒ Object
34
35
36
37
38
|
# File 'lib/concourse.rb', line 34
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
80
81
82
|
# File 'lib/concourse.rb', line 80
def add_pipeline(name, erb_filename)
@pipelines << Concourse::Pipeline.new(name, @directory, erb_filename)
end
|
#create_tasks! ⇒ Object
130
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
284
|
# File 'lib/concourse.rb', line 130
def create_tasks!
unless Dir.exist? directory
mkdir_p directory
end
pipelines.each do |pipeline|
CLOBBER.include pipeline.filename if defined?(CLOBBER)
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
104
105
106
107
108
109
110
111
|
# File 'lib/concourse.rb', line 104
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
84
85
86
|
# File 'lib/concourse.rb', line 84
def pipeline_subcommands(command)
pipelines.collect { |p| "#{command}:#{p.name}" }
end
|
#rake_concourse_local ⇒ Object
113
114
115
116
117
|
# File 'lib/concourse.rb', line 113
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
125
126
127
128
|
# File 'lib/concourse.rb', line 125
def rake_concourse_local_down
ensure_docker_compose_file
docker_compose "down"
end
|
#rake_concourse_local_up ⇒ Object
119
120
121
122
123
|
# File 'lib/concourse.rb', line 119
def rake_concourse_local_up
ensure_docker_compose_file
docker_compose "up -d"
docker_compose "ps"
end
|
#rake_init ⇒ Object
88
89
90
91
92
93
94
|
# File 'lib/concourse.rb', line 88
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
96
97
98
99
100
101
102
|
# File 'lib/concourse.rb', line 96
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
|