-
-
Notifications
You must be signed in to change notification settings - Fork 187
/
release.ex
253 lines (211 loc) · 6.74 KB
/
release.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# credo:disable-for-this-file
defmodule Nerves.Release do
@moduledoc false
# No leading '/' here since this is passed to mksquashfs and it
# doesn't like the leading slash.
@target_release_path "srv/erlang"
@doc false
@spec init(Mix.Release.t()) :: Mix.Release.t()
def init(%{options: options} = release) do
opts = Keyword.merge(options, release_opts())
release = %{
release
| options: opts,
steps: release.steps ++ [&Nerves.Release.finalize/1]
}
check_vm_args_compatibility!(release)
_ = File.rm_rf!(release.path)
if Code.ensure_loaded?(Shoehorn.Release) do
apply(Shoehorn.Release, :init, [release])
else
release
end
end
@doc false
@spec finalize(Mix.Release.t()) :: Mix.Release.t()
def finalize(%Mix.Release{} = release) do
bootfile_path = Path.join([release.version_path, bootfile()])
case File.read(bootfile_path) do
{:ok, bootfile} ->
_ = write_rootfs_priorities(release.applications, release.path, bootfile)
:ok
_ ->
Nerves.Utils.Shell.warn("""
Unable to load bootfile: #{inspect(bootfile_path)}
Skipping rootfs priority file generation
""")
end
release
end
defp bootfile() do
Application.get_env(:nerves, :firmware)[:bootfile] || "shoehorn.boot"
end
@doc false
@spec erts() :: String.t() | true | nil
def erts() do
if Nerves.Env.loaded?() do
System.get_env("ERTS_DIR")
else
true
end
end
defp write_rootfs_priorities(applications, host_release_path, bootfile) do
applications = normalize_applications(applications)
{:script, _, boot_script} = :erlang.binary_to_term(bootfile)
target_beam_files = target_beam_files(boot_script, host_release_path)
target_app_files = target_app_files(applications)
target_priv_dirs = target_priv_dirs(applications)
priorities =
(target_beam_files ++ target_app_files ++ target_priv_dirs)
|> List.flatten()
|> Enum.zip(32_000..1_000)
|> Enum.map(fn {file, priority} -> [file, " ", to_string(priority), "\n"] end)
build_path = Path.join([Mix.Project.build_path(), "nerves"])
File.mkdir_p!(build_path)
Path.join(build_path, "rootfs.priorities")
|> File.write(priorities)
end
defp target_beam_files(boot_script, host_release_path) do
{_, loaded} =
Enum.reduce(boot_script, {nil, []}, fn
{:path, paths}, {_, loaded} ->
{rel_paths(paths), loaded}
{:primLoad, files}, {paths, loaded} ->
prim_loaded =
for path <- paths,
file <- files,
path = Path.join(["lib", path, "#{file}.beam"]),
host_path = Path.expand(Path.join(host_release_path, path)),
File.exists?(host_path),
reduce: [] do
acc ->
[expand_target_path(path) | acc]
end
{paths, [prim_loaded | loaded]}
_, acc ->
acc
end)
loaded
|> Enum.reverse()
|> List.flatten()
end
defp target_app_files(applications) do
for {app, vsn, path} <- applications,
host_path = Path.join([path, "ebin", "#{app}.app"]),
File.exists?(host_path),
reduce: [] do
acc ->
app_path = Path.join([@target_release_path, "lib", "#{app}-#{vsn}", "ebin", "#{app}.app"])
[app_path | acc]
end
end
defp target_priv_dirs(applications) do
for {app, vsn, path} <- applications,
host_priv_dir = Path.join(path, "priv"),
File.dir?(host_priv_dir),
not_empty_dir(host_priv_dir),
reduce: [] do
acc ->
priv_path = Path.join([@target_release_path, "lib", "#{app}-#{vsn}", "priv"])
[priv_path | acc]
end
end
defp rel_paths(paths) do
paths
|> Enum.reverse()
|> Enum.map(&to_string/1)
|> Enum.map(&Path.split/1)
|> Enum.map(fn [_root | path] ->
Path.join(path)
end)
end
defp release_opts() do
[
quiet: true,
include_executables_for: [],
include_erts: &Nerves.Release.erts/0,
boot_scripts: []
]
end
defp not_empty_dir(dir) do
File.ls(dir) != {:ok, []}
end
defp expand_target_path(path) do
Path.join(["/", @target_release_path, path])
|> Path.expand(@target_release_path)
|> String.trim_leading("/")
end
defp normalize_applications(applications) do
for {app, opts} <- applications do
{to_string(app), to_string(opts[:vsn]), Path.expand(opts[:path] || "")}
end
end
@elixir_1_15_opts ["-user elixir", "-run elixir start_iex"]
@legacy_elixir_opts ["-user Elixir.IEx.CLI"]
defp check_vm_args_compatibility!(release) do
Mix.shell().info([:yellow, "* [Nerves] ", :reset, "validating vm.args"])
vm_args_path = Mix.Release.rel_templates_path(release, "vm.args.eex")
if not File.exists?(vm_args_path) do
Mix.raise("Missing required #{vm_args_path}")
end
{exclusions, inclusions} =
if Version.match?(System.version(), ">= 1.15.0") do
{@legacy_elixir_opts, @elixir_1_15_opts}
else
{@elixir_1_15_opts, @legacy_elixir_opts}
end
vm_args = File.read!(vm_args_path)
errors =
[]
|> check_vm_args_inclusions(vm_args, inclusions, vm_args_path)
|> check_vm_args_exclusions(vm_args, exclusions, vm_args_path)
if length(errors) > 0 do
errs = IO.ANSI.format(errors) |> IO.chardata_to_string()
Mix.raise("""
Incompatible vm.args.eex
The procedure for starting IEx changed in Elixir 1.15. The rel/vm.args.eex for
this project starts IEx in an incompatible way for the version of Elixir you're
using and won't work.
To fix this, either change the version of Elixir that you're using or make the
following changes to vm.args.eex:
#{errs}
""")
else
:ok
end
end
defp check_vm_args_exclusions(errors, vm_args, exclusions, vm_args_path) do
String.split(vm_args, "\n")
|> Enum.with_index(1)
|> Enum.filter(fn {line, _} -> Enum.any?(exclusions, &String.contains?(line, &1)) end)
|> case do
[] ->
[]
lines ->
[
"\nPlease remove the following lines:\n\n",
Enum.map(lines, fn {line, line_num} ->
["* ", vm_args_path, ":", to_string(line_num), ":\n ", :red, line, "\n"]
end)
| errors
]
end
end
defp check_vm_args_inclusions(errors, vm_args, inclusions, vm_args_path) do
case Enum.reject(inclusions, &String.contains?(vm_args, &1)) do
[] ->
[]
lines ->
[
[
"\nPlease ensure the following lines are in ",
vm_args_path,
":\n",
:green,
Enum.map(lines, &[" ", &1, "\n"])
]
| errors
]
end
end
end