class Concourse
def create_tasks!
def create_tasks! unless Dir.exist? directory mkdir_p directory end unless File.exist? pipeline_erb_filename warn "WARNING: concourse template #{pipeline_erb_filename.inspect} does not exist, run `rake concourse:init`" end CLOBBER.include pipeline_filename if defined?(CLOBBER) namespace :concourse do # # project commands # desc "bootstrap a concourse config" task :init do rake_init end # # pipeline commands # desc "generate and validate the pipeline file for #{project_name}" task "generate" do |t| File.open pipeline_filename, "w" do |f| f.write erbify(File.read(pipeline_erb_filename)) end sh "fly validate-pipeline -c #{pipeline_filename}" end desc "upload the pipeline file for #{project_name}" task "set", [:fly_target] => ["generate"] do |t, args| fly_target = Concourse.validate_fly_target t, args options = [ "-p '#{project_name}'", "-c '#{pipeline_filename}'", ] if File.exist? private_var_file puts "using #{private_var_file} to resolve template vars" options << "-l '#{private_var_file}'" end sh "fly -t #{fly_target} set-pipeline #{options.join(" ")}" end %w[expose hide pause unpause destroy].each do |command| desc "#{command} the #{project_name} pipeline" task "#{command}", [:fly_target] do |t, args| fly_target = Concourse.validate_fly_target t, args sh "fly -t #{fly_target} #{command}-pipeline -p #{project_name}" end end desc "remove generate pipeline file" task "clean" do |t| rm_f pipeline_filename end # # task commands # desc "list all the available tasks from the #{project_name} pipeline" task "tasks" => "generate" do tasks = [] each_task do |job, task| tasks << "#{job["name"]}/#{task["task"]}" end puts "Available Concourse tasks for #{project_name} are:" tasks.sort.each { |task| puts " * #{task}" } end desc "fly execute the specified task" task "task", [:fly_target, :job_task, :fly_execute_args] => "generate" do |t, args| fly_target = Concourse.validate_fly_target t, args job_task = args[:job_task] fly_execute_args = args[:fly_execute_args] || "--input=#{project_name}=." unless job_task raise "ERROR: must specify a task name, like `rake #{t.name}[target,taskname]`" end concourse_task = find_task job_task raise "ERROR: could not find task `#{job_task}`" unless concourse_task 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 sh "fly -t #{fly_target} execute #{fly_execute_args} -c #{f.path} -x" end end end # # builds commands # desc "abort all running builds for this pipeline" task "abort-builds", [:fly_target] do |t, args| fly_target = Concourse.validate_fly_target t, args `fly -t #{fly_target} builds`.split("\n").each do |line| pipeline_job, build_id, status = *line.split(/\s+/)[1,3] next unless status == "started" sh "fly -t #{fly_target} abort-build -j #{pipeline_job} -b #{build_id}" end end end end