Skip to content

Instantly share code, notes, and snippets.

@thomasp11
Forked from zachad/autoprune.rb
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasp11/f88b08b593ca494c0c2f to your computer and use it in GitHub Desktop.
Save thomasp11/f88b08b593ca494c0c2f to your computer and use it in GitHub Desktop.
#!/usr/cloudshark/ruby/bin/ruby
#
#
# EXAMPLE CAPTURE PRUNING SCRIPT
#
# This is example code which must be run AT YOUR OWN RISK
# CloudShark is not responsible for any unintended data loss.
# This script is not officially supported by CloudShark or QA Cafe.
#
require 'date'
require 'time'
require 'json'
require 'open-uri'
require 'openssl'
###############################################################################
#
# Please fill in the following section with your settings:
#
################ VARIABLES FOR THE SYSADMIN TO SET ############################
$api_token="<API Token with GET, SEARCH, and DELETE PERMISSIONS>"
$server="https://<CloudShark URL>/"
$https=true
$verify_cert=false
# The number of days back to start deleting files from.
# Please change this to the actual cutoff you prefer. A month=30, etc.
delete_files_older_than_this_many_days=365
# Show the warning that you are about to delete a lot of files.
# Set this to false when deploying in production to speed up the script.
show_warning = true
################ END VARIABLES ################################################
if $verify_cert
$verify_cert = OpenSSL::SSL::VERIFY_PEER
else
$verify_cert = OpenSSL::SSL::VERIFY_NONE
end
def delete_by_capture_id(entry)
uri = URI "#{$server}/api/v1/#{$api_token}/delete/#{entry['id']}"
puts "Deleting capture #{entry['id']}: #{entry['filename']}"
options = {}
Net::HTTP.start(uri.host, uri.port, use_ssl: $https, verify_mode: $verify_cert) do |http|
begin
request = Net::HTTP::Post.new uri.request_uri
response = http.request request
res=JSON.parse(response.body)
if res['exceptions']
puts "Error from CloudShark server: #{res['exceptions'].first}"
exit 1
end
rescue Exception=>e
puts "There was an error deleting the capture by its id. #{e.message}"
exit 1
else
puts "Capture session #{entry['id']} deleted."
end
end
end
# calculate the date to delete files from before
ending_date = (Date.today.to_time - (delete_files_older_than_this_many_days * 86400)).strftime("%m/%d/%Y")
puts "Deleting captures uploaded on or before #{ending_date}"
params = "search[date]=1/1/1999-#{ending_date}&limit=90"
uri = "#{$server}/api/v1/#{$api_token}/search.json?#{params}"
puts "Searching for files via API:"
puts uri
# we need to run this search multiple times
# to get each *page* of results. The first time
# we will delete from the first page. Then we
# run the same query again to see if there are more results.
begin
loop do
response = open(uri, {ssl_verify_mode: $verify_cert}).read
res = JSON.parse(response)
if res['total'].to_i == 0 || res['captures'].nil?
puts "There were no captures matching the search parameters." if show_warning
exit
end
# display a warning the first time through
# remove this block of code if you do not need
# an escape plan
if show_warning
show_warning = false
puts "There are #{res['total']} captures about to be deleted."
puts "Press Control+C now if you don't want that to happen."
(1..10).each do
print "."
sleep 1
end
puts ""
end
# delete each of the captures on this page of results
res['captures'].each do |c|
delete_by_capture_id(c)
end
end
rescue Interrupt => e
puts "\nInterrupted by user."
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment