Skip to content

Commit fbbc0ac

Browse files
committed
chore: forward-port release-1.2
For housekeeping alone, an exandra fork is needed in order to have access to `Repo.checkout` Signed-off-by: Francesco Noacco <francesco.noacco@secomind.com>
2 parents 1383091 + 8cc9b48 commit fbbc0ac

File tree

18 files changed

+173
-235
lines changed

18 files changed

+173
-235
lines changed

apps/astarte_housekeeping/Dockerfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ RUN apt-get update -y && \
5656
ARG BUILD_ENV=prod
5757

5858
COPY --from=builder --chown=nobody:nogroup /app/_build/$BUILD_ENV/rel/astarte_housekeeping .
59-
COPY --from=builder --chown=nobody:nogroup /app/entrypoint.sh .
6059

6160
# Change to non-root user
6261
USER nobody
6362

64-
ENTRYPOINT ["/bin/bash", "entrypoint.sh"]
65-
CMD ["start"]
63+
CMD ["./bin/astarte_housekeeping", "start"]

apps/astarte_housekeeping/entrypoint.sh

Lines changed: 0 additions & 7 deletions
This file was deleted.

apps/astarte_housekeeping/lib/astarte_housekeeping/application.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ defmodule Astarte.Housekeeping.Application do
2222

2323
alias Astarte.DataAccess.Config, as: DataAccessConfig
2424
alias Astarte.Housekeeping.Config
25+
alias Astarte.Housekeeping.ReleaseTasks
2526

2627
require Logger
2728

@@ -45,6 +46,7 @@ defmodule Astarte.Housekeeping.Application do
4546

4647
# Define workers and child supervisors to be supervised
4748
children = [
49+
ReleaseTasks,
4850
Astarte.HousekeepingWeb.Telemetry,
4951
Astarte.HousekeepingWeb.Endpoint
5052
]

apps/astarte_housekeeping/lib/astarte_housekeeping/migrator.ex

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818

1919
defmodule Astarte.Housekeeping.Migrator do
2020
@moduledoc false
21-
import Ecto.Query
2221

2322
alias Astarte.DataAccess.Consistency
2423
alias Astarte.DataAccess.CSystem
2524
alias Astarte.DataAccess.KvStore
2625
alias Astarte.DataAccess.Realms.Realm
27-
alias Astarte.DataAccess.Repo
2826
alias Astarte.Events.AMQP.Vhost
2927
alias Astarte.Housekeeping.Realms.Queries
3028
alias Astarte.Housekeeping.Realms.Realm, as: HKRealm
@@ -36,8 +34,7 @@ defmodule Astarte.Housekeeping.Migrator do
3634
def run_astarte_keyspace_migrations do
3735
_ = Logger.info("Starting to migrate Astarte keyspace.", tag: "astarte_migration_started")
3836

39-
with :ok <- ensure_astarte_kv_store(),
40-
{:ok, astarte_schema_version} <- get_astarte_schema_version() do
37+
with {:ok, astarte_schema_version} <- get_astarte_schema_version() do
4138
migrate_astarte_keyspace_from_version(astarte_schema_version)
4239
end
4340
end
@@ -88,50 +85,6 @@ defmodule Astarte.Housekeeping.Migrator do
8885
end
8986
end
9087

91-
defp ensure_astarte_kv_store do
92-
keyspace_name = Realm.astarte_keyspace_name()
93-
94-
query =
95-
from t in "system_schema.tables",
96-
where:
97-
t.keyspace_name == ^keyspace_name and
98-
t.table_name == "kv_store",
99-
select: t.table_name
100-
101-
consistency = Consistency.domain_model(:read)
102-
103-
case Repo.safe_fetch_one(query, consistency: consistency) do
104-
{:ok, _item} ->
105-
:ok
106-
107-
{:error, :not_found} ->
108-
create_astarte_kv_store()
109-
110-
{:error, reason} ->
111-
Logger.warning("Error checking Astarte kv_store existence: #{inspect(reason)}.",
112-
tag: "database_error"
113-
)
114-
115-
{:error, reason}
116-
end
117-
end
118-
119-
defp create_astarte_kv_store do
120-
query = """
121-
CREATE TABLE #{Realm.astarte_keyspace_name()}.kv_store (
122-
group varchar,
123-
key varchar,
124-
value blob,
125-
126-
PRIMARY KEY ((group), key)
127-
);
128-
"""
129-
130-
with {:ok, %{rows: nil, num_rows: 1}} <- CSystem.execute_schema_change(query) do
131-
:ok
132-
end
133-
end
134-
13588
defp get_astarte_schema_version do
13689
get_schema_version(Realm.astarte_keyspace_name())
13790
end

apps/astarte_housekeeping/lib/astarte_housekeeping/realms/queries.ex

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,8 @@ defmodule Astarte.Housekeeping.Realms.Queries do
11121112
end
11131113

11141114
def initialize_database do
1115+
Logger.info("Starting Astarte keyspace initialization")
1116+
11151117
with :ok <- create_astarte_keyspace(),
11161118
:ok <- create_realms_table(),
11171119
:ok <- create_astarte_kv_store(),
@@ -1179,25 +1181,21 @@ defmodule Astarte.Housekeeping.Realms.Queries do
11791181
opts = [consistency: consistency]
11801182

11811183
case Repo.query(tablets_query, [], opts) do
1182-
{:ok, %{num_rows: 1}} ->
1184+
{:ok, _} ->
1185+
Logger.info("Astarte keyspace initialized")
11831186
:ok
11841187

1185-
{:ok, res} ->
1186-
"Unexpected ok result from database while creating astarte keyspace: #{inspect(res)}"
1187-
|> Logger.warning()
1188-
1189-
{:error, :astarte_keyspace_creation_failed}
1188+
{:error, %Xandra.Error{reason: :already_exists}} ->
1189+
:ok
11901190

11911191
_ ->
11921192
case Repo.query(base_query, [], opts) do
1193-
{:ok, %{num_rows: 1}} ->
1193+
{:ok, _} ->
1194+
Logger.info("Astarte keyspace initialized")
11941195
:ok
11951196

1196-
{:ok, res} ->
1197-
"Unexpected ok result from database while creating astarte keyspace: #{inspect(res)}"
1198-
|> Logger.warning()
1199-
1200-
{:error, :astarte_keyspace_creation_failed}
1197+
{:error, %Xandra.Error{reason: :already_exists}} ->
1198+
:ok
12011199

12021200
error ->
12031201
error
@@ -1215,10 +1213,18 @@ defmodule Astarte.Housekeeping.Realms.Queries do
12151213
"""
12161214

12171215
consistency = Consistency.domain_model(:write)
1216+
opts = [consistency: consistency]
12181217

1219-
with {:ok, %{rows: nil, num_rows: 1}} <-
1220-
Repo.query(query, [], consistency: consistency) do
1221-
:ok
1218+
case Repo.query(query, [], opts) do
1219+
{:ok, _} ->
1220+
Logger.info("Created Astarte realms table")
1221+
:ok
1222+
1223+
{:error, %Xandra.Error{reason: :already_exists}} ->
1224+
:ok
1225+
1226+
error ->
1227+
error
12221228
end
12231229
end
12241230

@@ -1234,10 +1240,18 @@ defmodule Astarte.Housekeeping.Realms.Queries do
12341240
"""
12351241

12361242
consistency = Consistency.domain_model(:write)
1243+
opts = [consistency: consistency]
12371244

1238-
with {:ok, %{rows: nil, num_rows: 1}} <-
1239-
Repo.query(query, [], consistency: consistency) do
1240-
:ok
1245+
case Repo.query(query, [], opts) do
1246+
{:ok, _} ->
1247+
Logger.info("Initialized Astarte KV Store")
1248+
:ok
1249+
1250+
{:error, %Xandra.Error{reason: :already_exists}} ->
1251+
:ok
1252+
1253+
error ->
1254+
error
12411255
end
12421256
end
12431257

@@ -1260,19 +1274,4 @@ defmodule Astarte.Housekeeping.Realms.Queries do
12601274

12611275
KvStore.insert(kv_store_map, opts)
12621276
end
1263-
1264-
def astarte_keyspace_existing? do
1265-
keyspace_name = Realm.astarte_keyspace_name()
1266-
1267-
query =
1268-
from k in "system_schema.keyspaces",
1269-
where: k.keyspace_name == ^keyspace_name,
1270-
select: count()
1271-
1272-
consistency = Consistency.domain_model(:read)
1273-
1274-
with {:ok, count} <- Repo.safe_fetch_one(query, consistency: consistency) do
1275-
{:ok, count > 0}
1276-
end
1277-
end
12781277
end

apps/astarte_housekeeping/lib/astarte_housekeeping/release_task.ex

Lines changed: 19 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -18,116 +18,39 @@
1818

1919
defmodule Astarte.Housekeeping.ReleaseTasks do
2020
@moduledoc false
21-
alias Astarte.DataAccess
21+
22+
use Task, restart: :transient
23+
24+
alias Astarte.DataAccess.Repo
2225
alias Astarte.Housekeeping.Migrator
2326
alias Astarte.Housekeeping.Realms.Queries
2427

2528
require Logger
2629

27-
@start_apps [
28-
:ecto,
29-
:httpoison,
30-
:logger,
31-
:crypto,
32-
:ssl
33-
]
34-
35-
def init_database do
36-
:ok = start_services()
37-
38-
case wait_connection_and_check_astarte_keyspace() do
39-
{:ok, exists?} ->
40-
unless exists? do
41-
_ =
42-
Logger.info("Astarte keyspace not found, creating it",
43-
tag: "astarte_db_initialization_started"
44-
)
45-
46-
:ok = Queries.initialize_database()
47-
end
30+
def start_link(_init_arg), do: Task.start_link(__MODULE__, :ensure_migrated!, [])
4831

32+
def ensure_migrated! do
33+
case Repo.checkout(&do_ensure_migrated!/0) do
34+
:ok ->
4935
:ok
5036

51-
{:error, reason} ->
52-
_ =
53-
Logger.error("Can't check if Astarte keyspace exists: #{inspect(reason)}",
54-
tag: "astarte_db_initialization_failed"
55-
)
37+
error ->
38+
"Error during astarte initialization: #{inspect(error)}"
39+
|> Logger.error(tag: "astarte_db_migration_failed")
5640

41+
# Here we raise in order to notify the supervisor something went wrong
5742
raise "init_database failed"
5843
end
59-
60-
:ok = stop_services()
6144
end
6245

63-
def migrate do
64-
:ok = start_services()
65-
66-
Logger.info("Starting to migrate the database", tag: "astarte_db_migration_started")
46+
defp do_ensure_migrated! do
47+
with :ok <- Queries.initialize_database(),
48+
:ok <- Migrator.run_astarte_keyspace_migrations(),
49+
:ok <- Migrator.run_realms_migrations() do
50+
"Astarte database correctly initialized"
51+
|> Logger.info(tag: "astarte_db_initialization_finished")
6752

68-
case wait_connection_and_check_astarte_keyspace() do
69-
{:ok, true} ->
70-
with :ok <- Migrator.run_astarte_keyspace_migrations(),
71-
:ok <- Migrator.run_realms_migrations() do
72-
:ok
73-
else
74-
{:error, reason} ->
75-
Logger.error("Cannot migrate the database: #{inspect(reason)}",
76-
tag: "astarte_db_migration_failed"
77-
)
78-
79-
raise "migrate failed"
80-
end
81-
82-
:ok = stop_services()
83-
84-
{:ok, false} ->
85-
Logger.error("Cannot migrate the database, Astarte keyspace does not exist",
86-
tag: "astarte_db_migration_failed"
87-
)
88-
89-
raise "migrate failed"
53+
:ok
9054
end
9155
end
92-
93-
defp wait_connection_and_check_astarte_keyspace(retries \\ 60) do
94-
case Queries.astarte_keyspace_existing?() do
95-
{:ok, exists?} ->
96-
{:ok, exists?}
97-
98-
{:error, :database_connection_error} ->
99-
if retries > 0 do
100-
:timer.sleep(1000)
101-
wait_connection_and_check_astarte_keyspace(retries - 1)
102-
else
103-
{:error, :database_connection_error}
104-
end
105-
106-
{:error, reason} ->
107-
{:error, reason}
108-
end
109-
end
110-
111-
defp start_services do
112-
DataAccess.Config.validate!()
113-
Enum.each(@start_apps, &Application.ensure_all_started/1)
114-
115-
# Load astarte_data_access, without starting it. This makes the application env accessible.
116-
:ok = Application.ensure_loaded(:astarte_data_access)
117-
118-
_ = Logger.info("Starting Xandra connection to #{inspect(DataAccess.Config.xandra_nodes!())}")
119-
120-
{:ok, _} = Application.ensure_all_started(:astarte_data_access)
121-
122-
:ok
123-
end
124-
125-
defp stop_services do
126-
_ =
127-
Logger.info("Astarte database correctly initialized",
128-
tag: "astarte_db_initialization_finished"
129-
)
130-
131-
:init.stop()
132-
end
13356
end

apps/astarte_housekeeping/mix.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ defmodule Astarte.Housekeeping.Mixfile do
7979
{:plug_cowboy, "~> 2.1"},
8080
{:guardian, "~> 2.3.2"},
8181
{:excoveralls, "~> 0.15", only: :test},
82+
{:exandra, github: "noaccOS/exandra", branch: "push-zlnkysuyqwmt", override: true},
8283
{:pretty_log, "~> 0.1"},
8384
{:skogsra, "~> 2.5"},
8485
{:observer_cli, "~> 1.5"},

apps/astarte_housekeeping/mix.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
"dialyxir": {:hex, :dialyxir, "1.4.7", "dda948fcee52962e4b6c5b4b16b2d8fa7d50d8645bbae8b8685c3f9ecb7f5f4d", [:mix], [{:erlex, ">= 0.2.8", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "b34527202e6eb8cee198efec110996c25c5898f43a4094df157f8d28f27d9efe"},
2020
"ecto": {:hex, :ecto, "3.13.5", "9d4a69700183f33bf97208294768e561f5c7f1ecf417e0fa1006e4a91713a834", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "df9efebf70cf94142739ba357499661ef5dbb559ef902b68ea1f3c1fabce36de"},
2121
"ecto_morph": {:hex, :ecto_morph, "0.1.29", "bc0b915779636bd2d30c54cad6922b3cb40f85b1d4ad59bdffd3c788d9d1f972", [:mix], [{:ecto, ">= 3.0.3", [hex: :ecto, repo: "hexpm", optional: false]}], "hexpm", "814bed72e3d03b278c1dfb3fbc4da37f478a37518ee54f010c1ad9254f1ca0e3"},
22-
"ecto_sql": {:hex, :ecto_sql, "3.13.4", "b6e9d07557ddba62508a9ce4a484989a5bb5e9a048ae0e695f6d93f095c25d60", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.13.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.7", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.19 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "2b38cf0749ca4d1c5a8bcbff79bbe15446861ca12a61f9fba604486cb6b62a14"},
22+
"ecto_sql": {:hex, :ecto_sql, "3.13.5", "2f8282b2ad97bf0f0d3217ea0a6fff320ead9e2f8770f810141189d182dc304e", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.13.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.7", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.19 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "aa36751f4e6a2b56ae79efb0e088042e010ff4935fc8684e74c23b1f49e25fdc"},
2323
"elixir_uuid": {:hex, :elixir_uuid, "1.2.1", "dce506597acb7e6b0daeaff52ff6a9043f5919a4c3315abb4143f0b00378c097", [:mix], [], "hexpm", "f7eba2ea6c3555cea09706492716b0d87397b88946e6380898c2889d68585752"},
2424
"erlex": {:hex, :erlex, "0.2.8", "cd8116f20f3c0afe376d1e8d1f0ae2452337729f68be016ea544a72f767d9c12", [:mix], [], "hexpm", "9d66ff9fedf69e49dc3fd12831e12a8a37b76f8651dd21cd45fcf5561a8a7590"},
2525
"ex_rabbit_pool": {:git, "https://github.com/leductam/ex_rabbit_pool.git", "9951452ab51d36648b9a9d3373609e48d1379a0d", []},
26-
"exandra": {:hex, :exandra, "0.16.0", "848690b915c96a80f05c0bea782fd7b0fed7963ffb449f9022939fe6ade96e0c", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ecto, "~> 3.13", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.13", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:nimble_options, "~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:xandra, "~> 0.19.4", [hex: :xandra, repo: "hexpm", optional: false]}], "hexpm", "4b95adfdde42bd953bb80d083347ac69cd71a520213b4c2cd83cecd9b01bd919"},
26+
"exandra": {:git, "https://github.com/noaccOS/exandra.git", "e2a9d023ae9fc1c04a42a56585e9bfee6479fcc5", [branch: "push-zlnkysuyqwmt"]},
2727
"excoveralls": {:hex, :excoveralls, "0.18.5", "e229d0a65982613332ec30f07940038fe451a2e5b29bce2a5022165f0c9b157e", [:mix], [{:castore, "~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "523fe8a15603f86d64852aab2abe8ddbd78e68579c8525ae765facc5eae01562"},
2828
"expo": {:hex, :expo, "1.1.1", "4202e1d2ca6e2b3b63e02f69cfe0a404f77702b041d02b58597c00992b601db5", [:mix], [], "hexpm", "5fb308b9cb359ae200b7e23d37c76978673aa1b06e2b3075d814ce12c5811640"},
2929
"file_system": {:hex, :file_system, "1.1.1", "31864f4685b0148f25bd3fbef2b1228457c0c89024ad67f7a81a3ffbc0bbad3a", [:mix], [], "hexpm", "7a15ff97dfe526aeefb090a7a9d3d03aa907e100e262a0f8f7746b78f8f87a5d"},
@@ -60,7 +60,7 @@
6060
"skogsra": {:hex, :skogsra, "2.5.2", "d90e5e8425799c249c462ec0f66f16d98a11d224d0fad6222493a25beb368bc4", [:mix], [{:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: true]}, {:yamerl, "~> 0.10", [hex: :yamerl, repo: "hexpm", optional: true]}], "hexpm", "89843669d40dc3b0a5ac2ad1bab24d3f17bdf8364e9f700a0fa856208579ac36"},
6161
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"},
6262
"stream_data": {:hex, :stream_data, "1.2.0", "58dd3f9e88afe27dc38bef26fce0c84a9e7a96772b2925c7b32cd2435697a52b", [:mix], [], "hexpm", "eb5c546ee3466920314643edf68943a5b14b32d1da9fe01698dc92b73f89a9ed"},
63-
"telemetry": {:hex, :telemetry, "1.3.0", "fedebbae410d715cf8e7062c96a1ef32ec22e764197f70cda73d82778d61e7a2", [:rebar3], [], "hexpm", "7015fc8919dbe63764f4b4b87a95b7c0996bd539e0d499be6ec9d7f3875b79e6"},
63+
"telemetry": {:hex, :telemetry, "1.4.1", "ab6de178e2b29b58e8256b92b382ea3f590a47152ca3651ea857a6cae05ac423", [:rebar3], [], "hexpm", "2172e05a27531d3d31dd9782841065c50dd5c3c7699d95266b2edd54c2dafa1c"},
6464
"telemetry_metrics": {:hex, :telemetry_metrics, "1.1.0", "5bd5f3b5637e0abea0426b947e3ce5dd304f8b3bc6617039e2b5a008adc02f8f", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "e7b79e8ddfde70adb6db8a6623d1778ec66401f366e9a8f5dd0955c56bc8ce67"},
6565
"telemetry_metrics_prometheus_core": {:hex, :telemetry_metrics_prometheus_core, "1.2.1", "c9755987d7b959b557084e6990990cb96a50d6482c683fb9622a63837f3cd3d8", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:telemetry_metrics, "~> 0.6 or ~> 1.0", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}], "hexpm", "5e2c599da4983c4f88a33e9571f1458bf98b0cf6ba930f1dc3a6e8cf45d5afb6"},
6666
"telemetry_poller": {:hex, :telemetry_poller, "1.3.0", "d5c46420126b5ac2d72bc6580fb4f537d35e851cc0f8dbd571acf6d6e10f5ec7", [:rebar3], [{:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "51f18bed7128544a50f75897db9974436ea9bfba560420b646af27a9a9b35211"},

apps/astarte_housekeeping/test/astarte_housekeeping/migrator_test.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ defmodule Astarte.Housekeeping.MigratorTest do
8686
test "returns ok with missing capabilities" do
8787
new_realm = "realm#{System.unique_integer([:positive])}"
8888

89+
on_exit(fn ->
90+
Database.teardown_realm_keyspace(new_realm)
91+
end)
92+
8993
realm_migrations_path =
9094
Application.app_dir(
9195
:astarte_housekeeping,

0 commit comments

Comments
 (0)