Skip to content

Instantly share code, notes, and snippets.

@gilmation
Last active August 29, 2015 14:01
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 gilmation/91305101a8cfe3747146 to your computer and use it in GitHub Desktop.
Save gilmation/91305101a8cfe3747146 to your computer and use it in GitHub Desktop.
A ruby script containing utility methods for git projects using the NetDNA CDN.
require 'rubygems'
require 'yaml'
require 'netdnarws'
#
# Utility methods for git projects using
# the NetDNA CDN. Put this file at the root of the project that
# you wish to manage so that the paths match the resources in the
# CDN.
#
# Example Gemfile:
#
# source "https://rubygems.org"
# gem 'netdnarws', '0.2.8'
#
# Run the script to check your configuration
# See -help for the available methods
#
class Netdna
CONFIG_FILE = File.join(ENV['HOME'],'.netdna.yml')
#
# Load up the netdna config file and connect to the API
# The location of the config file is defined in Netdna.CONFIG_FILE
#
def initialize
@netdna_data = _load_netdna_data
@api = NetDNARWS::NetDNA.new( @netdna_data['alias'], @netdna_data['key'], @netdna_data['secret'] )
end
#
# Get Account information
#
def account
@api.get("/account.json")
end
#
# Print Account information
#
def list_account
puts account
end
#
# Get all types of Zones
#
def zones
@api.get('/zones.json')
end
#
# Print information on all types of Zones
#
def list_zones
puts zones
end
#
# Get all Pull Zones
#
def pull_zones
@api.get('/zones/pull.json')
end
#
# Print information on all Pull Zones
#
def list_pull_zones
puts pull_zones
end
#
# Get the ID of the main app-central pull zone
#
def pull_zone_id
@netdna_data['pull_zone_id']
end
#
# List the ID of the main app-central pull zone
#
def list_pull_zone_id
puts pull_zone_id
end
#
# Purge the full cache in the App Central Pull Zone
# (by sending no argument or a nil argument) or purge a single file or an Array of filenames
# file_or_files - The uri to purge or the Array of uris to be purged.
#
def purge( file_or_files=nil )
if file_or_files.nil?
puts "Purging all files from [#{pull_zone_id}]"
@api.purge( pull_zone_id )
else
puts "Purging specific files from [#{pull_zone_id}]"
puts "Files are [#{file_or_files}]"
@api.purge( pull_zone_id, file_or_files )
end
end
#
# Purge all the files that have been changed between
# HEAD (The most recent commit in the index) and
# the commit_sha argument (This can be a normal SHA or HEAD^, HEAD~2, etc)
# commit_sha the commit_sha to diff with HEAD (This can be a normal SHA or HEAD^, HEAD~2, etc)
#
def purge_all_since_commit( commit_sha )
purge( git_files_changed( commit_sha ) )
end
#
# Get all the files that have been changed between
# HEAD (The most recent commit in the index) and
# the commit_sha argument (This can be a normal SHA or HEAD^, HEAD~2, etc)
# commit_sha the commit_sha to diff with HEAD (This can be a normal SHA or HEAD^, HEAD~2, etc)
#
def git_files_changed( commit_sha )
files = `git diff --name-only HEAD #{commit_sha}`
files = files.split(' ')
files
end
#
# List all the files that have been changed between
# HEAD (The most recent commit in the index) and
# the commit_sha argument (This can be a normal SHA or HEAD^, HEAD~2, etc)
# commit_sha the commit_sha to diff with HEAD (This can be a normal SHA or HEAD^, HEAD~2, etc)
#
def list_git_files_changed( commit_sha )
puts git_files_changed( commit_sha )
end
#
# Check to make sure that the config file exists
#
def self.check_config_file
File.exists?(Netdna::CONFIG_FILE)
end
private
#
# Load the netdna config file
#
def _load_netdna_data
_load_yaml( Netdna::CONFIG_FILE )
end
# Load the yaml or throw an error
# yaml_file - The path to the yaml file
#
def _load_yaml( yaml_file )
# load the config
throw "Oops, can't load the yaml file [#{yaml_file}]" unless yaml_file && File.exists?(yaml_file)
YAML.load_file( yaml_file )
end
end
if __FILE__==$0
unless Netdna.check_config_file
puts ""
puts "Config file [#{Netdna::CONFIG_FILE}] is missing"
puts ""
puts "File contents should be:"
puts <<-eos
---
key: $VALUE_OF_API_KEY_FROM_MAXCDN_CONTROL_PANEL_GOES_HERE
secret: $VALUE_OF_API_SECRET_FROM_MAXCDN_CONTROL_PANEL_GOES_HERE
alias: $ALIAS
pull_zone_id: $ZONE_ID
eos
exit
end
if ARGV.size < 1 || ARGV[0] =~ /\-help/
puts
puts "usage: #{File.basename(__FILE__)} -option args"
ins_meths = Netdna.instance_methods(false)
puts "----------------"
puts "Options are:"
ins_meths.each do | method |
puts "-#{method}"
end
puts "-help"
puts "----------------"
puts "The name of the option that you want to run is NOT optional...not specifying an option will cause this message to be rendered"
puts
exit
elsif ARGV[0] =~ /^-/
Netdna.new.send( ARGV[0].sub('-',''), *ARGV[1..-1] )
else
#problem with the argument
raise "Arguments [#{ARGV} are not the correct format"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment