giraffesoft-chef 0.7.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (149) hide show
  1. data/LICENSE +201 -0
  2. data/README.rdoc +135 -0
  3. data/bin/chef-client +26 -0
  4. data/bin/chef-solo +26 -0
  5. data/distro/debian/etc/init.d/chef-client +175 -0
  6. data/distro/debian/etc/init.d/chef-indexer +175 -0
  7. data/distro/debian/etc/init.d/chef-server +120 -0
  8. data/distro/debian/man/man1/chef-indexer.1 +42 -0
  9. data/distro/debian/man/man1/chef-server.1 +108 -0
  10. data/distro/debian/man/man8/chef-client.8 +61 -0
  11. data/distro/debian/man/man8/chef-solo.8 +58 -0
  12. data/distro/redhat/etc/chef/client.rb +16 -0
  13. data/distro/redhat/etc/chef/indexer.rb +10 -0
  14. data/distro/redhat/etc/chef/server.rb +22 -0
  15. data/distro/redhat/etc/init.d/chef-client +74 -0
  16. data/distro/redhat/etc/init.d/chef-indexer +76 -0
  17. data/distro/redhat/etc/init.d/chef-server +77 -0
  18. data/lib/chef.rb +49 -0
  19. data/lib/chef/application.rb +98 -0
  20. data/lib/chef/application/agent.rb +18 -0
  21. data/lib/chef/application/client.rb +209 -0
  22. data/lib/chef/application/indexer.rb +141 -0
  23. data/lib/chef/application/server.rb +18 -0
  24. data/lib/chef/application/solo.rb +214 -0
  25. data/lib/chef/client.rb +421 -0
  26. data/lib/chef/compile.rb +170 -0
  27. data/lib/chef/config.rb +141 -0
  28. data/lib/chef/cookbook.rb +171 -0
  29. data/lib/chef/cookbook/metadata.rb +407 -0
  30. data/lib/chef/cookbook/metadata/version.rb +87 -0
  31. data/lib/chef/cookbook_loader.rb +180 -0
  32. data/lib/chef/couchdb.rb +176 -0
  33. data/lib/chef/daemon.rb +170 -0
  34. data/lib/chef/exceptions.rb +36 -0
  35. data/lib/chef/file_cache.rb +205 -0
  36. data/lib/chef/log.rb +39 -0
  37. data/lib/chef/mixin/check_helper.rb +31 -0
  38. data/lib/chef/mixin/checksum.rb +37 -0
  39. data/lib/chef/mixin/command.rb +386 -0
  40. data/lib/chef/mixin/convert_to_class_name.rb +48 -0
  41. data/lib/chef/mixin/create_path.rb +56 -0
  42. data/lib/chef/mixin/deep_merge.rb +36 -0
  43. data/lib/chef/mixin/find_preferred_file.rb +92 -0
  44. data/lib/chef/mixin/from_file.rb +50 -0
  45. data/lib/chef/mixin/generate_url.rb +49 -0
  46. data/lib/chef/mixin/language.rb +79 -0
  47. data/lib/chef/mixin/params_validate.rb +197 -0
  48. data/lib/chef/mixin/recipe_definition_dsl_core.rb +77 -0
  49. data/lib/chef/mixin/template.rb +84 -0
  50. data/lib/chef/node.rb +406 -0
  51. data/lib/chef/node/attribute.rb +412 -0
  52. data/lib/chef/openid_registration.rb +181 -0
  53. data/lib/chef/platform.rb +254 -0
  54. data/lib/chef/provider.rb +101 -0
  55. data/lib/chef/provider/cron.rb +187 -0
  56. data/lib/chef/provider/deploy.rb +281 -0
  57. data/lib/chef/provider/deploy/revision.rb +70 -0
  58. data/lib/chef/provider/deploy/timestamped.rb +33 -0
  59. data/lib/chef/provider/directory.rb +72 -0
  60. data/lib/chef/provider/execute.rb +58 -0
  61. data/lib/chef/provider/file.rb +191 -0
  62. data/lib/chef/provider/git.rb +198 -0
  63. data/lib/chef/provider/group.rb +120 -0
  64. data/lib/chef/provider/group/gpasswd.rb +50 -0
  65. data/lib/chef/provider/group/groupadd.rb +78 -0
  66. data/lib/chef/provider/group/pw.rb +88 -0
  67. data/lib/chef/provider/group/usermod.rb +57 -0
  68. data/lib/chef/provider/http_request.rb +102 -0
  69. data/lib/chef/provider/ifconfig.rb +131 -0
  70. data/lib/chef/provider/link.rb +157 -0
  71. data/lib/chef/provider/mount.rb +117 -0
  72. data/lib/chef/provider/mount/mount.rb +208 -0
  73. data/lib/chef/provider/package.rb +160 -0
  74. data/lib/chef/provider/package/apt.rb +110 -0
  75. data/lib/chef/provider/package/dpkg.rb +109 -0
  76. data/lib/chef/provider/package/freebsd.rb +153 -0
  77. data/lib/chef/provider/package/macports.rb +105 -0
  78. data/lib/chef/provider/package/portage.rb +124 -0
  79. data/lib/chef/provider/package/rpm.rb +99 -0
  80. data/lib/chef/provider/package/rubygems.rb +136 -0
  81. data/lib/chef/provider/package/yum-dump.py +106 -0
  82. data/lib/chef/provider/package/yum.rb +175 -0
  83. data/lib/chef/provider/remote_directory.rb +126 -0
  84. data/lib/chef/provider/remote_file.rb +141 -0
  85. data/lib/chef/provider/route.rb +118 -0
  86. data/lib/chef/provider/ruby_block.rb +15 -0
  87. data/lib/chef/provider/script.rb +42 -0
  88. data/lib/chef/provider/service.rb +135 -0
  89. data/lib/chef/provider/service/debian.rb +64 -0
  90. data/lib/chef/provider/service/freebsd.rb +157 -0
  91. data/lib/chef/provider/service/gentoo.rb +54 -0
  92. data/lib/chef/provider/service/init.rb +71 -0
  93. data/lib/chef/provider/service/redhat.rb +62 -0
  94. data/lib/chef/provider/service/simple.rb +115 -0
  95. data/lib/chef/provider/subversion.rb +148 -0
  96. data/lib/chef/provider/template.rb +143 -0
  97. data/lib/chef/provider/user.rb +170 -0
  98. data/lib/chef/provider/user/pw.rb +113 -0
  99. data/lib/chef/provider/user/useradd.rb +107 -0
  100. data/lib/chef/queue.rb +145 -0
  101. data/lib/chef/recipe.rb +144 -0
  102. data/lib/chef/resource.rb +380 -0
  103. data/lib/chef/resource/apt_package.rb +34 -0
  104. data/lib/chef/resource/bash.rb +33 -0
  105. data/lib/chef/resource/cron.rb +179 -0
  106. data/lib/chef/resource/csh.rb +33 -0
  107. data/lib/chef/resource/deploy.rb +350 -0
  108. data/lib/chef/resource/deploy_revision.rb +35 -0
  109. data/lib/chef/resource/directory.rb +76 -0
  110. data/lib/chef/resource/dpkg_package.rb +34 -0
  111. data/lib/chef/resource/execute.rb +127 -0
  112. data/lib/chef/resource/file.rb +84 -0
  113. data/lib/chef/resource/gem_package.rb +41 -0
  114. data/lib/chef/resource/git.rb +36 -0
  115. data/lib/chef/resource/group.rb +70 -0
  116. data/lib/chef/resource/http_request.rb +52 -0
  117. data/lib/chef/resource/ifconfig.rb +134 -0
  118. data/lib/chef/resource/link.rb +78 -0
  119. data/lib/chef/resource/macports_package.rb +29 -0
  120. data/lib/chef/resource/mount.rb +135 -0
  121. data/lib/chef/resource/package.rb +80 -0
  122. data/lib/chef/resource/perl.rb +33 -0
  123. data/lib/chef/resource/portage_package.rb +33 -0
  124. data/lib/chef/resource/python.rb +33 -0
  125. data/lib/chef/resource/remote_directory.rb +91 -0
  126. data/lib/chef/resource/remote_file.rb +60 -0
  127. data/lib/chef/resource/route.rb +135 -0
  128. data/lib/chef/resource/ruby.rb +33 -0
  129. data/lib/chef/resource/ruby_block.rb +20 -0
  130. data/lib/chef/resource/scm.rb +129 -0
  131. data/lib/chef/resource/script.rb +51 -0
  132. data/lib/chef/resource/service.rb +134 -0
  133. data/lib/chef/resource/subversion.rb +33 -0
  134. data/lib/chef/resource/template.rb +60 -0
  135. data/lib/chef/resource/timestamped_deploy.rb +31 -0
  136. data/lib/chef/resource/user.rb +98 -0
  137. data/lib/chef/resource_collection.rb +204 -0
  138. data/lib/chef/resource_definition.rb +67 -0
  139. data/lib/chef/rest.rb +238 -0
  140. data/lib/chef/role.rb +231 -0
  141. data/lib/chef/run_list.rb +156 -0
  142. data/lib/chef/runner.rb +130 -0
  143. data/lib/chef/search.rb +88 -0
  144. data/lib/chef/search/result.rb +64 -0
  145. data/lib/chef/search_index.rb +77 -0
  146. data/lib/chef/tasks/chef_repo.rake +347 -0
  147. data/lib/chef/util/file_edit.rb +125 -0
  148. data/lib/chef/util/fileedit.rb +121 -0
  149. metadata +293 -0
@@ -0,0 +1,106 @@
1
+ #
2
+ # Author:: Matthew Kent (<mkent@magoazul.com>)
3
+ # Copyright:: Copyright (c) 2009 Matthew Kent
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
+ # yum-dump.py
20
+ # Inspired by yumhelper.py by David Lutterkort
21
+ #
22
+ # Produce a list of installed and available packages using yum and dump the
23
+ # result to stdout.
24
+ #
25
+ # This invokes yum just as the command line would which makes it subject to
26
+ # all the caching related configuration paramaters in yum.conf.
27
+ #
28
+ # Can be run as non root, but that won't update the cache.
29
+
30
+ import os
31
+ import sys
32
+ import time
33
+ import yum
34
+
35
+ from yum import Errors
36
+
37
+ PIDFILE='/var/run/yum.pid'
38
+
39
+ # Seconds to wait for exclusive access to yum
40
+ lock_timeout = 10
41
+
42
+ failure = False
43
+
44
+ # Can't do try: except: finally: in python 2.4 it seems, hence this fun.
45
+ try:
46
+ try:
47
+ y = yum.YumBase()
48
+ # Only want our output
49
+ y.doConfigSetup(debuglevel=0, errorlevel=0)
50
+
51
+ # Yum assumes it can update the cache directory. Disable this for non root
52
+ # users.
53
+ y.conf.cache = os.geteuid() != 0
54
+
55
+ # Spin up to lock_timeout.
56
+ countdown = lock_timeout
57
+ while True:
58
+ try:
59
+ y.doLock(PIDFILE)
60
+ except Errors.LockError, e:
61
+ time.sleep(1)
62
+ countdown -= 1
63
+ if countdown == 0:
64
+ print >> sys.stderr, "Error! Couldn't obtain an exclusive yum lock in %d seconds. Giving up." % lock_timeout
65
+ failure = True
66
+ sys.exit(1)
67
+ else:
68
+ break
69
+
70
+ y.doTsSetup()
71
+ y.doRpmDBSetup()
72
+
73
+ db = y.doPackageLists('all')
74
+
75
+ y.closeRpmDB()
76
+
77
+ except Errors.YumBaseError, e:
78
+ print >> sys.stderr, "Error! %s" % e
79
+ failure = True
80
+ sys.exit(1)
81
+
82
+ # Ensure we clear the lock.
83
+ finally:
84
+ try:
85
+ y.doUnlock(PIDFILE)
86
+ # Keep Unlock from raising a second exception as it does with a yum.conf
87
+ # config error.
88
+ except Errors.YumBaseError:
89
+ if failure == False:
90
+ print >> sys.stderr, "Error! %s" % e
91
+ sys.exit(1)
92
+
93
+ for pkg in db.installed:
94
+ print '%s,installed,%s,%s,%s,%s' % ( pkg.name,
95
+ pkg.epoch,
96
+ pkg.version,
97
+ pkg.release,
98
+ pkg.arch )
99
+ for pkg in db.available:
100
+ print '%s,available,%s,%s,%s,%s' % ( pkg.name,
101
+ pkg.epoch,
102
+ pkg.version,
103
+ pkg.release,
104
+ pkg.arch )
105
+
106
+ sys.exit(0)
@@ -0,0 +1,175 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Copyright:: Copyright (c) 2008 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/package'
20
+ require 'chef/mixin/command'
21
+ require 'chef/resource/package'
22
+ require 'singleton'
23
+
24
+ class Chef
25
+ class Provider
26
+ class Package
27
+ class Yum < Chef::Provider::Package
28
+
29
+ class YumCache
30
+ include Chef::Mixin::Command
31
+ include Singleton
32
+
33
+ def initialize
34
+ load_data
35
+ end
36
+
37
+ def stale?
38
+ interval = Chef::Config[:interval].to_f
39
+
40
+ # run once mode
41
+ if interval == 0
42
+ return false
43
+ elsif (Time.now - @updated_at) > interval
44
+ return true
45
+ end
46
+
47
+ false
48
+ end
49
+
50
+ def refresh
51
+ if @data.empty?
52
+ reload
53
+ elsif stale?
54
+ reload
55
+ end
56
+ end
57
+
58
+ def load_data
59
+ @data = Hash.new
60
+ error = String.new
61
+
62
+ helper = ::File.join(::File.dirname(__FILE__), 'yum-dump.py')
63
+ status = popen4("python #{helper}", :waitlast => true) do |pid, stdin, stdout, stderr|
64
+ stdout.each do |line|
65
+ line.chomp!
66
+ name, type, epoch, version, release, arch = line.split(',')
67
+ type_sym = type.to_sym
68
+ if !@data.has_key?(name)
69
+ @data[name] = Hash.new
70
+ end
71
+ @data[name][type_sym] = { :epoch => epoch, :version => version,
72
+ :release => release, :arch => arch }
73
+ end
74
+
75
+ error = stderr.readlines
76
+ end
77
+
78
+ unless status.exitstatus == 0
79
+ raise Chef::Exceptions::Package, "yum failed - #{status.inspect} - returns: #{error}"
80
+ end
81
+
82
+ @updated_at = Time.now
83
+ end
84
+ alias :reload :load_data
85
+
86
+ def version(package_name, type)
87
+ if (x = @data[package_name])
88
+ if (y = x[type])
89
+ return "#{y[:version]}-#{y[:release]}"
90
+ end
91
+ end
92
+
93
+ nil
94
+ end
95
+
96
+ def installed_version(package_name)
97
+ version(package_name, :installed)
98
+ end
99
+
100
+ def candidate_version(package_name)
101
+ version(package_name, :available)
102
+ end
103
+
104
+ def flush
105
+ @data.clear
106
+ end
107
+ end
108
+
109
+ def initialize(node, new_resource, collection=nil, definitions=nil, cookbook_loader=nil)
110
+ @yum = YumCache.instance
111
+ super(node, new_resource, collection, definitions, cookbook_loader)
112
+ end
113
+
114
+ def load_current_resource
115
+ @current_resource = Chef::Resource::Package.new(@new_resource.name)
116
+ @current_resource.package_name(@new_resource.package_name)
117
+
118
+ Chef::Log.debug("Checking yum info for #{@new_resource.package_name}")
119
+
120
+ @yum.refresh
121
+
122
+ installed_version = @yum.installed_version(@new_resource.package_name)
123
+ @candidate_version = @yum.candidate_version(@new_resource.package_name)
124
+
125
+ @current_resource.version(installed_version)
126
+ if candidate_version
127
+ @candidate_version = candidate_version
128
+ else
129
+ @candidate_version = installed_version
130
+ end
131
+
132
+ @current_resource
133
+ end
134
+
135
+ def install_package(name, version)
136
+ run_command_with_systems_locale(
137
+ :command => "yum -q -y install #{name}-#{version}"
138
+ )
139
+ @yum.flush
140
+ end
141
+
142
+ def upgrade_package(name, version)
143
+ # If we have a version, we can upgrade - otherwise, install
144
+ if @current_resource.version
145
+ run_command_with_systems_locale(
146
+ :command => "yum -q -y update #{name}-#{version}"
147
+ )
148
+ @yum.flush
149
+ else
150
+ install_package(name, version)
151
+ end
152
+ end
153
+
154
+ def remove_package(name, version)
155
+ if version
156
+ run_command_with_systems_locale(
157
+ :command => "yum -q -y remove #{name}-#{version}"
158
+ )
159
+ else
160
+ run_command_with_systems_locale(
161
+ :command => "yum -q -y remove #{name}"
162
+ )
163
+ end
164
+
165
+ @yum.flush
166
+ end
167
+
168
+ def purge_package(name, version)
169
+ remove_package(name, version)
170
+ end
171
+
172
+ end
173
+ end
174
+ end
175
+ end
@@ -0,0 +1,126 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Copyright:: Copyright (c) 2008 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/file'
20
+ require 'chef/provider/directory'
21
+ require 'chef/rest'
22
+ require 'chef/mixin/find_preferred_file'
23
+ require 'chef/resource/directory'
24
+ require 'chef/resource/remote_file'
25
+ require 'chef/platform'
26
+ require 'uri'
27
+ require 'tempfile'
28
+ require 'net/https'
29
+
30
+ class Chef
31
+ class Provider
32
+ class RemoteDirectory < Chef::Provider::Directory
33
+
34
+ include ::Chef::Mixin::FindPreferredFile
35
+
36
+ def action_create
37
+ super
38
+
39
+ @remote_file_list = Hash.new
40
+ do_recursive
41
+ end
42
+
43
+ protected
44
+
45
+ def do_recursive
46
+ if Chef::Config[:solo]
47
+ Chef::Log.debug("Doing a local recursive directory copy for #{@new_resource}")
48
+ files_to_transfer = files_for_directory(@new_resource.source)
49
+ else
50
+ Chef::Log.debug("Doing a remote recursive directory transfer for #{@new_resource}")
51
+ r = Chef::REST.new(Chef::Config[:remotefile_url])
52
+ files_to_transfer = r.get_rest(generate_url(@new_resource.source, "files", { :recursive => "true" }))
53
+ end
54
+
55
+ files_to_transfer.each do |remote_file_source|
56
+ fetch_remote_file(remote_file_source)
57
+ end
58
+ end
59
+
60
+ def fetch_remote_file(remote_file_source)
61
+ full_path = ::File.join(@new_resource.path, remote_file_source)
62
+ full_dir = ::File.dirname(full_path)
63
+
64
+ if !::File.directory?(full_dir)
65
+ create_directory(full_dir)
66
+ end
67
+
68
+ remote_file = Chef::Resource::RemoteFile.new(full_path, nil, @node)
69
+ remote_file.cookbook_name = @new_resource.cookbook || @new_resource.cookbook_name
70
+ remote_file.source(::File.join(@new_resource.source, remote_file_source))
71
+ remote_file.mode(@new_resource.files_mode) if @new_resource.files_mode
72
+ remote_file.group(@new_resource.files_group) if @new_resource.files_group
73
+ remote_file.owner(@new_resource.files_owner) if @new_resource.files_owner
74
+ remote_file.backup(@new_resource.files_backup) if @new_resource.files_backup
75
+
76
+ rf_provider = Chef::Platform.provider_for_node(@node, remote_file)
77
+ rf_provider.load_current_resource
78
+ rf_provider.action_create
79
+ @new_resource.updated = true if rf_provider.new_resource.updated
80
+ end
81
+
82
+ def create_directory(full_dir)
83
+ new_dir = Chef::Resource::Directory.new(full_dir, nil, @node)
84
+ new_dir.cookbook_name = @new_resource.cookbook || @new_resource.cookbook_name
85
+ new_dir.mode(@new_resource.mode)
86
+ new_dir.group(@new_resource.group)
87
+ new_dir.owner(@new_resource.owner)
88
+ new_dir.recursive(true)
89
+
90
+ d_provider = Chef::Platform.provider_for_node(@node, new_dir)
91
+ d_provider.load_current_resource
92
+ d_provider.action_create
93
+ @new_resource.updated = true if d_provider.new_resource.updated
94
+ end
95
+
96
+ def action_create_if_missing
97
+ raise Chef::Exceptions::UnsupportedAction, "Remote Directories do not support create_if_missing."
98
+ end
99
+ # Pulled from chef-server-slice files controller
100
+
101
+ def files_for_directory(path)
102
+ directory = find_preferred_file(
103
+ @new_resource.cookbook_name,
104
+ :remote_file,
105
+ path,
106
+ @node[:fqdn],
107
+ @node[:platform],
108
+ @node[:platform_version]
109
+ )
110
+
111
+ unless (directory && ::File.directory?(directory))
112
+ raise NotFound, "Cannot find a suitable directory"
113
+ end
114
+
115
+ directory_listing = Array.new
116
+ Dir[::File.join(directory, '**', '*')].sort { |a,b| b <=> a }.each do |file|
117
+ next if ::File.directory?(file)
118
+ file =~ /^#{directory}\/(.+)$/
119
+ directory_listing << $1
120
+ end
121
+ directory_listing
122
+ end
123
+
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,141 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Copyright:: Copyright (c) 2008 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/file'
20
+ require 'chef/rest'
21
+ require 'chef/mixin/find_preferred_file'
22
+ require 'uri'
23
+ require 'tempfile'
24
+ require 'net/https'
25
+
26
+ class Chef
27
+ class Provider
28
+ class RemoteFile < Chef::Provider::File
29
+
30
+ include Chef::Mixin::FindPreferredFile
31
+
32
+ def action_create
33
+ Chef::Log.debug("Checking #{@new_resource} for changes")
34
+ do_remote_file(@new_resource.source, @current_resource.path)
35
+ end
36
+
37
+ def action_create_if_missing
38
+ if ::File.exists?(@new_resource.path)
39
+ Chef::Log.debug("File #{@new_resource.path} exists, taking no action.")
40
+ else
41
+ action_create
42
+ end
43
+ end
44
+
45
+ def do_remote_file(source, path)
46
+ retval = true
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")
50
+ else
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
70
+ FileUtils.cp raw_file.path, @new_resource.path
71
+ @new_resource.updated = true
72
+ else
73
+ Chef::Log.debug "#{@new_resource}: Target and Source checksums are the same, taking no action"
74
+ 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
+ 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
+ rescue Net::HTTPRetriableError => e
85
+ if e.response.kind_of?(Net::HTTPNotModified)
86
+ Chef::Log.debug("File #{path} is unchanged")
87
+ retval = false
88
+ else
89
+ raise e
90
+ end
91
+ end
92
+ end
93
+
94
+ set_owner if @new_resource.owner
95
+ set_group if @new_resource.group
96
+ set_mode if @new_resource.mode
97
+
98
+ retval
99
+ end
100
+
101
+ def get_from_uri(source)
102
+ begin
103
+ uri = URI.parse(source)
104
+ if uri.absolute
105
+ r = Chef::REST.new(source)
106
+ Chef::Log.debug("Downloading from absolute URI: #{source}")
107
+ r.get_rest(source, true).open
108
+ end
109
+ rescue URI::InvalidURIError
110
+ nil
111
+ end
112
+ end
113
+
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
120
+ end
121
+ end
122
+
123
+ def get_from_local_cookbook(source)
124
+ if Chef::Config[:solo]
125
+ cookbook_name = @new_resource.cookbook || @new_resource.cookbook_name
126
+ filename = find_preferred_file(
127
+ cookbook_name,
128
+ :remote_file,
129
+ source,
130
+ @node[:fqdn],
131
+ @node[:platform],
132
+ @node[:platform_version]
133
+ )
134
+ Chef::Log.debug("Using local file for remote_file:#{filename}")
135
+ ::File.open(filename)
136
+ end
137
+ end
138
+
139
+ end
140
+ end
141
+ end