Class: Meez

Inherits:
Object
  • Object
show all
Defined in:
lib/meez/meez.rb

Overview

The main Meez driver

Class Method Summary collapse

Class Method Details

.bundle_install(cookbook_name, options) ⇒ Object



227
228
229
230
231
232
233
# File 'lib/meez/meez.rb', line 227

def self.bundle_install(cookbook_name, options)
  require 'bundler'
  puts '* Running bundle install'
  path = File.join(options[:path], cookbook_name)
  puts "\t append .gitignore"
  Bundler.with_clean_env { exec({ 'BUNDLE_GEMFILE' => '/tmp/test/Gemfile' }, 'bundle install') }
end

.init(cookbook_name, options) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/meez/meez.rb', line 6

def self.init(cookbook_name, options)
  init_cookbook(cookbook_name, options)
  init_berkshelf(cookbook_name, options)
  init_strainer(cookbook_name, options)
  init_knife(cookbook_name, options)
  init_rubocop(cookbook_name, options)
  init_foodcritic(cookbook_name, options)
  init_chefspec(cookbook_name, options)
  init_serverspec(cookbook_name, options)
  init_kitchenci(cookbook_name, options)
  #bundle_install(cookbook_name, options)
end

.init_berkshelf(cookbook_name, options) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/meez/meez.rb', line 44

def self.init_berkshelf(cookbook_name, options)
  puts '* Initializing Berkshelf'
  path = File.join(options[:path], cookbook_name)
  gem 'berkshelf', '~> 2.0.11'
  require 'berkshelf'
  require 'berkshelf/base_generator'
  require 'berkshelf/init_generator'
  Berkshelf::InitGenerator.new(
    [path],
    {
      skip_test_kitchen: true
    }
  ).invoke_all
  File.open(File.join(path, 'Berksfile'), 'a') { |f| f.write("metadata\n") }
end

.init_chefspec(cookbook_name, options) ⇒ Object



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
# File 'lib/meez/meez.rb', line 88

def self.init_chefspec(cookbook_name, options)
  puts '* Initializing Chef Spec'
  path = File.join(options[:path], cookbook_name)
  spec_path = File.join(path, 'spec')
  Dir.mkdir(spec_path, 0755)
  puts "\tCreating #{File.join(spec_path, 'spec_helper.rb')}"

  File.open(File.join(spec_path, 'spec_helper.rb'), 'w') do |file|
    contents = "# Encoding: utf-8\nrequire 'chefspec'\nrequire 'chefspec/berkshelf'\n\n::LOG_LEVEL = :fatal\n::UBUNTU_OPTS = {\nplatform:   'ubuntu',\nversion:    '12.04',\nlog_level:  ::LOG_LEVEL\n}\n::CHEFSPEC_OPTS = {\nlog_level:  ::LOG_LEVEL\n}\n\ndef stub_resources\nend\n\nat_exit { ChefSpec::Coverage.report! }\n    EOF\n    file.write(contents)\n  end\n\n  puts \"\\tCreating \#{File.join(spec_path, 'default_spec.rb')}\"\n  File.open(File.join(spec_path, 'default_spec.rb'), 'w') do |file|\n    contents = <<-EOF\n# Encoding: utf-8\n\nrequire_relative 'spec_helper'\n\ndescribe '\#{cookbook_name}::default' do\ndescribe 'ubuntu' do\n  before do\n    @chef_run = ::ChefSpec::Runner.new(::UBUNTU_OPTS)\n    stub_resources\n    @chef_run.converge '\#{cookbook_name}::default'\n  end\n\n  it 'writes some tests' do\n    pending 'or it gets the hose again'\n  end\n\nend\nend\n    EOF\n    file.write(contents)\n  end\n\n  puts \"\\tAppend Gemfile\"\n  File.open(File.join(path, 'Gemfile'), 'a') { |f| f.write(\"gem 'chefspec', '~> 3.1.4'\\n\") }\n  File.open(File.join(path, 'Strainerfile'), 'a') { |f| f.write(\"chefspec:   bundle exec rspec $SANDBOX/$COOKBOOK/spec\\n\") }\nend\n"

.init_cookbook(cookbook_name, options) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/meez/meez.rb', line 19

def self.init_cookbook(cookbook_name, options)
  require 'chef/knife/cookbook_create'
  puts '* Initializing Cookbook'
  path = File.join(options[:path], cookbook_name)
  create_cookbook = Chef::Knife::CookbookCreate.new
  create_cookbook.name_args = [cookbook_name]
  create_cookbook.config[:cookbook_path]      = options[:path]
  create_cookbook.config[:cookbook_copyright] = options[:copyright] || 'YOUR_COMPANY_NAME'
  create_cookbook.config[:cookbook_license]   = options[:license]   || 'YOUR_EMAIL'
  create_cookbook.config[:cookbook_email]     = options[:email]     || 'none'
  create_cookbook.run
  %w{ metadata.rb recipes/default.rb }.each do |file|
    puts "\tRewriting #{file}"
    contents = "# Encoding: utf-8\n#{File.read(File.join(path, file))}"
    File.open(File.join(path, file), 'w') { |f| f.write(contents) }
  end
end

.init_foodcritic(cookbook_name, options) ⇒ Object



176
177
178
179
180
181
182
183
# File 'lib/meez/meez.rb', line 176

def self.init_foodcritic(cookbook_name, options)
  puts '* Initializing Food Critic'
  path = File.join(options[:path], cookbook_name)
  puts "\tAppend Gemfile"
  File.open(File.join(path, 'Gemfile'), 'a') { |f| f.write("gem 'foodcritic', '~> 3.0.0'\n") }
  puts "\tAppend Strainerfile"
  File.open(File.join(path, 'Strainerfile'), 'a') { |f| f.write("foodcritic: bundle exec foodcritic -f any $SANDBOX/$COOKBOOK\n") }
end

.init_git(cookbook_name, options) ⇒ Object



37
38
39
40
41
42
# File 'lib/meez/meez.rb', line 37

def self.init_git(cookbook_name, options)
  puts '* Initializing GIT repo'
  path = File.join(options[:path], cookbook_name)
  require 'git'
  Git.init( path, { repository: path } )
end

.init_kitchenci(cookbook_name, options) ⇒ Object



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
# File 'lib/meez/meez.rb', line 60

def self.init_kitchenci(cookbook_name, options)
  puts '* Initializing Test Kitchen'
  path = File.join(options[:path], cookbook_name)
  require 'kitchen'
  require 'kitchen/generator/init'
  Kitchen::Generator::Init.new([], {}, destination_root: path).invoke_all
  File.open(File.join(path, 'Strainerfile'), 'a') { |f| f.write("kitchen:   bundle exec kitchen test --destroy=always\n") }
  File.open(File.join(path, '.kitchen.yml'), 'w') do |file|
    contents = "---\ndriver:\nname: vagrant\n\nprovisioner:\nname: chef_solo\n\nplatforms:\n- name: ubuntu-12.04\n\nsuites:\n- name: default\n  run_list: recipe[\#{cookbook_name}::default]\n  attributes:\n    EOF\n    file.write(contents)\n  end\nend\n"

.init_knife(cookbook_name, options) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/meez/meez.rb', line 167

def self.init_knife(cookbook_name, options)
  puts '* Initializing Knife'
  path = File.join(options[:path], cookbook_name)
  puts "\tAppend Gemfile"
  File.open(File.join(path, 'Gemfile'), 'a') { |f| f.write("gem 'chef', '~> 11.8.0'\n") }
  puts "\tAppend Strainerfile"
  File.open(File.join(path, 'Strainerfile'), 'a') { |f| f.write("knife test: bundle exec knife cookbook test $COOKBOOK\n") }
end

.init_rubocop(cookbook_name, options) ⇒ Object



158
159
160
161
162
163
164
165
# File 'lib/meez/meez.rb', line 158

def self.init_rubocop(cookbook_name, options)
  puts '* Initializing Rubocop'
  path = File.join(options[:path], cookbook_name)
  puts "\tAppend Gemfile"
  File.open(File.join(path, 'Gemfile'), 'a') { |f| f.write("gem 'rubocop', '~> 0.16.0'\n") }
  puts "\tAppend Strainerfile"
  File.open(File.join(path, 'Strainerfile'), 'a') { |f| f.write("rubocop:    bundle exec rubocop $COOKBOOK\n") }
end

.init_serverspec(cookbook_name, options) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/meez/meez.rb', line 185

def self.init_serverspec(cookbook_name, options)
  puts '* Initializing Server Spec'
  path = File.join(options[:path], cookbook_name)
  spec_path = File.join(path, 'test', 'integration', 'default', 'serverspec')
  FileUtils.mkdir_p(spec_path)
  puts "\t Creating #{File.join(spec_path, 'spec_helper.rb')}"

  File.open(File.join(spec_path, 'spec_helper.rb'), 'w') do |file|
    contents = "# Encoding: utf-8\nrequire 'serverspec'\n\ninclude Serverspec::Helper::Exec\ninclude Serverspec::Helper::DetectOS\n\nRSpec.configure do |c|\nc.before :all do\n  c.path = '/sbin:/usr/bin'\nend\nend\n    EOF\n    file.write(contents)\n  end\n\n  puts \"\\tCreating \#{File.join(spec_path, 'default_spec.rb')}\"\n  File.open(File.join(spec_path, 'default_spec.rb'), 'w') do |file|\n    contents = <<-EOF\n# Encoding: utf-8\n\nrequire_relative 'spec_helper'\n\ndescribe 'default' do\nit { pending 'write some tests' }\nend\n    EOF\n    file.write(contents)\n  end\n\n  puts \"\\tAppend Gemfile\"\n  File.open(File.join(path, 'Gemfile'), 'a') { |f| f.write(\"gem 'serverspec', '~> 0.14.2'\\n\") }\nend\n"

.init_strainer(cookbook_name, options) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/meez/meez.rb', line 149

def self.init_strainer(cookbook_name, options)
  puts '* Initializing Strainer'
  path = File.join(options[:path], cookbook_name)
  puts "\tCreating #{File.join(path, 'Strainerfile')}"
  File.open(File.join(path, 'Strainerfile'), 'w') { |f| f.write("# Strainerfile\n") }
  puts "\tAppend Gemfile"
  File.open(File.join(path, 'Gemfile'), 'a') { |f| f.write("gem 'strainer', '~> 3.3.0'\n") }
end