Class: Chef::Knife::CookbookSiteShare

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/cookbook_site_share.rb

Constant Summary

Constants inherited from Chef::Knife

DEFAULT_SUBCOMMAND_FILES

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args

Instance Method Summary collapse

Methods inherited from Chef::Knife

#ask_question, #bulk_delete, category, #configure_chef, #confirm, #create_object, #delete_object, #edit_data, #edit_object, #file_exists_and_is_readable?, #format_for_display, #format_list_for_display, guess_category, inherited, #initialize, list_commands, load_commands, #load_from_file, #msg, msg, #output, #parse_options, #pretty_print, #rest, run, #show_usage, snake_case_name, #stdin, #stdout, subcommand_category, subcommand_class_from, subcommands, subcommands_by_category, unnamed?

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Constructor Details

This class inherits a constructor from Chef::Knife

Instance Method Details

#do_upload(cookbook_filename, cookbook_category, user_id, user_secret_filename) ⇒ Object



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
# File 'lib/chef/knife/cookbook_site_share.rb', line 78

def do_upload(cookbook_filename, cookbook_category, user_id, user_secret_filename)
  uri = "http://cookbooks.opscode.com/api/v1/cookbooks"

  category_string = { 'category'=>cookbook_category }.to_json

  http_resp = Chef::CookbookSiteStreamingUploader.post(uri, user_id, user_secret_filename, {
    :tarball => File.open(cookbook_filename),
    :cookbook => category_string
  })

  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/
        Chef::Log.error "The same version of this cookbook already exists on the Opscode Cookbook Site."
        exit(1)
      else
        Chef::Log.error "#{res['error_messages'][0]}"
        exit(1)
      end
    else
      Chef::Log.error "Unknown error while sharing cookbook"
      Chef::Log.error "Server response: #{http_resp.body}"
      exit(1)
    end
  end
  res
end

#runObject



36
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
# File 'lib/chef/knife/cookbook_site_share.rb', line 36

def run
  if @name_args.length < 2
    show_usage
    Chef::Log.fatal("You must specify the cookbook name and the category you want to share this cookbook to.")
    exit 1
  end

  cookbook_name = @name_args[0]
  category = @name_args[1]
  cl = Chef::CookbookLoader.new
  if cl.cookbook_exists?(cookbook_name)
    cookbook = cl[cookbook_name]
    Chef::CookbookUploader.validate_cookbook(cookbook)
    tmp_cookbook_dir = Chef::CookbookSiteStreamingUploader.create_build_dir(cookbook)
    begin
      Chef::Log.debug("temp cookbook directory is #{tmp_cookbook_dir.inspect}")
      Chef::Log.info("Making tarball #{cookbook_name}.tgz")
      Chef::Mixin::Command.run_command(:command => "tar -czf #{cookbook_name}.tgz #{cookbook_name}", :cwd => tmp_cookbook_dir)
    rescue => e
      Chef::Log.error("Error making tarball #{cookbook_name}.tgz: #{e.message}. Set log level to debug (-l debug) for more information.")
      Chef::Log.debug("\n#{e.backtrace.join("\n")}")
      exit(1)
    end

    begin
      do_upload("#{tmp_cookbook_dir}/#{cookbook_name}.tgz", category, Chef::Config[:node_name], Chef::Config[:client_key])
      Chef::Log.info("Upload complete!")
      Chef::Log.debug("Removing local staging directory at #{tmp_cookbook_dir}")
      FileUtils.rm_rf tmp_cookbook_dir
    rescue => e
      Chef::Log.error("Error uploading cookbook #{cookbook_name} to the Opscode Cookbook Site: #{e.message}. Set log level to debug (-l debug) for more information.")
      Chef::Log.debug("\n#{e.backtrace.join("\n")}")
      exit(1)
    end

  else
    Chef::Log.error("Could not find cookbook #{cookbook_name} in your cookbook path.")
    exit(1)
  end

end