require 'rake/clean' namespace :cassandra do CASS_VERSION = "1.0.8" CASS_TARBALL = "apache-cassandra-#{CASS_VERSION}-bin.tar.gz" CASS_MD5 = "676887f6d185689c3383908f3ad8e015" task :download => "tmp/#{CASS_TARBALL}" directory 'tmp' file "tmp/#{CASS_TARBALL}" => "tmp" do require 'digest/md5' tarball = "tmp/#{CASS_TARBALL}" puts "*** Downloading Cassandra" sh "curl http://archive.apache.org/dist/cassandra/#{CASS_VERSION}/#{CASS_TARBALL} -o #{tarball}" digest = Digest::MD5.hexdigest(File.read(tarball)) unless digest == CASS_MD5 rm tarball raise "Expected MD5 #{CASS_MD5} but got #{digest}." end end task :install => :download do require 'yaml' puts "*** Unpacking Cassandra" rm_rf "cassandra" if File.exists? "cassandra" sh "tar -zxvf tmp/#{CASS_TARBALL}" mv "apache-cassandra-#{CASS_VERSION}", "cassandra" home = File.expand_path("../../cassandra", __FILE__) # Create base configuration data = File.join(home, "data").encode("us-ascii") mkdir_p data commit = File.join(home, "commit").encode("us-ascii") mkdir_p commit caches = File.join(home, "caches").encode("us-ascii") mkdir_p caches config_file = File.join(home, "conf", "cassandra.yaml") env_file = File.join(home, "conf", "cassandra-env.sh") conf = YAML::load_file(config_file) conf.merge!({ "initial_token" => 0, "partitioner" => "org.apache.cassandra.dht.RandomPartitioner", "data_file_directories" => [data], "commitlog_directory" => commit, "saved_caches_directory" => caches, "storage_port" => 27000, "ssl_storage_port" => 27001, "listen_address" => "127.0.0.1", "rpc_address" => "127.0.0.1", "rpc_port" => 29160, # DSE doesn't work OOTB as a single node unless you switch to simplesnitch "endpoint_snitch" => "org.apache.cassandra.locator.SimpleSnitch", }) File.open(config_file, "w") { |file| file.puts YAML::dump(conf) } env = File.read(env_file) env.sub!(/JMX_PORT=['"]?\d+['"]?/, 'JMX_PORT="27199"') File.open(env_file, "w") { |file| file.puts env } end task :start => :cassandra do puts "*** Starting Cassandra" # adding the -Dcassandra.for=dcell makes it easier to kill the right # cassandra if you're running more than one, e.g. pkill -f dcell # or DTRT and kill $(< cassandra/cassandra.pid) sh "cd cassandra && bin/cassandra -p cassandra.pid -Dcassandra.for=dcell" puts "*** waiting for Cassandra to come up before creating schema ..." sleep 2 puts "*** creating schema ..." sh "echo 'create keyspace dcell;' | cassandra/bin/cassandra-cli -h 127.0.0.1 -p 29160" sh "echo 'create column family dcell;' | cassandra/bin/cassandra-cli -h 127.0.0.1 -p 29160 -k dcell" end end file 'cassandra' do Rake::Task['cassandra:install'].invoke end CLEAN.include "tmp", "cassandra"