Skip to content

Instantly share code, notes, and snippets.

@benridane
Last active November 4, 2016 16:35
Show Gist options
  • Save benridane/1a5858cde6278e82c7ca6e804b105c69 to your computer and use it in GitHub Desktop.
Save benridane/1a5858cde6278e82c7ca6e804b105c69 to your computer and use it in GitHub Desktop.
#!/bin/bash
if type git > /dev/null 2>&1; then
echo "git found"
else
sudo yum install git
fi
if type rbenv > /dev/null 2>&1; then
echo "rbenv found"
else
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
exec $SHELL -l
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
fi
if type ruby > /dev/null 2>&1; then
echo "ruby found"
else
rbenv install 2.3.0
rbenv global 2.3.0
rbenv rehash
fi
if type bundle > /dev/null 2>&1; then
echo "bundle found"
else
gem install bundle
fi
mkdir -p itamae/recipes/remote_files itamae/nodes itamae/spec/centos
cat << 'EOS' > itamae/Gemfile
source 'https://rubygems.org'
gem 'itamae'
gem 'rake'
gem 'serverspec'
EOS
cat << 'EOS' > itamae/recipes/ruby_build.rb
package "epel-release"
package "gcc"
package "openssl-devel"
package "libyaml-devel"
package "readline-devel"
package "zlib-devel"
package "git"
RBENV_DIR = "/usr/local/rbenv"
RBENV_SCRIPT = "/etc/profile.d/rbenv.sh"
git RBENV_DIR do
repository "git://github.com/sstephenson/rbenv.git"
end
remote_file RBENV_SCRIPT do
source "remote_files/rbenv.sh"
end
execute "set owner and mode for #{RBENV_SCRIPT} " do
command "chown root: #{RBENV_SCRIPT}; chmod 644 #{RBENV_SCRIPT}"
user "root"
end
execute "mkdir #{RBENV_DIR}/plugins" do
not_if "test -d #{RBENV_DIR}/plugins"
end
git "#{RBENV_DIR}/plugins/ruby-build" do
repository "git://github.com/sstephenson/ruby-build.git"
end
node["rbenv"]["versions"].each do |version|
execute "install ruby #{version}" do
command "source #{RBENV_SCRIPT}; rbenv install #{version}"
not_if "source #{RBENV_SCRIPT}; rbenv versions | grep #{version}"
end
end
execute "set global ruby #{node["rbenv"]["global"]}" do
command "source #{RBENV_SCRIPT}; rbenv global #{node["rbenv"]["global"]}; rbenv rehash"
not_if "source #{RBENV_SCRIPT}; rbenv global | grep #{node["rbenv"]["global"]}"
end
node["rbenv"]["gems"].each do |gem|
execute "gem install #{gem}" do
command "source #{RBENV_SCRIPT}; gem install #{gem}; rbenv rehash"
not_if "source #{RBENV_SCRIPT}; gem list | grep #{gem}"
end
end
execute "echo using user rbenv" do
command "echo 'using user rbenv below'; cat RBENV_SCRIPT"
end
EOS
cat << 'EOS' > itamae/recipes/remote_files/rbenv.sh
export RBENV_ROOT="/usr/local/rbenv"
export PATH="${RBENV_ROOT}/bin:${PATH}"
eval "$(rbenv init --no-rehash -)"
EOS
cat << 'EOS' > itamae/nodes/node.json
{
"rbenv": {
"versions": ["2.1.1", "2.1.2",2.3.0],
"global": "2.3.0",
"gems": ["bundler"]
}
}
EOS
cat << 'EOS' > itamae/spec/centos/ruby_build_spec.rb
require 'spec_helper'
describe command('which rbenv') do
let(:disable_sudo) { true }
its(:stdout) { should match %r{~/.rbenv/bin/rbenv} }
end
describe file('~/.bash_profile') do
it { should be_file }
it { should be_owned_by 'root' }
it { should be_grouped_into 'root' }
it { should be_mode 644 }
its(:content) { should match(/^export RBENV_ROOT="\~/.rbenv"$/) }
its(:content) { should match(/^export PATH="\${RBENV_ROOT}\/bin:\${PATH}"$/) }
its(:content) { should match(/^eval "\$\(rbenv init --no-rehash -\)"$/) }
end
describe file('/usr/local/rbenv/plugins') do
it { should be_directory }
it { should be_owned_by 'mike' }
it { should be_grouped_into 'mike' }
it { should be_mode 755 }
end
describe file('/usr/local/rbenv/plugins/ruby-build') do
it { should be_directory }
it { should be_owned_by 'mike' }
it { should be_grouped_into 'mike' }
it { should be_mode 755 }
end
%w(2.3.0).each do |versoin|
describe command("rbenv versions | grep #{versoin}") do
let(:disable_sudo) { true }
its(:stdout) { should match(/#{Regexp.escape(versoin)}/) }
end
end
describe command('rbenv global') do
let(:disable_sudo) { true }
its(:stdout) { should match(/2.3.0/) }
end
EOS
cd itamae
bundle install --path=vendor/bundle
#~/bin/bundle exec itamae ssh -h localhost -u vagrant -j nodes/node.json recipes/ruby_build.rb
#~/bin/bundle exec itamae ssh -h localhost -u mike -j nodes/node.json recipes/ruby_build.rb
bundle exec serverspec-init
#~/bin/bundle exec rake spec spec/centos/ruby_build_spec.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment