Class: BerksToRightscale::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/berks_to_rightscale/cli.rb

Instance Method Summary collapse

Instance Method Details

#list_destinationsObject



111
112
113
# File 'lib/berks_to_rightscale/cli.rb', line 111

def list_destinations
  puts ::Fog::Storage.providers
end

#release(projectname, releasename) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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