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

.add_gem(cookbook_path, name, version = nil) ⇒ Object



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

def self.add_gem(cookbook_path, name, version = nil)
  puts "adding #{name} gem to Gemfile"
  if version
    append_file(File.join(cookbook_path, 'Gemfile'), "gem '#{name}', '#{version}'")
  else
    append_file(File.join(cookbook_path, 'Gemfile'), "gem '#{name}'")
  end
end

.append_file(file, content) ⇒ Object



31
32
33
# File 'lib/meez/meez.rb', line 31

def self.append_file(file, content)
  File.open(file, 'a') { |f| f.write("#{content}\n") }
end

.bundle_install(cookbook_name, options) ⇒ Object



173
174
175
176
177
178
179
# File 'lib/meez/meez.rb', line 173

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' => "#{path}/Gemfile" }, 'bundle install') }
end

.gitignore(cookbook_path, file) ⇒ Object



44
45
46
# File 'lib/meez/meez.rb', line 44

def self.gitignore(cookbook_path, file)
  append_file(File.join(cookbook_path, '.gitignore'), file)
end

.init(cookbook_name, options) ⇒ Object



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

def self.init(cookbook_name, options)
  init_cookbook(cookbook_name, options)
  init_berkshelf(cookbook_name, options)
  init_vagrant(cookbook_name, options)
  init_knife(cookbook_name, options)
  init_rakefile(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)
  init_guard(cookbook_name, options)
  init_drone(cookbook_name, options)
  init_docker(cookbook_name, options)
end

.init_berkshelf(cookbook_name, options) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/meez/meez.rb', line 73

def self.init_berkshelf(cookbook_name, options)
  puts '* Initializing Berkshelf'
  path = File.join(options[:path], cookbook_name)
  require 'berkshelf'
  require 'berkshelf/base_generator'
  require 'berkshelf/init_generator'
  Berkshelf::InitGenerator.new(
    [path],
    skip_test_kitchen: true,
    skip_vagrant: true
  ).invoke_all
  contents = File.read(File.join(path, 'Gemfile'))
  newgemfile = contents.gsub("\ngem 'berkshelf'\n", "\ngem 'berkshelf', '> 3.1'\n")
  File.open(File.join(path, 'Gemfile'), 'w') { |f| f.write(newgemfile) }
end

.init_chefspec(cookbook_name, options) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/meez/meez.rb', line 106

def self.init_chefspec(cookbook_name, options)
  puts '* Initializing Chef Spec'
  path = File.join(options[:path], cookbook_name)
  spec_path = File.join(path, 'test', 'unit', 'spec')
  FileUtils.mkdir_p(spec_path)
  write_template('chefspec/spec_helper.rb.erb', spec_path, cookbook_name, options)
  write_template('chefspec/default_spec.rb.erb', spec_path, cookbook_name, options)
  gitignore(path, '.coverage/*')
  add_gem(path, 'chefspec')
end

.init_cookbook(cookbook_name, options) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/meez/meez.rb', line 48

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_docker(cookbook_name, options) ⇒ Object



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

def self.init_docker(cookbook_name, options)
  puts '* Initializing Docker'
  path = File.join(options[:path], cookbook_name)
  write_template('Dockerfile.erb', path, cookbook_name, options)
end

.init_drone(cookbook_name, options) ⇒ Object



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

def self.init_drone(cookbook_name, options)
  puts '* Initializing Drone'
  path = File.join(options[:path], cookbook_name)
  write_template('.drone.yml.erb', path, cookbook_name, options)
end

.init_foodcritic(cookbook_name, options) ⇒ Object



137
138
139
140
141
# File 'lib/meez/meez.rb', line 137

def self.init_foodcritic(cookbook_name, options)
  puts '* Initializing Food Critic'
  path = File.join(options[:path], cookbook_name)
  add_gem(path, 'foodcritic')
end

.init_git(cookbook_name, options) ⇒ Object



66
67
68
69
70
71
# File 'lib/meez/meez.rb', line 66

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_guard(cookbook_name, options) ⇒ Object



152
153
154
155
156
157
158
159
# File 'lib/meez/meez.rb', line 152

def self.init_guard(cookbook_name, options)
  puts '* Initializing Guard'
  path = File.join(options[:path], cookbook_name)
  write_template('Guardfile.erb', path, cookbook_name, options)
  add_gem(path, 'guard', '>= 2.6')
  add_gem(path, 'guard-rubocop', '>= 1.1')
  add_gem(path, 'guard-foodcritic', '>= 1.0.2')
end

.init_kitchenci(cookbook_name, options) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/meez/meez.rb', line 89

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
  options[:driver] ||= 'vagrant'
  write_template('.kitchen.yml.erb', path, cookbook_name, options)
  add_gem(path, 'kitchen-docker') if options[:driver].eql? 'docker'
end

.init_knife(cookbook_name, options) ⇒ Object



131
132
133
134
135
# File 'lib/meez/meez.rb', line 131

def self.init_knife(cookbook_name, options)
  puts '* Initializing Knife'
  path = File.join(options[:path], cookbook_name)
  add_gem(path, 'chef', '> 11')
end

.init_rakefile(cookbook_name, options) ⇒ Object



117
118
119
120
121
122
# File 'lib/meez/meez.rb', line 117

def self.init_rakefile(cookbook_name, options)
  puts '* Initializing Rakefile'
  path = File.join(options[:path], cookbook_name)
  write_template('Rakefile.erb', path, cookbook_name, options)
  add_gem(path, 'rake')
end

.init_rubocop(cookbook_name, options) ⇒ Object



124
125
126
127
128
129
# File 'lib/meez/meez.rb', line 124

def self.init_rubocop(cookbook_name, options)
  puts '* Initializing Rubocop'
  path = File.join(options[:path], cookbook_name)
  write_template('.rubocop.yml.erb', path, cookbook_name, options)
  add_gem(path, 'rubocop')
end

.init_serverspec(cookbook_name, options) ⇒ Object



143
144
145
146
147
148
149
150
# File 'lib/meez/meez.rb', line 143

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)
  write_template('serverspec/spec_helper.rb.erb', spec_path, cookbook_name, options)
  write_template('serverspec/default_spec.rb.erb', spec_path, cookbook_name, options)
end

.init_vagrant(cookbook_name, options) ⇒ Object



100
101
102
103
104
# File 'lib/meez/meez.rb', line 100

def self.init_vagrant(cookbook_name, options)
  puts '* Initializing Vagrantfile'
  path = File.join(options[:path], cookbook_name)
  write_template('Vagrantfile.erb', path, cookbook_name, options)
end

.write_template(name, path, cookbook_name, options) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/meez/meez.rb', line 22

def self.write_template(name, path, cookbook_name, options)
  require 'erb'
  template = File.join(File.dirname(__FILE__), '../../templates', name)
  target = File.join(path, File.basename(name, '.erb'))
  puts "\tCreating #{target} from template"
  content = ERB.new File.new(template).read
  File.open(target, 'w') { |f| f.write(content.result(binding)) }
end