├── .chefignore ├── .gitignore ├── .jenkins ├── .kitchen.yml ├── .ruby-version ├── Berksfile ├── Gemfile ├── LICENSE ├── README.md ├── Thorfile ├── Vagrantfile ├── attributes └── default.rb ├── chefignore ├── libraries ├── chef_mixin_rbenv.rb ├── chef_mixin_ruby_build.rb ├── matchers.rb ├── provider_rbenv_rubygems.rb ├── recipe_ext.rb └── resource_ext.rb ├── metadata.rb ├── providers ├── execute.rb └── ruby.rb ├── recipes ├── default.rb ├── ohai_plugin.rb ├── rbenv_vars.rb └── ruby_build.rb ├── resources ├── execute.rb ├── gem.rb └── ruby.rb ├── templates └── default │ ├── plugins │ └── rbenv.rb.erb │ └── rbenv.sh.erb └── test ├── fixtures └── cookbooks │ └── fake │ ├── attributes │ └── default.rb │ ├── metadata.rb │ └── recipes │ ├── gem_install.rb │ └── ruby_install.rb └── integration ├── gem_install └── bats │ └── gem_install.bats └── ruby_install └── bats └── ruby_install.bats /.chefignore: -------------------------------------------------------------------------------- 1 | # Put files/directories that should be ignored in this file. 2 | # Lines that start with '# ' are comments. 3 | 4 | ## OS 5 | .DS_Store 6 | Icon? 7 | nohup.out 8 | 9 | ## EDITORS 10 | \#* 11 | .#* 12 | *~ 13 | *.sw[a-z] 14 | *.bak 15 | REVISION 16 | TAGS* 17 | tmtags 18 | *_flymake.* 19 | *_flymake 20 | *.tmproj 21 | .project 22 | .settings 23 | mkmf.log 24 | 25 | ## COMPILED 26 | a.out 27 | *.o 28 | *.pyc 29 | *.so 30 | 31 | ## OTHER SCM 32 | */.bzr/* 33 | */.hg/* 34 | */.svn/* 35 | 36 | ## Don't send rspecs up in cookbook 37 | .watchr 38 | .rspec 39 | spec/* 40 | spec/fixtures/* 41 | features/* 42 | 43 | ## SCM 44 | .gitignore 45 | 46 | # Berkshelf 47 | Berksfile 48 | Berksfile.lock 49 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vagrant 2 | Berksfile.lock 3 | Gemfile.lock 4 | *~ 5 | *# 6 | .#* 7 | \#*# 8 | .*.sw[a-z] 9 | *.un~ 10 | /cookbooks 11 | .kitchen 12 | -------------------------------------------------------------------------------- /.jenkins: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | bash -lc 'gem install bundler --no-rdoc --no-ri; bundle install' 3 | -------------------------------------------------------------------------------- /.kitchen.yml: -------------------------------------------------------------------------------- 1 | --- 2 | driver_plugin: vagrant 3 | driver_config: 4 | require_chef_omnibus: true 5 | 6 | platforms: 7 | - name: debian-7.1.0 8 | - name: debian-6 9 | driver_config: 10 | box: opscode-debian-6.0.7 11 | box_url: http://opscode-vm.s3.amazonaws.com/vagrant/opscode_debian-6.0.7_chef-11.2.0.box 12 | - name: ubuntu-13.04 13 | - name: ubuntu-12.04 14 | - name: ubuntu-10.04 15 | - name: centos-6.4 16 | - name: centos-5.9 17 | suites: 18 | - name: ruby_install 19 | run_list: 20 | - recipe[fake::ruby_install] 21 | - name: gem_install 22 | run_list: 23 | - recipe[fake::gem_install] 24 | -------------------------------------------------------------------------------- /.ruby-version: -------------------------------------------------------------------------------- 1 | 1.9.3-p448 2 | -------------------------------------------------------------------------------- /Berksfile: -------------------------------------------------------------------------------- 1 | site :opscode 2 | 3 | metadata 4 | 5 | group :test do 6 | cookbook 'fake', :path => 'test/fixtures/cookbooks/fake' 7 | end 8 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gem 'berkshelf', '>= 3.0.0.beta4' 4 | gem 'thor-foodcritic' 5 | 6 | group :development do 7 | gem 'thor' 8 | end 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2012, Riot Games 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rbenv cookbook 2 | 3 | Installs and manages your versions of Ruby and Gems in Chef with rbenv and ruby_build 4 | 5 | * [rbenv](https://github.com/sstephenson/rbenv) 6 | * [ruby_build](https://github.com/sstephenson/ruby-build) 7 | 8 | # Requirements 9 | 10 | * Chef 10 11 | * Centos / Redhat / Fedora / Ubuntu / Debian 12 | * Ruby >= 1.9 13 | 14 | # Usage 15 | 16 | Add a dependency on rbenv to your cookbook's `metadata.rb` 17 | 18 | depends 'rbenv' 19 | 20 | ## Installing rbenv and ruby_build 21 | 22 | To install rbenv and ruby_build; Include each recipe in one of your cookbook's recipes 23 | 24 | include_recipe "rbenv::default" 25 | include_recipe "rbenv::ruby_build" 26 | 27 | ## Installing rbenv-vars 28 | 29 | To install rbenv-vars; Include this recipe in one of your cookbook's recipes 30 | 31 | include_recipe "rbenv::rbenv_vars" 32 | 33 | ## Installing a Ruby 34 | 35 | And now to install a Ruby use the `rbenv_ruby` LWRP 36 | 37 | rbenv_ruby "1.9.3-p194" 38 | 39 | ## Installing Gems for rbenv managed Rubies 40 | 41 | If you'd like a specific Ruby installed by rbenv to include a Gem, say bundler, use the `rbenv_gem` LWRP 42 | 43 | rbenv_gem "bundler" do 44 | ruby_version "1.9.3-p194" 45 | end 46 | 47 | Be sure to include a value for the `ruby_version` attribute so the gem is installed for the correct Ruby 48 | 49 | # Attributes 50 | 51 | ## rbenv 52 | 53 | * `rbenv[:group_users]` - Array of users belonging to the rbenv group 54 | * `rbenv[:git_repository]` - Git url of the rbenv repository to clone 55 | * `rbenv[:git_revision]` - Revision of the rbenv repository to checkout 56 | * `rbenv[:install_prefix]` - Path prefix rbenv will be installed into 57 | 58 | ## ruby_build 59 | 60 | * `ruby_build[:git_repository]` - Git url of the ruby_build repository to clone 61 | * `ruby_build[:git_revision]` - Revision of the ruby_build repository to checkout 62 | * `ruby_build[:prefix]` - Path prefix where ruby_build will be installed to 63 | 64 | # Recipes 65 | 66 | ## default 67 | 68 | Configures a node with a system wide rbenv accessible by users in the rbenv group 69 | 70 | ## ruby_build 71 | 72 | Installs ruby_build to a node which enables the `rbenv_ruby` LWRP to install Rubies to the node 73 | 74 | ## ohai_plugin 75 | 76 | Installs an rbenv Ohai plugin onto the node to automatically populate attributes about the rbenv installation 77 | 78 | # Resources / Providers 79 | 80 | ## rbenv_ruby 81 | 82 | Install specified version of Ruby to be managed by rbenv 83 | 84 | ### Actions 85 | Action | Description | Default 86 | ------- |------------- |--------- 87 | install | Install the version of Ruby | Yes 88 | 89 | ### Attributes 90 | Attribute | Description | Default 91 | ------- |------------- |--------- 92 | ruby_version | the ruby version and patch level you wish to install | name 93 | force | install even if this version is already present (reinstall) | false 94 | global | set this ruby version as the global version | false 95 | patch | a url for `rbenv install --patch` to pull in | 96 | 97 | Using the `patch` attribute requires that patchutils is installed, so that it can use filterdiff to remove parts of the patch (the Changelog which usually causes merge conflicts). 98 | 99 | ### Examples 100 | 101 | ##### Installing Ruby 1.9.2-p290 102 | 103 | rbenv_ruby "1.9.2-p290" 104 | 105 | ##### Forcefully install Ruby 1.9.3-p0 106 | 107 | rbenv_ruby "Ruby 1.9.3" do 108 | ruby_version "1.9.3-p0" 109 | force true 110 | end 111 | 112 | ##### Install Ruby 2.1.1 with a patch for ubuntu 14.04 support 113 | 114 | rbenv_ruby "2.1.1" do 115 | global true 116 | patch "https://bugs.ruby-lang.org/projects/ruby-trunk/repository/revisions/45225/diff?format=diff" 117 | end 118 | 119 | 120 | ## rbenv_gem 121 | 122 | Install specified RubyGem for the specified ruby_version managed by rbenv 123 | 124 | ### Actions 125 | 126 | Action | Description | Default 127 | ------- |------------- |--------- 128 | install | Install the gem | Yes 129 | upgrade | Upgrade the gem to the given version | 130 | remove | Remove the gem | 131 | purge | Purge the gem and configuration files | 132 | 133 | ### Attributes 134 | 135 | Attribute | Description | Default 136 | ------- |------------- |--------- 137 | package_name | Name of given to modify | name 138 | ruby_version | Ruby of version the gem belongs to | 139 | version | Version of the gem to modify | 140 | source | Specified if you have a local .gem file to install | 141 | gem_binary | Override for path to gem command | 142 | response_file | | 143 | options | Additional options to the underlying gem command | 144 | 145 | ### Examples 146 | 147 | ##### Installing Bundler for Ruby 1.9.2-p290 148 | 149 | rbenv_gem "bundler" do 150 | ruby_version "1.9.2-p290" 151 | end 152 | 153 | ## rbenv_execute 154 | 155 | Safely execute shell commands with RBENV and a particular Ruby activated 156 | 157 | ### Actions 158 | 159 | Action | Description | Default 160 | -------|------------- |--------- 161 | run | Run the command | Yes 162 | 163 | ### Attributes 164 | 165 | Attribute | Description 166 | ---------- |------------ 167 | command | The name of the command to be executed 168 | creates | Indicates that a command to create a file will not be run when that file already exists 169 | cwd | The current working directory from which a command is run 170 | environment | A hash of environment variables. These will be automatically merged with the required RBENV environment variables 171 | group | The group name or group ID that must be changed before running a command 172 | path | An array of paths to use when searching for a command. These paths will be added to the command's environment `$PATH` and the required RBENV path variables 173 | returns | The return value for a command. This may be an array of accepted values. An exception is raised when the return value(s) do not match 174 | ruby_version | The version of Ruby to activate when running the command 175 | timeout | The amount of time (in seconds) a command will wait before timing out 176 | user | The user name or user ID that should be changed before running a command 177 | umask | The file mode creation mask, or umask 178 | 179 | # Releasing 180 | 181 | 1. Install the prerequisite gems 182 | 183 | $ bundle install 184 | 185 | 2. Increment the version number in the metadata.rb file 186 | 187 | 3. Run the Thor release task to create a tag and push to the community site 188 | 189 | $ bundle exec thor release 190 | 191 | # Authors and Contributors 192 | 193 | * Jamie Winsor () 194 | -------------------------------------------------------------------------------- /Thorfile: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | 3 | require 'thor/foodcritic' 4 | require 'berkshelf/thor' 5 | 6 | class Default < Thor 7 | attr_reader :cookbook_name 8 | attr_reader :cookbook_category 9 | 10 | def initialize(*args) 11 | @cookbook_name = "rbenv" 12 | @cookbook_category = "Programming Languages" 13 | super(*args) 14 | end 15 | 16 | class_option :verbose, 17 | :type => :boolean, 18 | :aliases => "-v", 19 | :default => false 20 | 21 | method_option :knife_config, 22 | :type => :string, 23 | :aliases => "-c", 24 | :desc => "Path to your knife configuration file", 25 | :default => "~/.chef/knife.rb" 26 | desc "release", "Create a tag from the version specific in the metadata.rb and push to the community site" 27 | def release 28 | unless clean? 29 | say "There are files that need to be committed first.", :red 30 | exit 1 31 | end 32 | 33 | invoke :'foodcritic:lint' 34 | 35 | tag_version { publish_cookbook(options) } 36 | end 37 | 38 | private 39 | 40 | def clean? 41 | sh_with_excode("git diff --exit-code")[1] == 0 42 | end 43 | 44 | def current_version 45 | Berkshelf::CachedCookbook.from_path(source_root).version 46 | end 47 | 48 | def publish_cookbook(options) 49 | cmd = "knife cookbook site share #{cookbook_name} \"#{cookbook_category}\" -o #{source_root.join("..")} -c #{options[:knife_config]}" 50 | cmd << " -V" if options[:verbose] 51 | sh cmd 52 | end 53 | 54 | def tag_version 55 | sh "git tag -a -m \"Version #{current_version}\" #{current_version}" 56 | say "Tagged: #{current_version}", :green 57 | yield if block_given? 58 | sh "git push --tags" 59 | rescue => e 60 | say "Untagging: #{current_version} due to error", :red 61 | sh_with_excode "git tag -d #{current_version}" 62 | say e, :red 63 | exit 1 64 | end 65 | 66 | def source_root 67 | Pathname.new File.dirname(File.expand_path(__FILE__)) 68 | end 69 | 70 | def sh(cmd, dir = source_root, &block) 71 | out, code = sh_with_excode(cmd, dir, &block) 72 | code == 0 ? out : raise(out.empty? ? "Running `#{cmd}` failed. Run this command directly for more detailed output." : out) 73 | end 74 | 75 | def sh_with_excode(cmd, dir = source_root, &block) 76 | cmd << " 2>&1" 77 | outbuf = '' 78 | 79 | Dir.chdir(dir) { 80 | outbuf = `#{cmd}` 81 | if $? == 0 82 | block.call(outbuf) if block 83 | end 84 | } 85 | 86 | [ outbuf, $? ] 87 | end 88 | end 89 | 90 | -------------------------------------------------------------------------------- /Vagrantfile: -------------------------------------------------------------------------------- 1 | Vagrant.configure("2") do |config| 2 | config.vm.hostname = "rbenv-berkshelf" 3 | config.vm.box = "Berkshelf-CentOS-6.3-x86_64-minimal" 4 | config.vm.box_url = "https://dl.dropbox.com/u/31081437/Berkshelf-CentOS-6.3-x86_64-minimal.box" 5 | 6 | config.vm.network :private_network, ip: "192.168.33.10" 7 | config.vm.network :forwarded_port, guest: 8081, host: 8081 8 | config.vm.network :forwarded_port, guest: 8443, host: 8443 9 | 10 | config.ssh.max_tries = 40 11 | config.ssh.timeout = 120 12 | 13 | config.vm.provision :chef_solo do |chef| 14 | chef.json = { 15 | :rbenv => { 16 | :group_users => ["vagrant"] 17 | } 18 | } 19 | 20 | chef.run_list = [ 21 | "recipe[rbenv::default]", 22 | "recipe[rbenv::ruby_build]" 23 | ] 24 | end 25 | end 26 | -------------------------------------------------------------------------------- /attributes/default.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook Name:: rbenv 3 | # Attributes:: default 4 | # 5 | # Author:: Jamie Winsor () 6 | # 7 | # Copyright 2011-2012, Riot Games 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 | default[:rbenv][:user] = "rbenv" 23 | default[:rbenv][:group] = "rbenv" 24 | default[:rbenv][:manage_home] = true 25 | default[:rbenv][:group_users] = Array.new 26 | default[:rbenv][:git_repository] = "https://github.com/sstephenson/rbenv.git" 27 | default[:rbenv][:git_revision] = "master" 28 | default[:rbenv][:install_prefix] = "/opt" 29 | default[:rbenv][:root_path] = "#{node[:rbenv][:install_prefix]}/rbenv" 30 | default[:rbenv][:user_home] = "/home/#{node[:rbenv][:user]}" 31 | 32 | default[:ruby_build][:git_repository] = "https://github.com/sstephenson/ruby-build.git" 33 | default[:ruby_build][:git_revision] = "master" 34 | 35 | default[:rbenv_vars][:git_repository] = "https://github.com/sstephenson/rbenv-vars.git" 36 | default[:rbenv_vars][:git_revision] = "master" 37 | -------------------------------------------------------------------------------- /chefignore: -------------------------------------------------------------------------------- 1 | # Put files/directories that should be ignored in this file. 2 | # Lines that start with '# ' are comments. 3 | 4 | ## OS 5 | .DS_Store 6 | Icon? 7 | nohup.out 8 | 9 | ## EDITORS 10 | \#* 11 | .#* 12 | *~ 13 | *.sw[a-z] 14 | *.bak 15 | REVISION 16 | TAGS* 17 | tmtags 18 | *_flymake.* 19 | *_flymake 20 | *.tmproj 21 | .project 22 | .settings 23 | mkmf.log 24 | 25 | ## COMPILED 26 | a.out 27 | *.o 28 | *.pyc 29 | *.so 30 | 31 | ## OTHER SCM 32 | */.bzr/* 33 | */.hg/* 34 | */.svn/* 35 | 36 | ## Don't send rspecs up in cookbook 37 | .watchr 38 | .rspec 39 | spec/* 40 | spec/fixtures/* 41 | features/* 42 | 43 | ## SCM 44 | .gitignore 45 | 46 | # Berkshelf 47 | Berksfile 48 | Berksfile.lock 49 | cookbooks/* 50 | 51 | # Vagrant 52 | .vagrant 53 | -------------------------------------------------------------------------------- /libraries/chef_mixin_rbenv.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook Name:: rbenv 3 | # Library:: mixin_rbenv 4 | # 5 | # Author:: Jamie Winsor () 6 | # 7 | # Copyright 2011-2012, Riot Games 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/shell_out' 23 | 24 | class Chef 25 | module Mixin 26 | module Rbenv 27 | include Chef::Mixin::ShellOut 28 | 29 | def rbenv_command(cmd, options = {}) 30 | unless rbenv_installed? 31 | Chef::Log.error("rbenv is not yet installed. Unable to run " + 32 | "rbenv_command:`#{cmd}`. Are you trying to use " + 33 | "`rbenv_command` at the top level of your recipe? " + 34 | "This is known to cause this error") 35 | raise "rbenv not installed. Can't run rbenv_command" 36 | end 37 | 38 | default_options = { 39 | :user => node[:rbenv][:user], 40 | :group => node[:rbenv][:group], 41 | :cwd => rbenv_root_path, 42 | :env => { 43 | 'RBENV_ROOT' => rbenv_root_path 44 | }, 45 | :timeout => 3600 46 | } 47 | if patch = options.delete(:patch) 48 | Chef::Log.info("Patch found at: #{patch}") 49 | unless filterdiff_installed? 50 | Chef::Log.error("Cannot find filterdiff. Please install patchutils to be able to use patches.") 51 | raise "Cannot find filterdiff. Please install patchutils to be able to use patches." 52 | end 53 | shell_out("curl -fsSL #{patch} | filterdiff -x ChangeLog | #{rbenv_bin_path}/rbenv #{cmd}", Chef::Mixin::DeepMerge.deep_merge!(options, default_options)) 54 | else 55 | shell_out("#{rbenv_bin_path}/rbenv #{cmd}", Chef::Mixin::DeepMerge.deep_merge!(options, default_options)) 56 | end 57 | end 58 | 59 | def rbenv_installed? 60 | out = shell_out("ls #{rbenv_bin_path}/rbenv") 61 | out.exitstatus == 0 62 | end 63 | 64 | def ruby_version_installed?(version) 65 | out = rbenv_command("prefix", :env => { 'RBENV_VERSION' => version }) 66 | out.exitstatus == 0 67 | end 68 | 69 | def filterdiff_installed? 70 | out = shell_out("which filterdiff") 71 | out.exitstatus == 0 72 | end 73 | 74 | def rbenv_global_version?(version) 75 | out = rbenv_command("global") 76 | unless out.exitstatus == 0 77 | raise Chef::Exceptions::ShellCommandFailed, "\n" + out.format_for_exception 78 | end 79 | 80 | global_version = out.stdout.chomp 81 | global_version == version 82 | end 83 | 84 | def gem_binary_path_for(version) 85 | "#{rbenv_prefix_for(version)}/bin/gem" 86 | end 87 | 88 | def rbenv_prefix_for(version) 89 | out = rbenv_command("prefix", :env => { 'RBENV_VERSION' => version }) 90 | 91 | unless out.exitstatus == 0 92 | raise Chef::Exceptions::ShellCommandFailed, "\n" + out.format_for_exception 93 | end 94 | 95 | prefix = out.stdout.chomp 96 | end 97 | 98 | def rbenv_bin_path 99 | ::File.join(rbenv_root_path, "bin") 100 | end 101 | 102 | def rbenv_shims_path 103 | ::File.join(rbenv_root_path, "shims") 104 | end 105 | 106 | def rbenv_root_path 107 | node[:rbenv][:root_path] 108 | end 109 | 110 | # Ensures $HOME is temporarily set to the given user. The original 111 | # $HOME is preserved and re-set after the block has been yielded 112 | # to. 113 | # 114 | # This is a workaround for CHEF-3940. TL;DR Certain versions of 115 | # `git` misbehave if configuration is inaccessible in $HOME. 116 | # 117 | # More info here: 118 | # 119 | # https://github.com/git/git/commit/4698c8feb1bb56497215e0c10003dd046df352fa 120 | # 121 | def with_home_for_user(username, &block) 122 | 123 | time = Time.now.to_i 124 | 125 | ruby_block "set HOME for #{username} at #{time}" do 126 | block do 127 | ENV['OLD_HOME'] = ENV['HOME'] 128 | ENV['HOME'] = begin 129 | require 'etc' 130 | Etc.getpwnam(username).dir 131 | rescue ArgumentError # user not found 132 | "/home/#{username}" 133 | end 134 | end 135 | end 136 | 137 | yield 138 | 139 | ruby_block "unset HOME for #{username} #{time}" do 140 | block do 141 | ENV['HOME'] = ENV['OLD_HOME'] 142 | end 143 | end 144 | end 145 | end 146 | end 147 | end 148 | -------------------------------------------------------------------------------- /libraries/chef_mixin_ruby_build.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook Name:: rbenv 3 | # Library:: mixin_ruby_build 4 | # 5 | # Author:: Jamie Winsor () 6 | # 7 | # Copyright 2011-2012, Riot Games 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/shell_out' 23 | 24 | class Chef 25 | module Mixin 26 | module RubyBuild 27 | def ruby_build_binary_path 28 | "#{node[:ruby_build][:bin_path]}/ruby-build" 29 | end 30 | 31 | def ruby_build_installed_version 32 | out = shell_out("#{ruby_build_binary_path} --version", :env => nil) 33 | out.stdout.chomp 34 | end 35 | end 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /libraries/matchers.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook Name:: rbenv 3 | # Library:: matchers 4 | # 5 | # Author:: Kai Forsthoevel () 6 | # 7 | # Copyright 2014, injixo 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 | if defined?(ChefSpec) 23 | def install_rbenv_ruby(ruby_version) 24 | ChefSpec::Matchers::ResourceMatcher.new(:rbenv_ruby, :install, ruby_version) 25 | end 26 | 27 | def install_rbenv_gem(package_name) 28 | ChefSpec::Matchers::ResourceMatcher.new(:rbenv_gem, :install, package_name) 29 | end 30 | 31 | def upgrade_rbenv_gem(package_name) 32 | ChefSpec::Matchers::ResourceMatcher.new(:rbenv_gem, :upgrade, package_name) 33 | end 34 | 35 | def remove_rbenv_gem(package_name) 36 | ChefSpec::Matchers::ResourceMatcher.new(:rbenv_gem, :remove, package_name) 37 | end 38 | 39 | def purge_rbenv_gem(package_name) 40 | ChefSpec::Matchers::ResourceMatcher.new(:rbenv_gem, :purge, package_name) 41 | end 42 | 43 | def run_rbenv_execute(command) 44 | ChefSpec::Matchers::ResourceMatcher.new(:rbenv_execute, :run, command) 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /libraries/provider_rbenv_rubygems.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook Name:: rbenv 3 | # Library:: provider_rbenv_rubygems 4 | # 5 | # Author:: Jamie Winsor () 6 | # 7 | # Copyright 2011-2012, Riot Games 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_relative 'chef_mixin_rbenv' 23 | 24 | class Chef 25 | class Provider 26 | class Package 27 | class RbenvRubygems < Chef::Provider::Package::Rubygems 28 | include Chef::Mixin::Rbenv 29 | 30 | class RbenvGemEnvironment < AlternateGemEnvironment 31 | attr_reader :ruby_version 32 | attr_reader :rbenv_root_path 33 | 34 | alias_method :original_shell_out!, :shell_out! 35 | 36 | include Chef::Mixin::Rbenv 37 | 38 | def initialize(gem_binary_path, ruby_version, rbenv_root) 39 | @ruby_version = ruby_version 40 | @rbenv_root_path = rbenv_root 41 | super(gem_binary_path) 42 | end 43 | 44 | def shell_out!(*args) 45 | options = args.last.is_a?(Hash) ? args.pop : Hash.new 46 | options.merge!(env: { 47 | "RBENV_ROOT" => rbenv_root_path, 48 | "RBENV_VERSION" => ruby_version, 49 | "PATH" => ([ rbenv_shims_path, rbenv_bin_path ] + system_path).join(':') 50 | }) 51 | original_shell_out!(*args, options) 52 | end 53 | 54 | private 55 | 56 | def system_path 57 | original_shell_out!("echo $PATH").stdout.chomp.split(':') 58 | end 59 | end 60 | 61 | attr_reader :gem_binary_path 62 | 63 | def initialize(new_resource, run_context = nil) 64 | super 65 | @gem_binary_path = gem_binary_path_for(new_resource.ruby_version) 66 | @rbenv_root = node[:rbenv][:root_path] 67 | @gem_env = RbenvGemEnvironment.new(gem_binary_path, new_resource.ruby_version, @rbenv_root) 68 | end 69 | 70 | def install_package(name, version) 71 | install_via_gem_command(name, version) 72 | rbenv_command("rehash") 73 | 74 | true 75 | end 76 | 77 | def remove_package(name, version) 78 | uninstall_via_gem_command(name, version) 79 | 80 | true 81 | end 82 | 83 | def install_via_gem_command(name, version = nil) 84 | src = @new_resource.source && " --source=#{@new_resource.source} --source=http://rubygems.org" 85 | version_option = (version.nil? || version.empty?) ? "" : " -v \"#{version}\"" 86 | 87 | shell_out!( 88 | "#{gem_binary_path} install #{name} -q --no-rdoc --no-ri #{version_option} #{src}#{opts}", 89 | :user => node[:rbenv][:user], 90 | :group => node[:rbenv][:group], 91 | :env => { 92 | 'RBENV_VERSION' => @new_resource.ruby_version, 93 | 'RBENV_ROOT' => @rbenv_root 94 | } 95 | ) 96 | end 97 | end 98 | end 99 | end 100 | end 101 | -------------------------------------------------------------------------------- /libraries/recipe_ext.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook Name:: rbenv 3 | # Library:: recipe_ext 4 | # 5 | # Author:: Jamie Winsor () 6 | # 7 | # Copyright 2011-2012, Riot Games 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 Mixin 24 | module Rbenv 25 | # stub to satisfy RecipeExt (library load order not guaranteed) 26 | end 27 | end 28 | 29 | module Rbenv 30 | module RecipeExt 31 | include Chef::Mixin::Rbenv 32 | end 33 | end 34 | end 35 | 36 | Chef::Recipe.send(:include, Chef::Rbenv::RecipeExt) 37 | -------------------------------------------------------------------------------- /libraries/resource_ext.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook Name:: rbenv 3 | # Library:: resource_ext 4 | # 5 | # Author:: Jamie Winsor () 6 | # 7 | # Copyright 2011-2012, Riot Games 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/shell_out' 23 | 24 | class Chef 25 | module Mixin 26 | module Rbenv 27 | # stub to satisfy ResourceExt (library load order not guaranteed) 28 | end 29 | 30 | module RubyBuild 31 | # stub to satisfy ResourceExt (library load order not guaranteed) 32 | end 33 | end 34 | 35 | module Rbenv 36 | module ResourceExt 37 | include Chef::Mixin::Rbenv 38 | include Chef::Mixin::RubyBuild 39 | include Chef::Mixin::ShellOut 40 | 41 | def desired_ruby_build_version? 42 | if File.exists?(ruby_build_binary_path) 43 | ruby_build_installed_version.match(/#{node[:ruby_build][:version]}$/).nil? ? false : true 44 | else 45 | false 46 | end 47 | end 48 | end 49 | end 50 | end 51 | 52 | Chef::Resource::Bash.send(:include, Chef::Rbenv::ResourceExt) 53 | -------------------------------------------------------------------------------- /metadata.rb: -------------------------------------------------------------------------------- 1 | name "rbenv" 2 | maintainer "Riot Games" 3 | maintainer_email "jamie@vialstudios.com" 4 | license "Apache 2.0" 5 | description "Installs and configures rbenv" 6 | version "1.7.1" 7 | 8 | recipe "rbenv", "Installs and configures rbenv" 9 | recipe "rbenv::ruby_build", "Installs and configures ruby_build" 10 | recipe "rbenv::ohai_plugin", "Installs an rbenv Ohai plugin to populate automatic_attrs about rbenv and ruby_build" 11 | recipe "rbenv::rbenv_vars", "Installs an rbenv plugin rbenv-vars that lets you set global and project-specific environment variables before spawning Ruby processes" 12 | 13 | %w{ centos redhat fedora ubuntu debian amazon oracle}.each do |os| 14 | supports os 15 | end 16 | 17 | %w{ git build-essential apt }.each do |cb| 18 | depends cb 19 | end 20 | 21 | depends 'ohai', '>= 1.1' 22 | -------------------------------------------------------------------------------- /providers/execute.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook Name:: rbenv 3 | # Provider:: execute 4 | # 5 | # Author:: Jamie Winsor () 6 | # 7 | 8 | include Chef::Mixin::Rbenv 9 | 10 | def load_current_resource 11 | @path = [ rbenv_shims_path, rbenv_bin_path ] + new_resource.path + system_path 12 | @environment = new_resource.environment 13 | @environment["PATH"] = @path.join(":") 14 | @environment["RBENV_ROOT"] = rbenv_root_path 15 | @environment["RBENV_VERSION"] = new_resource.ruby_version if new_resource.ruby_version 16 | 17 | new_resource.environment(@environment) 18 | end 19 | 20 | action :run do 21 | execute "eval \"$(rbenv init -)\"" do 22 | environment new_resource.environment 23 | end 24 | 25 | execute new_resource.name do 26 | command new_resource.command 27 | creates new_resource.creates 28 | cwd new_resource.cwd 29 | environment new_resource.environment 30 | group new_resource.group 31 | returns new_resource.returns 32 | timeout new_resource.timeout 33 | user new_resource.user 34 | umask new_resource.umask 35 | end 36 | 37 | new_resource.updated_by_last_action(true) 38 | end 39 | 40 | private 41 | 42 | def system_path 43 | shell_out!("echo $PATH").stdout.chomp.split(':') 44 | end 45 | -------------------------------------------------------------------------------- /providers/ruby.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook Name:: rbenv 3 | # Provider:: ruby 4 | # 5 | # Author:: Jamie Winsor () 6 | # 7 | # Copyright 2011-2012, Riot Games 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 | include Chef::Mixin::Rbenv 23 | 24 | action :install do 25 | resource_descriptor = "rbenv_ruby[#{new_resource.name}] (version #{new_resource.ruby_version})" 26 | if !new_resource.force && ruby_version_installed?(new_resource.ruby_version) 27 | Chef::Log.debug "#{resource_descriptor} is already installed so skipping" 28 | else 29 | Chef::Log.info "#{resource_descriptor} is building, this may take a while..." 30 | 31 | start_time = Time.now 32 | out = new_resource.patch ? 33 | rbenv_command("install --patch #{new_resource.ruby_version}", patch: new_resource.patch) : 34 | rbenv_command("install #{new_resource.ruby_version}") 35 | 36 | unless out.exitstatus == 0 37 | raise Chef::Exceptions::ShellCommandFailed, "\n" + out.format_for_exception 38 | end 39 | 40 | Chef::Log.debug("#{resource_descriptor} build time was #{(Time.now - start_time)/60.0} minutes.") 41 | 42 | chmod_options = { 43 | user: node[:rbenv][:user], 44 | group: node[:rbenv][:group], 45 | cwd: rbenv_root_path 46 | } 47 | 48 | unless Chef::Platform.windows? 49 | shell_out("chmod -R 0775 versions/#{new_resource.ruby_version}", chmod_options) 50 | shell_out("find versions/#{new_resource.ruby_version} -type d -exec chmod +s {} \\;", chmod_options) 51 | end 52 | 53 | new_resource.updated_by_last_action(true) 54 | end 55 | 56 | if new_resource.global && !rbenv_global_version?(new_resource.name) 57 | Chef::Log.info "Setting #{resource_descriptor} as the rbenv global version" 58 | out = rbenv_command("global #{new_resource.ruby_version}") 59 | unless out.exitstatus == 0 60 | raise Chef::Exceptions::ShellCommandFailed, "\n" + out.format_for_exception 61 | end 62 | new_resource.updated_by_last_action(true) 63 | end 64 | end 65 | -------------------------------------------------------------------------------- /recipes/default.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook Name:: rbenv 3 | # Recipe:: default 4 | # 5 | # Author:: Jamie Winsor () 6 | # 7 | # Copyright 2011-2012, Riot Games 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 | node.set[:rbenv][:root] = rbenv_root_path 23 | node.set[:ruby_build][:prefix] = "#{node[:rbenv][:root]}/plugins/ruby_build" 24 | node.set[:ruby_build][:bin_path] = "#{node[:ruby_build][:prefix]}/bin" 25 | 26 | case node[:platform] 27 | when "ubuntu", "debian" 28 | include_recipe "apt" 29 | end 30 | 31 | include_recipe "build-essential" 32 | include_recipe "git" 33 | package "curl" 34 | 35 | case node[:platform] 36 | when "redhat", "centos", "amazon", "oracle" 37 | # TODO: add as per "rvm requirements" 38 | package "openssl-devel" 39 | package "zlib-devel" 40 | package "readline-devel" 41 | package "libxml2-devel" 42 | package "libxslt-devel" 43 | when "ubuntu", "debian" 44 | package "libc6-dev" 45 | package "automake" 46 | package "libtool" 47 | 48 | # https://github.com/sstephenson/ruby-build/issues/119 49 | # "It seems your ruby installation is missing psych (for YAML 50 | # output). To eliminate this warning, please install libyaml and 51 | # reinstall your ruby." 52 | package 'libyaml-dev' 53 | 54 | # needed to unpack rubygems 55 | package 'zlib1g' 56 | package 'zlib1g-dev' 57 | 58 | # openssl support for ruby 59 | package "openssl" 60 | package 'libssl-dev' 61 | 62 | # readline for irb and rails console 63 | package "libreadline-dev" 64 | 65 | # for ruby stdlib rexml and nokogiri 66 | # http://nokogiri.org/tutorials/installing_nokogiri.html 67 | package "libxml2-dev" 68 | package "libxslt1-dev" 69 | 70 | # better irb support 71 | package "ncurses-dev" 72 | 73 | # for searching packages 74 | package "pkg-config" 75 | end 76 | 77 | group node[:rbenv][:group] do 78 | members node[:rbenv][:group_users] if node[:rbenv][:group_users] 79 | end 80 | 81 | user node[:rbenv][:user] do 82 | shell "/bin/bash" 83 | group node[:rbenv][:group] 84 | supports :manage_home => node[:rbenv][:manage_home] 85 | home node[:rbenv][:user_home] 86 | end 87 | 88 | directory node[:rbenv][:root] do 89 | owner node[:rbenv][:user] 90 | group node[:rbenv][:group] 91 | mode "2775" 92 | recursive true 93 | end 94 | 95 | with_home_for_user(node[:rbenv][:user]) do 96 | 97 | git node[:rbenv][:root] do 98 | repository node[:rbenv][:git_repository] 99 | reference node[:rbenv][:git_revision] 100 | user node[:rbenv][:user] 101 | group node[:rbenv][:group] 102 | action :sync 103 | 104 | notifies :create, "template[/etc/profile.d/rbenv.sh]", :immediately 105 | end 106 | 107 | end 108 | 109 | template "/etc/profile.d/rbenv.sh" do 110 | source "rbenv.sh.erb" 111 | mode "0644" 112 | variables( 113 | :rbenv_root => node[:rbenv][:root], 114 | :ruby_build_bin_path => node[:ruby_build][:bin_path] 115 | ) 116 | 117 | notifies :create, "ruby_block[initialize_rbenv]", :immediately 118 | end 119 | 120 | ruby_block "initialize_rbenv" do 121 | block do 122 | ENV['RBENV_ROOT'] = node[:rbenv][:root] 123 | ENV['PATH'] = "#{node[:rbenv][:root]}/bin:#{node[:rbenv][:root]}/shims:#{node[:ruby_build][:bin_path]}:#{ENV['PATH']}" 124 | end 125 | 126 | action :nothing 127 | end 128 | 129 | # rbenv init creates these directories as root because it is called 130 | # from /etc/profile.d/rbenv.sh But we want them to be owned by rbenv 131 | # check https://github.com/sstephenson/rbenv/blob/master/libexec/rbenv-init#L71 132 | %w{shims versions plugins}.each do |dir_name| 133 | directory "#{node[:rbenv][:root]}/#{dir_name}" do 134 | owner node[:rbenv][:user] 135 | group node[:rbenv][:group] 136 | mode "2775" 137 | action [:create] 138 | end 139 | end 140 | -------------------------------------------------------------------------------- /recipes/ohai_plugin.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook Name:: rbenv 3 | # Recipe:: ohai_plugin 4 | # 5 | # Author:: Jamie Winsor () 6 | # 7 | # Copyright 2011-2012, Riot Games 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 | include_recipe "ohai" 23 | 24 | bin_path = ::File.join(rbenv_bin_path, "rbenv") 25 | 26 | template "#{node[:ohai][:plugin_path]}/rbenv.rb" do 27 | source 'plugins/rbenv.rb.erb' 28 | owner 'root' 29 | group 'root' 30 | mode 0755 31 | 32 | variables( 33 | :rbenv_bin => bin_path 34 | ) 35 | 36 | notifies :reload, "ohai[custom_plugins]", :immediately 37 | end 38 | -------------------------------------------------------------------------------- /recipes/rbenv_vars.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook Name:: rbenv 3 | # Recipe:: rbenv_vars 4 | # 5 | # Author:: Deepak Kannan () 6 | # 7 | # Copyright 2011-2012, Riot Games 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 | include_recipe "git" 23 | 24 | plugin_path = "#{node[:rbenv][:root]}/plugins/rbenv-vars" 25 | 26 | with_home_for_user(node[:rbenv][:user]) do 27 | 28 | git plugin_path do 29 | repository node[:rbenv_vars][:git_repository] 30 | reference node[:rbenv_vars][:git_revision] 31 | action :sync 32 | user node[:rbenv][:user] 33 | group node[:rbenv][:group] 34 | end 35 | 36 | end 37 | -------------------------------------------------------------------------------- /recipes/ruby_build.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook Name:: rbenv 3 | # Recipe:: ruby_build 4 | # 5 | # Author:: Jamie Winsor () 6 | # 7 | # Copyright 2011-2012, Riot Games 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 | include_recipe "git" 23 | 24 | with_home_for_user(node[:rbenv][:user]) do 25 | 26 | git node[:ruby_build][:prefix] do 27 | repository node[:ruby_build][:git_repository] 28 | reference node[:ruby_build][:git_revision] 29 | action :sync 30 | user node[:rbenv][:user] 31 | group node[:rbenv][:group] 32 | end 33 | 34 | end 35 | -------------------------------------------------------------------------------- /resources/execute.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook Name:: rbenv 3 | # Resource:: execute 4 | # 5 | # Author:: Jamie Winsor () 6 | # 7 | 8 | actions :run 9 | default_action :run 10 | 11 | attribute :command, kind_of: [String, Array], name_attribute: true 12 | attribute :creates, kind_of: String 13 | attribute :cwd, kind_of: String 14 | attribute :environment, kind_of: Hash, default: Hash.new 15 | attribute :group, kind_of: [String, Integer] 16 | attribute :path, kind_of: Array, default: Array.new 17 | attribute :returns, kind_of: [Integer, Array] 18 | attribute :timeout, kind_of: Integer 19 | attribute :umask, kind_of: [String, Integer] 20 | attribute :user, kind_of: [String, Integer] 21 | attribute :ruby_version, kind_of: String 22 | -------------------------------------------------------------------------------- /resources/gem.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook Name:: rbenv 3 | # Resource:: gem 4 | # 5 | # Author:: Jamie Winsor () 6 | # 7 | # Copyright 2011-2012, Riot Games 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 | actions :install, :upgrade, :remove, :purge 23 | 24 | attribute :package_name, :kind_of => String, :name_attribute => true 25 | attribute :ruby_version, :kind_of => String 26 | attribute :version, :kind_of => String 27 | attribute :source, :kind_of => String 28 | attribute :gem_binary, :kind_of => String 29 | attribute :response_file, :kind_of => String 30 | attribute :options, :kind_of => [String, Hash] 31 | 32 | def initialize(*args) 33 | super 34 | @action = :install 35 | @resource_name = :rbenv_gem 36 | @provider = Chef::Provider::Package::RbenvRubygems 37 | end 38 | -------------------------------------------------------------------------------- /resources/ruby.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook Name:: rbenv 3 | # Resource:: ruby 4 | # 5 | # Author:: Jamie Winsor () 6 | # 7 | # Copyright 2011-2012, Riot Games 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 | actions :install 23 | 24 | attribute :name, :kind_of => String 25 | attribute :ruby_version, :kind_of => String 26 | attribute :force, :default => false 27 | attribute :global, :default => false 28 | attribute :patch, :default => nil 29 | 30 | def initialize(*args) 31 | super 32 | @action = :install 33 | @ruby_version ||= @name 34 | end 35 | -------------------------------------------------------------------------------- /templates/default/plugins/rbenv.rb.erb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Jamie Winsor () 3 | # 4 | # Copyright 2011-2012, Riot Games 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 | provides "rbenv" 20 | provides "rbenv/root" 21 | provides "rbenv/global_version" 22 | provides "rbenv/current_version" 23 | provides "rbenv/current_version_prefix" 24 | provides "rbenv/versions" 25 | 26 | rbenv Mash.new unless rbenv 27 | 28 | def rbenv_command(*args) 29 | run_command(:command => "<%= @rbenv_bin %> #{args.join(' ')}", :no_status_check => true) 30 | end 31 | 32 | def root 33 | status, stdout, stderr = rbenv_command("root") 34 | status == 0 ? stdout : nil 35 | end 36 | 37 | def version 38 | status, stdout, stderr = rbenv_command("version-name") 39 | status == 0 ? stdout : nil 40 | end 41 | 42 | def prefix 43 | status, stdout, stderr = rbenv_command("prefix") 44 | status == 0 ? stdout : nil 45 | end 46 | 47 | def global 48 | status, stdout, stderr = rbenv_command("global") 49 | status == 0 ? stdout : nil 50 | end 51 | 52 | def versions 53 | status, stdout, stderr = rbenv_command("versions") 54 | versions = [] 55 | 56 | if status == 0 57 | version_dirs = Dir[File.join(root, "versions", "*")].select { |f| File.directory?(f) } 58 | versions = version_dirs.map { |d| File.basename(d) } 59 | end 60 | 61 | versions 62 | end 63 | 64 | rbenv[:root] = root 65 | rbenv[:global_version] = global 66 | rbenv[:current_version] = version 67 | rbenv[:current_version_prefix] = prefix 68 | rbenv[:versions] = versions 69 | -------------------------------------------------------------------------------- /templates/default/rbenv.sh.erb: -------------------------------------------------------------------------------- 1 | export RBENV_ROOT=<%= @rbenv_root %> 2 | export PATH=$RBENV_ROOT/bin:<%= @ruby_build_bin_path %>:$PATH 3 | 4 | case $SHELL in 5 | */zsh) 6 | source $RBENV_ROOT/completions/rbenv.zsh 7 | ;; 8 | */bash) 9 | source $RBENV_ROOT/completions/rbenv.bash 10 | ;; 11 | *) 12 | source $RBENV_ROOT/completions/rbenv.bash 13 | ;; 14 | esac 15 | 16 | eval "$(rbenv init -)" 17 | -------------------------------------------------------------------------------- /test/fixtures/cookbooks/fake/attributes/default.rb: -------------------------------------------------------------------------------- 1 | 2 | default['fake']['ruby_version'] = '1.9.3-p448' 3 | -------------------------------------------------------------------------------- /test/fixtures/cookbooks/fake/metadata.rb: -------------------------------------------------------------------------------- 1 | name 'fake' 2 | version '1.0.0' 3 | 4 | depends 'rbenv' 5 | -------------------------------------------------------------------------------- /test/fixtures/cookbooks/fake/recipes/gem_install.rb: -------------------------------------------------------------------------------- 1 | include_recipe 'fake::ruby_install' 2 | 3 | rbenv_gem 'bundler' do 4 | ruby_version node['fake']['ruby_version'] 5 | version '1.3.5' 6 | end 7 | -------------------------------------------------------------------------------- /test/fixtures/cookbooks/fake/recipes/ruby_install.rb: -------------------------------------------------------------------------------- 1 | 2 | include_recipe 'rbenv::default' 3 | include_recipe 'rbenv::ruby_build' 4 | 5 | rbenv_ruby node['fake']['ruby_version'] do 6 | global true 7 | end 8 | -------------------------------------------------------------------------------- /test/integration/gem_install/bats/gem_install.bats: -------------------------------------------------------------------------------- 1 | @test "installs a gem into the correct Ruby" { 2 | # We have unset these vars to bust out of Busser's sandbox 3 | unset GEM_HOME GEM_PATH GEM_CACHE 4 | run /opt/rbenv/shims/gem list bundler -v 1.3.5 -i 5 | [ "$status" -eq 0 ] 6 | } 7 | -------------------------------------------------------------------------------- /test/integration/ruby_install/bats/ruby_install.bats: -------------------------------------------------------------------------------- 1 | @test "installs the correct version of Ruby" { 2 | /opt/rbenv/shims/ruby --version | grep 1.9.3p448 3 | } 4 | 5 | @test "properly sets PATH environment variable" { 6 | . /etc/profile.d/rbenv.sh 7 | ruby --version | grep 1.9.3p448 8 | } 9 | --------------------------------------------------------------------------------