Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: ruby
rvm:
- 1.9.3
- 2.0.0
- 2.1.0
- 2.1.1
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

[CloudFlare](http://www.cloudflare.com/) is a service that protects and speeds up websites. Capistrano Cloudflare provides [Capistrano](https://github.com/capistrano/capistrano/wiki/Documentation-v2.x) tasks to update your CloudFlare settings.

Capistrano CloudFlare Version 1.0 and above supports the new Capistrano v3 API. For compatbility with Capistrano v2, please use version `0.0.2`.
Capistrano CloudFlare Version 2.0 and above supports the new Capistrano v3 API. For compatbility with Capistrano v2, please use version `0.0.2`.

This fork of the library supports CloudFlare's version 4 API

Currently only cache purging is supported.

Expand All @@ -27,12 +29,12 @@ When using Capistrano, add:
require 'capistrano/cloudflare'

set :cloudflare_options, {
:domain => 'example.com',
:zone => 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
:email => 'me@example.com',
:api_key => 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF'
}

to config/deploy.rb. These options can be found in your [CloudFlare account seetings](https://www.cloudflare.com/my-account).
to config/deploy.rb. Your api key can be found in your [CloudFlare account seetings](https://www.cloudflare.com/my-account). Your Zone can be determined by called the CloudFlare API [List Zones Endpoint](https://api.cloudflare.com/#zone-list-zones)

The following Capistrano tasks should now be available:

Expand Down
6 changes: 4 additions & 2 deletions capistrano-cloudflare.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
require File.expand_path('../lib/capistrano/cloudflare/version', __FILE__)

Gem::Specification.new do |gem|
gem.authors = ["Nathan L Smith"]
gem.email = ["nlloyds@gmail.com"]
gem.authors = ["Nathan L Smith", "Tristan D Havelick"]
gem.email = ["nlloyds@gmail.com", "tristan@havelick.com"]
gem.description = %q{Capistrano extensions for CloudFlare}
gem.summary = %q{Lets you make CloudFlare API calls when deploying with Capistrano.}
gem.homepage = "https://github.com/cramerdev/capistrano-cloudflare"
Expand All @@ -14,9 +14,11 @@ Gem::Specification.new do |gem|
gem.name = "capistrano-cloudflare"
gem.require_paths = ["lib"]
gem.version = Capistrano::CloudFlare::VERSION
gem.required_ruby_version = '>= 2.0.0'

gem.add_dependency 'capistrano', '>= 2.0'
gem.add_dependency 'json'
gem.add_dependency 'httparty'

gem.add_development_dependency 'rake'
gem.add_development_dependency 'rspec'
Expand Down
30 changes: 17 additions & 13 deletions lib/capistrano/cloudflare.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
require 'capistrano'
require 'capistrano/cloudflare/version'
require 'json'
require 'net/http'
require 'httparty'
require 'rake'

module Capistrano
module CloudFlare
def self.send_request(options = {})
uri = URI('https://www.cloudflare.com/api_json.html')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data({
:v => 1,
:a => 'fpurge_ts',
:z => options[:domain],
:tkn => options[:api_key],
:email => options[:email]
zone = options[:zone]
params = {
:purge_everything => true,
}
headers = {
'Content-Type' => 'application/json',
'X-Auth-Email' => options[:email],
'X-Auth-Key' => options[:api_key]
}

})
response = JSON.parse(http.request(request).body)
resp = HTTParty.delete(
"https://api.cloudflare.com/client/v4/zones/#{zone}/purge_cache",
:body => params.to_json,
:headers => headers
)

JSON.parse(resp.body)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/capistrano/cloudflare/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Capistrano
module CloudFlare
VERSION = '1.0.0'
VERSION = '2.0'
end
end
8 changes: 4 additions & 4 deletions lib/capistrano/tasks/cloudflare.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ namespace :cloudflare do
namespace :cache do
desc "Purge the CloudFlare cache"
task :purge do
on roles(:all) do
run_locally do
raise 'Missing CloudFlare configuration.' unless fetch(:cloudflare_options).respond_to?(:[])
response = Capistrano::CloudFlare.send_request(fetch(:cloudflare_options))
if response['result'] == 'success'
info "Purged CloudFlare cache for #{fetch(:cloudflare_options)[:domain]}"
if response['success']
info "Purged CloudFlare cache for #{fetch(:cloudflare_options)[:zone]}"
else
error "CloudFlare cache purge failed. Reason: #{response['msg'] || 'unknown.'}"
error "CloudFlare cache purge failed. Reason: #{response['errors'].first['message'] || 'unknown.'}"
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions spec/capistrano/cloudflare_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
it { should be_a Module }

describe '.send_request' do
it 'should POST to the cloudflare API and return the response body as a hash' do
it 'should call out to the cloudflare API and return the response body as a hash' do
options = {
domain: 'example.com',
zone: 'Z',
email: 'me@example.com',
api_key: 'F'
}

body = { 'result' => 'success' }
stub_request(:post, 'https://www.cloudflare.com/api_json.html').to_return(
body = { 'success' => true }
stub_request(:delete, "https://api.cloudflare.com/client/v4/zones/Z/purge_cache").to_return(
:status => 200, :body => body.to_json
)

Capistrano::CloudFlare.send_request(options).should eq(body)
Capistrano::CloudFlare.send_request(options)['success'].should eq(true)
end
end
end