Last active
October 14, 2021 08:44
-
-
Save cblavier/d14d122b64909bdd7eb312a711e5c70e to your computer and use it in GitHub Desktop.
Elixir nerves to upload firmware / reload code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Mix.Tasks.PhenixPlayer.Firmware.Reload do | |
use Mix.Task | |
alias IEx.Helpers | |
alias PhenixPlayer.MixProject | |
@app_name Mix.Project.config()[:app] | |
def run(_) do | |
node = :"#{@app_name}@#{System.get_env("MDNS_DOMAIN")}.local" | |
{:ok, _} = Node.start(:console) | |
Node.set_cookie(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}]} = Helpers.nl([node], module) | |
end | |
end | |
defp cookie do | |
String.to_atom(MixProject.release()[:cookie]) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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