Module: Concourse::Util

Includes:
Term::ANSIColor
Included in:
Concourse
Defined in:
lib/concourse/util.rb

Constant Summary collapse

GITIGNORE_FILE =
".gitignore"
GITATTRIBUTES_FILE =
".gitattributes"

Instance Method Summary collapse

Instance Method Details

#docker_compose(command) ⇒ Object



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

def docker_compose command
  sh "docker-compose -f #{docker_compose_path} #{command}"
end

#docker_compose_pathObject



30
31
32
# File 'lib/concourse/util.rb', line 30

def docker_compose_path
  File.join(directory, CONCOURSE_DOCKER_COMPOSE)
end

#each_job(pipeline) ⇒ Object



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

def each_job pipeline
  pdata = YAML.load_file(pipeline.filename)

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

#each_task(pipeline) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/concourse/util.rb', line 68

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

#ensure_in_gitignore(file_glob) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/concourse/util.rb', line 10

def ensure_in_gitignore file_glob
  if File.exist?(GITIGNORE_FILE)
    if File.read(GITIGNORE_FILE).split("\n").include?(file_glob)
      note "found '#{file_glob}' already present in #{GITIGNORE_FILE}"
      return
    end
  end
  note "adding '#{file_glob}' to #{GITIGNORE_FILE}"
  File.open(GITIGNORE_FILE, "a") { |f| f.puts file_glob }
end

#erbify_file(filename, working_directory: nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/concourse/util.rb', line 47

def erbify_file filename, working_directory: nil
  raise "ERROR: erbify_file: could not find file `#{filename}`" unless File.exist?(filename)
  template = File.read(filename)

  if working_directory.nil?
    working_directory = "." # so chdir is a no-op below
  else
    fqwd = File.expand_path(working_directory)
    $LOAD_PATH << fqwd unless $LOAD_PATH.include?(fqwd) # so "require" can load relative paths
  end
  Dir.chdir(working_directory) { ERB.new(template, nil, "%-").result(binding) }
end

#find_task(job_task) ⇒ Object



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

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

#fly(command, args) ⇒ Object



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

def fly command, args
  command_args = Array(fly_args[command])
  sh ["fly -t", fly_target, command, command_args, args].flatten.compact.join(" ")
end

#note(message) ⇒ Object



43
44
45
# File 'lib/concourse/util.rb', line 43

def note message
  print bold, green, "NOTE: ", reset, message, "\n"
end

#running(message) ⇒ Object



39
40
41
# File 'lib/concourse/util.rb', line 39

def running message
  print bold, red, "RUNNING: ", reset, message, "\n"
end

#sh(command) ⇒ Object



34
35
36
37
# File 'lib/concourse/util.rb', line 34

def sh command
  running "(in #{Dir.pwd}) #{command}"
  Rake.sh command, verbose: false
end