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.1 2.2 2.3 2.4], # docker repository: "ruby"
  jruby: %w[1.7 9.1],         # docker repository: "jruby"
  rbx:   %w[latest],          # docker repository: "rubinius/docker"
}
DIRECTORY =
"concourse"
PRIVATE_VAR_FILE =
File.join DIRECTORY, "private.yml"
VERSION =
"0.9.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_name) ⇒ Concourse

Returns a new instance of Concourse.



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

def initialize project_name
  @project_name = project_name
  @pipeline_filename = File.join(DIRECTORY, "#{project_name}.yml")
  @pipeline_erb_filename = "#{pipeline_filename}.erb"
end

Instance Attribute Details

#pipeline_erb_filenameObject (readonly)

Returns the value of attribute pipeline_erb_filename.



16
17
18
# File 'lib/concourse.rb', line 16

def pipeline_erb_filename
  @pipeline_erb_filename
end

#pipeline_filenameObject (readonly)

Returns the value of attribute pipeline_filename.



16
17
18
# File 'lib/concourse.rb', line 16

def pipeline_filename
  @pipeline_filename
end

#project_nameObject (readonly)

Returns the value of attribute project_name.



16
17
18
# File 'lib/concourse.rb', line 16

def project_name
  @project_name
end

Class Method Details

.url_for(fly_target) ⇒ Object



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

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



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

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

#badge_url_for(url_prefix, team_name, job_name) ⇒ Object



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

def badge_url_for url_prefix, team_name, job_name
  File.join url_prefix,
            "api/v1/teams",
            team_name,
            "pipelines",
            project_name,
            "jobs",
            job_name,
            "badge"
end

#create_tasks!Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
# File 'lib/concourse.rb', line 41

def create_tasks!
  unless Dir.exist? DIRECTORY
    mkdir_p DIRECTORY
  end

  unless File.exist? pipeline_erb_filename
    raise "ERROR: concourse pipeline template #{pipeline_erb_filename.inspect} does not exist"
  end

  CLOBBER.include pipeline_filename if defined?(CLOBBER)

  namespace :concourse do
    #
    #  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].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 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

    #
    #  badge commands
    #
    desc "display a list of jobs and badge urls"
    task "badges", [:fly_target, :team_name] => "generate" do |t, args|
      fly_target = Concourse.validate_fly_target t, args
      team_name = args[:team_name] || "main"

      puts ""
      puts "| Build | Status |"
      puts "|--|--|"

      each_job do |job|
        job_name = job["name"]
        puts "| #{titleize job_name} | #{markdown_badge fly_target, team_name, job_name} |"
      end
    end
  end
end

#each_jobObject



149
150
151
152
153
154
155
# File 'lib/concourse.rb', line 149

def each_job
  pipeline = YAML.load_file(pipeline_filename)

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

#each_taskObject



157
158
159
160
161
162
163
# File 'lib/concourse.rb', line 157

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



37
38
39
# File 'lib/concourse.rb', line 37

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

#find_task(job_task) ⇒ Object



165
166
167
168
169
170
171
# File 'lib/concourse.rb', line 165

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

#job_url_for(url_prefix, team_name, job_name) ⇒ Object



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

def job_url_for url_prefix, team_name, job_name
  File.join url_prefix,
            "teams",
            team_name,
            "pipelines",
            project_name,
            "jobs",
            job_name
end

#markdown_badge(fly_target, team_name, job_name) ⇒ Object



177
178
179
180
# File 'lib/concourse.rb', line 177

def markdown_badge fly_target, team_name, job_name
  url_prefix = Concourse.url_for fly_target
  "[![name](#{badge_url_for(url_prefix, team_name, job_name)})](#{job_url_for(url_prefix, team_name, job_name)})"
end

#titleize(string) ⇒ Object



173
174
175
# File 'lib/concourse.rb', line 173

def titleize string
  string.gsub(/[^a-zA-Z0-9\.]/, " ")
end