Class: Concourse

Inherits:
Object
  • Object
show all
Includes:
Rake::DSL
Defined in:
lib/concourse.rb,
lib/concourse/version.rb

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.21.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_name, args = {}) ⇒ Concourse

Returns a new instance of Concourse.



49
50
51
52
53
54
55
# File 'lib/concourse.rb', line 49

def initialize project_name, args={}
  @project_name = project_name
  @directory = args[:directory] || DEFAULT_DIRECTORY
  @pipeline_filename = File.join(@directory, "#{project_name}.final.yml")
  @pipeline_erb_filename = File.join(@directory, "#{project_name}.yml")
  @private_var_file = File.join(@directory, "private.yml")
end

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory.



18
19
20
# File 'lib/concourse.rb', line 18

def directory
  @directory
end

#pipeline_erb_filenameObject (readonly)

Returns the value of attribute pipeline_erb_filename.



18
19
20
# File 'lib/concourse.rb', line 18

def pipeline_erb_filename
  @pipeline_erb_filename
end

#pipeline_filenameObject (readonly)

Returns the value of attribute pipeline_filename.



18
19
20
# File 'lib/concourse.rb', line 18

def pipeline_filename
  @pipeline_filename
end

#private_var_fileObject (readonly)

Returns the value of attribute private_var_file.



18
19
20
# File 'lib/concourse.rb', line 18

def private_var_file
  @private_var_file
end

#project_nameObject (readonly)

Returns the value of attribute project_name.



18
19
20
# File 'lib/concourse.rb', line 18

def project_name
  @project_name
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

.validate_fly_target(task, task_args) ⇒ Object



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

def self.validate_fly_target task, task_args
  unless task_args[:fly_target]
    raise "ERROR: must specify a fly target, like `rake #{task.name}[targetname]`"
  end
  return task_args[:fly_target]
end

Instance Method Details

#create_tasks!Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
# File 'lib/concourse.rb', line 70

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]
      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 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

#each_jobObject



184
185
186
187
188
189
190
# File 'lib/concourse.rb', line 184

def each_job
  pipeline = YAML.load_file(pipeline_filename)

  pipeline["jobs"].each do |job|
    yield job
  end
end

#each_taskObject



192
193
194
195
196
197
198
# File 'lib/concourse.rb', line 192

def each_task
  each_job do |job|
    job["plan"].each do |task|
      yield job, task if task["task"]
    end
  end
end

#erbify(document_string, *args) ⇒ Object



57
58
59
# File 'lib/concourse.rb', line 57

def erbify document_string, *args
  ERB.new(document_string, nil, "%-").result(binding)
end

#find_task(job_task) ⇒ Object



200
201
202
203
204
205
206
# File 'lib/concourse.rb', line 200

def find_task job_task
  job_name, task_name = *job_task.split("/")
  each_task do |job, task|
    return task if task["task"] == task_name && job["name"] == job_name
  end
  nil
end

#rake_initObject



61
62
63
64
65
66
67
68
# File 'lib/concourse.rb', line 61

def rake_init
  FileUtils.mkdir_p File.join(directory, "tasks")
  FileUtils.touch pipeline_erb_filename
  File.open ".gitignore", "a" do |f|
    f.puts private_var_file
    f.puts pipeline_filename
  end
end