Class: Concourse

Inherits:
Object
  • 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], # docker repository: "ruby"
  jruby: %w[9.1 9.2], # docker repository: "jruby"
  rbx: %w[latest], # docker repository: "rubinius/docker"
  windows: %w[2.3 2.4 2.5 2.6], # windows-ruby-dev-tools-release
}
DEFAULT_DIRECTORY =
"concourse"
DEFAULT_FLY_TARGET =
"default"
DEFAULT_SECRETS =
"private.yml"
CONCOURSE_DOCKER_COMPOSE =
"docker-compose.yml"
VERSION =
"0.30.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.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/concourse.rb', line 54

def initialize(project_name, options = {}, &block)
  @project_name = project_name

  @directory = options[:directory] || DEFAULT_DIRECTORY
  @fly_target = options[:fly_target] || DEFAULT_FLY_TARGET

  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

#directoryObject (readonly)

Returns the value of attribute directory.



25
26
27
# File 'lib/concourse.rb', line 25

def directory
  @directory
end

#fly_targetObject (readonly)

Returns the value of attribute fly_target.



27
28
29
# File 'lib/concourse.rb', line 27

def fly_target
  @fly_target
end

#pipelinesObject (readonly)

Returns the value of attribute pipelines.



26
27
28
# File 'lib/concourse.rb', line 26

def pipelines
  @pipelines
end

#project_nameObject (readonly)

Returns the value of attribute project_name.



24
25
26
# File 'lib/concourse.rb', line 24

def project_name
  @project_name
end

#secrets_filenameObject (readonly)

Returns the value of attribute secrets_filename.



28
29
30
# File 'lib/concourse.rb', line 28

def secrets_filename
  @secrets_filename
end

Class Method Details

.default_execute_args(task) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/concourse.rb', line 38

def self.default_execute_args(task)
  args = []
  task["config"]["inputs"].each do |input|
    args << "--input=#{input["name"]}=."
  end
  args.join(" ")
end

.production_rubiesObject



46
47
48
# File 'lib/concourse.rb', line 46

def self.production_rubies
  RUBIES[:mri].reject { |r| r =~ /rc/ }
end

.rc_rubiesObject



50
51
52
# File 'lib/concourse.rb', line 50

def self.rc_rubies
  RUBIES[:mri].select { |r| r =~ /rc/ }
end

.url_for(fly_target) ⇒ Object



32
33
34
35
36
# File 'lib/concourse.rb', line 32

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



72
73
74
# File 'lib/concourse.rb', line 72

def add_pipeline(name, erb_filename)
  @pipelines << Concourse::Pipeline.new(name, @directory, erb_filename)
end

#create_tasks!Object



121
122
123
124
125
126
127
128
129
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
# File 'lib/concourse.rb', line 121

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
    #
    #  project commands
    #
    desc "bootstrap a concourse config"
    task :init do
      rake_init
    end

    #
    #  pipeline commands
    #
    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 "remove generated pipeline files"
    task "clean" do
      pipelines.each do |pipeline|
        rm_f pipeline.filename
      end
    end

    #
    #  task commands
    #
    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_clean_env do
          fly "execute #{fly_execute_args} -c #{f.path}"
        end
      end
    end

    #
    #  builds commands
    #
    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

    #
    #  worker commands
    #
    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

    #
    #  docker commands
    #
    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_fileObject



95
96
97
98
99
100
101
102
# File 'lib/concourse.rb', line 95

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 open("https://concourse-ci.org/docker-compose.yml").read
    sh "docker pull concourse/concourse"
  end
end

#pipeline_subcommands(command) ⇒ Object



76
77
78
# File 'lib/concourse.rb', line 76

def pipeline_subcommands(command)
  pipelines.collect { |p| "#{command}:#{p.name}" }
end

#rake_concourse_localObject



104
105
106
107
108
# File 'lib/concourse.rb', line 104

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_downObject



116
117
118
119
# File 'lib/concourse.rb', line 116

def rake_concourse_local_down
  ensure_docker_compose_file
  docker_compose "down"
end

#rake_concourse_local_upObject



110
111
112
113
114
# File 'lib/concourse.rb', line 110

def rake_concourse_local_up
  ensure_docker_compose_file
  docker_compose "up -d"
  docker_compose "ps"
end

#rake_initObject



80
81
82
83
84
85
86
# File 'lib/concourse.rb', line 80

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



88
89
90
91
92
93
# File 'lib/concourse.rb', line 88

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}"
end