Skip to content

Instantly share code, notes, and snippets.

@karmi
Last active December 24, 2015 14:53
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save karmi/4239703 to your computer and use it in GitHub Desktop.
Save karmi/4239703 to your computer and use it in GitHub Desktop.
Build a virtual machine with Elasticsearch from scratch with Vagrant
.vagrant
Gemfile.lock
Berksfile.lock
tmp/

Build a Vagrant virtual machine with Elasticsearch

This code allows you to launch, bootstrap and configure an Ubuntu 12.04 server with Elasticsearch, from scratch.

You need Ruby 1.9 and the Bundler gem. You need to install VirtualBox for Vagrant.

Elasticsearch is installed via Chef Solo integration from the karmi/cookbook-elasticsearch cookbook.

You may customize the setup with a configuration and installation file (see instructions below).

Setup:

1/ Install required Rubygems:

bundle install

2/ Launch and provision the virtual machine:

time bundle exec vagrant up

3/ Connect to the box via SSH:

bundle exec vagrant ssh

You should see elasticsearch running on http://33.33.33.10:9200/.

4/ Execute a command via SSH:

bundle exec vagrant ssh -c "service elasticsearch status -v"

5/ To customize the Elasticsearch installation, pass a path to JSON file over-riding the default attributes:

echo '
{
  "elasticsearch" : {
    "cluster_name" : "elasticsearch_test_in_vagrant",
    "index_shards" : 1,
    "index_replicas" : 0
  }
}' > node.json

NODE_CONFIG=node.json time bundle exec vagrant up

6/ To perform additional commands after the installation, pass a path to file with commands:

echo '
# Install the Tire Rubygem
apt-get install make git libcurl3 libcurl3-gnutls libcurl4-openssl-dev libsqlite3-dev -y
apt-get install redis-server  -y
apt-get install ruby1.9.3 -y
update-alternatives --set ruby /usr/bin/ruby1.9.1
gem install bundler --no-rdoc --no-ri
test -d "tire" || git clone git://github.com/karmi/tire.git
chown -R vagrant:staff tire
cd tire
bundle install
' > install.sh

NODE_CONFIG=node.json INSTALL=install.sh time bundle exec vagrant up

7/ After you start the virtual machine, you can update the node configuration and additional commands and re-run the provisioning:

NODE_CONFIG=node.json INSTALL=install.sh time bundle exec vagrant provision

8/ To run a sequence of commands prepared locally, pass it to SSH:

echo '
cd tire

echo "Running Tire tests in '`pwd`'"
echo "Ruby: `ruby -v`"
echo "Java: `java -version 2>&1 | head -1`"

rm -f results.txt
for i in {1..5}
  do
    echo "-------------------------------------------------------------------------------"
    echo "TEST $i"
    echo "-------------------------------------------------------------------------------"
    time bundle exec rake test
  if [ $? == 0 ]; then echo "$i: OK" >> results.txt; else echo "$i: FAIL">> results.txt; fi
done

echo; echo; echo "==============================================================================="
cat results.txt
' > run_tire_tests.sh

cat run_tire_tests.sh | xargs -0 bundle exec vagrant ssh -c
cookbook 'apt'
cookbook 'ark'
cookbook 'java', git: 'git://github.com/opscode-cookbooks/java.git'
cookbook 'elasticsearch', git: 'git://github.com/elasticsearch/cookbook-elasticsearch.git'
source 'http://rubygems.org'
gem 'vagrant', '1.0.6'
gem 'berkshelf', '1.2.1'
require 'json'
begin
require 'active_support/core_ext/hash/deep_merge'
rescue LoadError => e
STDERR.puts '', '[!] ERROR -- Please install ActiveSupport (gem install activesupport)', '-'*80, ''
exit 1
end
# Automatically install and mount cookbooks from Berksfile
#
require 'berkshelf/vagrant'
node_config = {
"elasticsearch" => {
"cluster" => { "name" => "elasticsearch_vagrant" },
"plugins" => {
'karmi/elasticsearch-paramedic' => {},
'lukas-vlcek/bigdesk' => {}
},
"bootstrap" => { "mlockall" => false }
}
}
if extra_node_config_file = ENV['NODE_CONFIG']
STDERR.puts "[PLASTIC] Reading extra config from file: #{extra_node_config_file}"
node_config.deep_merge!( JSON.parse( File.read(extra_node_config_file) ) )
end
run_list = %w| apt java elasticsearch elasticsearch::plugins |
Vagrant::Config.run do |config|
config.vm.define 'elasticsearch' do |box_config|
box_config.vm.box = 'precise64'
box_config.vm.box_url = 'http://files.vagrantup.com/precise64.box'
box_config.vm.host_name = 'elasticsearch'
box_config.vm.network :hostonly, '33.33.33.10'
box_config.vm.customize ["modifyvm", :id, "--memory", 1024]
# Install latest Chef on the machine
#
box_config.vm.provision :shell do |shell|
shell.inline = %Q{
R=`which ruby` || R='/opt/vagrant_ruby/bin/ruby'
test -x $R && $R -e 'File.mtime("/var/lib/apt/lists/partial/") < Time.now - 3600 ? exit(1) : exit(0)' > /dev/null 2>&1 || \
(
apt-get update --quiet --yes && \
apt-get install curl vim --quiet --yes
)
test -d "/opt/chef" || \
(
curl -# -L http://www.opscode.com/chef/install.sh | bash
)
}
end
# Provision the machine with Chef Solo
#
box_config.vm.provision :chef_solo do |chef|
chef.log_level = :debug
chef.run_list = run_list
chef.json = node_config
end
# Run extra post-install commands from file
#
if extra_install_file = ENV['INSTALL']
STDERR.puts "[PLASTIC] Running post-installation commands from file: #{extra_install_file}"
box_config.vm.provision :shell, :path => extra_install_file
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment