Skip to content

Commit

Permalink
Create predeploy rake task to handle Theme Editor
Browse files Browse the repository at this point in the history
closes: CNVS-21990

This is the rake task we call from a job server
once that has new code before restarting all the
app servers.  It will make sure that our s3 bucket
has all static assets including the css for custom
themes people have created in the Theme Editor

test plan:

* make sure you have an config/canvas_cdn.yml file
* `rm -rf public/dist`
* run `bundle exec rake brand_configs:generate_and_upload_all`
* in log/development.log you should see it write
  a _variables.scss file for each brand_config
  in any shard to disk, compile the css for each
  of those brands and push all the js/css/images/etc to s3
* browse pages in canvas, the css/images/js
  should be served from your "host:" configured
  in canvas_cdn.yml and none should 404

this change also includes:

better error message when brandable_css manifest doesn't exist

if the manifest file can't be found, this will
tell you the full path to the file it was looking
for so that if it can't find the manifest file,it
will tell you the path to the file it was looking for.

test plan:

with RAILS_ENV=production:

* rm -rf public/dist
* try to access a page

the error that it give you should tell you the
full path to the json file it tried to read.

if we haven't loaded rails yet, we still want
to look at RAILS_ENV to get which style of css
to generate

Change-Id: I2dbb12541d6a28e90a326a51f0cddb90f313842f
Reviewed-on: https://gerrit.instructure.com/58809
Reviewed-by: Rob Orton <[email protected]>
QA-Review: Jeremy Putnam <[email protected]>
Tested-by: Jenkins
Product-Review: Ryan Shaw <[email protected]>
  • Loading branch information
ryankshaw committed Jul 20, 2015
1 parent 0ba6eab commit 1ef1549
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
11 changes: 11 additions & 0 deletions app/models/brand_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,15 @@ def self.destroy_if_unused(md5)
end
end

def self.clean_unused_from_db!
BrandConfig.
where("NOT EXISTS (?)", Account.where("brand_config_md5=brand_configs.md5")).
where('NOT share').
# When someone is actively working in the theme editor, it just saves one
# in their session, so only delete stuff that is more than a week old,
# to not clear out a theme someone was working on.
where(["created_at < ?", 1.week.ago]).
delete_all
end

end
6 changes: 4 additions & 2 deletions lib/brandable_css.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ module BrandableCSS
APP_ROOT = defined?(Rails) && Rails.root || Pathname.pwd
CONFIG = YAML.load_file(APP_ROOT.join('config/brandable_css.yml')).freeze
BRANDABLE_VARIABLES = JSON.parse(File.read(APP_ROOT.join(CONFIG['paths']['brandable_variables_json']))).freeze
SASS_STYLE = (ENV['SASS_STYLE'] || ((defined?(Rails) && Rails.env.production?) ? 'compressed' : 'nested')).freeze

use_compressed = (defined?(Rails) && Rails.env.production?) || (ENV['RAILS_ENV'] == 'production')
SASS_STYLE = ENV['SASS_STYLE'] || ((use_compressed ? 'compressed' : 'nested')).freeze

class << self
def variables_map
Expand Down Expand Up @@ -50,7 +52,7 @@ def combined_checksums
memo[k] = v['combinedChecksum']
end.freeze
elsif defined?(Rails) && Rails.env.production?
raise "you need to run #{cli} before you can serve css."
raise "#{file.expand_path} does not exist. You need to run #{cli} before you can serve css."
else
# for dev/test there might be cases where you don't want it to raise an exception
# if you haven't ran `brandable_css` and the manifest file doesn't exist yet.
Expand Down
21 changes: 20 additions & 1 deletion lib/tasks/brand_configs.rake
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
require 'lib/brandable_css'

namespace :brand_configs do
desc "Write _brand_variable.scss to disk so canvas_css can render stylesheets for that branding. " +
"Set BRAND_CONFIG_MD5=<whatever> to save just that one, otherwise writes a file for each BrandConfig in db."
task :write => :environment do
if md5 = ENV['BRAND_CONFIG_MD5']
BrandConfig.find(md5).save_scss_file!
else
Rake::Task['brand_configs:clean'].invoke
BrandConfig.clean_unused_from_db!
BrandConfig.find_each(&:save_scss_file!)
end
end
Switchman::Rake.shardify_task('brand_configs:write')

desc "Remove all Brand Variable scss files"
task :clean do
rm_rf BrandableCSS.branded_scss_folder
end

# This is the rake task we call from a job server that has new code,
# before restarting all the app servers. It will make sure that our s3
# bucket has all static assets including the css for custom themes people
# have created in the Theme Editor.
desc "generate all brands and upload everything to s3"
task :generate_and_upload_all do
Rake::Task['brand_configs:clean'].invoke
Rake::Task['brand_configs:write'].invoke

# This'll pick up on all those written brand_configs and compile their css.
BrandableCSS.compile_all!

Rake::Task['canvas:cdn:upload_to_s3'].invoke
end

end

0 comments on commit 1ef1549

Please sign in to comment.