inspec/www/Rakefile
Adam Leff d022faea81
Add rake task for flushing Fastly cache
inspec.io is now hosted by Fastly which operates as a caching reverse
proxy. This change adds a Rake task that will flush the cache at Fastly
if the API key and Service ID are set in the appropriate environment
variables.

Flushing the cache is not required, so this will not error out if the
environment variables aren't provided; the cache will eventually clear
on its own. This will simply speed along the process as needed.

Signed-off-by: Adam Leff <adam@leff.co>
2017-04-28 14:22:00 -04:00

218 lines
7 KiB
Ruby

#!/usr/bin/env rake
# encoding: utf-8
# Copyright:: Copyright (c) 2015 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
$LOAD_PATH.unshift(File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib'))
require 'uri'
require 'net/http'
require 'fastly'
require_relative '../tasks/docs.rb'
require_relative '../tasks/shared.rb'
task :default do
puts 'There is no default task - see `rake --tasks` for available www-related tasks.'
puts 'Run `rake www` to do a full website release.'
exit(1)
end
namespace :www do
desc 'Builds the tutorial contents'
task :tutorial do
Log.section 'Build the online tutorial in tutorial/'
sh('cd tutorial/ && npm install')
sh('cd tutorial/ && gulp build')
Verify.file('tutorial/dist/index.html')
Verify.file('tutorial/dist/css/inspec_tutorial.css')
Verify.file('tutorial/dist/scripts/inspec_tutorial.js')
Verify.file('tutorial/dist/dist/inspec_tutorial.js')
end
desc 'Builds the middleman site'
task :site do
Log.section 'Build middleman project'
Bundler.with_clean_env {
sh('bundle install && bundle exec middleman build')
}
Verify.file('build/index.html')
Verify.file('build/javascripts/all.js')
Verify.file('build/stylesheets/site.css')
end
desc 'Assemble the website site from middleman and the tutorial'
task :assemble do
Log.section 'Copy only tutorial into middleman build directory'
sh('rsync -a --exclude=index.html tutorial/dist/* build/')
sh('cp tutorial/dist/index.html build/tutorial.html')
end
desc 'Builds the full site locally'
task build: ['www:tutorial', 'www:site', 'www:assemble']
task :clean do
dst = 'build'
FileUtils.rm_rf(dst) if File.directory?(dst)
end
desc 'Releases the site to gh-pages'
task :release do
# This folder contains the built files
dst = 'build'
unless File.directory?(dst) && File.file?(File.join(dst, 'index.html'))
puts 'It looks like you have not built the site yet. Calling rake www:build'
Rake::Task['www:build'].invoke
end
unless File.directory?(dst) && File.file?(File.join(dst, 'index.html'))
raise 'It looks like the site was not build. Aborting.'
end
# check if git exists
sh('command -v git >/dev/null 2>&1') ||
raise("It looks like `git` isn't installed. It is required to run this build task.")
unless sh('git diff-index --quiet HEAD --')
raise 'Please make sure you have no uncommitted changes in this repository.'
end
File.write(File.join(dst, 'CNAME'), 'origin.inspec.io')
file_count = Dir[File.join(dst, '*')].length
file_size = `du -hs #{dst}`.sub(/\s+.*$/m, '')
if system('git rev-parse --verify gh-pages')
Log.info 'Remove local gh-pages branch'
sh('git branch -D gh-pages')
end
current_branch = `git rev-parse --abbrev-ref HEAD`.strip
if current_branch.empty?
raise 'Cannot determine current branch to go back to! Aborting.'
end
Log.info 'Create empty gh-pages branch'
sh('git checkout --orphan gh-pages')
# this rest of this task needs to be run from the root of the inspec repo
# so it can properly move and clean files in the gh-pages branch
Dir.chdir(File.join(Dir.pwd, '..')) do
dst_from_root = File.join('www', dst)
Log.info 'Clear out all local git files!'
sh('git rm -rf .')
Log.info "Add the built files in #{dst_from_root}"
sh("git add #{dst_from_root}")
Log.info 'Remove all other files in this empty branch'
sh('git clean -df')
Log.info 'Move the site to the root directory'
sh("git mv #{File.join(dst_from_root, '*')} .")
Log.info 'Commit to gh-pages'
sh("git commit -m 'website update'")
require 'inquirer'
if Ask.confirm("Ready to go, I have #{file_count} files at #{file_size}. "\
'Do you want to push this live?', default: false)
Log.info 'push to origin, this may take a moment'
sh('git push -u origin --force-with-lease gh-pages')
else
puts 'Aborted.'
end
end
sh("git checkout #{current_branch}")
end
desc 'Release the site to the Netlify acceptance environment'
task :acceptance do
raise 'NETLIFYKEY environment variable not set' unless ENV.key?('NETLIFYKEY')
Log.info 'Cleaning the existing build directory'
Rake::Task['www:clean'].invoke
Log.info 'Building the docs'
Rake::Task['docs'].invoke
Log.info 'Building the site'
Rake::Task['www:build'].invoke
Log.info 'Creating zip file of website contents'
Dir.chdir(File.join(Dir.pwd, 'build')) do
sh('zip -r inspec-acceptance.zip *')
end
Log.info 'Uploading to Netlify'
uri = URI.parse('https://api.netlify.com/api/v1/sites/inspec-acceptance.netlify.com/deploys')
request = Net::HTTP::Post.new(uri)
request['Content-Type'] = 'application/zip'
request['Authorization'] = "Bearer #{ENV['NETLIFYKEY']}"
request.body = File.read(File.join(Dir.pwd, 'build', 'inspec-acceptance.zip'), mode: 'rb')
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.request(request)
end
raise "Failed to upload to Netlify: #{response.code} -- #{response.body}" unless response.code == '200'
Log.info 'Removing zip file'
File.unlink(File.join(Dir.pwd, 'build', 'inspec-acceptance.zip'))
end
desc 'Flush the inspec.io cache at Fastly'
task :flush do
api_key = ENV['FASTLY_API_KEY']
service_id = ENV['FASTLY_SERVICE_ID']
if api_key.nil? || service_id.nil?
puts ''
puts 'WARNING: could not flush the Fastly cache for inspec.io.'
puts 'Please set FASTLY_API_KEY and FASTLY_SERVICE_ID environment variables'
puts 'and run `rake www:flush`.'
puts ''
next
end
Log.info 'Flushing the Fastly cache'
begin
fastly_client = Fastly.new(api_key: api_key)
fastly_service = Fastly::Service.new({ id: service_id }, fastly_client)
purge_result = fastly_service.purge_all
rescue => e
puts "ERROR: Fastly cache could not be purged: #{e.class} - #{e.message}"
next
end
if purge_result['status'] == 'ok'
puts 'Success! The Fastly cache is purged.'
else
puts "ERROR: Fastly cache could not be purged: #{purge_result}"
end
end
end
desc 'Full website release: includes local clean, docs build, site build, and then release'
task :www do
Rake::Task['www:clean'].invoke
Rake::Task['docs'].invoke
Rake::Task['www:build'].invoke
Rake::Task['www:release'].invoke
Rake::Task['www:flush'].invoke
end