from-scratch 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (174) hide show
  1. checksums.yaml +7 -0
  2. data/.chef/knife.rb +6 -0
  3. data/.gitignore +9 -0
  4. data/.rspec +2 -0
  5. data/.travis.yml +4 -0
  6. data/Berksfile +17 -0
  7. data/Berksfile.lock +19 -0
  8. data/Gemfile +4 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +58 -0
  11. data/Rakefile +6 -0
  12. data/Thorfile +5 -0
  13. data/bin/scratchify +6 -0
  14. data/chefignore +101 -0
  15. data/cookbooks/chef_gem/CHANGELOG.md +24 -0
  16. data/cookbooks/chef_gem/README.md +38 -0
  17. data/cookbooks/chef_gem/libraries/chef_gem.rb +72 -0
  18. data/cookbooks/chef_gem/metadata.json +29 -0
  19. data/cookbooks/chef_gem/recipes/default.rb +0 -0
  20. data/cookbooks/java/.gitignore +21 -0
  21. data/cookbooks/java/.kitchen.docker.yml +15 -0
  22. data/cookbooks/java/.kitchen.yml +126 -0
  23. data/cookbooks/java/.travis.yml +6 -0
  24. data/cookbooks/java/Berksfile +10 -0
  25. data/cookbooks/java/CHANGELOG.md +398 -0
  26. data/cookbooks/java/CONTRIBUTING.md +12 -0
  27. data/cookbooks/java/Gemfile +15 -0
  28. data/cookbooks/java/ISSUES.md +43 -0
  29. data/cookbooks/java/LICENSE +201 -0
  30. data/cookbooks/java/README.md +402 -0
  31. data/cookbooks/java/Rakefile +60 -0
  32. data/cookbooks/java/TESTING.md +41 -0
  33. data/cookbooks/java/attributes/default.rb +146 -0
  34. data/cookbooks/java/libraries/helpers.rb +120 -0
  35. data/cookbooks/java/libraries/matchers.rb +5 -0
  36. data/cookbooks/java/metadata.json +69 -0
  37. data/cookbooks/java/providers/alternatives.rb +94 -0
  38. data/cookbooks/java/providers/ark.rb +255 -0
  39. data/cookbooks/java/recipes/default.rb +22 -0
  40. data/cookbooks/java/recipes/default_java_symlink.rb +19 -0
  41. data/cookbooks/java/recipes/homebrew.rb +3 -0
  42. data/cookbooks/java/recipes/ibm.rb +76 -0
  43. data/cookbooks/java/recipes/ibm_tar.rb +70 -0
  44. data/cookbooks/java/recipes/openjdk.rb +74 -0
  45. data/cookbooks/java/recipes/oracle.rb +74 -0
  46. data/cookbooks/java/recipes/oracle_i386.rb +73 -0
  47. data/cookbooks/java/recipes/oracle_jce.rb +75 -0
  48. data/cookbooks/java/recipes/oracle_rpm.rb +60 -0
  49. data/cookbooks/java/recipes/purge_packages.rb +20 -0
  50. data/cookbooks/java/recipes/set_attributes_from_version.rb +55 -0
  51. data/cookbooks/java/recipes/set_java_home.rb +44 -0
  52. data/cookbooks/java/recipes/windows.rb +82 -0
  53. data/cookbooks/java/resources/alternatives.rb +30 -0
  54. data/cookbooks/java/resources/ark.rb +58 -0
  55. data/cookbooks/java/templates/default/ibm_jdk.installer.properties.erb +3 -0
  56. data/cookbooks/java/templates/default/oracle.jinfo.erb +6 -0
  57. data/cookbooks/rvm/.foodcritic +1 -0
  58. data/cookbooks/rvm/.gitignore +7 -0
  59. data/cookbooks/rvm/.kitchen.yml +66 -0
  60. data/cookbooks/rvm/.travis.yml +9 -0
  61. data/cookbooks/rvm/Berksfile +10 -0
  62. data/cookbooks/rvm/CHANGELOG.md +253 -0
  63. data/cookbooks/rvm/CODE_OF_CONDUCT.md +12 -0
  64. data/cookbooks/rvm/CONTRIBUTING.md +26 -0
  65. data/cookbooks/rvm/Gemfile +20 -0
  66. data/cookbooks/rvm/Guardfile +12 -0
  67. data/cookbooks/rvm/README.md +1459 -0
  68. data/cookbooks/rvm/Rakefile +18 -0
  69. data/cookbooks/rvm/attributes/.gitkeep +0 -0
  70. data/cookbooks/rvm/attributes/default.rb +68 -0
  71. data/cookbooks/rvm/attributes/gem_package.rb +23 -0
  72. data/cookbooks/rvm/attributes/vagrant.rb +23 -0
  73. data/cookbooks/rvm/libraries/chef_rvm_environment_helpers.rb +55 -0
  74. data/cookbooks/rvm/libraries/chef_rvm_gemset_helpers.rb +67 -0
  75. data/cookbooks/rvm/libraries/chef_rvm_recipe_helpers.rb +80 -0
  76. data/cookbooks/rvm/libraries/chef_rvm_ruby_helpers.rb +95 -0
  77. data/cookbooks/rvm/libraries/chef_rvm_set_helpers.rb +16 -0
  78. data/cookbooks/rvm/libraries/chef_rvm_shell_helpers.rb +57 -0
  79. data/cookbooks/rvm/libraries/chef_rvm_string_cache.rb +104 -0
  80. data/cookbooks/rvm/libraries/chef_rvm_string_helpers.rb +61 -0
  81. data/cookbooks/rvm/libraries/chef_rvm_version_helpers.rb +45 -0
  82. data/cookbooks/rvm/libraries/gem_package_monkeypatch.rb +34 -0
  83. data/cookbooks/rvm/libraries/provider_rvm_installation.rb +185 -0
  84. data/cookbooks/rvm/libraries/resource_rvm_installation.rb +103 -0
  85. data/cookbooks/rvm/libraries/rvm_chef_user_environment.rb +55 -0
  86. data/cookbooks/rvm/libraries/rvm_rubygems_package.rb +189 -0
  87. data/cookbooks/rvm/libraries/rvm_shell_chef_wrapper.rb +99 -0
  88. data/cookbooks/rvm/metadata.json +51 -0
  89. data/cookbooks/rvm/providers/default_ruby.rb +63 -0
  90. data/cookbooks/rvm/providers/environment.rb +78 -0
  91. data/cookbooks/rvm/providers/gemset.rb +118 -0
  92. data/cookbooks/rvm/providers/global_gem.rb +97 -0
  93. data/cookbooks/rvm/providers/ruby.rb +178 -0
  94. data/cookbooks/rvm/providers/shell.rb +121 -0
  95. data/cookbooks/rvm/providers/wrapper.rb +73 -0
  96. data/cookbooks/rvm/recipes/.gitkeep +0 -0
  97. data/cookbooks/rvm/recipes/default.rb +40 -0
  98. data/cookbooks/rvm/recipes/gem_package.rb +33 -0
  99. data/cookbooks/rvm/recipes/system.rb +37 -0
  100. data/cookbooks/rvm/recipes/system_install.rb +42 -0
  101. data/cookbooks/rvm/recipes/user.rb +43 -0
  102. data/cookbooks/rvm/recipes/user_install.rb +58 -0
  103. data/cookbooks/rvm/recipes/vagrant.rb +37 -0
  104. data/cookbooks/rvm/resources/default_ruby.rb +31 -0
  105. data/cookbooks/rvm/resources/environment.rb +31 -0
  106. data/cookbooks/rvm/resources/gem.rb +37 -0
  107. data/cookbooks/rvm/resources/gemset.rb +32 -0
  108. data/cookbooks/rvm/resources/global_gem.rb +34 -0
  109. data/cookbooks/rvm/resources/ruby.rb +32 -0
  110. data/cookbooks/rvm/resources/shell.rb +41 -0
  111. data/cookbooks/rvm/resources/wrapper.rb +34 -0
  112. data/cookbooks/rvm/templates/default/rvmrc.erb +14 -0
  113. data/cookbooks/rvm/templates/default/vagrant-chef-client-wrapper.erb +23 -0
  114. data/cookbooks/rvm/templates/default/vagrant-chef-solo-wrapper.erb +23 -0
  115. data/cookbooks/rvm/test/integration/data_bags/users/virgil1.json +4 -0
  116. data/cookbooks/rvm/test/integration/data_bags/users/virgil2.json +4 -0
  117. data/cookbooks/rvm/test/integration/data_bags/users/wigglebottom.json +3 -0
  118. data/cookbooks/rvm/test/integration/installs/bats/version_pinning.bats +13 -0
  119. data/cookbooks/rvm/test/integration/rubies/bats/_common.bash +16 -0
  120. data/cookbooks/rvm/test/integration/rubies/bats/verify_1.9.3.bats +9 -0
  121. data/cookbooks/rvm/test/integration/rubies/bats/verify_jruby.bats +12 -0
  122. data/cookbooks/rvm/test/integration/rubies/bats/verify_patch_support.bats +19 -0
  123. data/cookbooks/rvm/test/integration/rubies/bats/verify_ree.bats +9 -0
  124. data/cookbooks/rvm/test/integration/rubies/bats/verify_rubygems_version_support.bats +15 -0
  125. data/cookbooks/rvm/test/integration/stock_system_and_user/bats/system.bats +45 -0
  126. data/cookbooks/rvm/test/unit/libraries/provider_rvm_installation_spec.rb +220 -0
  127. data/cookbooks/rvm/test/unit/libraries/resource_rvm_installation_spec.rb +139 -0
  128. data/cookbooks/rvm/test/unit/spec_helper.rb +23 -0
  129. data/cookbooks/scratchify/.chef/knife.rb +9 -0
  130. data/cookbooks/scratchify/Berksfile +16 -0
  131. data/cookbooks/scratchify/Berksfile.lock +19 -0
  132. data/cookbooks/scratchify/LICENSE.txt +21 -0
  133. data/cookbooks/scratchify/README.md +41 -0
  134. data/cookbooks/scratchify/Thorfile +5 -0
  135. data/cookbooks/scratchify/bin/console +14 -0
  136. data/cookbooks/scratchify/bin/scratchify +6 -0
  137. data/cookbooks/scratchify/bin/setup +7 -0
  138. data/cookbooks/scratchify/chefignore +101 -0
  139. data/cookbooks/scratchify/data_bags/users/deploy.json +6 -0
  140. data/cookbooks/scratchify/environments/.gitkeep +0 -0
  141. data/cookbooks/scratchify/from-scratch.gemspec +36 -0
  142. data/cookbooks/scratchify/lib/from/scratch.rb +31 -0
  143. data/cookbooks/scratchify/lib/from/scratch/interviewer.rb +35 -0
  144. data/cookbooks/scratchify/lib/from/scratch/version.rb +3 -0
  145. data/cookbooks/scratchify/metadata.json +36 -0
  146. data/cookbooks/scratchify/nodes/normfood.ru.json +75 -0
  147. data/cookbooks/scratchify/recipes/default.rb +0 -0
  148. data/cookbooks/scratchify/roles/.gitkeep +0 -0
  149. data/cookbooks/scratchify/spec/from/scratch_spec.rb +11 -0
  150. data/cookbooks/scratchify/spec/spec_helper.rb +2 -0
  151. data/cookbooks/user/.gitignore +5 -0
  152. data/cookbooks/user/.kitchen.yml +46 -0
  153. data/cookbooks/user/.travis.yml +4 -0
  154. data/cookbooks/user/Berksfile +7 -0
  155. data/cookbooks/user/CHANGELOG.md +101 -0
  156. data/cookbooks/user/Gemfile +14 -0
  157. data/cookbooks/user/README.md +447 -0
  158. data/cookbooks/user/Rakefile +21 -0
  159. data/cookbooks/user/attributes/default.rb +50 -0
  160. data/cookbooks/user/libraries/matchers.rb +26 -0
  161. data/cookbooks/user/metadata.json +46 -0
  162. data/cookbooks/user/providers/account.rb +212 -0
  163. data/cookbooks/user/recipes/data_bag.rb +59 -0
  164. data/cookbooks/user/recipes/default.rb +18 -0
  165. data/cookbooks/user/resources/account.rb +41 -0
  166. data/cookbooks/user/templates/default/authorized_keys.erb +7 -0
  167. data/from-scratch.gemspec +33 -0
  168. data/lib/from-scratch.rb +25 -0
  169. data/lib/from-scratch/version.rb +3 -0
  170. data/metadata.rb +15 -0
  171. data/recipes/default.rb +0 -0
  172. data/templates/node.json.erb +31 -0
  173. data/templates/user.json.erb +6 -0
  174. metadata +330 -0
@@ -0,0 +1,57 @@
1
+ #
2
+ # Cookbook Name:: rvm
3
+ # Library:: Chef::RVM::ShellHelpers
4
+ #
5
+ # Author:: Fletcher Nichol <fnichol@nichol.ca>
6
+ #
7
+ # Copyright 2011, Fletcher Nichol
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ #
21
+
22
+ class Chef
23
+ module RVM
24
+ module ShellHelpers
25
+ ##
26
+ # Finds the correct shell profile to source to init an RVM-aware
27
+ # shell environment
28
+ #
29
+ # @param [String] a user's home directory path if this is for a user or
30
+ # nil if it is in a system context
31
+ # @return [String] full path the shell profile
32
+ def find_profile_to_source(user_dir = nil)
33
+ if user_dir
34
+ "#{user_dir}/.rvm/scripts/rvm"
35
+ else
36
+ if ::File.directory?("/etc/profile.d")
37
+ "/etc/profile.d/rvm.sh"
38
+ else
39
+ "/etc/profile"
40
+ end
41
+ end
42
+ end
43
+
44
+ ##
45
+ # Returns a shell command that is RVM-aware
46
+ #
47
+ # @param [String, #to_s] the shell command to be wrapped
48
+ # @param [String] A user's home directory path if this is for a user or
49
+ # nil if it is in a system context
50
+ # @return [String] the command wrapped in RVM-initialized bash command
51
+ def rvm_wrap_cmd(cmd, user_dir = nil)
52
+ profile = find_profile_to_source(user_dir)
53
+ %{bash -c "source #{profile} && #{cmd.gsub(/"/, '\"')}"}
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,104 @@
1
+ #
2
+ # Cookbook Name:: rvm
3
+ # Library:: Chef::RVM::StringCache
4
+ #
5
+ # Author:: Fletcher Nichol <fnichol@nichol.ca>
6
+ #
7
+ # Copyright 2011, Fletcher Nichol
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ #
21
+
22
+ require 'chef/mixin/command'
23
+
24
+ class Chef
25
+ module RVM
26
+ module ShellHelpers
27
+ # stub to satisfy StringCache (library load order not guarenteed)
28
+ end
29
+
30
+ class StringCache
31
+ class << self
32
+ include Chef::Mixin::Command
33
+ include Chef::RVM::ShellHelpers
34
+ end
35
+
36
+ ##
37
+ # Returns a fully qualified RVM Ruby string for the given input string
38
+ #
39
+ # @param [String] a string that can interpreted by RVM
40
+ # @param [String] the username if this is for a user install or nil if
41
+ # it is a system install
42
+ # @return [String] a fully qualified RVM Ruby string
43
+ def self.fetch(str, user = nil)
44
+ @@strings ||= Hash.new
45
+ rvm_install = user || "system"
46
+ @@strings[rvm_install] ||= Hash.new
47
+
48
+ return @@strings[rvm_install][str] if @@strings[rvm_install].has_key?(str)
49
+
50
+ result = canonical_ruby_string(str, user)
51
+ # cache everything except default environment
52
+ if str == 'default'
53
+ result
54
+ else
55
+ @@strings[rvm_install][str] = result
56
+ end
57
+ end
58
+
59
+ protected
60
+
61
+ def self.canonical_ruby_string(str, user)
62
+ Chef::Log.debug("Fetching canonical RVM string for: #{str} " +
63
+ "(#{user || 'system'})")
64
+ if user
65
+ user_dir = Etc.getpwnam(user).dir
66
+ else
67
+ user_dir = nil
68
+ end
69
+
70
+ cmd = ["source #{find_profile_to_source(user_dir)}",
71
+ "rvm_ruby_string='#{str}'", "__rvm_ruby_string",
72
+ "echo $rvm_ruby_string"].join(" && ")
73
+ pid, stdin, stdout, stderr = popen4('bash', shell_params(user, user_dir))
74
+ stdin.puts(cmd)
75
+ stdin.close
76
+
77
+ result = stdout.read.split('\n').first.chomp
78
+ if result =~ /^-/ # if the result has a leading dash, value is bogus
79
+ Chef::Log.warn("Could not determine canonical RVM string for: #{str} " +
80
+ "(#{user || 'system'})")
81
+ nil
82
+ else
83
+ Chef::Log.debug("Canonical RVM string is: #{str} => #{result} " +
84
+ "(#{user || 'system'})")
85
+ result
86
+ end
87
+ end
88
+
89
+ def self.shell_params(user, user_dir)
90
+ if user
91
+ {
92
+ :user => user,
93
+ :environment => {
94
+ 'USER' => user,
95
+ 'HOME' => user_dir
96
+ }
97
+ }
98
+ else
99
+ Hash.new
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,61 @@
1
+ #
2
+ # Cookbook Name:: rvm
3
+ # Library:: Chef::RVM::StringHelpers
4
+ #
5
+ # Author:: Fletcher Nichol <fnichol@nichol.ca>
6
+ #
7
+ # Copyright 2011, Fletcher Nichol
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ #
21
+
22
+ class Chef
23
+ module RVM
24
+ module StringHelpers
25
+ ##
26
+ # Filters out any gemset declarations in an RVM Ruby string
27
+ #
28
+ # @param [String, #to_s] the RVM Ruby string
29
+ # @return [String] the Ruby string, minus gemset
30
+ def select_ruby(ruby_string)
31
+ ruby_string.split('@').first
32
+ end
33
+
34
+ ##
35
+ # Filters out any Ruby declaration in an RVM Ruby string
36
+ #
37
+ # @param [String, #to_s] the RVM Ruby string
38
+ # @return [String] the gemset string, minus Ruby or nil if no gemset given
39
+ def select_gemset(ruby_string)
40
+ if ruby_string.include?('@')
41
+ ruby_string.split('@').last
42
+ else
43
+ nil
44
+ end
45
+ end
46
+
47
+ ##
48
+ # Sanitizes a Ruby string so that it's more normalized.
49
+ #
50
+ # @param [String, #to_s] an RVM Ruby string
51
+ # @param [String] a specific user RVM or nil for system-wide
52
+ # @return [String] a fully qualified RVM Ruby string
53
+ def normalize_ruby_string(ruby_string, user = new_resource.user, patch = new_resource.patch)
54
+ return "system" if ruby_string == "system"
55
+ fetched_ruby_string = StringCache.fetch(ruby_string, user)
56
+ return "#{fetched_ruby_string} --patch #{patch}" if patch
57
+ fetched_ruby_string
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,45 @@
1
+ class Chef
2
+ module RVM
3
+ module ShellHelpers
4
+ # stub to satisfy VersionCache (library load order not guarenteed)
5
+ end
6
+
7
+ module VersionHelpers
8
+ def rvm_version(user = nil)
9
+ VersionCache.fetch_version(user)
10
+ end
11
+ end
12
+
13
+ class VersionCache
14
+ class << self
15
+ include Chef::Mixin::ShellOut
16
+ include Chef::RVM::ShellHelpers
17
+ end
18
+
19
+ def self.fetch_version(user = nil)
20
+ @@versions ||= Hash.new
21
+ rvm_install = user || "system"
22
+ @@versions[rvm_install] ||= rvm_version(user)
23
+ end
24
+
25
+ def self.rvm_version(user = nil)
26
+ cmd = "rvm version | cut -d ' ' -f 2"
27
+
28
+ if user
29
+ user_dir = Etc.getpwnam(user).dir
30
+ environment = { 'USER' => user, 'HOME' => user_dir }
31
+ else
32
+ user_dir = nil
33
+ environment = nil
34
+ end
35
+
36
+ version = shell_out!(
37
+ rvm_wrap_cmd(cmd, user_dir), :env => environment).stdout.strip
38
+
39
+ Chef::Log.debug "RVM version = #{version} (#{user || 'system'})"
40
+
41
+ version
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,34 @@
1
+ #
2
+ # Cookbook Name:: rvm
3
+ # Library: gem_package resource monkey patch
4
+ #
5
+ # Author:: Fletcher Nichol <fnichol@nichol.ca>
6
+ #
7
+ # Copyright 2011, Fletcher Nichol
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ #
21
+
22
+ ##
23
+ # Patch Chef::Resource::GemPackage resource to use the RVMRubygems provider.
24
+ # This has potentially dangerous side effects and should be considered
25
+ # experimental. You have been warned.
26
+ def patch_gem_package
27
+ ::Chef::Resource::GemPackage.class_eval do
28
+ def initialize(name, run_context=nil)
29
+ super
30
+ @resource_name = :gem_package
31
+ @provider = Chef::Provider::Package::RVMRubygems
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,185 @@
1
+ #
2
+ # Cookbook Name:: rvm
3
+ # Provider:: rvm_installation
4
+ #
5
+ # Author:: Fletcher Nichol <fnichol@nichol.ca>
6
+ #
7
+ # Copyright 2013, Fletcher Nichol
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ #
21
+
22
+ require "chef/provider"
23
+ require "chef/resource/remote_file"
24
+ require "chef/resource/package"
25
+ require "chef/resource/template"
26
+ require "chef/mixin/shell_out"
27
+
28
+ class Chef
29
+
30
+ class Provider
31
+
32
+ class RvmInstallation < Chef::Provider
33
+
34
+ include Chef::Mixin::ShellOut
35
+
36
+ def load_current_resource
37
+ @current_resource = Chef::Resource::RvmInstallation.new(new_resource.name)
38
+ if @current_resource.installed(installed?)
39
+ @current_resource.version(version)
40
+ end
41
+ @current_resource
42
+ end
43
+
44
+ def action_install
45
+ converge_rvmrc
46
+ if current_resource.installed
47
+ Chef::Log.info("#{new_resource} #{current_resource.version} " +
48
+ "already installed - nothing to do")
49
+ else
50
+ converge_install
51
+ end
52
+ end
53
+
54
+ def action_force
55
+ converge_rvmrc
56
+ end
57
+
58
+ def converge_rvmrc
59
+ converge_by("manage rvmrc for #{new_resource}") { write_rvmrc }
60
+ end
61
+
62
+ def converge_install
63
+ converge_by("install RVM for #{new_resource}") { install_rvm }
64
+ Chef::Log.info("#{new_resource} #{new_resource.version} installed")
65
+ end
66
+
67
+ def install_rvm
68
+ install_packages
69
+ download_installer
70
+ run_install_cmd
71
+
72
+ new_resource.version(version)
73
+ end
74
+
75
+ def install_packages
76
+ Array(new_resource.install_pkgs).map do |pkg|
77
+ r = Chef::Resource::Package.new(pkg, run_context)
78
+ r.run_action(:install)
79
+ r
80
+ end
81
+ end
82
+
83
+ def write_rvmrc
84
+ r = Chef::Resource::Template.new(rvmrc_path, run_context)
85
+ r.owner(new_resource.user)
86
+ r.group(etc_user.gid)
87
+ r.mode("0644")
88
+ r.source(new_resource.rvmrc_template_source)
89
+ r.cookbook(new_resource.rvmrc_template_cookbook)
90
+ r.variables(
91
+ :user => new_resource.user,
92
+ :rvm_path => rvm_path,
93
+ :rvmrc_env => new_resource.rvmrc_env
94
+ )
95
+ r.run_action(:create)
96
+ r
97
+ end
98
+
99
+ def download_installer
100
+ r = Chef::Resource::RemoteFile.new(rvm_installer_path, run_context)
101
+ r.source(new_resource.installer_url)
102
+ r.run_action(:create)
103
+ r
104
+ end
105
+
106
+ def run_install_cmd
107
+ rvm_shell_out!(%{bash #{rvm_installer_path} #{new_resource.installer_flags}})
108
+ end
109
+
110
+ def rvm_installer_path
111
+ ::File.join(
112
+ Chef::Config[:file_cache_path], "rvm-installer-#{new_resource.user}"
113
+ )
114
+ end
115
+
116
+ def installed?
117
+ cmd = rvm_shell_out(
118
+ %{bash -c "source #{rvm_path}/scripts/rvm && type rvm"}
119
+ )
120
+ (cmd.exitstatus == 0 && cmd.stdout.lines.first == "rvm is a function\n")
121
+ end
122
+
123
+ def version
124
+ cmd = rvm("version")
125
+ matches = /^rvm ([\w.]+)/.match(cmd.stdout)
126
+
127
+ if cmd.exitstatus != 0
128
+ raise "Could not determine version for #{new_resource}, " +
129
+ "exited (#{cmd.exitstatus})"
130
+ end
131
+
132
+ if matches && matches[1]
133
+ return matches[1]
134
+ else
135
+ raise "Could not determine version for #{new_resource} " +
136
+ "from version string [#{cmd.stdout}]"
137
+ end
138
+ end
139
+
140
+ def rvm_shell_out(cmd)
141
+ user = new_resource.user
142
+ home_dir = etc_user.dir
143
+ opts = {
144
+ :user => user,
145
+ :group => etc_user.gid,
146
+ :cwd => home_dir,
147
+ :env => { "HOME" => home_dir, "USER" => user, "TERM" => "dumb" }
148
+ }
149
+
150
+ Chef::Log.debug("Running [#{cmd}] with #{opts}")
151
+ shell_out(cmd, opts)
152
+ end
153
+
154
+ def rvm_shell_out!(*args)
155
+ cmd = rvm_shell_out(*args)
156
+ cmd.error!
157
+ cmd
158
+ end
159
+
160
+ def rvm(subcommand)
161
+ rvm_shell_out(%{#{rvm_path}/bin/rvm #{subcommand}})
162
+ end
163
+
164
+ def rvm_path
165
+ if new_resource.user == "root"
166
+ "/usr/local/rvm"
167
+ else
168
+ ::File.join(etc_user.dir, ".rvm")
169
+ end
170
+ end
171
+
172
+ def rvmrc_path
173
+ if new_resource.user == "root"
174
+ "/etc/rvmrc"
175
+ else
176
+ ::File.join(etc_user.dir, ".rvmrc")
177
+ end
178
+ end
179
+
180
+ def etc_user
181
+ Etc.getpwnam(new_resource.user)
182
+ end
183
+ end
184
+ end
185
+ end