Farmbot-Web-App/lib/tasks/api.rake

154 lines
4.8 KiB
Ruby
Raw Permalink Normal View History

2017-12-14 07:52:01 -07:00
def check_for_digests
Log
.where(sent_at: nil, created_at: 1.day.ago...Time.now)
.where(Log::IS_EMAIL_ISH)
.where
.not(Log::IS_FATAL_EMAIL)
.pluck(:device_id)
.uniq
.map do |id|
2019-04-19 15:07:47 -06:00
device = Device.find(id)
puts "Sending log digest to device \##{id} (#{device.name})"
LogDeliveryMailer.log_digest(device).deliver
end
sleep 10.minutes
end
2018-10-03 12:07:22 -06:00
def hard_reset_api
sh "sudo docker stop $(sudo docker ps -a -q)"
sh "sudo docker rm $(sudo docker ps -a -q)"
sh "sudo rm -rf docker_volumes/"
2018-10-03 12:07:22 -06:00
rescue => exception
puts exception.message
puts "Reset failed. Was there nothing to destroy?"
end
def rebuild_deps
sh "sudo docker-compose run web bundle install"
sh "sudo docker-compose run web npm install"
sh "sudo docker-compose run web bundle exec rails db:setup"
sh "sudo docker-compose run web rake keys:generate"
sh "sudo docker-compose run web npm run build"
end
def user_typed?(word)
STDOUT.flush
STDIN.gets.chomp.downcase.include?(word.downcase)
end
2017-10-10 16:27:28 -06:00
namespace :api do
2019-04-19 15:07:47 -06:00
desc "Runs pending email digests. " \
"Use the `FOREVER` ENV var to continually check."
task log_digest: :environment do
2019-01-24 07:07:11 -07:00
puts "Running log digest loop..."
ENV["FOREVER"] ? loop { check_for_digests } : check_for_digests
end
2019-01-30 07:00:26 -07:00
desc "Run Rails _ONLY_. No parcel."
task only: :environment do
2019-01-30 07:00:26 -07:00
sh "sudo docker-compose up --scale parcel=0"
end
2019-02-05 08:10:06 -07:00
def parcel(cmd, opts = " ")
2019-07-12 09:16:47 -06:00
intro = [
"NODE_ENV=#{Rails.env}",
"node_modules/parcel-bundler/bin/cli.js",
cmd,
DashboardController::PARCEL_ASSET_LIST,
"--out-dir",
DashboardController::PUBLIC_OUTPUT_DIR,
"--public-url",
DashboardController::OUTPUT_URL,
].join(" ")
2019-03-20 13:48:37 -06:00
sh [intro, opts].join(" ")
2019-02-05 08:10:06 -07:00
end
2019-07-14 17:53:22 -06:00
def clean_assets
# Clear out cache and previous builds on initial load.
sh [
"rm -rf",
DashboardController::CACHE_DIR,
DashboardController::PUBLIC_OUTPUT_DIR,
].join(" ") unless ENV["NO_CLEAN"]
end
2019-02-06 12:28:26 -07:00
desc "Serve javascript assets (via Parcel bundler)."
task serve_assets: :environment do
2019-07-14 17:53:22 -06:00
clean_assets
2019-02-05 08:10:06 -07:00
parcel "watch", DashboardController::PARCEL_HMR_OPTS
end
2019-02-06 12:28:26 -07:00
desc "Don't call this directly. Use `rake assets:precompile`."
2019-02-05 11:33:50 -07:00
task parcel_compile: :environment do
2019-02-05 08:10:06 -07:00
parcel "build"
end
desc "Clean out old demo accounts"
task clean_demo_accounts: :environment do
User
.where("email ILIKE '%@farmbot.guest%'")
.where("updated_at < ?", 1.hour.ago)
.destroy_all
end
2018-10-03 12:07:22 -06:00
desc "Reset _everything_, including your database"
task :reset do
2019-04-19 15:07:47 -06:00
puts "This is going to destroy _ALL_ of your local Farmbot SQL data and " \
"configs. Type 'destroy' to continue, enter to abort."
2018-10-03 12:07:22 -06:00
if user_typed?("destroy")
hard_reset_api
puts "Done. Type 'build' to re-install dependencies, enter to abort."
2018-10-03 12:07:22 -06:00
rebuild_deps if user_typed?("build")
end
end
2018-11-15 10:13:11 -07:00
2019-11-20 12:50:49 -07:00
RELEASES_URL = "https://api.github.com/repos/farmbot/farmbot_os/releases"
2019-04-19 15:07:47 -06:00
VERSION = "tag_name"
2018-11-15 10:13:11 -07:00
TIMESTAMP = "created_at"
2019-05-16 13:35:42 -06:00
PRERELEASE = "prerelease"
2018-11-15 10:13:11 -07:00
def deprecate!
2018-11-15 10:13:11 -07:00
# Get current version
2019-04-19 15:07:47 -06:00
version_str = GlobalConfig.dump.fetch("FBOS_END_OF_LIFE_VERSION")
2018-11-15 10:13:11 -07:00
# Convert it to Gem::Version for easy comparisons (>, <, ==, etc)
current_version = Gem::Version::new(version_str)
# 60 days is the current policy.
2019-04-19 15:07:47 -06:00
cutoff = 60.days.ago
2018-11-15 10:13:11 -07:00
# Download release data from github
2020-01-03 10:18:31 -07:00
string_page_1 = URI.open("#{RELEASES_URL}?per_page=100&page=1").read
string_page_2 = URI.open("#{RELEASES_URL}?per_page=100&page=2").read
2019-11-20 12:50:49 -07:00
data = JSON.parse(string_page_1).push(*JSON.parse(string_page_2))
2019-05-16 13:35:42 -06:00
.map { |x| x.slice(VERSION, TIMESTAMP, PRERELEASE) } # Only grab keys that matter
2019-04-19 15:07:47 -06:00
.reject { |x| x.fetch(VERSION).include?("-") } # Remove RC/Beta releases
2019-05-16 13:35:42 -06:00
.reject { |x| x.fetch(PRERELEASE) } # Remove pre-releases
2019-04-19 15:07:47 -06:00
.map do |x|
2018-11-15 10:13:11 -07:00
# Convert string-y version/timestamps to Real ObjectsTM
version = Gem::Version::new(x.fetch(VERSION).gsub("v", ""))
2019-04-19 15:07:47 -06:00
time = DateTime.parse(x.fetch(TIMESTAMP))
2018-11-15 10:13:11 -07:00
Pair.new(version, time)
end
2019-04-19 15:07:47 -06:00
.select do |pair|
2018-11-15 10:13:11 -07:00
# Grab versions that are > current version and outside of cutoff window
2019-06-11 13:42:58 -06:00
(pair.head >= current_version) && (pair.tail < cutoff)
2018-11-15 10:13:11 -07:00
end
2019-04-19 15:07:47 -06:00
.sort_by { |p| p.tail } # Sort by release date
2019-04-23 12:42:11 -06:00
.last(2) # Grab 2 latest versions (closest to cutoff)
2019-04-19 15:07:47 -06:00
.first # Give 'em some leeway, grabbing the 2nd most outdated version.
.try(:head) # We might already be up-to-date?
2018-11-15 10:13:11 -07:00
if data # ...or not
puts "Setting new support target to #{data.to_s}"
GlobalConfig # Set the new oldest support version.
.find_by(key: "FBOS_END_OF_LIFE_VERSION")
.update!(value: data.to_s)
2018-11-15 10:13:11 -07:00
end
end
desc "Deprecate old FBOS version, delete inactive accounts, etc.."
task tidy: :environment do
deprecate!
InactiveAccountJob.perform_later
end
2017-10-10 16:27:28 -06:00
end
2019-02-05 12:05:54 -07:00
Rake::Task["assets:precompile"].enhance ["api:parcel_compile"]