Skip to content

Commit

Permalink
Rename release server -> update server
Browse files Browse the repository at this point in the history
  • Loading branch information
mobileoverlord committed Jan 16, 2025
1 parent cf395b2 commit d317665
Show file tree
Hide file tree
Showing 15 changed files with 511 additions and 308 deletions.
4 changes: 2 additions & 2 deletions lib/peridiod/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Peridiod.Application do
Connection,
Socket,
Distribution,
Release,
Update,
Binary
}

Expand All @@ -19,7 +19,7 @@ defmodule Peridiod.Application do
Binary.Installer.Supervisor,
Binary.StreamDownloader.Supervisor,
Binary.CacheDownloader.Supervisor,
{Release.Server, config}
{Update.Server, config}
]

children =
Expand Down
4 changes: 2 additions & 2 deletions lib/peridiod/binary/installer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ defmodule Peridiod.Binary.Installer do
end

def handle_continue(false, state) do
Logger.error("Installer [#{state.binary_metadata.prn}]: Installing from download stream")
Logger.debug("Installer [#{state.binary_metadata.prn}]: Installing from download stream")
do_install_from_download(state)
{:noreply, state}
{:noreply, %{state | status: :installing, source: :download}}
end

# Streaming from Cache
Expand Down
31 changes: 2 additions & 29 deletions lib/peridiod/bundle.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
defmodule Peridiod.Bundle do
alias Peridiod.{Binary, Cache, Bundle}

import Peridiod.Utils, only: [stamp_utc_now: 0]
use Peridiod.Cache.Helpers, cache_path: "bundle"

@cache_path "bundle"
@stamp_cached ".stamp_cached"
@stamp_installed ".stamp_installed"
alias Peridiod.{Binary, Cache, Bundle}

defstruct prn: nil,
binaries: nil
Expand Down Expand Up @@ -104,29 +100,6 @@ defmodule Peridiod.Bundle do
end
end

def cache_path(%__MODULE__{prn: bundle_prn}) do
cache_path(bundle_prn)
end

def cache_path(bundle_prn) when is_binary(bundle_prn) do
Path.join([@cache_path, bundle_prn])
end

def installed?(cache_pid \\ Cache, %__MODULE__{} = release_metadata) do
stamp_file = Path.join([cache_path(release_metadata), @stamp_installed])
Cache.exists?(cache_pid, stamp_file)
end

def stamp_cached(cache_pid \\ Cache, %__MODULE__{} = release_metadata) do
stamp_file = Path.join([cache_path(release_metadata), @stamp_cached])
Cache.write(cache_pid, stamp_file, stamp_utc_now())
end

def stamp_installed(cache_pid \\ Cache, %__MODULE__{} = release_metadata) do
stamp_file = Path.join([cache_path(release_metadata), @stamp_installed])
Cache.write(cache_pid, stamp_file, stamp_utc_now())
end

def filter_binaries_by_targets(%__MODULE__{binaries: binaries}, []), do: binaries

def filter_binaries_by_targets(%__MODULE__{binaries: binaries}, targets) do
Expand Down
86 changes: 86 additions & 0 deletions lib/peridiod/bundle_override.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
defmodule Peridiod.BundleOverride do
use Peridiod.Cache.Helpers, cache_path: "override"

alias Peridiod.{BundleOverride, Bundle}

require Logger

defstruct prn: nil,
bundle: nil

@type t() :: %__MODULE__{
prn: String.t(),
bundle: Bundle.t()
}

defimpl Jason.Encoder, for: BundleOverride do
def encode(%BundleOverride{} = override_metadata, opts) do
override_metadata
|> Map.take([:prn, :name, :version_requirement])
|> Map.put(:bundle_prn, override_metadata.bundle.prn)
|> Jason.Encode.map(opts)
end
end

def metadata_from_cache(cache_pid, override_prn) do
manifest_file = Path.join([@cache_path, override_prn, "manifest"])

with {:ok, json} <- Cache.read(cache_pid, manifest_file),
{:ok, map} <- Jason.decode(json) do
bundle_metadata =
case Bundle.metadata_from_cache(cache_pid, map["bundle_prn"]) do
{:ok, bundle_metadata} -> bundle_metadata
_ -> nil
end

map = Map.put(map, "bundle", bundle_metadata)
{:ok, metadata_from_map(map)}
end
end

def metadata_from_map(override_metadata) do
%__MODULE__{
prn: override_metadata["prn"],
bundle: override_metadata["bundle"]
}
end

@doc """
Recursively parse a release manifest into structs
"""
@spec metadata_from_manifest(map()) :: {:ok, t()}
def metadata_from_manifest(
%{
"bundle_override" => override_metadata
} = manifest
) do
bundle =
case Bundle.metadata_from_manifest(manifest) do
{:ok, bundle} ->
bundle

error ->
Logger.error("Error loading bundle from manifest: #{inspect(error)}")
nil
end

{:ok,
%__MODULE__{
prn: override_metadata["prn"],
bundle: bundle
}}
end

def metadata_to_cache(
cache_pid \\ Cache,
%__MODULE__{prn: override_prn, bundle: bundle} = override_metadata
) do
override_json = Jason.encode!(override_metadata)
manifest_file = Path.join([@cache_path, override_prn, "manifest"])

with :ok <- Cache.write(cache_pid, manifest_file, override_json),
:ok <- Bundle.metadata_to_cache(cache_pid, bundle) do
:ok
end
end
end
41 changes: 41 additions & 0 deletions lib/peridiod/cache/helpers.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
defmodule Peridiod.Cache.Helpers do
defmacro __using__(opts) do
quote do
import Peridiod.Utils, only: [stamp_utc_now: 0]
alias Peridiod.Cache

@cache_path unquote(opts[:cache_path])
@cache_file unquote(opts[:cache_file])
@stamp_cached ".stamp_cached"
@stamp_installed ".stamp_installed"

def cache_path(%{prn: prn}) do
cache_path(prn)
end

def cache_path(prn) when is_binary(prn) do
Path.join([@cache_path, prn])
end

def cached?(cache_pid \\ Cache, metadata) do
stamp_file = Path.join([cache_path(metadata), @stamp_cached])
Cache.exists?(cache_pid, stamp_file)
end

def installed?(cache_pid \\ Cache, metadata) do
stamp_file = Path.join([cache_path(metadata), @stamp_installed])
Cache.exists?(cache_pid, stamp_file)
end

def stamp_cached(cache_pid \\ Cache, metadata) do
stamp_file = Path.join([cache_path(metadata), @stamp_cached])
Cache.write(cache_pid, stamp_file, stamp_utc_now())
end

def stamp_installed(cache_pid \\ Cache, metadata) do
stamp_file = Path.join([cache_path(metadata), @stamp_installed])
Cache.write(cache_pid, stamp_file, stamp_utc_now())
end
end
end
end
18 changes: 8 additions & 10 deletions lib/peridiod/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ defmodule Peridiod.Config do
remote_shell: false,
remote_iex: false,
remote_access_tunnels: %{},
release_poll_enabled: false,
release_poll_interval: 300_000,
release_prn: nil,
release_version: nil,
update_poll_enabled: false,
update_poll_interval: 300_000,
targets: ["portable"],
trusted_signing_keys: [],
trusted_signing_key_dir: nil,
Expand Down Expand Up @@ -62,10 +60,8 @@ defmodule Peridiod.Config do
remote_iex: boolean,
remote_shell: boolean,
remote_access_tunnels: map(),
release_poll_enabled: boolean,
release_poll_interval: non_neg_integer(),
release_prn: String.t(),
release_version: String.t(),
update_poll_enabled: boolean,
update_poll_interval: non_neg_integer(),
targets: [String.t()],
trusted_signing_keys: [SigningKey.t()],
trusted_signing_key_dir: Path.t(),
Expand Down Expand Up @@ -167,8 +163,10 @@ defmodule Peridiod.Config do
|> override_if_set(:targets, config_file["targets"])
|> override_if_set(:trusted_signing_key_dir, config_file["trusted_signing_key_dir"])
|> override_if_set(:trusted_signing_keys, config_file["trusted_signing_keys"])
|> override_if_set(:release_poll_enabled, config_file["release_poll_enabled"])
|> override_if_set(:release_poll_interval, config_file["release_poll_interval"])
|> override_if_set(:update_poll_enabled, config_file["release_poll_enabled"])
|> override_if_set(:update_poll_enabled, config_file["update_poll_enabled"])
|> override_if_set(:update_poll_interval, config_file["release_poll_interval"])
|> override_if_set(:update_poll_interval, config_file["update_poll_interval"])
|> override_if_set(
:trusted_signing_key_threshold,
config_file["trusted_signing_key_threshold"]
Expand Down
87 changes: 9 additions & 78 deletions lib/peridiod/release.ex
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
defmodule Peridiod.Release do
use Peridiod.Cache.Helpers, cache_path: "release"

alias Peridiod.{Cache, Release, Bundle}
alias PeridiodPersistence.KV

import Peridiod.Utils, only: [stamp_utc_now: 0]

require Logger

@cache_path "release"
@stamp_cached ".stamp_cached"
@stamp_installed ".stamp_installed"

defstruct prn: nil,
name: nil,
version: nil,
Expand Down Expand Up @@ -52,7 +49,12 @@ defmodule Peridiod.Release do

with {:ok, json} <- Cache.read(cache_pid, manifest_file),
{:ok, map} <- Jason.decode(json) do
bundle_metadata = bundle_metadata_from_cache(cache_pid, map["bundle_prn"])
bundle_metadata =
case Bundle.metadata_from_cache(cache_pid, map["bundle_prn"]) do
{:ok, bundle_metadata} -> bundle_metadata
_ -> nil
end

map = Map.put(map, "bundle", bundle_metadata)
{:ok, metadata_from_map(map)}
end
Expand Down Expand Up @@ -127,82 +129,11 @@ defmodule Peridiod.Release do
) do
release_json = Jason.encode!(release_metadata)

manifest_file = Path.join(["release", release_prn, "manifest"])
manifest_file = Path.join([@cache_path, release_prn, "manifest"])

with :ok <- Cache.write(cache_pid, manifest_file, release_json),
:ok <- Bundle.metadata_to_cache(cache_pid, bundle) do
:ok
end
end

def cached?(cache_pid \\ Cache, %__MODULE__{} = release_metadata) do
stamp_file = Path.join([cache_path(release_metadata), @stamp_cached])
Cache.exists?(cache_pid, stamp_file)
end

def cache_path(%__MODULE__{prn: release_prn}) do
Path.join([@cache_path, release_prn])
end

def installed?(cache_pid \\ Cache, %__MODULE__{} = release_metadata) do
stamp_file = Path.join([cache_path(release_metadata), @stamp_installed])
Cache.exists?(cache_pid, stamp_file)
end

def stamp_cached(cache_pid \\ Cache, %__MODULE__{} = release_metadata) do
stamp_file = Path.join([cache_path(release_metadata), @stamp_cached])
Cache.write(cache_pid, stamp_file, stamp_utc_now())
end

def stamp_installed(cache_pid \\ Cache, %__MODULE__{} = release_metadata) do
stamp_file = Path.join([cache_path(release_metadata), @stamp_installed])
Cache.write(cache_pid, stamp_file, stamp_utc_now())
end

def kv_progress(kv_pid \\ KV, %__MODULE__{prn: prn} = release_metadata) when not is_nil(prn) do
version =
case release_metadata.version do
nil -> ""
version -> Version.to_string(version)
end

KV.put_map(kv_pid, %{
"peridio_rel_progress" => release_metadata.prn,
"peridio_vsn_progress" => version
})
end

def kv_advance(kv_pid \\ KV) do
KV.reinitialize(kv_pid)

KV.get_all_and_update(kv_pid, fn kv ->
rel_progress = Map.get(kv, "peridio_rel_progress", "")
vsn_progress = Map.get(kv, "peridio_vsn_progress", "")
bin_progress = Map.get(kv, "peridio_bin_progress", "")

rel_current = Map.get(kv, "peridio_rel_current", "")
vsn_current = Map.get(kv, "peridio_vsn_current", "")
bin_current = Map.get(kv, "peridio_bin_current", "")

kv
|> Map.put("peridio_rel_previous", rel_current)
|> Map.put("peridio_vsn_previous", vsn_current)
|> Map.put("peridio_bin_previous", bin_current)
|> Map.put("peridio_rel_current", rel_progress)
|> Map.put("peridio_vsn_current", vsn_progress)
|> Map.put("peridio_bin_current", bin_progress)
|> Map.put("peridio_rel_progress", "")
|> Map.put("peridio_vsn_progress", "")
|> Map.put("peridio_bin_progress", "")
end)
end

defp bundle_metadata_from_cache(_cache_pid, nil), do: nil

defp bundle_metadata_from_cache(cache_pid, bundle_prn) do
case Bundle.metadata_from_cache(cache_pid, bundle_prn) do
{:ok, bundle_metadata} -> bundle_metadata
_ -> nil
end
end
end
11 changes: 5 additions & 6 deletions lib/peridiod/socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Peridiod.Socket do
use Slipstream
require Logger

alias Peridiod.{Client, Distribution, RemoteConsole, Utils}
alias Peridiod.{Client, Distribution, RemoteConsole, Utils, Update}
alias Peridiod.Binary.Installer.Fwup
alias PeridiodPersistence.KV

Expand Down Expand Up @@ -84,13 +84,12 @@ defmodule Peridiod.Socket do
false -> params
end

current_release_prn = KV.get("peridio_rel_current")
current_release_version = KV.get("peridio_vsn_current")
current_via_prn = KV.get("peridio_via_current") || KV.get("peridio_rel_current")
current_bundle_prn = KV.get("peridio_bun_current")
current_version = KV.get("peridio_vsn_current")

sdk_client =
config.sdk_client
|> Map.put(:release_prn, current_release_prn)
|> Map.put(:release_version, current_release_version)
Update.sdk_client(config.sdk_client, current_via_prn, current_bundle_prn, current_version)

socket =
new_socket()
Expand Down
Loading

0 comments on commit d317665

Please sign in to comment.