Fix farmwares not in repos getting synced

This commit is contained in:
Connor Rigby 2017-12-08 18:55:13 -08:00
parent a8d1a885d9
commit 129288418d
3 changed files with 50 additions and 33 deletions

View file

@ -43,13 +43,16 @@ config :farmbot, Farmbot.System.ConfigStorage,
database: "tmp/#{Farmbot.System.ConfigStorage}_dev.sqlite3",
pool_size: 1
# config :farmbot, :farmware, first_part_farmware_manifest_url: nil
# Configure Farmbot Behaviours.
# Default Authorization behaviour.
# SystemTasks for host mode.
config :farmbot, :behaviour,
authorization: Farmbot.Bootstrap.Authorization,
system_tasks: Farmbot.Host.SystemTasks,
update_handler: Farmbot.Host.UpdateHandler,
firmware_handler: Farmbot.Firmware.UartHandler
update_handler: Farmbot.Host.UpdateHandler
# firmware_handler: Farmbot.Firmware.UartHandler
config :farmbot, :uart_handler, tty: "/dev/ttyACM0"

View file

@ -43,33 +43,27 @@ defmodule Farmbot.Farmware.Installer do
end
@doc "Enable a repo from a url or struct."
def sync_repo(url_or_repo_struct, acc \\ [])
def sync_repo(url_or_repo_struct, success \\ [], fail \\ [])
def sync_repo(url, _acc) when is_binary(url) do
def sync_repo(url, [], []) when is_binary(url) do
Logger.busy 1, "Syncing repo from: #{url}"
with {:ok, %{status_code: code, body: body}} when code > 199 and code < 300 <- HTTP.get(url),
{:ok, json_map} <- Poison.decode(body),
{:ok, repo} <- Repository.new(json_map)
do
case sync_repo(repo, []) do
[] ->
Logger.success 1, "Successfully synced repo."
:ok
list_of_entries ->
Logger.error 1, "Failed to enable some entries: #{inspect list_of_entries}"
{:error, list_of_entries}
end
sync_repo(repo, [], [])
end
end
def sync_repo(%Repository{manifests: [%Repository.Entry{manifest: manifest_url} = entry | entries]} = repo, acc) do
def sync_repo(%Repository{manifests: [%Repository.Entry{manifest: manifest_url} = entry | entries]} = repo, success, fails) do
case install(manifest_url) do
:ok -> sync_repo(%{repo | manifests: entries}, acc)
{:error, _err} -> sync_repo(%{repo | manifests: entries}, [entry | acc])
:ok -> sync_repo(%{repo | manifests: entries}, [entry | success], fails)
{:error, _err} -> sync_repo(%{repo | manifests: entries}, success, [entry | fails])
end
end
def sync_repo(%Repository{manifests: []}, acc), do: acc
def sync_repo(%Repository{manifests: []}, success, []), do: {:ok, success}
def sync_repo(%Repository{manifests: []}, _success, failed), do: {:error, failed}
@doc "Install a farmware from a URL."
def install(url) do
@ -78,10 +72,9 @@ defmodule Farmbot.Farmware.Installer do
{:ok, json_map} <- Poison.decode(body),
{:ok, farmware} <- Farmware.new(json_map),
:ok <- preflight_checks(farmware) do
finish_install(farmware, json_map)
finish_install(farmware, Map.put(json_map, "url", url))
else
{:error, {name, version, :already_installed}} ->
# v = Version.parse!(version)
{:ok, fw} = Farmbot.Farmware.lookup(name)
Farmbot.BotState.register_farmware(fw)
Logger.info 1, "Farmware #{name} - #{version} is already installed."

View file

@ -3,8 +3,9 @@ defmodule Farmbot.Farmware.Installer.Repository.SyncTask do
use Task, restart: :transient
use Farmbot.Logger
alias Farmbot.System.ConfigStorage
alias Farmbot.Farmware.Installer
alias Farmbot.Farmware.Installer.Repository
alias Farmbot.Farmware
alias Farmware.Installer
alias Installer.Repository
@doc false
def start_link(_) do
@ -13,6 +14,16 @@ defmodule Farmbot.Farmware.Installer.Repository.SyncTask do
def sync_all do
Logger.busy 2, "Syncing all repos. This may take a while."
setup_repos()
synced = fetch_and_sync()
fw_dir = Installer.install_root_path
if File.exists?(fw_dir) do
sync_not_in_repos(fw_dir, synced)
end
end
defp setup_repos do
import Ecto.Query
# first party farmware url could be nil. This would mean it is disabled.
@ -25,23 +36,33 @@ defmodule Farmbot.Farmware.Installer.Repository.SyncTask do
else
Logger.warn 2, "First party farmware is disabled."
end
end
defp fetch_and_sync do
repos = ConfigStorage.all(Repository)
for repo <- repos do
Installer.sync_repo(repo)
end
Enum.reduce(repos, [], fn(repo, acc) ->
case Installer.sync_repo(repo) do
{:ok, list_of_entries} ->
Enum.map(list_of_entries, fn(%{name: name}) -> name end) ++ acc
{:error, _} -> acc
end
end)
end
fw_dir = Installer.install_root_path
if File.exists?(fw_dir) do
all_fws = File.ls!(fw_dir)
for fw_name <- all_fws do
case Farmbot.Farmware.lookup(fw_name) do
{:ok, %Farmbot.Farmware{} = farmware} -> Farmbot.BotState.register_farmware(farmware)
_ -> :ok
end
defp sync_not_in_repos(fw_dir, synced) do
all_fws = File.ls!(fw_dir)
not_in_repos = all_fws -- synced
for fw_name <- not_in_repos do
case Farmware.lookup(fw_name) do
{:ok, %Farmware{} = farmware} ->
Logger.busy 3, "Syncing: #{inspect farmware}"
if farmware.url == "" do
require IEx; IEx.pry
end
Installer.install(farmware.url)
_ -> :ok
end
end
:ok
end
end