chef 0.8.10 → 0.8.14

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of chef might be problematic. Click here for more details.

Files changed (105) hide show
  1. data/bin/chef-client +2 -2
  2. data/bin/chef-solo +1 -2
  3. data/bin/knife +1 -2
  4. data/bin/shef +2 -5
  5. data/lib/chef.rb +11 -7
  6. data/lib/chef/application.rb +25 -20
  7. data/lib/chef/application/client.rb +12 -7
  8. data/lib/chef/application/knife.rb +7 -1
  9. data/lib/chef/application/solo.rb +1 -1
  10. data/lib/chef/applications.rb +4 -0
  11. data/lib/chef/cache/checksum.rb +7 -6
  12. data/lib/chef/certificate.rb +2 -2
  13. data/lib/chef/client.rb +28 -4
  14. data/lib/chef/config.rb +4 -3
  15. data/lib/chef/cookbook_loader.rb +6 -1
  16. data/lib/chef/daemon.rb +2 -0
  17. data/lib/chef/data_bag_item.rb +2 -1
  18. data/lib/chef/exceptions.rb +5 -0
  19. data/lib/chef/file_cache.rb +18 -18
  20. data/lib/chef/index_queue/indexable.rb +5 -3
  21. data/lib/chef/knife.rb +45 -13
  22. data/lib/chef/knife/client_list.rb +1 -1
  23. data/lib/chef/knife/client_show.rb +1 -1
  24. data/lib/chef/knife/configure.rb +47 -36
  25. data/lib/chef/knife/cookbook_list.rb +1 -1
  26. data/lib/chef/knife/cookbook_metadata.rb +22 -27
  27. data/lib/chef/knife/cookbook_metadata_from_file.rb +40 -0
  28. data/lib/chef/knife/cookbook_show.rb +2 -2
  29. data/lib/chef/knife/cookbook_site_download.rb +57 -0
  30. data/lib/chef/knife/cookbook_site_list.rb +55 -0
  31. data/lib/chef/knife/cookbook_site_search.rb +50 -0
  32. data/lib/chef/knife/cookbook_site_show.rb +56 -0
  33. data/lib/chef/knife/cookbook_site_vendor.rb +114 -0
  34. data/lib/chef/knife/cookbook_test.rb +103 -0
  35. data/lib/chef/knife/cookbook_upload.rb +29 -38
  36. data/lib/chef/knife/data_bag_edit.rb +1 -1
  37. data/lib/chef/knife/data_bag_list.rb +2 -2
  38. data/lib/chef/knife/data_bag_show.rb +1 -1
  39. data/lib/chef/knife/ec2_instance_data.rb +1 -1
  40. data/lib/chef/knife/index_rebuild.rb +2 -2
  41. data/lib/chef/knife/node_from_file.rb +1 -1
  42. data/lib/chef/knife/node_list.rb +2 -2
  43. data/lib/chef/knife/node_run_list_add.rb +1 -1
  44. data/lib/chef/knife/node_run_list_remove.rb +1 -1
  45. data/lib/chef/knife/node_show.rb +1 -1
  46. data/lib/chef/knife/rackspace_server_create.rb +156 -0
  47. data/lib/chef/knife/rackspace_server_delete.rb +57 -0
  48. data/lib/chef/knife/rackspace_server_list.rb +59 -0
  49. data/lib/chef/knife/role_from_file.rb +1 -1
  50. data/lib/chef/knife/role_list.rb +1 -1
  51. data/lib/chef/knife/role_show.rb +1 -1
  52. data/lib/chef/knife/search.rb +1 -1
  53. data/lib/chef/knife/ssh.rb +21 -4
  54. data/lib/chef/knife/terremark_server_create.rb +152 -0
  55. data/lib/chef/knife/terremark_server_delete.rb +87 -0
  56. data/lib/chef/knife/terremark_server_list.rb +77 -0
  57. data/lib/chef/mixin/command.rb +11 -9
  58. data/lib/chef/mixin/params_validate.rb +1 -1
  59. data/lib/chef/mixin/recipe_definition_dsl_core.rb +4 -1
  60. data/lib/chef/mixin/template.rb +6 -5
  61. data/lib/chef/mixin/xml_escape.rb +3 -3
  62. data/lib/chef/mixins.rb +16 -0
  63. data/lib/chef/node.rb +53 -52
  64. data/lib/chef/openid_registration.rb +0 -1
  65. data/lib/chef/platform.rb +171 -137
  66. data/lib/chef/provider.rb +2 -2
  67. data/lib/chef/provider/cron.rb +22 -22
  68. data/lib/chef/provider/deploy/revision.rb +5 -1
  69. data/lib/chef/provider/erl_call.rb +2 -2
  70. data/lib/chef/provider/file.rb +44 -23
  71. data/lib/chef/provider/group/dscl.rb +6 -4
  72. data/lib/chef/provider/mdadm.rb +0 -4
  73. data/lib/chef/provider/mount/mount.rb +20 -8
  74. data/lib/chef/provider/package.rb +1 -1
  75. data/lib/chef/provider/package/freebsd.rb +22 -18
  76. data/lib/chef/provider/package/rubygems.rb +7 -10
  77. data/lib/chef/provider/remote_directory.rb +15 -0
  78. data/lib/chef/provider/remote_file.rb +73 -50
  79. data/lib/chef/provider/script.rb +10 -8
  80. data/lib/chef/provider/service/windows.rb +129 -0
  81. data/lib/chef/provider/subversion.rb +1 -1
  82. data/lib/chef/provider/template.rb +51 -50
  83. data/lib/chef/providers.rb +80 -0
  84. data/lib/chef/recipe.rb +2 -4
  85. data/lib/chef/resource.rb +21 -7
  86. data/lib/chef/resource/cron.rb +14 -5
  87. data/lib/chef/resource/deploy.rb +52 -45
  88. data/lib/chef/resource/execute.rb +1 -1
  89. data/lib/chef/resource/file.rb +16 -8
  90. data/lib/chef/resource/mount.rb +1 -1
  91. data/lib/chef/resource/remote_directory.rb +19 -10
  92. data/lib/chef/resource/scm.rb +23 -16
  93. data/lib/chef/resource/service.rb +10 -1
  94. data/lib/chef/resources.rb +60 -0
  95. data/lib/chef/rest.rb +234 -189
  96. data/lib/chef/rest/auth_credentials.rb +78 -0
  97. data/lib/chef/{application/server.rb → rest/cookie_jar.rb} +18 -6
  98. data/lib/chef/rest/rest_request.rb +151 -0
  99. data/lib/chef/role.rb +38 -46
  100. data/lib/chef/streaming_cookbook_uploader.rb +8 -2
  101. data/lib/chef/tasks/chef_repo.rake +14 -4
  102. data/lib/chef/util/file_edit.rb +0 -1
  103. data/lib/chef/webui_user.rb +0 -1
  104. metadata +46 -9
  105. data/distro/suse/etc/init.d/chef-client +0 -121
@@ -27,9 +27,8 @@ class Chef
27
27
 
28
28
  def gem_list_parse(line)
29
29
  installed_versions = Array.new
30
- if line.match("^#{@new_resource.package_name} \\((.+?)\\)$")
31
- installed_versions = $1.split(/, /)
32
- installed_versions
30
+ if md = line.match(/^#{@new_resource.package_name} \((.+?)(?: [^\)\.]+)?\)$/)
31
+ md.captures.first.split(/, /)
33
32
  else
34
33
  nil
35
34
  end
@@ -47,9 +46,8 @@ class Chef
47
46
 
48
47
  # First, we need to look up whether we have the local gem installed or not
49
48
  status = popen4("#{gem_binary_path} list --local #{@new_resource.package_name}") do |pid, stdin, stdout, stderr|
50
- stdout.each do |line|
51
- installed_versions = gem_list_parse(line)
52
- next unless installed_versions
49
+ stdout.each_line do |line|
50
+ next unless installed_versions = gem_list_parse(line)
53
51
  # If the version we are asking for is installed, make that our current
54
52
  # version. Otherwise, go ahead and use the highest one, which
55
53
  # happens to come first in the array.
@@ -75,9 +73,8 @@ class Chef
75
73
  return @candidate_version if @candidate_version
76
74
 
77
75
  status = popen4("#{gem_binary_path} list --remote #{@new_resource.package_name}#{' --source=' + @new_resource.source if @new_resource.source}") do |pid, stdin, stdout, stderr|
78
- stdout.each do |line|
79
- installed_versions = gem_list_parse(line)
80
- next unless installed_versions
76
+ stdout.each_line do |line|
77
+ next unless installed_versions = gem_list_parse(line)
81
78
  Chef::Log.debug("candidate_version: remote rubygem(s) available: #{installed_versions.inspect}")
82
79
 
83
80
  unless installed_versions.empty?
@@ -97,7 +94,7 @@ class Chef
97
94
  def install_package(name, version)
98
95
  src = nil
99
96
  if @new_resource.source
100
- src = " --source=#{@new_resource.source} --source=http://gems.rubyforge.org"
97
+ src = " --source=#{@new_resource.source} --source=http://rubygems.org"
101
98
  end
102
99
  run_command_with_systems_locale(
103
100
  :command => "#{gem_binary_path} install #{name} -q --no-rdoc --no-ri -v \"#{version}\"#{src}#{opts}"
@@ -26,6 +26,7 @@ require 'chef/platform'
26
26
  require 'uri'
27
27
  require 'tempfile'
28
28
  require 'net/https'
29
+ require 'set'
29
30
 
30
31
  class Chef
31
32
  class Provider
@@ -47,8 +48,22 @@ class Chef
47
48
  Chef::Log.debug("Doing a remote recursive directory transfer for #{@new_resource}")
48
49
  end
49
50
 
51
+ existing_files = Set.new Dir[::File.join(@new_resource.path, '**', '*')]
52
+
50
53
  files_to_transfer.each do |remote_file_source|
51
54
  fetch_remote_file(remote_file_source)
55
+ existing_files.delete(::File.join(@new_resource.path, remote_file_source))
56
+ end
57
+ if @new_resource.purge
58
+ existing_files.sort { |a,b| b <=> a }.each do |f|
59
+ if ::File.directory?(f)
60
+ Chef::Log.debug("Removing directory #{f}")
61
+ Dir::rmdir(f)
62
+ else
63
+ Chef::Log.debug("Deleting file #{f}")
64
+ ::File.delete(f)
65
+ end
66
+ end
52
67
  end
53
68
  end
54
69
 
@@ -45,42 +45,20 @@ class Chef
45
45
  def do_remote_file(source, path)
46
46
  retval = true
47
47
 
48
- if(@new_resource.checksum && @current_resource.checksum && @current_resource.checksum =~ /^#{@new_resource.checksum}/)
49
- Chef::Log.debug("File #{@new_resource} checksum matches, not updating")
48
+ if current_resource_matches_target_checksum?
49
+ Chef::Log.debug("File #{@new_resource} checksum matches target checksum (#{@new_resource.checksum}), not updating")
50
50
  else
51
51
  begin
52
- # The remote filehandle
53
- raw_file = get_from_uri(source) ||
54
- get_from_server(source, @current_resource.checksum) ||
55
- get_from_local_cookbook(source)
56
-
57
- # If the file exists
58
- Chef::Log.debug "#{@new_resource}: Checking for file existence of #{@new_resource.path}"
59
- if ::File.exists?(@new_resource.path)
60
- # And it matches the checksum of the raw file
61
- @new_resource.checksum(self.checksum(raw_file.path))
62
- Chef::Log.debug "#{@new_resource}: File exists at #{@new_resource.path}"
63
- Chef::Log.debug "#{@new_resource}: Target checksum: #{@current_resource.checksum}"
64
- Chef::Log.debug "#{@new_resource}: Source checksum: #{@new_resource.checksum}"
65
- if @new_resource.checksum != @current_resource.checksum
66
- # Updating target file, let's perform a backup!
67
- Chef::Log.debug "#{@new_resource}: checksum changed from #{@current_resource.checksum} to #{@new_resource.checksum}"
68
- Chef::Log.info "#{@new_resource}: Updating #{@new_resource.path}"
69
- backup @new_resource.path
52
+ source_file(source, @current_resource.checksum) do |raw_file|
53
+ if matches_current_checksum?(raw_file)
54
+ Chef::Log.debug "#{@new_resource}: Target and Source checksums are the same, taking no action"
55
+ else
56
+ backup_new_resource
57
+ Chef::Log.debug "copying remote file from origin #{raw_file.path} to destination #{@new_resource.path}"
70
58
  FileUtils.cp raw_file.path, @new_resource.path
71
59
  @new_resource.updated = true
72
- else
73
- Chef::Log.debug "#{@new_resource}: Target and Source checksums are the same, taking no action"
74
60
  end
75
- else
76
- # We're creating a new file
77
- Chef::Log.info "#{@new_resource}: Creating #{@new_resource.path}"
78
- FileUtils.cp raw_file.path, @new_resource.path
79
- @new_resource.updated = true
80
61
  end
81
-
82
- # We're done with the file, so make sure to close it if it was open.
83
- raw_file.close unless raw_file.closed?
84
62
  rescue Net::HTTPRetriableError => e
85
63
  if e.response.kind_of?(Net::HTTPNotModified)
86
64
  Chef::Log.debug("File #{path} is unchanged")
@@ -89,38 +67,78 @@ class Chef
89
67
  raise e
90
68
  end
91
69
  end
70
+
71
+ Chef::Log.debug "#{@new_resource} completed"
72
+ retval
92
73
  end
93
-
74
+ enforce_ownership_and_permissions
75
+
76
+ retval
77
+ end
78
+
79
+ def enforce_ownership_and_permissions
94
80
  set_owner if @new_resource.owner
95
81
  set_group if @new_resource.group
96
82
  set_mode if @new_resource.mode
97
83
 
98
- retval
99
84
  end
100
85
 
101
- def get_from_uri(source)
102
- begin
103
- uri = URI.parse(source)
104
- if uri.absolute
105
- r = Chef::REST.new(source, nil, nil)
106
- Chef::Log.debug("Downloading from absolute URI: #{source}")
107
- r.get_rest(source, true).open
108
- end
109
- rescue URI::InvalidURIError
110
- nil
86
+ def current_resource_matches_target_checksum?
87
+ @new_resource.checksum && @current_resource.checksum && @current_resource.checksum =~ /^#{@new_resource.checksum}/
88
+ end
89
+
90
+ def matches_current_checksum?(candidate_file)
91
+ Chef::Log.debug "#{@new_resource}: Checking for file existence of #{@new_resource.path}"
92
+ if ::File.exists?(@new_resource.path)
93
+ Chef::Log.debug "#{@new_resource}: File exists at #{@new_resource.path}"
94
+ @new_resource.checksum(checksum(candidate_file.path))
95
+ Chef::Log.debug "#{@new_resource}: Target checksum: #{@current_resource.checksum}"
96
+ Chef::Log.debug "#{@new_resource}: Source checksum: #{@new_resource.checksum}"
97
+ @new_resource.checksum == @current_resource.checksum
98
+ else
99
+ Chef::Log.info "#{@new_resource}: Creating #{@new_resource.path}"
100
+ false
101
+ end
102
+ end
103
+
104
+ def backup_new_resource
105
+ if ::File.exists?(@new_resource.path)
106
+ Chef::Log.debug "#{@new_resource}: checksum changed from #{@current_resource.checksum} to #{@new_resource.checksum}"
107
+ Chef::Log.info "#{@new_resource}: Updating #{@new_resource.path}"
108
+ backup @new_resource.path
111
109
  end
112
110
  end
113
111
 
114
- def get_from_server(source, current_checksum)
115
- unless Chef::Config[:solo]
116
- r = Chef::REST.new(Chef::Config[:remotefile_url])
117
- url = generate_url(source, "files", :checksum => current_checksum)
118
- Chef::Log.debug("Downloading from server: #{url}")
119
- r.get_rest(url, true).open
112
+ def source_file(source, current_checksum, &block)
113
+ if absolute_uri?(source)
114
+ fetch_from_uri(source, &block)
115
+ elsif !Chef::Config[:solo]
116
+ fetch_from_chef_server(source, current_checksum, &block)
117
+ else
118
+ fetch_from_local_cookbook(source, &block)
120
119
  end
121
120
  end
122
121
 
123
- def get_from_local_cookbook(source)
122
+ private
123
+
124
+ def absolute_uri?(source)
125
+ URI.parse(source).absolute?
126
+ rescue URI::InvalidURIError
127
+ false
128
+ end
129
+
130
+ def fetch_from_uri(source)
131
+ Chef::Log.debug("Downloading from absolute URI: #{source}")
132
+ Chef::REST.new(source, nil, nil).fetch(source) { |tmp_file| yield tmp_file }
133
+ end
134
+
135
+ def fetch_from_chef_server(source, current_checksum)
136
+ url = generate_url(source, "files", :checksum => current_checksum)
137
+ Chef::Log.debug("Downloading #{@new_resource} from server: #{url}")
138
+ Chef::REST.new(Chef::Config[:remotefile_url]).fetch(url) { |tmp_file| yield tmp_file }
139
+ end
140
+
141
+ def fetch_from_local_cookbook(source)
124
142
  if Chef::Config[:solo]
125
143
  cookbook_name = @new_resource.cookbook || @new_resource.cookbook_name
126
144
  filename = find_preferred_file(
@@ -132,7 +150,12 @@ class Chef
132
150
  @node[:platform_version]
133
151
  )
134
152
  Chef::Log.debug("Using local file for remote_file:#{filename}")
135
- ::File.open(filename)
153
+ begin
154
+ file = ::File.open(filename)
155
+ yield file
156
+ ensure
157
+ file.close
158
+ end
136
159
  end
137
160
  end
138
161
 
@@ -6,9 +6,9 @@
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
8
8
  # You may obtain a copy of the License at
9
- #
9
+ #
10
10
  # http://www.apache.org/licenses/LICENSE-2.0
11
- #
11
+ #
12
12
  # Unless required by applicable law or agreed to in writing, software
13
13
  # distributed under the License is distributed on an "AS IS" BASIS,
14
14
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -22,21 +22,23 @@ require 'chef/provider/execute'
22
22
  class Chef
23
23
  class Provider
24
24
  class Script < Chef::Provider::Execute
25
-
26
- def action_run
25
+
26
+ def action_run
27
27
  tf = Tempfile.new("chef-script")
28
28
  tf.puts(@new_resource.code)
29
29
  tf.close
30
-
30
+
31
31
  fr = Chef::Resource::File.new(tf.path, nil, @node)
32
32
  fr.owner(@new_resource.user)
33
33
  fr.group(@new_resource.group)
34
34
  fr.run_action(:create)
35
-
36
- @new_resource.command("#{@new_resource.interpreter} #{tf.path}")
35
+
36
+ @new_resource.command("#{@new_resource.interpreter} #{tf.path}")
37
37
  super
38
+ ensure
39
+ tf && tf.close!
38
40
  end
39
-
41
+
40
42
  end
41
43
  end
42
44
  end
@@ -0,0 +1,129 @@
1
+ #
2
+ # Author:: Nuo Yan <nuo@opscode.com>
3
+ # Copyright:: Copyright (c) 2010 Opscode, Inc
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'chef/provider/service/init'
20
+
21
+ class Chef::Provider::Service::Windows < Chef::Provider::Service::Init
22
+
23
+ def initialize(node, new_resource, collection=nil, definitions=nil, cookbook_loader=nil)
24
+ super(node, new_resource, collection, definitions, cookbook_loader)
25
+ @init_command = "sc"
26
+ end
27
+
28
+ def load_current_resource
29
+ @current_resource = Chef::Resource::Service.new(@new_resource.name)
30
+ @current_resource.service_name(@new_resource.service_name)
31
+ status = IO.popen("#{@init_command} query #{@new_resource.service_name}").entries
32
+ raise Chef::Exceptions::Exec, "Service #{@new_resource.service_name} does not exist.\n#{status.join}\n" if status[0].include?("FAILED 1060")
33
+
34
+ begin
35
+ started = status[3].include?("4")
36
+ @current_resource.running started
37
+
38
+ start_type = IO.popen("#{@init_command} qc #{@new_resource.service_name}").entries[4]
39
+ @current_resource.enabled(start_type.include?('2') || start_type.include?('3') ? true : false)
40
+
41
+ Chef::Log.debug "#{@new_resource}: running: #{@current_resource.running}"
42
+ rescue StandardError
43
+ raise Chef::Exceptions::Exec
44
+ rescue Chef::Exceptions::Exec
45
+ Chef::Log.debug "Failed to determine the current status of the service, assuming it is not running"
46
+ @current_resource.running false
47
+ nil
48
+ end
49
+ @current_resource
50
+ end
51
+
52
+ def start_service
53
+ begin
54
+ result = if @new_resource.start_command
55
+ Chef::Log.debug "starting service using the given start_command"
56
+ IO.popen(@new_resource.start_command).readlines
57
+ else
58
+ IO.popen("#{@init_command} start #{@new_resource.service_name}").readlines
59
+ end
60
+ Chef::Log.debug result.join
61
+ result[3].include?('4') || result.include?('2') ? true : false
62
+ rescue
63
+ Chef::Log.debug "Failed to start service #{@new_resource.service_name}"
64
+ false
65
+ end
66
+ end
67
+
68
+ def stop_service
69
+ begin
70
+ Chef::Log.debug "stopping service using the given stop_command"
71
+ result = if @new_resource.stop_command
72
+ IO.popen(@new_resource.stop_command).readlines
73
+ else
74
+ IO.popen("#{@init_command} stop #{@new_resource.service_name}").readlines
75
+ end
76
+ Chef::Log.debug result.join
77
+ result[3].include?('1')
78
+ rescue
79
+ Chef::Log.debug "Failed to stop service #{@new_resource.service_name}"
80
+ false
81
+ end
82
+ end
83
+
84
+ def restart_service
85
+ begin
86
+ if @new_resource.restart_command
87
+ Chef::Log.debug "restarting service using the given restart_command"
88
+ result = IO.popen(@new_resource.restart_command).readlines
89
+ Chef::Log.debug result.join
90
+ else
91
+ Chef::Log.debug IO.popen("#{@init_command} stop #{@new_resource.service_name}").readlines.join
92
+ sleep 1
93
+ result = IO.popen("#{@init_command} start #{@new_resource.service_name}").readlines
94
+ Chef::Log.debug result.join
95
+ end
96
+ result[3].include?('4') || result.include?('2')
97
+ rescue
98
+ Chef::Log.debug "Failed to restart service #{@new_resource.service_name}"
99
+ false
100
+ end
101
+ end
102
+
103
+ def enable_service()
104
+ begin
105
+ Chef::Log.debug result = IO.popen("#{@init_command} config #{@new_resource.service_name} start= #{determine_startup_type}").readlines.join
106
+ result.include?('SUCCESS')
107
+ rescue
108
+ Chef::Log.debug "Failed to enable service #{@new_resource.service_name}"
109
+ false
110
+ end
111
+ end
112
+
113
+ def disable_service()
114
+ begin
115
+ Chef::Log.debug result = IO.popen("#{@init_command} config #{@new_resource.service_name} start= disabled").readlines.join
116
+ result.include?('SUCCESS')
117
+ rescue
118
+ Chef::Log.debug "Failed to disable service #{@new_resource.service_name}"
119
+ false
120
+ end
121
+ end
122
+
123
+ private
124
+
125
+ def determine_startup_type
126
+ {:automatic => 'auto', :mannual => 'demand'}[@new_resource.startup_type]
127
+ end
128
+
129
+ end
@@ -86,7 +86,7 @@ class Chef
86
86
  if @new_resource.revision =~ /^\d+$/
87
87
  @new_resource.revision
88
88
  else
89
- command = scm(:info, @new_resource.repository, authentication, "-r#{@new_resource.revision}")
89
+ command = scm(:info, @new_resource.repository, @new_resource.svn_info_args, authentication, "-r#{@new_resource.revision}")
90
90
  status, svn_info, error_message = output_of_command(command, run_options)
91
91
  handle_command_failures(status, "STDOUT: #{svn_info}\nSTDERR: #{error_message}")
92
92
  extract_revision_info(svn_info)
@@ -6,9 +6,9 @@
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
8
8
  # You may obtain a copy of the License at
9
- #
9
+ #
10
10
  # http://www.apache.org/licenses/LICENSE-2.0
11
- #
11
+ #
12
12
  # Unless required by applicable law or agreed to in writing, software
13
13
  # distributed under the License is distributed on an "AS IS" BASIS,
14
14
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -27,63 +27,64 @@ require 'tempfile'
27
27
 
28
28
  class Chef
29
29
  class Provider
30
-
30
+
31
31
  class Template < Chef::Provider::File
32
-
32
+
33
33
  include Chef::Mixin::Checksum
34
34
  include Chef::Mixin::Template
35
35
  include Chef::Mixin::FindPreferredFile
36
-
36
+
37
37
  def action_create
38
38
  raw_template_file = nil
39
-
39
+
40
40
  Chef::Log.debug("looking for template #{@new_resource.source} in cookbook #{cookbook_name.inspect}")
41
-
41
+
42
42
  cache_file_name = "cookbooks/#{cookbook_name}/templates/default/#{@new_resource.source}"
43
43
  template_cache_name = "#{cookbook_name}_#{@new_resource.source}"
44
-
44
+
45
45
  if @new_resource.local
46
46
  cache_file_name = @new_resource.source
47
47
  elsif Chef::Config[:solo]
48
48
  cache_file_name = solo_cache_file_name
49
49
  else
50
50
  raw_template_file = fetch_template_via_rest(cache_file_name, template_cache_name)
51
- end
52
-
51
+ end
52
+
53
53
  if template_updated?
54
54
  Chef::Log.debug("Updating template for #{@new_resource} in the cache")
55
55
  Chef::FileCache.move_to(raw_template_file.path, cache_file_name)
56
56
  end
57
57
 
58
- template_file = render_with_context(cache_file_name)
58
+ render_with_context(cache_file_name) do |template_file|
59
59
 
60
- update = false
61
-
62
- if ::File.exists?(@new_resource.path)
63
- @new_resource.checksum(self.checksum(template_file.path))
64
- if @new_resource.checksum != @current_resource.checksum
65
- Chef::Log.debug("#{@new_resource} changed from #{@current_resource.checksum} to #{@new_resource.checksum}")
66
- Chef::Log.info("Updating #{@new_resource} at #{@new_resource.path}")
60
+ update = false
61
+
62
+ if ::File.exists?(@new_resource.path)
63
+ @new_resource.checksum(checksum(template_file.path))
64
+ if @new_resource.checksum != @current_resource.checksum
65
+ Chef::Log.debug("#{@new_resource} changed from #{@current_resource.checksum} to #{@new_resource.checksum}")
66
+ Chef::Log.info("Updating #{@new_resource} at #{@new_resource.path}")
67
+ update = true
68
+ end
69
+ else
70
+ Chef::Log.info("Creating #{@new_resource} at #{@new_resource.path}")
67
71
  update = true
68
72
  end
69
- else
70
- Chef::Log.info("Creating #{@new_resource} at #{@new_resource.path}")
71
- update = true
72
- end
73
-
74
- if update
75
- backup
76
- FileUtils.cp(template_file.path, @new_resource.path)
77
- @new_resource.updated = true
78
- else
79
- Chef::Log.debug("#{@new_resource} is unchanged")
73
+
74
+ if update
75
+ backup
76
+ FileUtils.mv(template_file.path, @new_resource.path)
77
+ @new_resource.updated = true
78
+ else
79
+ Chef::Log.debug("#{@new_resource} is unchanged")
80
+ end
80
81
  end
81
-
82
+
82
83
  set_owner if @new_resource.owner != nil
83
84
  set_group if @new_resource.group != nil
84
85
  set_mode if @new_resource.mode != nil
85
86
  end
86
-
87
+
87
88
  def action_create_if_missing
88
89
  if ::File.exists?(@new_resource.path)
89
90
  Chef::Log.debug("Template #{@new_resource} exists, taking no action.")
@@ -91,32 +92,32 @@ class Chef
91
92
  action_create
92
93
  end
93
94
  end
94
-
95
+
95
96
  private
96
-
97
+
97
98
  def template_updated
98
99
  @template_updated = true
99
100
  end
100
-
101
+
101
102
  def template_not_updated
102
103
  @template_updated = false
103
104
  end
104
-
105
+
105
106
  def template_updated?
106
107
  @template_updated
107
108
  end
108
-
109
+
109
110
  def cookbook_name
110
111
  @cookbook_name = (@new_resource.cookbook || @new_resource.cookbook_name)
111
112
  end
112
-
113
- def render_with_context(cache_file_name)
113
+
114
+ def render_with_context(cache_file_name, &block)
114
115
  context = {}
115
116
  context.merge!(@new_resource.variables)
116
117
  context[:node] = @node
117
- render_template(Chef::FileCache.load(cache_file_name), context)
118
+ render_template(Chef::FileCache.load(cache_file_name), context, &block)
118
119
  end
119
-
120
+
120
121
  def solo_cache_file_name
121
122
  filename = find_preferred_file(
122
123
  cookbook_name,
@@ -129,32 +130,32 @@ class Chef
129
130
  Chef::Log.debug("Using local file for template:#{filename}")
130
131
  Pathname.new(filename).relative_path_from(Pathname.new(Chef::Config[:file_cache_path])).to_s
131
132
  end
132
-
133
+
133
134
  def fetch_template_via_rest(cache_file_name, template_cache_name)
134
135
  if @node.run_state[:template_cache].has_key?(template_cache_name)
135
136
  Chef::Log.debug("I have already fetched the template for #{@new_resource} once this run, not checking again.")
136
137
  template_not_updated
137
138
  return false
138
139
  end
139
-
140
+
140
141
  r = Chef::REST.new(Chef::Config[:template_url])
141
-
142
+
142
143
  current_checksum = nil
143
-
144
+
144
145
  if Chef::FileCache.has_key?(cache_file_name)
145
146
  current_checksum = self.checksum(Chef::FileCache.load(cache_file_name, false))
146
147
  else
147
148
  Chef::Log.debug("Template #{@new_resource} is not in the template cache")
148
149
  end
149
-
150
+
150
151
  template_url = generate_url(
151
- @new_resource.source,
152
+ @new_resource.source,
152
153
  "templates",
153
154
  {
154
155
  :checksum => current_checksum
155
156
  }
156
157
  )
157
-
158
+
158
159
  begin
159
160
  raw_template_file = r.get_rest(template_url, true)
160
161
  template_updated
@@ -165,13 +166,13 @@ class Chef
165
166
  raise e
166
167
  end
167
168
  end
168
-
169
+
169
170
  # We have checked the cache for this template this run
170
171
  @node.run_state[:template_cache][template_cache_name] = true
171
-
172
+
172
173
  raw_template_file
173
174
  end
174
-
175
+
175
176
  end
176
177
  end
177
178
  end