Class: Builderator::Tasks::Cookbook

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/builderator/tasks/cookbook.rb

Instance Method Summary collapse

Instance Method Details

#build(cookbook = nil) ⇒ Object



33
34
35
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
# File 'lib/builderator/tasks/cookbook.rb', line 33

def build(cookbook = nil)
  Util::Cookbook.path(cookbook) unless cookbook.nil?

  ## Generate metadata.json
   = invoke(Tasks::Cookbook, :metadata, [], options)

  ## Create a gzipped tarball and add cookbook files to it. We avoid
  ##   buffering this in memory (e.g. using StringIO) at all cost
  ##   to keep large files from gumming things up.
  say_status :package, "cookbook into #{ metadata.archive }"
  .archive.open('wb') do |package|
    Zlib::GzipWriter.wrap(package) do |gz|
      Gem::Package::TarWriter.new(gz) do |tar|
        .files.each do |f|
          f_stat = File.stat(f)

          ## Add directories
          next tar.mkdir(Util::Cookbook.archive_path(, f).to_s, f_stat.mode) if File.directory?(f)

          ## Add files
          tar.add_file_simple(Util::Cookbook.archive_path(, f).to_s, f_stat.mode, f_stat.size) do |entry|
            f.open('rb') { |h| entry.write(h.read) }
          end
        end
      end
    end
  end

  
end

#metadata(cookbook = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/builderator/tasks/cookbook.rb', line 21

def (cookbook = nil)
  Util::Cookbook.path(cookbook) unless cookbook.nil?
   = Util::Cookbook.

  invoke 'version:current', [], options if options['version']
  say_status :metadata, "for cookbook #{ metadata.name }@#{ metadata.version }"
  create_file Util::Cookbook.path.join('metadata.json').to_s, .to_json, :force => true

  
end

#push(cookbook = nil) ⇒ Object



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
# File 'lib/builderator/tasks/cookbook.rb', line 71

def push(cookbook = nil)
  Chef::Config.from_file(options['chef-config'])
  Util::Cookbook.path(cookbook) unless cookbook.nil?

  ## Set defaults after Chef::Config is loaded
  options['site'] ||= Chef::Config.knife['supermarket_site'] || 'https://supermarket.chef.io/'
  options['user'] ||= Chef::Config.knife['supermarket_user'] || Chef::Config.node_name
  options['key'] ||= Chef::Config.knife['supermarket_key'] || Chef::Config.client_key

  ## Build the cookbook taball
   = invoke(Tasks::Cookbook, :build, [cookbook], options)
  say_status :upload, "cookbook #{ metadata.name }@#{ metadata.version } to #{ options['site'] }"

  .archive.open('rb') do |c|
    http_resp = Chef::CookbookSiteStreamingUploader.post(
      File.join(options['site'], '/api/v1/cookbooks'),
      options['user'],
      options['key'],
      :tarball => c,
      :cookbook => { :category => '' }.to_json
    )

    if http_resp.code.to_i != 201
      say_status :error, "Error uploading cookbook: #{ http_resp.code } #{ http_resp.message }", :red
      say http_resp.body
      exit(1)
    end
  end
end

#version(cookbook) ⇒ Object



103
104
105
106
# File 'lib/builderator/tasks/cookbook.rb', line 103

def version(cookbook)
  Util::Cookbook.path(File.join(options['path'], cookbook))
  puts Util::Cookbook..version
end