module Concourse::Deployer::Utils

Constants

GITATTRIBUTES_FILE
GITIGNORE_FILE

Public Instance Methods

bbl_external_ip() click to toggle source
# File lib/concourse/deployer/utils.rb, line 157
def bbl_external_ip
  `bbl lbs`.split(":").last.strip
end
bosh_secrets() { |vars| ... } click to toggle source
# File lib/concourse/deployer/utils.rb, line 161
def bosh_secrets(&block)
  vars = File.exists?(BOSH_SECRETS) ? YAML.load_file(BOSH_SECRETS) : {}
  return vars unless block_given?

  yield vars
  File.open(BOSH_SECRETS, "w") { |f| f.write vars.to_yaml }
  vars
end
bosh_update_from_git_repo(git) click to toggle source
# File lib/concourse/deployer/utils.rb, line 191
def bosh_update_from_git_repo(git)
  dirname = File.basename(git)
  Dir.mktmpdir do |dir|
    Dir.chdir dir do
      sh "git clone '#{git}'"
      Dir.chdir dirname do
        sh "bosh create-release"
        sh "bosh upload-release"
      end
    end
  end
end
bosh_update_release(repo) click to toggle source
# File lib/concourse/deployer/utils.rb, line 179
def bosh_update_release(repo)
  doc = Nokogiri::XML(open("https://bosh.io/releases/github.com/#{repo}?all=1"))
  url = doc.at_xpath("//a[contains(text(), 'Release Tarball')]/@href")
  if url.nil?
    error "Could not find the latest release `#{repo}`"
  end
  if url.value =~ %r{\A/}
    url = "https://bosh.io#{url}"
  end
  sh "bosh upload-release #{url}"
end
bosh_update_stemcell(name) click to toggle source
# File lib/concourse/deployer/utils.rb, line 170
def bosh_update_stemcell(name)
  doc = Nokogiri::XML(open("https://bosh.io/stemcells/#{name}"))
  url = doc.at_xpath("//a[contains(text(), 'Light Stemcell')]/@href")
  if url.nil?
    error "Could not find the latest stemcell `#{name}`"
  end
  sh "bosh upload-stemcell #{url}"
end
ensure_file(filename, &block) click to toggle source
# File lib/concourse/deployer/utils.rb, line 33
def ensure_file(filename, &block)
  return if File.exist?(filename)
  File.open(filename, "w") do |f|
    block.call f
  end
end
ensure_git_submodule(repo_url, commitish) click to toggle source
# File lib/concourse/deployer/utils.rb, line 111
def ensure_git_submodule(repo_url, commitish)
  repo_name = File.basename repo_url
  sh "git submodule add '#{repo_url}'" unless Dir.exists?(repo_name)
  Dir.chdir(repo_name) do
    sh "git remote update"
    sh "git checkout '#{commitish}'"
  end
end
ensure_in_envrc(entry_key, entry_value = nil) click to toggle source
# File lib/concourse/deployer/utils.rb, line 63
def ensure_in_envrc(entry_key, entry_value = nil)
  entries = if File.exist?(ENVRC_FILE)
              File.read(ENVRC_FILE).split("\n")
            else
              Array.new
            end

  if entry_value
    #
    #  set an env var
    #
    entry_match = /^export #{entry_key}=/
    entry_contents = "export #{entry_key}=#{entry_value}"

    found_entry = entries.grep(entry_match).first

    if found_entry.nil?
      note "adding '#{entry_key}=#{entry_value}' to #{ENVRC_FILE}"
      File.open(ENVRC_FILE, "a") { |f| f.puts entry_contents }
    else
      if found_entry == entry_contents
        note "found '#{entry_key}=#{entry_value}' already present in #{ENVRC_FILE}"
        return
      else
        note "overwriting '#{entry_key}' entry with '#{entry_value}' in #{ENVRC_FILE}"
        entries.map! do |jentry|
          jentry =~ entry_match ? entry_contents : jentry
        end
        File.open(ENVRC_FILE, "w") { |f| f.puts entries.join("\n") }
      end
    end
  else
    #
    #  add a line of bash
    #
    entry_contents = entry_key
    found_entry = entries.find { |line| line == entry_contents }

    if found_entry.nil?
      note "adding '#{entry_contents}' to #{ENVRC_FILE}"
      File.open(ENVRC_FILE, "a") { |f| f.puts entry_contents }
    else
      note "found '#{entry_contents}' already present in #{ENVRC_FILE}"
      return
    end
  end
end
ensure_in_gitcrypt(file_glob) click to toggle source
# File lib/concourse/deployer/utils.rb, line 51
def ensure_in_gitcrypt(file_glob)
  crypt_entry = "#{file_glob} filter=git-crypt diff=git-crypt"
  if File.exist?(GITATTRIBUTES_FILE)
    if File.read(GITATTRIBUTES_FILE).split("\n").include?(crypt_entry)
      note "found '#{file_glob}' already git-crypted in #{GITATTRIBUTES_FILE}"
      return
    end
  end
  note "adding '#{file_glob}' as git-crypted to #{GITATTRIBUTES_FILE}"
  File.open(GITATTRIBUTES_FILE, "a") { |f| f.puts crypt_entry }
end
ensure_in_gitignore(file_glob) click to toggle source
# File lib/concourse/deployer/utils.rb, line 40
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
error(message, continue = false) click to toggle source
# File lib/concourse/deployer/utils.rb, line 28
def error(message, continue = false)
  print red, bold, "ERROR: #{message}", reset, "\n"
  exit 1 unless continue
end
important(message) click to toggle source
# File lib/concourse/deployer/utils.rb, line 24
def important(message)
  print bold, "NOTE: ", message, reset, "\n"
end
note(message) click to toggle source
# File lib/concourse/deployer/utils.rb, line 20
def note(message)
  print bold, green, "NOTE: ", reset, message, "\n"
end
prompt(query, default = nil) click to toggle source
# File lib/concourse/deployer/utils.rb, line 133
def prompt(query, default = nil)
  loop do
    message = query
    message += " [#{default}]" if default
    message += ": "
    print bold, message, reset
    answer = STDIN.gets.chomp.strip
    if answer.empty?
      return default if default
      error "Please provide an answer.", true
    else
      return answer
    end
  end
end
prompt_for_file_contents(query) click to toggle source
# File lib/concourse/deployer/utils.rb, line 149
def prompt_for_file_contents(query)
  loop do
    path = prompt query
    return File.read(path) if File.exists?(path)
    error("File '#{path}' does not exist.", true)
  end
end
running(message) click to toggle source
# File lib/concourse/deployer/utils.rb, line 16
def running(message)
  print bold, red, "RUNNING: ", reset, message, "\n"
end
sh(command) click to toggle source
Calls superclass method
# File lib/concourse/deployer/utils.rb, line 11
def sh(command)
  running "(in #{Dir.pwd}) #{command}"
  super command, verbose: false
end
unless_which(command, whereto) click to toggle source
# File lib/concourse/deployer/utils.rb, line 125
def unless_which(command, whereto)
  if which command
    note "found command '#{command}'"
    return
  end
  error "please install '#{command}' by visiting #{whereto}"
end
which(command) click to toggle source
# File lib/concourse/deployer/utils.rb, line 120
def which(command)
  found = `which #{command}`
  return $?.success? ? found : nil
end