Class: Chef::Knife::CookbookUpload

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

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args

Instance Method Summary collapse

Methods inherited from Chef::Knife

#ask_question, build_sub_class, #bulk_delete, #configure_chef, #confirm, #create_object, #delete_object, #edit_data, #edit_object, #file_exists_and_is_readable?, find_command, #format_for_display, #format_list_for_display, list_commands, load_commands, #load_from_file, #output, #pretty_print, #rest, #stdin, #stdout

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Instance Method Details

#runObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/chef/knife/cookbook_upload.rb', line 40

def run
  Chef::Log.debug "Uploading cookbooks from #{config[:cookbook_path]}"

  if config[:cookbook_path]
    Chef::Config[:cookbook_path] = config[:cookbook_path]
  else
    config[:cookbook_path] = Chef::Config[:cookbook_path]
  end

  if config[:all]
    cl = Chef::CookbookLoader.new
    cl.each do |cookbook|
      Chef::Log.info("** #{cookbook.name.to_s} **")
      upload_cookbook(cookbook.name.to_s)
    end
  else
    @name_args.each do |cb|
      Chef::Log.info("** #{cb} **")
      upload_cookbook(cb)
    end
  end
end

#upload_cookbook(cookbook_name) ⇒ Object



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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/chef/knife/cookbook_upload.rb', line 63

def upload_cookbook(cookbook_name)
  # Syntax check all cookbook paths rather than tmp_cookbook_dir as to
  # take advantage of the existing cache used/generated by knife cookbook
  # test.
  check = Chef::Knife::CookbookTest.new
  check.config[:cookbook_path] = config[:cookbook_path]
  check.name_args = [ cookbook_name ]
  check.run

  if cookbook_name =~ /^#{File::SEPARATOR}/
    child_folders = cookbook_name
    cookbook_name = File.basename(cookbook_name)
  else
    child_folders = config[:cookbook_path].inject([]) do |r, e|
      r << File.join(e, cookbook_name)
      r
    end
  end

  tmp_cookbook_tarball = Tempfile.new("chef-#{cookbook_name}")
  tmp_cookbook_tarball.close
  tarball_name = "#{tmp_cookbook_tarball.path}.tar.gz"
  File.unlink(tmp_cookbook_tarball.path)

  tmp_cookbook_path = Tempfile.new("chef-#{cookbook_name}-build")
  tmp_cookbook_path.close
  tmp_cookbook_dir = tmp_cookbook_path.path
  File.unlink(tmp_cookbook_dir)
  FileUtils.mkdir_p(tmp_cookbook_dir)

  Chef::Log.debug("Staging at #{tmp_cookbook_dir}")

  found_cookbook = false

  child_folders.each do |file_path|
    if File.directory?(file_path)
      found_cookbook = true
      Chef::Log.info("Copying from #{file_path} to #{tmp_cookbook_dir}")
      FileUtils.cp_r(file_path, tmp_cookbook_dir, :remove_destination => true, :preserve => true)
    else
      Chef::Log.info("Nothing to copy from #{file_path}")
    end
  end

  unless found_cookbook
    Chef::Log.fatal("Could not find cookbook #{cookbook_name}!")
    exit 17
  end

  # First, generate metadata
  kcm = Chef::Knife::CookbookMetadata.new
  kcm.config[:cookbook_path] = [ tmp_cookbook_dir ]
  kcm.name_args = [ cookbook_name ]
  kcm.run

  Chef::Log.info("Creating tarball at #{tarball_name}")
  Chef::Mixin::Command.run_command(
    :command => "tar -C #{tmp_cookbook_dir} -cvzf #{tarball_name} ./#{cookbook_name}"
  )

  begin
    cb = rest.get_rest("cookbooks/#{cookbook_name}")
    cookbook_uploaded = true
  rescue Net::HTTPServerException => e
    case e.response.code
    when "404"
      cookbook_uploaded = false
    when "401"
      Chef::Log.fatal "Failed to fetch remote cookbook '#{cookbook_name}' due to authentication failure (#{e}), check your client configuration (username, key)"
      exit 18
    end
  end

  if cookbook_uploaded
    Chef::StreamingCookbookUploader.put(
      "#{Chef::Config[:chef_server_url]}/cookbooks/#{cookbook_name}/_content",
      Chef::Config[:node_name],
      Chef::Config[:client_key],
      {
        :file => File.new(tarball_name),
        :name => cookbook_name
      }
    )
  else
    Chef::StreamingCookbookUploader.post(
      "#{Chef::Config[:chef_server_url]}/cookbooks",
      Chef::Config[:node_name],
      Chef::Config[:client_key],
      {
        :file => File.new(tarball_name),
        :name => cookbook_name
      }
    )
  end
  Chef::Log.info("Upload complete!")
  Chef::Log.debug("Removing local tarball at #{tarball_name}")
  FileUtils.rm_rf tarball_name
  Chef::Log.debug("Removing local staging directory at #{tmp_cookbook_dir}")
  FileUtils.rm_rf tmp_cookbook_dir
end