Skip to content

Instantly share code, notes, and snippets.

@nmilford
Created February 9, 2013 16:49
Show Gist options
  • Save nmilford/4745987 to your computer and use it in GitHub Desktop.
Save nmilford/4745987 to your computer and use it in GitHub Desktop.
When chef-client is run manually node[:Cassandra][Keyspaces] populates properly from discover_cassandra_schema() and then the cassandra_maintenance.sh.erb template is generated properly. When Chef is run in the background (we keep it in cron) it does not populate, and the template is blank. I suspect it might have something to do with the way 's…
# relevent bits from my library
def discover_cassandra_schema
# Discover schema information for this host so we can dynamically generate
# maintenance scripts and collectd config.
#
# This depends on the discover-cassandra-schema.sh script. Eventually
# I will us the Cassandra Ruby gem to get this data directly.
# Gets schema data into an array in and the form of keyspace.columnfamily
schema_data = `/var/tmp/discover-cassandra-schema.sh`.split("\n")
schema = {}
keyspaces = []
columnfamilies = []
schema_data.each do |x|
keyspaces << x.split(".")[0]
end
keyspaces.uniq.each do |ks|
schema_data.each do |cf|
if cf.include?("#{ks}")
columnfamilies << cf.split(".")[1]
end
end
schema["#{ks}"] = columnfamilies
columnfamilies = []
end
return schema
end
#/bin/bash
# Generated by Chef for <%= @node[:fqdn] %>
logFile="/var/log/cassandra/mainenance.log"
<% @node[:Cassandra][:Keyspaces].each do |ks| -%>
<% ks[1].each do |cf| -%>
echo "<%= ks[0] %>.<%= cf %> repair started at $(date)" >> $logFile
nodetool -h <%= node[:fqdn] %> -p <%= @node[:Cassandra][:JMX_PORT] %> repair <%= ks[0] %> <%= cf %>
echo "<%= ks[0] %>.<%= cf %> repair completed at $(date)" >> $logFile
echo "<%= ks[0] %>.<%= cf %> compaction started at $(date)" >> $logFile
nodetool -h <%= node[:fqdn] %> -p <%= @node[:Cassandra][:JMX_PORT] %> compact <%= ks[0] %> <%= cf %>
echo "<%= ks[0] %>.<%= cf %> compaction completed at $(date)" >> $logFile
echo "<%= ks[0] %>.<%= cf %> cleanup started at $(date)" >> $logFile
nodetool -h <%= node[:fqdn] %> -p <%= @node[:Cassandra][:JMX_PORT] %> cleanup <%= ks[0] %> <%= cf %>
echo "<%= ks[0] %>.<%= cf %> cleanup completed at $(date)" >> $logFile
<% end -%>
<% end -%>
# Relevant bits from the recipe.
class ::Chef::Recipe
include ::CassandraHelpers
end
# Drop the schema discovery script. Make sure it loads before the run.
a = cookbook_file "/var/tmp/discover-cassandra-schema.sh" do
source "discover-cassandra-schema.sh"
mode "0775"
owner "root"
group "root"
end
a.run_action(:create)
# This depends on the script above existing.
node.normal[:Cassandra][:Keyspaces] = discover_cassandra_schema()
template "/var/tmp/cassandra-maintenance.sh" do
source "cassandra-maintenance.sh.erb"
mode "0775"
owner "root"
group "root"
end
#/bin/bash
getKeyspaces() {
echo "show keyspaces;" > /var/tmp/ks.cmd
keyspaces=( $(cassandra-cli -h $HOSTNAME -p 9160 -f /var/tmp/ks.cmd 2>&1 | grep Keyspace | grep -v 'system\|OpsCenter' | awk {' print $2 '} | sed 's/://' ) )
rm -f /var/tmp/ks.cmd
}
getColumnfamilies() {
echo "describe $keyspace;" > /var/tmp/cf.cmd
columnfamilies=( $(cassandra-cli -h $HOSTNAME -p 9160 -f /var/tmp/cf.cmd 2>&1 | grep ColumnFamily | awk {' print $2 '} | sed 's/://' ) )
rm -f /var/tmp/cf.cmd
}
getKeyspaces
for keyspace in "${keyspaces[@]}"; do
getColumnfamilies
for columnfamily in "${columnfamilies[@]}"; do
echo "$keyspace.$columnfamily"
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment