namespace :tidy do desc "Makes application pristine." task :all, [:stage] => [:validate, :app_stop, :vars, :git, :db, :cache, :restart, :app_start] do |t, args| # Executes all tasks end ACCEPTED_ENVIRONMENTS = ['test', 'sandbox', 'integration'] # Prevents mistakes. Raises an error for stages not test or sandbox. # All tidy tasks should :validate as a task dependency. task :validate, [:stage] => [:environment] do |t, args| if (args.stage && !ACCEPTED_ENVIRONMENTS.include?(args.stage)) || !ACCEPTED_ENVIRONMENTS.include?(STAGE) raise "ERROR: tasks are meant for 'test' and 'sandbox' ONLY." end end desc "Deploys the master branch to a remote application." task :git, [:stage] => [:validate] do |t, args| app = TidyReset.app_name(args.stage) # Ensure the remote is set TidyReset.execute("if test ! -n \"$(git remote -v | grep -E '^#{args.stage}')\"; then git remote add #{args.stage} git@heroku.com:#{app}.git; fi") if ENV['GIT_REF'] == 'none' # show ref on heroku remote TidyReset.execute("git ls-remote #{args.stage} | grep HEAD") else ref = ENV['GIT_REF'] || 'master' TidyReset.execute("git checkout #{ref}") # creates a local branch that tracks the remote's ref, if it doesn't already exist TidyReset.execute("git push -f #{args.stage} #{ref}:master") end end desc "Enables maintenance mode. Scales all dynos to 0." task :app_stop, [:stage] => [:validate] do |t, args| TidyReset.app_stop(args.stage) end desc "Disables maintenance mode. Scales all dynos to configuration." task :app_start, [:stage] => [:validate] do |t, args| TidyReset.app_start(args.stage) end desc "Sets default environment variables." task :vars, [:stage] => [:validate] do |t, args| app = TidyReset.app_name(args.stage) yaml_config = YAML.load_file("#{Rails.root}/config/tidy/vars.yml") set_vars_command = yaml_config[args.stage].inject("") { |command, config| command += "#{config.first}='#{config.last}' " } TidyReset.execute("heroku config:set #{set_vars_command} --app #{app}") end desc "Purges remotely configured databases, runs migrations, and creates db seeds." task :db, [:stage] => [:validate] do |t, args| app = TidyReset.app_name(args.stage) TidyReset.execute("heroku run rake tidy:db:purge --app #{app}") TidyReset.execute("heroku run rake tidy:db:migrate --app #{app}") TidyReset.execute("heroku run rake tidy:db:seed --app #{app}") end namespace :db do # Purges the locally configured databases. Should be called from tidy:db. task :purge => [:validate, :environment] do |t, args| TidyReset.configuration.databases.each do |database| TidyReset.database_purge(database) puts "Tidy task done." end end task :migrate => [:validate, :environment] do |t, args| Rake::Task['db:migrate'].invoke puts "Tidy task done." end task :seed => [:validate, :environment] do |t, args| Rake::Task['db:seed'].invoke puts "Tidy task done." end end desc "Clears the Rails.cache on a remote application." task :cache, [:stage] => [:validate] do |t, args| app = TidyReset.app_name(args.stage) TidyReset.execute("heroku run rake tidy:cache_local --app #{app}") end # Clear's a local Rails cache. Should be called by tidy:cache. task :cache_local => [:validate, :environment] do Rails.cache.clear end desc "Restarts Heroku dynos for a remote application." task :restart, [:stage] => [:validate] do |t, args| app = TidyReset.app_name(args.stage) TidyReset.execute("heroku restart --app #{app}") end end