class BerksToRightscale::Cli

Public Instance Methods

list_destinations() click to toggle source
# File lib/berks_to_rightscale/cli.rb, line 111
def list_destinations
  puts ::Fog::Storage.providers
end
release(projectname, releasename) click to toggle source
# File lib/berks_to_rightscale/cli.rb, line 37
def release(projectname, releasename)
  output_path = ::File.join(Dir.pwd, "cookbooks")
  sym_options = {}
  options.each{|k,v| sym_options[k.to_sym] = v }
  final_opts = {:path => output_path, :force => false, :no_cleanup => false}.merge(sym_options)
  tarball = "#{releasename}.tar.gz"
  file_key = "#{projectname}/#{tarball}"

  tarball_path = ::File.join(Dir.pwd, tarball)

  fog_params = { :provider => final_opts[:provider] }

  if final_opts[:provider_options]
    provider_opts = { }
    begin
      # convert comma-separated list to hash
      option_list = final_opts[:provider_options].split(",")
      provider_opts = option_list.inject({}) do |hash, kv_pair|
        kv = kv_pair.split("=")
        raise "ERROR: value not found." unless kv[1]
        hash.merge({ kv[0] => kv[1] })
      end
    rescue
      puts "ERROR: malformed ---provider-options parameter.  Must be in the form of 'opt1=value1,opt2=value2'. Please try again."
      exit 1
    end
    # merge in provider options hash
    fog_params.merge!(provider_opts)
  end


  begin
    @fog = ::Fog::Storage.new(fog_params)
  rescue Exception => e
    puts "ERROR: Fog had a problem initializing storage provider: #{e.message}"
    exit 1
  end

  unless container = @fog.directories.all.detect {|cont| cont.key == final_opts[:container]}
    puts "There was no container named #{final_opts[:container]} for provider #{final_opts[:provider]}"
    exit 1
  end

  if container.files.all.detect {|file| file.key == file_key} && !final_opts[:force]
    puts "There is already a released named #{releasename} for the project #{projectname}.  If you want to overwrite it, specify the --force flag"
    exit 1
  end

  berksfile = ::Berkshelf::Berksfile.from_file(final_opts[:berksfile])
  berksfile.install(final_opts)

  meta = ::Chef::Knife::CookbookMetadata.new
  meta.config[:all] = true
  meta.config[:cookbook_path] = output_path
  meta.run

  puts "Creating a tarball containing the specified cookbooks"
  `tar -C #{output_path} -zcvf #{tarball_path} . 2>&1`

  file = File.open(tarball_path, 'r')
  fog_file = container.files.create(:key => file_key, :body => file, :acl => 'public-read')
  fog_file.save
  file.close

  puts "Released file can be found at #{fog_file.public_url}"

  # Cleanup
  unless final_opts[:no_cleanup]
    FileUtils.rm tarball if File.exist? tarball
    FileUtils.rm_rf output_path if File.directory? output_path
  end
end