Class: Chef::Knife::CookbookCreate

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/cookbook_create.rb

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args, #ui

Instance Method Summary collapse

Methods inherited from Chef::Knife

#api_key, category, common_name, #configure_chef, #create_object, #delete_object, deps, #format_rest_error, guess_category, #highlight_config_error, #humanize_exception, #humanize_http_exception, inherited, #initialize, list_commands, load_commands, load_deps, msg, #noauth_rest, #parse_options, #read_config_file, reset_subcommands!, #rest, run, #run_with_pretty_exceptions, #server_url, #show_usage, snake_case_name, subcommand_category, subcommand_class_from, subcommand_loader, subcommands, subcommands_by_category, ui, unnamed?, #username

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Constructor Details

This class inherits a constructor from Chef::Knife

Instance Method Details

#create_cookbook(dir, cookbook_name, copyright, license) ⇒ Object


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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/chef/knife/cookbook_create.rb', line 81

def create_cookbook(dir, cookbook_name, copyright, license)
  msg("** Creating cookbook #{cookbook_name}")
  FileUtils.mkdir_p "#{File.join(dir, cookbook_name, "attributes")}"
  FileUtils.mkdir_p "#{File.join(dir, cookbook_name, "recipes")}"
  FileUtils.mkdir_p "#{File.join(dir, cookbook_name, "definitions")}"
  FileUtils.mkdir_p "#{File.join(dir, cookbook_name, "libraries")}"
  FileUtils.mkdir_p "#{File.join(dir, cookbook_name, "resources")}"
  FileUtils.mkdir_p "#{File.join(dir, cookbook_name, "providers")}"
  FileUtils.mkdir_p "#{File.join(dir, cookbook_name, "files", "default")}"
  FileUtils.mkdir_p "#{File.join(dir, cookbook_name, "templates", "default")}"
  unless File.exists?(File.join(dir, cookbook_name, "recipes", "default.rb"))
    open(File.join(dir, cookbook_name, "recipes", "default.rb"), "w") do |file|
      file.puts "#\n# Cookbook Name:: \#{cookbook_name}\n# Recipe:: default\n#\n# Copyright \#{Time.now.year}, \#{copyright}\n#\n"
      case license
      when "apachev2"
        file.puts "# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n#\n"
      when "gplv2"
        file.puts "# This program is free software; you can redistribute it and/or modify\n# it under the terms of the GNU General Public License as published by\n# the Free Software Foundation; either version 2 of the License, or\n# (at your option) any later version.\n#\n# This program is distributed in the hope that it will be useful,\n# but WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n# GNU General Public License for more details.\n#\n# You should have received a copy of the GNU General Public License along\n# with this program; if not, write to the Free Software Foundation, Inc.,\n# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\n#\n"
      when "gplv3"
        file.puts "# This program is free software: you can redistribute it and/or modify\n# it under the terms of the GNU General Public License as published by\n# the Free Software Foundation, either version 3 of the License, or\n# (at your option) any later version.\n#\n# This program is distributed in the hope that it will be useful,\n# but WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n# GNU General Public License for more details.\n#\n# You should have received a copy of the GNU General Public License\n# along with this program.  If not, see <http://www.gnu.org/licenses/>.\n#\n"
      when "mit"
        file.puts "# Permission is hereby granted, free of charge, to any person obtaining\n# a copy of this software and associated documentation files (the\n# \"Software\"), to deal in the Software without restriction, including\n# without limitation the rights to use, copy, modify, merge, publish,\n# distribute, sublicense, and/or sell copies of the Software, and to\n# permit persons to whom the Software is furnished to do so, subject to\n# the following conditions:\n#\n# The above copyright notice and this permission notice shall be\n# included in all copies or substantial portions of the Software.\n#\n# THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\n# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\n# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\n# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n#\n"
      when "none"
        file.puts "# All rights reserved - Do Not Redistribute\n#\n"
      end
    end
  end
end

#create_metadata(dir, cookbook_name, copyright, email, license, readme_format) ⇒ Object


229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/chef/knife/cookbook_create.rb', line 229

def (dir, cookbook_name, copyright, email, license,readme_format)
  msg("** Creating metadata for cookbook: #{cookbook_name}")

  license_name = case license
                 when "apachev2"
                   "Apache 2.0"
                 when "gplv2"
                   "GNU Public License 2.0"
                 when "gplv3"
                   "GNU Public License 3.0"
                 when "mit"
                   "MIT"
                 when "none"
                   "All rights reserved"
                 end

  unless File.exists?(File.join(dir, cookbook_name, "metadata.rb"))
    open(File.join(dir, cookbook_name, "metadata.rb"), "w") do |file|
      if File.exists?(File.join(dir, cookbook_name, "README.#{readme_format}"))
        long_description = "long_description IO.read(File.join(File.dirname(__FILE__), 'README.#{readme_format}'))"
      end
      file.puts "maintainer       \"\#{copyright}\"\nmaintainer_email \"\#{email}\"\nlicense          \"\#{license_name}\"\ndescription      \"Installs/Configures \#{cookbook_name}\"\n\#{long_description}\nversion          \"0.0.1\"\n"
    end
  end
end

#create_readme(dir, cookbook_name, readme_format) ⇒ Object


182
183
184
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
226
227
# File 'lib/chef/knife/cookbook_create.rb', line 182

def create_readme(dir, cookbook_name,readme_format)
  msg("** Creating README for cookbook: #{cookbook_name}")
  unless File.exists?(File.join(dir, cookbook_name, "README.#{readme_format}"))
    open(File.join(dir, cookbook_name, "README.#{readme_format}"), "w") do |file|
      case readme_format
      when "rdoc"
        file.puts "= DESCRIPTION:\n\n= REQUIREMENTS:\n\n= ATTRIBUTES:\n\n= USAGE:\n\n"
      when "md","mkd","txt"
        file.puts "Description\n===========\n\nRequirements\n============\n\nAttributes\n==========\n\nUsage\n=====\n\n"
      else
        file.puts "Description\n\nRequirements\n\nAttributes\n\nUsage\n\n"
      end
    end
  end
end

#runObject


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/chef/knife/cookbook_create.rb', line 58

def run
  self.config = Chef::Config.merge!(config)
  if @name_args.length < 1
    show_usage
    ui.fatal("You must specify a cookbook name")
    exit 1
  end

  if default_cookbook_path_empty? && parameter_empty?(config[:cookbook_path])
    raise ArgumentError, "Default cookbook_path is not specified in the knife.rb config file, and a value to -o is not provided. Nowhere to write the new cookbook to."
  end

  cookbook_path = File.expand_path(Array(config[:cookbook_path]).first)
  cookbook_name = @name_args.first
  copyright = config[:cookbook_copyright] || "YOUR_COMPANY_NAME"
  email = config[:cookbook_email] || "YOUR_EMAIL"
  license = ((config[:cookbook_license] != "false") && config[:cookbook_license]) || "none"
  readme_format = ((config[:readme_format] != "false") && config[:readme_format]) || "md"
  create_cookbook(cookbook_path,cookbook_name, copyright, license)
  create_readme(cookbook_path,cookbook_name,readme_format)
  (cookbook_path,cookbook_name, copyright, email, license,readme_format)
end