Class: CapsuleCD::Chef::ChefEngine

Inherits:
Engine
  • Object
show all
Defined in:
lib/capsulecd/chef/chef_engine.rb

Instance Attribute Summary

Attributes inherited from Engine

#config

Instance Method Summary collapse

Methods inherited from Engine

#initialize, #post_build_step, #post_package_step, #post_release_step, #post_runner_retrieve_payload, #post_source_configure, #post_source_process_pull_request_payload, #post_source_process_push_payload, #post_source_release, #post_test_step, #pre_build_step, #pre_package_step, #pre_release_step, #pre_runner_retrieve_payload, #pre_source_configure, #pre_source_process_pull_request_payload, #pre_source_process_push_payload, #pre_source_release, #pre_test_step, #start

Constructor Details

This class inherits a constructor from CapsuleCD::Engine

Instance Method Details

#build_stepObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/capsulecd/chef/chef_engine.rb', line 12

def build_step
  super
  # validate that the chef metadata.rb file exists

  unless File.exist?(@source_git_local_path + '/metadata.rb')
    fail CapsuleCD::Error::BuildPackageInvalid, 'metadata.rb file is required to process Chef cookbook'
  end

  # bump up the chef cookbook version
   = CapsuleCD::Chef::ChefHelper.(@source_git_local_path)
   = CapsuleCD::Chef::ChefHelper.()
  next_version = bump_version(SemVer.parse(.version))

   = .gsub(/(version\s+['"])[0-9\.]+(['"])/, "\\1#{next_version}\\2")
  CapsuleCD::Chef::ChefHelper.(@source_git_local_path, )

  # TODO: check if this cookbook name and version already exist.

  # check for/create any required missing folders/files
  # Berksfile.lock and Gemfile.lock are not required to be commited, but they should be.
  unless File.exist?(@source_git_local_path + '/Rakefile')
    File.open(@source_git_local_path + '/Rakefile', 'w') { |file| file.write('task :test') }
  end
  unless File.exist?(@source_git_local_path + '/Berksfile')
    File.open(@source_git_local_path + '/Berksfile', 'w') { |file| file.write('site :opscode') }
  end
  unless File.exist?(@source_git_local_path + '/Gemfile')
    File.open(@source_git_local_path + '/Gemfile', 'w') { |file| file.write('source "https://rubygems.org"') }
  end
  unless File.exist?(@source_git_local_path + '/spec')
    FileUtils.mkdir(@source_git_local_path + '/spec')
  end
  unless File.exist?(@source_git_local_path + '/.gitignore')
    CapsuleCD::GitUtils.create_gitignore(@source_git_local_path, ['ChefCookbook'])
  end
end

#package_stepObject



104
105
106
107
108
109
110
111
112
113
# File 'lib/capsulecd/chef/chef_engine.rb', line 104

def package_step
  super
   = CapsuleCD::Chef::ChefHelper.(@source_git_local_path)
   = CapsuleCD::Chef::ChefHelper.()
  next_version = SemVer.parse(.version)
  # commit changes to the cookbook. (test run occurs before this, and it should clean up any instrumentation files, created,
  # as they will be included in the commmit and any release artifacts)
  CapsuleCD::GitUtils.commit(@source_git_local_path, "(v#{next_version}) Automated packaging of release by CapsuleCD")
  @source_release_commit = CapsuleCD::GitUtils.tag(@source_git_local_path, "v#{next_version}")
end

#release_stepObject

this step should push the release to the package repository (ie. npm, chef supermarket, rubygems)



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
163
164
165
166
167
168
# File 'lib/capsulecd/chef/chef_engine.rb', line 116

def release_step
  super
  puts @source_git_parent_path
  pem_path = File.expand_path('~/client.pem')
  knife_path = File.expand_path('~/knife.rb')

  unless @config.chef_supermarket_username || @config.chef_supermarket_key
    fail CapsuleCD::Error::ReleaseCredentialsMissing, 'cannot deploy cookbook to supermarket, credentials missing'
  end

  # knife is really sensitive to folder names. The cookbook name MUST match the folder name otherwise knife throws up
  # when doing a knife cookbook share. So we're going to make a new tmp directory, create a subdirectory with the EXACT
  # cookbook name, and then copy the cookbook contents into it. Yeah yeah, its pretty nasty, but blame Chef.
   = CapsuleCD::Chef::ChefHelper.(@source_git_local_path)
   = CapsuleCD::Chef::ChefHelper.()

  Dir.mktmpdir {|tmp_parent_path|
    # create cookbook folder
    tmp_local_path = File.join(tmp_parent_path, .name)
    FileUtils.mkdir_p(tmp_local_path)
    FileUtils.copy_entry(@source_git_local_path, tmp_local_path)

    # write the knife.rb config jfile.
    File.open(knife_path, 'w+') do |file|
      file.write(<<-EOT.gsub(/^\s+/, '')
      node_name "#{@config.chef_supermarket_username }" # Replace with the login name you use to login to the Supermarket.
      client_key "#{pem_path}" # Define the path to wherever your client.pem file lives.  This is the key you generated when you signed up for a Chef account.
      cookbook_path [ '#{tmp_parent_path}' ] # Directory where the cookbook you're uploading resides.
      EOT
      )
    end

    File.open(pem_path, 'w+') do |file|
      file.write(@config.chef_supermarket_key)
    end

    command = "knife cookbook site share #{.name} #{@config.chef_supermarket_type} -c #{knife_path}"
    Open3.popen3(command) do |_stdin, stdout, stderr, external|
      { stdout: stdout, stderr: stderr }. each do |name, stream_buffer|
        Thread.new do
          until (line = stream_buffer.gets).nil?
            puts "#{name} -> #{line}"
          end
        end
      end
      # wait for process
      external.join
      unless external.value.success?
        fail CapsuleCD::Error::ReleasePackageError, 'knife cookbook upload to supermarket failed'
      end
    end
  }
end

#test_stepObject



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
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
# File 'lib/capsulecd/chef/chef_engine.rb', line 49

def test_step
  super

  # the cookbook has already been downloaded. lets make sure all its dependencies are available.
  Open3.popen3('berks install', chdir: @source_git_local_path) do |_stdin, stdout, stderr, external|
    { stdout: stdout, stderr: stderr }. each do |name, stream_buffer|
      Thread.new do
        until (line = stream_buffer.gets).nil?
          puts "#{name} -> #{line}"
        end
      end
    end
    # wait for process
    external.join
    unless external.value.success?
      fail CapsuleCD::Error::TestDependenciesError, 'berks install failed. Check cookbook dependencies'
    end
  end

  # lets download all its gem dependencies
  Bundler.with_clean_env do
    Open3.popen3('bundle install', chdir: @source_git_local_path) do |_stdin, stdout, stderr, external|
      { stdout: stdout, stderr: stderr }. each do |name, stream_buffer|
        Thread.new do
          until (line = stream_buffer.gets).nil?
            puts "#{name} -> #{line}"
          end
        end
      end
      # wait for process
      external.join
      unless external.value.success?
        fail CapsuleCD::Error::TestDependenciesError, 'bundle install failed. Check gem dependencies'
      end
    end

    # run test command
    test_cmd = @config.engine_cmd_test || 'rake test'
    Open3.popen3(test_cmd, chdir: @source_git_local_path) do |_stdin, stdout, stderr, external|
      { stdout: stdout, stderr: stderr }. each do |name, stream_buffer|
        Thread.new do
          until (line = stream_buffer.gets).nil?
            puts "#{name} -> #{line}"
          end
        end
      end
      # wait for process
      external.join
      unless external.value.success?
        fail CapsuleCD::Error::TestRunnerError, test_cmd + ' failed. Check log for exact error'
      end
    end unless @config.engine_disable_test
  end
end