#!/bin/env ruby ## # fastly_upload_vcl - upload raw VCL files to Fastly # # Author:: Fastly Inc # Copyright:: Copyright (c) 2011 Fastly Inc # License:: Distributes under the same terms as Ruby # # = USAGE # # fastly_upload_vcl # # = CONFIGURATION # # You can either have a config file in either ~/.fastly or /etc/fastly with # # api_key = # # or a config file with # # user = # password = # # Alternatively you can pass in any of those options on the command line # # fastly_upload_vcl --api_key # fastly_upload_vcl --user --password # require 'rubygems' require 'fastly' # :nodoc: def die(message) warn message exit -1 end params = Fastly.get_options("#{ENV['HOME']}/.fastly", "/etc/fastly") service_id = ARGV.shift || die("You must pass in a service id") vcl_file = ARGV.shift || die("You must pass in a vcl file") die("Couldn't find any of the config files - #{configs.join(',')}") unless params.keys.size>0 die("Couldn't find vcl file #{vcl_file}") unless File.file?(vcl_file) fastly = Fastly.new(params) service = fastly.get_service(service_id) || die("Couldn't find service #{service_id}") old = service.version updated = old.clone puts "Uploading #{vcl_file} and bumping version from #{old.number} to #{updated.number}" begin name = File.basename(vcl_file, ".vcl") content = File.new(vcl_file, "r").read vcl = updated.vcl(name) if vcl vcl.content = content vcl.save! else updated.upload_vcl(name, content) end updated.activate! rescue => e die("Couldn't upload and activate vcl: #{e}") end puts "Done!"