Class: CookbookRelease::Supermarket
- Inherits:
-
Object
- Object
- CookbookRelease::Supermarket
- Includes:
- Chef::Mixin::ShellOut
- Defined in:
- lib/cookbook-release/supermarket.rb
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Supermarket
constructor
This code is adapted from “knife cookbook share” and travis dpl provider for supermarket.
- #publish_ck(category) ⇒ Object
- #upload(filename, category) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Supermarket
This code is adapted from “knife cookbook share” and travis dpl provider for supermarket.
14 15 16 17 18 19 |
# File 'lib/cookbook-release/supermarket.rb', line 14 def initialize(opts={}) @url = opts[:url] || ENV['SUPERMARKET_URL'] || (raise "Require a supermarket url") @user_id = opts[:user_id] || ENV['SUPERMARKET_USERID'] || (raise "Require a user id") @client_key = opts[:client_key_file] || ENV['SUPERMARKET_CLIENTKEYFILE'] || (raise "Require a client key file") Chef::Config[:ssl_verify_mode] = :verify_none if ENV['SUPERMARKET_NO_SSL_VERIFY'] end |
Instance Method Details
#publish_ck(category) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/cookbook-release/supermarket.rb', line 23 def publish_ck(category) ck = ::Chef::Cookbook::CookbookVersionLoader.new('.') ck.load! cookbook = ck.cookbook_version # we have to provide a rest option otherwise it will try to load a # client.pem key ::Chef::CookbookUploader.new(cookbook, rest: 'fake_rest').validate_cookbooks tmp_cookbook_dir = Chef::CookbookSiteStreamingUploader.create_build_dir(cookbook) begin shell_out!("tar -czf #{cookbook.name}.tgz #{cookbook.name}", :cwd => tmp_cookbook_dir) rescue StandardError => e raise "Impossible to make a tarball out of the cookbook, #{e}" end begin upload("#{tmp_cookbook_dir}/#{cookbook.name}.tgz", category) puts "Uploaded to supermarket #{@url}" FileUtils.rm_rf tmp_cookbook_dir rescue StandardError => e $stderr.puts "Impossible to upload the cookbook to supermarket: #{e}" raise end end |
#upload(filename, category) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/cookbook-release/supermarket.rb', line 48 def upload(filename, category) http_resp = ::Chef::CookbookSiteStreamingUploader.post( @url, @user_id, @client_key, { tarball: File.open(filename), cookbook: { category: category }.to_json, }) res = ::Chef::JSONCompat.from_json(http_resp.body) if http_resp.code.to_i != 201 if res['error_messages'] if res['error_messages'][0] =~ /Version already exists/ raise "The same version of this cookbook already exists on the Opscode Cookbook Site." else raise "#{res['error_messages'][0]}" end else raise "Unknown error while sharing cookbook\nServer response: #{http_resp.body}" end end end |