Last active
May 12, 2016 01:07
-
-
Save benridane/84ea04fd15c310e813fe7c5d041224ff to your computer and use it in GitHub Desktop.
fast setup ruby and itamae
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if type gem > /dev/null 2>&1; then | |
echo "ruby found" | |
else | |
sudo yum install centos-release-scl | |
sudo yum install ruby200 | |
scl enable ruby200 bash | |
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"], | |
"global": "2.1.2", | |
"gems": ["bundler"] | |
} | |
} | |
EOS | |
cat << 'EOS' > itamae/spec/centos/ruby_build_spec.rb | |
require 'spec_helper' | |
describe command('source /etc/profile.d/rbenv.sh; which rbenv') do | |
let(:disable_sudo) { true } | |
its(:stdout) { should match %r{/usr/local/rbenv/bin/rbenv} } | |
end | |
describe file('/etc/profile.d/rbenv.sh') 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="\/usr\/local\/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 'root' } | |
it { should be_grouped_into 'root' } | |
it { should be_mode 755 } | |
end | |
describe file('/usr/local/rbenv/plugins/ruby-build') do | |
it { should be_directory } | |
it { should be_owned_by 'root' } | |
it { should be_grouped_into 'root' } | |
it { should be_mode 755 } | |
end | |
%w(2.1.1 2.1.2).each do |versoin| | |
describe command("source /etc/profile.d/rbenv.sh; rbenv versions | grep #{versoin}") do | |
let(:disable_sudo) { true } | |
its(:stdout) { should match(/#{Regexp.escape(versoin)}/) } | |
end | |
end | |
describe command('source /etc/profile.d/rbenv.sh; rbenv global') do | |
let(:disable_sudo) { true } | |
its(:stdout) { should match(/2.1.2/) } | |
end | |
EOS | |
cd itamae | |
~/bin/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 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