Skip to content

Instantly share code, notes, and snippets.

@kazu69
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kazu69/8b824595016ba612ff53 to your computer and use it in GitHub Desktop.
Save kazu69/8b824595016ba612ff53 to your computer and use it in GitHub Desktop.
bundle exec itamae docker cookbooks/base/node_build.rb --node-json=nodes/docker.json --image=IMAGE --no-tls-verify-peer
docker run -it ContainerID /bin/bash
source 'https://rubygems.org'
gem 'itamae'
gem 'rake'
gem 'serverspec'
gem 'highline'
require 'rake'
require 'rspec/core/rake_task'
task :spec => 'spec:all'
task :default => :spec
namespace :spec do
targets = []
Dir.glob('./spec/*').each do |dir|
next unless File.directory?(dir)
target = File.basename(dir)
target = "_#{target}" if target == "default"
targets << target
end
task :all => targets
task :default => :all
targets.each do |target|
original_target = target == "_default" ? target[1..-1] : target
desc "Run serverspec tests to #{original_target}"
RSpec::Core::RakeTask.new(target.to_sym) do |t|
ENV['TARGET_HOST'] = original_target
t.pattern = "spec/#{original_target}/*_spec.rb"
end
end
end
package "openssl-devel"
package "git"
package "tar"
NDENV_DIR = "#{node["ndenv"]["home"]}/.ndenv"
NDENV_SCRIPT = "/etc/profile.d/ndenv.sh"
git NDENV_DIR do
repository "git://github.com/riywo/ndenv.git"
not_if "test -d #{NDENV_DIR}"
end
directory "#{NDENV_DIR}/plugins" do
action :create
not_if "test -d #{NDENV_DIR}/plugins"
end
git "#{NDENV_DIR}/plugins/node-build" do
repository "git://github.com/riywo/node-build.git"
end
if node['ndenv']['user'] == 'vagrant'
execute "set owner and mode for #{NDENV_DIR}" do
command <<-EOH
chown -R vagrant: #{NDENV_DIR}
EOH
end
end
execute "expose ndenv path" do
command <<-EOH
echo 'export PATH="#{NDENV_DIR}/bin:$PATH"' >> #{NDENV_SCRIPT}
echo 'eval "$(ndenv init -)"' >> #{NDENV_SCRIPT}
EOH
not_if "echo $PATH | grep #{NDENV_DIR}"
end
node["ndenv"]["versions"].each do |version|
execute "install node #{version}" do
command <<-EOH
source #{NDENV_SCRIPT}
ndenv install #{version}
ndenv rehash
EOH
user node['ndenv']['user'] if user node['ndenv']['user']
not_if "ndenv versions | grep #{version}"
end
end
execute "set global node #{node["ndenv"]["global"]}" do
command <<-EOH
source #{NDENV_SCRIPT}
ndenv global #{node["ndenv"]["global"]}
ndenv rehash
EOH
user node['ndenv']['user'] if user node['ndenv']['user']
not_if "ndenv global | grep #{node["ndenv"]["global"]}"
end
execute "set environemt variables" do
command <<-EOH
echo 'export NODE_ENV=development' >> #{node["ndenv"]["home"]}/.bash_profile
EOH
end
# spec/default/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
# spec/spec_helper.rb
require 'serverspec'
require 'net/ssh'
require 'tempfile'
set :backend, :ssh
if ENV['ASK_SUDO_PASSWORD']
begin
require 'highline/import'
rescue LoadError
fail "highline is not available. Try installing it."
end
set :sudo_password, ask("Enter sudo password: ") { |q| q.echo = false }
else
set :sudo_password, ENV['SUDO_PASSWORD']
end
host = ENV['TARGET_HOST']
`vagrant up #{host}`
config = Tempfile.new('', Dir.tmpdir)
config.write(`vagrant ssh-config #{host}`)
config.close
options = Net::SSH::Config.for(host, [config.path])
options[:user] ||= Etc.getlogin
set :host, options[:host_name] || host
set :ssh_options, options
# Disable sudo
# set :disable_sudo, true
# Set environment variables
# set :env, :LANG => 'C', :LC_MESSAGES => 'C'
# Set PATH
# set :path, '/sbin:/usr/local/sbin:$PATH'
.
├── Gemfile
├── Rakefile
├── Vagrantfile
├── cookbooks
│   └── base
├── nodes
│   ├── docker.json
│   └── vagrant.json
└── spec
{
"ndenv": {
"user": "vagrant",
"home": "/home/vagrant",
"versions": ["v0.12.0", "iojs-v1.2.0"],
"global": "v0.12.0"
}
}
vagrant up
bundle itamae ssh --vagrant -h default -j nodes/vagrant.json cookbooks/base/node_build.rb
Vagrant.configure(2) do |config|
config.vm.box = "centos-6.5-x86_64"
config.vm.box_url = "http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.5-x86_64-v20140110.box"
config.vm.network "private_network", ip: "192.168.33.109"
config.ssh.forward_agent = true
config.vm.provision :itamae do |config|
config.sudo = true
config.recipes = 'cookbooks/base/node_build.rb'
config.json = 'nodes/vagrant.json'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment