bin/appbundle-updater in appbundle-updater-0.1.0 vs bin/appbundle-updater in appbundle-updater-0.2.0

- old
+ new

@@ -46,10 +46,12 @@ TAR_LONGLINK = '././@LongLink' # pure ruby `tar xzf`, handles longlinks and directories ending in '/' # (http://stackoverflow.com/a/31310593/506908) def extract_tgz(file, destination = '.') + # NOTE: THIS IS DELIBERATELY PURE RUBY USING NO NATIVE GEMS AND ONLY + # THE RUBY STDLIB BY DESIGN Gem::Package::TarReader.new( Zlib::GzipReader.open file ) do |tar| dest = nil tar.each do |entry| if entry.full_name == TAR_LONGLINK dest = File.join destination, entry.read.strip @@ -125,38 +127,52 @@ "#{bin_dir.join("rake")} install", ) ].freeze class Updater - attr_reader :app, :ref + attr_reader :app, :ref, :tarball, :repo def initialize(options) @app = options[:app] @ref = options[:ref] + @tarball = options[:tarball] + @repo = options[:repo] || @app.repo end def start if !windows? && Process.uid != 0 abort "#{$0} needs to be run as root user or with sudo" end - ruby("-rpp -e 'pp ENV'") banner("Cleaning #{app} checkout") app_dir.rmtree if app_dir.directory? - git_url = "https://github.com/#{app.repo}/archive/#{ref}.tar.gz" - banner("Extracting #{app} from #{git_url}") - Dir.chdir(chefdk.join("embedded/apps")) do - Tempfile.open('appbundle-updater') do |tempfile| - open(git_url) do |uri| - tempfile.write(uri.read) + if ( tarball ) + # NOTE: THIS IS DELIBERATELY PURE RUBY USING NO NATIVE GEMS AND ONLY + # THE RUBY STDLIB BY DESIGN + git_url = "https://github.com/#{repo}/archive/#{ref}.tar.gz" + banner("Extracting #{app} from #{git_url}") + Dir.chdir(chefdk.join("embedded/apps")) do + Tempfile.open('appbundle-updater') do |tempfile| + open(git_url) do |uri| + tempfile.write(uri.read) + end + tempfile.close + extract_tgz(tempfile.path) end - tempfile.close - extract_tgz(tempfile.path) + base = File.basename repo + FileUtils.mv "#{base}-#{ref}".gsub(/\//, "-"), "#{app.name}" end - base = File.basename app.repo - FileUtils.mv "#{base}-#{ref}", "#{app.name}" + else + git_url = "https://github.com/#{repo}.git" + banner("Cloning #{app} from #{git_url}") + run("git clone #{git_url} #{app_dir}") + + banner("Checking out #{app} to #{ref}") + Dir.chdir(app_dir) do + run("git checkout #{ref}") + end end banner("Installing dependencies") Dir.chdir(app_dir) do cmd = "#{bin_dir.join("bundle")} install" @@ -198,11 +214,14 @@ def run(cmd) ENV_KEYS.each { |key| ENV["_YOLO_#{key}"] = ENV[key]; ENV.delete(key) } ENV['PATH'] = "#{bin_dir}:#{ENV['_YOLO_PATH']}" puts " running: #{cmd}" - system(cmd) or raise("Command [#{cmd}] failed!") + output = `#{cmd} 2>&1` #FIXME: bash/zsh-ism, will not work on csh + unless $?.exited? && $?.exitstatus == 0 + raise("Command [#{cmd}] failed!\n\n---BEGIN OUTPUT--\n#{output}\n---END OUTPUT--\n") + end ENV_KEYS.each { |key| ENV[key] = ENV.delete("_YOLO_#{key}") } end end @@ -215,9 +234,15 @@ def initialize @options = Hash.new @parser = OptionParser.new { |opts| opts.banner = "Usage: #{$0} PROJECT APP_NAME GIT_REF" + opts.on("-t", "--[no-]tarball", "Do a tarball download instead of git clone") do |t| + options[:tarball] = t + end + opts.on("-g", "--github REPO", "Github repo (e.g. chef/chef) to pull from") do |g| + options[:repo] = g + end opts.on("-h", "--help", "Prints this help") do puts opts exit end opts.separator("")