# Copyright (C) 2011-2012 RightScale, Inc, All Rights Reserved Worldwide. # # THIS PROGRAM IS CONFIDENTIAL AND PROPRIETARY TO RIGHTSCALE # AND CONSTITUTES A VALUABLE TRADE SECRET. Any unauthorized use, # reproduction, modification, or disclosure of this program is # strictly prohibited. Any use of this program by an authorized # licensee is strictly subject to the terms and conditions, # including confidentiality obligations, set forth in the applicable # License Agreement between RightScale.com, Inc. and # the licensee module RightConf class CassandraConfigurator SOURCE_BASE_URL = 'http://archive.apache.org/dist/cassandra/VERSION/apache-cassandra-VERSION-bin.tar.gz' DEFAULT_CASSANDRA_INSTALL = ['/opt/cassandra', File.join(ENV['HOME'], 'cassandra')] include Configurator register :cassandra description 'Installs Cassandra' setting 'version', 'Cassandra version to install', :required => true setting 'install_path', 'Path to Cassandra installation directory, uses /opt/cassandra if it ' + 'is writable by the current user or ~/cassandra otherwise by default' setting 'abort_on_failure','Whether to abort if Cassandra failed to install (false by default)' setting 'endpoint_snitch', 'Set custom endpoint snitch for installed cassandra' # Check whether Cassandra is installed # # === Return # true:: If Cassandra is installed # false:: Otherwise def check_linux report_check('Checking for Cassandra') installed = Command.execute('cassandra', '-h').success? report_result(installed) installed end alias :check_darwin :check_linux # Check on windows # # === Return # true:: Always return true def check_windows true end # Install Linux Mercurial package # # === Return # true:: Always return true def run_linux install_path(default_install_path) if install_path.nil? abort_on_failure(true) if abort_on_failure.nil? report_check('Installing Cassandra') url = SOURCE_BASE_URL.gsub('VERSION', version) if File.exist?(install_path) FileUtils.rm_rf("#{install_path}_old") FileUtils.mv(install_path, "#{install_path}_old") post_note "Had to move #{install_path} to #{install_path}_old" end FileUtils.mkdir_p(install_path) Dir.chdir(install_path) do Command.execute('curl', '-O', '-f', url, abort_option('Failed to download Cassandra')) unless File.exist?(File.join(install_path, File.basename(url))) Command.execute('tar', 'xvzf', File.basename(url), abort_option('Failed to untar Cassandra')) end bin_path = File.join(install_path, "apache-cassandra-#{version}", 'bin') if EnvironmentUpdater.update("PATH=$PATH:#{bin_path}", ['PATH']) post_note 'Cassandra was installed, please reload your shell to activate it and re-run rconf' aborting(true) end report_success if (endpoint_snitch) report_check 'Change cassandra configuration.' config_path = File.join(install_path, "apache-cassandra-#{version}", 'conf', 'cassandra.yaml') backup_ext = '.orig' Command.execute('sed', '-i', backup_ext, '-e', "s%^endpoint_snitch: .*$%endpoint_snitch: #{endpoint_snitch}%", config_path) report_success post_note "Origin config file in #{config_path}#{backup_ext}" end end alias :run_darwin :run_linux # Install Windows software # # === Return # true:: Always return true def run_windows # TBD end protected # Retrieve default install path # Try /opt/cassandra and revert to ~/cassandra if not writable # # === Return # path:: Default cassandra install path def default_install_path path = if File.exist?('/opt') if File.exist?('/opt/cassandra') pick_default_path(File.writable?('/opt/cassandra')) else res = Command.execute('mkdir', '/opt/cassandra') pick_default_path(res.success?) end else res = Command.execute('mkdir', '-p', '/opt/cassandra') pick_default_path(res.success?) end end # First or second choice nginx install path # # === Parameter # first_choice(TrueClass|FalseClass):: Whether to use first or second choice path # # === Return # path(String):: Path def pick_default_path(first_choice) path = DEFAULT_CASSANDRA_INSTALL[first_choice && 0 || 1] end # Produce abort on failure option # # === Parameters # message(String):: Abort message to be used in case abort option should be set # # === Return # {}:: Empty hash if 'abort_on_failure' is notset # opts(Hash):: Abort option with give message otherwise def abort_option(message) opts = abort_on_failure && { :abort_on_failure => message } || {} end end end