Deployment and lifecycle management for Nix
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

server: first pass at requesting a subscription upgrade

+122 -1
+30
apps/sower/lib/sower/orchestration.ex
··· 217 217 """ 218 218 def get_subscription_sid!(sid), do: Repo.get_by!(Subscription, sid: sid) 219 219 220 + def get_subscription_sid(sid) do 221 + Subscription 222 + |> Repo.get_by(sid: sid) 223 + |> Repo.preload(:seed) 224 + end 225 + 220 226 @doc """ 221 227 Creates a subscription. 222 228 ··· 385 391 """ 386 392 def change_deployment(%Deployment{} = deployment, attrs \\ %{}) do 387 393 Deployment.changeset(deployment, attrs) 394 + end 395 + 396 + @doc """ 397 + Request and create a deployment for a subscription 398 + """ 399 + def request_subscription_deployment( 400 + %SowerClient.Schemas.Subscription.UpgradeRequest{} = upgrade 401 + ) do 402 + with sub when not is_nil(sub) <- get_subscription_sid(upgrade.subscription_sid), 403 + seed_store_path <- Sower.Seed.latest_store_path(sub.seed), 404 + {:ok, deploy} <- 405 + create_deployment(%{ 406 + store_paths: [seed_store_path.store_path], 407 + subscription: sub 408 + }) do 409 + {:ok, 410 + %SowerClient.Schemas.Orchestration.Deployment{subscription_sid: sub.sid, sid: deploy.sid}} 411 + else 412 + {:error, _} = err -> 413 + err 414 + 415 + nil -> 416 + {:error, :subscription_not_found} 417 + end 388 418 end 389 419 end
+1
apps/sower/lib/sower/orchestration/deployment.ex
··· 26 26 deployment 27 27 |> cast(attrs, [:deployed_at]) 28 28 |> put_assoc(:store_paths, attrs.store_paths) 29 + |> put_assoc(:subscription, attrs.subscription) 29 30 |> validate_required([]) 30 31 end 31 32 end
+9
apps/sower/lib/sower/seed.ex
··· 84 84 ) 85 85 end 86 86 87 + def latest_store_path(%__MODULE__{id: id}) do 88 + Repo.one( 89 + from s in Sower.SeedStorePath, 90 + where: s.id == ^id, 91 + order_by: [desc: s.updated_at] 92 + ) 93 + |> Repo.preload(:store_path) 94 + end 95 + 87 96 def latest_store_path_by_sid(sid) do 88 97 seed = Sower.Seed.get_sid!(sid) 89 98
+16
apps/sower/lib/sower_web/agent_channel.ex
··· 137 137 end 138 138 end 139 139 140 + def handle_in("subscription:upgrade", payload, socket) do 141 + with {:ok, req_sub} <- SowerClient.Schemas.Subscription.UpgradeRequest.cast(payload), 142 + {:ok, deploy} <- Sower.Orchestration.request_subscription_deployment(req_sub) do 143 + {:reply, {:ok, deploy}, socket} 144 + else 145 + {:error, error} -> 146 + Logger.error( 147 + msg: "Failed to request subscription upgrade", 148 + payload: payload, 149 + error: error 150 + ) 151 + 152 + {:reply, :error, socket} 153 + end 154 + end 155 + 140 156 @impl Phoenix.Channel 141 157 def handle_info(:track_presence, %Phoenix.Socket{assigns: %{agent: agent}} = socket) do 142 158 Logger.debug(msg: "Tracking agent presence", agent_sid: agent.sid)
+22
apps/sower_agent/lib/sower_agent/socket_client.ex
··· 38 38 {:reply, {:ok, :pong}, socket} 39 39 end 40 40 41 + def handle_call({:subscription_upgrade, sid}, _from, socket) do 42 + with {:ok, upgrade_request} <- 43 + SowerClient.Schemas.Subscription.UpgradeRequest.cast(%{ 44 + subscription_sid: sid 45 + }), 46 + {:ok, ref} <- 47 + push(socket, private_channel(), "subscription:upgrade", upgrade_request), 48 + {:ok, response} <- await_reply(ref), 49 + {:ok, deployment} <- SowerClient.Schemas.Orchestration.Deployment.cast(response) do 50 + {:reply, {:ok, deployment}, socket} 51 + else 52 + {:error, error} -> 53 + Logger.error( 54 + msg: "Failed to request subscription upgrade", 55 + error: error, 56 + subscription_sid: sid 57 + ) 58 + 59 + {:reply, {:error, error}, socket} 60 + end 61 + end 62 + 41 63 def handle_call({event, params}, _from, socket) do 42 64 {:ok, ref} = push(socket, private_channel(), event, params) 43 65 {:reply, await_reply(ref), socket}
+1 -1
apps/sower_agent/lib/sower_agent/storage.ex
··· 10 10 field :local_sid, String.t() 11 11 field :agent_sid, String.t() 12 12 13 - # field :subscriptions, 13 + field :subscriptions, list(SowerClient.Schemas.Subscription) 14 14 end 15 15 16 16 # client
+27
apps/sower_client/lib/schemas/orchestration/deployment.ex
··· 1 + defmodule SowerClient.Schemas.Orchestration.Deployment do 2 + use SowerClient.Schema 3 + 4 + OpenApiSpex.schema(%{ 5 + title: "Deployment", 6 + type: :object, 7 + properties: %{ 8 + sid: %Schema{ 9 + type: :string, 10 + description: "deployment sid allocated by Sower", 11 + readOnly: true 12 + }, 13 + subscription_sid: %Schema{ 14 + type: :string, 15 + description: "subscription sid allocated by Sower", 16 + readOnly: true 17 + }, 18 + deployed_at: %Schema{ 19 + type: :string, 20 + format: :date_time, 21 + description: "when the deployment was deployed", 22 + default: DateTime.from_unix!(0) |> DateTime.to_iso8601() 23 + } 24 + }, 25 + required: ~w(subscription_sid)a 26 + }) 27 + end
+16
apps/sower_client/lib/schemas/subscription/upgrade_request.ex
··· 1 + defmodule SowerClient.Schemas.Subscription.UpgradeRequest do 2 + use SowerClient.Schema 3 + 4 + OpenApiSpex.schema(%{ 5 + title: "SubscriptionUpgradeRequest", 6 + type: :object, 7 + properties: %{ 8 + subscription_sid: %Schema{ 9 + type: :string, 10 + description: "subscription sid allocated by Sower", 11 + readOnly: true 12 + } 13 + }, 14 + required: ~w(subscription_sid)a 15 + }) 16 + end