├── .rspec ├── .ruby-version ├── Gemfile ├── Gemfile.lock ├── LICENSE ├── README.md ├── ab_tests └── ab_tests.yaml ├── deploy_vcl ├── fastly.yaml ├── jenkins.sh ├── lib └── render_template.rb ├── spec ├── render_template_spec.rb └── spec_helper.rb └── vcl_templates ├── .gitkeep └── test.vcl.erb /.rspec: -------------------------------------------------------------------------------- 1 | --color 2 | --format progress 3 | -------------------------------------------------------------------------------- /.ruby-version: -------------------------------------------------------------------------------- 1 | 2.4.2 2 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | gem "colorize", "0.5.8" 4 | gem "diffy", "3.0.1" 5 | gem "fastly", "~> 1.4" 6 | gem "rspec", "~> 2.14.1" 7 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | colorize (0.5.8) 5 | diff-lcs (1.2.5) 6 | diffy (3.0.1) 7 | fastly (1.4.2) 8 | rspec (2.14.1) 9 | rspec-core (~> 2.14.0) 10 | rspec-expectations (~> 2.14.0) 11 | rspec-mocks (~> 2.14.0) 12 | rspec-core (2.14.8) 13 | rspec-expectations (2.14.5) 14 | diff-lcs (>= 1.1.3, < 2.0) 15 | rspec-mocks (2.14.6) 16 | 17 | PLATFORMS 18 | ruby 19 | 20 | DEPENDENCIES 21 | colorize (= 0.5.8) 22 | diffy (= 3.0.1) 23 | fastly (~> 1.4) 24 | rspec (~> 2.14.1) 25 | 26 | BUNDLED WITH 27 | 1.14.5 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Crown Copyright (Government Digital Service) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Fastly Configure 2 | 3 | A utility to configure the [Fastly CDN](https://fastly.com) from version-controllable VCL and 4 | YAML files, using Fastly's [API](https://docs.fastly.com/api/). 5 | 6 | ## Prerequisites 7 | 8 | To run Fastly Configure, you'll need: 9 | 10 | - Ruby 2.1.8 or later 11 | - Bundler 1.5.3 or later 12 | - a Fastly account 13 | 14 | The ability to upload custom VCL is not turned on for accounts by default. 15 | To enable it, contact Fastly's support team. 16 | 17 | ## Usage 18 | 19 | ### Installing 20 | 21 | This script makes use of a few third-party Gems. Before running this script, 22 | you'll need to install the Gems listed in the Gemfile. The easiest way to do 23 | this is with Bundler: 24 | 25 | ``` 26 | bundle install 27 | ``` 28 | 29 | ### Authentication 30 | 31 | You need to set two environment variables to help this script connect to the 32 | Fastly API: 33 | 34 | - FASTLY_USER 35 | - FASTLY_PASS 36 | 37 | Due to API restrictions, you can't currently use an account that has 38 | two-factor authentication enabled with this script. 39 | 40 | ### Service configuration 41 | 42 | Service configuration is passed in to the script using a YAML file, which 43 | must live at `fastly.yaml`. You can find an example in `fastly.yaml` in this 44 | repository. Additional configuration parameters can also be set in this 45 | file to enable the use of ERB look-ups in the VCL. 46 | 47 | ### VCL 48 | 49 | Your VCL script should be stored as an ERB template in `/vcl_templates`. The 50 | VCL file should be named the same as the `configuration`. 51 | 52 | ## Contributing 53 | 54 | 1. Fork it. 55 | 2. Create a feature branch. 56 | 3. Commit your changes. 57 | 4. Push to the new branch. 58 | 5. Create a pull request. 59 | -------------------------------------------------------------------------------- /ab_tests/ab_tests.yaml: -------------------------------------------------------------------------------- 1 | # List the variants for your AB or multivariate test here. 2 | # Please leave the 'Example' test config in place. 3 | --- 4 | - Example: 5 | - A 6 | - B 7 | - ViewDrivingLicence: 8 | - A 9 | - B 10 | - RelatedLinksAATest: 11 | - A 12 | - B 13 | -------------------------------------------------------------------------------- /deploy_vcl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require 'diffy' 4 | require 'erb' 5 | require 'fastly' 6 | require 'yaml' 7 | 8 | require_relative './lib/render_template' 9 | 10 | CONFIGS = YAML.load_file(File.join(__dir__, "fastly.yaml")) 11 | 12 | def get_config(args) 13 | raise "Usage: #{$0} " unless args.size == 2 14 | 15 | configuration = args[0] 16 | environment = args[1] 17 | config_hash = CONFIGS[configuration][environment] rescue nil 18 | 19 | raise "ERROR: Unknown configuration/environment combination. Check this combination exists in fastly.yaml." unless config_hash 20 | 21 | return configuration, environment, config_hash 22 | end 23 | 24 | def get_git_version 25 | ref = %x{git describe --always}.chomp 26 | ref = "unknown" if ref.empty? 27 | 28 | ref 29 | end 30 | 31 | def get_dev_version(configuration) 32 | # Sometimes the latest version isn't the development version. 33 | version = configuration.version 34 | version = version.clone if version.active? 35 | 36 | version 37 | end 38 | 39 | def delete_ui_objects(service_id, version_number) 40 | # Delete objects created by the UI. We want VCL to be the source of truth. 41 | # Most of these don't have real objects in the Fastly API gem. 42 | to_delete = %w{backend healthcheck cache_settings request_settings response_object header gzip} 43 | to_delete.each do |type| 44 | type_path = "/service/#{service_id}/version/#{version_number}/#{type}" 45 | @f.client.get(type_path).map{ |i| i["name"] }.each do |name| 46 | puts "Deleting #{type}: #{name}" 47 | resp = @f.client.delete("#{type_path}/#{ERB::Util.url_encode(name)}") 48 | raise 'ERROR: Failed to delete configuration' unless resp 49 | end 50 | end 51 | end 52 | 53 | def modify_settings(version, ttl) 54 | settings = version.settings 55 | settings.settings.update({ 56 | "general.default_host" => "", 57 | "general.default_ttl" => ttl, 58 | }) 59 | settings.save! 60 | end 61 | 62 | def upload_vcl(version, contents) 63 | vcl_name = 'main' 64 | 65 | begin 66 | version.vcl(vcl_name) && version.delete_vcl(vcl_name) 67 | rescue Fastly::Error 68 | end 69 | 70 | vcl = version.upload_vcl(vcl_name, contents) 71 | @f.client.put(Fastly::VCL.put_path(vcl) + '/main') 72 | end 73 | 74 | def diff_vcl(configuration, version_new) 75 | version_current = configuration.versions.find { |v| v.active? } 76 | 77 | if version_current.nil? 78 | raise 'There are no active versions of this configuration' 79 | end 80 | 81 | diff = Diffy::Diff.new( 82 | version_current.generated_vcl.content, 83 | version_new.generated_vcl.content, 84 | :context => 3 85 | ) 86 | 87 | puts "Diff versions: #{version_current.number} -> #{version_new.number}" 88 | puts diff.to_s(:color) 89 | end 90 | 91 | def validate_config(version) 92 | # version.validate doesn't return the right thing. 93 | valid_hash = @f.client.get(Fastly::Version.put_path(version) + '/validate') 94 | unless valid_hash.fetch('status') == "ok" 95 | raise "ERROR: Invalid configuration:\n" + valid_hash.fetch('msg') 96 | end 97 | end 98 | 99 | configuration, environment, config = get_config(ARGV) 100 | 101 | ['FASTLY_USER', 'FASTLY_PASS'].each do |envvar| 102 | if ENV[envvar].nil? 103 | raise "#{envvar} is not set in the environment" 104 | end 105 | end 106 | 107 | username = ENV['FASTLY_USER'] 108 | password = ENV['FASTLY_PASS'] 109 | 110 | @f = Fastly.new({ :user => username, :password => password }) 111 | config['git_version'] = get_git_version 112 | 113 | service = @f.get_service(config['service_id']) 114 | version = get_dev_version(service) 115 | puts "Current version: #{version.number}" 116 | puts "Configuration: #{configuration}" 117 | puts "Environment: #{environment}" 118 | 119 | vcl = RenderTemplate.render_template(configuration, environment, config, version) 120 | delete_ui_objects(service.id, version.number) 121 | upload_vcl(version, vcl) 122 | diff_vcl(service, version) 123 | 124 | modify_settings(version, config['default_ttl']) 125 | 126 | validate_config(version) 127 | version.activate! 128 | -------------------------------------------------------------------------------- /fastly.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | # Example configuration file 3 | # 4 | # Usage: 5 | # configuration: 6 | # environment: 7 | # key: a1b2c3d4e5 8 | # 9 | # configuration, environment and key are all configurable. 10 | # For instance, configuration will be the name of the service. 11 | # Environment would be the specific service to apply to. 12 | # Keys are passed to the API. 13 | 14 | api: 15 | production: 16 | service_id: b9aj2as9f8j2ads2u 17 | staging: 18 | service_id: e88819794e9uasfsk 19 | preview: 20 | service_id: 97skasfiasy2j9dsm 21 | 22 | frontend: 23 | production: 24 | service_id: 2817hasu29jasgu29 25 | staging: 26 | service_id: asj298asgy2j9asdf 27 | preview: 28 | service_id: 9uask39agyhskasoj 29 | -------------------------------------------------------------------------------- /jenkins.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | 5 | git clone 'git@github.com:alphagov/cdn-configs.git' 6 | 7 | cp cdn-configs/fastly/fastly.yaml . 8 | 9 | git clone 'git@github.com:alphagov/govuk-cdn-config.git' 10 | 11 | cp govuk-cdn-config/vcl_templates/*.vcl.erb vcl_templates/ 12 | 13 | bundle install --path "${HOME}/bundles/${JOB_NAME}" 14 | bundle exec ./deploy_vcl ${vhost} ${ENVIRONMENT} 15 | -------------------------------------------------------------------------------- /lib/render_template.rb: -------------------------------------------------------------------------------- 1 | require 'yaml' 2 | 3 | class RenderTemplate 4 | def self.render_template(configuration, environment, config, version) 5 | # Both config and ab_tests are used inside the vcl.erb template 6 | vcl_file = File.join(File.dirname(__FILE__), '..', 'vcl_templates', "#{configuration}.vcl.erb") 7 | ab_tests = YAML.load_file(File.join(__dir__, '..', 'ab_tests', 'ab_tests.yaml')) 8 | ERB.new(File.read(vcl_file), nil, '-').result(binding) 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /spec/render_template_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | require_relative './../lib/render_template' 3 | 4 | RSpec.describe RenderTemplate do 5 | describe ".render_template" do 6 | it "renders the ERB template" do 7 | result = RenderTemplate.render_template("test", {}, "my_variable", {}) 8 | 9 | expect(result).to eql("my_variable\n") 10 | end 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # This file was generated by the `rspec --init` command. Conventionally, all 2 | # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. 3 | # Require this file using `require "spec_helper"` to ensure that it is only 4 | # loaded once. 5 | # 6 | # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration 7 | RSpec.configure do |config| 8 | config.treat_symbols_as_metadata_keys_with_true_values = true 9 | config.run_all_when_everything_filtered = true 10 | config.filter_run :focus 11 | 12 | # Run specs in random order to surface order dependencies. If you find an 13 | # order dependency and want to debug it, you can fix the order by providing 14 | # the seed, which is printed after each run. 15 | # --seed 1234 16 | config.order = 'random' 17 | end 18 | -------------------------------------------------------------------------------- /vcl_templates/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alphagov/fastly-configure/5ef705da6c559cdfa7b740f4bd9454e2143f239b/vcl_templates/.gitkeep -------------------------------------------------------------------------------- /vcl_templates/test.vcl.erb: -------------------------------------------------------------------------------- 1 | <%= config %> 2 | --------------------------------------------------------------------------------