Wiping riak

I have a need in our QA Environment to wipe some of the keys from Riak but not all of them before deploying a new build.

I started with using the ruby riak client, and had the following working

require 'riak'

#https://github.com/basho/riak-ruby-client/blob/master/README.markdown
#http://docs.basho.com/riak/latest/dev/taste-of-riak/ruby/

client=Riak::Client.new(:protocol => "pbc")

client = Riak::Client.new(:nodes => [
  {:host => '10.10.10.1', :pb_port => 8098}
])

client.buckets.each do |bucket|
  if bucket.name.start_with?('test')
    puts(bucket.name)
    bucket.keys.each do |key|
      bucket.delete(key)
    end
  end
end

Which gave a little error, but worked till I tired to run it on a windows machines, seem riak uses the ruby expect class which isn’t available on windows.

So then I took a different approach

require 'rest-client'
require 'json'
require 'uri'

host = "http://10.10.10.1:8098"

buckets_url = host + '/riak?buckets=true'
e_buckets_url = URI.escape(buckets_url)

json_object = JSON.parse(RestClient.get(e_buckets_url))
p json_object

json_object["buckets"].each do |bucket|
  if bucket.start_with?('test')
    p bucket 
    keys_url = 'http://10.10.10.1:8098' + '/riak/' + bucket + '?keys=true'
    e_keys_url = URI.escape(keys_url)
    p e_keys_url
    json_object2 = JSON.parse(RestClient.get(e_keys_url))
    p json_object2["keys"]
    json_object2["keys"].each do |key|
      delete_url = 'http://10.10.10.1:8098/riak' + '/' + bucket + '/' + key
      e_delete_url = URI.escape(delete_url)
      p e_delete_url
      response2 = RestClient.delete(e_delete_url)      
    end
  end

Then I wrapped it in ruby block for chef

ruby_block  "wipe riak" do
  block do
    host = "http://#{node['test']['avenger']['riakhostAddress']}:8098"

    buckets_url = host + '/riak?buckets=true'
    e_buckets_url = URI.escape(buckets_url)

    json_object = JSON.parse(RestClient.get(e_buckets_url))
    p json_object

    json_object["buckets"].each do |bucket|
      if bucket.start_with?('test')
        p bucket
        keys_url = host + '/riak/' + bucket + '?keys=true'
        e_keys_url = URI.escape(keys_url)
        p e_keys_url
        json_object2 = JSON.parse(RestClient.get(e_keys_url))
        p json_object2["keys"]
        json_object2["keys"].each do |key|
          delete_url = host + '/riak/' + bucket + '/' + key
          e_delete_url = URI.escape(delete_url)
          p e_delete_url
          response2 = RestClient.delete(e_delete_url)
        end
      end
    end
  end
end

Leave a Reply

Your email address will not be published.