Skip to content

Commit

Permalink
Merge pull request #6 from jaronoff97/cleanup-and-actions
Browse files Browse the repository at this point in the history
Much cleanup, new actions
  • Loading branch information
jaronoff97 authored Apr 3, 2024
2 parents dab01d6 + 4a5fb0b commit 6cbf066
Show file tree
Hide file tree
Showing 28 changed files with 380 additions and 159 deletions.
138 changes: 138 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
name: Elixir CI

on:
push:
branches: ["main"]
pull_request:
branches: ["main"]

env:
MIX_ENV: test

permissions:
contents: read

jobs:
analysis:
runs-on: ubuntu-latest
name: Static analysis for ${{matrix.otp}} / Elixir ${{matrix.elixir}}
strategy:
# Specify the OTP and Elixir versions to use when building
# and running the workflow steps.
matrix:
otp: ["26.0"] # Define the OTP version [required]
elixir: ["1.16"] # Define the elixir version [required]

steps:
- uses: actions/checkout@v4
- name: Set up Elixir
id: beam
uses: erlef/setup-beam@v1
with:
otp-version: ${{matrix.otp}}
elixir-version: ${{matrix.elixir}}

- name: Restore dependencies cache
uses: actions/cache@v4
env:
cache-name: cache-elixir-deps
with:
path: deps
key: ${{ runner.os }}-mix-${{ env.cache-name }}-${{ hashFiles('**/mix.lock') }}
restore-keys: |
${{ runner.os }}-mix-${{ env.cache-name }}-
# Step: Download project dependencies. If unchanged, uses
# the cached version.
- name: Install dependencies
run: mix deps.get

# Cache key based on Erlang/Elixir version and the mix.lock hash
- name: Restore PLT cache
id: plt_cache
uses: actions/cache/restore@v3
with:
key: |
plt-${{ runner.os }}-${{ steps.beam.outputs.otp-version }}-${{ steps.beam.outputs.elixir-version }}-${{ hashFiles('**/mix.lock') }}
restore-keys: |
plt-${{ runner.os }}-${{ steps.beam.outputs.otp-version }}-${{ steps.beam.outputs.elixir-version }}-
path: |
priv/plts
# Create PLTs if no cache was found
- name: Create PLTs
if: steps.plt_cache.outputs.cache-hit != 'true'
run: mix dialyzer --plt

# By default, the GitHub Cache action will only save the cache if all steps in the job succeed,
# so we separate the cache restore and save steps in case running dialyzer fails.
- name: Save PLT cache
id: plt_cache_save
uses: actions/cache/save@v3
if: steps.plt_cache.outputs.cache-hit != 'true'
with:
key: |
plt-${{ runner.os }}-${{ steps.beam.outputs.otp-version }}-${{ steps.beam.outputs.elixir-version }}-${{ hashFiles('**/mix.lock') }}
path: |
priv/plts
- name: Run dialyzer
run: mix dialyzer --format github
test:
runs-on: ubuntu-latest
name: Test on OTP ${{matrix.otp}} / Elixir ${{matrix.elixir}}
strategy:
# Specify the OTP and Elixir versions to use when building
# and running the workflow steps.
matrix:
otp: ["26.0"] # Define the OTP version [required]
elixir: ["1.16"] # Define the elixir version [required]

steps:
- uses: actions/checkout@v4
- name: Set up Elixir
uses: erlef/setup-beam@v1
with:
otp-version: ${{matrix.otp}}
elixir-version: ${{matrix.elixir}}
- name: Restore dependencies cache
uses: actions/cache@v4
env:
cache-name: cache-elixir-deps
with:
path: deps
key: ${{ runner.os }}-mix-${{ env.cache-name }}-${{ hashFiles('**/mix.lock') }}
restore-keys: |
${{ runner.os }}-mix-${{ env.cache-name }}-
- name: Cache compiled build
id: cache-build
uses: actions/cache@v4
env:
cache-name: cache-compiled-build
with:
path: _build
key: ${{ runner.os }}-mix-${{ env.cache-name }}-${{ hashFiles('**/mix.lock') }}
restore-keys: |
${{ runner.os }}-mix-${{ env.cache-name }}-
${{ runner.os }}-mix-
# Step: Download project dependencies. If unchanged, uses
# the cached version.
- name: Install dependencies
run: mix deps.get

# Step: Compile the project treating any warnings as errors.
# Customize this step if a different behavior is desired.
- name: Compiles without warnings
run: mix compile --warnings-as-errors

# Step: Check that the checked in code has already been formatted.
# This step fails if something was found unformatted.
# Customize this step as desired.
- name: Check Formatting
run: mix format --check-formatted

# Step: Execute the tests.
- name: Run tests
run: mix test
50 changes: 50 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Create and publish a Docker image

# Configures this workflow to run every time a change is pushed to the branch called `release`.
on:
push:
branches: ["main"]

# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds.
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu.
jobs:
build-and-push-image:
runs-on: ubuntu-latest
# Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job.
permissions:
contents: read
packages: write
#
steps:
- name: Checkout repository
uses: actions/checkout@v4

- uses: docker/setup-qemu-action@v3
- uses: docker/setup-buildx-action@v3
# Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here.
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels.
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v3
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/config/*.secret.exs
.elixir_ls/

/priv/plts/*.plt
/priv/plts/*.plt.hash

# The directory Mix will write compiled artifacts to.
/_build/

Expand Down
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ARG RUNNER_IMAGE="debian:${DEBIAN_VERSION}"
FROM ${BUILDER_IMAGE} as builder

# install build dependencies
RUN apt-get update -y && apt-get install -y build-essential git \
RUN apt-get update -y && apt-get install -y build-essential nodejs npm git \
&& apt-get clean && rm -f /var/lib/apt/lists/*_*

# prepare build dir
Expand Down Expand Up @@ -52,6 +52,7 @@ COPY lib lib
COPY assets assets

# compile assets
RUN mix npm.setup
RUN mix assets.deploy

# Compile the release
Expand All @@ -68,8 +69,8 @@ RUN mix release
FROM ${RUNNER_IMAGE}

RUN apt-get update -y && \
apt-get install -y libstdc++6 openssl libncurses5 locales ca-certificates \
&& apt-get clean && rm -f /var/lib/apt/lists/*_*
apt-get install -y libstdc++6 openssl libncurses5 locales ca-certificates \
&& apt-get clean && rm -f /var/lib/apt/lists/*_*

# Set the locale
RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen
Expand Down
1 change: 0 additions & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,3 @@ config :phoenix, :plug_init_mode, :runtime

# Include HEEx debug annotations as HTML comments in rendered markup
config :phoenix_live_view, :debug_heex_annotations, true

1 change: 0 additions & 1 deletion config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import Config
# before starting your production server.
config :tails, TailsWeb.Endpoint, cache_static_manifest: "priv/static/cache_manifest.json"


# Do not print debug messages in production
config :logger, level: :info

Expand Down
2 changes: 0 additions & 2 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ if config_env() == :prod do
host = System.get_env("PHX_HOST") || "example.com"
port = String.to_integer(System.get_env("PORT") || "4000")


config :tails, TailsWeb.UserSocket, check_origin: :conn

config :tails, TailsWeb.Endpoint,
Expand Down Expand Up @@ -83,5 +82,4 @@ if config_env() == :prod do
# force_ssl: [hsts: true]
#
# Check `Plug.SSL` for all available options in `force_ssl`.

end
2 changes: 0 additions & 2 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ config :tails, TailsWeb.Endpoint,
secret_key_base: "jL+jAXQfWvILYEYDuNjHpVKlhClWiyXt/KRQy8cySo0Cls7hFcgiJ30REtH4JmAu",
server: false



# Print only warnings and errors during test
config :logger, level: :warning

Expand Down
24 changes: 9 additions & 15 deletions lib/tails/agents.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,28 @@ defmodule Tails.Agents do
Phoenix.PubSub.subscribe(Tails.PubSub, "agents:" <> agent_id)
end

defp broadcast({:error, _reason} = error, _event), do: error

@spec broadcast({:ok, map}, atom) :: {:ok, map}
defp broadcast({:ok, agent}, event) do
Phoenix.PubSub.broadcast(Tails.PubSub, "agents", {event, agent})
Phoenix.PubSub.broadcast(Tails.PubSub, "agents:" <> agent.id, {event, agent})
:ok = Phoenix.PubSub.broadcast(Tails.PubSub, "agents", {event, agent})
:ok = Phoenix.PubSub.broadcast(Tails.PubSub, "agents:" <> agent.id, {event, agent})
{:ok, agent}
end

def get_agent(_id), do: nil

def request_latest_config do
# IO.puts("requesting!!!")
Phoenix.PubSub.broadcast(Tails.PubSub, "agents", {:request_config, %{}})
end
@spec request_latest_config() :: :ok | {:error, term()}
def request_latest_config,
do: Phoenix.PubSub.broadcast(Tails.PubSub, "agents", {:request_config, %{}})

@doc """
Creates a agent.
## Examples
iex> create_agent(%{field: value})
{:ok, %Agent{}}
iex> create_agent(%{field: bad_value})
{:error, %Ecto.Changeset{}}
{:ok, %{}}
"""
def create_agent(attrs \\ %{}) do
@spec create_agent(map) :: {:ok, map}
def create_agent(attrs) do
broadcast({:ok, attrs}, :agent_created)
end

Expand Down
17 changes: 13 additions & 4 deletions lib/tails/remotetapclient.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,20 @@ defmodule Tails.RemoteTapClient do
# IO.puts("Received Message -- Message: #{inspect(msg)}")

case Jason.decode(msg) do
{:ok, parsed} -> Tails.Telemetry.new_message(parsed)
{:error, err} -> IO.puts("error: #{err}")
end
{:ok, parsed} ->
case Tails.Telemetry.new_message(parsed) do
{:ok, _message} ->
{:ok, state}

{:ok, state}
{:error, reason} ->
IO.puts("failed to create new message: #{inspect(reason)}")
{:close, state}
end

{:error, reason} ->
IO.puts("failed to decode message: #{inspect(reason)}")
{:close, state}
end
end

def handle_cast({:send, {type, msg} = frame}, state) do
Expand Down
9 changes: 7 additions & 2 deletions lib/tails/telemetry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ defmodule Tails.Telemetry do
end

defp broadcast({:ok, message}, event) do
Phoenix.PubSub.broadcast(Tails.PubSub, @channel, {event, message})
{:ok, message}
case Phoenix.PubSub.broadcast(Tails.PubSub, @channel, {event, message}) do
:ok ->
{:ok, message}

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

def new_message(data) do
Expand Down
Loading

0 comments on commit 6cbf066

Please sign in to comment.