Skip to content

Commit

Permalink
Merge branch 'release/0.17.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed Nov 6, 2024
2 parents a65fdcf + eaeb2b9 commit a778f51
Show file tree
Hide file tree
Showing 47 changed files with 2,277 additions and 102 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
name: 'CI'
name: "CI"

on:
push:
branches:
- master
- develop
- 'feature/**'
- 'hotfix/**'
- "feature/**"
- "hotfix/**"

jobs:
deps:
name: Dependencies
runs-on: warp-ubuntu-latest-x64-2x
runs-on: warp-ubuntu-2204-x64-2x
container:
image: alpine:3.18

Expand Down Expand Up @@ -55,7 +55,7 @@ jobs:
static_code_analysis:
name: Static Code Analysis
needs: deps
runs-on: warp-ubuntu-latest-x64-2x
runs-on: warp-ubuntu-2204-x64-2x
container:
image: alpine:3.18

Expand Down Expand Up @@ -96,7 +96,7 @@ jobs:
test:
name: Unit Tests
needs: deps
runs-on: warp-ubuntu-latest-x64-2x
runs-on: warp-ubuntu-2204-x64-2x
container:
image: alpine:3.18

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:
jobs:
build:
name: Build
runs-on: warp-ubuntu-latest-x64-2x
runs-on: warp-ubuntu-2204-x64-2x
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- name: "Checkout"
Expand Down Expand Up @@ -49,7 +49,7 @@ jobs:
deploy:
name: Deploy
needs: build
runs-on: warp-ubuntu-latest-x64-2x
runs-on: warp-ubuntu-2204-x64-2x
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ uplink-*.tar

mnesia

.mnesia
.mnesia

.envrc
4 changes: 0 additions & 4 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ config :uplink, Oban,
components: 1
]

config :reverse_proxy_plug,
:http_client,
ReverseProxyPlug.HTTPClient.Adapters.HTTPoison

config :formation, Formation.Lxd, timeout: 180

config :logger,
Expand Down
7 changes: 5 additions & 2 deletions config/dev.exs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import Config

config :uplink, Uplink.Secret, "secretsomethingsixteen"
config :uplink,
Uplink.Secret,
System.get_env("UPLINK_SECRET", "secretsomethingsixteen")

config :uplink, Uplink.Data, mode: "lite"

config :uplink, Uplink.Clients.Instellar, endpoint: "http://localhost/uplink"
config :uplink, Uplink.Clients.Instellar,
endpoint: "http://localhost:4000/uplink"

config :uplink, :environment, :dev

Expand Down
9 changes: 9 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import Config

config :uplink, Uplink.Data, mode: "pro"

config :uplink, Uplink.Monitors, enabled: false

config :uplink, Uplink.Metrics.Pipeline,
producer_module: Broadway.DummyProducer,
producer_options: []

config :uplink, Uplink.PipelineSupervisor, sync_interval: 100

config :uplink, Uplink.Repo,
username:
System.get_env("UPLINK_DB_USERNAME") || System.get_env("POSTGRES_USERNAME"),
Expand Down Expand Up @@ -38,4 +46,5 @@ config :uplink, :drivers, aws_s3: Uplink.Drivers.Bucket.AwsMock

# config :plug, :validate_header_keys_during_test, false
# Print only warnings and errors during test
# Disable logging in tests
config :logger, level: :warn
12 changes: 12 additions & 0 deletions lib/uplink/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@ defmodule Uplink.Application do

alias Uplink.Web

@pipeline_supervisor Uplink.PipelineSupervisor

def start(_type, _args) do
%{key: key, cert: cert} = Web.Certificate.generate()

pipeline_supervisor_config =
Application.get_env(:uplink, @pipeline_supervisor, [])

sync_interval =
Keyword.get(pipeline_supervisor_config, :sync_interval, 5_000)

router_config = Application.get_env(:uplink, Uplink.Router, port: 4040)

internal_router_config =
Expand All @@ -22,6 +31,9 @@ defmodule Uplink.Application do
{Cluster.Supervisor, [topologies, [name: Uplink.ClusterSupervisor]]},
{Task.Supervisor, name: Uplink.TaskSupervisor},
{Plug.Cowboy, plug: Uplink.Internal, scheme: :http, port: internal_port},
{Pogo.DynamicSupervisor,
name: @pipeline_supervisor, scope: :uplink, sync_interval: sync_interval},
{Uplink.Monitors, []},
{
Plug.Cowboy,
plug: Uplink.Router,
Expand Down
5 changes: 5 additions & 0 deletions lib/uplink/clients/instellar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule Uplink.Clients.Instellar do
Instance,
Register,
Component,
Monitor,
Variable,
Proxy,
Self
Expand All @@ -35,6 +36,10 @@ defmodule Uplink.Clients.Instellar do
to: Proxy,
as: :list

defdelegate list_monitors,
to: Monitor,
as: :list

defdelegate deployment_metadata(install),
to: Installation,
as: :metadata
Expand Down
21 changes: 21 additions & 0 deletions lib/uplink/clients/instellar/monitor.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule Uplink.Clients.Instellar.Monitor do
alias Uplink.Clients.Instellar

def list do
headers = Instellar.Self.headers()

[Instellar.endpoint(), "self", "monitors"]
|> Path.join()
|> Req.get(headers: headers, max_retries: 1)
|> case do
{:ok, %{status: 200, body: %{"data" => monitors}}} ->
{:ok, monitors}

{:ok, %{status: _, body: body}} ->
{:error, body}

{:error, error} ->
{:error, error}
end
end
end
71 changes: 37 additions & 34 deletions lib/uplink/clients/lxd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ defmodule Uplink.Clients.LXD do

alias Uplink.Clients.LXD

defdelegate get_node(name),
to: __MODULE__.Node.Manager,
as: :show

defdelegate list_cluster_members(),
to: __MODULE__.Cluster.Manager,
as: :list_members
Expand All @@ -16,11 +20,11 @@ defmodule Uplink.Clients.LXD do
to: __MODULE__.Profile.Manager,
as: :get

defdelegate list_instances(project),
to: __MODULE__.Instance.Manager,
defdelegate list_metrics(options \\ []),
to: __MODULE__.Metric.Manager,
as: :list

defdelegate list_instances(),
defdelegate list_instances(options \\ []),
to: __MODULE__.Instance.Manager,
as: :list

Expand All @@ -33,37 +37,36 @@ defmodule Uplink.Clients.LXD do
as: :leases

def uplink_leases do
Cache.get({:leases, "uplink"}) ||
(
config = Application.get_env(:uplink, Uplink.Data) || []
uplink_project = Keyword.get(config, :project, "default")
client = LXD.client()

uplink_project =
client
|> Lexdee.get_project(uplink_project)
|> case do
{:ok, %{body: %{"name" => name}}} -> name
{:error, %{"error_code" => 404}} -> "default"
end

case LXD.network_leases(uplink_project) do
leases when is_list(leases) ->
uplink_addresses =
Enum.map(leases, fn lease ->
lease.address
end)

Cache.put({:leases, "uplink"}, uplink_addresses,
ttl: :timer.hours(3)
)

uplink_addresses

{:error, error} ->
{:error, error}
end
)
Cache.get({:leases, "uplink"}) || fetch_leases()
end

defp fetch_leases do
config = Application.get_env(:uplink, Uplink.Data) || []
uplink_project = Keyword.get(config, :project, "default")
client = LXD.client()

uplink_project =
client
|> Lexdee.get_project(uplink_project)
|> case do
{:ok, %{body: %{"name" => name}}} -> name
{:error, %{"error_code" => 404}} -> "default"
end

case LXD.network_leases(uplink_project) do
leases when is_list(leases) ->
uplink_addresses =
Enum.map(leases, fn lease ->
lease.address
end)

Cache.put({:leases, "uplink"}, uplink_addresses, ttl: :timer.hours(3))

uplink_addresses

{:error, error} ->
{:error, error}
end
end

def client do
Expand Down
6 changes: 6 additions & 0 deletions lib/uplink/clients/lxd/instance.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ defmodule Uplink.Clients.LXD.Instance do
status
architecture
profiles
project
description
created_at
last_used_at
expanded_config
expanded_devices
state
)a

@required_attrs ~w(
Expand All @@ -34,13 +37,16 @@ defmodule Uplink.Clients.LXD.Instance do
field :status, :string
field :architecture, :string
field :profiles, {:array, :string}
field :project, :string
field :description, :string

field :created_at, :utc_datetime_usec
field :last_used_at, :utc_datetime_usec

field :expanded_config, :map
field :expanded_devices, :map

field :state, :map
end

def changeset(schema, params) do
Expand Down
32 changes: 15 additions & 17 deletions lib/uplink/clients/lxd/instance/manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,25 @@ defmodule Uplink.Clients.LXD.Instance.Manager do
alias Clients.LXD
alias LXD.Instance

def list do
LXD.client()
|> Lexdee.list_instances(query: [{:recursion, 1}, {"all-projects", true}])
|> case do
{:ok, %{body: instances}} ->
instances =
instances
|> Enum.map(fn instance ->
Instance.parse(instance)
end)
def list(options \\ []) do
project = Keyword.get(options, :project, nil)
recursion = Keyword.get(options, :recursion, 1)

instances

error ->
error
if recursion < 1 do
raise "recursion must be greater than 0 and less than 3 but got #{recursion}"
end
end

def list(project) do
project_query =
if project do
[project: project]
else
[{"all-projects", true}]
end

query = [{:recursion, recursion} | project_query]

LXD.client()
|> Lexdee.list_instances(query: [recursion: 1, project: project])
|> Lexdee.list_instances(query: query)
|> case do
{:ok, %{body: instances}} ->
instances =
Expand Down
23 changes: 23 additions & 0 deletions lib/uplink/clients/lxd/metric.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule Uplink.Clients.LXD.Metric do
use Ecto.Schema
import Ecto.Changeset

@primary_key false
embedded_schema do
field :instance, :string
field :label, :string
field :value, :string
end

def changeset(metric, params) do
metric
|> cast(params, [:instance, :type, :value])
|> validate_required([:instance, :type, :value])
end

def parse(params) do
%__MODULE__{}
|> changeset(params)
|> apply_action!(:insert)
end
end
Loading

0 comments on commit a778f51

Please sign in to comment.