Class: Chef::Knife::CookbookSiteShare

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

Constant Summary

Constants included from Mixin::ShellOut

Mixin::ShellOut::DEPRECATED_OPTIONS

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args, #ui

Instance Method Summary collapse

Methods included from Mixin::ShellOut

#run_command_compatible_options, #shell_out, #shell_out!, #shell_out_with_systems_locale, #shell_out_with_systems_locale!

Methods inherited from Chef::Knife

#api_key, #apply_computed_config, category, chef_config_dir, common_name, #config_file_settings, config_loader, #configure_chef, #create_object, #delete_object, dependency_loaders, deps, #format_rest_error, guess_category, #humanize_exception, #humanize_http_exception, inherited, #initialize, load_commands, load_config, load_deps, #maybe_setup_fips, #merge_configs, msg, #noauth_rest, #parse_options, reset_config_loader!, reset_subcommands!, #rest, run, #run_with_pretty_exceptions, #server_url, #show_usage, snake_case_name, subcommand_category, subcommand_class_from, subcommand_files, subcommand_loader, subcommands, subcommands_by_category, #test_mandatory_field, ui, unnamed?, use_separate_defaults?, #username

Methods included from Mixin::ConvertToClassName

#constantize, #convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #normalize_snake_case_name, #snake_case_basename

Methods included from Mixin::PathSanity

#enforce_path_sanity

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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/chef/knife/cookbook_site_share.rb', line 124

def do_upload(cookbook_filename, cookbook_category, user_id, user_secret_filename)
  uri = "https://supermarket.chef.io/api/v1/cookbooks"

  category_string = Chef::JSONCompat.to_json({ "category" => cookbook_category })

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

#get_category(cookbook_name) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/chef/knife/cookbook_site_share.rb', line 108

def get_category(cookbook_name)
  begin
    data = noauth_rest.get("https://supermarket.chef.io/api/v1/cookbooks/#{@name_args[0]}")
    if !data["category"] && data["error_code"]
      ui.fatal("Received an error from Supermarket: #{data["error_code"]}. On the first time you upload it, you are required to specify the category you want to share this cookbook to.")
      exit(1)
    else
      data["category"]
    end
  rescue => e
    ui.fatal("Unable to reach Supermarket: #{e.message}. Increase log verbosity (-VV) for more information.")
    Chef::Log.debug("\n#{e.backtrace.join("\n")}")
    exit(1)
  end
end

#runObject



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

def run
  config[:cookbook_path] ||= Chef::Config[:cookbook_path]

  if @name_args.length < 1
    show_usage
    ui.fatal("You must specify the cookbook name.")
    exit(1)
  elsif @name_args.length < 2
    cookbook_name = @name_args[0]
    category = get_category(cookbook_name)
  else
    cookbook_name = @name_args[0]
    category = @name_args[1]
  end

  cl = Chef::CookbookLoader.new(config[:cookbook_path])
  if cl.cookbook_exists?(cookbook_name)
    cookbook = cl[cookbook_name]
    Chef::CookbookUploader.new(cookbook).validate_cookbooks
    tmp_cookbook_dir = Chef::CookbookSiteStreamingUploader.create_build_dir(cookbook)
    begin
      Chef::Log.debug("Temp cookbook directory is #{tmp_cookbook_dir.inspect}")
      ui.info("Making tarball #{cookbook_name}.tgz")
      shell_out!("#{tar_cmd} -czf #{cookbook_name}.tgz #{cookbook_name}", :cwd => tmp_cookbook_dir)
    rescue => e
      ui.error("Error making tarball #{cookbook_name}.tgz: #{e.message}. Increase log verbosity (-VV) for more information.")
      Chef::Log.debug("\n#{e.backtrace.join("\n")}")
      exit(1)
    end

    if config[:dry_run]
      ui.info("Not uploading #{cookbook_name}.tgz due to --dry-run flag.")
      result = shell_out!("#{tar_cmd} -tzf #{cookbook_name}.tgz", :cwd => tmp_cookbook_dir)
      ui.info(result.stdout)
      FileUtils.rm_rf tmp_cookbook_dir
      return
    end

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

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

#tar_cmdObject



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/chef/knife/cookbook_site_share.rb', line 153

def tar_cmd
  if !@tar_cmd
    @tar_cmd = "tar"
    begin
      # Unix and Mac only - prefer gnutar
      if shell_out("which gnutar").exitstatus.equal?(0)
        @tar_cmd = "gnutar"
      end
    rescue Errno::ENOENT
    end
  end
  @tar_cmd
end