Fix config and ConfigStorage

* Fix host/target specific elixirc paths.
* Add cred copier to db for host env.
pull/363/head
Connor Rigby 2017-09-26 07:46:29 -07:00
parent fa3492f5aa
commit 02da0a982f
10 changed files with 46 additions and 11 deletions

View File

@ -10,6 +10,7 @@ config :farmbot, data_path: "tmp/"
# Configure your our system.
# Default implementation needs no special stuff.
config :farmbot, :init, [
Farmbot.Host.InputCredentials,
# DELETEME
Farmbot.Bootstrap.Configurator,
]

View File

@ -54,6 +54,7 @@ defmodule Farmbot.Bootstrap.Supervisor do
use Supervisor
alias Farmbot.Bootstrap.Authorization, as: Auth
alias Farmbot.System.ConfigStorage
require Logger
error_msg = """
@ -86,15 +87,13 @@ defmodule Farmbot.Bootstrap.Supervisor do
@spec get_creds() :: auth | {:error, term}
defp get_creds do
try do
# Get out authorization data out of the environment.
# for host environment this will be configured at compile time.
# for target environment it will be configured by `configurator`.
email = Application.get_env(:farmbot, :authorization)[:email ] || raise "No email provided."
pass = Application.get_env(:farmbot, :authorization)[:password] || raise "No password provided."
server = Application.get_env(:farmbot, :authorization)[:server ] || raise "No server provided."
email = ConfigStorage.get_config_value(:string, "authorization", "email") || raise "No email provided."
pass = ConfigStorage.get_config_value(:string, "authorization", "password") || raise "No password provided."
server = ConfigStorage.get_config_value(:string, "authorization", "server") || raise "No server provided."
{email, pass, server}
rescue
e -> {:error, Exception.message(e)}
e in RuntimeError -> {:error, Exception.message(e)}
e -> reraise(e, System.stacktrace())
end
end

View File

@ -33,8 +33,7 @@ defmodule Farmbot.System.ConfigStorage do
def update_config_value(type, group_name, key_name, value) when type in [:bool, :float, :string] do
__MODULE__
|> apply(:"get_#{type}_value", [group_name, key_name])
|> Map.put(:value, value)
|> BoolValue.changeset()
|> Ecto.Changeset.change(value: value)
|> update!()
end

View File

@ -15,6 +15,7 @@ defmodule Farmbot.System.Supervisor do
|> Application.get_env(:init)
|> Enum.map(fn(child) -> fb_init(child, [args, [name: child]]) end)
|> Kernel.++([supervisor(Farmbot.System.ConfigStorage, [])])
|> Enum.reverse()
|> supervise([strategy: :one_for_all])
end
end

View File

@ -119,12 +119,16 @@ defmodule Farmbot.Mixfile do
]
end
defp elixirc_paths(:dev, "host") do
["./lib", "./nerves/host"]
end
defp elixirc_paths(:test, "host") do
["./lib", "./nerves/farmbot/host", "./test/support"]
["./lib", "./nerves/host", "./test/support"]
end
defp elixirc_paths(_env, target) do
["./lib", "./nerves/farmbot/target"]
["./lib", "./nerves/target"]
end
defp aliases("host"), do: []

View File

@ -0,0 +1,31 @@
defmodule Farmbot.Host.InputCredentials do
@behaviour Farmbot.System.Init
alias Farmbot.System.ConfigStorage
def start_link(_, opts) do
Supervisor.start_link(__MODULE__, [], opts)
end
def init(_) do
# Get out authorization data out of the environment.
# for host environment this will be configured at compile time.
# for target environment it will be configured by `configurator`.
email = Application.get_env(:farmbot, :authorization)[:email ] || raise error("email")
pass = Application.get_env(:farmbot, :authorization)[:password] || raise error("password")
server = Application.get_env(:farmbot, :authorization)[:server ] || raise error("server")
ConfigStorage.update_config_value(:string, "authorization", "email", email)
ConfigStorage.update_config_value(:string, "authorization", "password", pass)
ConfigStorage.update_config_value(:string, "authorization", "server", server)
ConfigStorage.update_config_value(:string, "authorization", "token", nil)
# require IEx; IEx.pry
# Process.sleep(1000)
:ignore
end
defp error(field) do
"""
Your environment is not properly configured! You will need to follow the
directions in `config/host/auth_secret_template.exs` before continuing.
"""
end
end