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, #json_pretty_print, list_commands, load_commands, #load_from_file, #pretty_print, #rest

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string

Instance Method Details

#runObject



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

def run 
  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
    upload_cookbook(@name_args[0]) 
  end
end

#test_ruby(cookbook_dir) ⇒ Object



59
60
61
62
63
64
# File 'lib/chef/knife/cookbook_upload.rb', line 59

def test_ruby(cookbook_dir)
  Dir[File.join(cookbook_dir, '**', '*.rb')].each do |ruby_file|
    Chef::Log.info("Testing #{ruby_file} for syntax errors...")
    Chef::Mixin::Command.run_command(:command => "ruby -c #{ruby_file}")
  end
end

#test_templates(cookbook_dir) ⇒ Object



66
67
68
69
70
71
# File 'lib/chef/knife/cookbook_upload.rb', line 66

def test_templates(cookbook_dir)
  Dir[File.join(cookbook_dir, '**', '*.erb')].each do |erb_file|
    Chef::Log.info("Testing template #{erb_file} for syntax errors...")
    Chef::Mixin::Command.run_command(:command => "sh -c 'erubis -x #{erb_file} | ruby -c'")
  end
end

#upload_cookbook(cookbook_name) ⇒ Object



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 73

def upload_cookbook(cookbook_name)

  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)
    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

  test_ruby(tmp_cookbook_dir)
  test_templates(tmp_cookbook_dir)

  # 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
    cookbook_uploaded = false
  end
  Chef::Log.info("Uploading #{cookbook_name} (#{cookbook_uploaded ? 'new version' : 'first time'})")
  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