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.3 2.4 2.5 2.6], # 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"
VERSION =
"0.24.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

#each_job, #each_task, #ensure_in_gitignore, #erbify_file, #find_task, #note, #running, #sh

Constructor Details

#initialize(project_name, options = {}, &block) ⇒ Concourse

Returns a new instance of Concourse.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/concourse.rb', line 49

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

  @directory = options[:directory] || DEFAULT_DIRECTORY
  @fly_target = options[:fly_target] || "default"

  base_secrets_filename = options[:secrets_filename] || "private.yml"
  @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.



22
23
24
# File 'lib/concourse.rb', line 22

def directory
  @directory
end

#fly_targetObject (readonly)

Returns the value of attribute fly_target.



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

def fly_target
  @fly_target
end

#pipelinesObject (readonly)

Returns the value of attribute pipelines.



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

def pipelines
  @pipelines
end

#project_nameObject (readonly)

Returns the value of attribute project_name.



21
22
23
# File 'lib/concourse.rb', line 21

def project_name
  @project_name
end

#secrets_filenameObject (readonly)

Returns the value of attribute secrets_filename.



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

def secrets_filename
  @secrets_filename
end

Class Method Details

.default_execute_args(task) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/concourse.rb', line 33

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

.production_rubiesObject



41
42
43
# File 'lib/concourse.rb', line 41

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

.rc_rubiesObject



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

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

.url_for(fly_target) ⇒ Object



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

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



67
68
69
# File 'lib/concourse.rb', line 67

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

#create_tasks!Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
# File 'lib/concourse.rb', line 91

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
        sh "fly -t #{fly_target} 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
          sh "fly -t #{fly_target} #{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}[target,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)

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

        sh "fly -t #{fly_target} 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
        system("fly -t #{fly_target} prune-worker -w #{worker_id}")
      end
    end
  end
end

#pipeline_subcommands(command) ⇒ Object



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

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

#rake_initObject



75
76
77
78
79
80
81
82
# File 'lib/concourse.rb', line 75

def rake_init
  FileUtils.mkdir_p File.join(directory, "tasks")
  pipelines.each do |pipeline|
    FileUtils.touch pipeline.erb_filename
    ensure_in_gitignore pipeline.filename
  end
  ensure_in_gitignore secrets_filename
end

#rake_pipeline_generate(pipeline) ⇒ Object



84
85
86
87
88
89
# File 'lib/concourse.rb', line 84

def rake_pipeline_generate pipeline
  File.open pipeline.filename, "w" do |f|
    f.write erbify_file(pipeline.erb_filename, working_directory: directory)
  end
  sh "fly validate-pipeline -c #{pipeline.filename}"
end