|
| 1 | +defmodule Engine.Adaptor.Service do |
| 2 | + @moduledoc """ |
| 3 | + The Adaptor Service is use to query and install adaptors in order to run jobs. |
| 4 | +
|
| 5 | + On startup, it queries the filesystem for `package.json` files and builds up |
| 6 | + a list of available adaptors. |
| 7 | +
|
| 8 | + ## Configuration |
| 9 | +
|
| 10 | + The service requires at least `:adaptors_path`, which is used to both query |
| 11 | + which adaptors are installed and when to install new adaptors. |
| 12 | +
|
| 13 | + Another optional setting is: `:repo`, which must point at a module that will be |
| 14 | + used to do the querying and installing. |
| 15 | +
|
| 16 | + ## Installing Adaptors |
| 17 | +
|
| 18 | + Using the `install/3` function an adaptor can be installed, which will also |
| 19 | + add it to the list of available adaptors. |
| 20 | +
|
| 21 | + The adaptor is marked as `:installing`, to allow for conditional behaviour |
| 22 | + elsewhere such as delaying or rejecting processing until the adaptor becomes |
| 23 | + available. |
| 24 | +
|
| 25 | + ## Looking up adaptors |
| 26 | +
|
| 27 | + The module leans on Elixir's built-in `Version` module to provide version |
| 28 | + lookups. |
| 29 | +
|
| 30 | + When looking up an adaptor, either a string or a tuple can be used. |
| 31 | + In the case of requesting the latest version, any one of these will return |
| 32 | + the latest version the service is aware of. |
| 33 | +
|
| 34 | + - `@openfn/language-http` |
| 35 | + - `@openfn/language-http@latest` |
| 36 | + - `{"@openfn/language-http", nil}` |
| 37 | + - `{"@openfn/language-http", "latest"}` |
| 38 | + - `{~r/language-http/, "latest"}` |
| 39 | +
|
| 40 | + You can also request a specific version, or use a range specification: |
| 41 | +
|
| 42 | + - `@openfn/language-http@1.2.3` |
| 43 | + - `{"@openfn/language-http", "~> 1.2.0"}` |
| 44 | + - `{"@openfn/language-http", "< 2.0.0"}` |
| 45 | +
|
| 46 | + > **NOTE** |
| 47 | + > More complex npm style install strings like: `">=0.1.0 <0.2.0"` |
| 48 | + > are not supported. |
| 49 | + > Generally the tuple style is preferred when using range specifications as |
| 50 | + > the npm style strings have a simplistic regex splitter. |
| 51 | +
|
| 52 | + See [Version](https://hexdocs.pm/elixir/Version.html) for more details on |
| 53 | + matching versions. |
| 54 | + """ |
| 55 | + |
| 56 | + @type package_spec :: {name :: String.t() | Regex.t(), version :: String.t() | nil} |
| 57 | + |
| 58 | + use Agent |
| 59 | + require Logger |
| 60 | + |
| 61 | + defmodule State do |
| 62 | + @moduledoc false |
| 63 | + |
| 64 | + @type t :: %__MODULE__{ |
| 65 | + name: GenServer.server(), |
| 66 | + adaptors: [Engine.Adaptor.t()], |
| 67 | + adaptors_path: binary(), |
| 68 | + repo: module() |
| 69 | + } |
| 70 | + |
| 71 | + @enforce_keys [:adaptors_path] |
| 72 | + defstruct @enforce_keys ++ [:name, adaptors: [], repo: Engine.Adaptor.Repo] |
| 73 | + |
| 74 | + def find_adaptor(%{adaptors: adaptors}, fun) when is_function(fun) do |
| 75 | + Enum.find(adaptors, fun) |
| 76 | + end |
| 77 | + |
| 78 | + def find_adaptor(state, {package_name, version}) do |
| 79 | + find_adaptor(state, fn %{name: n, version: v} -> |
| 80 | + n == package_name && (v == version || is_nil(version)) |
| 81 | + end) |
| 82 | + end |
| 83 | + |
| 84 | + def refresh_list(state) do |
| 85 | + %{state | adaptors: state.repo.list_local(state.adaptors_path)} |
| 86 | + end |
| 87 | + |
| 88 | + def add_adaptor(state, adaptor) do |
| 89 | + %{state | adaptors: state.adaptors ++ [adaptor]} |
| 90 | + end |
| 91 | + |
| 92 | + def remove_adaptor(state, fun) do |
| 93 | + %{state | adaptors: Enum.reject(state.adaptors, fun)} |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + def start_link(opts) do |
| 98 | + state = struct!(State, opts) |> State.refresh_list() |
| 99 | + |
| 100 | + Agent.start_link(fn -> state end, name: state.name || __MODULE__) |
| 101 | + end |
| 102 | + |
| 103 | + def get_adaptors(agent) do |
| 104 | + Agent.get(agent, fn state -> state.adaptors end) |
| 105 | + end |
| 106 | + |
| 107 | + @spec find_adaptor(Agent.agent(), package :: String.t()) :: Adaptor.t() | nil |
| 108 | + def find_adaptor(agent, package) when is_binary(package) do |
| 109 | + find_adaptor(agent, resolve_package_name(package)) |
| 110 | + end |
| 111 | + |
| 112 | + @spec find_adaptor(Agent.agent(), package_spec()) :: Adaptor.t() | nil |
| 113 | + def find_adaptor(agent, {package_name, version}) do |
| 114 | + requirement = version_to_requirement(version) |
| 115 | + |
| 116 | + get_adaptors(agent) |
| 117 | + |> Enum.filter(&by_name_and_requirement(&1, package_name, requirement)) |
| 118 | + |> Enum.max_by( |
| 119 | + fn %{version: version} -> |
| 120 | + Version.parse!(version) |
| 121 | + end, |
| 122 | + Version, |
| 123 | + fn -> nil end |
| 124 | + ) |
| 125 | + end |
| 126 | + |
| 127 | + defp by_name_and_requirement(adaptor, matcher = %Regex{}, requirement) do |
| 128 | + !!(Regex.match?(matcher, adaptor.name) && |
| 129 | + Version.match?(adaptor.version, requirement)) |
| 130 | + end |
| 131 | + |
| 132 | + defp by_name_and_requirement(adaptor, name, requirement) do |
| 133 | + !!(match?(%{name: ^name}, adaptor) && |
| 134 | + Version.match?(adaptor.version, requirement)) |
| 135 | + end |
| 136 | + |
| 137 | + defp version_to_requirement(version) do |
| 138 | + cond do |
| 139 | + version in ["latest", nil] -> |
| 140 | + "> 0.0.0" |
| 141 | + |
| 142 | + String.match?(version, ~r/[<=>]/) -> |
| 143 | + raise ArgumentError, message: "Version specs not implemented yet." |
| 144 | + |
| 145 | + true -> |
| 146 | + version |
| 147 | + end |
| 148 | + |> Version.parse_requirement!() |
| 149 | + end |
| 150 | + |
| 151 | + def installed?(agent, package_spec) do |
| 152 | + !!find_adaptor(agent, package_spec) |
| 153 | + end |
| 154 | + |
| 155 | + @spec install(Agent.agent(), binary()) :: |
| 156 | + {:ok, Engine.Adaptor.t()} | {:error, {Collectable.t(), exit_status :: non_neg_integer}} |
| 157 | + def install(agent, package) when is_binary(package) do |
| 158 | + install(agent, resolve_package_name(package)) |
| 159 | + end |
| 160 | + |
| 161 | + @spec install(Agent.agent(), package_spec()) :: |
| 162 | + {:ok, Engine.Adaptor.t()} | {:error, {Collectable.t(), exit_status :: non_neg_integer}} |
| 163 | + def install(agent, package_spec) do |
| 164 | + agent |
| 165 | + |> find_adaptor(package_spec) |
| 166 | + |> case do |
| 167 | + nil -> install!(agent, package_spec) |
| 168 | + existing -> {:ok, existing} |
| 169 | + end |
| 170 | + end |
| 171 | + |
| 172 | + def install!(agent, {package_name, version} = package_spec) do |
| 173 | + new_adaptor = %Engine.Adaptor{name: package_name, version: version, status: :installing} |
| 174 | + |
| 175 | + agent |> Agent.update(&State.add_adaptor(&1, new_adaptor)) |
| 176 | + |
| 177 | + {repo, adaptors_path} = |
| 178 | + agent |
| 179 | + |> Agent.get(fn state -> |
| 180 | + {state.repo, state.adaptors_path} |
| 181 | + end) |
| 182 | + |
| 183 | + repo.install(build_aliased_name(package_spec), adaptors_path) |
| 184 | + |> case do |
| 185 | + {_stdout, 0} -> |
| 186 | + Logger.info("Refreshing Adaptor list") |
| 187 | + agent |> Agent.update(&State.refresh_list/1, 30_000) |
| 188 | + {:ok, agent |> Agent.get(&State.find_adaptor(&1, {package_name, version}))} |
| 189 | + |
| 190 | + {stdout, code} -> |
| 191 | + agent |
| 192 | + |> Agent.update(fn state -> |
| 193 | + State.remove_adaptor(state, &match?(^new_adaptor, &1)) |
| 194 | + end) |
| 195 | + |
| 196 | + {:error, {stdout, code}} |
| 197 | + end |
| 198 | + end |
| 199 | + |
| 200 | + def resolve_package_name(package_name) when is_binary(package_name) do |
| 201 | + ~r/(@?[\/\d\n\w-]+)(?:@([\d\.\w]+))?$/ |
| 202 | + |> Regex.run(package_name) |
| 203 | + |> case do |
| 204 | + [_, name, version] -> |
| 205 | + {name, version} |
| 206 | + |
| 207 | + [_, _name] -> |
| 208 | + {package_name, nil} |
| 209 | + |
| 210 | + _ -> |
| 211 | + raise ArgumentError, "Only npm style package names are currently supported" |
| 212 | + end |
| 213 | + end |
| 214 | + |
| 215 | + @doc """ |
| 216 | + Turns a package name and version into a string for NPM. |
| 217 | +
|
| 218 | + Since multiple versions of the same package can be installed, it's important |
| 219 | + to rely on npms built-in package aliasing. |
| 220 | +
|
| 221 | + E.g. `@openfn/language-http@1.2.8` turns into: |
| 222 | + `@openfn/language-http-v1.2.8@npm:@openfn/language-http@1.2.8` |
| 223 | +
|
| 224 | + Which is pretty long winded but necessary for the reason above. |
| 225 | +
|
| 226 | + If using this module as a base, it's likely you would need to adaptor this |
| 227 | + to suit your particular naming strategy. |
| 228 | + """ |
| 229 | + def build_aliased_name({package, version}) do |
| 230 | + "#{package}-v#{version}@npm:#{package}@#{version}" |
| 231 | + end |
| 232 | +end |
0 commit comments