Skip to content

Instantly share code, notes, and snippets.

@axelson
Forked from cblavier/firmware_reload.ex
Created July 9, 2019 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axelson/3bd5dce798950b84096c69b1d84c2cb1 to your computer and use it in GitHub Desktop.
Save axelson/3bd5dce798950b84096c69b1d84c2cb1 to your computer and use it in GitHub Desktop.
Elixir nerves to upload firmware / reload code
defmodule Mix.Tasks.MyNervesProject.Firmware.Reload do
use Mix.Task
@app_name Mix.Project.config[:app]
@cookie_file "rel/vm.args"
@mdns_domain Application.get_env(:nerves_init_gadget, :mdns_domain)
@node_name Application.get_env(:nerves_init_gadget, :node_name)
def run(_) do
node = :"#{@node_name}@#{@mdns_domain}"
{:ok, _} = Node.start(:console)
cookie() |> Node.set_cookie
true = Node.connect(node)
Application.load(@app_name)
{:ok, modules} = :application.get_key(@app_name, :modules)
for module <- modules do
{:ok, [{^node, :loaded, ^module}]} = IEx.Helpers.nl([node], module)
end
end
defp cookie do
{:ok, vm_content} = File.read(@cookie_file)
%{"cookie" => cookie} = Regex.named_captures(~r/-setcookie (?<cookie>.*)/m, vm_content)
String.to_atom(cookie)
end
end
defmodule Mix.Tasks.MyNervesProject.Firmware.Upload do
use Mix.Task
@target Mix.target
@env Mix.env
@app_name Mix.Project.config[:app]
@fw_path "_build/#{@target}_#{@env}/nerves/images/#{@app_name}.fw"
@script_name "upload.sh"
@mdns_domain Application.get_env(:nerves_init_gadget, :mdns_domain)
def run(_) do
Mix.Task.run("compile")
Mix.Task.run("firmware")
unless File.exists?(@script_name) do
generate_script()
end
run_upload_script()
end
defp generate_script do
Mix.shell().info("Writing #{@script_name}...")
upload_script_contents =
:nerves_firmware_ssh
|> Application.app_dir("priv/templates/script.upload.eex")
|> EEx.eval_file([])
File.write!(@script_name, upload_script_contents)
File.chmod!(@script_name, 0o755)
end
defp run_upload_script do
Mix.shell().info("Uploading firmware...")
File.cwd!
|> Path.join(@script_name)
|> System.cmd([@mdns_domain, @fw_path])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment