2017-03-22 15:58:12 +00:00
|
|
|
#!/usr/bin/env rake
|
2016-09-22 19:26:19 +00:00
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
$LOAD_PATH.unshift(File.join(File.expand_path(File.dirname(__FILE__)), "..", "lib"))
|
2017-03-22 15:58:12 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
require "uri"
|
|
|
|
require "net/http"
|
|
|
|
require "fastly"
|
2017-04-05 22:30:00 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
require_relative "../tasks/docs.rb"
|
|
|
|
require_relative "../tasks/shared.rb"
|
2017-03-22 15:58:12 +00:00
|
|
|
|
|
|
|
task :default do
|
2019-06-11 22:24:35 +00:00
|
|
|
puts "There is no default task - see `rake --tasks` for available www-related tasks."
|
|
|
|
puts "Run `rake www` to do a full website release."
|
2017-03-22 15:58:12 +00:00
|
|
|
exit(1)
|
|
|
|
end
|
2016-09-22 19:26:19 +00:00
|
|
|
|
2017-11-21 07:49:41 +00:00
|
|
|
namespace :www do # rubocop:disable Metrics/BlockLength
|
2019-05-30 21:44:24 +00:00
|
|
|
|
|
|
|
task :accept_license do
|
2019-06-11 22:24:35 +00:00
|
|
|
FileUtils.mkdir_p(File.join(Dir.home, ".chef", "accepted_licenses"))
|
2019-05-30 21:44:24 +00:00
|
|
|
# If the user has not accepted the license, touch the acceptance
|
|
|
|
# file, but also touch a marker that it is only for testing.
|
2019-06-11 22:24:35 +00:00
|
|
|
unless File.exist?(File.join(Dir.home, ".chef", "accepted_licenses", "inspec"))
|
2019-05-30 21:44:24 +00:00
|
|
|
puts "\n\nTemporarily accepting Chef user license for the duration of testing...\n"
|
2019-06-11 22:24:35 +00:00
|
|
|
FileUtils.touch(File.join(Dir.home, ".chef", "accepted_licenses", "inspec"))
|
|
|
|
FileUtils.touch(File.join(Dir.home, ".chef", "accepted_licenses", "inspec.for_testing"))
|
2019-05-30 21:44:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Regardless of what happens, when this process exits, check for cleanup.
|
|
|
|
at_exit do
|
2019-06-11 22:24:35 +00:00
|
|
|
if File.exist?(File.join(Dir.home, ".chef", "accepted_licenses", "inspec.for_testing"))
|
2019-05-30 21:44:24 +00:00
|
|
|
puts "\n\nRemoving temporary Chef user license acceptance file that was placed for test duration.\n"
|
2019-06-11 22:24:35 +00:00
|
|
|
FileUtils.rm_f(File.join(Dir.home, ".chef", "accepted_licenses", "inspec"))
|
|
|
|
FileUtils.rm_f(File.join(Dir.home, ".chef", "accepted_licenses", "inspec.for_testing"))
|
2019-05-30 21:44:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
desc "Builds the middleman site"
|
2016-09-23 13:26:22 +00:00
|
|
|
task :site do
|
2019-06-11 22:24:35 +00:00
|
|
|
Log.section "Build middleman project"
|
|
|
|
Bundler.with_clean_env do
|
|
|
|
sh("bundle install && bundle exec middleman build")
|
|
|
|
end
|
|
|
|
Verify.file("build/index.html")
|
|
|
|
Verify.file("build/javascripts/all.js")
|
|
|
|
Verify.file("build/stylesheets/site.css")
|
2016-09-22 19:26:19 +00:00
|
|
|
end
|
2019-05-31 05:48:27 +00:00
|
|
|
task site: [:accept_license]
|
2016-09-22 19:26:19 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
desc "Assemble the website site from middleman"
|
2016-09-23 13:26:22 +00:00
|
|
|
task :assemble do
|
2019-06-11 22:24:35 +00:00
|
|
|
Log.section "Copy only tutorial into middleman build directory"
|
|
|
|
sh("rsync -a --exclude=index.html build/")
|
2016-09-23 13:26:22 +00:00
|
|
|
end
|
2019-05-31 05:48:27 +00:00
|
|
|
task assemble: [:accept_license]
|
2016-09-23 13:26:22 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
desc "Builds the full site locally"
|
|
|
|
task build: ["www:site", "www:assemble"]
|
2019-05-31 05:48:27 +00:00
|
|
|
task build: [:accept_license]
|
2016-09-23 13:26:22 +00:00
|
|
|
|
2016-09-26 04:25:49 +00:00
|
|
|
task :clean do
|
2019-06-11 22:24:35 +00:00
|
|
|
dst = "build"
|
2016-09-26 04:25:49 +00:00
|
|
|
FileUtils.rm_rf(dst) if File.directory?(dst)
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
desc "Releases the site to gh-pages"
|
2016-09-22 19:26:19 +00:00
|
|
|
task :release do
|
|
|
|
# This folder contains the built files
|
2019-06-11 22:24:35 +00:00
|
|
|
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
|
2016-09-22 19:26:19 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
unless File.directory?(dst) && File.file?(File.join(dst, "index.html"))
|
|
|
|
raise "It looks like the site was not build. Aborting."
|
2016-09-22 19:26:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# check if git exists
|
2019-06-11 22:24:35 +00:00
|
|
|
sh("command -v git >/dev/null 2>&1") ||
|
2017-02-08 22:49:16 +00:00
|
|
|
raise("It looks like `git` isn't installed. It is required to run this build task.")
|
2016-09-22 19:26:19 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
unless sh("git diff-index --quiet HEAD --")
|
|
|
|
raise "Please make sure you have no uncommitted changes in this repository."
|
2016-09-22 19:26:19 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
File.write(File.join(dst, "CNAME"), "origin.inspec.io")
|
|
|
|
file_count = Dir[File.join(dst, "*")].length
|
|
|
|
file_size = `du -hs #{dst}`.sub(/\s+.*$/m, "")
|
2016-09-22 19:26:19 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
if system("git rev-parse --verify gh-pages")
|
|
|
|
Log.info "Remove local gh-pages branch"
|
|
|
|
sh("git branch -D gh-pages")
|
2016-12-01 18:37:44 +00:00
|
|
|
end
|
2016-09-22 19:26:19 +00:00
|
|
|
|
|
|
|
current_branch = `git rev-parse --abbrev-ref HEAD`.strip
|
|
|
|
if current_branch.empty?
|
2019-06-11 22:24:35 +00:00
|
|
|
raise "Cannot determine current branch to go back to! Aborting."
|
2016-09-22 19:26:19 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
Log.info "Create empty gh-pages branch"
|
|
|
|
sh("git checkout --orphan gh-pages")
|
2016-09-22 19:26:19 +00:00
|
|
|
|
2017-03-30 18:27:18 +00:00
|
|
|
# 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
|
2019-06-11 22:24:35 +00:00
|
|
|
Dir.chdir(File.join(Dir.pwd, "..")) do
|
|
|
|
dst_from_root = File.join("www", dst)
|
2016-09-22 19:26:19 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
Log.info "Clear out all local git files!"
|
|
|
|
sh("git rm -rf .")
|
2016-09-22 19:26:19 +00:00
|
|
|
|
2017-03-30 18:27:18 +00:00
|
|
|
Log.info "Add the built files in #{dst_from_root}"
|
|
|
|
sh("git add #{dst_from_root}")
|
2016-09-22 19:26:19 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
Log.info "Remove all other files in this empty branch"
|
|
|
|
sh("git clean -df")
|
2016-09-22 19:26:19 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
Log.info "Move the site to the root directory"
|
2017-03-30 18:27:18 +00:00
|
|
|
sh("git mv #{File.join(dst_from_root, '*')} .")
|
2016-09-22 19:26:19 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
Log.info "Commit to gh-pages"
|
2017-03-30 18:27:18 +00:00
|
|
|
sh("git commit -m 'website update'")
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
require "inquirer"
|
2017-03-30 18:27:18 +00:00
|
|
|
if Ask.confirm("Ready to go, I have #{file_count} files at #{file_size}. "\
|
2019-06-11 22:24:35 +00:00
|
|
|
"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")
|
2017-03-30 18:27:18 +00:00
|
|
|
else
|
2019-06-11 22:24:35 +00:00
|
|
|
puts "Aborted."
|
2017-03-30 18:27:18 +00:00
|
|
|
end
|
2016-09-22 19:26:19 +00:00
|
|
|
end
|
|
|
|
|
2016-09-28 14:39:26 +00:00
|
|
|
sh("git checkout #{current_branch}")
|
2016-09-22 19:26:19 +00:00
|
|
|
end
|
2017-04-05 22:30:00 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
desc "Release the site to the Netlify acceptance environment"
|
2017-04-05 22:30:00 +00:00
|
|
|
task :acceptance do
|
2019-06-11 22:24:35 +00:00
|
|
|
raise "NETLIFYKEY environment variable not set" unless ENV.key?("NETLIFYKEY")
|
2017-04-05 22:30:00 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
Log.info "Cleaning the existing build directory"
|
|
|
|
Rake::Task["www:clean"].invoke
|
2017-04-05 22:30:00 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
Log.info "Building the docs"
|
|
|
|
Rake::Task["docs"].invoke
|
2017-04-05 22:30:00 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
Log.info "Building the site"
|
|
|
|
Rake::Task["www:build"].invoke
|
2017-04-05 22:30:00 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
Log.info "Creating zip file of website contents"
|
|
|
|
Dir.chdir(File.join(Dir.pwd, "build")) do
|
|
|
|
sh("zip -r inspec-acceptance.zip *")
|
2017-04-05 22:30:00 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
Log.info "Uploading to Netlify"
|
|
|
|
uri = URI.parse("https://api.netlify.com/api/v1/sites/inspec-acceptance.netlify.com/deploys")
|
2017-04-05 22:30:00 +00:00
|
|
|
request = Net::HTTP::Post.new(uri)
|
2019-06-11 22:24:35 +00:00
|
|
|
request["Content-Type"] = "application/zip"
|
|
|
|
request["Authorization"] = "Bearer #{ENV['NETLIFYKEY']}"
|
|
|
|
request.body = File.read(File.join(Dir.pwd, "build", "inspec-acceptance.zip"), mode: "rb")
|
2017-04-05 22:30:00 +00:00
|
|
|
|
|
|
|
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
|
|
|
|
http.request(request)
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
raise "Failed to upload to Netlify: #{response.code} -- #{response.body}" unless response.code == "200"
|
2017-04-05 22:30:00 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
Log.info "Removing zip file"
|
|
|
|
File.unlink(File.join(Dir.pwd, "build", "inspec-acceptance.zip"))
|
2017-04-05 22:30:00 +00:00
|
|
|
end
|
2017-04-28 16:18:20 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
desc "Flush the inspec.io cache at Fastly"
|
2017-04-28 16:18:20 +00:00
|
|
|
task :flush do
|
2019-06-11 22:24:35 +00:00
|
|
|
api_key = ENV["FASTLY_API_KEY"]
|
|
|
|
service_id = ENV["FASTLY_SERVICE_ID"]
|
2017-04-28 16:18:20 +00:00
|
|
|
|
|
|
|
if api_key.nil? || service_id.nil?
|
2019-06-11 22:24:35 +00:00
|
|
|
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 ""
|
2017-04-28 16:18:20 +00:00
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
Log.info "Flushing the Fastly cache"
|
2017-04-28 16:18:20 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
if purge_result["status"] == "ok"
|
|
|
|
puts "Success! The Fastly cache is purged."
|
2017-04-28 16:18:20 +00:00
|
|
|
else
|
|
|
|
puts "ERROR: Fastly cache could not be purged: #{purge_result}"
|
|
|
|
end
|
|
|
|
end
|
2016-09-22 19:26:19 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
desc "Full website release: includes local clean, docs build, site build, and then release"
|
2016-09-22 19:26:19 +00:00
|
|
|
task :www do
|
2019-06-11 22:24:35 +00:00
|
|
|
Rake::Task["www:clean"].invoke
|
|
|
|
Rake::Task["docs"].invoke
|
|
|
|
Rake::Task["www:build"].invoke
|
|
|
|
Rake::Task["www:release"].invoke
|
|
|
|
Rake::Task["www:flush"].invoke
|
2016-09-22 19:26:19 +00:00
|
|
|
end
|