-
-
Save damm/c7e89264f3130b2c62fc to your computer and use it in GitHub Desktop.
neutered copy of https://github.com/opscode/kb/blob/go/install
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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