Skip to content

Instantly share code, notes, and snippets.

@damm
Created March 12, 2013 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damm/c7e89264f3130b2c62fc to your computer and use it in GitHub Desktop.
Save damm/c7e89264f3130b2c62fc to your computer and use it in GitHub Desktop.
# -*- encoding: utf-8 -*-
require "chef/rest"
require "chef/platform"
require "fileutils"
require "tempfile"
EXCEPTIONS = [ SocketError, Errno::ECONNREFUSED, Timeout::Error,
Net::HTTPFatalError, Net::HTTPServerException ]
class KbInstaller
def initialize(opts = {})
@url = opts[:url] || calculate_url(opts[:tag])
@root = opts[:root] || "/opt/kb"
@tarfile = Tempfile.new("kitchen.tar.gz")
end
def run!
if File.exists?(File.join(@root, "bin/kb"))
log "Detected previous kb installation, skipping install"
else
download
install
log "kb installed!"
end
end
private
def log(msg)
puts " #{msg}"
end
def download
log "Downloading kb from #{@url}"
rest = Chef::REST.new(@url, nil, nil, {})
raw_file = rest.streaming_request(rest.create_url(@url), {})
FileUtils.cp raw_file.path, @tarfile.path
raw_file.close!
rescue *EXCEPTIONS => e
abort "Could not download from #{@url} (#{e.message})"
end
def install
log "Installing kb to #{@root}"
FileUtils.mkdir_p @root
`cd #{@root} && gunzip -c #{@tarfile.path} | tar xf - --strip-components=1`
@tarfile.unlink
end
def calculate_url(tag)
"http://mymirror/kb/v0.3.1.tar.gz"
# "https://github.com/opscode/kb/archive/#{tag || latest_tag}.tar.gz"
end
# def latest_tag
# t_url = "https://api.github.com/repos/opscode/kb/git/refs/tags"
# rest = Chef::REST.new(t_url, nil, nil, {})
# tags = rest.get_rest(t_url, false, {}).map { |t| t["ref"].split("/").last }
# tags.sort.select { |t| t =~ /^v[0-9]/ }.last
# rescue *EXCEPTIONS => e
# return "master" if e.message == %{404 "Not Found"}
# abort "Could not access #{t_url} (#{e.message})"
# end
end
if __FILE__ == $0
KbInstaller.new({
:url => ENV["KB_URL"], :tag => ENV["KB_TAG"], :root => ENV["KB_INSTALL_ROOT"]
}).run!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment