chef 0.9.6 → 0.9.8.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (127) hide show
  1. data/lib/chef/application.rb +19 -3
  2. data/lib/chef/application/client.rb +7 -8
  3. data/lib/chef/application/knife.rb +9 -3
  4. data/lib/chef/application/solo.rb +5 -5
  5. data/lib/chef/cache/checksum.rb +2 -2
  6. data/lib/chef/client.rb +30 -64
  7. data/lib/chef/config.rb +52 -30
  8. data/lib/chef/cookbook/metadata.rb +12 -1
  9. data/lib/chef/cookbook_site_streaming_uploader.rb +211 -0
  10. data/lib/chef/cookbook_uploader.rb +102 -0
  11. data/lib/chef/cookbook_version.rb +47 -30
  12. data/lib/chef/couchdb.rb +1 -11
  13. data/lib/chef/exceptions.rb +1 -0
  14. data/lib/chef/file_access_control.rb +1 -1
  15. data/lib/chef/index_queue/amqp_client.rb +10 -3
  16. data/lib/chef/knife.rb +154 -58
  17. data/lib/chef/knife/bootstrap.rb +84 -48
  18. data/lib/chef/knife/bootstrap/centos5-gems.erb +41 -0
  19. data/lib/chef/knife/bootstrap/fedora13-gems.erb +38 -0
  20. data/lib/chef/knife/bootstrap/ubuntu10.04-gems.erb +44 -0
  21. data/lib/chef/knife/client_bulk_delete.rb +1 -1
  22. data/lib/chef/knife/client_create.rb +1 -1
  23. data/lib/chef/knife/client_delete.rb +1 -1
  24. data/lib/chef/knife/client_edit.rb +1 -1
  25. data/lib/chef/knife/client_list.rb +1 -1
  26. data/lib/chef/knife/client_reregister.rb +1 -1
  27. data/lib/chef/knife/client_show.rb +1 -1
  28. data/lib/chef/knife/configure.rb +1 -1
  29. data/lib/chef/knife/configure_client.rb +1 -1
  30. data/lib/chef/knife/cookbook_bulk_delete.rb +1 -1
  31. data/lib/chef/knife/cookbook_create.rb +159 -0
  32. data/lib/chef/knife/cookbook_delete.rb +6 -6
  33. data/lib/chef/knife/cookbook_download.rb +1 -1
  34. data/lib/chef/knife/cookbook_list.rb +1 -1
  35. data/lib/chef/knife/cookbook_metadata.rb +1 -1
  36. data/lib/chef/knife/cookbook_metadata_from_file.rb +1 -1
  37. data/lib/chef/knife/cookbook_show.rb +1 -1
  38. data/lib/chef/knife/cookbook_site_download.rb +2 -1
  39. data/lib/chef/knife/cookbook_site_list.rb +2 -1
  40. data/lib/chef/knife/cookbook_site_search.rb +2 -1
  41. data/lib/chef/knife/cookbook_site_share.rb +108 -0
  42. data/lib/chef/knife/cookbook_site_show.rb +2 -1
  43. data/lib/chef/knife/cookbook_site_unshare.rb +52 -0
  44. data/lib/chef/knife/cookbook_site_vendor.rb +3 -2
  45. data/lib/chef/knife/cookbook_test.rb +1 -1
  46. data/lib/chef/knife/cookbook_upload.rb +22 -155
  47. data/lib/chef/knife/data_bag_create.rb +2 -1
  48. data/lib/chef/knife/data_bag_delete.rb +2 -1
  49. data/lib/chef/knife/data_bag_edit.rb +2 -1
  50. data/lib/chef/knife/data_bag_from_file.rb +2 -1
  51. data/lib/chef/knife/data_bag_list.rb +2 -1
  52. data/lib/chef/knife/data_bag_show.rb +2 -1
  53. data/lib/chef/knife/ec2_instance_data.rb +1 -1
  54. data/lib/chef/knife/ec2_server_create.rb +16 -4
  55. data/lib/chef/knife/ec2_server_delete.rb +8 -2
  56. data/lib/chef/knife/ec2_server_list.rb +8 -2
  57. data/lib/chef/knife/index_rebuild.rb +1 -1
  58. data/lib/chef/knife/node_bulk_delete.rb +1 -1
  59. data/lib/chef/knife/node_create.rb +1 -1
  60. data/lib/chef/knife/node_delete.rb +1 -1
  61. data/lib/chef/knife/node_edit.rb +1 -1
  62. data/lib/chef/knife/node_from_file.rb +1 -1
  63. data/lib/chef/knife/node_list.rb +1 -1
  64. data/lib/chef/knife/node_run_list_add.rb +1 -1
  65. data/lib/chef/knife/node_run_list_remove.rb +1 -1
  66. data/lib/chef/knife/node_show.rb +1 -1
  67. data/lib/chef/knife/rackspace_server_create.rb +1 -1
  68. data/lib/chef/knife/rackspace_server_delete.rb +1 -1
  69. data/lib/chef/knife/rackspace_server_list.rb +1 -1
  70. data/lib/chef/knife/recipe_list.rb +1 -1
  71. data/lib/chef/knife/role_bulk_delete.rb +1 -1
  72. data/lib/chef/knife/role_create.rb +1 -1
  73. data/lib/chef/knife/role_delete.rb +1 -1
  74. data/lib/chef/knife/role_edit.rb +1 -1
  75. data/lib/chef/knife/role_from_file.rb +1 -1
  76. data/lib/chef/knife/role_list.rb +1 -1
  77. data/lib/chef/knife/role_show.rb +1 -1
  78. data/lib/chef/knife/search.rb +1 -1
  79. data/lib/chef/knife/slicehost_images_list.rb +1 -1
  80. data/lib/chef/knife/slicehost_server_create.rb +1 -1
  81. data/lib/chef/knife/slicehost_server_delete.rb +1 -1
  82. data/lib/chef/knife/slicehost_server_list.rb +1 -1
  83. data/lib/chef/knife/ssh.rb +49 -9
  84. data/lib/chef/knife/status.rb +2 -2
  85. data/lib/chef/knife/terremark_server_create.rb +1 -1
  86. data/lib/chef/knife/terremark_server_delete.rb +1 -1
  87. data/lib/chef/knife/terremark_server_list.rb +1 -1
  88. data/lib/chef/mixin/command.rb +17 -204
  89. data/lib/chef/mixin/command/unix.rb +215 -0
  90. data/lib/chef/mixin/command/windows.rb +72 -0
  91. data/lib/chef/mixin/find_preferred_file.rb +3 -3
  92. data/lib/chef/mixin/language.rb +64 -23
  93. data/lib/chef/node.rb +27 -3
  94. data/lib/chef/node/attribute.rb +20 -10
  95. data/lib/chef/platform.rb +3 -9
  96. data/lib/chef/provider/cron.rb +1 -1
  97. data/lib/chef/provider/deploy.rb +4 -1
  98. data/lib/chef/provider/group.rb +1 -1
  99. data/lib/chef/provider/group/dscl.rb +2 -2
  100. data/lib/chef/provider/mount/mount.rb +6 -6
  101. data/lib/chef/provider/package/easy_install.rb +8 -7
  102. data/lib/chef/provider/package/pacman.rb +1 -1
  103. data/lib/chef/provider/package/rpm.rb +4 -4
  104. data/lib/chef/provider/package/solaris.rb +127 -0
  105. data/lib/chef/provider/package/yum.rb +51 -28
  106. data/lib/chef/provider/remote_directory.rb +5 -2
  107. data/lib/chef/provider/remote_file.rb +1 -1
  108. data/lib/chef/provider/ruby_block.rb +1 -0
  109. data/lib/chef/provider/script.rb +2 -2
  110. data/lib/chef/provider/service/debian.rb +40 -4
  111. data/lib/chef/provider/service/freebsd.rb +1 -1
  112. data/lib/chef/provider/service/upstart.rb +3 -10
  113. data/lib/chef/provider/user/dscl.rb +2 -2
  114. data/lib/chef/providers.rb +1 -0
  115. data/lib/chef/resource.rb +7 -8
  116. data/lib/chef/resource/service.rb +17 -0
  117. data/lib/chef/resource/solaris_package.rb +36 -0
  118. data/lib/chef/resource/yum_package.rb +11 -4
  119. data/lib/chef/rest/rest_request.rb +18 -1
  120. data/lib/chef/run_list/run_list_expansion.rb +4 -0
  121. data/lib/chef/run_list/run_list_item.rb +8 -1
  122. data/lib/chef/shef/ext.rb +1 -1
  123. data/lib/chef/shef/shef_session.rb +7 -3
  124. data/lib/chef/tasks/chef_repo.rake +1 -0
  125. data/lib/chef/version.rb +2 -1
  126. metadata +23 -8
  127. data/lib/chef/cache/file_cache_by_checksum.rb +0 -52
@@ -232,17 +232,7 @@ class Chef
232
232
  end
233
233
 
234
234
  def view_uri(design, view)
235
- Chef::Config[:couchdb_version] ||= @rest.run_request(:GET,
236
- URI.parse(@rest.url + "/"),
237
- {},
238
- 10,
239
- false)["version"].gsub(/-.+/,"").to_f
240
- case Chef::Config[:couchdb_version]
241
- when 0.8
242
- "#{couchdb_database}/_view/#{design}/#{view}"
243
- else
244
- "#{couchdb_database}/_design/#{design}/_view/#{view}"
245
- end
235
+ "#{couchdb_database}/_design/#{design}/_view/#{view}"
246
236
  end
247
237
 
248
238
  end
@@ -30,6 +30,7 @@ class Chef
30
30
  class UnsupportedAction < RuntimeError; end
31
31
  class MissingLibrary < RuntimeError; end
32
32
  class MissingRole < RuntimeError; end
33
+ class CannotDetermineNodeName < RuntimeError; end
33
34
  class User < RuntimeError; end
34
35
  class Group < RuntimeError; end
35
36
  class Link < RuntimeError; end
@@ -25,7 +25,7 @@ class Chef
25
25
  # the values specified by a value object, usually a Chef::Resource.
26
26
  class FileAccessControl
27
27
  UINT = (1 << 32)
28
- UID_MAX = (1 << 30)
28
+ UID_MAX = (1 << 31)
29
29
 
30
30
  attr_reader :resource
31
31
 
@@ -68,17 +68,24 @@ class Chef
68
68
  end
69
69
 
70
70
  def disconnected!
71
+ Chef::Log.error("Disconnected from the AMQP Broker (RabbitMQ)")
71
72
  @amqp_client = nil
72
73
  reset!
73
74
  end
74
75
 
75
76
  def send_action(action, data)
77
+ retries = 0
76
78
  begin
77
79
  exchange.publish({"action" => action.to_s, "payload" => data}.to_json)
78
- rescue Bunny::ServerDownError, Bunny::ConnectionError, Errno::ECONNRESET => e
79
- Chef::Log.error("Disconnected from the AMQP Broker, cannot queue data to the indexer")
80
+ rescue Bunny::ServerDownError, Bunny::ConnectionError, Errno::ECONNRESET
80
81
  disconnected!
81
- raise e
82
+ if (retries += 1) < 2
83
+ Chef::Log.info("Attempting to reconnect to the AMQP broker")
84
+ retry
85
+ else
86
+ Chef::Log.fatal("Could not re-connect to the AMQP broker, giving up")
87
+ raise
88
+ end
82
89
  end
83
90
  end
84
91
 
@@ -17,6 +17,7 @@
17
17
  # limitations under the License.
18
18
  #
19
19
 
20
+ require 'chef/version'
20
21
  require 'mixlib/cli'
21
22
  require 'chef/mixin/convert_to_class_name'
22
23
 
@@ -27,75 +28,166 @@ class Chef
27
28
  include Mixlib::CLI
28
29
  extend Chef::Mixin::ConvertToClassName
29
30
 
31
+ # The "require paths" of the core knife subcommands bundled with chef
32
+ DEFAULT_SUBCOMMAND_FILES = Dir[File.expand_path(File.join(File.dirname(__FILE__), 'knife', '*.rb'))]
33
+ DEFAULT_SUBCOMMAND_FILES.map! { |knife_file| knife_file[/#{CHEF_ROOT}#{Regexp.escape(File::SEPARATOR)}(.*)\.rb/,1] }
34
+
30
35
  attr_accessor :name_args
31
36
 
32
- # Load all the sub-commands
33
- def self.load_commands
34
- @sub_classes = Hash.new
35
- Dir[
36
- File.expand_path(File.join(File.dirname(__FILE__), 'knife', '*.rb'))
37
- ].each do |knife_file|
38
- require knife_file
39
- snake_case_file_name = File.basename(knife_file).sub(/\.rb$/, '')
40
- @sub_classes[snake_case_file_name] = convert_to_class_name(snake_case_file_name)
37
+ def self.msg(msg="")
38
+ puts msg
39
+ end
40
+
41
+ def self.inherited(subclass)
42
+ unless subclass.unnamed?
43
+ subcommands[subclass.snake_case_name] = subclass
41
44
  end
42
- @sub_classes
43
45
  end
44
46
 
45
- def self.list_commands
46
- load_commands
47
- @sub_classes.keys.sort.each do |snake_case|
48
- klass_instance = build_sub_class(snake_case)
49
- klass_instance.parse_options([])
50
- puts klass_instance.opt_parser
51
- puts
47
+ # Explicitly set the category for the current command to +new_category+
48
+ # The category is normally determined from the first word of the command
49
+ # name, but some commands make more sense using two or more words
50
+ # ===Arguments
51
+ # new_category::: A String to set the category to (see examples)
52
+ # ===Examples:
53
+ # Data bag commands would be in the 'data' category by default. To put them
54
+ # in the 'data bag' category:
55
+ # category('data bag')
56
+ def self.category(new_category)
57
+ @category = new_category
58
+ end
59
+
60
+ def self.subcommand_category
61
+ @category || snake_case_name.split('_').first unless unnamed?
62
+ end
63
+
64
+ def self.snake_case_name
65
+ convert_to_snake_case(name.split('::').last) unless unnamed?
66
+ end
67
+
68
+ # Does this class have a name? (Classes created via Class.new don't)
69
+ def self.unnamed?
70
+ name.nil? || name.empty?
71
+ end
72
+
73
+ def self.subcommands
74
+ @@subcommands ||= {}
75
+ end
76
+
77
+ def self.subcommands_by_category
78
+ unless @subcommands_by_category
79
+ @subcommands_by_category = Hash.new { |hash, key| hash[key] = [] }
80
+ subcommands.each do |snake_cased, klass|
81
+ @subcommands_by_category[klass.subcommand_category] << snake_cased
82
+ end
52
83
  end
84
+ @subcommands_by_category
53
85
  end
54
86
 
55
- def self.build_sub_class(snake_case, merge_opts=nil)
56
- klass = Chef::Knife.const_get(@sub_classes[snake_case])
57
- klass.options.merge!(merge_opts) if merge_opts
58
- klass.new
87
+ # Load all the sub-commands
88
+ def self.load_commands
89
+ DEFAULT_SUBCOMMAND_FILES.each { |subcommand| require subcommand }
90
+ subcommands
59
91
  end
60
92
 
61
- def self.find_command(args=ARGV, merge_opts={})
93
+ # Print the list of subcommands knife knows about. If +preferred_category+
94
+ # is given, only subcommands in that category are shown
95
+ def self.list_commands(preferred_category=nil)
62
96
  load_commands
97
+ category_desc = preferred_category ? preferred_category + " " : ''
98
+ msg "Available #{category_desc}subcommands: (for details, knife SUB-COMMAND --help)\n\n"
63
99
 
64
- non_dash_args = Array.new
65
- args.each do |arg|
66
- non_dash_args << arg if arg =~ /^([[:alnum:]]|_)+$/
100
+ if preferred_category && subcommands_by_category.key?(preferred_category)
101
+ commands_to_show = {preferred_category => subcommands_by_category[preferred_category]}
102
+ else
103
+ commands_to_show = subcommands_by_category
67
104
  end
68
105
 
69
- to_try = non_dash_args.length
70
- klass_instance = nil
71
- cli_bits = nil
106
+ commands_to_show.sort.each do |category, commands|
107
+ msg "** #{category.upcase} COMMANDS **"
108
+ commands.each do |command|
109
+ msg subcommands[command].banner if subcommands[command]
110
+ end
111
+ msg
112
+ end
113
+ end
72
114
 
73
- while(to_try >= 0)
74
- cli_bits = non_dash_args[0..to_try]
75
- snake_case_class_name = cli_bits.join("_")
115
+ # Run knife for the given +args+ (ARGV), adding +options+ to the list of
116
+ # CLI options that the subcommand knows how to handle.
117
+ # ===Arguments
118
+ # args::: usually ARGV
119
+ # options::: A Mixlib::CLI option parser hash. These +options+ are how
120
+ # subcommands know about global knife CLI options
121
+ def self.run(args, options={})
122
+ load_commands
123
+ subcommand_class = subcommand_class_from(args)
124
+ subcommand_class.options.merge!(options)
125
+ instance = subcommand_class.new(args)
126
+ instance.configure_chef
127
+ instance.run
128
+ end
76
129
 
77
- if @sub_classes.has_key?(snake_case_class_name)
78
- klass_instance = build_sub_class(snake_case_class_name, merge_opts)
79
- break
80
- end
130
+ def self.guess_category(args)
131
+ category_words = args.select {|arg| arg =~ /^([[:alnum:]]|_)+$/ }
132
+ matching_category = nil
133
+ while (!matching_category) && (!category_words.empty?)
134
+ candidate_category = category_words.join(' ')
135
+ matching_category = candidate_category if subcommands_by_category.key?(candidate_category)
136
+ matching_category || category_words.pop
137
+ end
138
+ matching_category
139
+ end
81
140
 
82
- to_try = to_try - 1
141
+ def self.subcommand_class_from(args)
142
+ command_words = args.select {|arg| arg =~ /^([[:alnum:]]|_)+$/ }
143
+ subcommand_class = nil
144
+
145
+ while ( !subcommand_class ) && ( !command_words.empty? )
146
+ snake_case_class_name = command_words.join("_")
147
+ unless subcommand_class = subcommands[snake_case_class_name]
148
+ command_words.pop
149
+ end
83
150
  end
151
+ subcommand_class || subcommand_not_found!(args)
152
+ end
84
153
 
85
- unless klass_instance
86
- Chef::Log.fatal("Cannot find sub command for: #{args.join(' ')}")
87
- Chef::Knife.list_commands
88
- exit 10
154
+ private
155
+
156
+ # :nodoc:
157
+ # Error out and print usage. probably becuase the arguments given by the
158
+ # user could not be resolved to a subcommand.
159
+ def self.subcommand_not_found!(args)
160
+ unless want_help?(args)
161
+ Chef::Log.fatal("Cannot find sub command for: '#{args.join(' ')}'")
89
162
  end
163
+ Chef::Knife.list_commands(guess_category(args))
164
+ exit 10
165
+ end
90
166
 
91
- extra = klass_instance.parse_options(args)
92
- if klass_instance.config[:help]
93
- puts klass_instance.opt_parser
167
+ # :nodoc:
168
+ # TODO: duplicated with chef/application/knife
169
+ # all logic should be removed from that and Chef::Knife should own it.
170
+ def self.want_help?(args)
171
+ (args.any? { |arg| arg =~ /^(:?(:?\-\-)?help|\-h)$/})
172
+ end
173
+
174
+ public
175
+
176
+ # Create a new instance of the current class configured for the given
177
+ # arguments and options
178
+ def initialize(argv=[])
179
+ super() # having to call super in initialize is the most annoying anti-pattern :(
180
+
181
+ command_name_words = self.class.snake_case_name.split('_')
182
+
183
+ # Mixlib::CLI ignores the embedded name_args
184
+ @name_args = parse_options(argv)
185
+ @name_args.reject! { |name_arg| command_name_words.delete(name_arg) }
186
+
187
+ if config[:help]
188
+ msg opt_parser
94
189
  exit 1
95
190
  end
96
- klass_instance.name_args = extra.inject([]) { |c, i| cli_bits.include?(i) ? cli_bits.delete(i) : c << i; c }
97
- klass_instance.configure_chef
98
- klass_instance
99
191
  end
100
192
 
101
193
  def parse_options(args)
@@ -141,7 +233,7 @@ class Chef
141
233
  Chef::Config.from_file(config[:config_file])
142
234
  else
143
235
  # ...but do log a message if no config was found.
144
- Chef::Log.info("No knife configuration file found")
236
+ self.msg("No knife configuration file found")
145
237
  end
146
238
 
147
239
  Chef::Config[:log_level] = config[:log_level] if config[:log_level]
@@ -201,7 +293,7 @@ class Chef
201
293
  data = data.send(attr.to_sym)
202
294
  end
203
295
  end
204
- { config[:attribute] => data.kind_of?(Chef::Node::Attribute) ? data.to_hash: data }
296
+ { config[:attribute] => data.kind_of?(Chef::Node::Attribute) ? data.to_hash : data }
205
297
  elsif config[:run_list]
206
298
  data = data.run_list.run_list
207
299
  { "run_list" => data }
@@ -237,19 +329,19 @@ class Chef
237
329
  def confirm(question, append_instructions=true)
238
330
  return true if config[:yes]
239
331
 
240
- print question
241
- print "? (Y/N) " if append_instructions
332
+ stdout.print question
333
+ stdout.print "? (Y/N) " if append_instructions
242
334
  answer = stdin.readline
243
335
  answer.chomp!
244
336
  case answer
245
337
  when "Y", "y"
246
338
  true
247
339
  when "N", "n"
248
- Chef::Log.info("You said no, so I'm done here.")
340
+ self.msg("You said no, so I'm done here.")
249
341
  exit 3
250
342
  else
251
- Chef::Log.error("I have no idea what to do with #{answer}")
252
- Chef::Log.error("Just say Y or N, please.")
343
+ self.msg("I have no idea what to do with #{answer}")
344
+ self.msg("Just say Y or N, please.")
253
345
  confirm(question)
254
346
  end
255
347
  end
@@ -304,7 +396,7 @@ class Chef
304
396
 
305
397
  output.save
306
398
 
307
- Chef::Log.info("Saved #{output}")
399
+ self.msg("Saved #{output}")
308
400
 
309
401
  output(format_for_display(object)) if config[:print_after]
310
402
  end
@@ -320,7 +412,7 @@ class Chef
320
412
 
321
413
  pretty_name ||= output
322
414
 
323
- Chef::Log.info("Created (or updated) #{pretty_name}")
415
+ self.msg("Created (or updated) #{pretty_name}")
324
416
 
325
417
  output(output) if config[:print_after]
326
418
  end
@@ -338,7 +430,7 @@ class Chef
338
430
  output(format_for_display(object)) if config[:print_after]
339
431
 
340
432
  obj_name = delete_name ? "#{delete_name}[#{name}]" : object
341
- Chef::Log.warn("Deleted #{obj_name}!")
433
+ self.msg("Deleted #{obj_name}!")
342
434
  end
343
435
 
344
436
  def bulk_delete(klass, fancy_name, delete_name=nil, list=nil, regex=nil, &block)
@@ -365,10 +457,14 @@ class Chef
365
457
  object.destroy
366
458
  end
367
459
  output(format_for_display(object)) if config[:print_after]
368
- Chef::Log.warn("Deleted #{fancy_name} #{name}")
460
+ self.msg("Deleted #{fancy_name} #{name}")
369
461
  end
370
462
  end
371
-
463
+
464
+ def msg(message)
465
+ stdout.puts message
466
+ end
467
+
372
468
  def stdout
373
469
  STDOUT
374
470
  end
@@ -19,12 +19,13 @@
19
19
  require 'chef/knife'
20
20
  require 'json'
21
21
  require 'tempfile'
22
+ require 'erubis'
22
23
 
23
24
  class Chef
24
25
  class Knife
25
26
  class Bootstrap < Knife
26
27
 
27
- banner "Sub-Command: knife bootstrap FQDN [RUN LIST...] (options)"
28
+ banner "knife bootstrap FQDN [RUN LIST...] (options)"
28
29
 
29
30
  option :ssh_user,
30
31
  :short => "-x USERNAME",
@@ -37,6 +38,11 @@ class Chef
37
38
  :long => "--ssh-password PASSWORD",
38
39
  :description => "The ssh password"
39
40
 
41
+ option :identity_file,
42
+ :short => "-i IDENTITY_FILE",
43
+ :long => "--identity-file IDENTITY_FILE",
44
+ :description => "The SSH identity file used for authentication"
45
+
40
46
  option :chef_node_name,
41
47
  :short => "-N NAME",
42
48
  :long => "--node-name NAME",
@@ -46,67 +52,97 @@ class Chef
46
52
  :long => "--prerelease",
47
53
  :description => "Install the pre-release chef gems"
48
54
 
55
+ option :distro,
56
+ :short => "-d DISTRO",
57
+ :long => "--distro DISTRO",
58
+ :description => "Bootstrap a distro using a template",
59
+ :default => "ubuntu10.04-gems"
60
+
61
+ option :identity_file,
62
+ :short => "-i IDENTITY_FILE",
63
+ :long => "--identity-file IDENTITY_FILE",
64
+ :description => "The SSH identity file used for authentication"
65
+
66
+ option :use_sudo,
67
+ :short => "-s",
68
+ :long => "--sudo",
69
+ :description => "Execute the bootstrap via sudo",
70
+ :boolean => true
71
+
72
+ option :template_file,
73
+ :long => "--template-file TEMPLATE",
74
+ :description => "Full path to location of template to use",
75
+ :default => false
76
+
77
+ option :run_list,
78
+ :short => "-r RUN_LIST",
79
+ :long => "--run-list RUN_LIST",
80
+ :description => "Comma separated list of roles/recipes to apply",
81
+ :proc => lambda { |o| o.split(",") },
82
+ :default => nil
49
83
 
50
84
  def h
51
85
  @highline ||= HighLine.new
52
86
  end
53
87
 
88
+ def load_template(template=nil)
89
+ # Are we bootstrapping using an already shipped template?
90
+ if config[:template_file]
91
+ bootstrap_files = config[:template_file]
92
+ else
93
+ bootstrap_files = []
94
+ bootstrap_files << File.join(File.dirname(__FILE__), 'bootstrap', "#{config[:distro]}.erb")
95
+ bootstrap_files << File.join(Dir.pwd, ".chef", "bootstrap", "#{config[:distro]}.erb")
96
+ bootstrap_files << File.join(ENV['HOME'], '.chef', 'bootstrap', "#{config[:distro]}.erb")
97
+ end
98
+
99
+ template = Array(bootstrap_files).find do |bootstrap_template|
100
+ Chef::Log.debug("Looking for bootstrap template in #{File.dirname(bootstrap_template)}")
101
+ File.exists?(bootstrap_template)
102
+ end
103
+
104
+ unless template
105
+ Chef::Log.info("Can not find bootstrap definition for #{config[:distro]}")
106
+ raise Errno::ENOENT
107
+ end
108
+
109
+ Chef::Log.debug("Found bootstrap template in #{File.dirname(template)}")
110
+
111
+ IO.read(template).chomp
112
+ end
113
+
114
+ def render_template(template=nil)
115
+ context = {}
116
+ context[:run_list] = config[:run_list]
117
+ context[:config] = config
118
+ command = Erubis::Eruby.new(template).evaluate(context)
119
+ end
120
+
54
121
  def run
55
122
  require 'highline'
56
123
 
57
- server_name = @name_args[0]
124
+ if @name_args.first == nil
125
+ Chef::Log.error("Must pass a node name/ip to bootstrap")
126
+ exit 1
127
+ end
58
128
 
59
- puts "Bootstrapping Chef on #{h.color(server_name, :bold)}"
129
+ config[:server_name] = @name_args.first
60
130
 
61
131
  $stdout.sync = true
62
132
 
63
- command = <<EOH
64
- bash -c '
65
- if [ ! -f /usr/bin/chef-client ]; then
66
- apt-get update
67
- apt-get install -y ruby ruby1.8-dev build-essential wget libruby-extras libruby1.8-extras
68
- cd /tmp
69
- wget http://rubyforge.org/frs/download.php/69365/rubygems-1.3.6.tgz
70
- tar xvf rubygems-1.3.6.tgz
71
- cd rubygems-1.3.6
72
- ruby setup.rb
73
- cp /usr/bin/gem1.8 /usr/bin/gem
74
- gem install ohai chef --no-rdoc --no-ri --verbose #{"--prerelease" if config[:prerelease]}
75
- fi
76
-
77
- mkdir -p /etc/chef
78
-
79
- (
80
- cat <<'EOP'
81
- #{IO.read(Chef::Config[:validation_key])}
82
- EOP
83
- ) > /tmp/validation.pem
84
- awk NF /tmp/validation.pem > /etc/chef/validation.pem
85
- rm /tmp/validation.pem
86
-
87
- (
88
- cat <<'EOP'
89
- log_level :info
90
- log_location STDOUT
91
- chef_server_url "#{Chef::Config[:chef_server_url]}"
92
- validation_client_name "#{Chef::Config[:validation_client_name]}"
93
- #{config[:chef_node_name] == nil ? "# Using default node name" : "node_name \"#{config[:chef_node_name]}\""}
94
- EOP
95
- ) > /etc/chef/client.rb
96
-
97
- (
98
- cat <<'EOP'
99
- #{{ "run_list" => @name_args[1..-1] }.to_json}
100
- EOP
101
- ) > /etc/chef/first-boot.json
102
-
103
- /usr/bin/chef-client -j /etc/chef/first-boot.json'
104
- EOH
133
+ command = render_template(load_template(config[:bootstrap_template]))
134
+
135
+ if config[:use_sudo]
136
+ command = "sudo #{command}"
137
+ end
138
+
139
+ Chef::Log.info("Bootstrapping Chef on #{h.color(config[:server_name], :bold)}")
105
140
 
106
141
  ssh = Chef::Knife::Ssh.new
107
- ssh.name_args = [ server_name, "sudo #{command}" ]
142
+ ssh.name_args = [ config[:server_name], command ]
108
143
  ssh.config[:ssh_user] = config[:ssh_user]
109
144
  ssh.config[:password] = config[:ssh_password]
145
+ ssh.config[:identity_file] = config[:identity_file]
110
146
  ssh.config[:manual] = true
111
147
 
112
148
  begin
@@ -115,15 +151,15 @@ EOH
115
151
  unless config[:ssh_password]
116
152
  puts "Failed to authenticate #{config[:ssh_user]} - trying password auth"
117
153
  ssh = Chef::Knife::Ssh.new
118
- ssh.name_args = [ server_name, "sudo #{command}" ]
154
+ ssh.name_args = [ config[:server_name], command ]
119
155
  ssh.config[:ssh_user] = config[:ssh_user]
120
156
  ssh.config[:manual] = true
121
157
  ssh.config[:password] = ssh.get_password
122
158
  ssh.run
123
159
  end
124
160
  end
125
-
126
161
  end
162
+
127
163
  end
128
164
  end
129
165
  end