class Object

Public Instance Methods

deploy_vcl(api_key, service_id, vcl_path, purge_all, include_files) click to toggle source
# File lib/methods.rb, line 5
def deploy_vcl(api_key, service_id, vcl_path, purge_all, include_files)
  login_opts = { api_key: api_key }
  fastly = Fastly.new(login_opts)
  service = fastly.get_service(service_id)

  active_version = service.versions.find(&:active?)
  puts "Active Version: #{active_version.number}"
  domain = fastly.list_domains(service_id: service.id,
                               version: active_version.number).first
  puts "Domain Name: #{domain.name}"

  new_version = active_version.clone
  puts "New Version: #{new_version.number}"

  fastly.list_vcls(service_id: service.id,
                   version: new_version.number)
        .each(&:delete!)

  can_verify_deployment = upload_main_vcl new_version, vcl_path, service_id

  unless include_files.nil?
    include_files.each do |include_file|
      upload_include_vcl new_version, include_file, service_id
    end
  end

  puts 'Validating...'

  validate(new_version)

  puts 'Activating...'
  new_version.activate!

  if can_verify_deployment
    print 'Waiting for changes to take effect.'
    attempts = 1
    deployed_vcl_version_number = 0

    while attempts < 150 && deployed_vcl_version_number != new_version.number.to_s
      sleep 2
      url = URI.parse("http://#{domain.name}.global.prod.fastly.net/vcl_version")
      req = Net::HTTP::Get.new(url.to_s)
      res = Net::HTTP.start(url.host, url.port) do |http|
        http.request(req)
      end
      deployed_vcl_version_number = res.body
      print '.'
      attempts += 1
    end
    puts 'done.'

    if deployed_vcl_version_number != new_version.number.to_s
      STDERR.puts "Verify failed. /vcl_version returned [#{deployed_vcl_version_number}].".red
      exit 1
    end
  end

  if purge_all
    puts 'Purging all...'
    service.purge_all
  end
  puts 'Deployment complete.'.green
end
get_includes(main_vcl_path, includes_dir) click to toggle source
# File lib/detect_includes.rb, line 1
def get_includes(main_vcl_path, includes_dir)
  includes_found = []
  get_includes_for_vcl main_vcl_path, includes_dir, includes_found
  includes_found
end
get_includes_directly_in_vcl(vcl_path, includes_dir) click to toggle source
# File lib/detect_includes.rb, line 17
def get_includes_directly_in_vcl(vcl_path, includes_dir)
  # Using '$' for line ending is os dependent and fails w/windows line endings on linux
  include_pattern = /^include "(.*)";?[\r\n]+/
  File.readlines(vcl_path).select { |line| include_pattern.match(line) }
      .map { |line| include_pattern.match(line)[1] }
      .map { |vcl_file_name| File.join(includes_dir, vcl_file_name + '.vcl') }
end
get_includes_for_vcl(vcl_path, includes_dir, includes_found) click to toggle source
# File lib/detect_includes.rb, line 7
def get_includes_for_vcl(vcl_path, includes_dir, includes_found)
  direct_includes = get_includes_directly_in_vcl vcl_path, includes_dir
  direct_includes_not_already_found = direct_includes - includes_found

  direct_includes_not_already_found.map do |include_vcl|
    includes_found.push include_vcl
    get_includes_for_vcl include_vcl, includes_dir, includes_found
  end
end
inject_deploy_verify_code(vcl, version_num) click to toggle source
# File lib/methods.rb, line 92
def inject_deploy_verify_code(vcl, version_num)
  deploy_recv_vcl = <<-END
  # --------- DEPLOY VERIFY CHECK START ---------
  if (req.url == "/vcl_version") {
    error 902;
  }
  # --------- DEPLOY VERIFY CHECK END ---------
  END

  deploy_error_vcl = <<-END
  # --------- DEPLOY VERIFY CHECK START ---------
  if (obj.status == 902) {
    set obj.status = 200;
    set obj.response = "OK";
    synthetic "#{version_num}";
    return(deliver);
  }
  # --------- DEPLOY VERIFY CHECK END ---------
  END

  vcl.gsub(/#7D_DEPLOY recv/, deploy_recv_vcl)
     .gsub(/#7D_DEPLOY error/, deploy_error_vcl)
end
inject_service_id(vcl_contents, service_id) click to toggle source
# File lib/methods.rb, line 116
def inject_service_id(vcl_contents, service_id)
  vcl_contents.gsub(/#7D_FASTLY_SERVICE_ID/, service_id)
end
upload_include_vcl(version, vcl_path, service_id) click to toggle source
# File lib/methods.rb, line 76
def upload_include_vcl(version, vcl_path, service_id)
  vcl_name = File.basename(vcl_path, '.vcl')
  upload_vcl version, vcl_path, vcl_name, service_id
end
upload_main_vcl(version, vcl_path, service_id) click to toggle source
# File lib/methods.rb, line 69
def upload_main_vcl(version, vcl_path, service_id)
  vcl_name = File.basename(vcl_path, '.vcl')
  can_verify_deployment = upload_vcl version, vcl_path, vcl_name, service_id
  version.vcl(vcl_name).set_main!
  can_verify_deployment
end
upload_vcl(version, vcl_path, name, service_id) click to toggle source
# File lib/methods.rb, line 81
def upload_vcl(version, vcl_path, name, service_id)
  vcl_contents_from_file = File.read(vcl_path)
  vcl_contents_with_service_id_injection = inject_service_id vcl_contents_from_file, service_id
  vcl_contents_with_deploy_injection = inject_deploy_verify_code(vcl_contents_with_service_id_injection, version.number)

  puts "Uploading #{name}"
  version.upload_vcl name, vcl_contents_with_deploy_injection

  vcl_contents_with_deploy_injection != vcl_contents_with_service_id_injection
end
validate(version) click to toggle source
# File lib/methods.rb, line 120
def validate(version)
  path = version.class.get_path(version.service_id, version.number)
  response = version.fetcher.client.get("#{path}/validate")
  status = response['status']
  raise response['msg'] if status != 'ok'
end