-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
523 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
name: Publish | ||
|
||
on: | ||
push: | ||
tags: | ||
- v*.*.* | ||
|
||
jobs: | ||
publish: | ||
name: Publish to hex | ||
runs-on: ubuntu-latest | ||
environment: Release | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: erlef/setup-beam@v1 | ||
with: | ||
otp-version: "26" | ||
elixir-version: "1.15.7" | ||
- uses: cucumber/[email protected] | ||
with: | ||
hex-api-key: ${{ secrets.HEX_API_KEY }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,5 @@ icepak-*.tar | |
|
||
# Temporary files, for example, from tests. | ||
/tmp/ | ||
|
||
.envrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
erlang 26.2.1 | ||
elixir 1.15.7-otp-26 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import Config | ||
|
||
import_config "#{config_env()}.exs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import Config | ||
|
||
config :icepak, :env, :dev |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import Config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import Config | ||
|
||
config :icepak, :env, :test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
defmodule Icepak.CLI do | ||
@commands %{ | ||
"push" => :push | ||
} | ||
|
||
@switches %{ | ||
push: [ | ||
switches: [ | ||
path: :string, | ||
serial: :string, | ||
os: :string, | ||
arch: :string, | ||
release: :string, | ||
variant: :string | ||
] | ||
] | ||
} | ||
|
||
require Logger | ||
|
||
def main(args \\ []) do | ||
command = List.first(args) | ||
call = Map.get(@commands, command) | ||
|
||
if call do | ||
switches = Map.get(@switches, command, switches: []) | ||
|
||
{options, _, _} = OptionParser.parse(args, switches) | ||
|
||
apply(Icepak, call, [options]) | ||
else | ||
IO.puts(""" | ||
Unknown command, please use one of the following: | ||
- push - will push the built image | ||
""") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
defmodule Icepak.Item do | ||
@derive Jason.Encoder | ||
|
||
defstruct name: "", | ||
file_type: "", | ||
size: 0, | ||
hash: "", | ||
path: "", | ||
source: nil, | ||
is_metadata: false, | ||
combined_hashes: [] | ||
|
||
@combinations %{ | ||
"combined_squashfs_sha256" => ["incus.tar.xz", "rootfs.squashfs"] | ||
} | ||
|
||
defmodule Hash do | ||
defstruct [:name, :hash] | ||
|
||
@type t :: %__MODULE__{ | ||
name: String.t(), | ||
hash: String.t() | ||
} | ||
end | ||
|
||
@type t :: %__MODULE__{ | ||
name: String.t(), | ||
file_type: String.t(), | ||
size: integer(), | ||
hash: String.t(), | ||
path: String.t(), | ||
source: String.t() | nil, | ||
is_metadata: boolean(), | ||
combined_hashes: [ | ||
Hash.t() | ||
] | ||
} | ||
|
||
def prepare("incus.tar.xz" = file, %{base_path: base_path, storage_path: storage_path}) do | ||
hash_ref = :crypto.hash_init(:sha256) | ||
full_path = Path.join(base_path, file) | ||
%{size: size} = File.stat!(full_path) | ||
|
||
storage_path = Path.join(storage_path, file) | ||
|
||
hash = | ||
full_path | ||
|> calculate_hash(hash_ref) | ||
|> finalize_hash() | ||
|
||
combined_hashes = | ||
Enum.map(@combinations, fn {key, values} -> | ||
files = Enum.map(values, fn f -> Path.join(base_path, f) end) | ||
|
||
%{ | ||
name: key, | ||
hash: | ||
files | ||
|> Enum.reduce(hash_ref, &calculate_hash/2) | ||
|> finalize_hash() | ||
} | ||
end) | ||
|
||
[ | ||
%__MODULE__{ | ||
name: file, | ||
file_type: "incus.tar.xz", | ||
size: size, | ||
hash: hash, | ||
path: storage_path, | ||
source: full_path, | ||
is_metadata: true, | ||
combined_hashes: combined_hashes | ||
}, | ||
%__MODULE__{ | ||
name: "lxd.tar.xz", | ||
file_type: "lxd.tar.xz", | ||
size: size, | ||
hash: hash, | ||
path: storage_path, | ||
is_metadata: true, | ||
combined_hashes: combined_hashes | ||
} | ||
] | ||
end | ||
|
||
def prepare("rootfs.squashfs" = file, %{base_path: base_path, storage_path: storage_path}) do | ||
hash_ref = :crypto.hash_init(:sha256) | ||
full_path = Path.join(base_path, file) | ||
%{size: size} = File.stat!(full_path) | ||
|
||
storage_path = Path.join(storage_path, file) | ||
|
||
hash = | ||
full_path | ||
|> calculate_hash(hash_ref) | ||
|> finalize_hash() | ||
|
||
[ | ||
%__MODULE__{ | ||
name: "root.squashfs", | ||
file_type: "squashfs", | ||
size: size, | ||
hash: hash, | ||
path: storage_path, | ||
source: full_path | ||
} | ||
] | ||
end | ||
|
||
defp calculate_hash(file, hash_ref) do | ||
file | ||
|> File.stream!([], 2048) | ||
|> Enum.reduce(hash_ref, fn chunk, prev_ref -> | ||
:crypto.hash_update(prev_ref, chunk) | ||
end) | ||
end | ||
|
||
defp finalize_hash(hash) do | ||
hash | ||
|> :crypto.hash_final() | ||
|> Base.encode16() | ||
|> String.downcase() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
defmodule Icepak.Polar do | ||
require Logger | ||
|
||
def get_product(client, key) do | ||
key = Base.url_encode64(key) | ||
|
||
Req.get!(client, url: "/publish/products/#{key}") | ||
end | ||
|
||
def create_version(client, product_id, version_params) do | ||
Req.post!(client, | ||
url: "/publish/products/#{product_id}/versions", | ||
json: %{version: version_params} | ||
) | ||
end | ||
|
||
def get_storage(client) do | ||
Req.get!(client, url: "/publish/storage") | ||
end | ||
|
||
def authenticate do | ||
auth_token = System.get_env("POLAR_AUTH_TOKEN") | ||
|
||
body = %{ | ||
user: %{ | ||
password: auth_token | ||
} | ||
} | ||
|
||
client = client() | ||
|
||
client | ||
|> Req.update(url: "/publish/sessions", json: body) | ||
|> Req.post() | ||
|> case do | ||
{:ok, %{body: %{"data" => %{"token" => session_token}}}} -> | ||
Logger.info("[Polar] Authenticated") | ||
|
||
Req.update(client, headers: [{"authorization", session_token}]) | ||
|
||
_ -> | ||
raise "Failed to authenticate with Polar" | ||
end | ||
end | ||
|
||
def client do | ||
endpoint = System.get_env("POLAR_ENDPOINT", "https://images.opsmaru.com") | ||
|
||
Req.new(base_url: endpoint, finch: Icepak.Finch) | ||
end | ||
end |
Oops, something went wrong.